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 #include "chrome/browser/extensions/settings/settings_backend.h" | 5 #include "chrome/browser/extensions/settings/settings_backend.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/linked_ptr.h" | 12 #include "base/memory/linked_ptr.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "chrome/browser/extensions/settings/settings_leveldb_storage.h" | 14 #include "chrome/browser/extensions/settings/failing_settings_storage.h" |
15 #include "chrome/browser/extensions/settings/settings_storage_cache.h" | 15 #include "chrome/browser/extensions/settings/settings_storage_cache.h" |
| 16 #include "chrome/browser/extensions/settings/settings_storage_factory.h" |
16 #include "chrome/browser/extensions/settings/settings_storage_quota_enforcer.h" | 17 #include "chrome/browser/extensions/settings/settings_storage_quota_enforcer.h" |
17 #include "chrome/browser/extensions/settings/settings_sync_util.h" | 18 #include "chrome/browser/extensions/settings/settings_sync_util.h" |
18 #include "chrome/browser/extensions/settings/in_memory_settings_storage.h" | |
19 #include "chrome/common/extensions/extension.h" | 19 #include "chrome/common/extensions/extension.h" |
20 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
21 #include "third_party/leveldatabase/src/include/leveldb/iterator.h" | 21 |
22 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" | 22 using content::BrowserThread; |
23 | 23 |
24 namespace extensions { | 24 namespace extensions { |
25 | 25 |
26 using content::BrowserThread; | |
27 | |
28 namespace { | 26 namespace { |
29 | 27 |
30 // Total quota for all settings per extension, in bytes. 100K should be enough | 28 // Total quota for all settings per extension, in bytes. 100K should be enough |
31 // for most uses, but this can be increased as demand increases. | 29 // for most uses, but this can be increased as demand increases. |
32 const size_t kTotalQuotaBytes = 100 * 1024; | 30 const size_t kTotalQuotaBytes = 100 * 1024; |
33 | 31 |
34 // Quota for each setting. Sync supports 5k per setting, so be a bit more | 32 // Quota for each setting. Sync supports 5k per setting, so be a bit more |
35 // restrictive than that. | 33 // restrictive than that. |
36 const size_t kQuotaPerSettingBytes = 2048; | 34 const size_t kQuotaPerSettingBytes = 2048; |
37 | 35 |
38 // Max number of settings per extension. Keep low for sync. | 36 // Max number of settings per extension. Keep low for sync. |
39 const size_t kMaxSettingKeys = 512; | 37 const size_t kMaxSettingKeys = 512; |
40 | 38 |
41 } // namespace | 39 } // namespace |
42 | 40 |
43 SettingsBackend::SettingsBackend( | 41 SettingsBackend::SettingsBackend( |
| 42 SettingsStorageFactory* storage_factory, |
44 const FilePath& base_path, | 43 const FilePath& base_path, |
45 const scoped_refptr<SettingsObserverList>& observers) | 44 const scoped_refptr<SettingsObserverList>& observers) |
46 : base_path_(base_path), | 45 : storage_factory_(storage_factory), |
| 46 base_path_(base_path), |
47 observers_(observers), | 47 observers_(observers), |
48 sync_type_(syncable::UNSPECIFIED), | 48 sync_type_(syncable::UNSPECIFIED), |
49 sync_processor_(NULL) { | 49 sync_processor_(NULL) { |
50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
51 } | 51 } |
52 | 52 |
53 SettingsBackend::~SettingsBackend() { | 53 SettingsBackend::~SettingsBackend() { |
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
55 } | 55 } |
56 | 56 |
57 SettingsStorage* SettingsBackend::GetStorage( | 57 SettingsStorage* SettingsBackend::GetStorage( |
58 const std::string& extension_id) const { | 58 const std::string& extension_id) const { |
59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
60 DictionaryValue empty; | 60 DictionaryValue empty; |
61 return GetOrCreateStorageWithSyncData(extension_id, empty); | 61 return GetOrCreateStorageWithSyncData(extension_id, empty); |
62 } | 62 } |
63 | 63 |
64 SyncableSettingsStorage* | 64 SyncableSettingsStorage* |
65 SettingsBackend::GetOrCreateStorageWithSyncData( | 65 SettingsBackend::GetOrCreateStorageWithSyncData( |
66 const std::string& extension_id, const DictionaryValue& sync_data) const { | 66 const std::string& extension_id, const DictionaryValue& sync_data) const { |
67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
68 | 68 |
69 linked_ptr<SyncableSettingsStorage> syncable_storage = | 69 StorageObjMap::iterator maybe_storage = storage_objs_.find(extension_id); |
70 storage_objs_[extension_id]; | 70 if (maybe_storage != storage_objs_.end()) { |
71 if (syncable_storage.get()) { | 71 return maybe_storage->second.get(); |
72 return syncable_storage.get(); | |
73 } | 72 } |
74 | 73 |
75 SettingsStorage* storage = | 74 SettingsStorage* storage = storage_factory_->Create(base_path_, extension_id); |
76 SettingsLeveldbStorage::Create(base_path_, extension_id); | |
77 if (storage) { | 75 if (storage) { |
78 storage = new SettingsStorageCache(storage); | 76 storage = new SettingsStorageCache(storage); |
| 77 // It's fine to create the quota enforcer underneath the sync layer, since |
| 78 // sync will only go ahead if each underlying storage operation succeeds. |
| 79 storage = new SettingsStorageQuotaEnforcer( |
| 80 kTotalQuotaBytes, kQuotaPerSettingBytes, kMaxSettingKeys, storage); |
79 } else { | 81 } else { |
80 // Failed to create a leveldb storage area, create an in-memory one. | 82 storage = new FailingSettingsStorage(); |
81 // It's ok for these to be synced, it just means that on next starting up | |
82 // extensions will see the "old" settings, then overwritten (and notified) | |
83 // when the sync changes come through. | |
84 storage = new InMemorySettingsStorage(); | |
85 } | 83 } |
86 | 84 |
87 // It's fine to create the quota enforcer underneath the sync later, since | 85 linked_ptr<SyncableSettingsStorage> syncable_storage( |
88 // sync will only go ahead if each underlying storage operation is successful. | 86 new SyncableSettingsStorage( |
89 storage = new SettingsStorageQuotaEnforcer( | 87 observers_, |
90 kTotalQuotaBytes, kQuotaPerSettingBytes, kMaxSettingKeys, storage); | 88 extension_id, |
91 | 89 storage)); |
92 syncable_storage = | |
93 linked_ptr<SyncableSettingsStorage>( | |
94 new SyncableSettingsStorage( | |
95 observers_, | |
96 extension_id, | |
97 storage)); | |
98 if (sync_processor_) { | 90 if (sync_processor_) { |
99 // TODO(kalman): do something if StartSyncing fails. | 91 // TODO(kalman): do something if StartSyncing fails. |
100 ignore_result(syncable_storage->StartSyncing( | 92 ignore_result(syncable_storage->StartSyncing( |
101 sync_type_, sync_data, sync_processor_)); | 93 sync_type_, sync_data, sync_processor_)); |
102 } | 94 } |
103 | 95 |
104 storage_objs_[extension_id] = syncable_storage; | 96 storage_objs_[extension_id] = syncable_storage; |
105 return syncable_storage.get(); | 97 return syncable_storage.get(); |
106 } | 98 } |
107 | 99 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 | 139 |
148 return result; | 140 return result; |
149 } | 141 } |
150 | 142 |
151 static void AddAllSyncData( | 143 static void AddAllSyncData( |
152 const std::string& extension_id, | 144 const std::string& extension_id, |
153 const DictionaryValue& src, | 145 const DictionaryValue& src, |
154 SyncDataList* dst) { | 146 SyncDataList* dst) { |
155 for (DictionaryValue::Iterator it(src); it.HasNext(); it.Advance()) { | 147 for (DictionaryValue::Iterator it(src); it.HasNext(); it.Advance()) { |
156 dst->push_back( | 148 dst->push_back( |
157 settings_sync_util::CreateData( | 149 settings_sync_util::CreateData(extension_id, it.key(), it.value())); |
158 extension_id, it.key(), it.value())); | |
159 } | 150 } |
160 } | 151 } |
161 | 152 |
162 SyncDataList SettingsBackend::GetAllSyncData( | 153 SyncDataList SettingsBackend::GetAllSyncData( |
163 syncable::ModelType type) const { | 154 syncable::ModelType type) const { |
164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
165 // Ignore the type, it's just for sanity checking; assume that whatever base | 156 // Ignore the type, it's just for sanity checking; assume that whatever base |
166 // path we're constructed with is correct for the sync type. | 157 // path we're constructed with is correct for the sync type. |
167 DCHECK(type == syncable::EXTENSION_SETTINGS || | 158 DCHECK(type == syncable::EXTENSION_SETTINGS || |
168 type == syncable::APP_SETTINGS); | 159 type == syncable::APP_SETTINGS); |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 sync_type_ = syncable::UNSPECIFIED; | 274 sync_type_ = syncable::UNSPECIFIED; |
284 sync_processor_ = NULL; | 275 sync_processor_ = NULL; |
285 | 276 |
286 for (StorageObjMap::iterator it = storage_objs_.begin(); | 277 for (StorageObjMap::iterator it = storage_objs_.begin(); |
287 it != storage_objs_.end(); ++it) { | 278 it != storage_objs_.end(); ++it) { |
288 it->second->StopSyncing(); | 279 it->second->StopSyncing(); |
289 } | 280 } |
290 } | 281 } |
291 | 282 |
292 } // namespace extensions | 283 } // namespace extensions |
OLD | NEW |