blob: 8c58ceeef6810dca56fed6fb63ab6c6943c412f1 [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"
msramek1f4746d2016-08-19 21:37:0717#include "base/strings/stringprintf.h"
mpearson3c6d7af2015-05-13 23:59:5318#include "base/strings/utf_string_conversions.h"
avie4d7b6f2015-12-26 00:59:1819#include "build/build_config.h"
msramek1f4746d2016-08-19 21:37:0720#include "chrome/browser/browsing_data/browsing_data_helper.h"
msramek1f4746d2016-08-19 21:37:0721#include "chrome/browser/browsing_data/browsing_data_remover_factory.h"
msramek1c2b3ca2017-03-14 17:57:5622#include "chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h"
dullweber525fa6782017-02-28 09:43:0223#include "chrome/browser/browsing_data/mock_browsing_data_remover.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"
[email protected]10a9bf92013-11-13 23:34:4832#include "content/public/browser/navigation_controller.h"
33#include "content/public/browser/navigation_entry.h"
34#include "content/public/browser/web_contents.h"
[email protected]30335fdf02014-02-26 19:51:2735#include "content/public/common/content_switches.h"
bmcquade5d2d9cf32015-06-19 17:42:2836#include "content/public/test/test_browser_thread_bundle.h"
Brett Wilson0748bf412016-11-22 17:55:4637#include "media/media_features.h"
msramek1f4746d2016-08-19 21:37:0738#include "testing/gmock/include/gmock/gmock.h"
[email protected]3d831992013-07-04 01:13:2939#include "testing/gtest/include/gtest/gtest.h"
[email protected]5f5ef802013-07-04 16:11:1340#include "url/gurl.h"
[email protected]3d831992013-07-04 01:13:2941
jam1c5a91492016-02-24 20:47:5342#if !defined(OS_ANDROID)
thestigf80564462015-09-29 23:12:0843#include "chrome/browser/ui/browser.h"
44#include "chrome/browser/ui/tabs/tab_strip_model.h"
bengre8a146f2016-03-10 01:20:2245#include "chrome/common/pref_names.h"
thestigf80564462015-09-29 23:12:0846#include "chrome/test/base/browser_with_test_window_test.h"
47#include "chrome/test/base/search_test_utils.h"
48#endif
49
msrameka3c7cfd2017-02-03 13:34:1350using content::BrowsingDataFilterBuilder;
msramek1f4746d2016-08-19 21:37:0751using testing::_;
mlamourieb40d552015-02-05 00:57:0852using ChromeContentBrowserClientTest = testing::Test;
[email protected]3d831992013-07-04 01:13:2953
[email protected]3d831992013-07-04 01:13:2954TEST_F(ChromeContentBrowserClientTest, ShouldAssignSiteForURL) {
55 ChromeContentBrowserClient client;
56 EXPECT_FALSE(client.ShouldAssignSiteForURL(GURL("chrome-native://test")));
57 EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("http://www.google.com")));
58 EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("https://www.google.com")));
59}
60
jam1c5a91492016-02-24 20:47:5361// BrowserWithTestWindowTest doesn't work on Android.
62#if !defined(OS_ANDROID)
mlamourieb40d552015-02-05 00:57:0863
peterbbcccc12015-02-11 22:23:3364using ChromeContentBrowserClientWindowTest = BrowserWithTestWindowTest;
65
66static void DidOpenURLForWindowTest(content::WebContents** target_contents,
67 content::WebContents* opened_contents) {
68 DCHECK(target_contents);
69
70 *target_contents = opened_contents;
71}
72
bengre8a146f2016-03-10 01:20:2273TEST_F(ChromeContentBrowserClientWindowTest, IsDataSaverEnabled) {
74 ChromeContentBrowserClient client;
75 content::BrowserContext* context = browser()->profile();
76 EXPECT_FALSE(client.IsDataSaverEnabled(context));
77 browser()->profile()->GetPrefs()->SetBoolean(prefs::kDataSaverEnabled, true);
78 EXPECT_TRUE(client.IsDataSaverEnabled(context));
79}
80
mlamourieb40d552015-02-05 00:57:0881// This test opens two URLs using ContentBrowserClient::OpenURL. It expects the
82// URLs to be opened in new tabs and activated, changing the active tabs after
83// each call and increasing the tab count by 2.
84TEST_F(ChromeContentBrowserClientWindowTest, OpenURL) {
85 ChromeContentBrowserClient client;
86
87 int previous_count = browser()->tab_strip_model()->count();
88
89 GURL urls[] = { GURL("https://www.google.com"),
90 GURL("https://www.chromium.org") };
91
92 for (const GURL& url : urls) {
nick3b04f322016-08-31 19:29:1993 content::OpenURLParams params(url, content::Referrer(),
94 WindowOpenDisposition::NEW_FOREGROUND_TAB,
95 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, false);
peterbbcccc12015-02-11 22:23:3396 // TODO(peter): We should have more in-depth browser tests for the window
97 // opening functionality, which also covers Android. This test can currently
98 // only be ran on platforms where OpenURL is implemented synchronously.
99 // See https://crbug.com/457667.
100 content::WebContents* web_contents = nullptr;
101 client.OpenURL(browser()->profile(),
102 params,
103 base::Bind(&DidOpenURLForWindowTest, &web_contents));
104
105 EXPECT_TRUE(web_contents);
mlamourieb40d552015-02-05 00:57:08106
107 content::WebContents* active_contents = browser()->tab_strip_model()->
108 GetActiveWebContents();
peterbbcccc12015-02-11 22:23:33109 EXPECT_EQ(web_contents, active_contents);
mlamourieb40d552015-02-05 00:57:08110 EXPECT_EQ(url, active_contents->GetVisibleURL());
111 }
112
113 EXPECT_EQ(previous_count + 2, browser()->tab_strip_model()->count());
114}
115
jam1c5a91492016-02-24 20:47:53116#endif // !defined(OS_ANDROID)
mlamourieb40d552015-02-05 00:57:08117
Brett Wilson0748bf412016-11-22 17:55:46118#if BUILDFLAG(ENABLE_WEBRTC)
brettwa68ea2b2015-02-01 02:54:07119
[email protected]30335fdf02014-02-26 19:51:27120// NOTE: Any updates to the expectations in these tests should also be done in
121// the browser test WebRtcDisableEncryptionFlagBrowserTest.
122class DisableWebRtcEncryptionFlagTest : public testing::Test {
123 public:
124 DisableWebRtcEncryptionFlagTest()
avi3ef9ec9e2014-12-22 22:50:17125 : from_command_line_(base::CommandLine::NO_PROGRAM),
126 to_command_line_(base::CommandLine::NO_PROGRAM) {}
[email protected]30335fdf02014-02-26 19:51:27127
128 protected:
dchenge1bc7982014-10-30 00:32:40129 void SetUp() override {
[email protected]30335fdf02014-02-26 19:51:27130 from_command_line_.AppendSwitch(switches::kDisableWebRtcEncryption);
131 }
132
sdefresne6e883e42015-07-30 08:05:54133 void MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel channel) {
[email protected]30335fdf02014-02-26 19:51:27134 ChromeContentBrowserClient::MaybeCopyDisableWebRtcEncryptionSwitch(
135 &to_command_line_,
136 from_command_line_,
137 channel);
138 }
139
avi3ef9ec9e2014-12-22 22:50:17140 base::CommandLine from_command_line_;
141 base::CommandLine to_command_line_;
[email protected]30335fdf02014-02-26 19:51:27142
thestigf80564462015-09-29 23:12:08143 private:
[email protected]30335fdf02014-02-26 19:51:27144 DISALLOW_COPY_AND_ASSIGN(DisableWebRtcEncryptionFlagTest);
145};
146
147TEST_F(DisableWebRtcEncryptionFlagTest, UnknownChannel) {
sdefresne6e883e42015-07-30 08:05:54148 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::UNKNOWN);
[email protected]30335fdf02014-02-26 19:51:27149 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
150}
151
152TEST_F(DisableWebRtcEncryptionFlagTest, CanaryChannel) {
sdefresne6e883e42015-07-30 08:05:54153 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::CANARY);
[email protected]30335fdf02014-02-26 19:51:27154 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
155}
156
157TEST_F(DisableWebRtcEncryptionFlagTest, DevChannel) {
sdefresne6e883e42015-07-30 08:05:54158 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::DEV);
[email protected]30335fdf02014-02-26 19:51:27159 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
160}
161
162TEST_F(DisableWebRtcEncryptionFlagTest, BetaChannel) {
sdefresne6e883e42015-07-30 08:05:54163 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::BETA);
[email protected]30335fdf02014-02-26 19:51:27164#if defined(OS_ANDROID)
165 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
166#else
167 EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
168#endif
169}
170
171TEST_F(DisableWebRtcEncryptionFlagTest, StableChannel) {
sdefresne6e883e42015-07-30 08:05:54172 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::STABLE);
[email protected]30335fdf02014-02-26 19:51:27173 EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
174}
175
brettwa68ea2b2015-02-01 02:54:07176#endif // ENABLE_WEBRTC
177
bmcquade5d2d9cf32015-06-19 17:42:28178class BlinkSettingsFieldTrialTest : public testing::Test {
179 public:
bmcquade9dd54cc2015-06-22 16:56:52180 static const char kParserFieldTrialName[];
181 static const char kIFrameFieldTrialName[];
pmeenan9ac669682015-08-17 14:57:03182 static const char kFakeGroupName[];
183 static const char kDefaultGroupName[];
bmcquade9dd54cc2015-06-22 16:56:52184
bmcquade5d2d9cf32015-06-19 17:42:28185 BlinkSettingsFieldTrialTest()
186 : trial_list_(NULL),
187 command_line_(base::CommandLine::NO_PROGRAM) {}
188
189 void SetUp() override {
190 command_line_.AppendSwitchASCII(
191 switches::kProcessType, switches::kRendererProcess);
192 }
193
194 void TearDown() override {
195 variations::testing::ClearAllVariationParams();
196 }
197
pmeenan9ac669682015-08-17 14:57:03198 void CreateFieldTrial(const char* trial_name, const char* group_name) {
199 base::FieldTrialList::CreateFieldTrial(trial_name, group_name);
bmcquade5d2d9cf32015-06-19 17:42:28200 }
201
bmcquade9dd54cc2015-06-22 16:56:52202 void CreateFieldTrialWithParams(
203 const char* trial_name,
pmeenan9ac669682015-08-17 14:57:03204 const char* group_name,
bmcquade9dd54cc2015-06-22 16:56:52205 const char* key1, const char* value1,
206 const char* key2, const char* value2) {
bmcquade5d2d9cf32015-06-19 17:42:28207 std::map<std::string, std::string> params;
bmcquade9dd54cc2015-06-22 16:56:52208 params.insert(std::make_pair(key1, value1));
209 params.insert(std::make_pair(key2, value2));
pmeenan9ac669682015-08-17 14:57:03210 CreateFieldTrial(trial_name, kFakeGroupName);
211 variations::AssociateVariationParams(trial_name, kFakeGroupName, params);
bmcquade5d2d9cf32015-06-19 17:42:28212 }
213
214 void AppendContentBrowserClientSwitches() {
215 client_.AppendExtraCommandLineSwitches(&command_line_, kFakeChildProcessId);
216 }
217
218 const base::CommandLine& command_line() const {
219 return command_line_;
220 }
221
222 void AppendBlinkSettingsSwitch(const char* value) {
223 command_line_.AppendSwitchASCII(switches::kBlinkSettings, value);
224 }
225
226 private:
227 static const int kFakeChildProcessId = 1;
bmcquade5d2d9cf32015-06-19 17:42:28228
229 ChromeContentBrowserClient client_;
230 base::FieldTrialList trial_list_;
231 base::CommandLine command_line_;
232
233 content::TestBrowserThreadBundle thread_bundle_;
234};
235
bmcquade9dd54cc2015-06-22 16:56:52236const char BlinkSettingsFieldTrialTest::kParserFieldTrialName[] =
bmcquade5d2d9cf32015-06-19 17:42:28237 "BackgroundHtmlParserTokenLimits";
bmcquade9dd54cc2015-06-22 16:56:52238const char BlinkSettingsFieldTrialTest::kIFrameFieldTrialName[] =
239 "LowPriorityIFrames";
pmeenan9ac669682015-08-17 14:57:03240const char BlinkSettingsFieldTrialTest::kFakeGroupName[] = "FakeGroup";
241const char BlinkSettingsFieldTrialTest::kDefaultGroupName[] = "Default";
bmcquade5d2d9cf32015-06-19 17:42:28242
243TEST_F(BlinkSettingsFieldTrialTest, NoFieldTrial) {
244 AppendContentBrowserClientSwitches();
245 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings));
246}
247
248TEST_F(BlinkSettingsFieldTrialTest, FieldTrialWithoutParams) {
pmeenan9ac669682015-08-17 14:57:03249 CreateFieldTrial(kParserFieldTrialName, kFakeGroupName);
bmcquade5d2d9cf32015-06-19 17:42:28250 AppendContentBrowserClientSwitches();
251 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings));
252}
253
254TEST_F(BlinkSettingsFieldTrialTest, BlinkSettingsSwitchAlreadySpecified) {
255 AppendBlinkSettingsSwitch("foo");
pmeenan9ac669682015-08-17 14:57:03256 CreateFieldTrialWithParams(kParserFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52257 "key1", "value1", "key2", "value2");
bmcquade5d2d9cf32015-06-19 17:42:28258 AppendContentBrowserClientSwitches();
259 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
260 EXPECT_EQ("foo",
261 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
262}
263
264TEST_F(BlinkSettingsFieldTrialTest, FieldTrialEnabled) {
pmeenan9ac669682015-08-17 14:57:03265 CreateFieldTrialWithParams(kParserFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52266 "key1", "value1", "key2", "value2");
bmcquade5d2d9cf32015-06-19 17:42:28267 AppendContentBrowserClientSwitches();
268 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
269 EXPECT_EQ("key1=value1,key2=value2",
270 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
271}
272
bmcquade9dd54cc2015-06-22 16:56:52273TEST_F(BlinkSettingsFieldTrialTest, MultipleFieldTrialsEnabled) {
pmeenan9ac669682015-08-17 14:57:03274 CreateFieldTrialWithParams(kParserFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52275 "key1", "value1", "key2", "value2");
pmeenan9ac669682015-08-17 14:57:03276 CreateFieldTrialWithParams(kIFrameFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52277 "keyA", "valueA", "keyB", "valueB");
278 AppendContentBrowserClientSwitches();
279 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
280 EXPECT_EQ("key1=value1,key2=value2,keyA=valueA,keyB=valueB",
281 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
282}
283
284TEST_F(BlinkSettingsFieldTrialTest, MultipleFieldTrialsDuplicateKeys) {
pmeenan9ac669682015-08-17 14:57:03285 CreateFieldTrialWithParams(kParserFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52286 "key1", "value1", "key2", "value2");
pmeenan9ac669682015-08-17 14:57:03287 CreateFieldTrialWithParams(kIFrameFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52288 "key2", "duplicate", "key3", "value3");
289 AppendContentBrowserClientSwitches();
290 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
291 EXPECT_EQ("key1=value1,key2=value2,key2=duplicate,key3=value3",
292 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
293}
294
jam1c5a91492016-02-24 20:47:53295#if !defined(OS_ANDROID)
[email protected]10a9bf92013-11-13 23:34:48296namespace content {
297
298class InstantNTPURLRewriteTest : public BrowserWithTestWindowTest {
299 protected:
dchenge1bc7982014-10-30 00:32:40300 void SetUp() override {
[email protected]10a9bf92013-11-13 23:34:48301 BrowserWithTestWindowTest::SetUp();
302 field_trial_list_.reset(new base::FieldTrialList(
robliao79393ffb2016-09-21 18:45:29303 base::MakeUnique<metrics::SHA1EntropyProvider>("42")));
[email protected]10a9bf92013-11-13 23:34:48304 }
305
306 void InstallTemplateURLWithNewTabPage(GURL new_tab_page_url) {
307 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
308 profile(), &TemplateURLServiceFactory::BuildInstanceFor);
309 TemplateURLService* template_url_service =
310 TemplateURLServiceFactory::GetForProfile(browser()->profile());
thestigf80564462015-09-29 23:12:08311 search_test_utils::WaitForTemplateURLServiceToLoad(template_url_service);
[email protected]10a9bf92013-11-13 23:34:48312
313 TemplateURLData data;
mpearson3c6d7af2015-05-13 23:59:53314 data.SetShortName(base::ASCIIToUTF16("foo.com"));
[email protected]10a9bf92013-11-13 23:34:48315 data.SetURL("http://foo.com/url?bar={searchTerms}");
316 data.new_tab_url = new_tab_page_url.spec();
avi8a64b715b2016-09-02 17:30:04317 TemplateURL* template_url =
318 template_url_service->Add(base::MakeUnique<TemplateURL>(data));
[email protected]f1cb5582014-04-25 07:35:26319 template_url_service->SetUserSelectedDefaultSearchProvider(template_url);
[email protected]10a9bf92013-11-13 23:34:48320 }
321
dcheng4af48582016-04-19 00:29:35322 std::unique_ptr<base::FieldTrialList> field_trial_list_;
[email protected]10a9bf92013-11-13 23:34:48323};
324
325TEST_F(InstantNTPURLRewriteTest, UberURLHandler_InstantExtendedNewTabPage) {
326 const GURL url_original("chrome://newtab");
327 const GURL url_rewritten("https://www.example.com/newtab");
328 InstallTemplateURLWithNewTabPage(url_rewritten);
329 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("InstantExtended",
330 "Group1 use_cacheable_ntp:1"));
331
332 AddTab(browser(), GURL("chrome://blank"));
333 NavigateAndCommitActiveTab(url_original);
334
335 NavigationEntry* entry = browser()->tab_strip_model()->
336 GetActiveWebContents()->GetController().GetLastCommittedEntry();
337 ASSERT_TRUE(entry != NULL);
338 EXPECT_EQ(url_rewritten, entry->GetURL());
339 EXPECT_EQ(url_original, entry->GetVirtualURL());
340}
341
342} // namespace content
jam1c5a91492016-02-24 20:47:53343#endif // !defined(OS_ANDROID)
msramek1f4746d2016-08-19 21:37:07344
345namespace {
346
msramek1f4746d2016-08-19 21:37:07347// Tests for ChromeContentBrowserClient::ClearSiteData().
348class ChromeContentBrowserClientClearSiteDataTest : public testing::Test {
349 public:
350 void SetUp() override {
351 BrowsingDataRemoverFactory::GetInstance()->SetTestingFactoryAndUse(
352 &profile_, &ChromeContentBrowserClientClearSiteDataTest::GetRemover);
353 }
354
355 content::BrowserContext* profile() { return &profile_; }
356
357 MockBrowsingDataRemover* remover() {
358 return static_cast<MockBrowsingDataRemover*>(
359 BrowsingDataRemoverFactory::GetForBrowserContext(&profile_));
360 }
361
362 void SetClearingFinished(bool finished) { finished_ = finished; }
363
364 bool IsClearingFinished() { return finished_; }
365
366 private:
367 static std::unique_ptr<KeyedService> GetRemover(
368 content::BrowserContext* context) {
369 return base::WrapUnique(new MockBrowsingDataRemover(context));
370 }
371
falken04a6912a2016-09-23 21:06:29372 content::TestBrowserThreadBundle thread_bundle_;
msramek1f4746d2016-08-19 21:37:07373 TestingProfile profile_;
374 bool finished_;
375};
376
377// Tests that the parameters to ClearBrowsingData() are translated to
378// the correct BrowsingDataRemover::RemoveInternal() operation. The fourth
379// parameter, |filter_builder|, is tested in detail in the RegistrableDomains
380// test below.
381TEST_F(ChromeContentBrowserClientClearSiteDataTest, Parameters) {
382 ChromeContentBrowserClient client;
383
384 struct TestCase {
385 bool cookies;
386 bool storage;
387 bool cache;
388 int mask;
389 } test_cases[] = {
390 {false, false, false, 0},
msramek1c2b3ca2017-03-14 17:57:56391 {true, false, false,
392 BrowsingDataRemover::DATA_TYPE_COOKIES |
393 BrowsingDataRemover::DATA_TYPE_CHANNEL_IDS |
394 ChromeBrowsingDataRemoverDelegate::DATA_TYPE_PLUGIN_DATA},
msramekf4ab8372017-03-15 10:10:40395 {false, true, false, BrowsingDataRemover::DATA_TYPE_DOM_STORAGE},
msramek1c2b3ca2017-03-14 17:57:56396 {false, false, true, BrowsingDataRemover::DATA_TYPE_CACHE},
397 {true, true, false,
msramekf4ab8372017-03-15 10:10:40398 BrowsingDataRemover::DATA_TYPE_COOKIES |
399 BrowsingDataRemover::DATA_TYPE_CHANNEL_IDS |
400 ChromeBrowsingDataRemoverDelegate::DATA_TYPE_PLUGIN_DATA |
401 BrowsingDataRemover::DATA_TYPE_DOM_STORAGE},
msramek1c2b3ca2017-03-14 17:57:56402 {true, false, true,
403 BrowsingDataRemover::DATA_TYPE_COOKIES |
404 BrowsingDataRemover::DATA_TYPE_CHANNEL_IDS |
405 ChromeBrowsingDataRemoverDelegate::DATA_TYPE_PLUGIN_DATA |
406 BrowsingDataRemover::DATA_TYPE_CACHE},
407 {false, true, true,
msramekf4ab8372017-03-15 10:10:40408 BrowsingDataRemover::DATA_TYPE_DOM_STORAGE |
409 BrowsingDataRemover::DATA_TYPE_CACHE},
msramek1c2b3ca2017-03-14 17:57:56410 {true, true, true,
msramekf4ab8372017-03-15 10:10:40411 BrowsingDataRemover::DATA_TYPE_COOKIES |
412 BrowsingDataRemover::DATA_TYPE_CHANNEL_IDS |
413 ChromeBrowsingDataRemoverDelegate::DATA_TYPE_PLUGIN_DATA |
414 BrowsingDataRemover::DATA_TYPE_DOM_STORAGE |
msramek1c2b3ca2017-03-14 17:57:56415 BrowsingDataRemover::DATA_TYPE_CACHE},
msramek1f4746d2016-08-19 21:37:07416 };
417
418 for (unsigned int i = 0; i < arraysize(test_cases); ++i) {
419 SCOPED_TRACE(base::StringPrintf("Test case %d", i));
420 const TestCase& test_case = test_cases[i];
421
422 // We always delete data for all time and all origin types.
msramek1c2b3ca2017-03-14 17:57:56423 int all_origin_types = ChromeBrowsingDataRemoverDelegate::ALL_ORIGIN_TYPES;
msramek1f4746d2016-08-19 21:37:07424
425 // Some data are deleted for the origin and some for the registrable domain.
426 // Depending on the chosen datatypes, this might result into one or two
427 // calls. In the latter case, the removal mask will be split into two
428 // parts - one for the origin deletion and one for the registrable domain.
msramek1c2b3ca2017-03-14 17:57:56429 const int domain_scoped_types =
430 BrowsingDataRemover::DATA_TYPE_COOKIES |
431 BrowsingDataRemover::DATA_TYPE_CHANNEL_IDS |
432 ChromeBrowsingDataRemoverDelegate::DATA_TYPE_PLUGIN_DATA;
msramek1f4746d2016-08-19 21:37:07433 int registrable_domain_deletion_mask = test_case.mask & domain_scoped_types;
434 int origin_deletion_mask = test_case.mask & ~domain_scoped_types;
435
436 if (registrable_domain_deletion_mask) {
437 remover()->ExpectCallDontCareAboutFilterBuilder(
msramek1c8e19d2017-01-04 20:04:53438 base::Time(), base::Time::Max(),
439 registrable_domain_deletion_mask, all_origin_types);
msramek1f4746d2016-08-19 21:37:07440 }
441
442 if (origin_deletion_mask) {
443 remover()->ExpectCallDontCareAboutFilterBuilder(
msramek1c8e19d2017-01-04 20:04:53444 base::Time(), base::Time::Max(),
445 origin_deletion_mask, all_origin_types);
msramek1f4746d2016-08-19 21:37:07446 }
447
448 SetClearingFinished(false);
449 client.ClearSiteData(
450 profile(), url::Origin(GURL("https://www.example.com")),
451 test_case.cookies, test_case.storage, test_case.cache,
452 base::Bind(
453 &ChromeContentBrowserClientClearSiteDataTest::SetClearingFinished,
454 base::Unretained(this), true));
455 EXPECT_TRUE(IsClearingFinished());
456
457 remover()->VerifyAndClearExpectations();
458 }
459}
460
461// Tests that ClearBrowsingData() called for an origin deletes cookies in the
462// scope of the registrable domain corresponding to that origin, while cache
463// is deleted for that exact origin.
464TEST_F(ChromeContentBrowserClientClearSiteDataTest, RegistrableDomains) {
465 ChromeContentBrowserClient client;
466
467 struct TestCase {
468 const char* origin; // origin on which ClearSiteData() is called.
469 const char* domain; // domain on which cookies will be deleted.
470 } test_cases[] = {
471 // TLD has no embedded dot.
472 {"https://example.com", "example.com"},
473 {"https://www.example.com", "example.com"},
474 {"https://www.fourth.third.second.com", "second.com"},
475
476 // TLD has one embedded dot.
477 {"https://www.example.co.uk", "example.co.uk"},
478 {"https://example.co.uk", "example.co.uk"},
479
480 // TLD has more embedded dots.
481 {"https://www.website.sp.nom.br", "website.sp.nom.br"},
482
483 // IP addresses.
484 {"http://127.0.0.1", "127.0.0.1"},
485 {"http://192.168.0.1", "192.168.0.1"},
486 {"http://192.168.0.1", "192.168.0.1"},
487
488 // Internal hostnames.
489 {"http://localhost", "localhost"},
490 {"http://fileserver", "fileserver"},
491
492 // These are not subdomains of internal hostnames, but subdomain of
493 // unknown TLDs.
494 {"http://subdomain.localhost", "subdomain.localhost"},
495 {"http://www.subdomain.localhost", "subdomain.localhost"},
496 {"http://documents.fileserver", "documents.fileserver"},
497
498 // Scheme and port don't matter.
499 {"http://example.com", "example.com"},
500 {"http://example.com:8080", "example.com"},
501 {"https://example.com:4433", "example.com"},
502 };
503
504 for (const TestCase& test_case : test_cases) {
505 SCOPED_TRACE(test_case.origin);
506
msramekc7ee9d52017-01-24 12:01:05507 std::unique_ptr<BrowsingDataFilterBuilder>
508 registrable_domain_filter_builder(BrowsingDataFilterBuilder::Create(
msramek1f4746d2016-08-19 21:37:07509 BrowsingDataFilterBuilder::WHITELIST));
510 registrable_domain_filter_builder->AddRegisterableDomain(test_case.domain);
511
512 remover()->ExpectCall(
msramek1c8e19d2017-01-04 20:04:53513 base::Time(), base::Time::Max(),
msramek1c2b3ca2017-03-14 17:57:56514 BrowsingDataRemover::DATA_TYPE_COOKIES |
515 BrowsingDataRemover::DATA_TYPE_CHANNEL_IDS |
516 ChromeBrowsingDataRemoverDelegate::DATA_TYPE_PLUGIN_DATA,
517 ChromeBrowsingDataRemoverDelegate::ALL_ORIGIN_TYPES,
518 std::move(registrable_domain_filter_builder));
msramek1f4746d2016-08-19 21:37:07519
msramekc7ee9d52017-01-24 12:01:05520 std::unique_ptr<BrowsingDataFilterBuilder> origin_filter_builder(
521 BrowsingDataFilterBuilder::Create(
522 BrowsingDataFilterBuilder::WHITELIST));
msramek1f4746d2016-08-19 21:37:07523 origin_filter_builder->AddOrigin(url::Origin(GURL(test_case.origin)));
524
msramek1c2b3ca2017-03-14 17:57:56525 remover()->ExpectCall(base::Time(), base::Time::Max(),
526 BrowsingDataRemover::DATA_TYPE_CACHE,
527 ChromeBrowsingDataRemoverDelegate::ALL_ORIGIN_TYPES,
528 std::move(origin_filter_builder));
msramek1f4746d2016-08-19 21:37:07529
530 SetClearingFinished(false);
531 client.ClearSiteData(
532 profile(), url::Origin(GURL(test_case.origin)), true /* cookies */,
533 false /* storage */, true /* cache */,
534 base::Bind(
535 &ChromeContentBrowserClientClearSiteDataTest::SetClearingFinished,
536 base::Unretained(this), true));
537 EXPECT_TRUE(IsClearingFinished());
538
539 remover()->VerifyAndClearExpectations();
540 }
541}
542
543// Tests that we always wait for all scheduled BrowsingDataRemover tasks and
544// that BrowsingDataRemoverObserver never leaks.
545TEST_F(ChromeContentBrowserClientClearSiteDataTest, Tasks) {
546 ChromeContentBrowserClient client;
547 url::Origin origin(GURL("https://www.example.com"));
548
549 // No removal tasks.
550 SetClearingFinished(false);
551 client.ClearSiteData(
552 profile(), origin, false /* cookies */, false /* storage */,
553 false /* cache */,
554 base::Bind(
555 &ChromeContentBrowserClientClearSiteDataTest::SetClearingFinished,
556 base::Unretained(this), true));
557 EXPECT_TRUE(IsClearingFinished());
558
559 // One removal task: deleting cookies with a domain filter.
560 SetClearingFinished(false);
561 client.ClearSiteData(
562 profile(), origin, true /* cookies */, false /* storage */,
563 false /* cache */,
564 base::Bind(
565 &ChromeContentBrowserClientClearSiteDataTest::SetClearingFinished,
566 base::Unretained(this), true));
567 EXPECT_TRUE(IsClearingFinished());
568
569 // One removal task: deleting cache with a domain filter.
570 SetClearingFinished(false);
571 client.ClearSiteData(
572 profile(), origin, false /* cookies */, false /* storage */,
573 true /* cache */,
574 base::Bind(
575 &ChromeContentBrowserClientClearSiteDataTest::SetClearingFinished,
576 base::Unretained(this), true));
577 EXPECT_TRUE(IsClearingFinished());
578
579 // Two removal tasks, with domain and origin filters respectively.
580 SetClearingFinished(false);
581 client.ClearSiteData(
582 profile(), origin, true /* cookies */, false /* storage */,
583 true /* cache */,
584 base::Bind(
585 &ChromeContentBrowserClientClearSiteDataTest::SetClearingFinished,
586 base::Unretained(this), true));
587 EXPECT_TRUE(IsClearingFinished());
588}
589
590} // namespace