Home | History | Annotate | Download | only in browsing_data
      1 // Copyright (c) 2012 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/browsing_data/browsing_data_remover.h"
      6 
      7 #include <set>
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/bind.h"
     12 #include "base/bind_helpers.h"
     13 #include "base/files/file_path.h"
     14 #include "base/files/file_util.h"
     15 #include "base/guid.h"
     16 #include "base/message_loop/message_loop.h"
     17 #include "base/prefs/testing_pref_service.h"
     18 #include "base/strings/utf_string_conversions.h"
     19 #include "base/task/cancelable_task_tracker.h"
     20 #include "chrome/browser/autofill/personal_data_manager_factory.h"
     21 #include "chrome/browser/browsing_data/browsing_data_helper.h"
     22 #include "chrome/browser/browsing_data/browsing_data_remover_test_util.h"
     23 #include "chrome/browser/chrome_notification_types.h"
     24 #include "chrome/browser/domain_reliability/service_factory.h"
     25 #include "chrome/browser/history/history_service.h"
     26 #include "chrome/browser/history/history_service_factory.h"
     27 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
     28 #include "chrome/common/pref_names.h"
     29 #include "chrome/test/base/testing_browser_process.h"
     30 #include "chrome/test/base/testing_profile.h"
     31 #include "components/autofill/core/browser/autofill_profile.h"
     32 #include "components/autofill/core/browser/autofill_test_utils.h"
     33 #include "components/autofill/core/browser/credit_card.h"
     34 #include "components/autofill/core/browser/personal_data_manager.h"
     35 #include "components/autofill/core/browser/personal_data_manager_observer.h"
     36 #include "components/domain_reliability/clear_mode.h"
     37 #include "components/domain_reliability/monitor.h"
     38 #include "components/domain_reliability/service.h"
     39 #include "content/public/browser/cookie_store_factory.h"
     40 #include "content/public/browser/dom_storage_context.h"
     41 #include "content/public/browser/local_storage_usage_info.h"
     42 #include "content/public/browser/notification_service.h"
     43 #include "content/public/browser/storage_partition.h"
     44 #include "content/public/test/test_browser_thread.h"
     45 #include "content/public/test/test_browser_thread_bundle.h"
     46 #include "content/public/test/test_utils.h"
     47 #include "net/cookies/cookie_store.h"
     48 #include "net/ssl/channel_id_service.h"
     49 #include "net/ssl/channel_id_store.h"
     50 #include "net/ssl/ssl_client_cert_type.h"
     51 #include "net/url_request/url_request_context.h"
     52 #include "net/url_request/url_request_context_getter.h"
     53 #include "testing/gmock/include/gmock/gmock.h"
     54 #include "testing/gtest/include/gtest/gtest.h"
     55 
     56 #if defined(OS_CHROMEOS)
     57 #include "chrome/browser/chromeos/login/users/mock_user_manager.h"
     58 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
     59 #include "chrome/browser/chromeos/settings/cros_settings.h"
     60 #include "chrome/browser/chromeos/settings/device_settings_service.h"
     61 #include "chromeos/dbus/dbus_thread_manager.h"
     62 #include "chromeos/dbus/mock_cryptohome_client.h"
     63 #endif
     64 
     65 #if defined(ENABLE_EXTENSIONS)
     66 #include "chrome/browser/extensions/mock_extension_special_storage_policy.h"
     67 #endif
     68 
     69 class MockExtensionSpecialStoragePolicy;
     70 
     71 using content::BrowserThread;
     72 using content::StoragePartition;
     73 using domain_reliability::CLEAR_BEACONS;
     74 using domain_reliability::CLEAR_CONTEXTS;
     75 using domain_reliability::DomainReliabilityClearMode;
     76 using domain_reliability::DomainReliabilityMonitor;
     77 using domain_reliability::DomainReliabilityService;
     78 using domain_reliability::DomainReliabilityServiceFactory;
     79 using testing::_;
     80 using testing::Invoke;
     81 using testing::WithArgs;
     82 
     83 namespace {
     84 
     85 const char kTestOrigin1[] = "http://host1:1/";
     86 const char kTestOrigin2[] = "http://host2:1/";
     87 const char kTestOrigin3[] = "http://host3:1/";
     88 const char kTestOriginExt[] = "chrome-extension://abcdefghijklmnopqrstuvwxyz/";
     89 const char kTestOriginDevTools[] = "chrome-devtools://abcdefghijklmnopqrstuvw/";
     90 
     91 // For Autofill.
     92 const char kChromeOrigin[] = "Chrome settings";
     93 const char kWebOrigin[] = "https://www.example.com/";
     94 
     95 const GURL kOrigin1(kTestOrigin1);
     96 const GURL kOrigin2(kTestOrigin2);
     97 const GURL kOrigin3(kTestOrigin3);
     98 const GURL kOriginExt(kTestOriginExt);
     99 const GURL kOriginDevTools(kTestOriginDevTools);
    100 
    101 const base::FilePath::CharType kDomStorageOrigin1[] =
    102     FILE_PATH_LITERAL("http_host1_1.localstorage");
    103 
    104 const base::FilePath::CharType kDomStorageOrigin2[] =
    105     FILE_PATH_LITERAL("http_host2_1.localstorage");
    106 
    107 const base::FilePath::CharType kDomStorageOrigin3[] =
    108     FILE_PATH_LITERAL("http_host3_1.localstorage");
    109 
    110 const base::FilePath::CharType kDomStorageExt[] = FILE_PATH_LITERAL(
    111     "chrome-extension_abcdefghijklmnopqrstuvwxyz_0.localstorage");
    112 
    113 #if defined(OS_CHROMEOS)
    114 void FakeDBusCall(const chromeos::BoolDBusMethodCallback& callback) {
    115   base::MessageLoop::current()->PostTask(
    116       FROM_HERE,
    117       base::Bind(callback, chromeos::DBUS_METHOD_CALL_SUCCESS, true));
    118 }
    119 #endif
    120 
    121 struct StoragePartitionRemovalData {
    122   uint32 remove_mask;
    123   uint32 quota_storage_remove_mask;
    124   GURL remove_origin;
    125   base::Time remove_begin;
    126   base::Time remove_end;
    127   StoragePartition::OriginMatcherFunction origin_matcher;
    128 
    129   StoragePartitionRemovalData() : remove_mask(0),
    130                                   quota_storage_remove_mask(0) {}
    131 };
    132 
    133 class TestStoragePartition : public StoragePartition {
    134  public:
    135   TestStoragePartition() {}
    136   virtual ~TestStoragePartition() {}
    137 
    138   // content::StoragePartition implementation.
    139   virtual base::FilePath GetPath() OVERRIDE { return base::FilePath(); }
    140   virtual net::URLRequestContextGetter* GetURLRequestContext() OVERRIDE {
    141     return NULL;
    142   }
    143   virtual net::URLRequestContextGetter* GetMediaURLRequestContext() OVERRIDE {
    144     return NULL;
    145   }
    146   virtual storage::QuotaManager* GetQuotaManager() OVERRIDE { return NULL; }
    147   virtual content::AppCacheService* GetAppCacheService() OVERRIDE {
    148     return NULL;
    149   }
    150   virtual storage::FileSystemContext* GetFileSystemContext() OVERRIDE {
    151     return NULL;
    152   }
    153   virtual storage::DatabaseTracker* GetDatabaseTracker() OVERRIDE {
    154     return NULL;
    155   }
    156   virtual content::DOMStorageContext* GetDOMStorageContext() OVERRIDE {
    157     return NULL;
    158   }
    159   virtual content::IndexedDBContext* GetIndexedDBContext() OVERRIDE {
    160     return NULL;
    161   }
    162   virtual content::ServiceWorkerContext* GetServiceWorkerContext() OVERRIDE {
    163     return NULL;
    164   }
    165 
    166   virtual void ClearDataForOrigin(uint32 remove_mask,
    167                                   uint32 quota_storage_remove_mask,
    168                                   const GURL& storage_origin,
    169                                   net::URLRequestContextGetter* rq_context,
    170                                   const base::Closure& callback) OVERRIDE {
    171     BrowserThread::PostTask(BrowserThread::UI,
    172                             FROM_HERE,
    173                             base::Bind(&TestStoragePartition::AsyncRunCallback,
    174                                        base::Unretained(this),
    175                                        callback));
    176   }
    177 
    178   virtual void ClearData(uint32 remove_mask,
    179                          uint32 quota_storage_remove_mask,
    180                          const GURL& storage_origin,
    181                          const OriginMatcherFunction& origin_matcher,
    182                          const base::Time begin,
    183                          const base::Time end,
    184                          const base::Closure& callback) OVERRIDE {
    185     // Store stuff to verify parameters' correctness later.
    186     storage_partition_removal_data_.remove_mask = remove_mask;
    187     storage_partition_removal_data_.quota_storage_remove_mask =
    188         quota_storage_remove_mask;
    189     storage_partition_removal_data_.remove_origin = storage_origin;
    190     storage_partition_removal_data_.remove_begin = begin;
    191     storage_partition_removal_data_.remove_end = end;
    192     storage_partition_removal_data_.origin_matcher = origin_matcher;
    193 
    194     BrowserThread::PostTask(
    195         BrowserThread::UI,
    196         FROM_HERE,
    197         base::Bind(&TestStoragePartition::AsyncRunCallback,
    198                    base::Unretained(this), callback));
    199   }
    200 
    201   StoragePartitionRemovalData GetStoragePartitionRemovalData() {
    202     return storage_partition_removal_data_;
    203   }
    204 
    205  private:
    206   void AsyncRunCallback(const base::Closure& callback) {
    207     callback.Run();
    208   }
    209 
    210   StoragePartitionRemovalData storage_partition_removal_data_;
    211 
    212   DISALLOW_COPY_AND_ASSIGN(TestStoragePartition);
    213 };
    214 
    215 }  // namespace
    216 
    217 // Testers -------------------------------------------------------------------
    218 
    219 class RemoveCookieTester {
    220  public:
    221   RemoveCookieTester() : get_cookie_success_(false), cookie_store_(NULL) {
    222   }
    223 
    224   // Returns true, if the given cookie exists in the cookie store.
    225   bool ContainsCookie() {
    226     scoped_refptr<content::MessageLoopRunner> message_loop_runner =
    227         new content::MessageLoopRunner;
    228     quit_closure_ = message_loop_runner->QuitClosure();
    229     get_cookie_success_ = false;
    230     cookie_store_->GetCookiesWithOptionsAsync(
    231         kOrigin1, net::CookieOptions(),
    232         base::Bind(&RemoveCookieTester::GetCookieCallback,
    233                    base::Unretained(this)));
    234     message_loop_runner->Run();
    235     return get_cookie_success_;
    236   }
    237 
    238   void AddCookie() {
    239     scoped_refptr<content::MessageLoopRunner> message_loop_runner =
    240         new content::MessageLoopRunner;
    241     quit_closure_ = message_loop_runner->QuitClosure();
    242     cookie_store_->SetCookieWithOptionsAsync(
    243         kOrigin1, "A=1", net::CookieOptions(),
    244         base::Bind(&RemoveCookieTester::SetCookieCallback,
    245                    base::Unretained(this)));
    246     message_loop_runner->Run();
    247   }
    248 
    249  protected:
    250   void SetMonster(net::CookieStore* monster) {
    251     cookie_store_ = monster;
    252   }
    253 
    254  private:
    255   void GetCookieCallback(const std::string& cookies) {
    256     if (cookies == "A=1") {
    257       get_cookie_success_ = true;
    258     } else {
    259       EXPECT_EQ("", cookies);
    260       get_cookie_success_ = false;
    261     }
    262     quit_closure_.Run();
    263   }
    264 
    265   void SetCookieCallback(bool result) {
    266     ASSERT_TRUE(result);
    267     quit_closure_.Run();
    268   }
    269 
    270   bool get_cookie_success_;
    271   base::Closure quit_closure_;
    272   net::CookieStore* cookie_store_;
    273 
    274   DISALLOW_COPY_AND_ASSIGN(RemoveCookieTester);
    275 };
    276 
    277 #if defined(FULL_SAFE_BROWSING) || defined(MOBILE_SAFE_BROWSING)
    278 class RemoveSafeBrowsingCookieTester : public RemoveCookieTester {
    279  public:
    280   RemoveSafeBrowsingCookieTester()
    281       : browser_process_(TestingBrowserProcess::GetGlobal()) {
    282     scoped_refptr<SafeBrowsingService> sb_service =
    283         SafeBrowsingService::CreateSafeBrowsingService();
    284     browser_process_->SetSafeBrowsingService(sb_service.get());
    285     sb_service->Initialize();
    286     base::MessageLoop::current()->RunUntilIdle();
    287 
    288     // Create a cookiemonster that does not have persistant storage, and replace
    289     // the SafeBrowsingService created one with it.
    290     net::CookieStore* monster =
    291         content::CreateCookieStore(content::CookieStoreConfig());
    292     sb_service->url_request_context()->GetURLRequestContext()->
    293         set_cookie_store(monster);
    294     SetMonster(monster);
    295   }
    296 
    297   virtual ~RemoveSafeBrowsingCookieTester() {
    298     browser_process_->safe_browsing_service()->ShutDown();
    299     base::MessageLoop::current()->RunUntilIdle();
    300     browser_process_->SetSafeBrowsingService(NULL);
    301   }
    302 
    303  private:
    304   TestingBrowserProcess* browser_process_;
    305 
    306   DISALLOW_COPY_AND_ASSIGN(RemoveSafeBrowsingCookieTester);
    307 };
    308 #endif
    309 
    310 class RemoveChannelIDTester : public net::SSLConfigService::Observer {
    311  public:
    312   explicit RemoveChannelIDTester(TestingProfile* profile)
    313       : ssl_config_changed_count_(0) {
    314     channel_id_service_ = profile->GetRequestContext()->
    315         GetURLRequestContext()->channel_id_service();
    316     ssl_config_service_ = profile->GetSSLConfigService();
    317     ssl_config_service_->AddObserver(this);
    318   }
    319 
    320   virtual ~RemoveChannelIDTester() {
    321     ssl_config_service_->RemoveObserver(this);
    322   }
    323 
    324   int ChannelIDCount() {
    325     return channel_id_service_->cert_count();
    326   }
    327 
    328   // Add a server bound cert for |server| with specific creation and expiry
    329   // times.  The cert and key data will be filled with dummy values.
    330   void AddChannelIDWithTimes(const std::string& server_identifier,
    331                                    base::Time creation_time,
    332                                    base::Time expiration_time) {
    333     GetChannelIDStore()->SetChannelID(server_identifier,
    334                                       creation_time,
    335                                       expiration_time,
    336                                       "a",
    337                                       "b");
    338   }
    339 
    340   // Add a server bound cert for |server|, with the current time as the
    341   // creation time.  The cert and key data will be filled with dummy values.
    342   void AddChannelID(const std::string& server_identifier) {
    343     base::Time now = base::Time::Now();
    344     AddChannelIDWithTimes(server_identifier,
    345                           now,
    346                           now + base::TimeDelta::FromDays(1));
    347   }
    348 
    349   void GetChannelIDList(net::ChannelIDStore::ChannelIDList* channel_ids) {
    350     GetChannelIDStore()->GetAllChannelIDs(
    351         base::Bind(&RemoveChannelIDTester::GetAllChannelIDsCallback,
    352                    channel_ids));
    353   }
    354 
    355   net::ChannelIDStore* GetChannelIDStore() {
    356     return channel_id_service_->GetChannelIDStore();
    357   }
    358 
    359   int ssl_config_changed_count() const {
    360     return ssl_config_changed_count_;
    361   }
    362 
    363   // net::SSLConfigService::Observer implementation:
    364   virtual void OnSSLConfigChanged() OVERRIDE {
    365     ssl_config_changed_count_++;
    366   }
    367 
    368  private:
    369   static void GetAllChannelIDsCallback(
    370       net::ChannelIDStore::ChannelIDList* dest,
    371       const net::ChannelIDStore::ChannelIDList& result) {
    372     *dest = result;
    373   }
    374 
    375   net::ChannelIDService* channel_id_service_;
    376   scoped_refptr<net::SSLConfigService> ssl_config_service_;
    377   int ssl_config_changed_count_;
    378 
    379   DISALLOW_COPY_AND_ASSIGN(RemoveChannelIDTester);
    380 };
    381 
    382 class RemoveHistoryTester {
    383  public:
    384   RemoveHistoryTester() : query_url_success_(false), history_service_(NULL) {}
    385 
    386   bool Init(TestingProfile* profile) WARN_UNUSED_RESULT {
    387     if (!profile->CreateHistoryService(true, false))
    388       return false;
    389     history_service_ = HistoryServiceFactory::GetForProfile(
    390         profile, Profile::EXPLICIT_ACCESS);
    391     return true;
    392   }
    393 
    394   // Returns true, if the given URL exists in the history service.
    395   bool HistoryContainsURL(const GURL& url) {
    396     scoped_refptr<content::MessageLoopRunner> message_loop_runner =
    397         new content::MessageLoopRunner;
    398     quit_closure_ = message_loop_runner->QuitClosure();
    399     history_service_->QueryURL(
    400         url,
    401         true,
    402         base::Bind(&RemoveHistoryTester::SaveResultAndQuit,
    403                    base::Unretained(this)),
    404         &tracker_);
    405     message_loop_runner->Run();
    406     return query_url_success_;
    407   }
    408 
    409   void AddHistory(const GURL& url, base::Time time) {
    410     history_service_->AddPage(url, time, NULL, 0, GURL(),
    411         history::RedirectList(), ui::PAGE_TRANSITION_LINK,
    412         history::SOURCE_BROWSED, false);
    413   }
    414 
    415  private:
    416   // Callback for HistoryService::QueryURL.
    417   void SaveResultAndQuit(bool success,
    418                          const history::URLRow&,
    419                          const history::VisitVector&) {
    420     query_url_success_ = success;
    421     quit_closure_.Run();
    422   }
    423 
    424   // For History requests.
    425   base::CancelableTaskTracker tracker_;
    426   bool query_url_success_;
    427   base::Closure quit_closure_;
    428 
    429   // TestingProfile owns the history service; we shouldn't delete it.
    430   HistoryService* history_service_;
    431 
    432   DISALLOW_COPY_AND_ASSIGN(RemoveHistoryTester);
    433 };
    434 
    435 class RemoveAutofillTester : public autofill::PersonalDataManagerObserver {
    436  public:
    437   explicit RemoveAutofillTester(TestingProfile* profile)
    438       : personal_data_manager_(
    439             autofill::PersonalDataManagerFactory::GetForProfile(profile)) {
    440     autofill::test::DisableSystemServices(profile->GetPrefs());
    441     personal_data_manager_->AddObserver(this);
    442   }
    443 
    444   virtual ~RemoveAutofillTester() {
    445     personal_data_manager_->RemoveObserver(this);
    446   }
    447 
    448   // Returns true if there are autofill profiles.
    449   bool HasProfile() {
    450     return !personal_data_manager_->GetProfiles().empty() &&
    451            !personal_data_manager_->GetCreditCards().empty();
    452   }
    453 
    454   bool HasOrigin(const std::string& origin) {
    455     const std::vector<autofill::AutofillProfile*>& profiles =
    456         personal_data_manager_->GetProfiles();
    457     for (std::vector<autofill::AutofillProfile*>::const_iterator it =
    458              profiles.begin();
    459          it != profiles.end(); ++it) {
    460       if ((*it)->origin() == origin)
    461         return true;
    462     }
    463 
    464     const std::vector<autofill::CreditCard*>& credit_cards =
    465         personal_data_manager_->GetCreditCards();
    466     for (std::vector<autofill::CreditCard*>::const_iterator it =
    467              credit_cards.begin();
    468          it != credit_cards.end(); ++it) {
    469       if ((*it)->origin() == origin)
    470         return true;
    471     }
    472 
    473     return false;
    474   }
    475 
    476   // Add two profiles and two credit cards to the database.  In each pair, one
    477   // entry has a web origin and the other has a Chrome origin.
    478   void AddProfilesAndCards() {
    479     std::vector<autofill::AutofillProfile> profiles;
    480     autofill::AutofillProfile profile;
    481     profile.set_guid(base::GenerateGUID());
    482     profile.set_origin(kWebOrigin);
    483     profile.SetRawInfo(autofill::NAME_FIRST, base::ASCIIToUTF16("Bob"));
    484     profile.SetRawInfo(autofill::NAME_LAST, base::ASCIIToUTF16("Smith"));
    485     profile.SetRawInfo(autofill::ADDRESS_HOME_ZIP, base::ASCIIToUTF16("94043"));
    486     profile.SetRawInfo(autofill::EMAIL_ADDRESS,
    487                        base::ASCIIToUTF16("sue (at) example.com"));
    488     profile.SetRawInfo(autofill::COMPANY_NAME, base::ASCIIToUTF16("Company X"));
    489     profiles.push_back(profile);
    490 
    491     profile.set_guid(base::GenerateGUID());
    492     profile.set_origin(kChromeOrigin);
    493     profiles.push_back(profile);
    494 
    495     personal_data_manager_->SetProfiles(&profiles);
    496     base::MessageLoop::current()->Run();
    497 
    498     std::vector<autofill::CreditCard> cards;
    499     autofill::CreditCard card;
    500     card.set_guid(base::GenerateGUID());
    501     card.set_origin(kWebOrigin);
    502     card.SetRawInfo(autofill::CREDIT_CARD_NUMBER,
    503                     base::ASCIIToUTF16("1234-5678-9012-3456"));
    504     cards.push_back(card);
    505 
    506     card.set_guid(base::GenerateGUID());
    507     card.set_origin(kChromeOrigin);
    508     cards.push_back(card);
    509 
    510     personal_data_manager_->SetCreditCards(&cards);
    511     base::MessageLoop::current()->Run();
    512   }
    513 
    514  private:
    515   virtual void OnPersonalDataChanged() OVERRIDE {
    516     base::MessageLoop::current()->Quit();
    517   }
    518 
    519   autofill::PersonalDataManager* personal_data_manager_;
    520   DISALLOW_COPY_AND_ASSIGN(RemoveAutofillTester);
    521 };
    522 
    523 class RemoveLocalStorageTester {
    524  public:
    525   explicit RemoveLocalStorageTester(TestingProfile* profile)
    526       : profile_(profile), dom_storage_context_(NULL) {
    527     dom_storage_context_ =
    528         content::BrowserContext::GetDefaultStoragePartition(profile)->
    529             GetDOMStorageContext();
    530   }
    531 
    532   // Returns true, if the given origin URL exists.
    533   bool DOMStorageExistsForOrigin(const GURL& origin) {
    534     scoped_refptr<content::MessageLoopRunner> message_loop_runner =
    535         new content::MessageLoopRunner;
    536     quit_closure_ = message_loop_runner->QuitClosure();
    537     GetLocalStorageUsage();
    538     message_loop_runner->Run();
    539     for (size_t i = 0; i < infos_.size(); ++i) {
    540       if (origin == infos_[i].origin)
    541         return true;
    542     }
    543     return false;
    544   }
    545 
    546   void AddDOMStorageTestData() {
    547     // Note: This test depends on details of how the dom_storage library
    548     // stores data in the host file system.
    549     base::FilePath storage_path =
    550         profile_->GetPath().AppendASCII("Local Storage");
    551     base::CreateDirectory(storage_path);
    552 
    553     // Write some files.
    554     base::WriteFile(storage_path.Append(kDomStorageOrigin1), NULL, 0);
    555     base::WriteFile(storage_path.Append(kDomStorageOrigin2), NULL, 0);
    556     base::WriteFile(storage_path.Append(kDomStorageOrigin3), NULL, 0);
    557     base::WriteFile(storage_path.Append(kDomStorageExt), NULL, 0);
    558 
    559     // Tweak their dates.
    560     base::Time now = base::Time::Now();
    561     base::TouchFile(storage_path.Append(kDomStorageOrigin1), now, now);
    562 
    563     base::Time one_day_ago = now - base::TimeDelta::FromDays(1);
    564     base::TouchFile(storage_path.Append(kDomStorageOrigin2),
    565                     one_day_ago, one_day_ago);
    566 
    567     base::Time sixty_days_ago = now - base::TimeDelta::FromDays(60);
    568     base::TouchFile(storage_path.Append(kDomStorageOrigin3),
    569                     sixty_days_ago, sixty_days_ago);
    570 
    571     base::TouchFile(storage_path.Append(kDomStorageExt), now, now);
    572   }
    573 
    574  private:
    575   void GetLocalStorageUsage() {
    576     dom_storage_context_->GetLocalStorageUsage(
    577         base::Bind(&RemoveLocalStorageTester::OnGotLocalStorageUsage,
    578                    base::Unretained(this)));
    579   }
    580   void OnGotLocalStorageUsage(
    581       const std::vector<content::LocalStorageUsageInfo>& infos) {
    582     infos_ = infos;
    583     quit_closure_.Run();
    584   }
    585 
    586   // We don't own these pointers.
    587   TestingProfile* profile_;
    588   content::DOMStorageContext* dom_storage_context_;
    589 
    590   std::vector<content::LocalStorageUsageInfo> infos_;
    591   base::Closure quit_closure_;
    592 
    593   DISALLOW_COPY_AND_ASSIGN(RemoveLocalStorageTester);
    594 };
    595 
    596 class MockDomainReliabilityService : public DomainReliabilityService {
    597  public:
    598   MockDomainReliabilityService() : clear_count_(0) {}
    599 
    600   virtual ~MockDomainReliabilityService() {}
    601 
    602   virtual scoped_ptr<DomainReliabilityMonitor> CreateMonitor(
    603       scoped_refptr<base::SingleThreadTaskRunner> network_task_runner)
    604       OVERRIDE {
    605     NOTREACHED();
    606     return scoped_ptr<DomainReliabilityMonitor>();
    607   }
    608 
    609   virtual void ClearBrowsingData(DomainReliabilityClearMode clear_mode,
    610                                  const base::Closure& callback) OVERRIDE {
    611     clear_count_++;
    612     last_clear_mode_ = clear_mode;
    613     callback.Run();
    614   }
    615 
    616   virtual void GetWebUIData(
    617       const base::Callback<void(scoped_ptr<base::Value>)>& callback)
    618       const OVERRIDE {
    619     NOTREACHED();
    620   }
    621 
    622   int clear_count() const { return clear_count_; }
    623 
    624   DomainReliabilityClearMode last_clear_mode() const {
    625     return last_clear_mode_;
    626   }
    627 
    628  private:
    629   unsigned clear_count_;
    630   DomainReliabilityClearMode last_clear_mode_;
    631 };
    632 
    633 struct TestingDomainReliabilityServiceFactoryUserData
    634     : public base::SupportsUserData::Data {
    635   TestingDomainReliabilityServiceFactoryUserData(
    636       content::BrowserContext* context,
    637       MockDomainReliabilityService* service)
    638       : context(context),
    639         service(service),
    640         attached(false) {}
    641   virtual ~TestingDomainReliabilityServiceFactoryUserData() {}
    642 
    643   content::BrowserContext* const context;
    644   MockDomainReliabilityService* const service;
    645   bool attached;
    646 
    647   static const void* kKey;
    648 };
    649 
    650 // static
    651 const void* TestingDomainReliabilityServiceFactoryUserData::kKey =
    652     &TestingDomainReliabilityServiceFactoryUserData::kKey;
    653 
    654 KeyedService* TestingDomainReliabilityServiceFactoryFunction(
    655     content::BrowserContext* context) {
    656   const void* kKey = TestingDomainReliabilityServiceFactoryUserData::kKey;
    657 
    658   TestingDomainReliabilityServiceFactoryUserData* data =
    659       static_cast<TestingDomainReliabilityServiceFactoryUserData*>(
    660           context->GetUserData(kKey));
    661   EXPECT_TRUE(data);
    662   EXPECT_EQ(data->context, context);
    663   EXPECT_FALSE(data->attached);
    664 
    665   data->attached = true;
    666   return data->service;
    667 }
    668 
    669 class ClearDomainReliabilityTester {
    670  public:
    671   explicit ClearDomainReliabilityTester(TestingProfile* profile) :
    672       profile_(profile),
    673       mock_service_(new MockDomainReliabilityService()) {
    674     AttachService();
    675   }
    676 
    677   unsigned clear_count() { return mock_service_->clear_count(); }
    678 
    679   DomainReliabilityClearMode last_clear_mode() {
    680     return mock_service_->last_clear_mode();
    681   }
    682 
    683  private:
    684   void AttachService() {
    685     const void* kKey = TestingDomainReliabilityServiceFactoryUserData::kKey;
    686 
    687     // Attach kludgey UserData struct to profile.
    688     TestingDomainReliabilityServiceFactoryUserData* data =
    689         new TestingDomainReliabilityServiceFactoryUserData(profile_,
    690                                                            mock_service_);
    691     EXPECT_FALSE(profile_->GetUserData(kKey));
    692     profile_->SetUserData(kKey, data);
    693 
    694     // Set and use factory that will attach service stuffed in kludgey struct.
    695     DomainReliabilityServiceFactory::GetInstance()->SetTestingFactoryAndUse(
    696         profile_,
    697         &TestingDomainReliabilityServiceFactoryFunction);
    698 
    699     // Verify and detach kludgey struct.
    700     EXPECT_EQ(data, profile_->GetUserData(kKey));
    701     EXPECT_TRUE(data->attached);
    702     profile_->RemoveUserData(kKey);
    703   }
    704 
    705   TestingProfile* profile_;
    706   MockDomainReliabilityService* mock_service_;
    707 };
    708 
    709 // Test Class ----------------------------------------------------------------
    710 
    711 class BrowsingDataRemoverTest : public testing::Test,
    712                                 public content::NotificationObserver {
    713  public:
    714   BrowsingDataRemoverTest()
    715       : profile_(new TestingProfile()) {
    716     registrar_.Add(this, chrome::NOTIFICATION_BROWSING_DATA_REMOVED,
    717                    content::Source<Profile>(profile_.get()));
    718   }
    719 
    720   virtual ~BrowsingDataRemoverTest() {
    721   }
    722 
    723   virtual void TearDown() {
    724 #if defined(ENABLE_EXTENSIONS)
    725     mock_policy_ = NULL;
    726 #endif
    727 
    728     // TestingProfile contains a DOMStorageContext.  BrowserContext's destructor
    729     // posts a message to the WEBKIT thread to delete some of its member
    730     // variables. We need to ensure that the profile is destroyed, and that
    731     // the message loop is cleared out, before destroying the threads and loop.
    732     // Otherwise we leak memory.
    733     profile_.reset();
    734     base::MessageLoop::current()->RunUntilIdle();
    735   }
    736 
    737   void BlockUntilBrowsingDataRemoved(BrowsingDataRemover::TimePeriod period,
    738                                      int remove_mask,
    739                                      bool include_protected_origins) {
    740     BrowsingDataRemover* remover = BrowsingDataRemover::CreateForPeriod(
    741         profile_.get(), period);
    742 
    743     TestStoragePartition storage_partition;
    744     remover->OverrideStoragePartitionForTesting(&storage_partition);
    745 
    746     called_with_details_.reset(new BrowsingDataRemover::NotificationDetails());
    747 
    748     // BrowsingDataRemover deletes itself when it completes.
    749     int origin_set_mask = BrowsingDataHelper::UNPROTECTED_WEB;
    750     if (include_protected_origins)
    751       origin_set_mask |= BrowsingDataHelper::PROTECTED_WEB;
    752 
    753     BrowsingDataRemoverCompletionObserver completion_observer(remover);
    754     remover->Remove(remove_mask, origin_set_mask);
    755     completion_observer.BlockUntilCompletion();
    756 
    757     // Save so we can verify later.
    758     storage_partition_removal_data_ =
    759         storage_partition.GetStoragePartitionRemovalData();
    760   }
    761 
    762   void BlockUntilOriginDataRemoved(BrowsingDataRemover::TimePeriod period,
    763                                    int remove_mask,
    764                                    const GURL& remove_origin) {
    765     BrowsingDataRemover* remover = BrowsingDataRemover::CreateForPeriod(
    766         profile_.get(), period);
    767     TestStoragePartition storage_partition;
    768     remover->OverrideStoragePartitionForTesting(&storage_partition);
    769 
    770     called_with_details_.reset(new BrowsingDataRemover::NotificationDetails());
    771 
    772     // BrowsingDataRemover deletes itself when it completes.
    773     BrowsingDataRemoverCompletionObserver completion_observer(remover);
    774     remover->RemoveImpl(remove_mask, remove_origin,
    775         BrowsingDataHelper::UNPROTECTED_WEB);
    776     completion_observer.BlockUntilCompletion();
    777 
    778     // Save so we can verify later.
    779     storage_partition_removal_data_ =
    780         storage_partition.GetStoragePartitionRemovalData();
    781   }
    782 
    783   TestingProfile* GetProfile() {
    784     return profile_.get();
    785   }
    786 
    787   base::Time GetBeginTime() {
    788     return called_with_details_->removal_begin;
    789   }
    790 
    791   int GetRemovalMask() {
    792     return called_with_details_->removal_mask;
    793   }
    794 
    795   int GetOriginSetMask() {
    796     return called_with_details_->origin_set_mask;
    797   }
    798 
    799   StoragePartitionRemovalData GetStoragePartitionRemovalData() {
    800     return storage_partition_removal_data_;
    801   }
    802 
    803   // content::NotificationObserver implementation.
    804   virtual void Observe(int type,
    805                        const content::NotificationSource& source,
    806                        const content::NotificationDetails& details) OVERRIDE {
    807     DCHECK_EQ(type, chrome::NOTIFICATION_BROWSING_DATA_REMOVED);
    808 
    809     // We're not taking ownership of the details object, but storing a copy of
    810     // it locally.
    811     called_with_details_.reset(new BrowsingDataRemover::NotificationDetails(
    812         *content::Details<BrowsingDataRemover::NotificationDetails>(
    813             details).ptr()));
    814 
    815     registrar_.RemoveAll();
    816   }
    817 
    818   MockExtensionSpecialStoragePolicy* CreateMockPolicy() {
    819 #if defined(ENABLE_EXTENSIONS)
    820     mock_policy_ = new MockExtensionSpecialStoragePolicy;
    821     return mock_policy_.get();
    822 #else
    823     NOTREACHED();
    824     return NULL;
    825 #endif
    826   }
    827 
    828   storage::SpecialStoragePolicy* mock_policy() {
    829 #if defined(ENABLE_EXTENSIONS)
    830     return mock_policy_.get();
    831 #else
    832     return NULL;
    833 #endif
    834   }
    835 
    836   // If |kOrigin1| is protected when extensions are enabled, the expected
    837   // result for tests where the OriginMatcherFunction result is variable.
    838   bool ShouldRemoveForProtectedOriginOne() const {
    839 #if defined(ENABLE_EXTENSIONS)
    840     return false;
    841 #else
    842     return true;
    843 #endif
    844   }
    845 
    846  protected:
    847   scoped_ptr<BrowsingDataRemover::NotificationDetails> called_with_details_;
    848 
    849  private:
    850   content::NotificationRegistrar registrar_;
    851 
    852   content::TestBrowserThreadBundle thread_bundle_;
    853   scoped_ptr<TestingProfile> profile_;
    854 
    855   StoragePartitionRemovalData storage_partition_removal_data_;
    856 
    857 #if defined(ENABLE_EXTENSIONS)
    858   scoped_refptr<MockExtensionSpecialStoragePolicy> mock_policy_;
    859 #endif
    860 
    861   DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemoverTest);
    862 };
    863 
    864 // Tests ---------------------------------------------------------------------
    865 
    866 TEST_F(BrowsingDataRemoverTest, RemoveCookieForever) {
    867   BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
    868                                 BrowsingDataRemover::REMOVE_COOKIES,
    869                                 false);
    870 
    871   EXPECT_EQ(BrowsingDataRemover::REMOVE_COOKIES, GetRemovalMask());
    872   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
    873 
    874   // Verify that storage partition was instructed to remove the cookies.
    875   StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
    876   EXPECT_EQ(removal_data.remove_mask,
    877             StoragePartition::REMOVE_DATA_MASK_COOKIES);
    878   EXPECT_EQ(removal_data.quota_storage_remove_mask,
    879             StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
    880   EXPECT_TRUE(removal_data.remove_origin.is_empty());
    881   EXPECT_EQ(removal_data.remove_begin, GetBeginTime());
    882 }
    883 
    884 TEST_F(BrowsingDataRemoverTest, RemoveCookieLastHour) {
    885   BlockUntilBrowsingDataRemoved(BrowsingDataRemover::LAST_HOUR,
    886                                 BrowsingDataRemover::REMOVE_COOKIES,
    887                                 false);
    888 
    889   EXPECT_EQ(BrowsingDataRemover::REMOVE_COOKIES, GetRemovalMask());
    890   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
    891 
    892   // Verify that storage partition was instructed to remove the cookies.
    893   StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
    894   EXPECT_EQ(removal_data.remove_mask,
    895             StoragePartition::REMOVE_DATA_MASK_COOKIES);
    896   // Removing with time period other than EVERYTHING should not clear
    897   // persistent storage data.
    898   EXPECT_EQ(removal_data.quota_storage_remove_mask,
    899             ~StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT);
    900   EXPECT_TRUE(removal_data.remove_origin.is_empty());
    901   EXPECT_EQ(removal_data.remove_begin, GetBeginTime());
    902 }
    903 
    904 #if defined(FULL_SAFE_BROWSING) || defined(MOBILE_SAFE_BROWSING)
    905 TEST_F(BrowsingDataRemoverTest, RemoveSafeBrowsingCookieForever) {
    906   RemoveSafeBrowsingCookieTester tester;
    907 
    908   tester.AddCookie();
    909   ASSERT_TRUE(tester.ContainsCookie());
    910 
    911   BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
    912       BrowsingDataRemover::REMOVE_COOKIES, false);
    913 
    914   EXPECT_EQ(BrowsingDataRemover::REMOVE_COOKIES, GetRemovalMask());
    915   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
    916   EXPECT_FALSE(tester.ContainsCookie());
    917 }
    918 
    919 TEST_F(BrowsingDataRemoverTest, RemoveSafeBrowsingCookieLastHour) {
    920   RemoveSafeBrowsingCookieTester tester;
    921 
    922   tester.AddCookie();
    923   ASSERT_TRUE(tester.ContainsCookie());
    924 
    925   BlockUntilBrowsingDataRemoved(BrowsingDataRemover::LAST_HOUR,
    926       BrowsingDataRemover::REMOVE_COOKIES, false);
    927 
    928   EXPECT_EQ(BrowsingDataRemover::REMOVE_COOKIES, GetRemovalMask());
    929   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
    930   // Removing with time period other than EVERYTHING should not clear safe
    931   // browsing cookies.
    932   EXPECT_TRUE(tester.ContainsCookie());
    933 }
    934 #endif
    935 
    936 TEST_F(BrowsingDataRemoverTest, RemoveChannelIDForever) {
    937   RemoveChannelIDTester tester(GetProfile());
    938 
    939   tester.AddChannelID(kTestOrigin1);
    940   EXPECT_EQ(0, tester.ssl_config_changed_count());
    941   EXPECT_EQ(1, tester.ChannelIDCount());
    942 
    943   BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
    944       BrowsingDataRemover::REMOVE_CHANNEL_IDS, false);
    945 
    946   EXPECT_EQ(BrowsingDataRemover::REMOVE_CHANNEL_IDS, GetRemovalMask());
    947   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
    948   EXPECT_EQ(1, tester.ssl_config_changed_count());
    949   EXPECT_EQ(0, tester.ChannelIDCount());
    950 }
    951 
    952 TEST_F(BrowsingDataRemoverTest, RemoveChannelIDLastHour) {
    953   RemoveChannelIDTester tester(GetProfile());
    954 
    955   base::Time now = base::Time::Now();
    956   tester.AddChannelID(kTestOrigin1);
    957   tester.AddChannelIDWithTimes(kTestOrigin2,
    958                                      now - base::TimeDelta::FromHours(2),
    959                                      now);
    960   EXPECT_EQ(0, tester.ssl_config_changed_count());
    961   EXPECT_EQ(2, tester.ChannelIDCount());
    962 
    963   BlockUntilBrowsingDataRemoved(BrowsingDataRemover::LAST_HOUR,
    964       BrowsingDataRemover::REMOVE_CHANNEL_IDS, false);
    965 
    966   EXPECT_EQ(BrowsingDataRemover::REMOVE_CHANNEL_IDS, GetRemovalMask());
    967   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
    968   EXPECT_EQ(1, tester.ssl_config_changed_count());
    969   ASSERT_EQ(1, tester.ChannelIDCount());
    970   net::ChannelIDStore::ChannelIDList channel_ids;
    971   tester.GetChannelIDList(&channel_ids);
    972   ASSERT_EQ(1U, channel_ids.size());
    973   EXPECT_EQ(kTestOrigin2, channel_ids.front().server_identifier());
    974 }
    975 
    976 TEST_F(BrowsingDataRemoverTest, RemoveUnprotectedLocalStorageForever) {
    977 #if defined(ENABLE_EXTENSIONS)
    978   MockExtensionSpecialStoragePolicy* policy = CreateMockPolicy();
    979   // Protect kOrigin1.
    980   policy->AddProtected(kOrigin1.GetOrigin());
    981 #endif
    982 
    983   BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
    984                                 BrowsingDataRemover::REMOVE_LOCAL_STORAGE,
    985                                 false);
    986 
    987   EXPECT_EQ(BrowsingDataRemover::REMOVE_LOCAL_STORAGE, GetRemovalMask());
    988   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
    989 
    990   // Verify that storage partition was instructed to remove the data correctly.
    991   StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
    992   EXPECT_EQ(removal_data.remove_mask,
    993             StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE);
    994   EXPECT_EQ(removal_data.quota_storage_remove_mask,
    995             StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
    996   EXPECT_TRUE(removal_data.remove_origin.is_empty());
    997   EXPECT_EQ(removal_data.remove_begin, GetBeginTime());
    998 
    999   // Check origin matcher.
   1000   EXPECT_EQ(ShouldRemoveForProtectedOriginOne(),
   1001             removal_data.origin_matcher.Run(kOrigin1, mock_policy()));
   1002   EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin2, mock_policy()));
   1003   EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin3, mock_policy()));
   1004   EXPECT_FALSE(removal_data.origin_matcher.Run(kOriginExt, mock_policy()));
   1005 }
   1006 
   1007 TEST_F(BrowsingDataRemoverTest, RemoveProtectedLocalStorageForever) {
   1008 #if defined(ENABLE_EXTENSIONS)
   1009   // Protect kOrigin1.
   1010   MockExtensionSpecialStoragePolicy* policy = CreateMockPolicy();
   1011   policy->AddProtected(kOrigin1.GetOrigin());
   1012 #endif
   1013 
   1014   BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
   1015                                 BrowsingDataRemover::REMOVE_LOCAL_STORAGE,
   1016                                 true);
   1017 
   1018   EXPECT_EQ(BrowsingDataRemover::REMOVE_LOCAL_STORAGE, GetRemovalMask());
   1019   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB |
   1020             BrowsingDataHelper::PROTECTED_WEB, GetOriginSetMask());
   1021 
   1022   // Verify that storage partition was instructed to remove the data correctly.
   1023   StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
   1024   EXPECT_EQ(removal_data.remove_mask,
   1025             StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE);
   1026   EXPECT_EQ(removal_data.quota_storage_remove_mask,
   1027             StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
   1028   EXPECT_TRUE(removal_data.remove_origin.is_empty());
   1029   EXPECT_EQ(removal_data.remove_begin, GetBeginTime());
   1030 
   1031   // Check origin matcher all http origin will match since we specified
   1032   // both protected and unprotected.
   1033   EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin1, mock_policy()));
   1034   EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin2, mock_policy()));
   1035   EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin3, mock_policy()));
   1036   EXPECT_FALSE(removal_data.origin_matcher.Run(kOriginExt, mock_policy()));
   1037 }
   1038 
   1039 TEST_F(BrowsingDataRemoverTest, RemoveLocalStorageForLastWeek) {
   1040 #if defined(ENABLE_EXTENSIONS)
   1041   CreateMockPolicy();
   1042 #endif
   1043 
   1044   BlockUntilBrowsingDataRemoved(BrowsingDataRemover::LAST_WEEK,
   1045                                 BrowsingDataRemover::REMOVE_LOCAL_STORAGE,
   1046                                 false);
   1047 
   1048   EXPECT_EQ(BrowsingDataRemover::REMOVE_LOCAL_STORAGE, GetRemovalMask());
   1049   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
   1050 
   1051   // Verify that storage partition was instructed to remove the data correctly.
   1052   StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
   1053   EXPECT_EQ(removal_data.remove_mask,
   1054             StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE);
   1055   // Persistent storage won't be deleted.
   1056   EXPECT_EQ(removal_data.quota_storage_remove_mask,
   1057             ~StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT);
   1058   EXPECT_TRUE(removal_data.remove_origin.is_empty());
   1059   EXPECT_EQ(removal_data.remove_begin, GetBeginTime());
   1060 
   1061   // Check origin matcher.
   1062   EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin1, mock_policy()));
   1063   EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin2, mock_policy()));
   1064   EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin3, mock_policy()));
   1065   EXPECT_FALSE(removal_data.origin_matcher.Run(kOriginExt, mock_policy()));
   1066 }
   1067 
   1068 TEST_F(BrowsingDataRemoverTest, RemoveHistoryForever) {
   1069   RemoveHistoryTester tester;
   1070   ASSERT_TRUE(tester.Init(GetProfile()));
   1071 
   1072   tester.AddHistory(kOrigin1, base::Time::Now());
   1073   ASSERT_TRUE(tester.HistoryContainsURL(kOrigin1));
   1074 
   1075   BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
   1076       BrowsingDataRemover::REMOVE_HISTORY, false);
   1077 
   1078   EXPECT_EQ(BrowsingDataRemover::REMOVE_HISTORY, GetRemovalMask());
   1079   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
   1080   EXPECT_FALSE(tester.HistoryContainsURL(kOrigin1));
   1081 }
   1082 
   1083 TEST_F(BrowsingDataRemoverTest, RemoveHistoryForLastHour) {
   1084   RemoveHistoryTester tester;
   1085   ASSERT_TRUE(tester.Init(GetProfile()));
   1086 
   1087   base::Time two_hours_ago = base::Time::Now() - base::TimeDelta::FromHours(2);
   1088 
   1089   tester.AddHistory(kOrigin1, base::Time::Now());
   1090   tester.AddHistory(kOrigin2, two_hours_ago);
   1091   ASSERT_TRUE(tester.HistoryContainsURL(kOrigin1));
   1092   ASSERT_TRUE(tester.HistoryContainsURL(kOrigin2));
   1093 
   1094   BlockUntilBrowsingDataRemoved(BrowsingDataRemover::LAST_HOUR,
   1095       BrowsingDataRemover::REMOVE_HISTORY, false);
   1096 
   1097   EXPECT_EQ(BrowsingDataRemover::REMOVE_HISTORY, GetRemovalMask());
   1098   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
   1099   EXPECT_FALSE(tester.HistoryContainsURL(kOrigin1));
   1100   EXPECT_TRUE(tester.HistoryContainsURL(kOrigin2));
   1101 }
   1102 
   1103 // This should crash (DCHECK) in Debug, but death tests don't work properly
   1104 // here.
   1105 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
   1106 TEST_F(BrowsingDataRemoverTest, RemoveHistoryProhibited) {
   1107   RemoveHistoryTester tester;
   1108   ASSERT_TRUE(tester.Init(GetProfile()));
   1109   PrefService* prefs = GetProfile()->GetPrefs();
   1110   prefs->SetBoolean(prefs::kAllowDeletingBrowserHistory, false);
   1111 
   1112   base::Time two_hours_ago = base::Time::Now() - base::TimeDelta::FromHours(2);
   1113 
   1114   tester.AddHistory(kOrigin1, base::Time::Now());
   1115   tester.AddHistory(kOrigin2, two_hours_ago);
   1116   ASSERT_TRUE(tester.HistoryContainsURL(kOrigin1));
   1117   ASSERT_TRUE(tester.HistoryContainsURL(kOrigin2));
   1118 
   1119   BlockUntilBrowsingDataRemoved(BrowsingDataRemover::LAST_HOUR,
   1120       BrowsingDataRemover::REMOVE_HISTORY, false);
   1121   EXPECT_EQ(BrowsingDataRemover::REMOVE_HISTORY, GetRemovalMask());
   1122   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
   1123 
   1124   // Nothing should have been deleted.
   1125   EXPECT_TRUE(tester.HistoryContainsURL(kOrigin1));
   1126   EXPECT_TRUE(tester.HistoryContainsURL(kOrigin2));
   1127 }
   1128 #endif
   1129 
   1130 TEST_F(BrowsingDataRemoverTest, RemoveMultipleTypes) {
   1131   // Add some history.
   1132   RemoveHistoryTester history_tester;
   1133   ASSERT_TRUE(history_tester.Init(GetProfile()));
   1134   history_tester.AddHistory(kOrigin1, base::Time::Now());
   1135   ASSERT_TRUE(history_tester.HistoryContainsURL(kOrigin1));
   1136 
   1137   int removal_mask = BrowsingDataRemover::REMOVE_HISTORY |
   1138                      BrowsingDataRemover::REMOVE_COOKIES;
   1139 
   1140   BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
   1141       removal_mask, false);
   1142 
   1143   EXPECT_EQ(removal_mask, GetRemovalMask());
   1144   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
   1145   EXPECT_FALSE(history_tester.HistoryContainsURL(kOrigin1));
   1146 
   1147   // The cookie would be deleted throught the StorageParition, check if the
   1148   // partition was requested to remove cookie.
   1149   StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
   1150   EXPECT_EQ(removal_data.remove_mask,
   1151             StoragePartition::REMOVE_DATA_MASK_COOKIES);
   1152   EXPECT_EQ(removal_data.quota_storage_remove_mask,
   1153             StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
   1154 }
   1155 
   1156 // This should crash (DCHECK) in Debug, but death tests don't work properly
   1157 // here.
   1158 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
   1159 TEST_F(BrowsingDataRemoverTest, RemoveMultipleTypesHistoryProhibited) {
   1160   PrefService* prefs = GetProfile()->GetPrefs();
   1161   prefs->SetBoolean(prefs::kAllowDeletingBrowserHistory, false);
   1162 
   1163   // Add some history.
   1164   RemoveHistoryTester history_tester;
   1165   ASSERT_TRUE(history_tester.Init(GetProfile()));
   1166   history_tester.AddHistory(kOrigin1, base::Time::Now());
   1167   ASSERT_TRUE(history_tester.HistoryContainsURL(kOrigin1));
   1168 
   1169   int removal_mask = BrowsingDataRemover::REMOVE_HISTORY |
   1170                      BrowsingDataRemover::REMOVE_COOKIES;
   1171 
   1172   BlockUntilBrowsingDataRemoved(BrowsingDataRemover::LAST_HOUR,
   1173                                 removal_mask, false);
   1174   EXPECT_EQ(removal_mask, GetRemovalMask());
   1175   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
   1176 
   1177   // 1/2. History should remain.
   1178   EXPECT_TRUE(history_tester.HistoryContainsURL(kOrigin1));
   1179 
   1180   // 2/2. The cookie(s) would be deleted throught the StorageParition, check if
   1181   // the partition was requested to remove cookie.
   1182   StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
   1183   EXPECT_EQ(removal_data.remove_mask,
   1184             StoragePartition::REMOVE_DATA_MASK_COOKIES);
   1185   // Persistent storage won't be deleted, since EVERYTHING was not specified.
   1186   EXPECT_EQ(removal_data.quota_storage_remove_mask,
   1187             ~StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT);
   1188 }
   1189 #endif
   1190 
   1191 TEST_F(BrowsingDataRemoverTest, RemoveQuotaManagedDataForeverBoth) {
   1192   BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
   1193                                 BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
   1194                                 BrowsingDataRemover::REMOVE_WEBSQL |
   1195                                 BrowsingDataRemover::REMOVE_APPCACHE |
   1196                                 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
   1197                                 BrowsingDataRemover::REMOVE_INDEXEDDB,
   1198                                 false);
   1199 
   1200   EXPECT_EQ(BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
   1201       BrowsingDataRemover::REMOVE_WEBSQL |
   1202       BrowsingDataRemover::REMOVE_APPCACHE |
   1203       BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
   1204       BrowsingDataRemover::REMOVE_INDEXEDDB, GetRemovalMask());
   1205   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
   1206 
   1207   // Verify storage partition related stuffs.
   1208   StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
   1209   EXPECT_EQ(removal_data.remove_mask,
   1210             StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
   1211                 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
   1212                 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
   1213                 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
   1214                 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
   1215   EXPECT_EQ(removal_data.quota_storage_remove_mask,
   1216             StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
   1217   EXPECT_TRUE(removal_data.remove_origin.is_empty());
   1218 }
   1219 
   1220 TEST_F(BrowsingDataRemoverTest, RemoveQuotaManagedDataForeverOnlyTemporary) {
   1221 #if defined(ENABLE_EXTENSIONS)
   1222   CreateMockPolicy();
   1223 #endif
   1224 
   1225   BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
   1226                                 BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
   1227                                 BrowsingDataRemover::REMOVE_WEBSQL |
   1228                                 BrowsingDataRemover::REMOVE_APPCACHE |
   1229                                 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
   1230                                 BrowsingDataRemover::REMOVE_INDEXEDDB,
   1231                                 false);
   1232 
   1233   EXPECT_EQ(BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
   1234       BrowsingDataRemover::REMOVE_WEBSQL |
   1235       BrowsingDataRemover::REMOVE_APPCACHE |
   1236       BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
   1237       BrowsingDataRemover::REMOVE_INDEXEDDB, GetRemovalMask());
   1238   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
   1239 
   1240   // Verify storage partition related stuffs.
   1241   StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
   1242 
   1243   EXPECT_EQ(removal_data.remove_mask,
   1244             StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
   1245                 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
   1246                 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
   1247                 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
   1248                 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
   1249   EXPECT_EQ(removal_data.quota_storage_remove_mask,
   1250             StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
   1251   EXPECT_TRUE(removal_data.remove_origin.is_empty());
   1252 
   1253   // Check that all related origin data would be removed, that is, origin
   1254   // matcher would match these origin.
   1255   EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin1, mock_policy()));
   1256   EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin2, mock_policy()));
   1257   EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin3, mock_policy()));
   1258 }
   1259 
   1260 TEST_F(BrowsingDataRemoverTest, RemoveQuotaManagedDataForeverOnlyPersistent) {
   1261 #if defined(ENABLE_EXTENSIONS)
   1262   CreateMockPolicy();
   1263 #endif
   1264 
   1265   BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
   1266                                 BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
   1267                                 BrowsingDataRemover::REMOVE_WEBSQL |
   1268                                 BrowsingDataRemover::REMOVE_APPCACHE |
   1269                                 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
   1270                                 BrowsingDataRemover::REMOVE_INDEXEDDB,
   1271                                 false);
   1272 
   1273   EXPECT_EQ(BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
   1274       BrowsingDataRemover::REMOVE_WEBSQL |
   1275       BrowsingDataRemover::REMOVE_APPCACHE |
   1276       BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
   1277       BrowsingDataRemover::REMOVE_INDEXEDDB, GetRemovalMask());
   1278   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
   1279 
   1280   // Verify storage partition related stuffs.
   1281   StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
   1282 
   1283   EXPECT_EQ(removal_data.remove_mask,
   1284             StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
   1285                 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
   1286                 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
   1287                 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
   1288                 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
   1289   EXPECT_EQ(removal_data.quota_storage_remove_mask,
   1290             StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
   1291   EXPECT_TRUE(removal_data.remove_origin.is_empty());
   1292 
   1293   // Check that all related origin data would be removed, that is, origin
   1294   // matcher would match these origin.
   1295   EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin1, mock_policy()));
   1296   EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin2, mock_policy()));
   1297   EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin3, mock_policy()));
   1298 }
   1299 
   1300 TEST_F(BrowsingDataRemoverTest, RemoveQuotaManagedDataForeverNeither) {
   1301 #if defined(ENABLE_EXTENSIONS)
   1302   CreateMockPolicy();
   1303 #endif
   1304 
   1305 
   1306   BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
   1307                                 BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
   1308                                 BrowsingDataRemover::REMOVE_WEBSQL |
   1309                                 BrowsingDataRemover::REMOVE_APPCACHE |
   1310                                 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
   1311                                 BrowsingDataRemover::REMOVE_INDEXEDDB,
   1312                                 false);
   1313 
   1314   EXPECT_EQ(BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
   1315       BrowsingDataRemover::REMOVE_WEBSQL |
   1316       BrowsingDataRemover::REMOVE_APPCACHE |
   1317       BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
   1318       BrowsingDataRemover::REMOVE_INDEXEDDB, GetRemovalMask());
   1319   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
   1320 
   1321   // Verify storage partition related stuffs.
   1322   StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
   1323 
   1324   EXPECT_EQ(removal_data.remove_mask,
   1325             StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
   1326                 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
   1327                 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
   1328                 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
   1329                 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
   1330   EXPECT_EQ(removal_data.quota_storage_remove_mask,
   1331             StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
   1332   EXPECT_TRUE(removal_data.remove_origin.is_empty());
   1333 
   1334   // Check that all related origin data would be removed, that is, origin
   1335   // matcher would match these origin.
   1336   EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin1, mock_policy()));
   1337   EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin2, mock_policy()));
   1338   EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin3, mock_policy()));
   1339 }
   1340 
   1341 TEST_F(BrowsingDataRemoverTest, RemoveQuotaManagedDataForeverSpecificOrigin) {
   1342   // Remove Origin 1.
   1343   BlockUntilOriginDataRemoved(BrowsingDataRemover::EVERYTHING,
   1344                               BrowsingDataRemover::REMOVE_APPCACHE |
   1345                               BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
   1346                               BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
   1347                               BrowsingDataRemover::REMOVE_INDEXEDDB |
   1348                               BrowsingDataRemover::REMOVE_WEBSQL,
   1349                               kOrigin1);
   1350 
   1351   EXPECT_EQ(BrowsingDataRemover::REMOVE_APPCACHE |
   1352       BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
   1353       BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
   1354       BrowsingDataRemover::REMOVE_INDEXEDDB |
   1355       BrowsingDataRemover::REMOVE_WEBSQL, GetRemovalMask());
   1356   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
   1357 
   1358   // Verify storage partition related stuffs.
   1359   StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
   1360 
   1361   EXPECT_EQ(removal_data.remove_mask,
   1362             StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
   1363                 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
   1364                 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
   1365                 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
   1366                 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
   1367   EXPECT_EQ(removal_data.quota_storage_remove_mask,
   1368             StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
   1369   EXPECT_EQ(removal_data.remove_origin, kOrigin1);
   1370 }
   1371 
   1372 TEST_F(BrowsingDataRemoverTest, RemoveQuotaManagedDataForLastHour) {
   1373   BlockUntilBrowsingDataRemoved(BrowsingDataRemover::LAST_HOUR,
   1374                                 BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
   1375                                 BrowsingDataRemover::REMOVE_WEBSQL |
   1376                                 BrowsingDataRemover::REMOVE_APPCACHE |
   1377                                 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
   1378                                 BrowsingDataRemover::REMOVE_INDEXEDDB,
   1379                                 false);
   1380 
   1381   EXPECT_EQ(BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
   1382       BrowsingDataRemover::REMOVE_WEBSQL |
   1383       BrowsingDataRemover::REMOVE_APPCACHE |
   1384       BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
   1385       BrowsingDataRemover::REMOVE_INDEXEDDB, GetRemovalMask());
   1386   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
   1387 
   1388   // Verify storage partition related stuffs.
   1389   StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
   1390 
   1391   EXPECT_EQ(removal_data.remove_mask,
   1392             StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
   1393                 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
   1394                 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
   1395                 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
   1396                 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
   1397 
   1398   // Persistent data would be left out since we are not removing from
   1399   // beginning of time.
   1400   uint32 expected_quota_mask =
   1401       ~StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT;
   1402   EXPECT_EQ(removal_data.quota_storage_remove_mask, expected_quota_mask);
   1403   EXPECT_TRUE(removal_data.remove_origin.is_empty());
   1404   // Check removal begin time.
   1405   EXPECT_EQ(removal_data.remove_begin, GetBeginTime());
   1406 }
   1407 
   1408 TEST_F(BrowsingDataRemoverTest, RemoveQuotaManagedDataForLastWeek) {
   1409   BlockUntilBrowsingDataRemoved(BrowsingDataRemover::LAST_WEEK,
   1410                                 BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
   1411                                 BrowsingDataRemover::REMOVE_WEBSQL |
   1412                                 BrowsingDataRemover::REMOVE_APPCACHE |
   1413                                 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
   1414                                 BrowsingDataRemover::REMOVE_INDEXEDDB,
   1415                                 false);
   1416 
   1417   EXPECT_EQ(BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
   1418       BrowsingDataRemover::REMOVE_WEBSQL |
   1419       BrowsingDataRemover::REMOVE_APPCACHE |
   1420       BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
   1421       BrowsingDataRemover::REMOVE_INDEXEDDB, GetRemovalMask());
   1422   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
   1423 
   1424   // Verify storage partition related stuffs.
   1425   StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
   1426 
   1427   EXPECT_EQ(removal_data.remove_mask,
   1428             StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
   1429                 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
   1430                 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
   1431                 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
   1432                 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
   1433 
   1434   // Persistent data would be left out since we are not removing from
   1435   // beginning of time.
   1436   uint32 expected_quota_mask =
   1437       ~StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT;
   1438   EXPECT_EQ(removal_data.quota_storage_remove_mask, expected_quota_mask);
   1439   EXPECT_TRUE(removal_data.remove_origin.is_empty());
   1440   // Check removal begin time.
   1441   EXPECT_EQ(removal_data.remove_begin, GetBeginTime());
   1442 }
   1443 
   1444 TEST_F(BrowsingDataRemoverTest, RemoveQuotaManagedUnprotectedOrigins) {
   1445 #if defined(ENABLE_EXTENSIONS)
   1446   MockExtensionSpecialStoragePolicy* policy = CreateMockPolicy();
   1447   // Protect kOrigin1.
   1448   policy->AddProtected(kOrigin1.GetOrigin());
   1449 #endif
   1450 
   1451   BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
   1452                                 BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
   1453                                 BrowsingDataRemover::REMOVE_WEBSQL |
   1454                                 BrowsingDataRemover::REMOVE_APPCACHE |
   1455                                 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
   1456                                 BrowsingDataRemover::REMOVE_INDEXEDDB,
   1457                                 false);
   1458 
   1459   EXPECT_EQ(BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
   1460       BrowsingDataRemover::REMOVE_WEBSQL |
   1461       BrowsingDataRemover::REMOVE_APPCACHE |
   1462       BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
   1463       BrowsingDataRemover::REMOVE_INDEXEDDB, GetRemovalMask());
   1464   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
   1465 
   1466   // Verify storage partition related stuffs.
   1467   StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
   1468 
   1469   EXPECT_EQ(removal_data.remove_mask,
   1470             StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
   1471                 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
   1472                 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
   1473                 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
   1474                 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
   1475   EXPECT_EQ(removal_data.quota_storage_remove_mask,
   1476             StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
   1477   EXPECT_TRUE(removal_data.remove_origin.is_empty());
   1478 
   1479   // Check OriginMatcherFunction.
   1480   EXPECT_EQ(ShouldRemoveForProtectedOriginOne(),
   1481             removal_data.origin_matcher.Run(kOrigin1, mock_policy()));
   1482   EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin2, mock_policy()));
   1483   EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin3, mock_policy()));
   1484 }
   1485 
   1486 TEST_F(BrowsingDataRemoverTest, RemoveQuotaManagedProtectedSpecificOrigin) {
   1487 #if defined(ENABLE_EXTENSIONS)
   1488   MockExtensionSpecialStoragePolicy* policy = CreateMockPolicy();
   1489   // Protect kOrigin1.
   1490   policy->AddProtected(kOrigin1.GetOrigin());
   1491 #endif
   1492 
   1493   // Try to remove kOrigin1. Expect failure.
   1494   BlockUntilOriginDataRemoved(BrowsingDataRemover::EVERYTHING,
   1495                               BrowsingDataRemover::REMOVE_APPCACHE |
   1496                               BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
   1497                               BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
   1498                               BrowsingDataRemover::REMOVE_INDEXEDDB |
   1499                               BrowsingDataRemover::REMOVE_WEBSQL,
   1500                               kOrigin1);
   1501 
   1502   EXPECT_EQ(BrowsingDataRemover::REMOVE_APPCACHE |
   1503       BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
   1504       BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
   1505       BrowsingDataRemover::REMOVE_INDEXEDDB |
   1506       BrowsingDataRemover::REMOVE_WEBSQL, GetRemovalMask());
   1507   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
   1508 
   1509   // Verify storage partition related stuffs.
   1510   StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
   1511 
   1512   EXPECT_EQ(removal_data.remove_mask,
   1513             StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
   1514                 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
   1515                 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
   1516                 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
   1517                 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
   1518   EXPECT_EQ(removal_data.quota_storage_remove_mask,
   1519             StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
   1520   EXPECT_EQ(removal_data.remove_origin, kOrigin1);
   1521 
   1522   // Check OriginMatcherFunction.
   1523   EXPECT_EQ(ShouldRemoveForProtectedOriginOne(),
   1524             removal_data.origin_matcher.Run(kOrigin1, mock_policy()));
   1525   EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin2, mock_policy()));
   1526   EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin3, mock_policy()));
   1527 }
   1528 
   1529 TEST_F(BrowsingDataRemoverTest, RemoveQuotaManagedProtectedOrigins) {
   1530 #if defined(ENABLE_EXTENSIONS)
   1531   MockExtensionSpecialStoragePolicy* policy = CreateMockPolicy();
   1532   // Protect kOrigin1.
   1533   policy->AddProtected(kOrigin1.GetOrigin());
   1534 #endif
   1535 
   1536   // Try to remove kOrigin1. Expect success.
   1537   BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
   1538                                 BrowsingDataRemover::REMOVE_APPCACHE |
   1539                                 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
   1540                                 BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
   1541                                 BrowsingDataRemover::REMOVE_INDEXEDDB |
   1542                                 BrowsingDataRemover::REMOVE_WEBSQL,
   1543                                 true);
   1544 
   1545   EXPECT_EQ(BrowsingDataRemover::REMOVE_APPCACHE |
   1546       BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
   1547       BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
   1548       BrowsingDataRemover::REMOVE_INDEXEDDB |
   1549       BrowsingDataRemover::REMOVE_WEBSQL, GetRemovalMask());
   1550   EXPECT_EQ(BrowsingDataHelper::PROTECTED_WEB |
   1551       BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
   1552 
   1553   // Verify storage partition related stuffs.
   1554   StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
   1555 
   1556   EXPECT_EQ(removal_data.remove_mask,
   1557             StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
   1558                 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
   1559                 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
   1560                 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
   1561                 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
   1562   EXPECT_EQ(removal_data.quota_storage_remove_mask,
   1563             StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
   1564   EXPECT_TRUE(removal_data.remove_origin.is_empty());
   1565 
   1566   // Check OriginMatcherFunction, |kOrigin1| would match mask since we
   1567   // would have 'protected' specified in origin_set_mask.
   1568   EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin1, mock_policy()));
   1569   EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin2, mock_policy()));
   1570   EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin3, mock_policy()));
   1571 }
   1572 
   1573 TEST_F(BrowsingDataRemoverTest, RemoveQuotaManagedIgnoreExtensionsAndDevTools) {
   1574 #if defined(ENABLE_EXTENSIONS)
   1575   CreateMockPolicy();
   1576 #endif
   1577 
   1578   BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
   1579                                 BrowsingDataRemover::REMOVE_APPCACHE |
   1580                                 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
   1581                                 BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
   1582                                 BrowsingDataRemover::REMOVE_INDEXEDDB |
   1583                                 BrowsingDataRemover::REMOVE_WEBSQL,
   1584                                 false);
   1585 
   1586   EXPECT_EQ(BrowsingDataRemover::REMOVE_APPCACHE |
   1587       BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
   1588       BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
   1589       BrowsingDataRemover::REMOVE_INDEXEDDB |
   1590       BrowsingDataRemover::REMOVE_WEBSQL, GetRemovalMask());
   1591   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
   1592 
   1593   // Verify storage partition related stuffs.
   1594   StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
   1595 
   1596   EXPECT_EQ(removal_data.remove_mask,
   1597             StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
   1598                 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
   1599                 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
   1600                 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
   1601                 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
   1602   EXPECT_EQ(removal_data.quota_storage_remove_mask,
   1603             StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
   1604   EXPECT_TRUE(removal_data.remove_origin.is_empty());
   1605 
   1606   // Check that extension and devtools data wouldn't be removed, that is,
   1607   // origin matcher would not match these origin.
   1608   EXPECT_FALSE(removal_data.origin_matcher.Run(kOriginExt, mock_policy()));
   1609   EXPECT_FALSE(removal_data.origin_matcher.Run(kOriginDevTools, mock_policy()));
   1610 }
   1611 
   1612 TEST_F(BrowsingDataRemoverTest, OriginBasedHistoryRemoval) {
   1613   RemoveHistoryTester tester;
   1614   ASSERT_TRUE(tester.Init(GetProfile()));
   1615 
   1616   base::Time two_hours_ago = base::Time::Now() - base::TimeDelta::FromHours(2);
   1617 
   1618   tester.AddHistory(kOrigin1, base::Time::Now());
   1619   tester.AddHistory(kOrigin2, two_hours_ago);
   1620   ASSERT_TRUE(tester.HistoryContainsURL(kOrigin1));
   1621   ASSERT_TRUE(tester.HistoryContainsURL(kOrigin2));
   1622 
   1623   BlockUntilOriginDataRemoved(BrowsingDataRemover::EVERYTHING,
   1624       BrowsingDataRemover::REMOVE_HISTORY, kOrigin2);
   1625 
   1626   EXPECT_EQ(BrowsingDataRemover::REMOVE_HISTORY, GetRemovalMask());
   1627   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
   1628 
   1629   // Nothing should have been deleted.
   1630   EXPECT_TRUE(tester.HistoryContainsURL(kOrigin1));
   1631   EXPECT_FALSE(tester.HistoryContainsURL(kOrigin2));
   1632 }
   1633 
   1634 TEST_F(BrowsingDataRemoverTest, OriginAndTimeBasedHistoryRemoval) {
   1635   RemoveHistoryTester tester;
   1636   ASSERT_TRUE(tester.Init(GetProfile()));
   1637 
   1638   base::Time two_hours_ago = base::Time::Now() - base::TimeDelta::FromHours(2);
   1639 
   1640   tester.AddHistory(kOrigin1, base::Time::Now());
   1641   tester.AddHistory(kOrigin2, two_hours_ago);
   1642   ASSERT_TRUE(tester.HistoryContainsURL(kOrigin1));
   1643   ASSERT_TRUE(tester.HistoryContainsURL(kOrigin2));
   1644 
   1645   BlockUntilOriginDataRemoved(BrowsingDataRemover::LAST_HOUR,
   1646       BrowsingDataRemover::REMOVE_HISTORY, kOrigin2);
   1647 
   1648   EXPECT_EQ(BrowsingDataRemover::REMOVE_HISTORY, GetRemovalMask());
   1649   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
   1650   EXPECT_TRUE(tester.HistoryContainsURL(kOrigin1));
   1651   EXPECT_TRUE(tester.HistoryContainsURL(kOrigin2));
   1652 }
   1653 
   1654 // Verify that clearing autofill form data works.
   1655 TEST_F(BrowsingDataRemoverTest, AutofillRemovalLastHour) {
   1656   GetProfile()->CreateWebDataService();
   1657   RemoveAutofillTester tester(GetProfile());
   1658 
   1659   ASSERT_FALSE(tester.HasProfile());
   1660   tester.AddProfilesAndCards();
   1661   ASSERT_TRUE(tester.HasProfile());
   1662 
   1663   BlockUntilBrowsingDataRemoved(
   1664       BrowsingDataRemover::LAST_HOUR,
   1665       BrowsingDataRemover::REMOVE_FORM_DATA, false);
   1666 
   1667   EXPECT_EQ(BrowsingDataRemover::REMOVE_FORM_DATA, GetRemovalMask());
   1668   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
   1669   ASSERT_FALSE(tester.HasProfile());
   1670 }
   1671 
   1672 TEST_F(BrowsingDataRemoverTest, AutofillRemovalEverything) {
   1673   GetProfile()->CreateWebDataService();
   1674   RemoveAutofillTester tester(GetProfile());
   1675 
   1676   ASSERT_FALSE(tester.HasProfile());
   1677   tester.AddProfilesAndCards();
   1678   ASSERT_TRUE(tester.HasProfile());
   1679 
   1680   BlockUntilBrowsingDataRemoved(
   1681       BrowsingDataRemover::EVERYTHING,
   1682       BrowsingDataRemover::REMOVE_FORM_DATA, false);
   1683 
   1684   EXPECT_EQ(BrowsingDataRemover::REMOVE_FORM_DATA, GetRemovalMask());
   1685   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
   1686   ASSERT_FALSE(tester.HasProfile());
   1687 }
   1688 
   1689 // Verify that clearing autofill form data works.
   1690 TEST_F(BrowsingDataRemoverTest, AutofillOriginsRemovedWithHistory) {
   1691   GetProfile()->CreateWebDataService();
   1692   RemoveAutofillTester tester(GetProfile());
   1693 
   1694   tester.AddProfilesAndCards();
   1695   EXPECT_FALSE(tester.HasOrigin(std::string()));
   1696   EXPECT_TRUE(tester.HasOrigin(kWebOrigin));
   1697   EXPECT_TRUE(tester.HasOrigin(kChromeOrigin));
   1698 
   1699   BlockUntilBrowsingDataRemoved(
   1700       BrowsingDataRemover::LAST_HOUR,
   1701       BrowsingDataRemover::REMOVE_HISTORY, false);
   1702 
   1703   EXPECT_EQ(BrowsingDataRemover::REMOVE_HISTORY, GetRemovalMask());
   1704   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
   1705   EXPECT_TRUE(tester.HasOrigin(std::string()));
   1706   EXPECT_FALSE(tester.HasOrigin(kWebOrigin));
   1707   EXPECT_TRUE(tester.HasOrigin(kChromeOrigin));
   1708 }
   1709 
   1710 TEST_F(BrowsingDataRemoverTest, CompletionInhibition) {
   1711   // The |completion_inhibitor| on the stack should prevent removal sessions
   1712   // from completing until after ContinueToCompletion() is called.
   1713   BrowsingDataRemoverCompletionInhibitor completion_inhibitor;
   1714 
   1715   called_with_details_.reset(new BrowsingDataRemover::NotificationDetails());
   1716 
   1717   // BrowsingDataRemover deletes itself when it completes.
   1718   BrowsingDataRemover* remover = BrowsingDataRemover::CreateForPeriod(
   1719       GetProfile(), BrowsingDataRemover::EVERYTHING);
   1720   remover->Remove(BrowsingDataRemover::REMOVE_HISTORY,
   1721                   BrowsingDataHelper::UNPROTECTED_WEB);
   1722 
   1723   // Process messages until the inhibitor is notified, and then some, to make
   1724   // sure we do not complete asynchronously before ContinueToCompletion() is
   1725   // called.
   1726   completion_inhibitor.BlockUntilNearCompletion();
   1727   base::RunLoop().RunUntilIdle();
   1728 
   1729   // Verify that the completion notification has not yet been broadcasted.
   1730   EXPECT_EQ(-1, GetRemovalMask());
   1731   EXPECT_EQ(-1, GetOriginSetMask());
   1732 
   1733   // Now run the removal process until completion, and verify that observers are
   1734   // now notified, and the notifications is sent out.
   1735   BrowsingDataRemoverCompletionObserver completion_observer(remover);
   1736   completion_inhibitor.ContinueToCompletion();
   1737   completion_observer.BlockUntilCompletion();
   1738 
   1739   EXPECT_EQ(BrowsingDataRemover::REMOVE_HISTORY, GetRemovalMask());
   1740   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
   1741 }
   1742 
   1743 TEST_F(BrowsingDataRemoverTest, ZeroSuggestCacheClear) {
   1744   PrefService* prefs = GetProfile()->GetPrefs();
   1745   prefs->SetString(prefs::kZeroSuggestCachedResults,
   1746                    "[\"\", [\"foo\", \"bar\"]]");
   1747   BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
   1748                                 BrowsingDataRemover::REMOVE_COOKIES,
   1749                                 false);
   1750 
   1751   // Expect the prefs to be cleared when cookies are removed.
   1752   EXPECT_TRUE(prefs->GetString(prefs::kZeroSuggestCachedResults).empty());
   1753   EXPECT_EQ(BrowsingDataRemover::REMOVE_COOKIES, GetRemovalMask());
   1754   EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
   1755 }
   1756 
   1757 #if defined(OS_CHROMEOS)
   1758 TEST_F(BrowsingDataRemoverTest, ContentProtectionPlatformKeysRemoval) {
   1759   chromeos::ScopedTestDeviceSettingsService test_device_settings_service;
   1760   chromeos::ScopedTestCrosSettings test_cros_settings;
   1761   chromeos::MockUserManager* mock_user_manager =
   1762       new testing::NiceMock<chromeos::MockUserManager>();
   1763   mock_user_manager->SetActiveUser("test (at) example.com");
   1764   chromeos::ScopedUserManagerEnabler user_manager_enabler(mock_user_manager);
   1765 
   1766   scoped_ptr<chromeos::DBusThreadManagerSetter> dbus_setter =
   1767       chromeos::DBusThreadManager::GetSetterForTesting();
   1768   chromeos::MockCryptohomeClient* cryptohome_client =
   1769       new chromeos::MockCryptohomeClient;
   1770   dbus_setter->SetCryptohomeClient(
   1771       scoped_ptr<chromeos::CryptohomeClient>(cryptohome_client));
   1772 
   1773   // Expect exactly one call.  No calls means no attempt to delete keys and more
   1774   // than one call means a significant performance problem.
   1775   EXPECT_CALL(*cryptohome_client, TpmAttestationDeleteKeys(_, _, _, _))
   1776       .WillOnce(WithArgs<3>(Invoke(FakeDBusCall)));
   1777 
   1778   BlockUntilBrowsingDataRemoved(
   1779       BrowsingDataRemover::EVERYTHING,
   1780       BrowsingDataRemover::REMOVE_CONTENT_LICENSES, false);
   1781 
   1782   chromeos::DBusThreadManager::Shutdown();
   1783 }
   1784 #endif
   1785 
   1786 TEST_F(BrowsingDataRemoverTest, DomainReliability_Null) {
   1787   ClearDomainReliabilityTester tester(GetProfile());
   1788 
   1789   EXPECT_EQ(0u, tester.clear_count());
   1790 }
   1791 
   1792 TEST_F(BrowsingDataRemoverTest, DomainReliability_Beacons) {
   1793   ClearDomainReliabilityTester tester(GetProfile());
   1794 
   1795   BlockUntilBrowsingDataRemoved(
   1796       BrowsingDataRemover::EVERYTHING,
   1797       BrowsingDataRemover::REMOVE_HISTORY, false);
   1798   EXPECT_EQ(1u, tester.clear_count());
   1799   EXPECT_EQ(CLEAR_BEACONS, tester.last_clear_mode());
   1800 }
   1801 
   1802 TEST_F(BrowsingDataRemoverTest, DomainReliability_Contexts) {
   1803   ClearDomainReliabilityTester tester(GetProfile());
   1804 
   1805   BlockUntilBrowsingDataRemoved(
   1806       BrowsingDataRemover::EVERYTHING,
   1807       BrowsingDataRemover::REMOVE_COOKIES, false);
   1808   EXPECT_EQ(1u, tester.clear_count());
   1809   EXPECT_EQ(CLEAR_CONTEXTS, tester.last_clear_mode());
   1810 }
   1811 
   1812 TEST_F(BrowsingDataRemoverTest, DomainReliability_ContextsWin) {
   1813   ClearDomainReliabilityTester tester(GetProfile());
   1814 
   1815   BlockUntilBrowsingDataRemoved(
   1816       BrowsingDataRemover::EVERYTHING,
   1817       BrowsingDataRemover::REMOVE_HISTORY |
   1818       BrowsingDataRemover::REMOVE_COOKIES, false);
   1819   EXPECT_EQ(1u, tester.clear_count());
   1820   EXPECT_EQ(CLEAR_CONTEXTS, tester.last_clear_mode());
   1821 }
   1822 
   1823 TEST_F(BrowsingDataRemoverTest, DomainReliability_ProtectedOrigins) {
   1824   ClearDomainReliabilityTester tester(GetProfile());
   1825 
   1826   BlockUntilBrowsingDataRemoved(
   1827       BrowsingDataRemover::EVERYTHING,
   1828       BrowsingDataRemover::REMOVE_COOKIES, true);
   1829   EXPECT_EQ(1u, tester.clear_count());
   1830   EXPECT_EQ(CLEAR_CONTEXTS, tester.last_clear_mode());
   1831 }
   1832 
   1833 TEST_F(BrowsingDataRemoverTest, DomainReliability_NoMonitor) {
   1834   BlockUntilBrowsingDataRemoved(
   1835       BrowsingDataRemover::EVERYTHING,
   1836       BrowsingDataRemover::REMOVE_HISTORY |
   1837       BrowsingDataRemover::REMOVE_COOKIES, false);
   1838 }
   1839