| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/browser/api/storage/storage_frontend.h" | 5 #include "extensions/browser/api/storage/storage_frontend.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/json/json_reader.h" | 12 #include "base/json/json_reader.h" |
| 13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 15 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
| 16 #include "base/trace_event/trace_event.h" | 16 #include "base/trace_event/trace_event.h" |
| 17 #include "content/public/browser/browser_context.h" | 17 #include "content/public/browser/browser_context.h" |
| 18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "extensions/browser/api/extensions_api_client.h" | 19 #include "extensions/browser/api/extensions_api_client.h" |
| 20 #include "extensions/browser/api/storage/backend_task_runner.h" |
| 20 #include "extensions/browser/api/storage/local_value_store_cache.h" | 21 #include "extensions/browser/api/storage/local_value_store_cache.h" |
| 21 #include "extensions/browser/event_router.h" | 22 #include "extensions/browser/event_router.h" |
| 22 #include "extensions/browser/extension_registry.h" | 23 #include "extensions/browser/extension_registry.h" |
| 23 #include "extensions/browser/extension_system.h" | 24 #include "extensions/browser/extension_system.h" |
| 24 #include "extensions/browser/value_store/value_store_factory.h" | 25 #include "extensions/browser/value_store/value_store_factory.h" |
| 25 #include "extensions/common/api/storage.h" | 26 #include "extensions/common/api/storage.h" |
| 26 | 27 |
| 27 using content::BrowserContext; | 28 using content::BrowserContext; |
| 28 using content::BrowserThread; | 29 using content::BrowserThread; |
| 29 | 30 |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 ExtensionsAPIClient::Get()->AddAdditionalValueStoreCaches( | 112 ExtensionsAPIClient::Get()->AddAdditionalValueStoreCaches( |
| 112 browser_context_, factory, observers_, &caches_); | 113 browser_context_, factory, observers_, &caches_); |
| 113 } | 114 } |
| 114 | 115 |
| 115 StorageFrontend::~StorageFrontend() { | 116 StorageFrontend::~StorageFrontend() { |
| 116 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 117 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 117 observers_->RemoveObserver(browser_context_observer_.get()); | 118 observers_->RemoveObserver(browser_context_observer_.get()); |
| 118 for (CacheMap::iterator it = caches_.begin(); it != caches_.end(); ++it) { | 119 for (CacheMap::iterator it = caches_.begin(); it != caches_.end(); ++it) { |
| 119 ValueStoreCache* cache = it->second; | 120 ValueStoreCache* cache = it->second; |
| 120 cache->ShutdownOnUI(); | 121 cache->ShutdownOnUI(); |
| 121 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, cache); | 122 GetBackendTaskRunner()->DeleteSoon(FROM_HERE, cache); |
| 122 } | 123 } |
| 123 } | 124 } |
| 124 | 125 |
| 125 ValueStoreCache* StorageFrontend::GetValueStoreCache( | 126 ValueStoreCache* StorageFrontend::GetValueStoreCache( |
| 126 settings_namespace::Namespace settings_namespace) const { | 127 settings_namespace::Namespace settings_namespace) const { |
| 127 CacheMap::const_iterator it = caches_.find(settings_namespace); | 128 CacheMap::const_iterator it = caches_.find(settings_namespace); |
| 128 if (it != caches_.end()) | 129 if (it != caches_.end()) |
| 129 return it->second; | 130 return it->second; |
| 130 return NULL; | 131 return NULL; |
| 131 } | 132 } |
| 132 | 133 |
| 133 bool StorageFrontend::IsStorageEnabled( | 134 bool StorageFrontend::IsStorageEnabled( |
| 134 settings_namespace::Namespace settings_namespace) const { | 135 settings_namespace::Namespace settings_namespace) const { |
| 135 return caches_.find(settings_namespace) != caches_.end(); | 136 return caches_.find(settings_namespace) != caches_.end(); |
| 136 } | 137 } |
| 137 | 138 |
| 138 void StorageFrontend::RunWithStorage( | 139 void StorageFrontend::RunWithStorage( |
| 139 scoped_refptr<const Extension> extension, | 140 scoped_refptr<const Extension> extension, |
| 140 settings_namespace::Namespace settings_namespace, | 141 settings_namespace::Namespace settings_namespace, |
| 141 const ValueStoreCache::StorageCallback& callback) { | 142 const ValueStoreCache::StorageCallback& callback) { |
| 142 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 143 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 143 CHECK(extension.get()); | 144 CHECK(extension.get()); |
| 144 | 145 |
| 145 ValueStoreCache* cache = caches_[settings_namespace]; | 146 ValueStoreCache* cache = caches_[settings_namespace]; |
| 146 CHECK(cache); | 147 CHECK(cache); |
| 147 | 148 |
| 148 BrowserThread::PostTask( | 149 GetBackendTaskRunner()->PostTask( |
| 149 BrowserThread::FILE, FROM_HERE, | 150 FROM_HERE, base::Bind(&ValueStoreCache::RunWithValueStoreForExtension, |
| 150 base::Bind(&ValueStoreCache::RunWithValueStoreForExtension, | 151 base::Unretained(cache), callback, extension)); |
| 151 base::Unretained(cache), callback, extension)); | |
| 152 } | 152 } |
| 153 | 153 |
| 154 void StorageFrontend::DeleteStorageSoon(const std::string& extension_id) { | 154 void StorageFrontend::DeleteStorageSoon(const std::string& extension_id) { |
| 155 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 155 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 156 for (CacheMap::iterator it = caches_.begin(); it != caches_.end(); ++it) { | 156 for (CacheMap::iterator it = caches_.begin(); it != caches_.end(); ++it) { |
| 157 ValueStoreCache* cache = it->second; | 157 ValueStoreCache* cache = it->second; |
| 158 BrowserThread::PostTask( | 158 GetBackendTaskRunner()->PostTask( |
| 159 BrowserThread::FILE, FROM_HERE, | 159 FROM_HERE, base::Bind(&ValueStoreCache::DeleteStorageSoon, |
| 160 base::Bind(&ValueStoreCache::DeleteStorageSoon, | 160 base::Unretained(cache), extension_id)); |
| 161 base::Unretained(cache), | |
| 162 extension_id)); | |
| 163 } | 161 } |
| 164 } | 162 } |
| 165 | 163 |
| 166 scoped_refptr<SettingsObserverList> StorageFrontend::GetObservers() { | 164 scoped_refptr<SettingsObserverList> StorageFrontend::GetObservers() { |
| 167 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 165 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 168 return observers_; | 166 return observers_; |
| 169 } | 167 } |
| 170 | 168 |
| 171 void StorageFrontend::DisableStorageForTesting( | 169 void StorageFrontend::DisableStorageForTesting( |
| 172 settings_namespace::Namespace settings_namespace) { | 170 settings_namespace::Namespace settings_namespace) { |
| 173 CacheMap::iterator it = caches_.find(settings_namespace); | 171 CacheMap::iterator it = caches_.find(settings_namespace); |
| 174 if (it != caches_.end()) { | 172 if (it != caches_.end()) { |
| 175 ValueStoreCache* cache = it->second; | 173 ValueStoreCache* cache = it->second; |
| 176 cache->ShutdownOnUI(); | 174 cache->ShutdownOnUI(); |
| 177 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, cache); | 175 GetBackendTaskRunner()->DeleteSoon(FROM_HERE, cache); |
| 178 caches_.erase(it); | 176 caches_.erase(it); |
| 179 } | 177 } |
| 180 } | 178 } |
| 181 | 179 |
| 182 // BrowserContextKeyedAPI implementation. | 180 // BrowserContextKeyedAPI implementation. |
| 183 | 181 |
| 184 // static | 182 // static |
| 185 BrowserContextKeyedAPIFactory<StorageFrontend>* | 183 BrowserContextKeyedAPIFactory<StorageFrontend>* |
| 186 StorageFrontend::GetFactoryInstance() { | 184 StorageFrontend::GetFactoryInstance() { |
| 187 return g_factory.Pointer(); | 185 return g_factory.Pointer(); |
| 188 } | 186 } |
| 189 | 187 |
| 190 // static | 188 // static |
| 191 const char* StorageFrontend::service_name() { return "StorageFrontend"; } | 189 const char* StorageFrontend::service_name() { return "StorageFrontend"; } |
| 192 | 190 |
| 193 } // namespace extensions | 191 } // namespace extensions |
| OLD | NEW |