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 <set> | 7 #include <set> |
| 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/file_util.h" | 12 #include "base/file_util.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/message_loop/message_loop_proxy.h" | 15 #include "base/message_loop/message_loop_proxy.h" |
| 16 #include "chrome/browser/extensions/api/storage/policy_value_store.h" | 16 #include "chrome/browser/extensions/api/storage/policy_value_store.h" |
| 17 #include "chrome/browser/extensions/api/storage/settings_storage_factory.h" | 17 #include "chrome/browser/extensions/api/storage/settings_storage_factory.h" |
| 18 #include "chrome/browser/extensions/event_names.h" | 18 #include "chrome/browser/extensions/event_names.h" |
| 19 #include "chrome/browser/extensions/extension_prefs.h" | 19 #include "chrome/browser/extensions/extension_prefs.h" |
| 20 #include "chrome/browser/extensions/extension_service.h" | 20 #include "chrome/browser/extensions/extension_service.h" |
| 21 #include "chrome/browser/extensions/extension_system.h" | 21 #include "chrome/browser/extensions/extension_system.h" |
| 22 #include "chrome/browser/policy/policy_domain_descriptor.h" | 22 #include "chrome/browser/policy/policy_domain_descriptor.h" |
| 23 #include "chrome/browser/policy/policy_schema.h" | 23 #include "chrome/browser/policy/policy_schema.h" |
| 24 #include "chrome/browser/policy/profile_policy_connector.h" | 24 #include "chrome/browser/policy/profile_policy_connector.h" |
| 25 #include "chrome/browser/policy/profile_policy_connector_factory.h" | 25 #include "chrome/browser/policy/profile_policy_connector_factory.h" |
| 26 #include "chrome/browser/profiles/profile.h" | 26 #include "chrome/browser/profiles/profile.h" |
| 27 #include "chrome/browser/value_store/value_store_change.h" | 27 #include "chrome/browser/value_store/value_store_change.h" |
| 28 #include "chrome/common/extensions/extension.h" | 28 #include "chrome/common/extensions/extension.h" |
| 29 #include "chrome/common/extensions/extension_manifest_constants.h" | |
| 29 #include "chrome/common/extensions/extension_set.h" | 30 #include "chrome/common/extensions/extension_set.h" |
| 31 #include "chrome/common/extensions/manifest.h" | |
| 30 #include "chrome/common/extensions/permissions/api_permission.h" | 32 #include "chrome/common/extensions/permissions/api_permission.h" |
| 31 #include "content/public/browser/browser_thread.h" | 33 #include "content/public/browser/browser_thread.h" |
| 32 #include "content/public/browser/notification_observer.h" | 34 #include "content/public/browser/notification_observer.h" |
| 33 #include "content/public/browser/notification_registrar.h" | 35 #include "content/public/browser/notification_registrar.h" |
| 34 | 36 |
| 35 using content::BrowserThread; | 37 using content::BrowserThread; |
| 36 | 38 |
| 37 namespace extensions { | 39 namespace extensions { |
| 38 | 40 |
| 39 // This helper observes initialization of all the installed extensions and | 41 // This helper observes initialization of all the installed extensions and |
| 40 // subsequent loads and unloads, and keeps the PolicyService of the Profile | 42 // subsequent loads and unloads, and keeps the PolicyService of the Profile |
| 41 // in sync with the current list of extensions. This allows the PolicyService | 43 // in sync with the current list of extensions. This allows the PolicyService |
| 42 // to fetch cloud policy for those extensions, and allows its providers to | 44 // to fetch cloud policy for those extensions, and allows its providers to |
| 43 // selectively load only extension policy that has users. | 45 // selectively load only extension policy that has users. |
| 44 class ManagedValueStoreCache::ExtensionTracker | 46 class ManagedValueStoreCache::ExtensionTracker |
| 45 : public content::NotificationObserver { | 47 : public content::NotificationObserver { |
| 46 public: | 48 public: |
| 47 explicit ExtensionTracker(Profile* profile); | 49 explicit ExtensionTracker(Profile* profile); |
| 48 virtual ~ExtensionTracker() {} | 50 virtual ~ExtensionTracker() {} |
| 49 | 51 |
| 50 // NotificationObserver implementation: | 52 // NotificationObserver implementation: |
| 51 virtual void Observe(int type, | 53 virtual void Observe(int type, |
| 52 const content::NotificationSource& source, | 54 const content::NotificationSource& source, |
| 53 const content::NotificationDetails& details) OVERRIDE; | 55 const content::NotificationDetails& details) OVERRIDE; |
| 54 | 56 |
| 55 private: | 57 private: |
| 58 // Loads the schemas of the |extensions| and passes a PolicyDomainDescriptor | |
| 59 // to RegisterDomain(). | |
| 60 static void LoadSchemas(scoped_ptr<ExtensionSet> extensions, | |
| 61 base::WeakPtr<ExtensionTracker> self); | |
| 62 void RegisterDomain( | |
| 63 scoped_refptr<const policy::PolicyDomainDescriptor> descriptor); | |
| 64 | |
| 56 Profile* profile_; | 65 Profile* profile_; |
| 57 bool is_ready_; | 66 bool is_ready_; |
| 58 content::NotificationRegistrar registrar_; | 67 content::NotificationRegistrar registrar_; |
| 68 base::WeakPtrFactory<ExtensionTracker> weak_factory_; | |
| 59 | 69 |
| 60 DISALLOW_COPY_AND_ASSIGN(ExtensionTracker); | 70 DISALLOW_COPY_AND_ASSIGN(ExtensionTracker); |
| 61 }; | 71 }; |
| 62 | 72 |
| 63 ManagedValueStoreCache::ExtensionTracker::ExtensionTracker(Profile* profile) | 73 ManagedValueStoreCache::ExtensionTracker::ExtensionTracker(Profile* profile) |
| 64 : profile_(profile), | 74 : profile_(profile), |
| 65 is_ready_(false) { | 75 is_ready_(false), |
| 76 weak_factory_(this) { | |
| 66 registrar_.Add(this, | 77 registrar_.Add(this, |
| 67 chrome::NOTIFICATION_EXTENSIONS_READY, | 78 chrome::NOTIFICATION_EXTENSIONS_READY, |
| 68 content::Source<Profile>(profile_)); | 79 content::Source<Profile>(profile_)); |
| 69 registrar_.Add(this, | 80 registrar_.Add(this, |
| 70 chrome::NOTIFICATION_EXTENSION_LOADED, | 81 chrome::NOTIFICATION_EXTENSION_LOADED, |
| 71 content::Source<Profile>(profile_)); | 82 content::Source<Profile>(profile_)); |
| 72 registrar_.Add(this, | 83 registrar_.Add(this, |
| 73 chrome::NOTIFICATION_EXTENSION_UNLOADED, | 84 chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 74 content::Source<Profile>(profile_)); | 85 content::Source<Profile>(profile_)); |
| 75 } | 86 } |
| 76 | 87 |
| 77 void ManagedValueStoreCache::ExtensionTracker::Observe( | 88 void ManagedValueStoreCache::ExtensionTracker::Observe( |
| 78 int type, | 89 int type, |
| 79 const content::NotificationSource& source, | 90 const content::NotificationSource& source, |
| 80 const content::NotificationDetails& details) { | 91 const content::NotificationDetails& details) { |
| 81 if (type == chrome::NOTIFICATION_EXTENSIONS_READY) | 92 if (type == chrome::NOTIFICATION_EXTENSIONS_READY) |
| 82 is_ready_ = true; | 93 is_ready_ = true; |
| 83 | 94 |
| 84 if (!is_ready_) | 95 if (!is_ready_) |
| 85 return; | 96 return; |
| 86 | 97 |
| 87 // TODO(joaodasilva): this currently only registers extensions that use | |
| 88 // the storage API, but that still includes a lot of extensions that don't | |
| 89 // use the storage.managed namespace. Use the presence of a schema for the | |
| 90 // managed namespace in the manifest as a better signal, once available. | |
| 91 scoped_refptr<policy::PolicyDomainDescriptor> descriptor( | 98 scoped_refptr<policy::PolicyDomainDescriptor> descriptor( |
| 92 new policy::PolicyDomainDescriptor(policy::POLICY_DOMAIN_EXTENSIONS)); | 99 new policy::PolicyDomainDescriptor(policy::POLICY_DOMAIN_EXTENSIONS)); |
| 93 const ExtensionSet* set = | 100 const ExtensionSet* set = |
| 94 ExtensionSystem::Get(profile_)->extension_service()->extensions(); | 101 ExtensionSystem::Get(profile_)->extension_service()->extensions(); |
| 102 scoped_ptr<ExtensionSet> managed_extensions(new ExtensionSet()); | |
| 95 for (ExtensionSet::const_iterator it = set->begin(); it != set->end(); ++it) { | 103 for (ExtensionSet::const_iterator it = set->begin(); it != set->end(); ++it) { |
|
Mattias Nissler (ping if slow)
2013/05/15 10:37:39
It seems this loop is not really needed, you could
not at google - send to devlin
2013/05/16 16:34:15
One advantage of filtering here is that the method
Joao da Silva
2013/05/19 13:21:35
It's expected that most extensions won't have the
Joao da Silva
2013/05/19 13:21:35
|managed_extensions| could be created only when th
| |
| 96 // TODO(joaodasilva): pass the parsed schema here, once available. | 104 if ((*it)->manifest()->HasPath( |
| 105 extension_manifest_keys::kStorageManagedSchema)) { | |
| 106 managed_extensions->Insert(*it); | |
| 107 } | |
| 108 | |
| 109 // TODO(joaodasilva): also load extensions that use the storage API for now, | |
| 110 // to support the Legacy Browser Support extension. Remove this for M30. | |
| 111 // http://crbug.com/240704 | |
| 97 if ((*it)->HasAPIPermission(APIPermission::kStorage)) | 112 if ((*it)->HasAPIPermission(APIPermission::kStorage)) |
| 98 descriptor->AddComponent((*it)->id(), scoped_ptr<policy::PolicySchema>()); | 113 managed_extensions->Insert(*it); |
| 99 } | 114 } |
| 100 | 115 |
| 116 // The 2nd check needs to be performed on the FILE thread. | |
|
Mattias Nissler (ping if slow)
2013/05/15 10:37:39
What is the 2nd check? Just spell it out :)
Joao da Silva
2013/05/19 13:21:35
Done.
| |
| 117 BrowserThread::PostTask( | |
| 118 BrowserThread::FILE, FROM_HERE, | |
| 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 path; | |
| 134 if (!(*it)->manifest()->GetString( | |
| 135 extension_manifest_keys::kStorageManagedSchema, &path)) { | |
| 136 // TODO(joaodasilva): Remove this for M30. http://crbug.com/240704 | |
| 137 if ((*it)->HasAPIPermission(APIPermission::kStorage)) { | |
| 138 descriptor->AddComponent((*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(path), | |
|
Mattias Nissler (ping if slow)
2013/05/15 10:37:39
I was momentarily confused by the two |path|s here
Joao da Silva
2013/05/19 13:21:35
Done.
| |
| 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->AddComponent((*it)->id(), schema.Pass()); | |
| 158 else | |
| 159 NOTREACHED(); | |
|
Mattias Nissler (ping if slow)
2013/05/15 10:37:39
Is this really a NOTREACHED()? Won't we hit this i
not at google - send to devlin
2013/05/16 16:34:15
Hopefully we are doing the schema validation at ex
Joao da Silva
2013/05/19 13:21:35
As Benjamin suspects, this is a NOTREACHED because
| |
| 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), |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 339 continuation.Run(); | 407 continuation.Run(); |
| 340 } | 408 } |
| 341 | 409 |
| 342 policy::PolicyService* ManagedValueStoreCache::GetPolicyService() { | 410 policy::PolicyService* ManagedValueStoreCache::GetPolicyService() { |
| 343 policy::ProfilePolicyConnector* connector = | 411 policy::ProfilePolicyConnector* connector = |
| 344 policy::ProfilePolicyConnectorFactory::GetForProfile(profile_); | 412 policy::ProfilePolicyConnectorFactory::GetForProfile(profile_); |
| 345 return connector->policy_service(); | 413 return connector->policy_service(); |
| 346 } | 414 } |
| 347 | 415 |
| 348 } // namespace extensions | 416 } // namespace extensions |
| OLD | NEW |