Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/api/storage/settings_frontend.h" | 5 #include "chrome/browser/extensions/api/storage/settings_frontend.h" |
| 6 | 6 |
| 7 #include <limits> | |
| 8 | |
| 9 #include "base/bind.h" | 7 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 11 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 12 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| 13 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 14 #include "chrome/browser/extensions/api/storage/leveldb_settings_storage_factory .h" | 12 #include "chrome/browser/extensions/api/storage/leveldb_settings_storage_factory .h" |
| 15 #include "chrome/browser/extensions/api/storage/sync_or_local_value_store_cache. h" | 13 #include "chrome/browser/extensions/api/storage/local_value_store_cache.h" |
| 16 #include "chrome/common/extensions/api/storage.h" | 14 #include "chrome/common/extensions/api/storage.h" |
| 17 #include "content/public/browser/browser_context.h" | 15 #include "content/public/browser/browser_context.h" |
| 18 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 19 #include "extensions/browser/api/extensions_api_client.h" | 17 #include "extensions/browser/api/extensions_api_client.h" |
| 20 #include "extensions/browser/event_router.h" | 18 #include "extensions/browser/event_router.h" |
| 21 #include "extensions/browser/extension_registry.h" | 19 #include "extensions/browser/extension_registry.h" |
| 22 #include "extensions/browser/extension_system.h" | 20 #include "extensions/browser/extension_system.h" |
| 23 | 21 |
| 24 using content::BrowserContext; | 22 using content::BrowserContext; |
| 25 using content::BrowserThread; | 23 using content::BrowserThread; |
| 26 | 24 |
| 27 namespace extensions { | 25 namespace extensions { |
| 28 | 26 |
| 29 namespace storage = api::storage; | |
| 30 | |
| 31 namespace { | 27 namespace { |
| 32 | 28 |
| 33 base::LazyInstance<BrowserContextKeyedAPIFactory<SettingsFrontend> > g_factory = | 29 base::LazyInstance<BrowserContextKeyedAPIFactory<SettingsFrontend> > g_factory = |
| 34 LAZY_INSTANCE_INITIALIZER; | 30 LAZY_INSTANCE_INITIALIZER; |
| 35 | 31 |
| 36 // Settings change Observer which forwards changes on to the extension | 32 // Settings change Observer which forwards changes on to the extension |
| 37 // processes for |context| and its incognito partner if it exists. | 33 // processes for |context| and its incognito partner if it exists. |
| 38 class DefaultObserver : public SettingsObserver { | 34 class DefaultObserver : public SettingsObserver { |
| 39 public: | 35 public: |
| 40 explicit DefaultObserver(BrowserContext* context) | 36 explicit DefaultObserver(BrowserContext* context) |
| 41 : browser_context_(context) {} | 37 : browser_context_(context) {} |
| 42 | 38 |
| 43 // SettingsObserver implementation. | 39 // SettingsObserver implementation. |
| 44 virtual void OnSettingsChanged( | 40 virtual void OnSettingsChanged( |
| 45 const std::string& extension_id, | 41 const std::string& extension_id, |
| 46 settings_namespace::Namespace settings_namespace, | 42 settings_namespace::Namespace settings_namespace, |
| 47 const std::string& change_json) OVERRIDE { | 43 const std::string& change_json) OVERRIDE { |
| 48 // TODO(gdk): This is a temporary hack while the refactoring for | 44 // TODO(gdk): This is a temporary hack while the refactoring for |
| 49 // string-based event payloads is removed. http://crbug.com/136045 | 45 // string-based event payloads is removed. http://crbug.com/136045 |
| 50 scoped_ptr<base::ListValue> args(new base::ListValue()); | 46 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 51 args->Append(base::JSONReader::Read(change_json)); | 47 args->Append(base::JSONReader::Read(change_json)); |
| 52 args->Append(new base::StringValue(settings_namespace::ToString( | 48 args->Append(new base::StringValue(settings_namespace::ToString( |
| 53 settings_namespace))); | 49 settings_namespace))); |
| 54 scoped_ptr<Event> event(new Event( | 50 scoped_ptr<Event> event(new Event( |
| 55 storage::OnChanged::kEventName, args.Pass())); | 51 api::storage::OnChanged::kEventName, args.Pass())); |
| 56 ExtensionSystem::Get(browser_context_)->event_router()-> | 52 ExtensionSystem::Get(browser_context_)->event_router()-> |
| 57 DispatchEventToExtension(extension_id, event.Pass()); | 53 DispatchEventToExtension(extension_id, event.Pass()); |
| 58 } | 54 } |
| 59 | 55 |
| 60 private: | 56 private: |
| 61 BrowserContext* const browser_context_; | 57 BrowserContext* const browser_context_; |
| 62 }; | 58 }; |
| 63 | 59 |
| 64 SettingsStorageQuotaEnforcer::Limits GetLocalLimits() { | |
| 65 SettingsStorageQuotaEnforcer::Limits limits = { | |
| 66 static_cast<size_t>(api::storage::local::QUOTA_BYTES), | |
| 67 std::numeric_limits<size_t>::max(), | |
| 68 std::numeric_limits<size_t>::max() | |
| 69 }; | |
| 70 return limits; | |
| 71 } | |
| 72 | |
| 73 SettingsStorageQuotaEnforcer::Limits GetSyncLimits() { | |
| 74 SettingsStorageQuotaEnforcer::Limits limits = { | |
| 75 static_cast<size_t>(api::storage::sync::QUOTA_BYTES), | |
| 76 static_cast<size_t>(api::storage::sync::QUOTA_BYTES_PER_ITEM), | |
| 77 static_cast<size_t>(api::storage::sync::MAX_ITEMS) | |
| 78 }; | |
| 79 return limits; | |
| 80 } | |
| 81 | |
| 82 } // namespace | 60 } // namespace |
| 83 | 61 |
| 84 // static | 62 // static |
| 85 SettingsFrontend* SettingsFrontend::Get(BrowserContext* context) { | 63 SettingsFrontend* SettingsFrontend::Get(BrowserContext* context) { |
| 86 return BrowserContextKeyedAPIFactory<SettingsFrontend>::Get(context); | 64 return BrowserContextKeyedAPIFactory<SettingsFrontend>::Get(context); |
| 87 } | 65 } |
| 88 | 66 |
| 89 // static | 67 // static |
| 90 SettingsFrontend* SettingsFrontend::CreateForTesting( | 68 SettingsFrontend* SettingsFrontend::CreateForTesting( |
| 91 const scoped_refptr<SettingsStorageFactory>& storage_factory, | 69 const scoped_refptr<SettingsStorageFactory>& storage_factory, |
| 92 BrowserContext* context) { | 70 BrowserContext* context) { |
| 93 return new SettingsFrontend(storage_factory, context); | 71 return new SettingsFrontend(storage_factory, context); |
| 94 } | 72 } |
| 95 | 73 |
| 96 SettingsFrontend::SettingsFrontend(BrowserContext* context) | 74 SettingsFrontend::SettingsFrontend(BrowserContext* context) |
| 97 : local_quota_limit_(GetLocalLimits()), | 75 : browser_context_(context) { |
| 98 sync_quota_limit_(GetSyncLimits()), | |
| 99 browser_context_(context) { | |
| 100 Init(new LeveldbSettingsStorageFactory()); | 76 Init(new LeveldbSettingsStorageFactory()); |
| 101 } | 77 } |
| 102 | 78 |
| 103 SettingsFrontend::SettingsFrontend( | 79 SettingsFrontend::SettingsFrontend( |
| 104 const scoped_refptr<SettingsStorageFactory>& factory, | 80 const scoped_refptr<SettingsStorageFactory>& factory, |
| 105 BrowserContext* context) | 81 BrowserContext* context) |
| 106 : local_quota_limit_(GetLocalLimits()), | 82 : browser_context_(context) { |
| 107 sync_quota_limit_(GetSyncLimits()), | |
| 108 browser_context_(context) { | |
| 109 Init(factory); | 83 Init(factory); |
| 110 } | 84 } |
| 111 | 85 |
| 112 void SettingsFrontend::Init( | 86 void SettingsFrontend::Init( |
| 113 const scoped_refptr<SettingsStorageFactory>& factory) { | 87 const scoped_refptr<SettingsStorageFactory>& factory) { |
| 114 observers_ = new SettingsObserverList(); | 88 observers_ = new SettingsObserverList(); |
| 115 browser_context_observer_.reset(new DefaultObserver(browser_context_)); | 89 browser_context_observer_.reset(new DefaultObserver(browser_context_)); |
| 116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 117 DCHECK(!browser_context_->IsOffTheRecord()); | 91 DCHECK(!browser_context_->IsOffTheRecord()); |
| 118 | 92 |
| 119 observers_->AddObserver(browser_context_observer_.get()); | 93 observers_->AddObserver(browser_context_observer_.get()); |
| 120 | 94 |
| 121 const base::FilePath& browser_context_path = browser_context_->GetPath(); | |
| 122 caches_[settings_namespace::LOCAL] = | 95 caches_[settings_namespace::LOCAL] = |
| 123 new SyncOrLocalValueStoreCache( | 96 new LocalValueStoreCache(factory, browser_context_->GetPath()); |
|
Yoyo Zhou
2014/03/06 00:43:22
Why not do this one in the APIClient too?
James Cook
2014/03/06 00:47:14
Do you mean to have the APIClient always be respon
Yoyo Zhou
2014/03/06 01:22:54
I don't have a strong preference. If it requires a
| |
| 124 settings_namespace::LOCAL, | |
| 125 factory, | |
| 126 local_quota_limit_, | |
| 127 observers_, | |
| 128 browser_context_path); | |
| 129 caches_[settings_namespace::SYNC] = | |
| 130 new SyncOrLocalValueStoreCache( | |
| 131 settings_namespace::SYNC, | |
| 132 factory, | |
| 133 sync_quota_limit_, | |
| 134 observers_, | |
| 135 browser_context_path); | |
| 136 | 97 |
| 137 // Add any additional caches the embedder supports (for example, a cache | 98 // Add any additional caches the embedder supports (for example, caches |
| 138 // for chrome.storage.managed). | 99 // for chrome.storage.managed and chrome.storage.sync). |
| 139 ExtensionsAPIClient::Get()->AddAdditionalValueStoreCaches( | 100 ExtensionsAPIClient::Get()->AddAdditionalValueStoreCaches( |
| 140 browser_context_, factory, observers_, &caches_); | 101 browser_context_, factory, observers_, &caches_); |
| 141 } | 102 } |
| 142 | 103 |
| 143 SettingsFrontend::~SettingsFrontend() { | 104 SettingsFrontend::~SettingsFrontend() { |
| 144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 145 observers_->RemoveObserver(browser_context_observer_.get()); | 106 observers_->RemoveObserver(browser_context_observer_.get()); |
| 146 for (CacheMap::iterator it = caches_.begin(); it != caches_.end(); ++it) { | 107 for (CacheMap::iterator it = caches_.begin(); it != caches_.end(); ++it) { |
| 147 ValueStoreCache* cache = it->second; | 108 ValueStoreCache* cache = it->second; |
| 148 cache->ShutdownOnUI(); | 109 cache->ShutdownOnUI(); |
| 149 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, cache); | 110 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, cache); |
| 150 } | 111 } |
| 151 } | 112 } |
| 152 | 113 |
| 153 syncer::SyncableService* SettingsFrontend::GetBackendForSync( | 114 ValueStoreCache* SettingsFrontend::GetValueStoreCache( |
| 154 syncer::ModelType type) const { | 115 settings_namespace::Namespace settings_namespace) const { |
| 155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 116 CacheMap::const_iterator it = caches_.find(settings_namespace); |
| 156 CacheMap::const_iterator it = caches_.find(settings_namespace::SYNC); | 117 if (it != caches_.end()) |
| 157 DCHECK(it != caches_.end()); | 118 return it->second; |
| 158 const SyncOrLocalValueStoreCache* sync_cache = | 119 return NULL; |
| 159 static_cast<const SyncOrLocalValueStoreCache*>(it->second); | |
| 160 DCHECK(type == syncer::APP_SETTINGS || type == syncer::EXTENSION_SETTINGS); | |
| 161 return sync_cache->GetSyncableService(type); | |
| 162 } | 120 } |
| 163 | 121 |
| 164 bool SettingsFrontend::IsStorageEnabled( | 122 bool SettingsFrontend::IsStorageEnabled( |
| 165 settings_namespace::Namespace settings_namespace) const { | 123 settings_namespace::Namespace settings_namespace) const { |
| 166 return caches_.find(settings_namespace) != caches_.end(); | 124 return caches_.find(settings_namespace) != caches_.end(); |
| 167 } | 125 } |
| 168 | 126 |
| 169 void SettingsFrontend::RunWithStorage( | 127 void SettingsFrontend::RunWithStorage( |
| 170 scoped_refptr<const Extension> extension, | 128 scoped_refptr<const Extension> extension, |
| 171 settings_namespace::Namespace settings_namespace, | 129 settings_namespace::Namespace settings_namespace, |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 SettingsFrontend::GetFactoryInstance() { | 176 SettingsFrontend::GetFactoryInstance() { |
| 219 return g_factory.Pointer(); | 177 return g_factory.Pointer(); |
| 220 } | 178 } |
| 221 | 179 |
| 222 // static | 180 // static |
| 223 const char* SettingsFrontend::service_name() { | 181 const char* SettingsFrontend::service_name() { |
| 224 return "SettingsFrontend"; | 182 return "SettingsFrontend"; |
| 225 } | 183 } |
| 226 | 184 |
| 227 } // namespace extensions | 185 } // namespace extensions |
| OLD | NEW |