| 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/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/message_loop/message_loop_proxy.h" | 13 #include "base/message_loop/message_loop_proxy.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/extensions/api/storage/settings_storage_factory.h" | 15 #include "chrome/browser/extensions/api/storage/settings_storage_factory.h" |
| 16 #include "chrome/browser/extensions/api/storage/storage_schema_manifest_handler.
h" |
| 16 #include "chrome/browser/extensions/event_names.h" | 17 #include "chrome/browser/extensions/event_names.h" |
| 17 #include "chrome/browser/extensions/extension_prefs.h" | 18 #include "chrome/browser/extensions/extension_prefs.h" |
| 18 #include "chrome/browser/extensions/extension_service.h" | 19 #include "chrome/browser/extensions/extension_service.h" |
| 19 #include "chrome/browser/extensions/extension_system.h" | 20 #include "chrome/browser/extensions/extension_system.h" |
| 20 #include "chrome/browser/policy/policy_domain_descriptor.h" | 21 #include "chrome/browser/policy/policy_domain_descriptor.h" |
| 21 #include "chrome/browser/policy/policy_schema.h" | 22 #include "chrome/browser/policy/policy_schema.h" |
| 22 #include "chrome/browser/policy/profile_policy_connector.h" | 23 #include "chrome/browser/policy/profile_policy_connector.h" |
| 23 #include "chrome/browser/policy/profile_policy_connector_factory.h" | 24 #include "chrome/browser/policy/profile_policy_connector_factory.h" |
| 24 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
| 25 #include "chrome/browser/value_store/value_store_change.h" | 26 #include "chrome/browser/value_store/value_store_change.h" |
| 26 #include "chrome/common/extensions/extension.h" | 27 #include "chrome/common/extensions/extension.h" |
| 28 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 27 #include "chrome/common/extensions/extension_set.h" | 29 #include "chrome/common/extensions/extension_set.h" |
| 30 #include "chrome/common/extensions/manifest.h" |
| 28 #include "chrome/common/extensions/permissions/api_permission.h" | 31 #include "chrome/common/extensions/permissions/api_permission.h" |
| 29 #include "content/public/browser/browser_thread.h" | 32 #include "content/public/browser/browser_thread.h" |
| 30 #include "content/public/browser/notification_observer.h" | 33 #include "content/public/browser/notification_observer.h" |
| 31 #include "content/public/browser/notification_registrar.h" | 34 #include "content/public/browser/notification_registrar.h" |
| 32 | 35 |
| 33 using content::BrowserThread; | 36 using content::BrowserThread; |
| 34 | 37 |
| 35 namespace extensions { | 38 namespace extensions { |
| 36 | 39 |
| 40 namespace { |
| 41 |
| 42 const char kLoadSchemasBackgroundTaskTokenName[] = |
| 43 "load_managed_storage_schemas_token"; |
| 44 |
| 45 } // namespace |
| 46 |
| 37 // This helper observes initialization of all the installed extensions and | 47 // This helper observes initialization of all the installed extensions and |
| 38 // subsequent loads and unloads, and keeps the PolicyService of the Profile | 48 // subsequent loads and unloads, and keeps the PolicyService of the Profile |
| 39 // in sync with the current list of extensions. This allows the PolicyService | 49 // in sync with the current list of extensions. This allows the PolicyService |
| 40 // to fetch cloud policy for those extensions, and allows its providers to | 50 // to fetch cloud policy for those extensions, and allows its providers to |
| 41 // selectively load only extension policy that has users. | 51 // selectively load only extension policy that has users. |
| 42 class ManagedValueStoreCache::ExtensionTracker | 52 class ManagedValueStoreCache::ExtensionTracker |
| 43 : public content::NotificationObserver { | 53 : public content::NotificationObserver { |
| 44 public: | 54 public: |
| 45 explicit ExtensionTracker(Profile* profile); | 55 explicit ExtensionTracker(Profile* profile); |
| 46 virtual ~ExtensionTracker() {} | 56 virtual ~ExtensionTracker() {} |
| 47 | 57 |
| 48 // NotificationObserver implementation: | 58 // NotificationObserver implementation: |
| 49 virtual void Observe(int type, | 59 virtual void Observe(int type, |
| 50 const content::NotificationSource& source, | 60 const content::NotificationSource& source, |
| 51 const content::NotificationDetails& details) OVERRIDE; | 61 const content::NotificationDetails& details) OVERRIDE; |
| 52 | 62 |
| 53 private: | 63 private: |
| 64 // Loads the schemas of the |extensions| and passes a PolicyDomainDescriptor |
| 65 // to RegisterDomain(). |
| 66 static void LoadSchemas(scoped_ptr<ExtensionSet> extensions, |
| 67 base::WeakPtr<ExtensionTracker> self); |
| 68 void RegisterDomain( |
| 69 scoped_refptr<const policy::PolicyDomainDescriptor> descriptor); |
| 70 |
| 54 Profile* profile_; | 71 Profile* profile_; |
| 55 content::NotificationRegistrar registrar_; | 72 content::NotificationRegistrar registrar_; |
| 73 base::WeakPtrFactory<ExtensionTracker> weak_factory_; |
| 56 | 74 |
| 57 DISALLOW_COPY_AND_ASSIGN(ExtensionTracker); | 75 DISALLOW_COPY_AND_ASSIGN(ExtensionTracker); |
| 58 }; | 76 }; |
| 59 | 77 |
| 60 ManagedValueStoreCache::ExtensionTracker::ExtensionTracker(Profile* profile) | 78 ManagedValueStoreCache::ExtensionTracker::ExtensionTracker(Profile* profile) |
| 61 : profile_(profile) { | 79 : profile_(profile), |
| 80 weak_factory_(this) { |
| 62 registrar_.Add(this, | 81 registrar_.Add(this, |
| 63 chrome::NOTIFICATION_EXTENSION_LOADED, | 82 chrome::NOTIFICATION_EXTENSION_LOADED, |
| 64 content::Source<Profile>(profile_)); | 83 content::Source<Profile>(profile_)); |
| 65 registrar_.Add(this, | 84 registrar_.Add(this, |
| 66 chrome::NOTIFICATION_EXTENSION_UNLOADED, | 85 chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 67 content::Source<Profile>(profile_)); | 86 content::Source<Profile>(profile_)); |
| 68 } | 87 } |
| 69 | 88 |
| 70 void ManagedValueStoreCache::ExtensionTracker::Observe( | 89 void ManagedValueStoreCache::ExtensionTracker::Observe( |
| 71 int type, | 90 int type, |
| 72 const content::NotificationSource& source, | 91 const content::NotificationSource& source, |
| 73 const content::NotificationDetails& details) { | 92 const content::NotificationDetails& details) { |
| 74 if (!ExtensionSystem::Get(profile_)->ready().is_signaled()) | 93 if (!ExtensionSystem::Get(profile_)->ready().is_signaled()) |
| 75 return; | 94 return; |
| 76 | 95 |
| 77 // TODO(joaodasilva): this currently only registers extensions that use | |
| 78 // the storage API, but that still includes a lot of extensions that don't | |
| 79 // use the storage.managed namespace. Use the presence of a schema for the | |
| 80 // managed namespace in the manifest as a better signal, once available. | |
| 81 scoped_refptr<policy::PolicyDomainDescriptor> descriptor( | 96 scoped_refptr<policy::PolicyDomainDescriptor> descriptor( |
| 82 new policy::PolicyDomainDescriptor(policy::POLICY_DOMAIN_EXTENSIONS)); | 97 new policy::PolicyDomainDescriptor(policy::POLICY_DOMAIN_EXTENSIONS)); |
| 83 const ExtensionSet* set = | 98 const ExtensionSet* set = |
| 84 ExtensionSystem::Get(profile_)->extension_service()->extensions(); | 99 ExtensionSystem::Get(profile_)->extension_service()->extensions(); |
| 100 scoped_ptr<ExtensionSet> managed_extensions(new ExtensionSet()); |
| 85 for (ExtensionSet::const_iterator it = set->begin(); it != set->end(); ++it) { | 101 for (ExtensionSet::const_iterator it = set->begin(); it != set->end(); ++it) { |
| 86 // TODO(joaodasilva): pass the parsed schema here, once available. | 102 if ((*it)->manifest()->HasPath( |
| 87 if ((*it)->HasAPIPermission(APIPermission::kStorage)) { | 103 extension_manifest_keys::kStorageManagedSchema)) { |
| 88 descriptor->RegisterComponent((*it)->id(), | 104 managed_extensions->Insert(*it); |
| 89 scoped_ptr<policy::PolicySchema>()); | |
| 90 } | 105 } |
| 106 |
| 107 // TODO(joaodasilva): also load extensions that use the storage API for now, |
| 108 // to support the Legacy Browser Support extension. Remove this for M30. |
| 109 // http://crbug.com/240704 |
| 110 if ((*it)->HasAPIPermission(APIPermission::kStorage)) |
| 111 managed_extensions->Insert(*it); |
| 91 } | 112 } |
| 92 | 113 |
| 114 // Load the schema files in a background thread. |
| 115 BrowserThread::PostBlockingPoolSequencedTask( |
| 116 kLoadSchemasBackgroundTaskTokenName, FROM_HERE, |
| 117 base::Bind(&ExtensionTracker::LoadSchemas, |
| 118 base::Passed(&managed_extensions), |
| 119 weak_factory_.GetWeakPtr())); |
| 120 } |
| 121 |
| 122 // static |
| 123 void ManagedValueStoreCache::ExtensionTracker::LoadSchemas( |
| 124 scoped_ptr<ExtensionSet> extensions, |
| 125 base::WeakPtr<ExtensionTracker> self) { |
| 126 scoped_refptr<policy::PolicyDomainDescriptor> descriptor = |
| 127 new policy::PolicyDomainDescriptor(policy::POLICY_DOMAIN_EXTENSIONS); |
| 128 |
| 129 for (ExtensionSet::const_iterator it = extensions->begin(); |
| 130 it != extensions->end(); ++it) { |
| 131 std::string schema_file; |
| 132 if (!(*it)->manifest()->GetString( |
| 133 extension_manifest_keys::kStorageManagedSchema, &schema_file)) { |
| 134 // TODO(joaodasilva): Remove this for M30. http://crbug.com/240704 |
| 135 if ((*it)->HasAPIPermission(APIPermission::kStorage)) { |
| 136 descriptor->RegisterComponent((*it)->id(), |
| 137 scoped_ptr<policy::PolicySchema>()); |
| 138 } else { |
| 139 NOTREACHED(); |
| 140 } |
| 141 continue; |
| 142 } |
| 143 // The extension should have been validated, so assume the schema exists |
| 144 // and is valid. |
| 145 std::string error; |
| 146 scoped_ptr<policy::PolicySchema> schema = |
| 147 StorageSchemaManifestHandler::GetSchema(*it, &error); |
| 148 CHECK(schema) << error; |
| 149 descriptor->RegisterComponent((*it)->id(), schema.Pass()); |
| 150 } |
| 151 |
| 152 BrowserThread::PostTask( |
| 153 BrowserThread::UI, FROM_HERE, |
| 154 base::Bind(&ExtensionTracker::RegisterDomain, self, descriptor)); |
| 155 } |
| 156 |
| 157 void ManagedValueStoreCache::ExtensionTracker::RegisterDomain( |
| 158 scoped_refptr<const policy::PolicyDomainDescriptor> descriptor) { |
| 93 policy::ProfilePolicyConnector* connector = | 159 policy::ProfilePolicyConnector* connector = |
| 94 policy::ProfilePolicyConnectorFactory::GetForProfile(profile_); | 160 policy::ProfilePolicyConnectorFactory::GetForProfile(profile_); |
| 95 connector->policy_service()->RegisterPolicyDomain(descriptor); | 161 connector->policy_service()->RegisterPolicyDomain(descriptor); |
| 96 } | 162 } |
| 97 | 163 |
| 98 ManagedValueStoreCache::ManagedValueStoreCache( | 164 ManagedValueStoreCache::ManagedValueStoreCache( |
| 99 Profile* profile, | 165 Profile* profile, |
| 100 const scoped_refptr<SettingsStorageFactory>& factory, | 166 const scoped_refptr<SettingsStorageFactory>& factory, |
| 101 const scoped_refptr<SettingsObserverList>& observers) | 167 const scoped_refptr<SettingsObserverList>& observers) |
| 102 : weak_factory_(this), | 168 : weak_factory_(this), |
| 103 weak_this_on_ui_(weak_factory_.GetWeakPtr()), | 169 weak_this_on_ui_(weak_factory_.GetWeakPtr()), |
| 104 profile_(profile), | 170 profile_(profile), |
| 105 event_router_(ExtensionSystem::Get(profile)->event_router()), | 171 event_router_(ExtensionSystem::Get(profile)->event_router()), |
| 106 storage_factory_(factory), | 172 storage_factory_(factory), |
| 107 observers_(observers), | 173 observers_(observers), |
| 108 base_path_(profile->GetPath().AppendASCII( | 174 base_path_(profile->GetPath().AppendASCII( |
| 109 ExtensionService::kManagedSettingsDirectoryName)) { | 175 ExtensionService::kManagedSettingsDirectoryName)) { |
| 110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 111 // |event_router_| can be NULL on unit_tests. | 177 // |event_router_| can be NULL on unit_tests. |
| 112 if (event_router_) | 178 if (event_router_) |
| 113 event_router_->RegisterObserver(this, event_names::kOnSettingsChanged); | 179 event_router_->RegisterObserver(this, event_names::kOnSettingsChanged); |
| 114 | 180 |
| 115 GetPolicyService()->AddObserver(policy::POLICY_DOMAIN_EXTENSIONS, this); | 181 GetPolicyService()->AddObserver(policy::POLICY_DOMAIN_EXTENSIONS, this); |
| 116 | 182 |
| 117 extension_tracker_.reset(new ExtensionTracker(profile_)); | 183 extension_tracker_.reset(new ExtensionTracker(profile_)); |
| 184 |
| 185 (new StorageSchemaManifestHandler)->Register(); |
| 118 } | 186 } |
| 119 | 187 |
| 120 ManagedValueStoreCache::~ManagedValueStoreCache() { | 188 ManagedValueStoreCache::~ManagedValueStoreCache() { |
| 121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 122 DCHECK(!event_router_); | 190 DCHECK(!event_router_); |
| 123 // Delete the PolicyValueStores on FILE. | 191 // Delete the PolicyValueStores on FILE. |
| 124 store_map_.clear(); | 192 store_map_.clear(); |
| 125 } | 193 } |
| 126 | 194 |
| 127 void ManagedValueStoreCache::ShutdownOnUI() { | 195 void ManagedValueStoreCache::ShutdownOnUI() { |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 continuation.Run(); | 399 continuation.Run(); |
| 332 } | 400 } |
| 333 | 401 |
| 334 policy::PolicyService* ManagedValueStoreCache::GetPolicyService() { | 402 policy::PolicyService* ManagedValueStoreCache::GetPolicyService() { |
| 335 policy::ProfilePolicyConnector* connector = | 403 policy::ProfilePolicyConnector* connector = |
| 336 policy::ProfilePolicyConnectorFactory::GetForProfile(profile_); | 404 policy::ProfilePolicyConnectorFactory::GetForProfile(profile_); |
| 337 return connector->policy_service(); | 405 return connector->policy_service(); |
| 338 } | 406 } |
| 339 | 407 |
| 340 } // namespace extensions | 408 } // namespace extensions |
| OLD | NEW |