1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/blocked_content/popup_blocker_tab_helper.h"
#include <iterator>
#include <string>
#include "base/metrics/histogram_macros.h"
#include "build/build_config.h"
#include "components/back_forward_cache/back_forward_cache_disable.h"
#include "components/blocked_content/list_item_position.h"
#include "components/blocked_content/popup_navigation_delegate.h"
#include "components/blocked_content/popup_tracker.h"
#include "components/blocked_content/safe_browsing_triggered_popup_blocker.h"
#include "components/content_settings/browser/page_specific_content_settings.h"
#include "content/public/browser/back_forward_cache.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/page_navigator.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "third_party/blink/public/mojom/window_features/window_features.mojom.h"
namespace blocked_content {
const size_t kMaximumNumberOfPopups = 25;
struct PopupBlockerTabHelper::BlockedRequest {
BlockedRequest(std::unique_ptr<PopupNavigationDelegate> delegate,
const blink::mojom::WindowFeatures& window_features,
PopupBlockType block_type)
: delegate(std::move(delegate)),
window_features(window_features),
block_type(block_type) {}
std::unique_ptr<PopupNavigationDelegate> delegate;
blink::mojom::WindowFeatures window_features;
PopupBlockType block_type;
};
PopupBlockerTabHelper::PopupBlockerTabHelper(content::WebContents* web_contents)
: content::WebContentsObserver(web_contents),
content::WebContentsUserData<PopupBlockerTabHelper>(*web_contents) {
blocked_content::SafeBrowsingTriggeredPopupBlocker::MaybeCreate(web_contents);
}
PopupBlockerTabHelper::~PopupBlockerTabHelper() = default;
void PopupBlockerTabHelper::DidFinishNavigation(
content::NavigationHandle* navigation_handle) {
// Clear all page actions, blocked content notifications and browser actions
// for this tab, unless this is an same-document navigation. Also only
// consider main frame navigations that successfully committed.
if (!navigation_handle->IsInPrimaryMainFrame() ||
!navigation_handle->HasCommitted() ||
navigation_handle->IsSameDocument()) {
return;
}
// Close blocked popups.
if (!blocked_popups_.empty()) {
blocked_popups_.clear();
HidePopupNotification();
// With back-forward cache we can restore the page, but |blocked_popups_|
// are lost here and can't be restored at the moment.
// Disable bfcache here to avoid potential loss of the page state.
content::BackForwardCache::DisableForRenderFrameHost(
navigation_handle->GetPreviousRenderFrameHostId(),
back_forward_cache::DisabledReason(
back_forward_cache::DisabledReasonId::kPopupBlockerTabHelper));
}
}
void PopupBlockerTabHelper::HidePopupNotification() {
auto* pscs = content_settings::PageSpecificContentSettings::GetForFrame(
web_contents()->GetPrimaryMainFrame());
if (pscs)
pscs->ClearPopupsBlocked();
}
void PopupBlockerTabHelper::AddBlockedPopup(
std::unique_ptr<PopupNavigationDelegate> delegate,
const blink::mojom::WindowFeatures& window_features,
PopupBlockType block_type) {
LogAction(Action::kBlocked);
if (blocked_popups_.size() >= kMaximumNumberOfPopups)
return;
int id = next_id_;
next_id_++;
blocked_popups_[id] = std::make_unique<BlockedRequest>(
std::move(delegate), window_features, block_type);
auto* content_settings =
content_settings::PageSpecificContentSettings::GetForFrame(
web_contents()->GetPrimaryMainFrame());
if (content_settings) {
content_settings->OnContentBlocked(ContentSettingsType::POPUPS);
}
auto* raw_delegate = blocked_popups_[id]->delegate.get();
manager_.NotifyObservers(id, raw_delegate->GetURL());
raw_delegate->OnPopupBlocked(web_contents(), GetBlockedPopupsCount());
}
void PopupBlockerTabHelper::ShowBlockedPopup(
int32_t id,
WindowOpenDisposition disposition) {
auto it = blocked_popups_.find(id);
if (it == blocked_popups_.end())
return;
BlockedRequest* popup = it->second.get();
std::optional<WindowOpenDisposition> updated_disposition;
if (disposition != WindowOpenDisposition::CURRENT_TAB)
updated_disposition = disposition;
PopupNavigationDelegate::NavigateResult result =
popup->delegate->NavigateWithGesture(popup->window_features,
updated_disposition);
if (result.navigated_or_inserted_contents) {
auto* tracker = blocked_content::PopupTracker::CreateForWebContents(
result.navigated_or_inserted_contents, web_contents(),
result.disposition);
tracker->set_is_trusted(true);
}
switch (popup->block_type) {
case PopupBlockType::kNotBlocked:
NOTREACHED();
case PopupBlockType::kNoGesture:
LogAction(Action::kClickedThroughNoGesture);
break;
case PopupBlockType::kAbusive:
LogAction(Action::kClickedThroughAbusive);
break;
}
blocked_popups_.erase(id);
if (blocked_popups_.empty())
HidePopupNotification();
}
void PopupBlockerTabHelper::ShowAllBlockedPopups() {
PopupIdMap blocked_popups = GetBlockedPopupRequests();
for (const auto& elem : blocked_popups) {
ShowBlockedPopup(elem.first, WindowOpenDisposition::CURRENT_TAB);
}
}
size_t PopupBlockerTabHelper::GetBlockedPopupsCount() const {
return blocked_popups_.size();
}
PopupBlockerTabHelper::PopupIdMap
PopupBlockerTabHelper::GetBlockedPopupRequests() {
PopupIdMap result;
for (const auto& it : blocked_popups_) {
result[it.first] = it.second->delegate->GetURL();
}
return result;
}
// static
void PopupBlockerTabHelper::LogAction(Action action) {
UMA_HISTOGRAM_ENUMERATION("ContentSettings.Popups.BlockerActions", action);
}
WEB_CONTENTS_USER_DATA_KEY_IMPL(PopupBlockerTabHelper);
} // namespace blocked_content
|