| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/permissions/permission_manager.h" | 5 #include "chrome/browser/permissions/permission_manager.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 11 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 11 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 12 #include "chrome/browser/permissions/permission_context.h" | 12 #include "chrome/browser/permissions/permission_context.h" |
| 13 #include "chrome/browser/permissions/permission_context_base.h" | 13 #include "chrome/browser/permissions/permission_context_base.h" |
| 14 #include "chrome/browser/permissions/permission_request_id.h" | 14 #include "chrome/browser/permissions/permission_request_id.h" |
| 15 #include "chrome/browser/permissions/permission_uma_util.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/tab_contents/tab_util.h" | 17 #include "chrome/browser/tab_contents/tab_util.h" |
| 17 #include "components/content_settings/core/browser/host_content_settings_map.h" | 18 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 18 #include "content/public/browser/permission_type.h" | 19 #include "content/public/browser/permission_type.h" |
| 19 #include "content/public/browser/render_frame_host.h" | 20 #include "content/public/browser/render_frame_host.h" |
| 20 #include "content/public/browser/render_process_host.h" | 21 #include "content/public/browser/render_process_host.h" |
| 21 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
| 22 | 23 |
| 23 #if !defined(OS_ANDROID) | 24 #if !defined(OS_ANDROID) |
| 24 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" | 25 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 PermissionType permission; | 192 PermissionType permission; |
| 192 GURL requesting_origin; | 193 GURL requesting_origin; |
| 193 GURL embedding_origin; | 194 GURL embedding_origin; |
| 194 base::Callback<void(PermissionStatus)> callback; | 195 base::Callback<void(PermissionStatus)> callback; |
| 195 ContentSetting current_value; | 196 ContentSetting current_value; |
| 196 }; | 197 }; |
| 197 | 198 |
| 198 PermissionManager::PermissionManager(Profile* profile) | 199 PermissionManager::PermissionManager(Profile* profile) |
| 199 : profile_(profile), | 200 : profile_(profile), |
| 200 weak_ptr_factory_(this) { | 201 weak_ptr_factory_(this) { |
| 202 HostContentSettingsMapFactory::GetForProfile(profile_)->AddObserver(this); |
| 201 } | 203 } |
| 202 | 204 |
| 203 PermissionManager::~PermissionManager() { | 205 PermissionManager::~PermissionManager() { |
| 204 if (!subscriptions_.IsEmpty()) | 206 if (!subscriptions_.IsEmpty()) |
| 205 HostContentSettingsMapFactory::GetForProfile(profile_) | 207 HostContentSettingsMapFactory::GetForProfile(profile_) |
| 206 ->RemoveObserver(this); | 208 ->RemoveObserver(this); |
| 207 } | 209 } |
| 208 | 210 |
| 209 int PermissionManager::RequestPermission( | 211 int PermissionManager::RequestPermission( |
| 210 PermissionType permission, | 212 PermissionType permission, |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 requesting_origin, | 346 requesting_origin, |
| 345 embedding_origin, | 347 embedding_origin, |
| 346 PermissionTypeToContentSetting(permission)); | 348 PermissionTypeToContentSetting(permission)); |
| 347 } | 349 } |
| 348 | 350 |
| 349 int PermissionManager::SubscribePermissionStatusChange( | 351 int PermissionManager::SubscribePermissionStatusChange( |
| 350 PermissionType permission, | 352 PermissionType permission, |
| 351 const GURL& requesting_origin, | 353 const GURL& requesting_origin, |
| 352 const GURL& embedding_origin, | 354 const GURL& embedding_origin, |
| 353 const base::Callback<void(PermissionStatus)>& callback) { | 355 const base::Callback<void(PermissionStatus)>& callback) { |
| 354 if (subscriptions_.IsEmpty()) | |
| 355 HostContentSettingsMapFactory::GetForProfile(profile_)->AddObserver(this); | |
| 356 | 356 |
| 357 Subscription* subscription = new Subscription(); | 357 Subscription* subscription = new Subscription(); |
| 358 subscription->permission = permission; | 358 subscription->permission = permission; |
| 359 subscription->requesting_origin = requesting_origin; | 359 subscription->requesting_origin = requesting_origin; |
| 360 subscription->embedding_origin = embedding_origin; | 360 subscription->embedding_origin = embedding_origin; |
| 361 subscription->callback = callback; | 361 subscription->callback = callback; |
| 362 | 362 |
| 363 if (IsConstantPermission(permission)) { | 363 if (IsConstantPermission(permission)) { |
| 364 subscription->current_value = GetContentSettingForConstantPermission( | 364 subscription->current_value = GetContentSettingForConstantPermission( |
| 365 permission); | 365 permission); |
| 366 } else { | 366 } else { |
| 367 subscription->current_value = PermissionContext::Get(profile_, permission) | 367 subscription->current_value = PermissionContext::Get(profile_, permission) |
| 368 ->GetPermissionStatus(subscription->requesting_origin, | 368 ->GetPermissionStatus(subscription->requesting_origin, |
| 369 subscription->embedding_origin); | 369 subscription->embedding_origin); |
| 370 } | 370 } |
| 371 | 371 |
| 372 return subscriptions_.Add(subscription); | 372 return subscriptions_.Add(subscription); |
| 373 } | 373 } |
| 374 | 374 |
| 375 void PermissionManager::UnsubscribePermissionStatusChange(int subscription_id) { | 375 void PermissionManager::UnsubscribePermissionStatusChange(int subscription_id) { |
| 376 // Whether |subscription_id| is known will be checked by the Remove() call. | 376 // Whether |subscription_id| is known will be checked by the Remove() call. |
| 377 subscriptions_.Remove(subscription_id); | 377 subscriptions_.Remove(subscription_id); |
| 378 | |
| 379 if (subscriptions_.IsEmpty()) | |
| 380 HostContentSettingsMapFactory::GetForProfile(profile_) | |
| 381 ->RemoveObserver(this); | |
| 382 } | 378 } |
| 383 | 379 |
| 384 bool PermissionManager::IsPermissionBubbleManagerMissing( | 380 bool PermissionManager::IsPermissionBubbleManagerMissing( |
| 385 content::WebContents* web_contents) { | 381 content::WebContents* web_contents) { |
| 386 // TODO(felt): Remove this method entirely. Leaving it to make a minimal | 382 // TODO(felt): Remove this method entirely. Leaving it to make a minimal |
| 387 // last-minute merge to 46. See crbug.com/457091 and crbug.com/534631. | 383 // last-minute merge to 46. See crbug.com/457091 and crbug.com/534631. |
| 388 return false; | 384 return false; |
| 389 } | 385 } |
| 390 | 386 |
| 391 void PermissionManager::OnContentSettingChanged( | 387 void PermissionManager::OnContentSettingChanged( |
| 392 const ContentSettingsPattern& primary_pattern, | 388 const ContentSettingsPattern& primary_pattern, |
| 393 const ContentSettingsPattern& secondary_pattern, | 389 const ContentSettingsPattern& secondary_pattern, |
| 394 ContentSettingsType content_type, | 390 ContentSettingsType content_type, |
| 391 ContentSetting previous_value, |
| 395 std::string resource_identifier) { | 392 std::string resource_identifier) { |
| 396 std::list<base::Closure> callbacks; | 393 std::list<base::Closure> callbacks; |
| 397 | 394 |
| 395 PermissionUmaUtil::OnContentsettingChanged(profile_, primary_pattern, |
| 396 secondary_pattern, content_type, |
| 397 previous_value); |
| 398 |
| 398 for (SubscriptionsMap::iterator iter(&subscriptions_); | 399 for (SubscriptionsMap::iterator iter(&subscriptions_); |
| 399 !iter.IsAtEnd(); iter.Advance()) { | 400 !iter.IsAtEnd(); iter.Advance()) { |
| 400 Subscription* subscription = iter.GetCurrentValue(); | 401 Subscription* subscription = iter.GetCurrentValue(); |
| 401 if (PermissionTypeToContentSetting(subscription->permission) != | 402 if (PermissionTypeToContentSetting(subscription->permission) != |
| 402 content_type) { | 403 content_type) { |
| 403 continue; | 404 continue; |
| 404 } | 405 } |
| 405 | 406 |
| 406 if (primary_pattern.IsValid() && | 407 if (primary_pattern.IsValid() && |
| 407 !primary_pattern.Matches(subscription->requesting_origin)) | 408 !primary_pattern.Matches(subscription->requesting_origin)) |
| (...skipping 14 matching lines...) Expand all Loading... |
| 422 // Add the callback to |callbacks| which will be run after the loop to | 423 // Add the callback to |callbacks| which will be run after the loop to |
| 423 // prevent re-entrance issues. | 424 // prevent re-entrance issues. |
| 424 callbacks.push_back( | 425 callbacks.push_back( |
| 425 base::Bind(subscription->callback, | 426 base::Bind(subscription->callback, |
| 426 ContentSettingToPermissionStatus(new_value))); | 427 ContentSettingToPermissionStatus(new_value))); |
| 427 } | 428 } |
| 428 | 429 |
| 429 for (const auto& callback : callbacks) | 430 for (const auto& callback : callbacks) |
| 430 callback.Run(); | 431 callback.Run(); |
| 431 } | 432 } |
| OLD | NEW |