1 // Copyright 2014 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_SYNC_STORAGE_BACKEND_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_STORAGE_SYNC_STORAGE_BACKEND_H_
12 #include "base/compiler_specific.h"
13 #include "base/files/file_path.h"
14 #include "base/memory/linked_ptr.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "extensions/browser/api/storage/settings_observer.h"
18 #include "extensions/browser/api/storage/settings_storage_factory.h"
19 #include "extensions/browser/api/storage/settings_storage_quota_enforcer.h"
20 #include "sync/api/syncable_service.h"
23 class SyncErrorFactory
;
26 namespace extensions
{
28 class SettingsSyncProcessor
;
29 class SyncableSettingsStorage
;
31 // Manages ValueStore objects for extensions, including routing
32 // changes from sync to them.
33 // Lives entirely on the FILE thread.
34 class SyncStorageBackend
: public syncer::SyncableService
{
36 // |storage_factory| is use to create leveldb storage areas.
37 // |base_path| is the base of the extension settings directory, so the
38 // databases will be at base_path/extension_id.
39 // |observers| is the list of observers to settings changes.
41 const scoped_refptr
<SettingsStorageFactory
>& storage_factory
,
42 const base::FilePath
& base_path
,
43 const SettingsStorageQuotaEnforcer::Limits
& quota
,
44 const scoped_refptr
<SettingsObserverList
>& observers
,
45 syncer::ModelType sync_type
,
46 const syncer::SyncableService::StartSyncFlare
& flare
);
48 ~SyncStorageBackend() override
;
50 virtual ValueStore
* GetStorage(const std::string
& extension_id
);
51 virtual void DeleteStorage(const std::string
& extension_id
);
53 // syncer::SyncableService implementation.
54 syncer::SyncDataList
GetAllSyncData(syncer::ModelType type
) const override
;
55 syncer::SyncMergeResult
MergeDataAndStartSyncing(
56 syncer::ModelType type
,
57 const syncer::SyncDataList
& initial_sync_data
,
58 scoped_ptr
<syncer::SyncChangeProcessor
> sync_processor
,
59 scoped_ptr
<syncer::SyncErrorFactory
> sync_error_factory
) override
;
60 syncer::SyncError
ProcessSyncChanges(
61 const tracked_objects::Location
& from_here
,
62 const syncer::SyncChangeList
& change_list
) override
;
63 void StopSyncing(syncer::ModelType type
) override
;
66 // Gets a weak reference to the storage area for a given extension,
67 // initializing sync with some initial data if sync enabled.
68 SyncableSettingsStorage
* GetOrCreateStorageWithSyncData(
69 const std::string
& extension_id
,
70 scoped_ptr
<base::DictionaryValue
> sync_data
) const;
72 // Gets all extension IDs known to extension settings. This may not be all
73 // installed extensions.
74 std::set
<std::string
> GetKnownExtensionIDs() const;
76 // Creates a new SettingsSyncProcessor for an extension.
77 scoped_ptr
<SettingsSyncProcessor
> CreateSettingsSyncProcessor(
78 const std::string
& extension_id
) const;
80 // The Factory to use for creating new ValueStores.
81 const scoped_refptr
<SettingsStorageFactory
> storage_factory_
;
83 // The base file path to use when creating new ValueStores.
84 const base::FilePath base_path_
;
86 // Quota limits (see SettingsStorageQuotaEnforcer).
87 const SettingsStorageQuotaEnforcer::Limits quota_
;
89 // The list of observers to settings changes.
90 const scoped_refptr
<SettingsObserverList
> observers_
;
92 // A cache of ValueStore objects that have already been created.
93 // Ensure that there is only ever one created per extension.
94 typedef std::map
<std::string
, linked_ptr
<SyncableSettingsStorage
> >
96 mutable StorageObjMap storage_objs_
;
98 // Current sync model type. Either EXTENSION_SETTINGS or APP_SETTINGS.
99 syncer::ModelType sync_type_
;
101 // Current sync processor, if any.
102 scoped_ptr
<syncer::SyncChangeProcessor
> sync_processor_
;
104 // Current sync error handler if any.
105 scoped_ptr
<syncer::SyncErrorFactory
> sync_error_factory_
;
107 syncer::SyncableService::StartSyncFlare flare_
;
109 DISALLOW_COPY_AND_ASSIGN(SyncStorageBackend
);
112 } // namespace extensions
114 #endif // CHROME_BROWSER_EXTENSIONS_API_STORAGE_SYNC_STORAGE_BACKEND_H_