• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "extensions/browser/api/storage/settings_observer.h"
15 #include "extensions/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       syncer::ModelType sync_type,
32       const syncer::SyncableService::StartSyncFlare& flare);
33 
34   virtual ~SyncableSettingsStorage();
35 
36   // ValueStore implementation.
37   virtual size_t GetBytesInUse(const std::string& key) OVERRIDE;
38   virtual size_t GetBytesInUse(const std::vector<std::string>& keys) OVERRIDE;
39   virtual size_t GetBytesInUse() OVERRIDE;
40   virtual ReadResult Get(const std::string& key) OVERRIDE;
41   virtual ReadResult Get(const std::vector<std::string>& keys) OVERRIDE;
42   virtual ReadResult Get() OVERRIDE;
43   virtual WriteResult Set(
44       WriteOptions options,
45       const std::string& key,
46       const base::Value& value) OVERRIDE;
47   virtual WriteResult Set(
48       WriteOptions options, const base::DictionaryValue& values) OVERRIDE;
49   virtual WriteResult Remove(const std::string& key) OVERRIDE;
50   virtual WriteResult Remove(const std::vector<std::string>& keys) OVERRIDE;
51   virtual WriteResult Clear() OVERRIDE;
52   virtual bool Restore() OVERRIDE;
53   virtual bool RestoreKey(const std::string& key) OVERRIDE;
54 
55   // Sync-related methods, analogous to those on SyncableService (handled by
56   // ExtensionSettings), but with looser guarantees about when the methods
57   // can be called.
58 
59   // Must only be called if sync isn't already active.
60   syncer::SyncError StartSyncing(
61       const base::DictionaryValue& sync_state,
62       scoped_ptr<SettingsSyncProcessor> sync_processor);
63 
64   // May be called at any time (idempotent).
65   void StopSyncing();
66 
67   // May be called at any time; changes will be ignored if sync isn't active.
68   syncer::SyncError ProcessSyncChanges(const SettingSyncDataList& sync_changes);
69 
70  private:
71   // Sends the changes from |result| to sync if it's enabled.
72   void SyncResultIfEnabled(const ValueStore::WriteResult& result);
73 
74   // Sends all local settings to sync (synced settings assumed to be empty).
75   syncer::SyncError SendLocalSettingsToSync(
76       const base::DictionaryValue& settings);
77 
78   // Overwrites local state with sync state.
79   syncer::SyncError OverwriteLocalSettingsWithSync(
80       const base::DictionaryValue& sync_state,
81       const base::DictionaryValue& settings);
82 
83   // Called when an Add/Update/Remove comes from sync.  Ownership of Value*s
84   // are taken.
85   syncer::SyncError OnSyncAdd(
86       const std::string& key,
87       base::Value* new_value,
88       ValueStoreChangeList* changes);
89   syncer::SyncError OnSyncUpdate(
90       const std::string& key,
91       base::Value* old_value,
92       base::Value* new_value,
93       ValueStoreChangeList* changes);
94   syncer::SyncError OnSyncDelete(
95       const std::string& key,
96       base::Value* old_value,
97       ValueStoreChangeList* changes);
98 
99   // List of observers to settings changes.
100   const scoped_refptr<SettingsObserverList> observers_;
101 
102   // Id of the extension these settings are for.
103   std::string const extension_id_;
104 
105   // Storage area to sync.
106   const scoped_ptr<ValueStore> delegate_;
107 
108   // Object which sends changes to sync.
109   scoped_ptr<SettingsSyncProcessor> sync_processor_;
110 
111   const syncer::ModelType sync_type_;
112   const syncer::SyncableService::StartSyncFlare flare_;
113 
114   DISALLOW_COPY_AND_ASSIGN(SyncableSettingsStorage);
115 };
116 
117 }  // namespace extensions
118 
119 #endif  // CHROME_BROWSER_EXTENSIONS_API_STORAGE_SYNCABLE_SETTINGS_STORAGE_H_
120