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