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/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_service.h" | |
bartfab (slow)
2013/11/05 15:53:04
Nit: As commented elsewhere, in line with your usu
Joao da Silva
2013/11/07 13:15:00
It's needed so that this unit sees that a SchemaRe
| |
23 #include "chrome/browser/policy/schema_registry_service_factory.h" | |
23 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
24 #include "chrome/browser/value_store/value_store_change.h" | 25 #include "chrome/browser/value_store/value_store_change.h" |
25 #include "chrome/common/extensions/api/storage.h" | 26 #include "chrome/common/extensions/api/storage.h" |
26 #include "chrome/common/extensions/api/storage/storage_schema_manifest_handler.h " | 27 #include "chrome/common/extensions/api/storage/storage_schema_manifest_handler.h " |
27 #include "chrome/common/extensions/extension.h" | 28 #include "chrome/common/extensions/extension.h" |
28 #include "chrome/common/extensions/extension_set.h" | 29 #include "chrome/common/extensions/extension_set.h" |
29 #include "components/policy/core/common/schema.h" | 30 #include "components/policy/core/common/schema.h" |
30 #include "content/public/browser/browser_thread.h" | 31 #include "content/public/browser/browser_thread.h" |
32 #include "content/public/browser/notification_details.h" | |
31 #include "content/public/browser/notification_observer.h" | 33 #include "content/public/browser/notification_observer.h" |
32 #include "content/public/browser/notification_registrar.h" | 34 #include "content/public/browser/notification_registrar.h" |
33 #include "content/public/browser/notification_source.h" | 35 #include "content/public/browser/notification_source.h" |
34 #include "extensions/common/constants.h" | 36 #include "extensions/common/constants.h" |
35 #include "extensions/common/manifest.h" | 37 #include "extensions/common/manifest.h" |
36 #include "extensions/common/manifest_constants.h" | 38 #include "extensions/common/manifest_constants.h" |
37 #include "extensions/common/permissions/api_permission.h" | 39 #include "extensions/common/permissions/api_permission.h" |
38 | 40 |
39 using content::BrowserThread; | 41 using content::BrowserThread; |
40 | 42 |
(...skipping 18 matching lines...) Expand all Loading... | |
59 public: | 61 public: |
60 explicit ExtensionTracker(Profile* profile); | 62 explicit ExtensionTracker(Profile* profile); |
61 virtual ~ExtensionTracker() {} | 63 virtual ~ExtensionTracker() {} |
62 | 64 |
63 // NotificationObserver implementation: | 65 // NotificationObserver implementation: |
64 virtual void Observe(int type, | 66 virtual void Observe(int type, |
65 const content::NotificationSource& source, | 67 const content::NotificationSource& source, |
66 const content::NotificationDetails& details) OVERRIDE; | 68 const content::NotificationDetails& details) OVERRIDE; |
67 | 69 |
68 private: | 70 private: |
69 // Loads the schemas of the |extensions| and passes a PolicyDomainDescriptor | 71 bool IsEnterpriseExtension(const Extension* extension) const; |
70 // to RegisterDomain(). | 72 |
73 // Loads the schemas of the |extensions| and passes a ComponentMap to | |
74 // Register(). | |
71 static void LoadSchemas(scoped_ptr<ExtensionSet> extensions, | 75 static void LoadSchemas(scoped_ptr<ExtensionSet> extensions, |
72 base::WeakPtr<ExtensionTracker> self); | 76 base::WeakPtr<ExtensionTracker> self); |
73 void RegisterDomain( | 77 void Register(const policy::ComponentMap* components); |
74 scoped_refptr<const policy::PolicyDomainDescriptor> descriptor); | |
75 | 78 |
76 Profile* profile_; | 79 Profile* profile_; |
77 content::NotificationRegistrar registrar_; | 80 content::NotificationRegistrar registrar_; |
81 policy::SchemaRegistry* schema_registry_; | |
bartfab (slow)
2013/11/05 15:53:04
Nit: #include "chrome/browser/policy/schema_regist
Joao da Silva
2013/11/07 13:15:00
Done.
| |
78 base::WeakPtrFactory<ExtensionTracker> weak_factory_; | 82 base::WeakPtrFactory<ExtensionTracker> weak_factory_; |
79 | 83 |
80 DISALLOW_COPY_AND_ASSIGN(ExtensionTracker); | 84 DISALLOW_COPY_AND_ASSIGN(ExtensionTracker); |
81 }; | 85 }; |
82 | 86 |
83 ManagedValueStoreCache::ExtensionTracker::ExtensionTracker(Profile* profile) | 87 ManagedValueStoreCache::ExtensionTracker::ExtensionTracker(Profile* profile) |
84 : profile_(profile), | 88 : profile_(profile), |
89 schema_registry_( | |
90 policy::SchemaRegistryServiceFactory::GetForContext(profile)), | |
85 weak_factory_(this) { | 91 weak_factory_(this) { |
86 registrar_.Add(this, | 92 registrar_.Add(this, |
87 chrome::NOTIFICATION_EXTENSIONS_READY, | 93 chrome::NOTIFICATION_EXTENSIONS_READY, |
88 content::Source<Profile>(profile_)); | 94 content::Source<Profile>(profile_)); |
89 registrar_.Add(this, | 95 registrar_.Add(this, |
90 chrome::NOTIFICATION_EXTENSION_LOADED, | 96 chrome::NOTIFICATION_EXTENSION_LOADED, |
91 content::Source<Profile>(profile_)); | 97 content::Source<Profile>(profile_)); |
92 registrar_.Add(this, | 98 registrar_.Add(this, |
93 chrome::NOTIFICATION_EXTENSION_UNLOADED, | 99 chrome::NOTIFICATION_EXTENSION_UNLOADED, |
94 content::Source<Profile>(profile_)); | 100 content::Source<Profile>(profile_)); |
95 } | 101 } |
96 | 102 |
97 void ManagedValueStoreCache::ExtensionTracker::Observe( | 103 void ManagedValueStoreCache::ExtensionTracker::Observe( |
98 int type, | 104 int type, |
99 const content::NotificationSource& source, | 105 const content::NotificationSource& source, |
100 const content::NotificationDetails& details) { | 106 const content::NotificationDetails& details) { |
101 if (!ExtensionSystem::Get(profile_)->ready().is_signaled()) | 107 if (!ExtensionSystem::Get(profile_)->ready().is_signaled()) |
102 return; | 108 return; |
103 | 109 |
104 scoped_refptr<policy::PolicyDomainDescriptor> descriptor( | 110 if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) { |
105 new policy::PolicyDomainDescriptor(policy::POLICY_DOMAIN_EXTENSIONS)); | 111 const Extension* extension = |
106 const ExtensionSet* set = | 112 content::Details<UnloadedExtensionInfo>(details)->extension; |
107 ExtensionSystem::Get(profile_)->extension_service()->extensions(); | 113 if (IsEnterpriseExtension(extension)) { |
108 scoped_ptr<ExtensionSet> managed_extensions(new ExtensionSet()); | 114 schema_registry_->UnregisterComponent(policy::PolicyNamespace( |
bartfab (slow)
2013/11/05 15:53:04
Nit: #include "components/policy/core/common/polic
Joao da Silva
2013/11/07 13:15:00
Done.
| |
109 for (ExtensionSet::const_iterator it = set->begin(); it != set->end(); ++it) { | 115 policy::POLICY_DOMAIN_EXTENSIONS, extension->id())); |
110 if ((*it)->manifest()->HasPath(manifest_keys::kStorageManagedSchema)) { | |
111 managed_extensions->Insert(*it); | |
112 } | 116 } |
117 return; | |
118 } | |
113 | 119 |
114 // TODO(joaodasilva): also load extensions that use the storage API for now, | 120 scoped_ptr<ExtensionSet> set(new ExtensionSet()); |
bartfab (slow)
2013/11/05 15:53:04
Nit: No need for ().
Joao da Silva
2013/11/07 13:15:00
Done.
| |
115 // to support the Legacy Browser Support extension. Remove this. | 121 if (type == chrome::NOTIFICATION_EXTENSION_LOADED) { |
116 // http://crbug.com/240704 | 122 const Extension* extension = |
117 if ((*it)->HasAPIPermission(APIPermission::kStorage)) | 123 content::Details<const Extension>(details).ptr(); |
118 managed_extensions->Insert(*it); | 124 if (IsEnterpriseExtension(extension)) |
125 set->Insert(extension); | |
126 } else if (type == chrome::NOTIFICATION_EXTENSIONS_READY) { | |
127 const ExtensionSet* extensions = | |
128 ExtensionSystem::Get(profile_)->extension_service()->extensions(); | |
129 for (ExtensionSet::const_iterator it = extensions->begin(); | |
130 it != extensions->end(); ++it) { | |
131 if (IsEnterpriseExtension(*it)) | |
132 set->Insert(*it); | |
133 } | |
134 } else { | |
135 NOTREACHED(); | |
119 } | 136 } |
120 | 137 |
138 if (set->is_empty()) | |
139 return; | |
140 | |
121 // Load the schema files in a background thread. | 141 // Load the schema files in a background thread. |
122 BrowserThread::PostBlockingPoolSequencedTask( | 142 BrowserThread::PostBlockingPoolSequencedTask( |
123 kLoadSchemasBackgroundTaskTokenName, FROM_HERE, | 143 kLoadSchemasBackgroundTaskTokenName, FROM_HERE, |
124 base::Bind(&ExtensionTracker::LoadSchemas, | 144 base::Bind(&ExtensionTracker::LoadSchemas, |
125 base::Passed(&managed_extensions), | 145 base::Passed(&set), |
126 weak_factory_.GetWeakPtr())); | 146 weak_factory_.GetWeakPtr())); |
127 } | 147 } |
128 | 148 |
149 bool ManagedValueStoreCache::ExtensionTracker::IsEnterpriseExtension( | |
150 const Extension* extension) const { | |
151 if (extension->manifest()->HasPath(manifest_keys::kStorageManagedSchema)) | |
152 return true; | |
153 | |
154 // TODO(joaodasilva): also load extensions that use the storage API for now, | |
155 // to support the Legacy Browser Support extension. Remove this. | |
156 // http://crbug.com/240704 | |
157 if (extension->HasAPIPermission(APIPermission::kStorage)) | |
158 return true; | |
159 | |
160 return false; | |
161 } | |
162 | |
129 // static | 163 // static |
130 void ManagedValueStoreCache::ExtensionTracker::LoadSchemas( | 164 void ManagedValueStoreCache::ExtensionTracker::LoadSchemas( |
131 scoped_ptr<ExtensionSet> extensions, | 165 scoped_ptr<ExtensionSet> extensions, |
132 base::WeakPtr<ExtensionTracker> self) { | 166 base::WeakPtr<ExtensionTracker> self) { |
133 scoped_refptr<policy::PolicyDomainDescriptor> descriptor = | 167 policy::ComponentMap* components = new policy::ComponentMap(); |
bartfab (slow)
2013/11/05 15:53:04
Nit 1: No need for ().
Nit 2: Use a scoped_ptr.
Joao da Silva
2013/11/07 13:15:00
Done.
| |
134 new policy::PolicyDomainDescriptor(policy::POLICY_DOMAIN_EXTENSIONS); | |
135 | 168 |
136 for (ExtensionSet::const_iterator it = extensions->begin(); | 169 for (ExtensionSet::const_iterator it = extensions->begin(); |
137 it != extensions->end(); ++it) { | 170 it != extensions->end(); ++it) { |
138 std::string schema_file; | 171 std::string schema_file; |
139 if (!(*it)->manifest()->GetString( | 172 if (!(*it)->manifest()->GetString( |
140 manifest_keys::kStorageManagedSchema, &schema_file)) { | 173 manifest_keys::kStorageManagedSchema, &schema_file)) { |
141 // TODO(joaodasilva): Remove this. http://crbug.com/240704 | 174 // TODO(joaodasilva): Remove this. http://crbug.com/240704 |
142 if ((*it)->HasAPIPermission(APIPermission::kStorage)) { | 175 if ((*it)->HasAPIPermission(APIPermission::kStorage)) { |
143 descriptor->RegisterComponent((*it)->id(), policy::Schema()); | 176 (*components)[(*it)->id()] = policy::Schema(); |
144 } else { | 177 } else { |
145 NOTREACHED(); | 178 NOTREACHED(); |
146 } | 179 } |
147 continue; | 180 continue; |
148 } | 181 } |
149 // The extension should have been validated, so assume the schema exists | 182 // The extension should have been validated, so assume the schema exists |
150 // and is valid. | 183 // and is valid. |
151 std::string error; | 184 std::string error; |
152 policy::Schema schema = | 185 policy::Schema schema = |
153 StorageSchemaManifestHandler::GetSchema(it->get(), &error); | 186 StorageSchemaManifestHandler::GetSchema(it->get(), &error); |
154 CHECK(schema.valid()) << error; | 187 CHECK(schema.valid()) << error; |
155 descriptor->RegisterComponent((*it)->id(), schema); | 188 (*components)[(*it)->id()] = schema; |
156 } | 189 } |
157 | 190 |
158 BrowserThread::PostTask( | 191 BrowserThread::PostTask( |
159 BrowserThread::UI, FROM_HERE, | 192 BrowserThread::UI, FROM_HERE, |
160 base::Bind(&ExtensionTracker::RegisterDomain, self, descriptor)); | 193 base::Bind(&ExtensionTracker::Register, self, base::Owned(components))); |
161 } | 194 } |
162 | 195 |
163 void ManagedValueStoreCache::ExtensionTracker::RegisterDomain( | 196 void ManagedValueStoreCache::ExtensionTracker::Register( |
164 scoped_refptr<const policy::PolicyDomainDescriptor> descriptor) { | 197 const policy::ComponentMap* components) { |
165 policy::ProfilePolicyConnector* connector = | 198 schema_registry_->RegisterComponents(policy::POLICY_DOMAIN_EXTENSIONS, |
166 policy::ProfilePolicyConnectorFactory::GetForProfile(profile_); | 199 *components); |
167 connector->policy_service()->RegisterPolicyDomain(descriptor); | |
168 } | 200 } |
169 | 201 |
170 ManagedValueStoreCache::ManagedValueStoreCache( | 202 ManagedValueStoreCache::ManagedValueStoreCache( |
171 Profile* profile, | 203 Profile* profile, |
172 const scoped_refptr<SettingsStorageFactory>& factory, | 204 const scoped_refptr<SettingsStorageFactory>& factory, |
173 const scoped_refptr<SettingsObserverList>& observers) | 205 const scoped_refptr<SettingsObserverList>& observers) |
174 : weak_factory_(this), | 206 : weak_factory_(this), |
175 weak_this_on_ui_(weak_factory_.GetWeakPtr()), | 207 weak_this_on_ui_(weak_factory_.GetWeakPtr()), |
176 profile_(profile), | 208 profile_(profile), |
177 event_router_(ExtensionSystem::Get(profile)->event_router()), | 209 event_router_(ExtensionSystem::Get(profile)->event_router()), |
178 storage_factory_(factory), | 210 storage_factory_(factory), |
179 observers_(observers), | 211 observers_(observers), |
180 base_path_(profile->GetPath().AppendASCII( | 212 base_path_(profile->GetPath().AppendASCII( |
181 extensions::kManagedSettingsDirectoryName)) { | 213 extensions::kManagedSettingsDirectoryName)) { |
182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
183 // |event_router_| can be NULL on unit_tests. | 215 // |event_router_| can be NULL on unit_tests. |
184 if (event_router_) | 216 if (event_router_) |
185 event_router_->RegisterObserver(this, storage::OnChanged::kEventName); | 217 event_router_->RegisterObserver(this, storage::OnChanged::kEventName); |
186 | 218 |
187 GetPolicyService()->AddObserver(policy::POLICY_DOMAIN_EXTENSIONS, this); | 219 GetPolicyService()->AddObserver(policy::POLICY_DOMAIN_EXTENSIONS, this); |
188 | 220 |
189 extension_tracker_.reset(new ExtensionTracker(profile_)); | 221 // Track the extensions of the original Profile only; the OTR profile has |
222 // a subset of those. | |
223 if (!profile->IsOffTheRecord()) | |
224 extension_tracker_.reset(new ExtensionTracker(profile_)); | |
190 } | 225 } |
191 | 226 |
192 ManagedValueStoreCache::~ManagedValueStoreCache() { | 227 ManagedValueStoreCache::~ManagedValueStoreCache() { |
193 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 228 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
194 DCHECK(!event_router_); | 229 DCHECK(!event_router_); |
195 // Delete the PolicyValueStores on FILE. | 230 // Delete the PolicyValueStores on FILE. |
196 store_map_.clear(); | 231 store_map_.clear(); |
197 } | 232 } |
198 | 233 |
199 void ManagedValueStoreCache::ShutdownOnUI() { | 234 void ManagedValueStoreCache::ShutdownOnUI() { |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
403 continuation.Run(); | 438 continuation.Run(); |
404 } | 439 } |
405 | 440 |
406 policy::PolicyService* ManagedValueStoreCache::GetPolicyService() { | 441 policy::PolicyService* ManagedValueStoreCache::GetPolicyService() { |
407 policy::ProfilePolicyConnector* connector = | 442 policy::ProfilePolicyConnector* connector = |
408 policy::ProfilePolicyConnectorFactory::GetForProfile(profile_); | 443 policy::ProfilePolicyConnectorFactory::GetForProfile(profile_); |
409 return connector->policy_service(); | 444 return connector->policy_service(); |
410 } | 445 } |
411 | 446 |
412 } // namespace extensions | 447 } // namespace extensions |
OLD | NEW |