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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_STORAGE_SYNCABLE_SETTINGS_STORAGE_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_STORAGE_SYNCABLE_SETTINGS_STORAGE_H_
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"
19 namespace extensions
{
21 class SettingsSyncProcessor
;
23 // Decorates a ValueStore with sync behaviour.
24 class SyncableSettingsStorage
: public ValueStore
{
26 SyncableSettingsStorage(
27 const scoped_refptr
<SettingsObserverList
>& observers
,
28 const std::string
& extension_id
,
31 syncer::ModelType sync_type
,
32 const syncer::SyncableService::StartSyncFlare
& flare
);
34 virtual ~SyncableSettingsStorage();
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(
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
;
55 // Sync-related methods, analogous to those on SyncableService (handled by
56 // ExtensionSettings), but with looser guarantees about when the methods
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
);
64 // May be called at any time (idempotent).
67 // May be called at any time; changes will be ignored if sync isn't active.
68 syncer::SyncError
ProcessSyncChanges(const SettingSyncDataList
& sync_changes
);
71 // Sends the changes from |result| to sync if it's enabled.
72 void SyncResultIfEnabled(const ValueStore::WriteResult
& result
);
74 // Sends all local settings to sync (synced settings assumed to be empty).
75 syncer::SyncError
SendLocalSettingsToSync(
76 const base::DictionaryValue
& settings
);
78 // Overwrites local state with sync state.
79 syncer::SyncError
OverwriteLocalSettingsWithSync(
80 const base::DictionaryValue
& sync_state
,
81 const base::DictionaryValue
& settings
);
83 // Called when an Add/Update/Remove comes from sync. Ownership of Value*s
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
);
99 // List of observers to settings changes.
100 const scoped_refptr
<SettingsObserverList
> observers_
;
102 // Id of the extension these settings are for.
103 std::string
const extension_id_
;
105 // Storage area to sync.
106 const scoped_ptr
<ValueStore
> delegate_
;
108 // Object which sends changes to sync.
109 scoped_ptr
<SettingsSyncProcessor
> sync_processor_
;
111 const syncer::ModelType sync_type_
;
112 const syncer::SyncableService::StartSyncFlare flare_
;
114 DISALLOW_COPY_AND_ASSIGN(SyncableSettingsStorage
);
117 } // namespace extensions
119 #endif // CHROME_BROWSER_EXTENSIONS_API_STORAGE_SYNCABLE_SETTINGS_STORAGE_H_