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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "build/build_config.h"
#include "components/blocked_content/popup_blocker_tab_helper.h"
#include "ui/base/page_transition_types.h"
#include "ui/base/window_open_disposition.h"
#include "weblayer/browser/browser_impl.h"
#include "weblayer/browser/host_content_settings_map_factory.h"
#include "weblayer/browser/profile_impl.h"
#include "weblayer/browser/tab_impl.h"
#include "weblayer/public/browser.h"
#include "weblayer/public/browser_observer.h"
#include "weblayer/public/navigation_controller.h"
#include "weblayer/public/new_tab_delegate.h"
#include "weblayer/shell/browser/shell.h"
#include "weblayer/test/test_navigation_observer.h"
#include "weblayer/test/weblayer_browser_test.h"
#include "weblayer/test/weblayer_browser_test_utils.h"
namespace weblayer {
class PopupBlockerBrowserTest : public WebLayerBrowserTest,
public NewTabDelegate,
public BrowserObserver {
public:
// WebLayerBrowserTest:
void SetUpOnMainThread() override {
ASSERT_TRUE(embedded_test_server()->Start());
original_tab_ = shell()->tab();
#if !defined(OS_ANDROID)
// Android does this in Java.
original_tab_->SetNewTabDelegate(this);
#endif
shell()->browser()->AddObserver(this);
NavigateAndWaitForCompletion(embedded_test_server()->GetURL("/echo"),
original_tab_);
}
void TearDownOnMainThread() override {
shell()->browser()->RemoveObserver(this);
}
// NewTabDelegate:
void OnNewTab(Tab* new_tab, NewTabType type) override {}
void CloseTab() override {}
// BrowserObserver:
void OnTabAdded(Tab* tab) override {
new_tab_ = tab;
if (new_tab_run_loop_)
new_tab_run_loop_->Quit();
}
void OnTabRemoved(Tab* tab, bool active_tab_changed) override {
ASSERT_EQ(tab, new_tab_);
new_tab_ = nullptr;
if (close_tab_run_loop_)
close_tab_run_loop_->Quit();
}
size_t GetBlockedPopupCount() {
return blocked_content::PopupBlockerTabHelper::FromWebContents(
GetWebContents(original_tab_))
->GetBlockedPopupsCount();
}
content::WebContents* GetWebContents(Tab* tab) {
return static_cast<TabImpl*>(tab)->web_contents();
}
Tab* WaitForNewTab() {
if (!new_tab_) {
new_tab_run_loop_ = std::make_unique<base::RunLoop>();
new_tab_run_loop_->Run();
new_tab_run_loop_ = nullptr;
}
return new_tab_;
}
void WaitForCloseTab() {
if (new_tab_) {
close_tab_run_loop_ = std::make_unique<base::RunLoop>();
close_tab_run_loop_->Run();
close_tab_run_loop_ = nullptr;
}
ASSERT_FALSE(new_tab_);
}
void ExpectTabURL(Tab* tab, const GURL& url) {
if (tab->GetNavigationController()->GetNavigationListSize() > 0) {
EXPECT_EQ(tab->GetNavigationController()->GetNavigationEntryDisplayURL(0),
url);
} else {
TestNavigationObserver(
url, TestNavigationObserver::NavigationEvent::kCompletion, tab)
.Wait();
}
}
Tab* ShowPopup(const GURL& url) {
auto* popup_blocker =
blocked_content::PopupBlockerTabHelper::FromWebContents(
GetWebContents(original_tab_));
popup_blocker->ShowBlockedPopup(
popup_blocker->GetBlockedPopupRequests().begin()->first,
WindowOpenDisposition::NEW_FOREGROUND_TAB);
Tab* new_tab = WaitForNewTab();
ExpectTabURL(new_tab, url);
EXPECT_EQ(GetBlockedPopupCount(), 0u);
return new_tab;
}
Tab* original_tab() { return original_tab_; }
private:
std::unique_ptr<base::RunLoop> new_tab_run_loop_;
std::unique_ptr<base::RunLoop> close_tab_run_loop_;
Tab* original_tab_ = nullptr;
Tab* new_tab_ = nullptr;
};
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, BlocksPopup) {
ExecuteScript(original_tab(), "window.open('https://google.com')", true);
EXPECT_EQ(GetBlockedPopupCount(), 1u);
}
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, BlocksMultiplePopups) {
ExecuteScript(original_tab(), "window.open('https://google.com')", true);
ExecuteScript(original_tab(), "window.open('https://google.com')", true);
EXPECT_EQ(GetBlockedPopupCount(), 2u);
}
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, DoesNotBlockUserGesture) {
GURL popup_url = embedded_test_server()->GetURL("/echo?popup");
ExecuteScriptWithUserGesture(
original_tab(),
base::StringPrintf("window.open('%s')", popup_url.spec().c_str()));
Tab* new_tab = WaitForNewTab();
ExpectTabURL(new_tab, popup_url);
EXPECT_EQ(GetBlockedPopupCount(), 0u);
}
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, OpensBlockedPopup) {
GURL popup_url = embedded_test_server()->GetURL("/echo?popup");
ExecuteScript(
original_tab(),
base::StringPrintf("window.open('%s')", popup_url.spec().c_str()), true);
EXPECT_EQ(GetBlockedPopupCount(), 1u);
Tab* new_tab = ShowPopup(popup_url);
// Blocked popups should no longer have the opener set to match Chrome
// behavior.
EXPECT_FALSE(GetWebContents(new_tab)->HasOpener());
// Make sure we can cleanly close the popup, and there's no crash.
ExecuteScriptWithUserGesture(new_tab, "window.close()");
WaitForCloseTab();
}
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
AllowsPopupThroughContentSettingException) {
GURL popup_url = embedded_test_server()->GetURL("/echo?popup");
HostContentSettingsMapFactory::GetForBrowserContext(
GetWebContents(original_tab())->GetBrowserContext())
->SetContentSettingDefaultScope(popup_url, GURL(),
ContentSettingsType::POPUPS,
std::string(), CONTENT_SETTING_ALLOW);
ExecuteScript(
original_tab(),
base::StringPrintf("window.open('%s')", popup_url.spec().c_str()), true);
Tab* new_tab = WaitForNewTab();
ExpectTabURL(new_tab, popup_url);
EXPECT_EQ(GetBlockedPopupCount(), 0u);
}
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
AllowsPopupThroughContentSettingDefaultValue) {
GURL popup_url = embedded_test_server()->GetURL("/echo?popup");
HostContentSettingsMapFactory::GetForBrowserContext(
GetWebContents(original_tab())->GetBrowserContext())
->SetDefaultContentSetting(ContentSettingsType::POPUPS,
CONTENT_SETTING_ALLOW);
ExecuteScript(
original_tab(),
base::StringPrintf("window.open('%s')", popup_url.spec().c_str()), true);
Tab* new_tab = WaitForNewTab();
ExpectTabURL(new_tab, popup_url);
EXPECT_EQ(GetBlockedPopupCount(), 0u);
}
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
BlockPopupThroughContentSettingException) {
GURL popup_url = embedded_test_server()->GetURL("/echo?popup");
HostContentSettingsMapFactory::GetForBrowserContext(
GetWebContents(original_tab())->GetBrowserContext())
->SetDefaultContentSetting(ContentSettingsType::POPUPS,
CONTENT_SETTING_ALLOW);
HostContentSettingsMapFactory::GetForBrowserContext(
GetWebContents(original_tab())->GetBrowserContext())
->SetContentSettingDefaultScope(popup_url, GURL(),
ContentSettingsType::POPUPS,
std::string(), CONTENT_SETTING_BLOCK);
ExecuteScript(
original_tab(),
base::StringPrintf("window.open('%s')", popup_url.spec().c_str()), true);
EXPECT_EQ(GetBlockedPopupCount(), 1u);
}
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
BlocksAndOpensPopupFromOpenURL) {
GURL popup_url = embedded_test_server()->GetURL("/echo?popup");
content::OpenURLParams params(popup_url, content::Referrer(),
WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui::PAGE_TRANSITION_LINK, true);
params.initiator_origin = url::Origin::Create(popup_url);
GetWebContents(original_tab())->OpenURL(params);
EXPECT_EQ(GetBlockedPopupCount(), 1u);
ShowPopup(popup_url);
}
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
DoesNotOpenPopupWithoutNewTabDelegate) {
NewTabDelegate* old_delegate =
static_cast<TabImpl*>(original_tab())->new_tab_delegate();
original_tab()->SetNewTabDelegate(nullptr);
GURL popup_url = embedded_test_server()->GetURL("/echo?popup");
ExecuteScriptWithUserGesture(
original_tab(),
base::StringPrintf("window.open('%s')", popup_url.spec().c_str()));
EXPECT_EQ(GetBlockedPopupCount(), 0u);
// Navigate the original tab, then make sure we still only have a single tab.
NavigateAndWaitForCompletion(embedded_test_server()->GetURL("/echo"),
original_tab());
EXPECT_EQ(shell()->browser()->GetTabs().size(), 1u);
// Restore the old delegate to make sure it is cleaned up on Android.
original_tab()->SetNewTabDelegate(old_delegate);
}
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
DoesNotOpenBlockedPopupWithoutNewTabDelegate) {
NewTabDelegate* old_delegate =
static_cast<TabImpl*>(original_tab())->new_tab_delegate();
original_tab()->SetNewTabDelegate(nullptr);
GURL popup_url = embedded_test_server()->GetURL("/echo?popup");
ExecuteScript(
original_tab(),
base::StringPrintf("window.open('%s')", popup_url.spec().c_str()), true);
EXPECT_EQ(GetBlockedPopupCount(), 0u);
// Navigate the original tab, then make sure we still only have a single tab.
NavigateAndWaitForCompletion(embedded_test_server()->GetURL("/echo"),
original_tab());
EXPECT_EQ(shell()->browser()->GetTabs().size(), 1u);
// Restore the old delegate to make sure it is cleaned up on Android.
original_tab()->SetNewTabDelegate(old_delegate);
}
} // namespace weblayer
|