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. 4 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_STORAGE_SYNC_STORAGE_BACKEND_H_ 6 #define CHROME_BROWSER_EXTENSIONS_API_STORAGE_SYNC_STORAGE_BACKEND_H_ 7 8 #include <map> 9 #include <set> 10 #include <string> 11 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" 21 22 namespace syncer { 23 class SyncErrorFactory; 24 } 25 26 namespace extensions { 27 28 class SettingsSyncProcessor; 29 class SyncableSettingsStorage; 30 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 { 35 public: 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. 40 SyncStorageBackend( 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); 47 48 virtual ~SyncStorageBackend(); 49 50 virtual ValueStore* GetStorage(const std::string& extension_id); 51 virtual void DeleteStorage(const std::string& extension_id); 52 53 // syncer::SyncableService implementation. 54 virtual syncer::SyncDataList GetAllSyncData(syncer::ModelType type) 55 const OVERRIDE; 56 virtual syncer::SyncMergeResult MergeDataAndStartSyncing( 57 syncer::ModelType type, 58 const syncer::SyncDataList& initial_sync_data, 59 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, 60 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) OVERRIDE; 61 virtual syncer::SyncError ProcessSyncChanges( 62 const tracked_objects::Location& from_here, 63 const syncer::SyncChangeList& change_list) OVERRIDE; 64 virtual void StopSyncing(syncer::ModelType type) OVERRIDE; 65 66 private: 67 // Gets a weak reference to the storage area for a given extension, 68 // initializing sync with some initial data if sync enabled. 69 SyncableSettingsStorage* GetOrCreateStorageWithSyncData( 70 const std::string& extension_id, 71 const base::DictionaryValue& sync_data) const; 72 73 // Gets all extension IDs known to extension settings. This may not be all 74 // installed extensions. 75 std::set<std::string> GetKnownExtensionIDs() const; 76 77 // Creates a new SettingsSyncProcessor for an extension. 78 scoped_ptr<SettingsSyncProcessor> CreateSettingsSyncProcessor( 79 const std::string& extension_id) const; 80 81 // The Factory to use for creating new ValueStores. 82 const scoped_refptr<SettingsStorageFactory> storage_factory_; 83 84 // The base file path to use when creating new ValueStores. 85 const base::FilePath base_path_; 86 87 // Quota limits (see SettingsStorageQuotaEnforcer). 88 const SettingsStorageQuotaEnforcer::Limits quota_; 89 90 // The list of observers to settings changes. 91 const scoped_refptr<SettingsObserverList> observers_; 92 93 // A cache of ValueStore objects that have already been created. 94 // Ensure that there is only ever one created per extension. 95 typedef std::map<std::string, linked_ptr<SyncableSettingsStorage> > 96 StorageObjMap; 97 mutable StorageObjMap storage_objs_; 98 99 // Current sync model type. Either EXTENSION_SETTINGS or APP_SETTINGS. 100 syncer::ModelType sync_type_; 101 102 // Current sync processor, if any. 103 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_; 104 105 // Current sync error handler if any. 106 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory_; 107 108 syncer::SyncableService::StartSyncFlare flare_; 109 110 DISALLOW_COPY_AND_ASSIGN(SyncStorageBackend); 111 }; 112 113 } // namespace extensions 114 115 #endif // CHROME_BROWSER_EXTENSIONS_API_STORAGE_SYNC_STORAGE_BACKEND_H_ 116