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

Side by Side Diff: chrome/browser/extensions/permissions_updater.cc

Issue 2499493004: Communicate ExtensionSettings policy to renderers (Closed)
Patch Set: -Updated unit tests, fixed nits, moved IsRuntimeBlockedHost for safety Created 3 years, 8 months 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/permissions_updater.h" 5 #include "chrome/browser/extensions/permissions_updater.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
10 #include "base/values.h" 10 #include "base/values.h"
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 // not the user, removed the permissions. This allows the extension to add 129 // not the user, removed the permissions. This allows the extension to add
130 // them again without prompting the user. 130 // them again without prompting the user.
131 if (remove_type == REMOVE_HARD) { 131 if (remove_type == REMOVE_HARD) {
132 ExtensionPrefs::Get(browser_context_) 132 ExtensionPrefs::Get(browser_context_)
133 ->RemoveGrantedPermissions(extension->id(), to_remove); 133 ->RemoveGrantedPermissions(extension->id(), to_remove);
134 } 134 }
135 135
136 NotifyPermissionsUpdated(REMOVED, extension, to_remove); 136 NotifyPermissionsUpdated(REMOVED, extension, to_remove);
137 } 137 }
138 138
139 void PermissionsUpdater::SetPolicyHostRestrictions(
140 const Extension* extension,
141 const URLPatternSet& runtime_blocked_hosts,
142 const URLPatternSet& runtime_allowed_hosts) {
143 extension->permissions_data()->SetPolicyHostRestrictions(
144 runtime_blocked_hosts, runtime_allowed_hosts);
145
146 // Send notification to the currently running renderers of the runtime block
147 // hosts settings.
148 const PermissionSet perms;
149 NotifyPermissionsUpdated(POLICY, extension, perms);
150 }
151
152 void PermissionsUpdater::SetUsesDefaultHostRestrictions(
153 const Extension* extension) {
154 extension->permissions_data()->SetUsesDefaultHostRestrictions();
155 const PermissionSet perms;
156 NotifyPermissionsUpdated(POLICY, extension, perms);
157 }
158
159 void PermissionsUpdater::SetDefaultPolicyHostRestrictions(
160 const URLPatternSet& default_runtime_blocked_hosts,
161 const URLPatternSet& default_runtime_allowed_hosts) {
162 PermissionsData::SetDefaultPolicyHostRestrictions(
163 default_runtime_blocked_hosts, default_runtime_allowed_hosts);
164
165 // Send notification to the currently running renderers of the runtime block
166 // hosts settings.
167 NotifyDefaultPolicyHostRestrictionsUpdated(default_runtime_blocked_hosts,
168 default_runtime_allowed_hosts);
169 }
170
139 void PermissionsUpdater::RemovePermissionsUnsafe( 171 void PermissionsUpdater::RemovePermissionsUnsafe(
140 const Extension* extension, 172 const Extension* extension,
141 const PermissionSet& to_remove) { 173 const PermissionSet& to_remove) {
142 const PermissionSet& active = 174 const PermissionSet& active =
143 extension->permissions_data()->active_permissions(); 175 extension->permissions_data()->active_permissions();
144 std::unique_ptr<const PermissionSet> total = 176 std::unique_ptr<const PermissionSet> total =
145 PermissionSet::CreateDifference(active, to_remove); 177 PermissionSet::CreateDifference(active, to_remove);
146 // |successfully_removed| might not equal |to_remove| if |to_remove| contains 178 // |successfully_removed| might not equal |to_remove| if |to_remove| contains
147 // permissions the extension didn't have. 179 // permissions the extension didn't have.
148 std::unique_ptr<const PermissionSet> successfully_removed = 180 std::unique_ptr<const PermissionSet> successfully_removed =
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 new Event(histogram_value, event_name, std::move(value))); 269 new Event(histogram_value, event_name, std::move(value)));
238 event->restrict_to_browser_context = browser_context_; 270 event->restrict_to_browser_context = browser_context_;
239 event_router->DispatchEventToExtension(extension_id, std::move(event)); 271 event_router->DispatchEventToExtension(extension_id, std::move(event));
240 } 272 }
241 273
242 void PermissionsUpdater::NotifyPermissionsUpdated( 274 void PermissionsUpdater::NotifyPermissionsUpdated(
243 EventType event_type, 275 EventType event_type,
244 const Extension* extension, 276 const Extension* extension,
245 const PermissionSet& changed) { 277 const PermissionSet& changed) {
246 DCHECK((init_flag_ & INIT_FLAG_TRANSIENT) == 0); 278 DCHECK((init_flag_ & INIT_FLAG_TRANSIENT) == 0);
247 if (changed.IsEmpty()) 279
280 if (changed.IsEmpty() && event_type != POLICY)
248 return; 281 return;
249 282
250 UpdatedExtensionPermissionsInfo::Reason reason; 283 UpdatedExtensionPermissionsInfo::Reason reason;
251 events::HistogramValue histogram_value; 284 events::HistogramValue histogram_value;
252 const char* event_name = NULL; 285 const char* event_name = NULL;
286 Profile* profile = Profile::FromBrowserContext(browser_context_);
253 287
254 if (event_type == REMOVED) { 288 if (event_type == REMOVED) {
255 reason = UpdatedExtensionPermissionsInfo::REMOVED; 289 reason = UpdatedExtensionPermissionsInfo::REMOVED;
256 histogram_value = events::PERMISSIONS_ON_REMOVED; 290 histogram_value = events::PERMISSIONS_ON_REMOVED;
257 event_name = permissions::OnRemoved::kEventName; 291 event_name = permissions::OnRemoved::kEventName;
258 } else { 292 } else if (event_type == ADDED) {
259 CHECK_EQ(ADDED, event_type);
260 reason = UpdatedExtensionPermissionsInfo::ADDED; 293 reason = UpdatedExtensionPermissionsInfo::ADDED;
261 histogram_value = events::PERMISSIONS_ON_ADDED; 294 histogram_value = events::PERMISSIONS_ON_ADDED;
262 event_name = permissions::OnAdded::kEventName; 295 event_name = permissions::OnAdded::kEventName;
296 } else {
297 CHECK_EQ(POLICY, event_type);
dcheng 2017/04/13 22:07:40 Unrelated (since this is inherited from the past c
nrpeter 2017/04/13 23:05:30 Done.
298 reason = UpdatedExtensionPermissionsInfo::POLICY;
263 } 299 }
264 300
265 // Notify other APIs or interested parties. 301 // Notify other APIs or interested parties.
266 UpdatedExtensionPermissionsInfo info = UpdatedExtensionPermissionsInfo( 302 UpdatedExtensionPermissionsInfo info =
267 extension, changed, reason); 303 UpdatedExtensionPermissionsInfo(extension, changed, reason);
268 Profile* profile = Profile::FromBrowserContext(browser_context_);
269 content::NotificationService::current()->Notify( 304 content::NotificationService::current()->Notify(
270 extensions::NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED, 305 extensions::NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED,
271 content::Source<Profile>(profile), 306 content::Source<Profile>(profile),
272 content::Details<UpdatedExtensionPermissionsInfo>(&info)); 307 content::Details<UpdatedExtensionPermissionsInfo>(&info));
273 308
274 ExtensionMsg_UpdatePermissions_Params params; 309 ExtensionMsg_UpdatePermissions_Params params;
275 params.extension_id = extension->id(); 310 params.extension_id = extension->id();
276 params.active_permissions = ExtensionMsg_PermissionSetStruct( 311 params.active_permissions = ExtensionMsg_PermissionSetStruct(
277 extension->permissions_data()->active_permissions()); 312 extension->permissions_data()->active_permissions());
278 params.withheld_permissions = ExtensionMsg_PermissionSetStruct( 313 params.withheld_permissions = ExtensionMsg_PermissionSetStruct(
279 extension->permissions_data()->withheld_permissions()); 314 extension->permissions_data()->withheld_permissions());
315 params.uses_default_policy_host_restrictions =
316 extension->permissions_data()->UsesDefaultPolicyHostRestrictions();
317 if (!params.uses_default_policy_host_restrictions) {
318 params.policy_blocked_hosts =
319 extension->permissions_data()->policy_blocked_hosts();
320 params.policy_allowed_hosts =
321 extension->permissions_data()->policy_allowed_hosts();
322 }
280 323
281 // Send the new permissions to the renderers. 324 // Send the new permissions to the renderers.
282 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); 325 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
283 !i.IsAtEnd(); i.Advance()) { 326 !i.IsAtEnd(); i.Advance()) {
284 RenderProcessHost* host = i.GetCurrentValue(); 327 RenderProcessHost* host = i.GetCurrentValue();
285 if (profile->IsSameProfile( 328 if (profile->IsSameProfile(
286 Profile::FromBrowserContext(host->GetBrowserContext()))) { 329 Profile::FromBrowserContext(host->GetBrowserContext()))) {
287 host->Send(new ExtensionMsg_UpdatePermissions(params)); 330 host->Send(new ExtensionMsg_UpdatePermissions(params));
288 } 331 }
289 } 332 }
290 333
291 // Trigger the onAdded and onRemoved events in the extension. 334 // Trigger the onAdded and onRemoved events in the extension. We explicitly
292 DispatchEvent(extension->id(), histogram_value, event_name, changed); 335 // don't do this for policy-related events.
336 if (event_name)
337 DispatchEvent(extension->id(), histogram_value, event_name, changed);
338 }
339
340 // Notify the renderers that extension policy (policy_blocked_hosts) is updated
341 // and provide new set of hosts.
342 void PermissionsUpdater::NotifyDefaultPolicyHostRestrictionsUpdated(
343 const URLPatternSet& default_runtime_blocked_hosts,
344 const URLPatternSet& default_runtime_allowed_hosts) {
345 DCHECK((init_flag_ & INIT_FLAG_TRANSIENT) == 0);
346
347 Profile* profile = Profile::FromBrowserContext(browser_context_);
348
349 ExtensionMsg_UpdateDefaultPolicyHostRestrictions_Params params;
350 params.default_policy_blocked_hosts = default_runtime_blocked_hosts;
351 params.default_policy_allowed_hosts = default_runtime_allowed_hosts;
352
353 // Send the new policy to the renderers.
354 for (RenderProcessHost::iterator host_iterator(
355 RenderProcessHost::AllHostsIterator());
356 !host_iterator.IsAtEnd(); host_iterator.Advance()) {
357 RenderProcessHost* host = host_iterator.GetCurrentValue();
358 if (profile->IsSameProfile(
359 Profile::FromBrowserContext(host->GetBrowserContext()))) {
360 host->Send(new ExtensionMsg_UpdateDefaultPolicyHostRestrictions(params));
361 }
362 }
293 } 363 }
294 364
295 } // namespace extensions 365 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698