| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_STORAGE_SETTINGS_BACKEND_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_STORAGE_SETTINGS_BACKEND_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_STORAGE_SETTINGS_BACKEND_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_STORAGE_SETTINGS_BACKEND_H_ |
| 7 | 7 |
| 8 #include <map> |
| 9 #include <set> |
| 8 #include <string> | 10 #include <string> |
| 9 | 11 |
| 12 #include "base/compiler_specific.h" |
| 10 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/memory/linked_ptr.h" |
| 11 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "chrome/browser/extensions/api/storage/settings_observer.h" |
| 18 #include "chrome/browser/extensions/api/storage/settings_storage_factory.h" |
| 13 #include "chrome/browser/extensions/api/storage/settings_storage_quota_enforcer.
h" | 19 #include "chrome/browser/extensions/api/storage/settings_storage_quota_enforcer.
h" |
| 20 #include "sync/api/syncable_service.h" |
| 14 | 21 |
| 15 namespace syncer { | 22 namespace syncer { |
| 16 class SyncableService; | 23 class SyncErrorFactory; |
| 17 } | 24 } |
| 18 | 25 |
| 19 class ValueStore; | 26 namespace extensions { |
| 20 | 27 |
| 21 namespace extensions { | 28 class SettingsSyncProcessor; |
| 22 class SettingsStorageFactory; | 29 class SyncableSettingsStorage; |
| 23 | 30 |
| 24 class SettingsBackend { | 31 // Manages ValueStore objects for extensions, including routing |
| 32 // changes from sync to them. |
| 33 // Lives entirely on the FILE thread. |
| 34 class SettingsBackend : public syncer::SyncableService { |
| 25 public: | 35 public: |
| 26 SettingsBackend(const scoped_refptr<SettingsStorageFactory>& storage_factory, | 36 // |storage_factory| is use to create leveldb storage areas. |
| 27 const base::FilePath& base_path, | 37 // |base_path| is the base of the extension settings directory, so the |
| 28 const SettingsStorageQuotaEnforcer::Limits& quota); | 38 // databases will be at base_path/extension_id. |
| 39 // |observers| is the list of observers to settings changes. |
| 40 SettingsBackend( |
| 41 const scoped_refptr<SettingsStorageFactory>& storage_factory, |
| 42 const base::FilePath& base_path, |
| 43 syncer::ModelType sync_type, |
| 44 const syncer::SyncableService::StartSyncFlare& flare, |
| 45 const SettingsStorageQuotaEnforcer::Limits& quota, |
| 46 const scoped_refptr<SettingsObserverList>& observers); |
| 47 |
| 29 virtual ~SettingsBackend(); | 48 virtual ~SettingsBackend(); |
| 30 | 49 |
| 31 // Gets a weak reference to the storage area for |extension_id|. | 50 // Gets a weak reference to the storage area for |extension_id|. |
| 32 // Must be run on the FILE thread. | 51 // Must be run on the FILE thread. |
| 33 virtual ValueStore* GetStorage(const std::string& extension_id) = 0; | 52 ValueStore* GetStorage(const std::string& extension_id) const; |
| 34 | 53 |
| 35 // Deletes all setting data for an extension. Call on the FILE thread. | 54 // Deletes all setting data for an extension. Call on the FILE thread. |
| 36 virtual void DeleteStorage(const std::string& extension_id) = 0; | 55 void DeleteStorage(const std::string& extension_id); |
| 37 | 56 |
| 38 // A slight hack so we can get a SyncableService from a SettingsBackend if | 57 // syncer::SyncableService implementation. |
| 39 // it's actually a SyncStorageBackend. If called on a LocalStorageBackend, | 58 virtual syncer::SyncDataList GetAllSyncData( |
| 40 // this asserts and returns null. | 59 syncer::ModelType type) const OVERRIDE; |
| 41 virtual syncer::SyncableService* GetAsSyncableService(); | 60 virtual syncer::SyncMergeResult MergeDataAndStartSyncing( |
| 61 syncer::ModelType type, |
| 62 const syncer::SyncDataList& initial_sync_data, |
| 63 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, |
| 64 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) OVERRIDE; |
| 65 virtual syncer::SyncError ProcessSyncChanges( |
| 66 const tracked_objects::Location& from_here, |
| 67 const syncer::SyncChangeList& change_list) OVERRIDE; |
| 68 virtual void StopSyncing(syncer::ModelType type) OVERRIDE; |
| 42 | 69 |
| 43 SettingsStorageFactory* storage_factory() const { | 70 private: |
| 44 return storage_factory_.get(); | 71 // Gets a weak reference to the storage area for a given extension, |
| 45 } | 72 // initializing sync with some initial data if sync enabled. |
| 46 const base::FilePath& base_path() const { return base_path_; } | 73 SyncableSettingsStorage* GetOrCreateStorageWithSyncData( |
| 47 const SettingsStorageQuotaEnforcer::Limits& quota() const { return quota_; } | 74 const std::string& extension_id, |
| 75 const base::DictionaryValue& sync_data) const; |
| 48 | 76 |
| 49 protected: | 77 // Gets all extension IDs known to extension settings. This may not be all |
| 50 // Creates a ValueStore decorated with quota-enforcing behavior (the default | 78 // installed extensions. |
| 51 // for both sync and local stores). If the database is corrupt, | 79 std::set<std::string> GetKnownExtensionIDs() const; |
| 52 // SettingsStorageQuotaEnforcer will try and restore it as part of the | 80 |
| 53 // initialization process (by necessity, since we need to read the database to | 81 // Creates a new SettingsSyncProcessor for an extension. |
| 54 // calculate the storage). | 82 scoped_ptr<SettingsSyncProcessor> CreateSettingsSyncProcessor( |
| 55 scoped_ptr<SettingsStorageQuotaEnforcer> CreateStorageForExtension( | |
| 56 const std::string& extension_id) const; | 83 const std::string& extension_id) const; |
| 57 | 84 |
| 58 private: | 85 // The Factory to use for creating leveldb storage areas. |
| 59 // The Factory to use for creating new ValueStores. | |
| 60 const scoped_refptr<SettingsStorageFactory> storage_factory_; | 86 const scoped_refptr<SettingsStorageFactory> storage_factory_; |
| 61 | 87 |
| 62 // The base file path to use when creating new ValueStores. | 88 // The base file path to create any leveldb databases at. |
| 63 const base::FilePath base_path_; | 89 const base::FilePath base_path_; |
| 64 | 90 |
| 65 // Quota limits (see SettingsStorageQuotaEnforcer). | 91 // Quota limits (see SettingsStorageQuotaEnforcer). |
| 66 const SettingsStorageQuotaEnforcer::Limits quota_; | 92 const SettingsStorageQuotaEnforcer::Limits quota_; |
| 67 | 93 |
| 94 // The list of observers to settings changes. |
| 95 const scoped_refptr<SettingsObserverList> observers_; |
| 96 |
| 97 // A cache of ValueStore objects that have already been created. |
| 98 // Ensure that there is only ever one created per extension. |
| 99 typedef std::map<std::string, linked_ptr<SyncableSettingsStorage> > |
| 100 StorageObjMap; |
| 101 mutable StorageObjMap storage_objs_; |
| 102 |
| 103 // Current sync model type. Either EXTENSION_SETTINGS or APP_SETTINGS. |
| 104 syncer::ModelType sync_type_; |
| 105 |
| 106 // Current sync processor, if any. |
| 107 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_; |
| 108 |
| 109 // Current sync error handler if any. |
| 110 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory_; |
| 111 |
| 112 syncer::SyncableService::StartSyncFlare flare_; |
| 113 |
| 68 DISALLOW_COPY_AND_ASSIGN(SettingsBackend); | 114 DISALLOW_COPY_AND_ASSIGN(SettingsBackend); |
| 69 }; | 115 }; |
| 70 | 116 |
| 71 } // namespace extensions | 117 } // namespace extensions |
| 72 | 118 |
| 73 #endif // CHROME_BROWSER_EXTENSIONS_API_STORAGE_SETTINGS_BACKEND_H_ | 119 #endif // CHROME_BROWSER_EXTENSIONS_API_STORAGE_SETTINGS_BACKEND_H_ |
| OLD | NEW |