OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chrome_content_browser_client.h" | 5 #include "chrome/browser/chrome_content_browser_client.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/ptr_util.h" | |
12 #include "base/message_loop/message_loop.h" | |
11 #include "base/metrics/field_trial.h" | 13 #include "base/metrics/field_trial.h" |
12 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
13 #include "build/build_config.h" | 15 #include "build/build_config.h" |
16 #include "chrome/browser/browsing_data/browsing_data_filter_builder.h" | |
17 #include "chrome/browser/browsing_data/browsing_data_helper.h" | |
18 #include "chrome/browser/browsing_data/browsing_data_remover.h" | |
19 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h" | |
20 #include "chrome/browser/browsing_data/origin_filter_builder.h" | |
21 #include "chrome/browser/browsing_data/registrable_domain_filter_builder.h" | |
14 #include "chrome/browser/search_engines/template_url_service_factory.h" | 22 #include "chrome/browser/search_engines/template_url_service_factory.h" |
23 #include "chrome/test/base/testing_profile.h" | |
15 #include "components/content_settings/core/browser/host_content_settings_map.h" | 24 #include "components/content_settings/core/browser/host_content_settings_map.h" |
16 #include "components/search_engines/template_url_service.h" | 25 #include "components/search_engines/template_url_service.h" |
17 #include "components/variations/entropy_provider.h" | 26 #include "components/variations/entropy_provider.h" |
18 #include "components/variations/variations_associated_data.h" | 27 #include "components/variations/variations_associated_data.h" |
19 #include "components/version_info/version_info.h" | 28 #include "components/version_info/version_info.h" |
20 #include "content/public/browser/navigation_controller.h" | 29 #include "content/public/browser/navigation_controller.h" |
21 #include "content/public/browser/navigation_entry.h" | 30 #include "content/public/browser/navigation_entry.h" |
22 #include "content/public/browser/web_contents.h" | 31 #include "content/public/browser/web_contents.h" |
23 #include "content/public/common/content_switches.h" | 32 #include "content/public/common/content_switches.h" |
24 #include "content/public/test/test_browser_thread_bundle.h" | 33 #include "content/public/test/test_browser_thread_bundle.h" |
34 #include "testing/gmock/include/gmock/gmock.h" | |
25 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
26 #include "url/gurl.h" | 36 #include "url/gurl.h" |
27 | 37 |
28 #if !defined(OS_ANDROID) | 38 #if !defined(OS_ANDROID) |
29 #include "chrome/browser/ui/browser.h" | 39 #include "chrome/browser/ui/browser.h" |
30 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 40 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
31 #include "chrome/common/pref_names.h" | 41 #include "chrome/common/pref_names.h" |
32 #include "chrome/test/base/browser_with_test_window_test.h" | 42 #include "chrome/test/base/browser_with_test_window_test.h" |
33 #include "chrome/test/base/search_test_utils.h" | 43 #include "chrome/test/base/search_test_utils.h" |
34 #endif | 44 #endif |
35 | 45 |
46 using testing::_; | |
36 using ChromeContentBrowserClientTest = testing::Test; | 47 using ChromeContentBrowserClientTest = testing::Test; |
37 | 48 |
38 TEST_F(ChromeContentBrowserClientTest, ShouldAssignSiteForURL) { | 49 TEST_F(ChromeContentBrowserClientTest, ShouldAssignSiteForURL) { |
39 ChromeContentBrowserClient client; | 50 ChromeContentBrowserClient client; |
40 EXPECT_FALSE(client.ShouldAssignSiteForURL(GURL("chrome-native://test"))); | 51 EXPECT_FALSE(client.ShouldAssignSiteForURL(GURL("chrome-native://test"))); |
41 EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("http://www.google.com"))); | 52 EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("http://www.google.com"))); |
42 EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("https://www.google.com"))); | 53 EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("https://www.google.com"))); |
43 } | 54 } |
44 | 55 |
45 // BrowserWithTestWindowTest doesn't work on Android. | 56 // BrowserWithTestWindowTest doesn't work on Android. |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
321 | 332 |
322 NavigationEntry* entry = browser()->tab_strip_model()-> | 333 NavigationEntry* entry = browser()->tab_strip_model()-> |
323 GetActiveWebContents()->GetController().GetLastCommittedEntry(); | 334 GetActiveWebContents()->GetController().GetLastCommittedEntry(); |
324 ASSERT_TRUE(entry != NULL); | 335 ASSERT_TRUE(entry != NULL); |
325 EXPECT_EQ(url_rewritten, entry->GetURL()); | 336 EXPECT_EQ(url_rewritten, entry->GetURL()); |
326 EXPECT_EQ(url_original, entry->GetVirtualURL()); | 337 EXPECT_EQ(url_original, entry->GetVirtualURL()); |
327 } | 338 } |
328 | 339 |
329 } // namespace content | 340 } // namespace content |
330 #endif // !defined(OS_ANDROID) | 341 #endif // !defined(OS_ANDROID) |
342 | |
343 namespace { | |
344 | |
345 // Tests for ChromeContentBrowserClient::ClearSiteData(). | |
346 class ChromeContentBrowserClientClearSiteDataTest : public testing::Test { | |
347 public: | |
348 class MockBrowsingDataRemover : public BrowsingDataRemover { | |
349 public: | |
350 explicit MockBrowsingDataRemover(content::BrowserContext* context) | |
351 : BrowsingDataRemover(context) {} | |
352 ~MockBrowsingDataRemover() {} | |
353 | |
354 MOCK_METHOD4(RemoveWithFilter, void( | |
355 const TimeRange& time_range, int remove_mask, int origin_type_mask, | |
356 const BrowsingDataFilterBuilder& filter_builder)); | |
357 }; | |
358 | |
359 void SetUp() override { | |
360 BrowsingDataRemoverFactory::GetInstance()->SetTestingFactoryAndUse( | |
361 &profile_, &ChromeContentBrowserClientClearSiteDataTest::GetRemover); | |
362 } | |
363 | |
364 content::BrowserContext* profile() { | |
365 return &profile_; | |
366 } | |
367 | |
368 MockBrowsingDataRemover* remover() { | |
369 return static_cast<MockBrowsingDataRemover*>( | |
370 BrowsingDataRemoverFactory::GetForBrowserContext(&profile_)); | |
371 } | |
372 | |
373 private: | |
374 static std::unique_ptr<KeyedService> GetRemover( | |
375 content::BrowserContext* context) { | |
376 return base::WrapUnique(new MockBrowsingDataRemover(context)); | |
377 } | |
378 | |
379 base::MessageLoop loop_; | |
380 TestingProfile profile_; | |
381 }; | |
382 | |
383 // Matcher for BrowsingDataFilterBuilder. As it is not possible to compare | |
384 // two BrowsingDataFilterBuilder-s of different kinds, it can only be used | |
385 // when we know which subclass of BrowsingDataFilterBuilder we expect: | |
386 // OriginFilterBuilder or RegistrableDomainFilterBuilder. | |
387 class SameBuilderMatcher : | |
388 public testing::MatcherInterface<const BrowsingDataFilterBuilder&> { | |
389 public: | |
390 explicit SameBuilderMatcher(const OriginFilterBuilder& builder) | |
391 : origin_filter_builder_(&builder), domain_filter_builder_(nullptr) { | |
392 } | |
393 | |
394 explicit SameBuilderMatcher(const RegistrableDomainFilterBuilder& builder) | |
395 : origin_filter_builder_(nullptr), domain_filter_builder_(&builder) { | |
396 } | |
397 | |
398 virtual bool MatchAndExplain(const BrowsingDataFilterBuilder& builder, | |
399 testing::MatchResultListener* listener) const { | |
400 DCHECK(origin_filter_builder_ || domain_filter_builder_); | |
401 DCHECK(!origin_filter_builder_ || !domain_filter_builder_); | |
Mike West
2016/07/18 13:35:48
Hrm. It's not clear to me that this is better than
msramek
2016/07/18 17:03:20
Well, there is no DCHECK_FALSE macro :) And I don'
| |
402 | |
403 if (origin_filter_builder_) { | |
404 return *origin_filter_builder_ == | |
405 static_cast<const OriginFilterBuilder&>(builder); | |
406 } | |
407 | |
408 if (domain_filter_builder_) { | |
409 return *domain_filter_builder_ == | |
410 static_cast<const RegistrableDomainFilterBuilder&>(builder); | |
411 } | |
412 | |
413 NOTREACHED(); | |
414 return false; | |
415 } | |
416 | |
417 void DescribeTo(std::ostream* os) const override { | |
418 *os << "has the same scope, mode, and added entries as the expected " | |
419 << "filter builder."; | |
420 } | |
421 | |
422 void DescribeNegationTo(std::ostream* os) const override { | |
423 *os << "differs from the expected filter builder in scope, mode, " | |
424 << "or added entries."; | |
425 } | |
426 | |
427 private: | |
428 const OriginFilterBuilder* origin_filter_builder_; | |
429 const RegistrableDomainFilterBuilder* domain_filter_builder_; | |
430 }; | |
431 | |
432 // Tests that the parameters to ClearBrowsingData() are translated to | |
433 // the correct BrowsingDataRemover::RemoveWithFilter() operation. The fourth | |
434 // parameter, |filter_builder|, is tested in detail in the RegistrableDomains | |
435 // test below. | |
436 TEST_F(ChromeContentBrowserClientClearSiteDataTest, Parameters) { | |
437 ChromeContentBrowserClient client; | |
438 | |
439 struct TestCase { | |
440 bool cookies; | |
441 bool storage; | |
442 bool cache; | |
443 int mask; | |
444 } test_cases[] = { | |
445 { false, false, false, 0 }, | |
446 { true, false, false, | |
447 BrowsingDataRemover::REMOVE_COOKIES | | |
448 BrowsingDataRemover::REMOVE_CHANNEL_IDS }, | |
449 { false, true, false, | |
450 BrowsingDataRemover::REMOVE_SITE_DATA & | |
451 ~BrowsingDataRemover::REMOVE_COOKIES & | |
452 ~BrowsingDataRemover::REMOVE_CHANNEL_IDS }, | |
453 { false, false, true, BrowsingDataRemover::REMOVE_CACHE }, | |
454 { true, true, false, BrowsingDataRemover::REMOVE_SITE_DATA }, | |
455 { true, false, true, | |
456 BrowsingDataRemover::REMOVE_COOKIES | | |
457 BrowsingDataRemover::REMOVE_CHANNEL_IDS | | |
458 BrowsingDataRemover::REMOVE_CACHE }, | |
459 { false, true, true, | |
460 BrowsingDataRemover::REMOVE_CACHE | | |
461 (BrowsingDataRemover::REMOVE_SITE_DATA & | |
462 ~BrowsingDataRemover::REMOVE_COOKIES & | |
463 ~BrowsingDataRemover::REMOVE_CHANNEL_IDS) }, | |
464 { true, true, true, | |
465 BrowsingDataRemover::REMOVE_SITE_DATA | | |
466 BrowsingDataRemover::REMOVE_CACHE }, | |
467 }; | |
468 | |
469 for (const TestCase& test_case : test_cases) { | |
470 SCOPED_TRACE(test_case.mask); | |
471 | |
472 // We always delete data for all time and all origin types. | |
473 BrowsingDataRemover::TimeRange all_time(base::Time(), base::Time::Max()); | |
474 BrowsingDataHelper::OriginTypeMask all_origin_types = | |
475 BrowsingDataHelper::ALL; | |
476 | |
477 // Some data are deleted for the origin and some for the registrable domain. | |
478 // Depending on the chosen datatypes, this might result into one or two | |
479 // calls. In the latter case, the removal mask will be split into two | |
480 // parts - one for the origin deletion and one for the registrable domain. | |
481 const int domain_scoped_types = | |
482 BrowsingDataRemover::REMOVE_COOKIES | | |
483 BrowsingDataRemover::REMOVE_CHANNEL_IDS; | |
484 int origin_deletion_mask = | |
485 test_case.mask & ~domain_scoped_types; | |
486 int registrable_domain_deletion_mask = | |
487 test_case.mask & domain_scoped_types; | |
488 | |
489 EXPECT_CALL( | |
490 *remover(), | |
491 RemoveWithFilter( | |
492 all_time, | |
493 origin_deletion_mask, | |
494 all_origin_types, | |
495 _)).Times(origin_deletion_mask != 0); | |
496 | |
497 EXPECT_CALL( | |
498 *remover(), | |
499 RemoveWithFilter( | |
500 all_time, | |
501 registrable_domain_deletion_mask, | |
502 all_origin_types, | |
503 _)).Times(registrable_domain_deletion_mask != 0); | |
504 | |
505 client.ClearSiteData( | |
506 profile(), url::Origin(GURL("https://www.example.com")), | |
507 test_case.cookies, test_case.storage, test_case.cache); | |
508 | |
509 testing::Mock::VerifyAndClearExpectations(remover()); | |
510 } | |
511 } | |
512 | |
513 // Tests that ClearBrowsingData() called for an origin deletes cookies in the | |
514 // scope of the registrable domain corresponding to that origin, while cache | |
515 // is deleted for that exact origin. | |
516 TEST_F(ChromeContentBrowserClientClearSiteDataTest, RegistrableDomains) { | |
517 ChromeContentBrowserClient client; | |
518 | |
519 struct TestCase { | |
520 const char* origin; // origin on which ClearSiteData() is called. | |
521 const char* domain; // domain on which cookies will be deleted. | |
522 } test_cases[] = { | |
523 // TLD has no embedded dot. | |
524 { "https://example.com", "example.com" }, | |
525 { "https://www.example.com", "example.com" }, | |
526 { "https://www.fourth.third.second.com", "second.com" }, | |
527 | |
528 // TLD has one embedded dot. | |
529 { "https://www.example.co.uk", "example.co.uk" }, | |
530 { "https://example.co.uk", "example.co.uk" }, | |
531 | |
532 // TLD has more embedded dots. | |
533 { "https://www.website.sp.nom.br", "website.sp.nom.br" }, | |
534 | |
535 // IP addresses. | |
536 { "http://127.0.0.1", "127.0.0.1" }, | |
537 { "http://192.168.0.1", "192.168.0.1" }, | |
538 { "http://192.168.0.1", "192.168.0.1" }, | |
539 | |
540 // Internal hostnames. | |
541 { "http://localhost", "localhost" }, | |
542 { "http://fileserver", "fileserver" }, | |
543 | |
544 // These are not subdomains of internal hostnames, but subdomain of | |
545 // unknown TLDs. | |
546 { "http://subdomain.localhost", "subdomain.localhost" }, | |
547 { "http://www.subdomain.localhost", "subdomain.localhost" }, | |
548 { "http://documents.fileserver", "documents.fileserver" }, | |
549 | |
550 // Scheme and port don't matter. | |
551 { "http://example.com", "example.com" }, | |
552 { "http://example.com:8080", "example.com" }, | |
553 { "https://example.com:4433", "example.com" }, | |
554 }; | |
555 | |
556 for (const TestCase& test_case : test_cases) { | |
557 SCOPED_TRACE(test_case.origin); | |
558 | |
559 OriginFilterBuilder origin_filter_builder( | |
560 BrowsingDataFilterBuilder::WHITELIST); | |
561 origin_filter_builder.AddOrigin(url::Origin(GURL(test_case.origin))); | |
562 | |
563 EXPECT_CALL( | |
564 *remover(), | |
565 RemoveWithFilter( | |
566 _, | |
567 BrowsingDataRemover::REMOVE_CACHE, | |
568 _, | |
569 MakeMatcher(new SameBuilderMatcher(origin_filter_builder)))); | |
570 | |
571 RegistrableDomainFilterBuilder registrable_domain_filter_builder( | |
572 BrowsingDataFilterBuilder::WHITELIST); | |
573 registrable_domain_filter_builder.AddRegisterableDomain(test_case.domain); | |
574 | |
575 EXPECT_CALL(*remover(), | |
576 RemoveWithFilter( | |
577 _, | |
578 BrowsingDataRemover::REMOVE_COOKIES | | |
579 BrowsingDataRemover::REMOVE_CHANNEL_IDS, | |
580 _, | |
581 MakeMatcher(new SameBuilderMatcher( | |
582 registrable_domain_filter_builder)))); | |
583 | |
584 client.ClearSiteData( | |
585 profile(), url::Origin(GURL(test_case.origin)), | |
586 true /* cookies */, false /* storage */, true /* cache */); | |
587 | |
588 testing::Mock::VerifyAndClearExpectations(remover()); | |
589 } | |
590 } | |
591 | |
592 } // namespace | |
OLD | NEW |