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