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/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 |
| 37 // This helper observes initialization of all the installed extensions and | 40 // This helper observes initialization of all the installed extensions and |
| 38 // subsequent loads and unloads, and keeps the PolicyService of the Profile | 41 // subsequent loads and unloads, and keeps the PolicyService of the Profile |
| 39 // in sync with the current list of extensions. This allows the PolicyService | 42 // 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 | 43 // to fetch cloud policy for those extensions, and allows its providers to |
| 41 // selectively load only extension policy that has users. | 44 // selectively load only extension policy that has users. |
| 42 class ManagedValueStoreCache::ExtensionTracker | 45 class ManagedValueStoreCache::ExtensionTracker |
| 43 : public content::NotificationObserver { | 46 : public content::NotificationObserver { |
| 44 public: | 47 public: |
| 45 explicit ExtensionTracker(Profile* profile); | 48 explicit ExtensionTracker(Profile* profile); |
| 46 virtual ~ExtensionTracker() {} | 49 virtual ~ExtensionTracker() {} |
| 47 | 50 |
| 48 // NotificationObserver implementation: | 51 // NotificationObserver implementation: |
| 49 virtual void Observe(int type, | 52 virtual void Observe(int type, |
| 50 const content::NotificationSource& source, | 53 const content::NotificationSource& source, |
| 51 const content::NotificationDetails& details) OVERRIDE; | 54 const content::NotificationDetails& details) OVERRIDE; |
| 52 | 55 |
| 53 private: | 56 private: |
| 57 // Loads the schemas of the |extensions| and passes a PolicyDomainDescriptor | |
| 58 // to RegisterDomain(). | |
| 59 static void LoadSchemas(scoped_ptr<ExtensionSet> extensions, | |
| 60 base::WeakPtr<ExtensionTracker> self); | |
| 61 void RegisterDomain( | |
| 62 scoped_refptr<const policy::PolicyDomainDescriptor> descriptor); | |
| 63 | |
| 54 Profile* profile_; | 64 Profile* profile_; |
| 55 bool is_ready_; | 65 bool is_ready_; |
| 56 content::NotificationRegistrar registrar_; | 66 content::NotificationRegistrar registrar_; |
| 67 base::WeakPtrFactory<ExtensionTracker> weak_factory_; | |
| 57 | 68 |
| 58 DISALLOW_COPY_AND_ASSIGN(ExtensionTracker); | 69 DISALLOW_COPY_AND_ASSIGN(ExtensionTracker); |
| 59 }; | 70 }; |
| 60 | 71 |
| 61 ManagedValueStoreCache::ExtensionTracker::ExtensionTracker(Profile* profile) | 72 ManagedValueStoreCache::ExtensionTracker::ExtensionTracker(Profile* profile) |
| 62 : profile_(profile), | 73 : profile_(profile), |
| 63 is_ready_(false) { | 74 is_ready_(false), |
| 75 weak_factory_(this) { | |
| 64 registrar_.Add(this, | 76 registrar_.Add(this, |
| 65 chrome::NOTIFICATION_EXTENSIONS_READY, | 77 chrome::NOTIFICATION_EXTENSIONS_READY, |
| 66 content::Source<Profile>(profile_)); | 78 content::Source<Profile>(profile_)); |
| 67 registrar_.Add(this, | 79 registrar_.Add(this, |
| 68 chrome::NOTIFICATION_EXTENSION_LOADED, | 80 chrome::NOTIFICATION_EXTENSION_LOADED, |
| 69 content::Source<Profile>(profile_)); | 81 content::Source<Profile>(profile_)); |
| 70 registrar_.Add(this, | 82 registrar_.Add(this, |
| 71 chrome::NOTIFICATION_EXTENSION_UNLOADED, | 83 chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 72 content::Source<Profile>(profile_)); | 84 content::Source<Profile>(profile_)); |
| 73 } | 85 } |
| 74 | 86 |
| 75 void ManagedValueStoreCache::ExtensionTracker::Observe( | 87 void ManagedValueStoreCache::ExtensionTracker::Observe( |
| 76 int type, | 88 int type, |
| 77 const content::NotificationSource& source, | 89 const content::NotificationSource& source, |
| 78 const content::NotificationDetails& details) { | 90 const content::NotificationDetails& details) { |
| 79 if (type == chrome::NOTIFICATION_EXTENSIONS_READY) | 91 if (type == chrome::NOTIFICATION_EXTENSIONS_READY) |
| 80 is_ready_ = true; | 92 is_ready_ = true; |
| 81 | 93 |
| 82 if (!is_ready_) | 94 if (!is_ready_) |
| 83 return; | 95 return; |
| 84 | 96 |
| 85 // TODO(joaodasilva): this currently only registers extensions that use | |
| 86 // the storage API, but that still includes a lot of extensions that don't | |
| 87 // use the storage.managed namespace. Use the presence of a schema for the | |
| 88 // managed namespace in the manifest as a better signal, once available. | |
| 89 scoped_refptr<policy::PolicyDomainDescriptor> descriptor( | 97 scoped_refptr<policy::PolicyDomainDescriptor> descriptor( |
| 90 new policy::PolicyDomainDescriptor(policy::POLICY_DOMAIN_EXTENSIONS)); | 98 new policy::PolicyDomainDescriptor(policy::POLICY_DOMAIN_EXTENSIONS)); |
| 91 const ExtensionSet* set = | 99 const ExtensionSet* set = |
| 92 ExtensionSystem::Get(profile_)->extension_service()->extensions(); | 100 ExtensionSystem::Get(profile_)->extension_service()->extensions(); |
| 101 scoped_ptr<ExtensionSet> managed_extensions(new ExtensionSet()); | |
| 93 for (ExtensionSet::const_iterator it = set->begin(); it != set->end(); ++it) { | 102 for (ExtensionSet::const_iterator it = set->begin(); it != set->end(); ++it) { |
| 94 // TODO(joaodasilva): pass the parsed schema here, once available. | 103 if ((*it)->manifest()->HasPath( |
| 95 if ((*it)->HasAPIPermission(APIPermission::kStorage)) { | 104 extension_manifest_keys::kStorageManagedSchema)) { |
| 96 descriptor->RegisterComponent((*it)->id(), | 105 managed_extensions->Insert(*it); |
| 97 scoped_ptr<policy::PolicySchema>()); | |
| 98 } | 106 } |
| 107 | |
| 108 // TODO(joaodasilva): also load extensions that use the storage API for now, | |
| 109 // to support the Legacy Browser Support extension. Remove this for M30. | |
| 110 // http://crbug.com/240704 | |
| 111 if ((*it)->HasAPIPermission(APIPermission::kStorage)) | |
| 112 managed_extensions->Insert(*it); | |
| 99 } | 113 } |
| 100 | 114 |
| 115 // Check the schema file on the FILE thread, for the extensions that may | |
|
bartfab (slow)
2013/05/22 11:00:09
Nit 1: "Load" instead of "Check"? LoadSchemas() do
Joao da Silva
2013/05/22 12:26:20
Done.
| |
| 116 // have it. | |
| 117 BrowserThread::PostTask( | |
| 118 BrowserThread::FILE, FROM_HERE, | |
|
bartfab (slow)
2013/05/22 11:00:09
IIRC, using the FILE thread in new code is a no-no
Joao da Silva
2013/05/22 12:26:20
Done. Didn't know it was a no-no, I thought the bl
| |
| 119 base::Bind(&ExtensionTracker::LoadSchemas, | |
| 120 base::Passed(&managed_extensions), | |
| 121 weak_factory_.GetWeakPtr())); | |
| 122 } | |
| 123 | |
| 124 // static | |
| 125 void ManagedValueStoreCache::ExtensionTracker::LoadSchemas( | |
| 126 scoped_ptr<ExtensionSet> extensions, | |
| 127 base::WeakPtr<ExtensionTracker> self) { | |
| 128 scoped_refptr<policy::PolicyDomainDescriptor> descriptor = | |
| 129 new policy::PolicyDomainDescriptor(policy::POLICY_DOMAIN_EXTENSIONS); | |
| 130 | |
| 131 for (ExtensionSet::const_iterator it = extensions->begin(); | |
| 132 it != extensions->end(); ++it) { | |
| 133 std::string schema_file; | |
| 134 if (!(*it)->manifest()->GetString( | |
| 135 extension_manifest_keys::kStorageManagedSchema, &schema_file)) { | |
| 136 // TODO(joaodasilva): Remove this for M30. http://crbug.com/240704 | |
| 137 if ((*it)->HasAPIPermission(APIPermission::kStorage)) { | |
| 138 descriptor->RegisterComponent((*it)->id(), | |
| 139 scoped_ptr<policy::PolicySchema>()); | |
| 140 } else { | |
| 141 NOTREACHED(); | |
| 142 } | |
| 143 continue; | |
| 144 } | |
| 145 // The extension should have been validated, so assume the schema exists | |
| 146 // and is valid. | |
| 147 std::string content; | |
| 148 if (!file_util::ReadFileToString((*it)->path().AppendASCII(schema_file), | |
| 149 &content)) { | |
| 150 NOTREACHED(); | |
| 151 continue; | |
| 152 } | |
| 153 std::string error; | |
| 154 scoped_ptr<policy::PolicySchema> schema = | |
| 155 policy::PolicySchema::Parse(content, &error); | |
| 156 if (schema) | |
| 157 descriptor->RegisterComponent((*it)->id(), schema.Pass()); | |
| 158 else | |
| 159 NOTREACHED(); | |
| 160 } | |
| 161 | |
| 162 BrowserThread::PostTask( | |
| 163 BrowserThread::UI, FROM_HERE, | |
| 164 base::Bind(&ExtensionTracker::RegisterDomain, self, descriptor)); | |
| 165 } | |
| 166 | |
| 167 void ManagedValueStoreCache::ExtensionTracker::RegisterDomain( | |
| 168 scoped_refptr<const policy::PolicyDomainDescriptor> descriptor) { | |
| 101 policy::ProfilePolicyConnector* connector = | 169 policy::ProfilePolicyConnector* connector = |
| 102 policy::ProfilePolicyConnectorFactory::GetForProfile(profile_); | 170 policy::ProfilePolicyConnectorFactory::GetForProfile(profile_); |
| 103 connector->policy_service()->RegisterPolicyDomain(descriptor); | 171 connector->policy_service()->RegisterPolicyDomain(descriptor); |
| 104 } | 172 } |
| 105 | 173 |
| 106 ManagedValueStoreCache::ManagedValueStoreCache( | 174 ManagedValueStoreCache::ManagedValueStoreCache( |
| 107 Profile* profile, | 175 Profile* profile, |
| 108 const scoped_refptr<SettingsStorageFactory>& factory, | 176 const scoped_refptr<SettingsStorageFactory>& factory, |
| 109 const scoped_refptr<SettingsObserverList>& observers) | 177 const scoped_refptr<SettingsObserverList>& observers) |
| 110 : weak_factory_(this), | 178 : weak_factory_(this), |
| 111 weak_this_on_ui_(weak_factory_.GetWeakPtr()), | 179 weak_this_on_ui_(weak_factory_.GetWeakPtr()), |
| 112 profile_(profile), | 180 profile_(profile), |
| 113 event_router_(ExtensionSystem::Get(profile)->event_router()), | 181 event_router_(ExtensionSystem::Get(profile)->event_router()), |
| 114 storage_factory_(factory), | 182 storage_factory_(factory), |
| 115 observers_(observers), | 183 observers_(observers), |
| 116 base_path_(profile->GetPath().AppendASCII( | 184 base_path_(profile->GetPath().AppendASCII( |
| 117 ExtensionService::kManagedSettingsDirectoryName)) { | 185 ExtensionService::kManagedSettingsDirectoryName)) { |
| 118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 119 // |event_router_| can be NULL on unit_tests. | 187 // |event_router_| can be NULL on unit_tests. |
| 120 if (event_router_) | 188 if (event_router_) |
| 121 event_router_->RegisterObserver(this, event_names::kOnSettingsChanged); | 189 event_router_->RegisterObserver(this, event_names::kOnSettingsChanged); |
| 122 | 190 |
| 123 GetPolicyService()->AddObserver(policy::POLICY_DOMAIN_EXTENSIONS, this); | 191 GetPolicyService()->AddObserver(policy::POLICY_DOMAIN_EXTENSIONS, this); |
| 124 | 192 |
| 125 extension_tracker_.reset(new ExtensionTracker(profile_)); | 193 extension_tracker_.reset(new ExtensionTracker(profile_)); |
| 194 | |
| 195 (new StorageSchemaManifestHandler)->Register(); | |
| 126 } | 196 } |
| 127 | 197 |
| 128 ManagedValueStoreCache::~ManagedValueStoreCache() { | 198 ManagedValueStoreCache::~ManagedValueStoreCache() { |
| 129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 130 DCHECK(!event_router_); | 200 DCHECK(!event_router_); |
| 131 // Delete the PolicyValueStores on FILE. | 201 // Delete the PolicyValueStores on FILE. |
| 132 store_map_.clear(); | 202 store_map_.clear(); |
| 133 } | 203 } |
| 134 | 204 |
| 135 void ManagedValueStoreCache::ShutdownOnUI() { | 205 void ManagedValueStoreCache::ShutdownOnUI() { |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 339 continuation.Run(); | 409 continuation.Run(); |
| 340 } | 410 } |
| 341 | 411 |
| 342 policy::PolicyService* ManagedValueStoreCache::GetPolicyService() { | 412 policy::PolicyService* ManagedValueStoreCache::GetPolicyService() { |
| 343 policy::ProfilePolicyConnector* connector = | 413 policy::ProfilePolicyConnector* connector = |
| 344 policy::ProfilePolicyConnectorFactory::GetForProfile(profile_); | 414 policy::ProfilePolicyConnectorFactory::GetForProfile(profile_); |
| 345 return connector->policy_service(); | 415 return connector->policy_service(); |
| 346 } | 416 } |
| 347 | 417 |
| 348 } // namespace extensions | 418 } // namespace extensions |
| OLD | NEW |