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; | 27 namespace storage = api::storage; |
|
Devlin
2014/03/05 23:30:53
nit: since this is now only used on line 53, pleas
James Cook
2014/03/05 23:45:19
Done.
| |
| 30 | 28 |
| 31 namespace { | 29 namespace { |
| 32 | 30 |
| 33 base::LazyInstance<BrowserContextKeyedAPIFactory<SettingsFrontend> > g_factory = | 31 base::LazyInstance<BrowserContextKeyedAPIFactory<SettingsFrontend> > g_factory = |
| 34 LAZY_INSTANCE_INITIALIZER; | 32 LAZY_INSTANCE_INITIALIZER; |
| 35 | 33 |
| 36 // Settings change Observer which forwards changes on to the extension | 34 // Settings change Observer which forwards changes on to the extension |
| 37 // processes for |context| and its incognito partner if it exists. | 35 // processes for |context| and its incognito partner if it exists. |
| 38 class DefaultObserver : public SettingsObserver { | 36 class DefaultObserver : public SettingsObserver { |
| 39 public: | 37 public: |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 54 scoped_ptr<Event> event(new Event( | 52 scoped_ptr<Event> event(new Event( |
| 55 storage::OnChanged::kEventName, args.Pass())); | 53 storage::OnChanged::kEventName, args.Pass())); |
| 56 ExtensionSystem::Get(browser_context_)->event_router()-> | 54 ExtensionSystem::Get(browser_context_)->event_router()-> |
| 57 DispatchEventToExtension(extension_id, event.Pass()); | 55 DispatchEventToExtension(extension_id, event.Pass()); |
| 58 } | 56 } |
| 59 | 57 |
| 60 private: | 58 private: |
| 61 BrowserContext* const browser_context_; | 59 BrowserContext* const browser_context_; |
| 62 }; | 60 }; |
| 63 | 61 |
| 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 | 62 } // namespace |
| 83 | 63 |
| 84 // static | 64 // static |
| 85 SettingsFrontend* SettingsFrontend::Get(BrowserContext* context) { | 65 SettingsFrontend* SettingsFrontend::Get(BrowserContext* context) { |
| 86 return BrowserContextKeyedAPIFactory<SettingsFrontend>::Get(context); | 66 return BrowserContextKeyedAPIFactory<SettingsFrontend>::Get(context); |
| 87 } | 67 } |
| 88 | 68 |
| 89 // static | 69 // static |
| 90 SettingsFrontend* SettingsFrontend::CreateForTesting( | 70 SettingsFrontend* SettingsFrontend::CreateForTesting( |
| 91 const scoped_refptr<SettingsStorageFactory>& storage_factory, | 71 const scoped_refptr<SettingsStorageFactory>& storage_factory, |
| 92 BrowserContext* context) { | 72 BrowserContext* context) { |
| 93 return new SettingsFrontend(storage_factory, context); | 73 return new SettingsFrontend(storage_factory, context); |
| 94 } | 74 } |
| 95 | 75 |
| 96 SettingsFrontend::SettingsFrontend(BrowserContext* context) | 76 SettingsFrontend::SettingsFrontend(BrowserContext* context) |
| 97 : local_quota_limit_(GetLocalLimits()), | 77 : browser_context_(context) { |
| 98 sync_quota_limit_(GetSyncLimits()), | |
| 99 browser_context_(context) { | |
| 100 Init(new LeveldbSettingsStorageFactory()); | 78 Init(new LeveldbSettingsStorageFactory()); |
| 101 } | 79 } |
| 102 | 80 |
| 103 SettingsFrontend::SettingsFrontend( | 81 SettingsFrontend::SettingsFrontend( |
| 104 const scoped_refptr<SettingsStorageFactory>& factory, | 82 const scoped_refptr<SettingsStorageFactory>& factory, |
| 105 BrowserContext* context) | 83 BrowserContext* context) |
| 106 : local_quota_limit_(GetLocalLimits()), | 84 : browser_context_(context) { |
| 107 sync_quota_limit_(GetSyncLimits()), | |
| 108 browser_context_(context) { | |
| 109 Init(factory); | 85 Init(factory); |
| 110 } | 86 } |
| 111 | 87 |
| 112 void SettingsFrontend::Init( | 88 void SettingsFrontend::Init( |
| 113 const scoped_refptr<SettingsStorageFactory>& factory) { | 89 const scoped_refptr<SettingsStorageFactory>& factory) { |
| 114 observers_ = new SettingsObserverList(); | 90 observers_ = new SettingsObserverList(); |
| 115 browser_context_observer_.reset(new DefaultObserver(browser_context_)); | 91 browser_context_observer_.reset(new DefaultObserver(browser_context_)); |
| 116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 117 DCHECK(!browser_context_->IsOffTheRecord()); | 93 DCHECK(!browser_context_->IsOffTheRecord()); |
| 118 | 94 |
| 119 observers_->AddObserver(browser_context_observer_.get()); | 95 observers_->AddObserver(browser_context_observer_.get()); |
| 120 | 96 |
| 121 const base::FilePath& browser_context_path = browser_context_->GetPath(); | |
| 122 caches_[settings_namespace::LOCAL] = | 97 caches_[settings_namespace::LOCAL] = |
| 123 new SyncOrLocalValueStoreCache( | 98 new LocalValueStoreCache(factory, browser_context_->GetPath()); |
| 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 | 99 |
| 137 // Add any additional caches the embedder supports (for example, a cache | 100 // Add any additional caches the embedder supports (for example, caches |
| 138 // for chrome.storage.managed). | 101 // for chrome.storage.managed and chrome.storage.sync). |
| 139 ExtensionsAPIClient::Get()->AddAdditionalValueStoreCaches( | 102 ExtensionsAPIClient::Get()->AddAdditionalValueStoreCaches( |
| 140 browser_context_, factory, observers_, &caches_); | 103 browser_context_, factory, observers_, &caches_); |
| 141 } | 104 } |
| 142 | 105 |
| 143 SettingsFrontend::~SettingsFrontend() { | 106 SettingsFrontend::~SettingsFrontend() { |
| 144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 145 observers_->RemoveObserver(browser_context_observer_.get()); | 108 observers_->RemoveObserver(browser_context_observer_.get()); |
| 146 for (CacheMap::iterator it = caches_.begin(); it != caches_.end(); ++it) { | 109 for (CacheMap::iterator it = caches_.begin(); it != caches_.end(); ++it) { |
| 147 ValueStoreCache* cache = it->second; | 110 ValueStoreCache* cache = it->second; |
| 148 cache->ShutdownOnUI(); | 111 cache->ShutdownOnUI(); |
| 149 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, cache); | 112 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, cache); |
| 150 } | 113 } |
| 151 } | 114 } |
| 152 | 115 |
| 153 syncer::SyncableService* SettingsFrontend::GetBackendForSync( | 116 ValueStoreCache* SettingsFrontend::GetValueStoreCache( |
| 154 syncer::ModelType type) const { | 117 settings_namespace::Namespace settings_namespace) const { |
| 155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 118 CacheMap::const_iterator it = caches_.find(settings_namespace); |
| 156 CacheMap::const_iterator it = caches_.find(settings_namespace::SYNC); | 119 if (it != caches_.end()) |
| 157 DCHECK(it != caches_.end()); | 120 return it->second; |
| 158 const SyncOrLocalValueStoreCache* sync_cache = | 121 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 } | 122 } |
| 163 | 123 |
| 164 bool SettingsFrontend::IsStorageEnabled( | 124 bool SettingsFrontend::IsStorageEnabled( |
| 165 settings_namespace::Namespace settings_namespace) const { | 125 settings_namespace::Namespace settings_namespace) const { |
| 166 return caches_.find(settings_namespace) != caches_.end(); | 126 return caches_.find(settings_namespace) != caches_.end(); |
| 167 } | 127 } |
| 168 | 128 |
| 169 void SettingsFrontend::RunWithStorage( | 129 void SettingsFrontend::RunWithStorage( |
| 170 scoped_refptr<const Extension> extension, | 130 scoped_refptr<const Extension> extension, |
| 171 settings_namespace::Namespace settings_namespace, | 131 settings_namespace::Namespace settings_namespace, |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 SettingsFrontend::GetFactoryInstance() { | 178 SettingsFrontend::GetFactoryInstance() { |
| 219 return g_factory.Pointer(); | 179 return g_factory.Pointer(); |
| 220 } | 180 } |
| 221 | 181 |
| 222 // static | 182 // static |
| 223 const char* SettingsFrontend::service_name() { | 183 const char* SettingsFrontend::service_name() { |
| 224 return "SettingsFrontend"; | 184 return "SettingsFrontend"; |
| 225 } | 185 } |
| 226 | 186 |
| 227 } // namespace extensions | 187 } // namespace extensions |
| OLD | NEW |