Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(193)

Side by Side Diff: chrome/browser/extensions/api/storage/managed_value_store_cache.cc

Issue 56623005: Policy providers all get a SchemaRegistry to work with. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@chrome-policy-schema-9-purge-with-callback
Patch Set: rebase Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/chrome_notification_types.h" 14 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/extensions/api/storage/policy_value_store.h" 15 #include "chrome/browser/extensions/api/storage/policy_value_store.h"
16 #include "chrome/browser/extensions/api/storage/settings_storage_factory.h" 16 #include "chrome/browser/extensions/api/storage/settings_storage_factory.h"
17 #include "chrome/browser/extensions/extension_prefs.h" 17 #include "chrome/browser/extensions/extension_prefs.h"
18 #include "chrome/browser/extensions/extension_service.h" 18 #include "chrome/browser/extensions/extension_service.h"
19 #include "chrome/browser/extensions/extension_system.h" 19 #include "chrome/browser/extensions/extension_system.h"
20 #include "chrome/browser/policy/policy_domain_descriptor.h"
21 #include "chrome/browser/policy/profile_policy_connector.h" 20 #include "chrome/browser/policy/profile_policy_connector.h"
22 #include "chrome/browser/policy/profile_policy_connector_factory.h" 21 #include "chrome/browser/policy/profile_policy_connector_factory.h"
22 #include "chrome/browser/policy/schema_registry.h"
23 #include "chrome/browser/policy/schema_registry_service.h"
24 #include "chrome/browser/policy/schema_registry_service_factory.h"
23 #include "chrome/browser/profiles/profile.h" 25 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/value_store/value_store_change.h" 26 #include "chrome/browser/value_store/value_store_change.h"
25 #include "chrome/common/extensions/api/storage.h" 27 #include "chrome/common/extensions/api/storage.h"
26 #include "chrome/common/extensions/api/storage/storage_schema_manifest_handler.h " 28 #include "chrome/common/extensions/api/storage/storage_schema_manifest_handler.h "
27 #include "chrome/common/extensions/extension.h" 29 #include "chrome/common/extensions/extension.h"
28 #include "chrome/common/extensions/extension_set.h" 30 #include "chrome/common/extensions/extension_set.h"
31 #include "components/policy/core/common/policy_namespace.h"
29 #include "components/policy/core/common/schema.h" 32 #include "components/policy/core/common/schema.h"
30 #include "content/public/browser/browser_thread.h" 33 #include "content/public/browser/browser_thread.h"
34 #include "content/public/browser/notification_details.h"
31 #include "content/public/browser/notification_observer.h" 35 #include "content/public/browser/notification_observer.h"
32 #include "content/public/browser/notification_registrar.h" 36 #include "content/public/browser/notification_registrar.h"
33 #include "content/public/browser/notification_source.h" 37 #include "content/public/browser/notification_source.h"
34 #include "extensions/common/constants.h" 38 #include "extensions/common/constants.h"
35 #include "extensions/common/manifest.h" 39 #include "extensions/common/manifest.h"
36 #include "extensions/common/manifest_constants.h" 40 #include "extensions/common/manifest_constants.h"
37 #include "extensions/common/permissions/api_permission.h" 41 #include "extensions/common/permissions/api_permission.h"
38 42
39 using content::BrowserThread; 43 using content::BrowserThread;
40 44
(...skipping 18 matching lines...) Expand all
59 public: 63 public:
60 explicit ExtensionTracker(Profile* profile); 64 explicit ExtensionTracker(Profile* profile);
61 virtual ~ExtensionTracker() {} 65 virtual ~ExtensionTracker() {}
62 66
63 // NotificationObserver implementation: 67 // NotificationObserver implementation:
64 virtual void Observe(int type, 68 virtual void Observe(int type,
65 const content::NotificationSource& source, 69 const content::NotificationSource& source,
66 const content::NotificationDetails& details) OVERRIDE; 70 const content::NotificationDetails& details) OVERRIDE;
67 71
68 private: 72 private:
69 // Loads the schemas of the |extensions| and passes a PolicyDomainDescriptor 73 bool IsEnterpriseExtension(const Extension* extension) const;
70 // to RegisterDomain(). 74
75 // Loads the schemas of the |extensions| and passes a ComponentMap to
76 // Register().
71 static void LoadSchemas(scoped_ptr<ExtensionSet> extensions, 77 static void LoadSchemas(scoped_ptr<ExtensionSet> extensions,
72 base::WeakPtr<ExtensionTracker> self); 78 base::WeakPtr<ExtensionTracker> self);
73 void RegisterDomain( 79 void Register(const policy::ComponentMap* components);
74 scoped_refptr<const policy::PolicyDomainDescriptor> descriptor);
75 80
76 Profile* profile_; 81 Profile* profile_;
77 content::NotificationRegistrar registrar_; 82 content::NotificationRegistrar registrar_;
83 policy::SchemaRegistry* schema_registry_;
78 base::WeakPtrFactory<ExtensionTracker> weak_factory_; 84 base::WeakPtrFactory<ExtensionTracker> weak_factory_;
79 85
80 DISALLOW_COPY_AND_ASSIGN(ExtensionTracker); 86 DISALLOW_COPY_AND_ASSIGN(ExtensionTracker);
81 }; 87 };
82 88
83 ManagedValueStoreCache::ExtensionTracker::ExtensionTracker(Profile* profile) 89 ManagedValueStoreCache::ExtensionTracker::ExtensionTracker(Profile* profile)
84 : profile_(profile), 90 : profile_(profile),
91 schema_registry_(
92 policy::SchemaRegistryServiceFactory::GetForContext(profile)),
85 weak_factory_(this) { 93 weak_factory_(this) {
86 registrar_.Add(this, 94 registrar_.Add(this,
87 chrome::NOTIFICATION_EXTENSIONS_READY, 95 chrome::NOTIFICATION_EXTENSIONS_READY,
88 content::Source<Profile>(profile_)); 96 content::Source<Profile>(profile_));
89 registrar_.Add(this, 97 registrar_.Add(this,
90 chrome::NOTIFICATION_EXTENSION_LOADED, 98 chrome::NOTIFICATION_EXTENSION_LOADED,
91 content::Source<Profile>(profile_)); 99 content::Source<Profile>(profile_));
92 registrar_.Add(this, 100 registrar_.Add(this,
93 chrome::NOTIFICATION_EXTENSION_UNLOADED, 101 chrome::NOTIFICATION_EXTENSION_UNLOADED,
94 content::Source<Profile>(profile_)); 102 content::Source<Profile>(profile_));
95 } 103 }
96 104
97 void ManagedValueStoreCache::ExtensionTracker::Observe( 105 void ManagedValueStoreCache::ExtensionTracker::Observe(
98 int type, 106 int type,
99 const content::NotificationSource& source, 107 const content::NotificationSource& source,
100 const content::NotificationDetails& details) { 108 const content::NotificationDetails& details) {
101 if (!ExtensionSystem::Get(profile_)->ready().is_signaled()) 109 if (!ExtensionSystem::Get(profile_)->ready().is_signaled())
not at google - send to devlin 2013/11/09 23:52:43 (how can this be false if we're getting extension
Joao da Silva 2013/11/11 08:46:26 Left a comment explaining why this is here: all t
102 return; 110 return;
103 111
104 scoped_refptr<policy::PolicyDomainDescriptor> descriptor( 112 if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) {
not at google - send to devlin 2013/11/09 23:52:43 use switch. easier to read. I think you could make
Joao da Silva 2013/11/11 08:46:26 Done.
105 new policy::PolicyDomainDescriptor(policy::POLICY_DOMAIN_EXTENSIONS)); 113 const Extension* extension =
106 const ExtensionSet* set = 114 content::Details<UnloadedExtensionInfo>(details)->extension;
107 ExtensionSystem::Get(profile_)->extension_service()->extensions(); 115 if (IsEnterpriseExtension(extension)) {
108 scoped_ptr<ExtensionSet> managed_extensions(new ExtensionSet()); 116 schema_registry_->UnregisterComponent(policy::PolicyNamespace(
109 for (ExtensionSet::const_iterator it = set->begin(); it != set->end(); ++it) { 117 policy::POLICY_DOMAIN_EXTENSIONS, extension->id()));
110 if ((*it)->manifest()->HasPath(manifest_keys::kStorageManagedSchema)) {
111 managed_extensions->Insert(*it);
112 } 118 }
119 return;
120 }
113 121
114 // TODO(joaodasilva): also load extensions that use the storage API for now, 122 scoped_ptr<ExtensionSet> set(new ExtensionSet);
115 // to support the Legacy Browser Support extension. Remove this. 123 if (type == chrome::NOTIFICATION_EXTENSION_LOADED) {
116 // http://crbug.com/240704 124 const Extension* extension =
117 if ((*it)->HasAPIPermission(APIPermission::kStorage)) 125 content::Details<const Extension>(details).ptr();
118 managed_extensions->Insert(*it); 126 if (IsEnterpriseExtension(extension))
127 set->Insert(extension);
128 } else if (type == chrome::NOTIFICATION_EXTENSIONS_READY) {
129 const ExtensionSet* extensions =
130 ExtensionSystem::Get(profile_)->extension_service()->extensions();
131 for (ExtensionSet::const_iterator it = extensions->begin();
132 it != extensions->end(); ++it) {
133 if (IsEnterpriseExtension(*it))
134 set->Insert(*it);
135 }
136 } else {
137 NOTREACHED();
119 } 138 }
120 139
140 if (set->is_empty())
141 return;
142
121 // Load the schema files in a background thread. 143 // Load the schema files in a background thread.
122 BrowserThread::PostBlockingPoolSequencedTask( 144 BrowserThread::PostBlockingPoolSequencedTask(
123 kLoadSchemasBackgroundTaskTokenName, FROM_HERE, 145 kLoadSchemasBackgroundTaskTokenName, FROM_HERE,
124 base::Bind(&ExtensionTracker::LoadSchemas, 146 base::Bind(&ExtensionTracker::LoadSchemas,
125 base::Passed(&managed_extensions), 147 base::Passed(&set),
126 weak_factory_.GetWeakPtr())); 148 weak_factory_.GetWeakPtr()));
127 } 149 }
128 150
151 bool ManagedValueStoreCache::ExtensionTracker::IsEnterpriseExtension(
not at google - send to devlin 2013/11/09 23:52:43 "IsEnterpriseExtension" is a confusing name. It ma
Joao da Silva 2013/11/11 08:46:26 Renamed to UsesManagedStorage.
152 const Extension* extension) const {
153 if (extension->manifest()->HasPath(manifest_keys::kStorageManagedSchema))
154 return true;
155
156 // TODO(joaodasilva): also load extensions that use the storage API for now,
157 // to support the Legacy Browser Support extension. Remove this.
158 // http://crbug.com/240704
159 if (extension->HasAPIPermission(APIPermission::kStorage))
160 return true;
161
162 return false;
163 }
164
129 // static 165 // static
130 void ManagedValueStoreCache::ExtensionTracker::LoadSchemas( 166 void ManagedValueStoreCache::ExtensionTracker::LoadSchemas(
131 scoped_ptr<ExtensionSet> extensions, 167 scoped_ptr<ExtensionSet> extensions,
132 base::WeakPtr<ExtensionTracker> self) { 168 base::WeakPtr<ExtensionTracker> self) {
133 scoped_refptr<policy::PolicyDomainDescriptor> descriptor = 169 scoped_ptr<policy::ComponentMap> components(new policy::ComponentMap);
134 new policy::PolicyDomainDescriptor(policy::POLICY_DOMAIN_EXTENSIONS);
135 170
136 for (ExtensionSet::const_iterator it = extensions->begin(); 171 for (ExtensionSet::const_iterator it = extensions->begin();
137 it != extensions->end(); ++it) { 172 it != extensions->end(); ++it) {
138 std::string schema_file; 173 std::string schema_file;
139 if (!(*it)->manifest()->GetString( 174 if (!(*it)->manifest()->GetString(
140 manifest_keys::kStorageManagedSchema, &schema_file)) { 175 manifest_keys::kStorageManagedSchema, &schema_file)) {
141 // TODO(joaodasilva): Remove this. http://crbug.com/240704 176 // TODO(joaodasilva): Remove this. http://crbug.com/240704
142 if ((*it)->HasAPIPermission(APIPermission::kStorage)) { 177 if ((*it)->HasAPIPermission(APIPermission::kStorage)) {
143 descriptor->RegisterComponent((*it)->id(), policy::Schema()); 178 (*components)[(*it)->id()] = policy::Schema();
144 } else { 179 } else {
145 NOTREACHED(); 180 NOTREACHED();
146 } 181 }
147 continue; 182 continue;
148 } 183 }
149 // The extension should have been validated, so assume the schema exists 184 // The extension should have been validated, so assume the schema exists
150 // and is valid. 185 // and is valid.
151 std::string error; 186 std::string error;
152 policy::Schema schema = 187 policy::Schema schema =
153 StorageSchemaManifestHandler::GetSchema(it->get(), &error); 188 StorageSchemaManifestHandler::GetSchema(it->get(), &error);
154 CHECK(schema.valid()) << error; 189 CHECK(schema.valid()) << error;
155 descriptor->RegisterComponent((*it)->id(), schema); 190 (*components)[(*it)->id()] = schema;
156 } 191 }
157 192
158 BrowserThread::PostTask( 193 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
159 BrowserThread::UI, FROM_HERE, 194 base::Bind(&ExtensionTracker::Register, self,
160 base::Bind(&ExtensionTracker::RegisterDomain, self, descriptor)); 195 base::Owned(components.release())));
161 } 196 }
162 197
163 void ManagedValueStoreCache::ExtensionTracker::RegisterDomain( 198 void ManagedValueStoreCache::ExtensionTracker::Register(
164 scoped_refptr<const policy::PolicyDomainDescriptor> descriptor) { 199 const policy::ComponentMap* components) {
165 policy::ProfilePolicyConnector* connector = 200 schema_registry_->RegisterComponents(policy::POLICY_DOMAIN_EXTENSIONS,
166 policy::ProfilePolicyConnectorFactory::GetForProfile(profile_); 201 *components);
167 connector->policy_service()->RegisterPolicyDomain(descriptor);
168 } 202 }
169 203
170 ManagedValueStoreCache::ManagedValueStoreCache( 204 ManagedValueStoreCache::ManagedValueStoreCache(
171 Profile* profile, 205 Profile* profile,
172 const scoped_refptr<SettingsStorageFactory>& factory, 206 const scoped_refptr<SettingsStorageFactory>& factory,
173 const scoped_refptr<SettingsObserverList>& observers) 207 const scoped_refptr<SettingsObserverList>& observers)
174 : weak_factory_(this), 208 : weak_factory_(this),
175 weak_this_on_ui_(weak_factory_.GetWeakPtr()), 209 weak_this_on_ui_(weak_factory_.GetWeakPtr()),
176 profile_(profile), 210 profile_(profile),
177 event_router_(ExtensionSystem::Get(profile)->event_router()), 211 event_router_(ExtensionSystem::Get(profile)->event_router()),
178 storage_factory_(factory), 212 storage_factory_(factory),
179 observers_(observers), 213 observers_(observers),
180 base_path_(profile->GetPath().AppendASCII( 214 base_path_(profile->GetPath().AppendASCII(
181 extensions::kManagedSettingsDirectoryName)) { 215 extensions::kManagedSettingsDirectoryName)) {
182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 216 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
183 // |event_router_| can be NULL on unit_tests. 217 // |event_router_| can be NULL on unit_tests.
184 if (event_router_) 218 if (event_router_)
185 event_router_->RegisterObserver(this, storage::OnChanged::kEventName); 219 event_router_->RegisterObserver(this, storage::OnChanged::kEventName);
186 220
187 GetPolicyService()->AddObserver(policy::POLICY_DOMAIN_EXTENSIONS, this); 221 GetPolicyService()->AddObserver(policy::POLICY_DOMAIN_EXTENSIONS, this);
188 222
189 extension_tracker_.reset(new ExtensionTracker(profile_)); 223 // Track the extensions of the original Profile only; the OTR profile has
224 // a subset of those.
225 if (!profile->IsOffTheRecord())
226 extension_tracker_.reset(new ExtensionTracker(profile_));
not at google - send to devlin 2013/11/09 23:52:43 Are we even creating one of these with an OTR prof
Joao da Silva 2013/11/11 08:46:26 That's right, removed this test.
190 } 227 }
191 228
192 ManagedValueStoreCache::~ManagedValueStoreCache() { 229 ManagedValueStoreCache::~ManagedValueStoreCache() {
193 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 230 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
194 DCHECK(!event_router_); 231 DCHECK(!event_router_);
195 // Delete the PolicyValueStores on FILE. 232 // Delete the PolicyValueStores on FILE.
196 store_map_.clear(); 233 store_map_.clear();
197 } 234 }
198 235
199 void ManagedValueStoreCache::ShutdownOnUI() { 236 void ManagedValueStoreCache::ShutdownOnUI() {
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 continuation.Run(); 440 continuation.Run();
404 } 441 }
405 442
406 policy::PolicyService* ManagedValueStoreCache::GetPolicyService() { 443 policy::PolicyService* ManagedValueStoreCache::GetPolicyService() {
407 policy::ProfilePolicyConnector* connector = 444 policy::ProfilePolicyConnector* connector =
408 policy::ProfilePolicyConnectorFactory::GetForProfile(profile_); 445 policy::ProfilePolicyConnectorFactory::GetForProfile(profile_);
409 return connector->policy_service(); 446 return connector->policy_service();
410 } 447 }
411 448
412 } // namespace extensions 449 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698