blob: e300d12c9ff0d6adbd781f16afd84ef073f4cfa9 [file] [log] [blame]
[email protected]3d831992013-07-04 01:13:291// Copyright 2013 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
5#include "chrome/browser/chrome_content_browser_client.h"
6
msramek1f4746d2016-08-19 21:37:077#include <list>
bmcquade5d2d9cf32015-06-19 17:42:288#include <map>
msramek1f4746d2016-08-19 21:37:079#include <memory>
bmcquade5d2d9cf32015-06-19 17:42:2810
msramek1f4746d2016-08-19 21:37:0711#include "base/bind.h"
[email protected]30335fdf02014-02-26 19:51:2712#include "base/command_line.h"
avie4d7b6f2015-12-26 00:59:1813#include "base/macros.h"
msramek1f4746d2016-08-19 21:37:0714#include "base/memory/ptr_util.h"
15#include "base/message_loop/message_loop.h"
[email protected]10a9bf92013-11-13 23:34:4816#include "base/metrics/field_trial.h"
msramek22328112017-03-23 16:50:2917#include "base/run_loop.h"
msramek1f4746d2016-08-19 21:37:0718#include "base/strings/stringprintf.h"
mpearson3c6d7af2015-05-13 23:59:5319#include "base/strings/utf_string_conversions.h"
avie4d7b6f2015-12-26 00:59:1820#include "build/build_config.h"
msramek1f4746d2016-08-19 21:37:0721#include "chrome/browser/browsing_data/browsing_data_helper.h"
msramek1c2b3ca2017-03-14 17:57:5622#include "chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h"
msramek22328112017-03-23 16:50:2923#include "chrome/browser/browsing_data/mock_browsing_data_remover_delegate.h"
[email protected]10a9bf92013-11-13 23:34:4824#include "chrome/browser/search_engines/template_url_service_factory.h"
msramek1f4746d2016-08-19 21:37:0725#include "chrome/test/base/testing_profile.h"
timvolodinea47291a2015-01-23 14:04:4326#include "components/content_settings/core/browser/host_content_settings_map.h"
[email protected]bf5c532d2014-07-05 00:29:5327#include "components/search_engines/template_url_service.h"
[email protected]10a9bf92013-11-13 23:34:4828#include "components/variations/entropy_provider.h"
bmcquade5d2d9cf32015-06-19 17:42:2829#include "components/variations/variations_associated_data.h"
sdefresne9fb67692015-08-03 18:48:2230#include "components/version_info/version_info.h"
msrameka3c7cfd2017-02-03 13:34:1331#include "content/public/browser/browsing_data_filter_builder.h"
msrameke169ccb2017-04-26 05:21:4132#include "content/public/browser/browsing_data_remover.h"
[email protected]10a9bf92013-11-13 23:34:4833#include "content/public/browser/navigation_controller.h"
34#include "content/public/browser/navigation_entry.h"
msramek22328112017-03-23 16:50:2935#include "content/public/browser/storage_partition.h"
[email protected]10a9bf92013-11-13 23:34:4836#include "content/public/browser/web_contents.h"
[email protected]30335fdf02014-02-26 19:51:2737#include "content/public/common/content_switches.h"
bmcquade5d2d9cf32015-06-19 17:42:2838#include "content/public/test/test_browser_thread_bundle.h"
Brett Wilson0748bf412016-11-22 17:55:4639#include "media/media_features.h"
msramek1f4746d2016-08-19 21:37:0740#include "testing/gmock/include/gmock/gmock.h"
[email protected]3d831992013-07-04 01:13:2941#include "testing/gtest/include/gtest/gtest.h"
[email protected]5f5ef802013-07-04 16:11:1342#include "url/gurl.h"
[email protected]3d831992013-07-04 01:13:2943
jam1c5a91492016-02-24 20:47:5344#if !defined(OS_ANDROID)
thestigf80564462015-09-29 23:12:0845#include "chrome/browser/ui/browser.h"
46#include "chrome/browser/ui/tabs/tab_strip_model.h"
bengre8a146f2016-03-10 01:20:2247#include "chrome/common/pref_names.h"
thestigf80564462015-09-29 23:12:0848#include "chrome/test/base/browser_with_test_window_test.h"
49#include "chrome/test/base/search_test_utils.h"
50#endif
51
msrameka3c7cfd2017-02-03 13:34:1352using content::BrowsingDataFilterBuilder;
msramek1f4746d2016-08-19 21:37:0753using testing::_;
mlamourieb40d552015-02-05 00:57:0854using ChromeContentBrowserClientTest = testing::Test;
[email protected]3d831992013-07-04 01:13:2955
[email protected]3d831992013-07-04 01:13:2956TEST_F(ChromeContentBrowserClientTest, ShouldAssignSiteForURL) {
57 ChromeContentBrowserClient client;
58 EXPECT_FALSE(client.ShouldAssignSiteForURL(GURL("chrome-native://test")));
59 EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("http://www.google.com")));
60 EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("https://www.google.com")));
61}
62
jam1c5a91492016-02-24 20:47:5363// BrowserWithTestWindowTest doesn't work on Android.
64#if !defined(OS_ANDROID)
mlamourieb40d552015-02-05 00:57:0865
peterbbcccc12015-02-11 22:23:3366using ChromeContentBrowserClientWindowTest = BrowserWithTestWindowTest;
67
68static void DidOpenURLForWindowTest(content::WebContents** target_contents,
69 content::WebContents* opened_contents) {
70 DCHECK(target_contents);
71
72 *target_contents = opened_contents;
73}
74
bengre8a146f2016-03-10 01:20:2275TEST_F(ChromeContentBrowserClientWindowTest, IsDataSaverEnabled) {
76 ChromeContentBrowserClient client;
77 content::BrowserContext* context = browser()->profile();
78 EXPECT_FALSE(client.IsDataSaverEnabled(context));
79 browser()->profile()->GetPrefs()->SetBoolean(prefs::kDataSaverEnabled, true);
80 EXPECT_TRUE(client.IsDataSaverEnabled(context));
81}
82
mlamourieb40d552015-02-05 00:57:0883// This test opens two URLs using ContentBrowserClient::OpenURL. It expects the
84// URLs to be opened in new tabs and activated, changing the active tabs after
85// each call and increasing the tab count by 2.
86TEST_F(ChromeContentBrowserClientWindowTest, OpenURL) {
87 ChromeContentBrowserClient client;
88
89 int previous_count = browser()->tab_strip_model()->count();
90
91 GURL urls[] = { GURL("https://www.google.com"),
92 GURL("https://www.chromium.org") };
93
94 for (const GURL& url : urls) {
nick3b04f322016-08-31 19:29:1995 content::OpenURLParams params(url, content::Referrer(),
96 WindowOpenDisposition::NEW_FOREGROUND_TAB,
97 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, false);
peterbbcccc12015-02-11 22:23:3398 // TODO(peter): We should have more in-depth browser tests for the window
99 // opening functionality, which also covers Android. This test can currently
100 // only be ran on platforms where OpenURL is implemented synchronously.
101 // See https://crbug.com/457667.
102 content::WebContents* web_contents = nullptr;
103 client.OpenURL(browser()->profile(),
104 params,
105 base::Bind(&DidOpenURLForWindowTest, &web_contents));
106
107 EXPECT_TRUE(web_contents);
mlamourieb40d552015-02-05 00:57:08108
109 content::WebContents* active_contents = browser()->tab_strip_model()->
110 GetActiveWebContents();
peterbbcccc12015-02-11 22:23:33111 EXPECT_EQ(web_contents, active_contents);
mlamourieb40d552015-02-05 00:57:08112 EXPECT_EQ(url, active_contents->GetVisibleURL());
113 }
114
115 EXPECT_EQ(previous_count + 2, browser()->tab_strip_model()->count());
116}
117
jam1c5a91492016-02-24 20:47:53118#endif // !defined(OS_ANDROID)
mlamourieb40d552015-02-05 00:57:08119
Brett Wilson0748bf412016-11-22 17:55:46120#if BUILDFLAG(ENABLE_WEBRTC)
brettwa68ea2b2015-02-01 02:54:07121
[email protected]30335fdf02014-02-26 19:51:27122// NOTE: Any updates to the expectations in these tests should also be done in
123// the browser test WebRtcDisableEncryptionFlagBrowserTest.
124class DisableWebRtcEncryptionFlagTest : public testing::Test {
125 public:
126 DisableWebRtcEncryptionFlagTest()
avi3ef9ec9e2014-12-22 22:50:17127 : from_command_line_(base::CommandLine::NO_PROGRAM),
128 to_command_line_(base::CommandLine::NO_PROGRAM) {}
[email protected]30335fdf02014-02-26 19:51:27129
130 protected:
dchenge1bc7982014-10-30 00:32:40131 void SetUp() override {
[email protected]30335fdf02014-02-26 19:51:27132 from_command_line_.AppendSwitch(switches::kDisableWebRtcEncryption);
133 }
134
sdefresne6e883e42015-07-30 08:05:54135 void MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel channel) {
[email protected]30335fdf02014-02-26 19:51:27136 ChromeContentBrowserClient::MaybeCopyDisableWebRtcEncryptionSwitch(
137 &to_command_line_,
138 from_command_line_,
139 channel);
140 }
141
avi3ef9ec9e2014-12-22 22:50:17142 base::CommandLine from_command_line_;
143 base::CommandLine to_command_line_;
[email protected]30335fdf02014-02-26 19:51:27144
thestigf80564462015-09-29 23:12:08145 private:
[email protected]30335fdf02014-02-26 19:51:27146 DISALLOW_COPY_AND_ASSIGN(DisableWebRtcEncryptionFlagTest);
147};
148
149TEST_F(DisableWebRtcEncryptionFlagTest, UnknownChannel) {
sdefresne6e883e42015-07-30 08:05:54150 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::UNKNOWN);
[email protected]30335fdf02014-02-26 19:51:27151 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
152}
153
154TEST_F(DisableWebRtcEncryptionFlagTest, CanaryChannel) {
sdefresne6e883e42015-07-30 08:05:54155 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::CANARY);
[email protected]30335fdf02014-02-26 19:51:27156 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
157}
158
159TEST_F(DisableWebRtcEncryptionFlagTest, DevChannel) {
sdefresne6e883e42015-07-30 08:05:54160 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::DEV);
[email protected]30335fdf02014-02-26 19:51:27161 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
162}
163
164TEST_F(DisableWebRtcEncryptionFlagTest, BetaChannel) {
sdefresne6e883e42015-07-30 08:05:54165 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::BETA);
[email protected]30335fdf02014-02-26 19:51:27166#if defined(OS_ANDROID)
167 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
168#else
169 EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
170#endif
171}
172
173TEST_F(DisableWebRtcEncryptionFlagTest, StableChannel) {
sdefresne6e883e42015-07-30 08:05:54174 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::STABLE);
[email protected]30335fdf02014-02-26 19:51:27175 EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
176}
177
brettwa68ea2b2015-02-01 02:54:07178#endif // ENABLE_WEBRTC
179
bmcquade5d2d9cf32015-06-19 17:42:28180class BlinkSettingsFieldTrialTest : public testing::Test {
181 public:
bmcquade9dd54cc2015-06-22 16:56:52182 static const char kParserFieldTrialName[];
183 static const char kIFrameFieldTrialName[];
pmeenan9ac669682015-08-17 14:57:03184 static const char kFakeGroupName[];
185 static const char kDefaultGroupName[];
bmcquade9dd54cc2015-06-22 16:56:52186
bmcquade5d2d9cf32015-06-19 17:42:28187 BlinkSettingsFieldTrialTest()
188 : trial_list_(NULL),
189 command_line_(base::CommandLine::NO_PROGRAM) {}
190
191 void SetUp() override {
192 command_line_.AppendSwitchASCII(
193 switches::kProcessType, switches::kRendererProcess);
194 }
195
196 void TearDown() override {
197 variations::testing::ClearAllVariationParams();
198 }
199
pmeenan9ac669682015-08-17 14:57:03200 void CreateFieldTrial(const char* trial_name, const char* group_name) {
201 base::FieldTrialList::CreateFieldTrial(trial_name, group_name);
bmcquade5d2d9cf32015-06-19 17:42:28202 }
203
bmcquade9dd54cc2015-06-22 16:56:52204 void CreateFieldTrialWithParams(
205 const char* trial_name,
pmeenan9ac669682015-08-17 14:57:03206 const char* group_name,
bmcquade9dd54cc2015-06-22 16:56:52207 const char* key1, const char* value1,
208 const char* key2, const char* value2) {
bmcquade5d2d9cf32015-06-19 17:42:28209 std::map<std::string, std::string> params;
bmcquade9dd54cc2015-06-22 16:56:52210 params.insert(std::make_pair(key1, value1));
211 params.insert(std::make_pair(key2, value2));
pmeenan9ac669682015-08-17 14:57:03212 CreateFieldTrial(trial_name, kFakeGroupName);
213 variations::AssociateVariationParams(trial_name, kFakeGroupName, params);
bmcquade5d2d9cf32015-06-19 17:42:28214 }
215
216 void AppendContentBrowserClientSwitches() {
217 client_.AppendExtraCommandLineSwitches(&command_line_, kFakeChildProcessId);
218 }
219
220 const base::CommandLine& command_line() const {
221 return command_line_;
222 }
223
224 void AppendBlinkSettingsSwitch(const char* value) {
225 command_line_.AppendSwitchASCII(switches::kBlinkSettings, value);
226 }
227
228 private:
229 static const int kFakeChildProcessId = 1;
bmcquade5d2d9cf32015-06-19 17:42:28230
231 ChromeContentBrowserClient client_;
232 base::FieldTrialList trial_list_;
233 base::CommandLine command_line_;
234
235 content::TestBrowserThreadBundle thread_bundle_;
236};
237
bmcquade9dd54cc2015-06-22 16:56:52238const char BlinkSettingsFieldTrialTest::kParserFieldTrialName[] =
bmcquade5d2d9cf32015-06-19 17:42:28239 "BackgroundHtmlParserTokenLimits";
bmcquade9dd54cc2015-06-22 16:56:52240const char BlinkSettingsFieldTrialTest::kIFrameFieldTrialName[] =
241 "LowPriorityIFrames";
pmeenan9ac669682015-08-17 14:57:03242const char BlinkSettingsFieldTrialTest::kFakeGroupName[] = "FakeGroup";
243const char BlinkSettingsFieldTrialTest::kDefaultGroupName[] = "Default";
bmcquade5d2d9cf32015-06-19 17:42:28244
245TEST_F(BlinkSettingsFieldTrialTest, NoFieldTrial) {
246 AppendContentBrowserClientSwitches();
247 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings));
248}
249
250TEST_F(BlinkSettingsFieldTrialTest, FieldTrialWithoutParams) {
pmeenan9ac669682015-08-17 14:57:03251 CreateFieldTrial(kParserFieldTrialName, kFakeGroupName);
bmcquade5d2d9cf32015-06-19 17:42:28252 AppendContentBrowserClientSwitches();
253 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings));
254}
255
256TEST_F(BlinkSettingsFieldTrialTest, BlinkSettingsSwitchAlreadySpecified) {
257 AppendBlinkSettingsSwitch("foo");
pmeenan9ac669682015-08-17 14:57:03258 CreateFieldTrialWithParams(kParserFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52259 "key1", "value1", "key2", "value2");
bmcquade5d2d9cf32015-06-19 17:42:28260 AppendContentBrowserClientSwitches();
261 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
262 EXPECT_EQ("foo",
263 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
264}
265
266TEST_F(BlinkSettingsFieldTrialTest, FieldTrialEnabled) {
pmeenan9ac669682015-08-17 14:57:03267 CreateFieldTrialWithParams(kParserFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52268 "key1", "value1", "key2", "value2");
bmcquade5d2d9cf32015-06-19 17:42:28269 AppendContentBrowserClientSwitches();
270 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
271 EXPECT_EQ("key1=value1,key2=value2",
272 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
273}
274
bmcquade9dd54cc2015-06-22 16:56:52275TEST_F(BlinkSettingsFieldTrialTest, MultipleFieldTrialsEnabled) {
pmeenan9ac669682015-08-17 14:57:03276 CreateFieldTrialWithParams(kParserFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52277 "key1", "value1", "key2", "value2");
pmeenan9ac669682015-08-17 14:57:03278 CreateFieldTrialWithParams(kIFrameFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52279 "keyA", "valueA", "keyB", "valueB");
280 AppendContentBrowserClientSwitches();
281 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
282 EXPECT_EQ("key1=value1,key2=value2,keyA=valueA,keyB=valueB",
283 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
284}
285
286TEST_F(BlinkSettingsFieldTrialTest, MultipleFieldTrialsDuplicateKeys) {
pmeenan9ac669682015-08-17 14:57:03287 CreateFieldTrialWithParams(kParserFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52288 "key1", "value1", "key2", "value2");
pmeenan9ac669682015-08-17 14:57:03289 CreateFieldTrialWithParams(kIFrameFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52290 "key2", "duplicate", "key3", "value3");
291 AppendContentBrowserClientSwitches();
292 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
293 EXPECT_EQ("key1=value1,key2=value2,key2=duplicate,key3=value3",
294 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
295}
296
jam1c5a91492016-02-24 20:47:53297#if !defined(OS_ANDROID)
[email protected]10a9bf92013-11-13 23:34:48298namespace content {
299
300class InstantNTPURLRewriteTest : public BrowserWithTestWindowTest {
301 protected:
dchenge1bc7982014-10-30 00:32:40302 void SetUp() override {
[email protected]10a9bf92013-11-13 23:34:48303 BrowserWithTestWindowTest::SetUp();
304 field_trial_list_.reset(new base::FieldTrialList(
robliao79393ffb2016-09-21 18:45:29305 base::MakeUnique<metrics::SHA1EntropyProvider>("42")));
[email protected]10a9bf92013-11-13 23:34:48306 }
307
308 void InstallTemplateURLWithNewTabPage(GURL new_tab_page_url) {
309 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
310 profile(), &TemplateURLServiceFactory::BuildInstanceFor);
311 TemplateURLService* template_url_service =
312 TemplateURLServiceFactory::GetForProfile(browser()->profile());
thestigf80564462015-09-29 23:12:08313 search_test_utils::WaitForTemplateURLServiceToLoad(template_url_service);
[email protected]10a9bf92013-11-13 23:34:48314
315 TemplateURLData data;
mpearson3c6d7af2015-05-13 23:59:53316 data.SetShortName(base::ASCIIToUTF16("foo.com"));
[email protected]10a9bf92013-11-13 23:34:48317 data.SetURL("http://foo.com/url?bar={searchTerms}");
318 data.new_tab_url = new_tab_page_url.spec();
avi8a64b715b2016-09-02 17:30:04319 TemplateURL* template_url =
320 template_url_service->Add(base::MakeUnique<TemplateURL>(data));
[email protected]f1cb5582014-04-25 07:35:26321 template_url_service->SetUserSelectedDefaultSearchProvider(template_url);
[email protected]10a9bf92013-11-13 23:34:48322 }
323
dcheng4af48582016-04-19 00:29:35324 std::unique_ptr<base::FieldTrialList> field_trial_list_;
[email protected]10a9bf92013-11-13 23:34:48325};
326
327TEST_F(InstantNTPURLRewriteTest, UberURLHandler_InstantExtendedNewTabPage) {
328 const GURL url_original("chrome://newtab");
329 const GURL url_rewritten("https://www.example.com/newtab");
330 InstallTemplateURLWithNewTabPage(url_rewritten);
331 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("InstantExtended",
332 "Group1 use_cacheable_ntp:1"));
333
334 AddTab(browser(), GURL("chrome://blank"));
335 NavigateAndCommitActiveTab(url_original);
336
337 NavigationEntry* entry = browser()->tab_strip_model()->
338 GetActiveWebContents()->GetController().GetLastCommittedEntry();
339 ASSERT_TRUE(entry != NULL);
340 EXPECT_EQ(url_rewritten, entry->GetURL());
341 EXPECT_EQ(url_original, entry->GetVirtualURL());
342}
343
344} // namespace content
jam1c5a91492016-02-24 20:47:53345#endif // !defined(OS_ANDROID)
msramek1f4746d2016-08-19 21:37:07346
347namespace {
348
msramek1f4746d2016-08-19 21:37:07349// Tests for ChromeContentBrowserClient::ClearSiteData().
350class ChromeContentBrowserClientClearSiteDataTest : public testing::Test {
351 public:
352 void SetUp() override {
msrameke169ccb2017-04-26 05:21:41353 content::BrowserContext::GetBrowsingDataRemover(profile())
354 ->SetEmbedderDelegate(&mock_delegate_);
msramek22328112017-03-23 16:50:29355 run_loop_.reset(new base::RunLoop());
msramek1f4746d2016-08-19 21:37:07356 }
357
358 content::BrowserContext* profile() { return &profile_; }
359
msrameke169ccb2017-04-26 05:21:41360 MockBrowsingDataRemoverDelegate* delegate() { return &mock_delegate_; }
msramek1f4746d2016-08-19 21:37:07361
msramek22328112017-03-23 16:50:29362 void OnClearingFinished() { run_loop_->Quit(); }
msramek1f4746d2016-08-19 21:37:07363
msramek22328112017-03-23 16:50:29364 void WaitForClearingFinished() {
365 run_loop_->Run();
366 run_loop_.reset(new base::RunLoop());
367 }
msramek1f4746d2016-08-19 21:37:07368
369 private:
msramek22328112017-03-23 16:50:29370 std::unique_ptr<base::RunLoop> run_loop_;
msrameke169ccb2017-04-26 05:21:41371 MockBrowsingDataRemoverDelegate mock_delegate_;
falken04a6912a2016-09-23 21:06:29372 content::TestBrowserThreadBundle thread_bundle_;
msramek1f4746d2016-08-19 21:37:07373 TestingProfile profile_;
msramek1f4746d2016-08-19 21:37:07374};
375
376// Tests that the parameters to ClearBrowsingData() are translated to
377// the correct BrowsingDataRemover::RemoveInternal() operation. The fourth
378// parameter, |filter_builder|, is tested in detail in the RegistrableDomains
379// test below.
380TEST_F(ChromeContentBrowserClientClearSiteDataTest, Parameters) {
381 ChromeContentBrowserClient client;
382
383 struct TestCase {
384 bool cookies;
385 bool storage;
386 bool cache;
387 int mask;
388 } test_cases[] = {
389 {false, false, false, 0},
msramek1c2b3ca2017-03-14 17:57:56390 {true, false, false,
msrameke169ccb2017-04-26 05:21:41391 content::BrowsingDataRemover::DATA_TYPE_COOKIES |
392 content::BrowsingDataRemover::DATA_TYPE_CHANNEL_IDS |
msramek1c2b3ca2017-03-14 17:57:56393 ChromeBrowsingDataRemoverDelegate::DATA_TYPE_PLUGIN_DATA},
msrameke169ccb2017-04-26 05:21:41394 {false, true, false, content::BrowsingDataRemover::DATA_TYPE_DOM_STORAGE},
395 {false, false, true, content::BrowsingDataRemover::DATA_TYPE_CACHE},
msramek1c2b3ca2017-03-14 17:57:56396 {true, true, false,
msrameke169ccb2017-04-26 05:21:41397 content::BrowsingDataRemover::DATA_TYPE_COOKIES |
398 content::BrowsingDataRemover::DATA_TYPE_CHANNEL_IDS |
msramekf4ab8372017-03-15 10:10:40399 ChromeBrowsingDataRemoverDelegate::DATA_TYPE_PLUGIN_DATA |
msrameke169ccb2017-04-26 05:21:41400 content::BrowsingDataRemover::DATA_TYPE_DOM_STORAGE},
msramek1c2b3ca2017-03-14 17:57:56401 {true, false, true,
msrameke169ccb2017-04-26 05:21:41402 content::BrowsingDataRemover::DATA_TYPE_COOKIES |
403 content::BrowsingDataRemover::DATA_TYPE_CHANNEL_IDS |
msramek1c2b3ca2017-03-14 17:57:56404 ChromeBrowsingDataRemoverDelegate::DATA_TYPE_PLUGIN_DATA |
msrameke169ccb2017-04-26 05:21:41405 content::BrowsingDataRemover::DATA_TYPE_CACHE},
msramek1c2b3ca2017-03-14 17:57:56406 {false, true, true,
msrameke169ccb2017-04-26 05:21:41407 content::BrowsingDataRemover::DATA_TYPE_DOM_STORAGE |
408 content::BrowsingDataRemover::DATA_TYPE_CACHE},
msramek1c2b3ca2017-03-14 17:57:56409 {true, true, true,
msrameke169ccb2017-04-26 05:21:41410 content::BrowsingDataRemover::DATA_TYPE_COOKIES |
411 content::BrowsingDataRemover::DATA_TYPE_CHANNEL_IDS |
msramekf4ab8372017-03-15 10:10:40412 ChromeBrowsingDataRemoverDelegate::DATA_TYPE_PLUGIN_DATA |
msrameke169ccb2017-04-26 05:21:41413 content::BrowsingDataRemover::DATA_TYPE_DOM_STORAGE |
414 content::BrowsingDataRemover::DATA_TYPE_CACHE},
msramek1f4746d2016-08-19 21:37:07415 };
416
417 for (unsigned int i = 0; i < arraysize(test_cases); ++i) {
418 SCOPED_TRACE(base::StringPrintf("Test case %d", i));
419 const TestCase& test_case = test_cases[i];
420
421 // We always delete data for all time and all origin types.
msramek1c2b3ca2017-03-14 17:57:56422 int all_origin_types = ChromeBrowsingDataRemoverDelegate::ALL_ORIGIN_TYPES;
msramek1f4746d2016-08-19 21:37:07423
424 // Some data are deleted for the origin and some for the registrable domain.
425 // Depending on the chosen datatypes, this might result into one or two
426 // calls. In the latter case, the removal mask will be split into two
427 // parts - one for the origin deletion and one for the registrable domain.
msramek1c2b3ca2017-03-14 17:57:56428 const int domain_scoped_types =
msrameke169ccb2017-04-26 05:21:41429 content::BrowsingDataRemover::DATA_TYPE_COOKIES |
430 content::BrowsingDataRemover::DATA_TYPE_CHANNEL_IDS |
msramek1c2b3ca2017-03-14 17:57:56431 ChromeBrowsingDataRemoverDelegate::DATA_TYPE_PLUGIN_DATA;
msramek1f4746d2016-08-19 21:37:07432 int registrable_domain_deletion_mask = test_case.mask & domain_scoped_types;
433 int origin_deletion_mask = test_case.mask & ~domain_scoped_types;
434
435 if (registrable_domain_deletion_mask) {
msramek22328112017-03-23 16:50:29436 delegate()->ExpectCallDontCareAboutFilterBuilder(
437 base::Time(), base::Time::Max(), registrable_domain_deletion_mask,
438 all_origin_types);
msramek1f4746d2016-08-19 21:37:07439 }
440
441 if (origin_deletion_mask) {
msramek22328112017-03-23 16:50:29442 delegate()->ExpectCallDontCareAboutFilterBuilder(
443 base::Time(), base::Time::Max(), origin_deletion_mask,
444 all_origin_types);
msramek1f4746d2016-08-19 21:37:07445 }
446
msramek1f4746d2016-08-19 21:37:07447 client.ClearSiteData(
448 profile(), url::Origin(GURL("https://www.example.com")),
449 test_case.cookies, test_case.storage, test_case.cache,
450 base::Bind(
msramek22328112017-03-23 16:50:29451 &ChromeContentBrowserClientClearSiteDataTest::OnClearingFinished,
452 base::Unretained(this)));
453 WaitForClearingFinished();
msramek1f4746d2016-08-19 21:37:07454
msramek22328112017-03-23 16:50:29455 delegate()->VerifyAndClearExpectations();
msramek1f4746d2016-08-19 21:37:07456 }
457}
458
459// Tests that ClearBrowsingData() called for an origin deletes cookies in the
460// scope of the registrable domain corresponding to that origin, while cache
461// is deleted for that exact origin.
462TEST_F(ChromeContentBrowserClientClearSiteDataTest, RegistrableDomains) {
463 ChromeContentBrowserClient client;
464
465 struct TestCase {
466 const char* origin; // origin on which ClearSiteData() is called.
467 const char* domain; // domain on which cookies will be deleted.
468 } test_cases[] = {
469 // TLD has no embedded dot.
470 {"https://example.com", "example.com"},
471 {"https://www.example.com", "example.com"},
472 {"https://www.fourth.third.second.com", "second.com"},
473
474 // TLD has one embedded dot.
475 {"https://www.example.co.uk", "example.co.uk"},
476 {"https://example.co.uk", "example.co.uk"},
477
478 // TLD has more embedded dots.
479 {"https://www.website.sp.nom.br", "website.sp.nom.br"},
480
481 // IP addresses.
482 {"http://127.0.0.1", "127.0.0.1"},
483 {"http://192.168.0.1", "192.168.0.1"},
484 {"http://192.168.0.1", "192.168.0.1"},
485
486 // Internal hostnames.
487 {"http://localhost", "localhost"},
488 {"http://fileserver", "fileserver"},
489
490 // These are not subdomains of internal hostnames, but subdomain of
491 // unknown TLDs.
492 {"http://subdomain.localhost", "subdomain.localhost"},
493 {"http://www.subdomain.localhost", "subdomain.localhost"},
494 {"http://documents.fileserver", "documents.fileserver"},
495
496 // Scheme and port don't matter.
497 {"http://example.com", "example.com"},
498 {"http://example.com:8080", "example.com"},
499 {"https://example.com:4433", "example.com"},
500 };
501
502 for (const TestCase& test_case : test_cases) {
503 SCOPED_TRACE(test_case.origin);
504
msramekc7ee9d52017-01-24 12:01:05505 std::unique_ptr<BrowsingDataFilterBuilder>
506 registrable_domain_filter_builder(BrowsingDataFilterBuilder::Create(
msramek1f4746d2016-08-19 21:37:07507 BrowsingDataFilterBuilder::WHITELIST));
508 registrable_domain_filter_builder->AddRegisterableDomain(test_case.domain);
509
msramek22328112017-03-23 16:50:29510 delegate()->ExpectCall(
msramek1c8e19d2017-01-04 20:04:53511 base::Time(), base::Time::Max(),
msrameke169ccb2017-04-26 05:21:41512 content::BrowsingDataRemover::DATA_TYPE_COOKIES |
513 content::BrowsingDataRemover::DATA_TYPE_CHANNEL_IDS |
msramek1c2b3ca2017-03-14 17:57:56514 ChromeBrowsingDataRemoverDelegate::DATA_TYPE_PLUGIN_DATA,
515 ChromeBrowsingDataRemoverDelegate::ALL_ORIGIN_TYPES,
msramek22328112017-03-23 16:50:29516 *registrable_domain_filter_builder);
msramek1f4746d2016-08-19 21:37:07517
msramekc7ee9d52017-01-24 12:01:05518 std::unique_ptr<BrowsingDataFilterBuilder> origin_filter_builder(
519 BrowsingDataFilterBuilder::Create(
520 BrowsingDataFilterBuilder::WHITELIST));
msramek1f4746d2016-08-19 21:37:07521 origin_filter_builder->AddOrigin(url::Origin(GURL(test_case.origin)));
522
msramek22328112017-03-23 16:50:29523 delegate()->ExpectCall(base::Time(), base::Time::Max(),
msrameke169ccb2017-04-26 05:21:41524 content::BrowsingDataRemover::DATA_TYPE_CACHE,
msramek22328112017-03-23 16:50:29525 ChromeBrowsingDataRemoverDelegate::ALL_ORIGIN_TYPES,
526 *origin_filter_builder);
msramek1f4746d2016-08-19 21:37:07527
msramek1f4746d2016-08-19 21:37:07528 client.ClearSiteData(
529 profile(), url::Origin(GURL(test_case.origin)), true /* cookies */,
530 false /* storage */, true /* cache */,
531 base::Bind(
msramek22328112017-03-23 16:50:29532 &ChromeContentBrowserClientClearSiteDataTest::OnClearingFinished,
533 base::Unretained(this)));
534 WaitForClearingFinished();
msramek1f4746d2016-08-19 21:37:07535
msramek22328112017-03-23 16:50:29536 delegate()->VerifyAndClearExpectations();
msramek1f4746d2016-08-19 21:37:07537 }
538}
539
540// Tests that we always wait for all scheduled BrowsingDataRemover tasks and
541// that BrowsingDataRemoverObserver never leaks.
542TEST_F(ChromeContentBrowserClientClearSiteDataTest, Tasks) {
543 ChromeContentBrowserClient client;
544 url::Origin origin(GURL("https://www.example.com"));
545
546 // No removal tasks.
msramek1f4746d2016-08-19 21:37:07547 client.ClearSiteData(
548 profile(), origin, false /* cookies */, false /* storage */,
549 false /* cache */,
550 base::Bind(
msramek22328112017-03-23 16:50:29551 &ChromeContentBrowserClientClearSiteDataTest::OnClearingFinished,
552 base::Unretained(this)));
553 WaitForClearingFinished();
msramek1f4746d2016-08-19 21:37:07554
555 // One removal task: deleting cookies with a domain filter.
msramek1f4746d2016-08-19 21:37:07556 client.ClearSiteData(
557 profile(), origin, true /* cookies */, false /* storage */,
558 false /* cache */,
559 base::Bind(
msramek22328112017-03-23 16:50:29560 &ChromeContentBrowserClientClearSiteDataTest::OnClearingFinished,
561 base::Unretained(this)));
562 WaitForClearingFinished();
msramek1f4746d2016-08-19 21:37:07563
564 // One removal task: deleting cache with a domain filter.
msramek1f4746d2016-08-19 21:37:07565 client.ClearSiteData(
566 profile(), origin, false /* cookies */, false /* storage */,
567 true /* cache */,
568 base::Bind(
msramek22328112017-03-23 16:50:29569 &ChromeContentBrowserClientClearSiteDataTest::OnClearingFinished,
570 base::Unretained(this)));
571 WaitForClearingFinished();
msramek1f4746d2016-08-19 21:37:07572
573 // Two removal tasks, with domain and origin filters respectively.
msramek1f4746d2016-08-19 21:37:07574 client.ClearSiteData(
575 profile(), origin, true /* cookies */, false /* storage */,
576 true /* cache */,
577 base::Bind(
msramek22328112017-03-23 16:50:29578 &ChromeContentBrowserClientClearSiteDataTest::OnClearingFinished,
579 base::Unretained(this)));
580 WaitForClearingFinished();
msramek1f4746d2016-08-19 21:37:07581}
582
583} // namespace