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