| 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/managed_value_store_cache.h" | 5 #include "chrome/browser/extensions/api/storage/managed_value_store_cache.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/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/scoped_observer.h" | 16 #include "base/scoped_observer.h" |
| 17 #include "base/threading/sequenced_worker_pool.h" | 17 #include "base/threading/sequenced_worker_pool.h" |
| 18 #include "chrome/browser/extensions/api/storage/policy_value_store.h" | 18 #include "chrome/browser/extensions/api/storage/policy_value_store.h" |
| 19 #include "chrome/browser/policy/profile_policy_connector.h" | 19 #include "chrome/browser/policy/profile_policy_connector.h" |
| 20 #include "chrome/browser/policy/profile_policy_connector_factory.h" | 20 #include "chrome/browser/policy/profile_policy_connector_factory.h" |
| 21 #include "chrome/browser/policy/schema_registry_service.h" | 21 #include "chrome/browser/policy/schema_registry_service.h" |
| 22 #include "chrome/browser/policy/schema_registry_service_factory.h" | 22 #include "chrome/browser/policy/schema_registry_service_factory.h" |
| 23 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
| 24 #include "chrome/common/extensions/api/storage/storage_schema_manifest_handler.h
" | 24 #include "chrome/common/extensions/api/storage/storage_schema_manifest_handler.h
" |
| 25 #include "components/policy/core/common/schema.h" | 25 #include "components/policy/core/common/schema.h" |
| 26 #include "components/policy/core/common/schema_map.h" | 26 #include "components/policy/core/common/schema_map.h" |
| 27 #include "components/policy/core/common/schema_registry.h" | 27 #include "components/policy/core/common/schema_registry.h" |
| 28 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
| 29 #include "extensions/browser/api/storage/backend_task_runner.h" |
| 29 #include "extensions/browser/extension_prefs.h" | 30 #include "extensions/browser/extension_prefs.h" |
| 30 #include "extensions/browser/extension_registry.h" | 31 #include "extensions/browser/extension_registry.h" |
| 31 #include "extensions/browser/extension_registry_observer.h" | 32 #include "extensions/browser/extension_registry_observer.h" |
| 32 #include "extensions/browser/extension_system.h" | 33 #include "extensions/browser/extension_system.h" |
| 33 #include "extensions/browser/value_store/value_store_change.h" | 34 #include "extensions/browser/value_store/value_store_change.h" |
| 34 #include "extensions/browser/value_store/value_store_factory.h" | 35 #include "extensions/browser/value_store/value_store_factory.h" |
| 35 #include "extensions/common/api/storage.h" | 36 #include "extensions/common/api/storage.h" |
| 36 #include "extensions/common/constants.h" | 37 #include "extensions/common/constants.h" |
| 37 #include "extensions/common/extension.h" | 38 #include "extensions/common/extension.h" |
| 38 #include "extensions/common/extension_set.h" | 39 #include "extensions/common/extension_set.h" |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 | 253 |
| 253 policy_service_->AddObserver(policy_domain_, this); | 254 policy_service_->AddObserver(policy_domain_, this); |
| 254 | 255 |
| 255 extension_tracker_.reset(new ExtensionTracker(profile_, policy_domain_)); | 256 extension_tracker_.reset(new ExtensionTracker(profile_, policy_domain_)); |
| 256 | 257 |
| 257 if (policy_service_->IsInitializationComplete(policy_domain_)) | 258 if (policy_service_->IsInitializationComplete(policy_domain_)) |
| 258 OnPolicyServiceInitialized(policy_domain_); | 259 OnPolicyServiceInitialized(policy_domain_); |
| 259 } | 260 } |
| 260 | 261 |
| 261 ManagedValueStoreCache::~ManagedValueStoreCache() { | 262 ManagedValueStoreCache::~ManagedValueStoreCache() { |
| 262 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 263 DCHECK(IsOnBackendSequence()); |
| 263 // Delete the PolicyValueStores on FILE. | 264 // Delete the PolicyValueStores on FILE. |
| 264 store_map_.clear(); | 265 store_map_.clear(); |
| 265 } | 266 } |
| 266 | 267 |
| 267 void ManagedValueStoreCache::ShutdownOnUI() { | 268 void ManagedValueStoreCache::ShutdownOnUI() { |
| 268 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 269 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 269 policy_service_->RemoveObserver(policy_domain_, this); | 270 policy_service_->RemoveObserver(policy_domain_, this); |
| 270 extension_tracker_.reset(); | 271 extension_tracker_.reset(); |
| 271 } | 272 } |
| 272 | 273 |
| 273 void ManagedValueStoreCache::RunWithValueStoreForExtension( | 274 void ManagedValueStoreCache::RunWithValueStoreForExtension( |
| 274 const StorageCallback& callback, | 275 const StorageCallback& callback, |
| 275 scoped_refptr<const Extension> extension) { | 276 scoped_refptr<const Extension> extension) { |
| 276 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 277 DCHECK(IsOnBackendSequence()); |
| 277 callback.Run(GetStoreFor(extension->id())); | 278 callback.Run(GetStoreFor(extension->id())); |
| 278 } | 279 } |
| 279 | 280 |
| 280 void ManagedValueStoreCache::DeleteStorageSoon( | 281 void ManagedValueStoreCache::DeleteStorageSoon( |
| 281 const std::string& extension_id) { | 282 const std::string& extension_id) { |
| 282 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 283 DCHECK(IsOnBackendSequence()); |
| 283 // It's possible that the store exists, but hasn't been loaded yet | 284 // It's possible that the store exists, but hasn't been loaded yet |
| 284 // (because the extension is unloaded, for example). Open the database to | 285 // (because the extension is unloaded, for example). Open the database to |
| 285 // clear it if it exists. | 286 // clear it if it exists. |
| 286 if (!HasStore(extension_id)) | 287 if (!HasStore(extension_id)) |
| 287 return; | 288 return; |
| 288 GetStoreFor(extension_id)->DeleteStorage(); | 289 GetStoreFor(extension_id)->DeleteStorage(); |
| 289 store_map_.erase(extension_id); | 290 store_map_.erase(extension_id); |
| 290 } | 291 } |
| 291 | 292 |
| 292 void ManagedValueStoreCache::OnPolicyServiceInitialized( | 293 void ManagedValueStoreCache::OnPolicyServiceInitialized( |
| (...skipping 27 matching lines...) Expand all Loading... |
| 320 const policy::PolicyMap& current) { | 321 const policy::PolicyMap& current) { |
| 321 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 322 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 322 | 323 |
| 323 if (!policy_service_->IsInitializationComplete(policy_domain_)) { | 324 if (!policy_service_->IsInitializationComplete(policy_domain_)) { |
| 324 // OnPolicyUpdated is called whenever a policy changes, but it doesn't | 325 // OnPolicyUpdated is called whenever a policy changes, but it doesn't |
| 325 // mean that all the policy providers are ready; wait until we get the | 326 // mean that all the policy providers are ready; wait until we get the |
| 326 // final policy values before passing them to the store. | 327 // final policy values before passing them to the store. |
| 327 return; | 328 return; |
| 328 } | 329 } |
| 329 | 330 |
| 330 BrowserThread::PostTask( | 331 GetBackendTaskRunner()->PostTask( |
| 331 BrowserThread::FILE, FROM_HERE, | 332 FROM_HERE, base::BindOnce(&ManagedValueStoreCache::UpdatePolicyOnBackend, |
| 332 base::BindOnce(&ManagedValueStoreCache::UpdatePolicyOnFILE, | 333 base::Unretained(this), ns.component_id, |
| 333 base::Unretained(this), ns.component_id, | 334 base::Passed(current.DeepCopy()))); |
| 334 base::Passed(current.DeepCopy()))); | |
| 335 } | 335 } |
| 336 | 336 |
| 337 // static | 337 // static |
| 338 policy::PolicyDomain ManagedValueStoreCache::GetPolicyDomain(Profile* profile) { | 338 policy::PolicyDomain ManagedValueStoreCache::GetPolicyDomain(Profile* profile) { |
| 339 #if defined(OS_CHROMEOS) | 339 #if defined(OS_CHROMEOS) |
| 340 return chromeos::ProfileHelper::IsSigninProfile(profile) | 340 return chromeos::ProfileHelper::IsSigninProfile(profile) |
| 341 ? policy::POLICY_DOMAIN_SIGNIN_EXTENSIONS | 341 ? policy::POLICY_DOMAIN_SIGNIN_EXTENSIONS |
| 342 : policy::POLICY_DOMAIN_EXTENSIONS; | 342 : policy::POLICY_DOMAIN_EXTENSIONS; |
| 343 #else | 343 #else |
| 344 return policy::POLICY_DOMAIN_EXTENSIONS; | 344 return policy::POLICY_DOMAIN_EXTENSIONS; |
| 345 #endif | 345 #endif |
| 346 } | 346 } |
| 347 | 347 |
| 348 void ManagedValueStoreCache::UpdatePolicyOnFILE( | 348 void ManagedValueStoreCache::UpdatePolicyOnBackend( |
| 349 const std::string& extension_id, | 349 const std::string& extension_id, |
| 350 std::unique_ptr<policy::PolicyMap> current_policy) { | 350 std::unique_ptr<policy::PolicyMap> current_policy) { |
| 351 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 351 DCHECK(IsOnBackendSequence()); |
| 352 | 352 |
| 353 if (!HasStore(extension_id) && current_policy->empty()) { | 353 if (!HasStore(extension_id) && current_policy->empty()) { |
| 354 // Don't create the store now if there are no policies configured for this | 354 // Don't create the store now if there are no policies configured for this |
| 355 // extension. If the extension uses the storage.managed API then the store | 355 // extension. If the extension uses the storage.managed API then the store |
| 356 // will be created at RunWithValueStoreForExtension(). | 356 // will be created at RunWithValueStoreForExtension(). |
| 357 return; | 357 return; |
| 358 } | 358 } |
| 359 | 359 |
| 360 GetStoreFor(extension_id)->SetCurrentPolicy(*current_policy); | 360 GetStoreFor(extension_id)->SetCurrentPolicy(*current_policy); |
| 361 } | 361 } |
| 362 | 362 |
| 363 PolicyValueStore* ManagedValueStoreCache::GetStoreFor( | 363 PolicyValueStore* ManagedValueStoreCache::GetStoreFor( |
| 364 const std::string& extension_id) { | 364 const std::string& extension_id) { |
| 365 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 365 DCHECK(IsOnBackendSequence()); |
| 366 | 366 |
| 367 auto it = store_map_.find(extension_id); | 367 auto it = store_map_.find(extension_id); |
| 368 if (it != store_map_.end()) | 368 if (it != store_map_.end()) |
| 369 return it->second.get(); | 369 return it->second.get(); |
| 370 | 370 |
| 371 // Create the store now, and serve the cached policy until the PolicyService | 371 // Create the store now, and serve the cached policy until the PolicyService |
| 372 // sends updated values. | 372 // sends updated values. |
| 373 std::unique_ptr<PolicyValueStore> store(new PolicyValueStore( | 373 std::unique_ptr<PolicyValueStore> store(new PolicyValueStore( |
| 374 extension_id, observers_, | 374 extension_id, observers_, |
| 375 storage_factory_->CreateSettingsStore(settings_namespace::MANAGED, | 375 storage_factory_->CreateSettingsStore(settings_namespace::MANAGED, |
| 376 kManagedModelType, extension_id))); | 376 kManagedModelType, extension_id))); |
| 377 PolicyValueStore* raw_store = store.get(); | 377 PolicyValueStore* raw_store = store.get(); |
| 378 store_map_[extension_id] = std::move(store); | 378 store_map_[extension_id] = std::move(store); |
| 379 | 379 |
| 380 return raw_store; | 380 return raw_store; |
| 381 } | 381 } |
| 382 | 382 |
| 383 bool ManagedValueStoreCache::HasStore(const std::string& extension_id) const { | 383 bool ManagedValueStoreCache::HasStore(const std::string& extension_id) const { |
| 384 // Note: Currently only manage extensions (not apps). | 384 // Note: Currently only manage extensions (not apps). |
| 385 return storage_factory_->HasSettings(settings_namespace::MANAGED, | 385 return storage_factory_->HasSettings(settings_namespace::MANAGED, |
| 386 kManagedModelType, extension_id); | 386 kManagedModelType, extension_id); |
| 387 } | 387 } |
| 388 | 388 |
| 389 } // namespace extensions | 389 } // namespace extensions |
| OLD | NEW |