Home | History | Annotate | Download | only in storage
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_EXTENSIONS_API_STORAGE_SYNCABLE_SETTINGS_STORAGE_H_
      6 #define CHROME_BROWSER_EXTENSIONS_API_STORAGE_SYNCABLE_SETTINGS_STORAGE_H_
      7 
      8 #include "base/compiler_specific.h"
      9 #include "base/memory/ref_counted.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/observer_list_threadsafe.h"
     12 #include "base/values.h"
     13 #include "chrome/browser/extensions/api/storage/setting_sync_data.h"
     14 #include "chrome/browser/extensions/api/storage/settings_observer.h"
     15 #include "chrome/browser/value_store/value_store.h"
     16 #include "sync/api/sync_change.h"
     17 #include "sync/api/syncable_service.h"
     18 
     19 namespace extensions {
     20 
     21 class SettingsSyncProcessor;
     22 
     23 // Decorates a ValueStore with sync behaviour.
     24 class SyncableSettingsStorage : public ValueStore {
     25  public:
     26   SyncableSettingsStorage(
     27       const scoped_refptr<SettingsObserverList>& observers,
     28       const std::string& extension_id,
     29       // Ownership taken.
     30       ValueStore* delegate);
     31 
     32   virtual ~SyncableSettingsStorage();
     33 
     34   // ValueStore implementation.
     35   virtual size_t GetBytesInUse(const std::string& key) OVERRIDE;
     36   virtual size_t GetBytesInUse(const std::vector<std::string>& keys) OVERRIDE;
     37   virtual size_t GetBytesInUse() OVERRIDE;
     38   virtual ReadResult Get(const std::string& key) OVERRIDE;
     39   virtual ReadResult Get(const std::vector<std::string>& keys) OVERRIDE;
     40   virtual ReadResult Get() OVERRIDE;
     41   virtual WriteResult Set(
     42       WriteOptions options,
     43       const std::string& key,
     44       const Value& value) OVERRIDE;
     45   virtual WriteResult Set(
     46       WriteOptions options, const base::DictionaryValue& values) OVERRIDE;
     47   virtual WriteResult Remove(const std::string& key) OVERRIDE;
     48   virtual WriteResult Remove(const std::vector<std::string>& keys) OVERRIDE;
     49   virtual WriteResult Clear() OVERRIDE;
     50 
     51   // Sync-related methods, analogous to those on SyncableService (handled by
     52   // ExtensionSettings), but with looser guarantees about when the methods
     53   // can be called.
     54 
     55   // Must only be called if sync isn't already active.
     56   syncer::SyncError StartSyncing(
     57       const base::DictionaryValue& sync_state,
     58       scoped_ptr<SettingsSyncProcessor> sync_processor);
     59 
     60   // May be called at any time (idempotent).
     61   void StopSyncing();
     62 
     63   // May be called at any time; changes will be ignored if sync isn't active.
     64   syncer::SyncError ProcessSyncChanges(const SettingSyncDataList& sync_changes);
     65 
     66  private:
     67   // Sends the changes from |result| to sync if it's enabled.
     68   void SyncResultIfEnabled(const ValueStore::WriteResult& result);
     69 
     70   // Sends all local settings to sync (synced settings assumed to be empty).
     71   syncer::SyncError SendLocalSettingsToSync(
     72       const base::DictionaryValue& settings);
     73 
     74   // Overwrites local state with sync state.
     75   syncer::SyncError OverwriteLocalSettingsWithSync(
     76       const base::DictionaryValue& sync_state,
     77       const base::DictionaryValue& settings);
     78 
     79   // Called when an Add/Update/Remove comes from sync.  Ownership of Value*s
     80   // are taken.
     81   syncer::SyncError OnSyncAdd(
     82       const std::string& key,
     83       Value* new_value,
     84       ValueStoreChangeList* changes);
     85   syncer::SyncError OnSyncUpdate(
     86       const std::string& key,
     87       Value* old_value,
     88       Value* new_value,
     89       ValueStoreChangeList* changes);
     90   syncer::SyncError OnSyncDelete(
     91       const std::string& key,
     92       Value* old_value,
     93       ValueStoreChangeList* changes);
     94 
     95   // List of observers to settings changes.
     96   const scoped_refptr<SettingsObserverList> observers_;
     97 
     98   // Id of the extension these settings are for.
     99   std::string const extension_id_;
    100 
    101   // Storage area to sync.
    102   const scoped_ptr<ValueStore> delegate_;
    103 
    104   // Object which sends changes to sync.
    105   scoped_ptr<SettingsSyncProcessor> sync_processor_;
    106 
    107   DISALLOW_COPY_AND_ASSIGN(SyncableSettingsStorage);
    108 };
    109 
    110 }  // namespace extensions
    111 
    112 #endif  // CHROME_BROWSER_EXTENSIONS_API_STORAGE_SYNCABLE_SETTINGS_STORAGE_H_
    113