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.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 Loading... |
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 UsesManagedStorage(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) { |
| 109 // All the extensions installed on this Profile will be loaded on startup, |
| 110 // triggering multiple NOTIFICATION_EXTENSION_LOADED. |
| 111 // Wait until all of them are ready before registering the schemas of managed |
| 112 // extensions, so that the policy loaders only have to reload at most once. |
101 if (!ExtensionSystem::Get(profile_)->ready().is_signaled()) | 113 if (!ExtensionSystem::Get(profile_)->ready().is_signaled()) |
102 return; | 114 return; |
103 | 115 |
104 scoped_refptr<policy::PolicyDomainDescriptor> descriptor( | 116 scoped_ptr<ExtensionSet> added(new ExtensionSet); |
105 new policy::PolicyDomainDescriptor(policy::POLICY_DOMAIN_EXTENSIONS)); | 117 const Extension* removed = NULL; |
106 const ExtensionSet* set = | 118 switch (type) { |
107 ExtensionSystem::Get(profile_)->extension_service()->extensions(); | 119 case chrome::NOTIFICATION_EXTENSIONS_READY: |
108 scoped_ptr<ExtensionSet> managed_extensions(new ExtensionSet()); | 120 added->InsertAll( |
109 for (ExtensionSet::const_iterator it = set->begin(); it != set->end(); ++it) { | 121 *ExtensionSystem::Get(profile_)->extension_service()->extensions()); |
110 if ((*it)->manifest()->HasPath(manifest_keys::kStorageManagedSchema)) { | 122 break; |
111 managed_extensions->Insert(*it); | 123 case chrome::NOTIFICATION_EXTENSION_LOADED: |
| 124 added->Insert(content::Details<const Extension>(details).ptr()); |
| 125 break; |
| 126 case chrome::NOTIFICATION_EXTENSION_UNLOADED: |
| 127 removed = content::Details<UnloadedExtensionInfo>(details)->extension; |
| 128 break; |
| 129 default: |
| 130 NOTREACHED(); |
| 131 return; |
| 132 } |
| 133 |
| 134 if (removed) { |
| 135 if (UsesManagedStorage(removed)) { |
| 136 schema_registry_->UnregisterComponent(policy::PolicyNamespace( |
| 137 policy::POLICY_DOMAIN_EXTENSIONS, removed->id())); |
112 } | 138 } |
| 139 return; |
| 140 } |
113 | 141 |
114 // TODO(joaodasilva): also load extensions that use the storage API for now, | 142 ExtensionSet::const_iterator it = added->begin(); |
115 // to support the Legacy Browser Support extension. Remove this. | 143 while (it != added->end()) { |
116 // http://crbug.com/240704 | 144 std::string to_remove; |
117 if ((*it)->HasAPIPermission(APIPermission::kStorage)) | 145 if (!UsesManagedStorage(*it)) |
118 managed_extensions->Insert(*it); | 146 to_remove = (*it)->id(); |
| 147 ++it; |
| 148 if (!to_remove.empty()) |
| 149 added->Remove(to_remove); |
119 } | 150 } |
120 | 151 |
| 152 if (added->is_empty()) |
| 153 return; |
| 154 |
121 // Load the schema files in a background thread. | 155 // Load the schema files in a background thread. |
122 BrowserThread::PostBlockingPoolSequencedTask( | 156 BrowserThread::PostBlockingPoolSequencedTask( |
123 kLoadSchemasBackgroundTaskTokenName, FROM_HERE, | 157 kLoadSchemasBackgroundTaskTokenName, FROM_HERE, |
124 base::Bind(&ExtensionTracker::LoadSchemas, | 158 base::Bind(&ExtensionTracker::LoadSchemas, |
125 base::Passed(&managed_extensions), | 159 base::Passed(&added), |
126 weak_factory_.GetWeakPtr())); | 160 weak_factory_.GetWeakPtr())); |
127 } | 161 } |
128 | 162 |
| 163 bool ManagedValueStoreCache::ExtensionTracker::UsesManagedStorage( |
| 164 const Extension* extension) const { |
| 165 if (extension->manifest()->HasPath(manifest_keys::kStorageManagedSchema)) |
| 166 return true; |
| 167 |
| 168 // TODO(joaodasilva): also load extensions that use the storage API for now, |
| 169 // to support the Legacy Browser Support extension. Remove this. |
| 170 // http://crbug.com/240704 |
| 171 if (extension->HasAPIPermission(APIPermission::kStorage)) |
| 172 return true; |
| 173 |
| 174 return false; |
| 175 } |
| 176 |
129 // static | 177 // static |
130 void ManagedValueStoreCache::ExtensionTracker::LoadSchemas( | 178 void ManagedValueStoreCache::ExtensionTracker::LoadSchemas( |
131 scoped_ptr<ExtensionSet> extensions, | 179 scoped_ptr<ExtensionSet> extensions, |
132 base::WeakPtr<ExtensionTracker> self) { | 180 base::WeakPtr<ExtensionTracker> self) { |
133 scoped_refptr<policy::PolicyDomainDescriptor> descriptor = | 181 scoped_ptr<policy::ComponentMap> components(new policy::ComponentMap); |
134 new policy::PolicyDomainDescriptor(policy::POLICY_DOMAIN_EXTENSIONS); | |
135 | 182 |
136 for (ExtensionSet::const_iterator it = extensions->begin(); | 183 for (ExtensionSet::const_iterator it = extensions->begin(); |
137 it != extensions->end(); ++it) { | 184 it != extensions->end(); ++it) { |
138 std::string schema_file; | 185 std::string schema_file; |
139 if (!(*it)->manifest()->GetString( | 186 if (!(*it)->manifest()->GetString( |
140 manifest_keys::kStorageManagedSchema, &schema_file)) { | 187 manifest_keys::kStorageManagedSchema, &schema_file)) { |
141 // TODO(joaodasilva): Remove this. http://crbug.com/240704 | 188 // TODO(joaodasilva): Remove this. http://crbug.com/240704 |
142 if ((*it)->HasAPIPermission(APIPermission::kStorage)) { | 189 if ((*it)->HasAPIPermission(APIPermission::kStorage)) { |
143 descriptor->RegisterComponent((*it)->id(), policy::Schema()); | 190 (*components)[(*it)->id()] = policy::Schema(); |
144 } else { | 191 } else { |
145 NOTREACHED(); | 192 NOTREACHED(); |
146 } | 193 } |
147 continue; | 194 continue; |
148 } | 195 } |
149 // The extension should have been validated, so assume the schema exists | 196 // The extension should have been validated, so assume the schema exists |
150 // and is valid. | 197 // and is valid. |
151 std::string error; | 198 std::string error; |
152 policy::Schema schema = | 199 policy::Schema schema = |
153 StorageSchemaManifestHandler::GetSchema(it->get(), &error); | 200 StorageSchemaManifestHandler::GetSchema(it->get(), &error); |
154 CHECK(schema.valid()) << error; | 201 CHECK(schema.valid()) << error; |
155 descriptor->RegisterComponent((*it)->id(), schema); | 202 (*components)[(*it)->id()] = schema; |
156 } | 203 } |
157 | 204 |
158 BrowserThread::PostTask( | 205 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
159 BrowserThread::UI, FROM_HERE, | 206 base::Bind(&ExtensionTracker::Register, self, |
160 base::Bind(&ExtensionTracker::RegisterDomain, self, descriptor)); | 207 base::Owned(components.release()))); |
161 } | 208 } |
162 | 209 |
163 void ManagedValueStoreCache::ExtensionTracker::RegisterDomain( | 210 void ManagedValueStoreCache::ExtensionTracker::Register( |
164 scoped_refptr<const policy::PolicyDomainDescriptor> descriptor) { | 211 const policy::ComponentMap* components) { |
165 policy::ProfilePolicyConnector* connector = | 212 schema_registry_->RegisterComponents(policy::POLICY_DOMAIN_EXTENSIONS, |
166 policy::ProfilePolicyConnectorFactory::GetForProfile(profile_); | 213 *components); |
167 connector->policy_service()->RegisterPolicyDomain(descriptor); | |
168 } | 214 } |
169 | 215 |
170 ManagedValueStoreCache::ManagedValueStoreCache( | 216 ManagedValueStoreCache::ManagedValueStoreCache( |
171 Profile* profile, | 217 Profile* profile, |
172 const scoped_refptr<SettingsStorageFactory>& factory, | 218 const scoped_refptr<SettingsStorageFactory>& factory, |
173 const scoped_refptr<SettingsObserverList>& observers) | 219 const scoped_refptr<SettingsObserverList>& observers) |
174 : weak_factory_(this), | 220 : weak_factory_(this), |
175 weak_this_on_ui_(weak_factory_.GetWeakPtr()), | 221 weak_this_on_ui_(weak_factory_.GetWeakPtr()), |
176 profile_(profile), | 222 profile_(profile), |
177 event_router_(ExtensionSystem::Get(profile)->event_router()), | 223 event_router_(ExtensionSystem::Get(profile)->event_router()), |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 continuation.Run(); | 449 continuation.Run(); |
404 } | 450 } |
405 | 451 |
406 policy::PolicyService* ManagedValueStoreCache::GetPolicyService() { | 452 policy::PolicyService* ManagedValueStoreCache::GetPolicyService() { |
407 policy::ProfilePolicyConnector* connector = | 453 policy::ProfilePolicyConnector* connector = |
408 policy::ProfilePolicyConnectorFactory::GetForProfile(profile_); | 454 policy::ProfilePolicyConnectorFactory::GetForProfile(profile_); |
409 return connector->policy_service(); | 455 return connector->policy_service(); |
410 } | 456 } |
411 | 457 |
412 } // namespace extensions | 458 } // namespace extensions |
OLD | NEW |