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/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 Loading... | |
| 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 // Keep track of runtime blocked and hosts for this extension in the browser | |
| 144 // process. We'll pull from here to populate when a new renderer is created. | |
| 145 extension->permissions_data()->SetPolicyHostRestrictions( | |
| 146 runtime_blocked_hosts, runtime_allowed_hosts); | |
| 147 | |
| 148 // Send notification to the currently running renderers of the runtime block | |
| 149 // hosts settings. | |
| 150 const PermissionSet perms; | |
| 151 NotifyPermissionsUpdated(POLICY, extension, perms); | |
| 152 } | |
| 153 | |
| 154 void PermissionsUpdater::SetUsesDefaultHostRestrictions( | |
| 155 const Extension* extension, | |
| 156 bool uses_default_restrictions) { | |
| 157 extension->permissions_data()->SetUsesDefaultHostRestrictions( | |
| 158 uses_default_restrictions); | |
| 159 } | |
| 160 | |
| 161 void PermissionsUpdater::SetDefaultPolicyHostRestrictions( | |
| 162 const URLPatternSet& default_runtime_blocked_hosts, | |
| 163 const URLPatternSet& default_runtime_allowed_hosts) { | |
| 164 // Keep track of runtime blocked and hosts for extensions without an | |
| 165 // individual policy. We'll pull from here when a new renderer is created. | |
| 166 PermissionsData::SetDefaultPolicyHostRestrictions( | |
| 167 default_runtime_blocked_hosts, default_runtime_allowed_hosts); | |
| 168 | |
| 169 // Send notification to the currently running renderers of the runtime block | |
| 170 // hosts settings. | |
| 171 NotifyDefaultPolicyHostRestrictionsUpdated(default_runtime_blocked_hosts, | |
| 172 default_runtime_allowed_hosts); | |
| 173 } | |
| 174 | |
| 139 void PermissionsUpdater::RemovePermissionsUnsafe( | 175 void PermissionsUpdater::RemovePermissionsUnsafe( |
| 140 const Extension* extension, | 176 const Extension* extension, |
| 141 const PermissionSet& to_remove) { | 177 const PermissionSet& to_remove) { |
| 142 const PermissionSet& active = | 178 const PermissionSet& active = |
| 143 extension->permissions_data()->active_permissions(); | 179 extension->permissions_data()->active_permissions(); |
| 144 std::unique_ptr<const PermissionSet> total = | 180 std::unique_ptr<const PermissionSet> total = |
| 145 PermissionSet::CreateDifference(active, to_remove); | 181 PermissionSet::CreateDifference(active, to_remove); |
| 146 // |successfully_removed| might not equal |to_remove| if |to_remove| contains | 182 // |successfully_removed| might not equal |to_remove| if |to_remove| contains |
| 147 // permissions the extension didn't have. | 183 // permissions the extension didn't have. |
| 148 std::unique_ptr<const PermissionSet> successfully_removed = | 184 std::unique_ptr<const PermissionSet> successfully_removed = |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 237 new Event(histogram_value, event_name, std::move(value))); | 273 new Event(histogram_value, event_name, std::move(value))); |
| 238 event->restrict_to_browser_context = browser_context_; | 274 event->restrict_to_browser_context = browser_context_; |
| 239 event_router->DispatchEventToExtension(extension_id, std::move(event)); | 275 event_router->DispatchEventToExtension(extension_id, std::move(event)); |
| 240 } | 276 } |
| 241 | 277 |
| 242 void PermissionsUpdater::NotifyPermissionsUpdated( | 278 void PermissionsUpdater::NotifyPermissionsUpdated( |
| 243 EventType event_type, | 279 EventType event_type, |
| 244 const Extension* extension, | 280 const Extension* extension, |
| 245 const PermissionSet& changed) { | 281 const PermissionSet& changed) { |
| 246 DCHECK((init_flag_ & INIT_FLAG_TRANSIENT) == 0); | 282 DCHECK((init_flag_ & INIT_FLAG_TRANSIENT) == 0); |
| 247 if (changed.IsEmpty()) | |
| 248 return; | |
| 249 | 283 |
| 250 UpdatedExtensionPermissionsInfo::Reason reason; | 284 UpdatedExtensionPermissionsInfo::Reason reason; |
| 251 events::HistogramValue histogram_value; | 285 events::HistogramValue histogram_value; |
| 252 const char* event_name = NULL; | 286 const char* event_name = NULL; |
| 287 Profile* profile = Profile::FromBrowserContext(browser_context_); | |
| 288 | |
| 289 if (changed.IsEmpty() && event_type != POLICY) | |
| 290 return; | |
| 253 | 291 |
| 254 if (event_type == REMOVED) { | 292 if (event_type == REMOVED) { |
| 255 reason = UpdatedExtensionPermissionsInfo::REMOVED; | 293 reason = UpdatedExtensionPermissionsInfo::REMOVED; |
| 256 histogram_value = events::PERMISSIONS_ON_REMOVED; | 294 histogram_value = events::PERMISSIONS_ON_REMOVED; |
| 257 event_name = permissions::OnRemoved::kEventName; | 295 event_name = permissions::OnRemoved::kEventName; |
| 258 } else { | 296 } else if (event_type == ADDED) { |
| 259 CHECK_EQ(ADDED, event_type); | |
| 260 reason = UpdatedExtensionPermissionsInfo::ADDED; | 297 reason = UpdatedExtensionPermissionsInfo::ADDED; |
| 261 histogram_value = events::PERMISSIONS_ON_ADDED; | 298 histogram_value = events::PERMISSIONS_ON_ADDED; |
| 262 event_name = permissions::OnAdded::kEventName; | 299 event_name = permissions::OnAdded::kEventName; |
| 300 } else { | |
| 301 CHECK_EQ(POLICY, event_type); | |
| 302 reason = UpdatedExtensionPermissionsInfo::POLICY; | |
| 263 } | 303 } |
| 264 | 304 |
| 265 // Notify other APIs or interested parties. | 305 // Notify other APIs or interested parties. |
| 266 UpdatedExtensionPermissionsInfo info = UpdatedExtensionPermissionsInfo( | 306 UpdatedExtensionPermissionsInfo info = |
| 267 extension, changed, reason); | 307 UpdatedExtensionPermissionsInfo(extension, changed, reason); |
| 268 Profile* profile = Profile::FromBrowserContext(browser_context_); | |
| 269 content::NotificationService::current()->Notify( | 308 content::NotificationService::current()->Notify( |
| 270 extensions::NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED, | 309 extensions::NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED, |
| 271 content::Source<Profile>(profile), | 310 content::Source<Profile>(profile), |
| 272 content::Details<UpdatedExtensionPermissionsInfo>(&info)); | 311 content::Details<UpdatedExtensionPermissionsInfo>(&info)); |
| 273 | 312 |
| 274 ExtensionMsg_UpdatePermissions_Params params; | 313 ExtensionMsg_UpdatePermissions_Params params; |
| 275 params.extension_id = extension->id(); | 314 params.extension_id = extension->id(); |
| 276 params.active_permissions = ExtensionMsg_PermissionSetStruct( | 315 params.active_permissions = ExtensionMsg_PermissionSetStruct( |
| 277 extension->permissions_data()->active_permissions()); | 316 extension->permissions_data()->active_permissions()); |
| 278 params.withheld_permissions = ExtensionMsg_PermissionSetStruct( | 317 params.withheld_permissions = ExtensionMsg_PermissionSetStruct( |
| 279 extension->permissions_data()->withheld_permissions()); | 318 extension->permissions_data()->withheld_permissions()); |
| 319 params.uses_default_policy_host_restrictions = | |
| 320 extension->permissions_data()->UsesDefaultPolicyHostRestrictions(); | |
| 321 if (!params.uses_default_policy_host_restrictions) { | |
| 322 params.policy_blocked_hosts = | |
| 323 extension->permissions_data()->policy_blocked_hosts(); | |
| 324 params.policy_allowed_hosts = | |
| 325 extension->permissions_data()->policy_allowed_hosts(); | |
|
dcheng
2017/03/31 23:48:06
This part makes it seem either-or.
nrpeter
2017/04/01 00:19:02
Yes, an extension either uses the default, or prov
dcheng
2017/04/01 00:24:07
Right, that's fine: what I'm saying is that callin
nrpeter
2017/04/05 23:13:26
I've SetPolicyHostRestrictions to automatically se
| |
| 326 } | |
| 280 | 327 |
| 281 // Send the new permissions to the renderers. | 328 // Send the new permissions to the renderers. |
| 282 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); | 329 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); |
| 283 !i.IsAtEnd(); i.Advance()) { | 330 !i.IsAtEnd(); i.Advance()) { |
| 284 RenderProcessHost* host = i.GetCurrentValue(); | 331 RenderProcessHost* host = i.GetCurrentValue(); |
| 285 if (profile->IsSameProfile( | 332 if (profile->IsSameProfile( |
| 286 Profile::FromBrowserContext(host->GetBrowserContext()))) { | 333 Profile::FromBrowserContext(host->GetBrowserContext()))) { |
| 287 host->Send(new ExtensionMsg_UpdatePermissions(params)); | 334 host->Send(new ExtensionMsg_UpdatePermissions(params)); |
| 288 } | 335 } |
| 289 } | 336 } |
| 290 | 337 |
| 291 // Trigger the onAdded and onRemoved events in the extension. | 338 // Trigger the onAdded and onRemoved events in the extension. |
| 292 DispatchEvent(extension->id(), histogram_value, event_name, changed); | 339 if (event_name) |
| 340 DispatchEvent(extension->id(), histogram_value, event_name, changed); | |
| 341 } | |
| 342 | |
| 343 // Notify the renderers that extension policy (policy_blocked_hosts) is updated | |
| 344 // and provide new set of hosts. | |
| 345 void PermissionsUpdater::NotifyDefaultPolicyHostRestrictionsUpdated( | |
| 346 const URLPatternSet& default_runtime_blocked_hosts, | |
| 347 const URLPatternSet& default_runtime_allowed_hosts) { | |
| 348 DCHECK((init_flag_ & INIT_FLAG_TRANSIENT) == 0); | |
| 349 | |
| 350 Profile* profile = Profile::FromBrowserContext(browser_context_); | |
| 351 | |
| 352 // Send the new policy to the renderers. | |
| 353 for (RenderProcessHost::iterator host_iterator( | |
| 354 RenderProcessHost::AllHostsIterator()); | |
| 355 !host_iterator.IsAtEnd(); host_iterator.Advance()) { | |
| 356 RenderProcessHost* host = host_iterator.GetCurrentValue(); | |
| 357 if (profile->IsSameProfile( | |
| 358 Profile::FromBrowserContext(host->GetBrowserContext()))) { | |
| 359 ExtensionMsg_UpdateDefaultPolicyHostRestrictions_Params params; | |
| 360 params.default_policy_blocked_hosts = default_runtime_blocked_hosts; | |
| 361 params.default_policy_allowed_hosts = default_runtime_allowed_hosts; | |
| 362 host->Send(new ExtensionMsg_UpdateDefaultPolicyHostRestrictions(params)); | |
| 363 } | |
| 364 } | |
| 293 } | 365 } |
| 294 | 366 |
| 295 } // namespace extensions | 367 } // namespace extensions |
| OLD | NEW |