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 // Starts syncing this storage area. Must only be called if sync isn't
60 // |sync_state| is the current state of the extension settings in sync.
61 // |sync_processor| is used to write out any changes.
62 // Returns any error when trying to sync, or an empty error on success.
63 syncer::SyncError
StartSyncing(
64 scoped_ptr
<base::DictionaryValue
> sync_state
,
65 scoped_ptr
<SettingsSyncProcessor
> sync_processor
);
67 // Stops syncing this storage area. May be called at any time (idempotent).
70 // Pushes a list of sync changes into this storage area. May be called at any
71 // time, changes will be ignored if sync isn't active.
72 // Returns any error when trying to sync, or an empty error on success.
73 syncer::SyncError
ProcessSyncChanges(
74 scoped_ptr
<SettingSyncDataList
> sync_changes
);
77 // Sends the changes from |result| to sync if it's enabled.
78 void SyncResultIfEnabled(const ValueStore::WriteResult
& result
);
80 // Sends all local settings to sync. This assumes that there are no settings
82 // Returns any error when trying to sync, or an empty error on success.
83 syncer::SyncError
SendLocalSettingsToSync(
84 scoped_ptr
<base::DictionaryValue
> local_state
);
86 // Overwrites local state with sync state.
87 // Returns any error when trying to sync, or an empty error on success.
88 syncer::SyncError
OverwriteLocalSettingsWithSync(
89 scoped_ptr
<base::DictionaryValue
> sync_state
,
90 scoped_ptr
<base::DictionaryValue
> local_state
);
92 // Called when an Add/Update/Remove comes from sync. Ownership of Value*s
94 syncer::SyncError
OnSyncAdd(
95 const std::string
& key
,
96 base::Value
* new_value
,
97 ValueStoreChangeList
* changes
);
98 syncer::SyncError
OnSyncUpdate(
99 const std::string
& key
,
100 base::Value
* old_value
,
101 base::Value
* new_value
,
102 ValueStoreChangeList
* changes
);
103 syncer::SyncError
OnSyncDelete(
104 const std::string
& key
,
105 base::Value
* old_value
,
106 ValueStoreChangeList
* changes
);
108 // List of observers to settings changes.
109 const scoped_refptr
<SettingsObserverList
> observers_
;
111 // Id of the extension these settings are for.
112 std::string
const extension_id_
;
114 // Storage area to sync.
115 const scoped_ptr
<ValueStore
> delegate_
;
117 // Object which sends changes to sync.
118 scoped_ptr
<SettingsSyncProcessor
> sync_processor_
;
120 const syncer::ModelType sync_type_
;
121 const syncer::SyncableService::StartSyncFlare flare_
;
123 DISALLOW_COPY_AND_ASSIGN(SyncableSettingsStorage
);
126 } // namespace extensions
128 #endif // CHROME_BROWSER_EXTENSIONS_API_STORAGE_SYNCABLE_SETTINGS_STORAGE_H_