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 bool is_default) { | |
| 144 // Keep track of runtime blocked and hosts for this extension in the browser | |
| 145 // process. We'll pull from here to populate when a new renderer is created. | |
| 146 extension->permissions_data()->SetPolicyHostRestrictions( | |
| 147 std::move(runtime_blocked_hosts), std::move(runtime_allowed_hosts), | |
|
Devlin
2017/01/26 22:47:39
std::move() casts to a &&, and const && are pretty
nrpeter
2017/02/03 19:32:24
Done.
| |
| 148 is_default); | |
| 149 | |
| 150 // Send notification to the currently running renderers of the runtime block | |
| 151 // hosts settings. | |
| 152 const PermissionSet perms; | |
| 153 NotifyPermissionsUpdated(POLICY, extension, perms); | |
| 154 } | |
| 155 | |
| 156 void PermissionsUpdater::SetDefaultPolicyHostRestrictions( | |
| 157 const URLPatternSet& default_runtime_blocked_hosts, | |
| 158 const URLPatternSet& default_runtime_allowed_hosts) { | |
| 159 // Keep track of runtime blocked and hosts for this extension in the browser | |
| 160 // process. We'll pull from here to populate when a new renderer is created. | |
| 161 PermissionsData::SetDefaultPolicyHostRestrictions( | |
| 162 std::move(default_runtime_blocked_hosts), | |
| 163 std::move(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 Loading... | |
| 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()) | |
| 248 return; | |
| 249 | 279 |
| 250 UpdatedExtensionPermissionsInfo::Reason reason; | 280 UpdatedExtensionPermissionsInfo::Reason reason; |
| 251 events::HistogramValue histogram_value; | 281 events::HistogramValue histogram_value; |
| 252 const char* event_name = NULL; | 282 const char* event_name = NULL; |
| 283 Profile* profile = Profile::FromBrowserContext(browser_context_); | |
| 253 | 284 |
| 254 if (event_type == REMOVED) { | 285 if (changed.IsEmpty() && event_type != POLICY) |
| 255 reason = UpdatedExtensionPermissionsInfo::REMOVED; | 286 return; |
| 256 histogram_value = events::PERMISSIONS_ON_REMOVED; | 287 |
| 257 event_name = permissions::OnRemoved::kEventName; | 288 // Policy isn't exposed via JS API. |
| 258 } else { | 289 if (event_type != POLICY) { |
| 259 CHECK_EQ(ADDED, event_type); | 290 if (event_type == REMOVED) { |
| 260 reason = UpdatedExtensionPermissionsInfo::ADDED; | 291 reason = UpdatedExtensionPermissionsInfo::REMOVED; |
| 261 histogram_value = events::PERMISSIONS_ON_ADDED; | 292 histogram_value = events::PERMISSIONS_ON_REMOVED; |
| 262 event_name = permissions::OnAdded::kEventName; | 293 event_name = permissions::OnRemoved::kEventName; |
| 294 } else { | |
| 295 CHECK_EQ(ADDED, event_type); | |
| 296 reason = UpdatedExtensionPermissionsInfo::ADDED; | |
| 297 histogram_value = events::PERMISSIONS_ON_ADDED; | |
| 298 event_name = permissions::OnAdded::kEventName; | |
| 299 } | |
| 300 | |
| 301 // Notify other APIs or interested parties. | |
| 302 UpdatedExtensionPermissionsInfo info = UpdatedExtensionPermissionsInfo( | |
| 303 extension, changed, reason); | |
| 304 content::NotificationService::current()->Notify( | |
| 305 extensions::NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED, | |
| 306 content::Source<Profile>(profile), | |
| 307 content::Details<UpdatedExtensionPermissionsInfo>(&info)); | |
| 263 } | 308 } |
| 264 | 309 |
| 265 // Notify other APIs or interested parties. | |
| 266 UpdatedExtensionPermissionsInfo info = UpdatedExtensionPermissionsInfo( | |
| 267 extension, changed, reason); | |
| 268 Profile* profile = Profile::FromBrowserContext(browser_context_); | |
| 269 content::NotificationService::current()->Notify( | |
| 270 extensions::NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED, | |
| 271 content::Source<Profile>(profile), | |
| 272 content::Details<UpdatedExtensionPermissionsInfo>(&info)); | |
| 273 | |
| 274 ExtensionMsg_UpdatePermissions_Params params; | 310 ExtensionMsg_UpdatePermissions_Params params; |
| 275 params.extension_id = extension->id(); | 311 params.extension_id = extension->id(); |
| 276 params.active_permissions = ExtensionMsg_PermissionSetStruct( | 312 params.active_permissions = ExtensionMsg_PermissionSetStruct( |
| 277 extension->permissions_data()->active_permissions()); | 313 extension->permissions_data()->active_permissions()); |
| 278 params.withheld_permissions = ExtensionMsg_PermissionSetStruct( | 314 params.withheld_permissions = ExtensionMsg_PermissionSetStruct( |
| 279 extension->permissions_data()->withheld_permissions()); | 315 extension->permissions_data()->withheld_permissions()); |
| 316 params.uses_default_policy_host_restrictions = | |
| 317 extension->permissions_data()->UsesDefaultPolicyHostRestrictions(); | |
| 318 if (!params.uses_default_policy_host_restrictions) { | |
| 319 params.runtime_blocked_hosts = | |
| 320 extension->permissions_data()->runtime_blocked_hosts(); | |
| 321 params.runtime_allowed_hosts = | |
| 322 extension->permissions_data()->runtime_allowed_hosts(); | |
| 323 } | |
| 280 | 324 |
| 281 // Send the new permissions to the renderers. | 325 // Send the new permissions to the renderers. |
| 282 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); | 326 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); |
| 283 !i.IsAtEnd(); i.Advance()) { | 327 !i.IsAtEnd(); i.Advance()) { |
| 284 RenderProcessHost* host = i.GetCurrentValue(); | 328 RenderProcessHost* host = i.GetCurrentValue(); |
| 285 if (profile->IsSameProfile( | 329 if (profile->IsSameProfile( |
| 286 Profile::FromBrowserContext(host->GetBrowserContext()))) { | 330 Profile::FromBrowserContext(host->GetBrowserContext()))) { |
| 287 host->Send(new ExtensionMsg_UpdatePermissions(params)); | 331 host->Send(new ExtensionMsg_UpdatePermissions(params)); |
| 288 } | 332 } |
| 289 } | 333 } |
| 290 | 334 |
| 291 // Trigger the onAdded and onRemoved events in the extension. | 335 // Trigger the onAdded and onRemoved events in the extension. |
| 292 DispatchEvent(extension->id(), histogram_value, event_name, changed); | 336 if (event_name != NULL) |
| 337 DispatchEvent(extension->id(), histogram_value, event_name, changed); | |
| 338 } | |
| 339 | |
| 340 // Notify the renderers that extension policy (runtime_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 // Send the new policy to the renderers. | |
| 350 for (RenderProcessHost::iterator host_iterator( | |
| 351 RenderProcessHost::AllHostsIterator()); !host_iterator.IsAtEnd(); | |
| 352 host_iterator.Advance()) { | |
| 353 RenderProcessHost* host = host_iterator.GetCurrentValue(); | |
| 354 if (profile->IsSameProfile( | |
| 355 Profile::FromBrowserContext(host->GetBrowserContext()))) { | |
| 356 ExtensionMsg_UpdateDefaultPolicyHostRestrictions_Params params; | |
| 357 params.default_runtime_blocked_hosts = default_runtime_blocked_hosts; | |
| 358 params.default_runtime_allowed_hosts = default_runtime_allowed_hosts; | |
| 359 host->Send(new ExtensionMsg_UpdateDefaultPolicyHostRestrictions(params)); | |
| 360 } | |
| 361 } | |
| 293 } | 362 } |
| 294 | 363 |
| 295 } // namespace extensions | 364 } // namespace extensions |
| OLD | NEW |