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/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "chrome/browser/extensions/extension_settings_storage.h" | 11 #include "chrome/browser/extensions/extension_service.h" |
| 12 #include "chrome/browser/extensions/syncable_extension_settings_storage.h" |
| 13 #include "chrome/browser/sync/api/syncable_service.h" |
12 | 14 |
13 // Manages ExtensionSettingsStorage objects for extensions. | 15 // Manages ExtensionSettingsStorage objects for extensions, including routing |
14 class ExtensionSettings : public base::RefCountedThreadSafe<ExtensionSettings> { | 16 // changes from sync to them. |
| 17 class ExtensionSettings |
| 18 : public base::RefCountedThreadSafe<ExtensionSettings>, |
| 19 public SyncableService { |
15 public: | 20 public: |
16 // File path is the base of the extension settings directory. | 21 // File path is the base of the extension settings directory. |
17 // The databases will be at base_path/extension_id. | 22 // The databases will be at base_path/extension_id. |
18 explicit ExtensionSettings(const FilePath& base_path); | 23 explicit ExtensionSettings(const FilePath& base_path); |
19 | 24 |
| 25 // Sets the current ExtensionService, required for GetAllSyncData. |
| 26 // Ownership NOT taken. |
| 27 void SetExtensionService(ExtensionServiceInterface* extension_service); |
| 28 |
20 // Gets a weak reference to the storage area for a given extension. | 29 // Gets a weak reference to the storage area for a given extension. |
21 // Must be run on the FILE thread. | 30 // Must be run on the FILE thread. |
22 // | 31 // |
23 // By default this will be of a cached LEVELDB storage, but on failure to | 32 // 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. | 33 // create a leveldb instance will fall back to cached NOOP storage. |
25 ExtensionSettingsStorage* GetStorage(const std::string& extension_id); | 34 ExtensionSettingsStorage* GetStorage( |
| 35 const std::string& extension_id) const; |
26 | 36 |
27 // Gets a weak reference to the storage area for a given extension, with a | 37 // 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. | 38 // specific type and whether it should be wrapped in a cache. |
29 // | 39 // |
30 // Use this for testing; if the given type fails to be created (e.g. if | 40 // Use this for testing; if the given type fails to be created (e.g. if |
31 // leveldb creation fails) then a DCHECK will fail. | 41 // leveldb creation fails) then a DCHECK will fail. |
32 ExtensionSettingsStorage* GetStorageForTesting( | 42 ExtensionSettingsStorage* GetStorageForTesting( |
33 ExtensionSettingsStorage::Type type, | 43 ExtensionSettingsStorage::Type type, |
34 bool cached, | 44 bool cached, |
35 const std::string& extension_id); | 45 const std::string& extension_id) const; |
| 46 |
| 47 // SyncableService implementation. |
| 48 virtual SyncDataList GetAllSyncData(syncable::ModelType type) const OVERRIDE; |
| 49 virtual SyncError MergeDataAndStartSyncing( |
| 50 syncable::ModelType type, |
| 51 const SyncDataList& initial_sync_data, |
| 52 SyncChangeProcessor* sync_processor) OVERRIDE; |
| 53 virtual SyncError ProcessSyncChanges( |
| 54 const tracked_objects::Location& from_here, |
| 55 const SyncChangeList& change_list) OVERRIDE; |
| 56 virtual void StopSyncing(syncable::ModelType type) OVERRIDE; |
36 | 57 |
37 private: | 58 private: |
38 friend class base::RefCountedThreadSafe<ExtensionSettings>; | 59 friend class base::RefCountedThreadSafe<ExtensionSettings>; |
39 virtual ~ExtensionSettings(); | 60 virtual ~ExtensionSettings(); |
40 | 61 |
| 62 // Gets a weak reference to the storage area for a given extension, |
| 63 // initializing sync with some initial data if sync enabled. |
| 64 // |
| 65 // By default this will be of a cached LEVELDB storage, but on failure to |
| 66 // create a leveldb instance will fall back to cached NOOP storage. |
| 67 SyncableExtensionSettingsStorage* GetOrCreateStorageWithSyncData( |
| 68 const std::string& extension_id, const DictionaryValue& sync_data) const; |
| 69 |
| 70 // If a storage area exists in the cache, returns the cached storage area. |
| 71 // Otherwise tries to create one using CreateAndInitStorage. |
| 72 SyncableExtensionSettingsStorage* GetOrCreateAndInitStorage( |
| 73 ExtensionSettingsStorage::Type type, |
| 74 bool cached, |
| 75 const std::string& extension_id, |
| 76 const DictionaryValue& initial_sync_data) const; |
| 77 |
41 // Creates a storage area of a given type, optionally wrapped in a cache. | 78 // Creates a storage area of a given type, optionally wrapped in a cache. |
42 // Returns NULL if creation fails. | 79 // Returns NULL if creation fails. Otherwise, adds the new storage area to |
43 ExtensionSettingsStorage* CreateStorage( | 80 // the cache and initializes sync if sync is enabled. |
| 81 SyncableExtensionSettingsStorage* CreateAndInitStorage( |
| 82 ExtensionSettingsStorage::Type type, |
| 83 bool cached, |
44 const std::string& extension_id, | 84 const std::string& extension_id, |
45 ExtensionSettingsStorage::Type type, | 85 const DictionaryValue& initial_sync_data) const; |
46 bool cached); | |
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 mutable |
| 93 std::map<std::string, SyncableExtensionSettingsStorage*> storage_objs_; |
| 94 |
| 95 // Current extension service. Needed in order to list all extensions when |
| 96 // getting all settings for GetAllSyncData(). |
| 97 const ExtensionServiceInterface* extension_service_; |
| 98 |
| 99 // Current sync processor, if any. |
| 100 SyncChangeProcessor* sync_processor_; |
54 | 101 |
55 DISALLOW_COPY_AND_ASSIGN(ExtensionSettings); | 102 DISALLOW_COPY_AND_ASSIGN(ExtensionSettings); |
56 }; | 103 }; |
57 | 104 |
58 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_SETTINGS_H_ | 105 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_SETTINGS_H_ |
OLD | NEW |