| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_EXTENSION_SETTINGS_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_SETTINGS_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_SETTINGS_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_SETTINGS_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" |
| 9 #include "base/file_path.h" | 10 #include "base/file_path.h" |
| 11 #include "base/memory/linked_ptr.h" |
| 10 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 11 #include "chrome/browser/extensions/extension_settings_storage.h" | 13 #include "base/task.h" |
| 14 #include "chrome/browser/extensions/syncable_extension_settings_storage.h" |
| 15 #include "chrome/browser/sync/api/syncable_service.h" |
| 12 | 16 |
| 13 // Manages ExtensionSettingsStorage objects for extensions. | 17 // Manages ExtensionSettingsStorage objects for extensions, including routing |
| 14 class ExtensionSettings : public base::RefCountedThreadSafe<ExtensionSettings> { | 18 // changes from sync to them. |
| 19 // Lives entirely on the FILE thread. |
| 20 class ExtensionSettings : public SyncableService { |
| 15 public: | 21 public: |
| 16 // File path is the base of the extension settings directory. | 22 // File path is the base of the extension settings directory. |
| 17 // The databases will be at base_path/extension_id. | 23 // The databases will be at base_path/extension_id. |
| 18 explicit ExtensionSettings(const FilePath& base_path); | 24 explicit ExtensionSettings(const FilePath& base_path); |
| 19 | 25 |
| 26 virtual ~ExtensionSettings(); |
| 27 |
| 20 // Gets a weak reference to the storage area for a given extension. | 28 // Gets a weak reference to the storage area for a given extension. |
| 21 // Must be run on the FILE thread. | 29 // Must be run on the FILE thread. |
| 22 // | 30 // |
| 23 // By default this will be of a cached LEVELDB storage, but on failure to | 31 // By default this will be of a cached LEVELDB storage, but on failure to |
| 24 // create a leveldb instance will fall back to cached NOOP storage. | 32 // create a leveldb instance will fall back to cached NOOP storage. |
| 25 ExtensionSettingsStorage* GetStorage(const std::string& extension_id); | 33 ExtensionSettingsStorage* GetStorage( |
| 34 const std::string& extension_id) const; |
| 26 | 35 |
| 27 // Gets a weak reference to the storage area for a given extension, with a | 36 // Gets a weak reference to the storage area for a given extension, with a |
| 28 // specific type and whether it should be wrapped in a cache. | 37 // specific type and whether it should be wrapped in a cache. |
| 29 // | 38 // |
| 30 // Use this for testing; if the given type fails to be created (e.g. if | 39 // Use this for testing; if the given type fails to be created (e.g. if |
| 31 // leveldb creation fails) then a DCHECK will fail. | 40 // leveldb creation fails) then a DCHECK will fail. |
| 32 ExtensionSettingsStorage* GetStorageForTesting( | 41 ExtensionSettingsStorage* GetStorageForTesting( |
| 33 ExtensionSettingsStorage::Type type, | 42 ExtensionSettingsStorage::Type type, |
| 34 bool cached, | 43 bool cached, |
| 35 const std::string& extension_id); | 44 const std::string& extension_id) const; |
| 45 |
| 46 // SyncableService implementation. |
| 47 virtual SyncDataList GetAllSyncData(syncable::ModelType type) const OVERRIDE; |
| 48 virtual SyncError MergeDataAndStartSyncing( |
| 49 syncable::ModelType type, |
| 50 const SyncDataList& initial_sync_data, |
| 51 SyncChangeProcessor* sync_processor) OVERRIDE; |
| 52 virtual SyncError ProcessSyncChanges( |
| 53 const tracked_objects::Location& from_here, |
| 54 const SyncChangeList& change_list) OVERRIDE; |
| 55 virtual void StopSyncing(syncable::ModelType type) OVERRIDE; |
| 36 | 56 |
| 37 private: | 57 private: |
| 38 friend class base::RefCountedThreadSafe<ExtensionSettings>; | 58 // Gets a weak reference to the storage area for a given extension, |
| 39 virtual ~ExtensionSettings(); | 59 // initializing sync with some initial data if sync enabled. |
| 60 // |
| 61 // By default this will be of a cached LEVELDB storage, but on failure to |
| 62 // create a leveldb instance will fall back to cached NOOP storage. |
| 63 SyncableExtensionSettingsStorage* GetOrCreateStorageWithSyncData( |
| 64 const std::string& extension_id, const DictionaryValue& sync_data) const; |
| 65 |
| 66 // If a storage area exists in the cache, returns the cached storage area. |
| 67 // Otherwise tries to create one using CreateAndInitStorage. |
| 68 SyncableExtensionSettingsStorage* GetOrCreateAndInitStorage( |
| 69 ExtensionSettingsStorage::Type type, |
| 70 bool cached, |
| 71 const std::string& extension_id, |
| 72 const DictionaryValue& initial_sync_data) const; |
| 40 | 73 |
| 41 // Creates a storage area of a given type, optionally wrapped in a cache. | 74 // Creates a storage area of a given type, optionally wrapped in a cache. |
| 42 // Returns NULL if creation fails. | 75 // Returns NULL if creation fails. Otherwise, adds the new storage area to |
| 43 ExtensionSettingsStorage* CreateStorage( | 76 // the cache and initializes sync if sync is enabled. |
| 77 SyncableExtensionSettingsStorage* CreateAndInitStorage( |
| 78 ExtensionSettingsStorage::Type type, |
| 79 bool cached, |
| 44 const std::string& extension_id, | 80 const std::string& extension_id, |
| 45 ExtensionSettingsStorage::Type type, | 81 const DictionaryValue& initial_sync_data) const; |
| 46 bool cached); | 82 |
| 83 // Gets all extension IDs known to extension settings. This may not be all |
| 84 // installed extensions. |
| 85 std::set<std::string> GetKnownExtensionIDs() const; |
| 47 | 86 |
| 48 // The base file path to create any leveldb databases at. | 87 // The base file path to create any leveldb databases at. |
| 49 const FilePath base_path_; | 88 const FilePath base_path_; |
| 50 | 89 |
| 51 // A cache of ExtensionSettingsStorage objects that have already been created. | 90 // A cache of ExtensionSettingsStorage objects that have already been created. |
| 52 // Ensure that there is only ever one created per extension. | 91 // Ensure that there is only ever one created per extension. |
| 53 std::map<std::string, ExtensionSettingsStorage*> storage_objs_; | 92 typedef std::map<std::string, linked_ptr<SyncableExtensionSettingsStorage> > |
| 93 StorageObjMap; |
| 94 mutable StorageObjMap storage_objs_; |
| 95 |
| 96 // Current sync processor, if any. |
| 97 SyncChangeProcessor* sync_processor_; |
| 54 | 98 |
| 55 DISALLOW_COPY_AND_ASSIGN(ExtensionSettings); | 99 DISALLOW_COPY_AND_ASSIGN(ExtensionSettings); |
| 56 }; | 100 }; |
| 57 | 101 |
| 58 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_SETTINGS_H_ | 102 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_SETTINGS_H_ |
| OLD | NEW |