popup_blocker.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/blocked_content/popup_blocker.h"
  5. #include <string>
  6. #include "base/check.h"
  7. #include "base/command_line.h"
  8. #include "components/blocked_content/popup_blocker_tab_helper.h"
  9. #include "components/blocked_content/popup_navigation_delegate.h"
  10. #include "components/blocked_content/safe_browsing_triggered_popup_blocker.h"
  11. #include "components/content_settings/core/browser/host_content_settings_map.h"
  12. #include "components/content_settings/core/common/content_settings.h"
  13. #include "components/embedder_support/switches.h"
  14. #include "content/public/browser/page_navigator.h"
  15. #include "content/public/browser/render_frame_host.h"
  16. #include "content/public/browser/web_contents.h"
  17. #include "third_party/blink/public/mojom/frame/frame.mojom-shared.h"
  18. namespace blocked_content {
  19. namespace {
  20. content::Page& GetSourcePageForPopup(
  21. const content::OpenURLParams* open_url_params,
  22. content::WebContents* web_contents) {
  23. if (open_url_params) {
  24. content::RenderFrameHost* source = content::RenderFrameHost::FromID(
  25. open_url_params->source_render_process_id,
  26. open_url_params->source_render_frame_id);
  27. if (source)
  28. return source->GetPage();
  29. }
  30. // When there's no source RenderFrameHost, attribute the popup to the primary
  31. // page.
  32. return web_contents->GetPrimaryPage();
  33. }
  34. // If the popup should be blocked, returns the reason why it was blocked.
  35. // Otherwise returns kNotBlocked.
  36. PopupBlockType ShouldBlockPopup(content::WebContents* web_contents,
  37. const GURL* opener_url,
  38. bool user_gesture,
  39. const content::OpenURLParams* open_url_params,
  40. HostContentSettingsMap* settings_map) {
  41. if (base::CommandLine::ForCurrentProcess()->HasSwitch(
  42. embedder_support::kDisablePopupBlocking)) {
  43. return PopupBlockType::kNotBlocked;
  44. }
  45. // If an explicit opener is not given, use the current committed load in this
  46. // web contents. This is because A page can't spawn popups (or do anything
  47. // else, either) until its load commits, so when we reach here, the popup was
  48. // spawned by the NavigationController's last committed entry, not the active
  49. // entry. For example, if a page opens a popup in an onunload() handler, then
  50. // the active entry is the page to be loaded as we navigate away from the
  51. // unloading page.
  52. const GURL& url =
  53. opener_url ? *opener_url : web_contents->GetLastCommittedURL();
  54. ContentSetting cs;
  55. if (url.is_valid()) {
  56. cs = settings_map->GetContentSetting(url, url, ContentSettingsType::POPUPS);
  57. } else {
  58. cs = settings_map->GetDefaultContentSetting(ContentSettingsType::POPUPS,
  59. nullptr);
  60. }
  61. if (cs == CONTENT_SETTING_ALLOW)
  62. return PopupBlockType::kNotBlocked;
  63. if (!user_gesture)
  64. return PopupBlockType::kNoGesture;
  65. // This is trusted user action (e.g. shift-click), so make sure it is not
  66. // blocked.
  67. if (open_url_params &&
  68. open_url_params->triggering_event_info !=
  69. blink::mojom::TriggeringEventInfo::kFromUntrustedEvent) {
  70. return PopupBlockType::kNotBlocked;
  71. }
  72. auto* safe_browsing_blocker =
  73. SafeBrowsingTriggeredPopupBlocker::FromWebContents(web_contents);
  74. if (safe_browsing_blocker &&
  75. safe_browsing_blocker->ShouldApplyAbusivePopupBlocker(
  76. GetSourcePageForPopup(open_url_params, web_contents))) {
  77. return PopupBlockType::kAbusive;
  78. }
  79. return PopupBlockType::kNotBlocked;
  80. }
  81. } // namespace
  82. bool ConsiderForPopupBlocking(WindowOpenDisposition disposition) {
  83. return disposition == WindowOpenDisposition::NEW_POPUP ||
  84. disposition == WindowOpenDisposition::NEW_FOREGROUND_TAB ||
  85. disposition == WindowOpenDisposition::NEW_BACKGROUND_TAB ||
  86. disposition == WindowOpenDisposition::NEW_WINDOW;
  87. }
  88. std::unique_ptr<PopupNavigationDelegate> MaybeBlockPopup(
  89. content::WebContents* web_contents,
  90. const GURL* opener_url,
  91. std::unique_ptr<PopupNavigationDelegate> delegate,
  92. const content::OpenURLParams* open_url_params,
  93. const blink::mojom::WindowFeatures& window_features,
  94. HostContentSettingsMap* settings_map) {
  95. DCHECK(web_contents);
  96. DCHECK(!open_url_params ||
  97. open_url_params->user_gesture == delegate->GetOriginalUserGesture());
  98. PopupBlockerTabHelper::LogAction(PopupBlockerTabHelper::Action::kInitiated);
  99. // Check |popup_blocker| first since it is cheaper than ShouldBlockPopup().
  100. auto* popup_blocker = PopupBlockerTabHelper::FromWebContents(web_contents);
  101. if (!popup_blocker)
  102. return delegate;
  103. PopupBlockType block_type = ShouldBlockPopup(
  104. web_contents, opener_url, delegate->GetOriginalUserGesture(),
  105. open_url_params, settings_map);
  106. if (block_type == PopupBlockType::kNotBlocked)
  107. return delegate;
  108. popup_blocker->AddBlockedPopup(std::move(delegate), window_features,
  109. block_type);
  110. return nullptr;
  111. }
  112. } // namespace blocked_content