| 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 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 // not the user, removed the permissions. This allows the extension to add | 139 // not the user, removed the permissions. This allows the extension to add |
| 140 // them again without prompting the user. | 140 // them again without prompting the user. |
| 141 if (remove_type == REMOVE_HARD) { | 141 if (remove_type == REMOVE_HARD) { |
| 142 ExtensionPrefs::Get(browser_context_) | 142 ExtensionPrefs::Get(browser_context_) |
| 143 ->RemoveGrantedPermissions(extension->id(), to_remove); | 143 ->RemoveGrantedPermissions(extension->id(), to_remove); |
| 144 } | 144 } |
| 145 | 145 |
| 146 NotifyPermissionsUpdated(REMOVED, extension, to_remove); | 146 NotifyPermissionsUpdated(REMOVED, extension, to_remove); |
| 147 } | 147 } |
| 148 | 148 |
| 149 void PermissionsUpdater::SetPolicyHostRestrictions( | |
| 150 const Extension* extension, | |
| 151 const URLPatternSet& runtime_blocked_hosts, | |
| 152 const URLPatternSet& runtime_allowed_hosts) { | |
| 153 extension->permissions_data()->SetPolicyHostRestrictions( | |
| 154 runtime_blocked_hosts, runtime_allowed_hosts); | |
| 155 | |
| 156 // Send notification to the currently running renderers of the runtime block | |
| 157 // hosts settings. | |
| 158 const PermissionSet perms; | |
| 159 NotifyPermissionsUpdated(POLICY, extension, perms); | |
| 160 } | |
| 161 | |
| 162 void PermissionsUpdater::SetUsesDefaultHostRestrictions( | |
| 163 const Extension* extension) { | |
| 164 extension->permissions_data()->SetUsesDefaultHostRestrictions(); | |
| 165 const PermissionSet perms; | |
| 166 NotifyPermissionsUpdated(POLICY, extension, perms); | |
| 167 } | |
| 168 | |
| 169 void PermissionsUpdater::SetDefaultPolicyHostRestrictions( | |
| 170 const URLPatternSet& default_runtime_blocked_hosts, | |
| 171 const URLPatternSet& default_runtime_allowed_hosts) { | |
| 172 PermissionsData::SetDefaultPolicyHostRestrictions( | |
| 173 default_runtime_blocked_hosts, default_runtime_allowed_hosts); | |
| 174 | |
| 175 // Send notification to the currently running renderers of the runtime block | |
| 176 // hosts settings. | |
| 177 NotifyDefaultPolicyHostRestrictionsUpdated(default_runtime_blocked_hosts, | |
| 178 default_runtime_allowed_hosts); | |
| 179 } | |
| 180 | |
| 181 void PermissionsUpdater::RemovePermissionsUnsafe( | 149 void PermissionsUpdater::RemovePermissionsUnsafe( |
| 182 const Extension* extension, | 150 const Extension* extension, |
| 183 const PermissionSet& to_remove) { | 151 const PermissionSet& to_remove) { |
| 184 const PermissionSet& active = | 152 const PermissionSet& active = |
| 185 extension->permissions_data()->active_permissions(); | 153 extension->permissions_data()->active_permissions(); |
| 186 std::unique_ptr<const PermissionSet> total = | 154 std::unique_ptr<const PermissionSet> total = |
| 187 PermissionSet::CreateDifference(active, to_remove); | 155 PermissionSet::CreateDifference(active, to_remove); |
| 188 // |successfully_removed| might not equal |to_remove| if |to_remove| contains | 156 // |successfully_removed| might not equal |to_remove| if |to_remove| contains |
| 189 // permissions the extension didn't have. | 157 // permissions the extension didn't have. |
| 190 std::unique_ptr<const PermissionSet> successfully_removed = | 158 std::unique_ptr<const PermissionSet> successfully_removed = |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 new Event(histogram_value, event_name, std::move(value))); | 250 new Event(histogram_value, event_name, std::move(value))); |
| 283 event->restrict_to_browser_context = browser_context_; | 251 event->restrict_to_browser_context = browser_context_; |
| 284 event_router->DispatchEventToExtension(extension_id, std::move(event)); | 252 event_router->DispatchEventToExtension(extension_id, std::move(event)); |
| 285 } | 253 } |
| 286 | 254 |
| 287 void PermissionsUpdater::NotifyPermissionsUpdated( | 255 void PermissionsUpdater::NotifyPermissionsUpdated( |
| 288 EventType event_type, | 256 EventType event_type, |
| 289 const Extension* extension, | 257 const Extension* extension, |
| 290 const PermissionSet& changed) { | 258 const PermissionSet& changed) { |
| 291 DCHECK_EQ(0, init_flag_ & INIT_FLAG_TRANSIENT); | 259 DCHECK_EQ(0, init_flag_ & INIT_FLAG_TRANSIENT); |
| 292 | 260 if (changed.IsEmpty()) |
| 293 if (changed.IsEmpty() && event_type != POLICY) | |
| 294 return; | 261 return; |
| 295 | 262 |
| 296 UpdatedExtensionPermissionsInfo::Reason reason; | 263 UpdatedExtensionPermissionsInfo::Reason reason; |
| 297 events::HistogramValue histogram_value = events::UNKNOWN; | 264 events::HistogramValue histogram_value; |
| 298 const char* event_name = NULL; | 265 const char* event_name = NULL; |
| 299 Profile* profile = Profile::FromBrowserContext(browser_context_); | |
| 300 | 266 |
| 301 if (event_type == REMOVED) { | 267 if (event_type == REMOVED) { |
| 302 reason = UpdatedExtensionPermissionsInfo::REMOVED; | 268 reason = UpdatedExtensionPermissionsInfo::REMOVED; |
| 303 histogram_value = events::PERMISSIONS_ON_REMOVED; | 269 histogram_value = events::PERMISSIONS_ON_REMOVED; |
| 304 event_name = permissions::OnRemoved::kEventName; | 270 event_name = permissions::OnRemoved::kEventName; |
| 305 } else if (event_type == ADDED) { | 271 } else { |
| 272 CHECK_EQ(ADDED, event_type); |
| 306 reason = UpdatedExtensionPermissionsInfo::ADDED; | 273 reason = UpdatedExtensionPermissionsInfo::ADDED; |
| 307 histogram_value = events::PERMISSIONS_ON_ADDED; | 274 histogram_value = events::PERMISSIONS_ON_ADDED; |
| 308 event_name = permissions::OnAdded::kEventName; | 275 event_name = permissions::OnAdded::kEventName; |
| 309 } else { | |
| 310 DCHECK_EQ(POLICY, event_type); | |
| 311 reason = UpdatedExtensionPermissionsInfo::POLICY; | |
| 312 } | 276 } |
| 313 | 277 |
| 314 // Notify other APIs or interested parties. | 278 // Notify other APIs or interested parties. |
| 315 UpdatedExtensionPermissionsInfo info = | 279 UpdatedExtensionPermissionsInfo info = UpdatedExtensionPermissionsInfo( |
| 316 UpdatedExtensionPermissionsInfo(extension, changed, reason); | 280 extension, changed, reason); |
| 281 Profile* profile = Profile::FromBrowserContext(browser_context_); |
| 317 content::NotificationService::current()->Notify( | 282 content::NotificationService::current()->Notify( |
| 318 extensions::NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED, | 283 extensions::NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED, |
| 319 content::Source<Profile>(profile), | 284 content::Source<Profile>(profile), |
| 320 content::Details<UpdatedExtensionPermissionsInfo>(&info)); | 285 content::Details<UpdatedExtensionPermissionsInfo>(&info)); |
| 321 | 286 |
| 322 ExtensionMsg_UpdatePermissions_Params params; | 287 ExtensionMsg_UpdatePermissions_Params params; |
| 323 params.extension_id = extension->id(); | 288 params.extension_id = extension->id(); |
| 324 params.active_permissions = ExtensionMsg_PermissionSetStruct( | 289 params.active_permissions = ExtensionMsg_PermissionSetStruct( |
| 325 extension->permissions_data()->active_permissions()); | 290 extension->permissions_data()->active_permissions()); |
| 326 params.withheld_permissions = ExtensionMsg_PermissionSetStruct( | 291 params.withheld_permissions = ExtensionMsg_PermissionSetStruct( |
| 327 extension->permissions_data()->withheld_permissions()); | 292 extension->permissions_data()->withheld_permissions()); |
| 328 params.uses_default_policy_host_restrictions = | |
| 329 extension->permissions_data()->UsesDefaultPolicyHostRestrictions(); | |
| 330 if (!params.uses_default_policy_host_restrictions) { | |
| 331 params.policy_blocked_hosts = | |
| 332 extension->permissions_data()->policy_blocked_hosts(); | |
| 333 params.policy_allowed_hosts = | |
| 334 extension->permissions_data()->policy_allowed_hosts(); | |
| 335 } | |
| 336 | 293 |
| 337 // Send the new permissions to the renderers. | 294 // Send the new permissions to the renderers. |
| 338 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); | 295 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); |
| 339 !i.IsAtEnd(); i.Advance()) { | 296 !i.IsAtEnd(); i.Advance()) { |
| 340 RenderProcessHost* host = i.GetCurrentValue(); | 297 RenderProcessHost* host = i.GetCurrentValue(); |
| 341 if (profile->IsSameProfile( | 298 if (profile->IsSameProfile( |
| 342 Profile::FromBrowserContext(host->GetBrowserContext()))) { | 299 Profile::FromBrowserContext(host->GetBrowserContext()))) { |
| 343 host->Send(new ExtensionMsg_UpdatePermissions(params)); | 300 host->Send(new ExtensionMsg_UpdatePermissions(params)); |
| 344 } | 301 } |
| 345 } | 302 } |
| 346 | 303 |
| 347 // Trigger the onAdded and onRemoved events in the extension. We explicitly | 304 // Trigger the onAdded and onRemoved events in the extension. |
| 348 // don't do this for policy-related events. | 305 DispatchEvent(extension->id(), histogram_value, event_name, changed); |
| 349 if (event_name) | |
| 350 DispatchEvent(extension->id(), histogram_value, event_name, changed); | |
| 351 } | |
| 352 | |
| 353 // Notify the renderers that extension policy (policy_blocked_hosts) is updated | |
| 354 // and provide new set of hosts. | |
| 355 void PermissionsUpdater::NotifyDefaultPolicyHostRestrictionsUpdated( | |
| 356 const URLPatternSet& default_runtime_blocked_hosts, | |
| 357 const URLPatternSet& default_runtime_allowed_hosts) { | |
| 358 DCHECK_EQ(0, init_flag_ & INIT_FLAG_TRANSIENT); | |
| 359 | |
| 360 Profile* profile = Profile::FromBrowserContext(browser_context_); | |
| 361 | |
| 362 ExtensionMsg_UpdateDefaultPolicyHostRestrictions_Params params; | |
| 363 params.default_policy_blocked_hosts = default_runtime_blocked_hosts; | |
| 364 params.default_policy_allowed_hosts = default_runtime_allowed_hosts; | |
| 365 | |
| 366 // Send the new policy to the renderers. | |
| 367 for (RenderProcessHost::iterator host_iterator( | |
| 368 RenderProcessHost::AllHostsIterator()); | |
| 369 !host_iterator.IsAtEnd(); host_iterator.Advance()) { | |
| 370 RenderProcessHost* host = host_iterator.GetCurrentValue(); | |
| 371 if (profile->IsSameProfile( | |
| 372 Profile::FromBrowserContext(host->GetBrowserContext()))) { | |
| 373 host->Send(new ExtensionMsg_UpdateDefaultPolicyHostRestrictions(params)); | |
| 374 } | |
| 375 } | |
| 376 } | 306 } |
| 377 | 307 |
| 378 } // namespace extensions | 308 } // namespace extensions |
| OLD | NEW |