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/managed_value_store_cache.h" | 5 #include "chrome/browser/extensions/api/storage/managed_value_store_cache.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "chrome/browser/chrome_notification_types.h" | 13 #include "base/scoped_observer.h" |
| 14 #include "chrome/browser/extensions/api/storage/policy_value_store.h" | 14 #include "chrome/browser/extensions/api/storage/policy_value_store.h" |
| 15 #include "chrome/browser/policy/profile_policy_connector.h" | 15 #include "chrome/browser/policy/profile_policy_connector.h" |
| 16 #include "chrome/browser/policy/profile_policy_connector_factory.h" | 16 #include "chrome/browser/policy/profile_policy_connector_factory.h" |
| 17 #include "chrome/browser/policy/schema_registry_service.h" | 17 #include "chrome/browser/policy/schema_registry_service.h" |
| 18 #include "chrome/browser/policy/schema_registry_service_factory.h" | 18 #include "chrome/browser/policy/schema_registry_service_factory.h" |
| 19 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 20 #include "chrome/common/extensions/api/storage/storage_schema_manifest_handler.h " | 20 #include "chrome/common/extensions/api/storage/storage_schema_manifest_handler.h " |
| 21 #include "components/policy/core/common/policy_namespace.h" | 21 #include "components/policy/core/common/policy_namespace.h" |
| 22 #include "components/policy/core/common/schema.h" | 22 #include "components/policy/core/common/schema.h" |
| 23 #include "components/policy/core/common/schema_map.h" | 23 #include "components/policy/core/common/schema_map.h" |
| 24 #include "components/policy/core/common/schema_registry.h" | 24 #include "components/policy/core/common/schema_registry.h" |
| 25 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
| 26 #include "content/public/browser/notification_details.h" | |
| 27 #include "content/public/browser/notification_observer.h" | |
| 28 #include "content/public/browser/notification_registrar.h" | |
| 29 #include "content/public/browser/notification_source.h" | |
| 30 #include "extensions/browser/api/storage/settings_storage_factory.h" | 26 #include "extensions/browser/api/storage/settings_storage_factory.h" |
| 31 #include "extensions/browser/extension_prefs.h" | 27 #include "extensions/browser/extension_prefs.h" |
| 32 #include "extensions/browser/extension_registry.h" | 28 #include "extensions/browser/extension_registry.h" |
| 29 #include "extensions/browser/extension_registry_observer.h" | |
| 33 #include "extensions/browser/extension_system.h" | 30 #include "extensions/browser/extension_system.h" |
| 34 #include "extensions/browser/value_store/value_store_change.h" | 31 #include "extensions/browser/value_store/value_store_change.h" |
| 35 #include "extensions/common/api/storage.h" | 32 #include "extensions/common/api/storage.h" |
| 36 #include "extensions/common/constants.h" | 33 #include "extensions/common/constants.h" |
| 37 #include "extensions/common/extension.h" | 34 #include "extensions/common/extension.h" |
| 38 #include "extensions/common/extension_set.h" | 35 #include "extensions/common/extension_set.h" |
| 39 #include "extensions/common/manifest.h" | 36 #include "extensions/common/manifest.h" |
| 40 #include "extensions/common/manifest_constants.h" | 37 #include "extensions/common/manifest_constants.h" |
| 41 #include "extensions/common/one_shot_event.h" | 38 #include "extensions/common/one_shot_event.h" |
| 42 | 39 |
| 43 using content::BrowserContext; | 40 using content::BrowserContext; |
| 44 using content::BrowserThread; | 41 using content::BrowserThread; |
| 45 | 42 |
| 46 namespace extensions { | 43 namespace extensions { |
| 44 class ExtensionRegistry; | |
| 47 | 45 |
| 48 namespace storage = core_api::storage; | 46 namespace storage = core_api::storage; |
| 49 | 47 |
| 50 namespace { | 48 namespace { |
| 51 | 49 |
| 52 const char kLoadSchemasBackgroundTaskTokenName[] = | 50 const char kLoadSchemasBackgroundTaskTokenName[] = |
| 53 "load_managed_storage_schemas_token"; | 51 "load_managed_storage_schemas_token"; |
| 54 | 52 |
| 55 // The Legacy Browser Support was the first user of the policy-for-extensions | 53 // The Legacy Browser Support was the first user of the policy-for-extensions |
| 56 // API, and relied on behavior that will be phased out. If this extension is | 54 // API, and relied on behavior that will be phased out. If this extension is |
| 57 // present then its policies will be loaded in a special way. | 55 // present then its policies will be loaded in a special way. |
| 58 // TODO(joaodasilva): remove this for M35. http://crbug.com/325349 | 56 // TODO(joaodasilva): remove this for M35. http://crbug.com/325349 |
| 59 const char kLegacyBrowserSupportExtensionId[] = | 57 const char kLegacyBrowserSupportExtensionId[] = |
| 60 "heildphpnddilhkemkielfhnkaagiabh"; | 58 "heildphpnddilhkemkielfhnkaagiabh"; |
| 61 | 59 |
| 62 } // namespace | 60 } // namespace |
| 63 | 61 |
| 64 // This helper observes initialization of all the installed extensions and | 62 // This helper observes initialization of all the installed extensions and |
| 65 // subsequent loads and unloads, and keeps the SchemaRegistry of the Profile | 63 // subsequent loads and unloads, and keeps the SchemaRegistry of the Profile |
| 66 // in sync with the current list of extensions. This allows the PolicyService | 64 // in sync with the current list of extensions. This allows the PolicyService |
| 67 // to fetch cloud policy for those extensions, and allows its providers to | 65 // to fetch cloud policy for those extensions, and allows its providers to |
| 68 // selectively load only extension policy that has users. | 66 // selectively load only extension policy that has users. |
| 69 class ManagedValueStoreCache::ExtensionTracker | 67 class ManagedValueStoreCache::ExtensionTracker |
| 70 : public content::NotificationObserver { | 68 : public ExtensionRegistryObserver { |
| 71 public: | 69 public: |
| 72 explicit ExtensionTracker(Profile* profile); | 70 explicit ExtensionTracker(Profile* profile); |
| 73 virtual ~ExtensionTracker() {} | 71 virtual ~ExtensionTracker() {} |
| 74 | 72 |
| 75 // NotificationObserver implementation: | 73 private: |
| 76 virtual void Observe(int type, | 74 // ExtensionRegistryObserver implementation. |
| 77 const content::NotificationSource& source, | 75 virtual void OnExtensionWillBeInstalled( |
| 78 const content::NotificationDetails& details) OVERRIDE; | 76 content::BrowserContext* browser_context, |
| 77 const Extension* extension, | |
| 78 bool is_update, | |
| 79 const std::string& old_name) OVERRIDE; | |
| 80 virtual void OnExtensionUninstalled(content::BrowserContext* browser_context, | |
| 81 const Extension* extension) OVERRIDE; | |
| 79 | 82 |
| 80 private: | |
| 81 // Handler for the signal from ExtensionSystem::ready(). | 83 // Handler for the signal from ExtensionSystem::ready(). |
| 82 void OnExtensionsReady(); | 84 void OnExtensionsReady(); |
| 83 | 85 |
| 84 // Starts a schema load for all extensions that use managed storage. | 86 // Starts a schema load for all extensions that use managed storage. |
| 85 void LoadSchemas(scoped_ptr<ExtensionSet> added); | 87 void LoadSchemas(scoped_ptr<ExtensionSet> added); |
| 86 | 88 |
| 87 bool UsesManagedStorage(const Extension* extension) const; | 89 bool UsesManagedStorage(const Extension* extension) const; |
| 88 | 90 |
| 89 // Loads the schemas of the |extensions| and passes a ComponentMap to | 91 // Loads the schemas of the |extensions| and passes a ComponentMap to |
| 90 // Register(). | 92 // Register(). |
| 91 static void LoadSchemasOnBlockingPool(scoped_ptr<ExtensionSet> extensions, | 93 static void LoadSchemasOnBlockingPool(scoped_ptr<ExtensionSet> extensions, |
| 92 base::WeakPtr<ExtensionTracker> self); | 94 base::WeakPtr<ExtensionTracker> self); |
| 93 void Register(const policy::ComponentMap* components); | 95 void Register(const policy::ComponentMap* components); |
| 94 | 96 |
| 95 Profile* profile_; | 97 Profile* profile_; |
| 96 content::NotificationRegistrar registrar_; | 98 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
| 99 extension_registry_observer_; | |
| 97 policy::SchemaRegistry* schema_registry_; | 100 policy::SchemaRegistry* schema_registry_; |
| 98 base::WeakPtrFactory<ExtensionTracker> weak_factory_; | 101 base::WeakPtrFactory<ExtensionTracker> weak_factory_; |
| 99 | 102 |
| 100 DISALLOW_COPY_AND_ASSIGN(ExtensionTracker); | 103 DISALLOW_COPY_AND_ASSIGN(ExtensionTracker); |
| 101 }; | 104 }; |
| 102 | 105 |
| 103 ManagedValueStoreCache::ExtensionTracker::ExtensionTracker(Profile* profile) | 106 ManagedValueStoreCache::ExtensionTracker::ExtensionTracker(Profile* profile) |
| 104 : profile_(profile), | 107 : profile_(profile), |
| 108 extension_registry_observer_(this), | |
| 105 schema_registry_( | 109 schema_registry_( |
| 106 policy::SchemaRegistryServiceFactory::GetForContext(profile)), | 110 policy::SchemaRegistryServiceFactory::GetForContext(profile)), |
| 107 weak_factory_(this) { | 111 weak_factory_(this) { |
| 108 registrar_.Add(this, | 112 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_)); |
| 109 chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED, | |
| 110 content::Source<Profile>(profile_)); | |
| 111 registrar_.Add(this, | |
| 112 chrome::NOTIFICATION_EXTENSION_UNINSTALLED, | |
| 113 content::Source<Profile>(profile_)); | |
| 114 | |
| 115 // Load schemas when the extension system is ready. It might be ready now. | 113 // Load schemas when the extension system is ready. It might be ready now. |
| 116 ExtensionSystem::Get(profile_)->ready().Post( | 114 ExtensionSystem::Get(profile_)->ready().Post( |
| 117 FROM_HERE, | 115 FROM_HERE, |
| 118 base::Bind(&ExtensionTracker::OnExtensionsReady, | 116 base::Bind(&ExtensionTracker::OnExtensionsReady, |
| 119 weak_factory_.GetWeakPtr())); | 117 weak_factory_.GetWeakPtr())); |
| 120 } | 118 } |
| 121 | 119 |
| 122 void ManagedValueStoreCache::ExtensionTracker::Observe( | 120 void ManagedValueStoreCache::ExtensionTracker::OnExtensionWillBeInstalled( |
| 123 int type, | 121 content::BrowserContext* browser_context, |
| 124 const content::NotificationSource& source, | 122 const Extension* extension, |
| 125 const content::NotificationDetails& details) { | 123 bool is_update, |
| 124 const std::string& old_name) { | |
| 126 // Some extensions are installed on the first run before the ExtensionSystem | 125 // Some extensions are installed on the first run before the ExtensionSystem |
| 127 // becomes ready. Wait until all of them are ready before registering the | 126 // becomes ready. Wait until all of them are ready before registering the |
| 128 // schemas of managed extensions, so that the policy loaders are reloaded at | 127 // schemas of managed extensions, so that the policy loaders are reloaded at |
| 129 // most once. | 128 // most once. |
| 130 if (!ExtensionSystem::Get(profile_)->ready().is_signaled()) | 129 if (!ExtensionSystem::Get(profile_)->ready().is_signaled()) |
| 131 return; | 130 return; |
| 132 | 131 scoped_ptr<ExtensionSet> added(new ExtensionSet); |
| 133 switch (type) { | 132 added->Insert(extension); |
| 134 case chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED: { | 133 LoadSchemas(added.Pass()); |
| 135 scoped_ptr<ExtensionSet> added(new ExtensionSet); | 134 } |
| 136 added->Insert( | 135 void ManagedValueStoreCache::ExtensionTracker::OnExtensionUninstalled( |
| 137 content::Details<InstalledExtensionInfo>(details)->extension); | 136 content::BrowserContext* browser_context, |
| 138 LoadSchemas(added.Pass()); | 137 const Extension* extension) { |
|
Devlin
2014/05/28 16:58:34
I don't _think_ this can ever happen, but the if s
limasdf
2014/05/28 17:38:43
Done.
The reason why I removed is the comment says
| |
| 139 break; | 138 if (extension && UsesManagedStorage(extension)) { |
| 140 } | 139 schema_registry_->UnregisterComponent(policy::PolicyNamespace( |
| 141 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: { | 140 policy::POLICY_DOMAIN_EXTENSIONS, extension->id())); |
| 142 const Extension* removed = | |
| 143 content::Details<const Extension>(details).ptr(); | |
| 144 if (removed && UsesManagedStorage(removed)) { | |
| 145 schema_registry_->UnregisterComponent(policy::PolicyNamespace( | |
| 146 policy::POLICY_DOMAIN_EXTENSIONS, removed->id())); | |
| 147 } | |
| 148 break; | |
| 149 } | |
| 150 default: | |
| 151 NOTREACHED(); | |
| 152 return; | |
| 153 } | 141 } |
| 154 } | 142 } |
| 155 | 143 |
| 156 void ManagedValueStoreCache::ExtensionTracker::OnExtensionsReady() { | 144 void ManagedValueStoreCache::ExtensionTracker::OnExtensionsReady() { |
| 157 // Load schemas for all installed extensions. | 145 // Load schemas for all installed extensions. |
| 158 LoadSchemas( | 146 LoadSchemas( |
| 159 ExtensionRegistry::Get(profile_)->GenerateInstalledExtensionsSet()); | 147 ExtensionRegistry::Get(profile_)->GenerateInstalledExtensionsSet()); |
| 160 } | 148 } |
| 161 | 149 |
| 162 void ManagedValueStoreCache::ExtensionTracker::LoadSchemas( | 150 void ManagedValueStoreCache::ExtensionTracker::LoadSchemas( |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 369 | 357 |
| 370 return store; | 358 return store; |
| 371 } | 359 } |
| 372 | 360 |
| 373 bool ManagedValueStoreCache::HasStore(const std::string& extension_id) const { | 361 bool ManagedValueStoreCache::HasStore(const std::string& extension_id) const { |
| 374 // TODO(joaodasilva): move this check to a ValueStore method. | 362 // TODO(joaodasilva): move this check to a ValueStore method. |
| 375 return base::DirectoryExists(base_path_.AppendASCII(extension_id)); | 363 return base::DirectoryExists(base_path_.AppendASCII(extension_id)); |
| 376 } | 364 } |
| 377 | 365 |
| 378 } // namespace extensions | 366 } // namespace extensions |
| OLD | NEW |