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/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/compiler_specific.h" |
10 #include "base/file_path.h" | 11 #include "base/file_path.h" |
11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/values.h" |
12 #include "chrome/browser/extensions/extension_settings_storage.h" | 15 #include "chrome/browser/extensions/extension_settings_storage.h" |
| 16 #include "chrome/browser/extensions/extension_settings.h" |
| 17 #include "chrome/browser/extensions/extension_settings_storage.h" |
| 18 #include "chrome/browser/extensions/extension_setting_sync_data.h" |
| 19 #include "chrome/browser/extensions/syncable_extension_settings_storage.h" |
| 20 #include "chrome/browser/sync/api/syncable_service.h" |
| 21 #include "chrome/browser/sync/api/sync_data.h" |
| 22 #include "chrome/browser/sync/api/sync_change.h" |
13 | 23 |
14 // Manages ExtensionSettingsStorage objects for extensions. | 24 // Manages ExtensionSettingsStorage objects and routes sync events to their |
15 class ExtensionSettings : public base::RefCountedThreadSafe<ExtensionSettings> { | 25 // target storage objects. |
| 26 class ExtensionSettings |
| 27 : public base::RefCountedThreadSafe<ExtensionSettings>, |
| 28 public SyncableService { |
16 public: | 29 public: |
17 // File path is the base of the extension settings directory. | 30 // File path is the base of the extension settings directory. |
18 // The databases will be at base_path/extension_id. | 31 // The databases will be at base_path/extension_id. |
19 explicit ExtensionSettings(const FilePath& base_path); | 32 explicit ExtensionSettings(const FilePath& base_path); |
20 | 33 |
21 // Callback from the GetStorage() methods. | 34 // Callback from the GetStorage() methods. |
22 typedef base::Callback<void(ExtensionSettingsStorage*)> Callback; | 35 typedef base::Callback<void(SyncableExtensionSettingsStorage*)> Callback; |
23 | 36 |
24 // Gets the storage area for a given extension. Only valid for the duration | 37 // Gets a sync-enabled storage area for a given extension. Only valid for |
25 // of the callback. | 38 // the duration of the callback. |
26 // By default this will be of a cached LEVELDB storage, but on failure to | 39 // By default this will be a cached LEVELDB storage area, but on failure to |
27 // create a leveldb instance will fall back to cached NOOP storage. | 40 // create a leveldb instance will fall back to cached NOOP storage. |
28 // Callbacks will happen asynchronously regardless of whether they need to go | 41 // Callbacks will happen asynchronously regardless of whether they need to go |
29 // to the FILE thread, but will always be called on the UI thread. | 42 // to the FILE thread, but will always be called on the UI thread. |
30 void GetStorage(const std::string& extension_id, const Callback& callback); | 43 void GetStorage(const std::string& extension_id, const Callback& callback); |
31 | 44 |
32 // Gets a storage area for a given extension with a specific type. | 45 // Gets a sync-endabled storage area for a given extension with a specific |
33 // and whether it should be wrapped in a cache. | 46 // type. Use this for testing; if the given type fails to be created (e.g. |
34 // Use this for testing; if the given type fails to be created (e.g. if | 47 // if leveldb creation fails) then a DCHECK will fail. |
35 // leveldb creation fails) then a DCHECK will fail. | |
36 // Callback objects will be deleted when used. | |
37 void GetStorageForTesting( | 48 void GetStorageForTesting( |
38 ExtensionSettingsStorage::Type type, | 49 ExtensionSettingsStorage::Type type, |
39 bool cached, | 50 bool cached, |
40 const std::string& extension_id, | 51 const std::string& extension_id, |
41 const Callback& callback); | 52 const Callback& callback); |
42 | 53 |
| 54 // SyncableService implementation. |
| 55 virtual SyncDataList GetAllSyncData(syncable::ModelType type) const OVERRIDE; |
| 56 virtual SyncError MergeDataAndStartSyncing( |
| 57 syncable::ModelType type, |
| 58 const SyncDataList& initial_sync_data, |
| 59 SyncChangeProcessor* sync_processor) OVERRIDE; |
| 60 virtual SyncError ProcessSyncChanges( |
| 61 const tracked_objects::Location& from_here, |
| 62 const SyncChangeList& change_list) OVERRIDE; |
| 63 virtual void StopSyncing(syncable::ModelType type) OVERRIDE; |
| 64 |
43 private: | 65 private: |
44 friend class base::RefCountedThreadSafe<ExtensionSettings>; | 66 friend class base::RefCountedThreadSafe<ExtensionSettings>; |
45 ~ExtensionSettings(); | 67 virtual ~ExtensionSettings(); |
46 | 68 |
47 // Attempts to get and callback with an existing storage area. Returns | 69 // Attempts to get an existing storage area, and runs a Callback if |
48 // whether storage existed and the callback run. | 70 // successful. Returns whether storage existed and the callback run. |
49 bool GetExistingStorage( | 71 bool GetExistingStorage( |
50 const std::string& extension_id, const Callback& callback); | 72 const std::string& extension_id, const Callback& callback); |
51 | 73 |
52 // Runs a Callback with a storage argument, then deletes the callback. | 74 // Runs a Callback with a storage argument, then deletes the callback. |
53 void RunWithStorage(Callback* callback, ExtensionSettingsStorage* storage); | 75 void RunWithStorage( |
| 76 Callback* callback, SyncableExtensionSettingsStorage* storage); |
54 | 77 |
55 // Starts the process of creation of a storage area. | 78 // Starts the process of creation of a storage area. |
56 // Must be run on the UI thread. | 79 // Must be run on the UI thread. |
57 void StartCreationOfStorage( | 80 void StartCreationOfStorage( |
58 const std::string& extension_id, | 81 const std::string& extension_id, |
59 ExtensionSettingsStorage::Type type, | 82 ExtensionSettingsStorage::Type type, |
60 ExtensionSettingsStorage::Type fallback_type, | 83 ExtensionSettingsStorage::Type fallback_type, |
61 bool cached, | 84 bool cached, |
62 const Callback& callback); | 85 const Callback& callback); |
63 | 86 |
64 // Creates a new storage area of a given type, with a fallback type if | 87 // Creates a new storage area of a given type, with a fallback type if |
65 // creation fails, and optionally wrapped in a cache. | 88 // creation fails, and optionally wrapped in a cache. |
66 // Must be run on the FILE thread. | 89 // Must be run on the FILE thread. |
67 void CreateStorageOnFileThread( | 90 void CreateStorageOnFileThread( |
68 const std::string& extension_id, | 91 const std::string& extension_id, |
69 ExtensionSettingsStorage::Type type, | 92 ExtensionSettingsStorage::Type type, |
70 ExtensionSettingsStorage::Type fallback_type, | 93 ExtensionSettingsStorage::Type fallback_type, |
71 bool cached, | 94 bool cached, |
72 const Callback& callback); | 95 const Callback& callback); |
73 | 96 |
74 // Creates a storage area of a given type, optionally wrapped in a cache. | 97 // Creates a sync-enabled storage area of a given type, optionally wrapped in |
75 // Returns NULL if creation fails. | 98 // a cache. Returns NULL if creation fails. |
76 ExtensionSettingsStorage* CreateStorage( | 99 SyncableExtensionSettingsStorage* CreateStorage( |
77 const std::string& extension_id, | 100 const std::string& extension_id, |
78 ExtensionSettingsStorage::Type type, | 101 ExtensionSettingsStorage::Type type, |
79 bool cached); | 102 bool cached); |
80 | 103 |
81 // End the creation of a storage area. | 104 // End the creation of a storage area. |
82 // Must be run on the UI thread. | 105 // Must be run on the UI thread. |
83 void EndCreationOfStorage( | 106 void EndCreationOfStorage( |
84 const std::string& extension_id, | 107 const std::string& extension_id, |
85 ExtensionSettingsStorage* storage, | 108 SyncableExtensionSettingsStorage* storage, |
86 const Callback& callback); | 109 const Callback& callback); |
87 | 110 |
| 111 // Start syncing some extension settings. |
| 112 void StartSyncingStorage( |
| 113 const std::string& extension_id, |
| 114 SyncableExtensionSettingsStorage* storage); |
| 115 |
| 116 // Stop syncing some extension settings. |
| 117 void StopSyncingStorage(ExtensionSettingsStorage* storage); |
| 118 |
| 119 // Asserts that there is no sync data for a storage area. |
| 120 void AssertNoSyncData( |
| 121 const std::string& extension_id, |
| 122 SyncableExtensionSettingsStorage* storage); |
| 123 |
88 // The base file path to create any leveldb databases at. | 124 // The base file path to create any leveldb databases at. |
89 const FilePath base_path_; | 125 const FilePath base_path_; |
90 | 126 |
91 // A cache of ExtensionSettingsStorage objects that have already been created. | 127 // A cache of ExtensionSettingsStorage objects that have already been created. |
92 // Ensure that there is only ever one created per extension. | 128 // Ensure that there is only ever one created per extension. |
93 std::map<std::string, ExtensionSettingsStorage*> storage_objs_; | 129 std::map<std::string, SyncableExtensionSettingsStorage*> storage_objs_; |
| 130 |
| 131 // Sync change processor for sync. |
| 132 SyncChangeProcessor* sync_processor_; |
| 133 |
| 134 // Initial settings received from sync that haven't been merged into their |
| 135 // respective storage areas. Populated in MergeDataAndStartSyncing and |
| 136 // depopulated in StartSyncingStorage. |
| 137 std::map<std::string, DictionaryValue*> unmerged_sync_data_; |
94 | 138 |
95 DISALLOW_COPY_AND_ASSIGN(ExtensionSettings); | 139 DISALLOW_COPY_AND_ASSIGN(ExtensionSettings); |
96 }; | 140 }; |
97 | 141 |
98 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_SETTINGS_H_ | 142 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_SETTINGS_H_ |
OLD | NEW |