Home | History | Annotate | Download | only in browser
      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
      3 // found in the LICENSE file.
      4 
      5 #include "base/file_util.h"
      6 #include "base/files/scoped_temp_dir.h"
      7 #include "base/message_loop/message_loop_proxy.h"
      8 #include "base/run_loop.h"
      9 #include "base/threading/thread.h"
     10 #include "content/browser/browser_thread_impl.h"
     11 #include "content/browser/gpu/shader_disk_cache.h"
     12 #include "content/browser/storage_partition_impl.h"
     13 #include "content/public/browser/local_storage_usage_info.h"
     14 #include "content/public/browser/storage_partition.h"
     15 #include "content/public/test/test_browser_context.h"
     16 #include "content/public/test/test_browser_thread.h"
     17 #include "content/public/test/test_browser_thread_bundle.h"
     18 #include "net/base/test_completion_callback.h"
     19 #include "net/cookies/cookie_monster.h"
     20 #include "net/url_request/url_request_context.h"
     21 #include "net/url_request/url_request_context_getter.h"
     22 #include "testing/gtest/include/gtest/gtest.h"
     23 #include "webkit/browser/quota/mock_quota_manager.h"
     24 #include "webkit/browser/quota/mock_special_storage_policy.h"
     25 #include "webkit/browser/quota/quota_manager.h"
     26 
     27 namespace content {
     28 namespace {
     29 
     30 const int kDefaultClientId = 42;
     31 const char kCacheKey[] = "key";
     32 const char kCacheValue[] = "cached value";
     33 
     34 const char kTestOrigin1[] = "http://host1:1/";
     35 const char kTestOrigin2[] = "http://host2:1/";
     36 const char kTestOrigin3[] = "http://host3:1/";
     37 const char kTestOriginDevTools[] = "chrome-devtools://abcdefghijklmnopqrstuvw/";
     38 
     39 const GURL kOrigin1(kTestOrigin1);
     40 const GURL kOrigin2(kTestOrigin2);
     41 const GURL kOrigin3(kTestOrigin3);
     42 const GURL kOriginDevTools(kTestOriginDevTools);
     43 
     44 const base::FilePath::CharType kDomStorageOrigin1[] =
     45     FILE_PATH_LITERAL("http_host1_1.localstorage");
     46 
     47 const base::FilePath::CharType kDomStorageOrigin2[] =
     48     FILE_PATH_LITERAL("http_host2_1.localstorage");
     49 
     50 const base::FilePath::CharType kDomStorageOrigin3[] =
     51     FILE_PATH_LITERAL("http_host3_1.localstorage");
     52 
     53 const quota::StorageType kTemporary = quota::kStorageTypeTemporary;
     54 const quota::StorageType kPersistent = quota::kStorageTypePersistent;
     55 
     56 const quota::QuotaClient::ID kClientFile = quota::QuotaClient::kFileSystem;
     57 
     58 const uint32 kAllQuotaRemoveMask =
     59     StoragePartition::REMOVE_DATA_MASK_INDEXEDDB |
     60     StoragePartition::REMOVE_DATA_MASK_WEBSQL |
     61     StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
     62     StoragePartition::REMOVE_DATA_MASK_APPCACHE;
     63 
     64 class TestClosureCallback {
     65  public:
     66   TestClosureCallback()
     67       : callback_(base::Bind(
     68           &TestClosureCallback::StopWaiting, base::Unretained(this))) {
     69   }
     70 
     71   void WaitForResult() {
     72     wait_run_loop_.reset(new base::RunLoop());
     73     wait_run_loop_->Run();
     74   }
     75 
     76   const base::Closure& callback() { return callback_; }
     77 
     78  private:
     79   void StopWaiting() {
     80     wait_run_loop_->Quit();
     81   }
     82 
     83   base::Closure callback_;
     84   scoped_ptr<base::RunLoop> wait_run_loop_;
     85 
     86   DISALLOW_COPY_AND_ASSIGN(TestClosureCallback);
     87 };
     88 
     89 class AwaitCompletionHelper {
     90  public:
     91   AwaitCompletionHelper() : start_(false), already_quit_(false) {}
     92   virtual ~AwaitCompletionHelper() {}
     93 
     94   void BlockUntilNotified() {
     95     if (!already_quit_) {
     96       DCHECK(!start_);
     97       start_ = true;
     98       base::MessageLoop::current()->Run();
     99     } else {
    100       DCHECK(!start_);
    101       already_quit_ = false;
    102     }
    103   }
    104 
    105   void Notify() {
    106     if (start_) {
    107       DCHECK(!already_quit_);
    108       base::MessageLoop::current()->Quit();
    109       start_ = false;
    110     } else {
    111       DCHECK(!already_quit_);
    112       already_quit_ = true;
    113     }
    114   }
    115 
    116  private:
    117   // Helps prevent from running message_loop, if the callback invoked
    118   // immediately.
    119   bool start_;
    120   bool already_quit_;
    121 
    122   DISALLOW_COPY_AND_ASSIGN(AwaitCompletionHelper);
    123 };
    124 
    125 class RemoveCookieTester {
    126  public:
    127   explicit RemoveCookieTester(TestBrowserContext* context)
    128       : get_cookie_success_(false), monster_(NULL) {
    129     SetMonster(context->GetRequestContext()->GetURLRequestContext()->
    130                    cookie_store()->GetCookieMonster());
    131   }
    132 
    133   // Returns true, if the given cookie exists in the cookie store.
    134   bool ContainsCookie() {
    135     get_cookie_success_ = false;
    136     monster_->GetCookiesWithOptionsAsync(
    137         kOrigin1, net::CookieOptions(),
    138         base::Bind(&RemoveCookieTester::GetCookieCallback,
    139                    base::Unretained(this)));
    140     await_completion_.BlockUntilNotified();
    141     return get_cookie_success_;
    142   }
    143 
    144   void AddCookie() {
    145     monster_->SetCookieWithOptionsAsync(
    146         kOrigin1, "A=1", net::CookieOptions(),
    147         base::Bind(&RemoveCookieTester::SetCookieCallback,
    148                    base::Unretained(this)));
    149     await_completion_.BlockUntilNotified();
    150   }
    151 
    152  protected:
    153   void SetMonster(net::CookieStore* monster) {
    154     monster_ = monster;
    155   }
    156 
    157  private:
    158   void GetCookieCallback(const std::string& cookies) {
    159     if (cookies == "A=1") {
    160       get_cookie_success_ = true;
    161     } else {
    162       EXPECT_EQ("", cookies);
    163       get_cookie_success_ = false;
    164     }
    165     await_completion_.Notify();
    166   }
    167 
    168   void SetCookieCallback(bool result) {
    169     ASSERT_TRUE(result);
    170     await_completion_.Notify();
    171   }
    172 
    173   bool get_cookie_success_;
    174   AwaitCompletionHelper await_completion_;
    175   net::CookieStore* monster_;
    176 
    177   DISALLOW_COPY_AND_ASSIGN(RemoveCookieTester);
    178 };
    179 
    180 class RemoveLocalStorageTester {
    181  public:
    182   explicit RemoveLocalStorageTester(TestBrowserContext* profile)
    183       : profile_(profile), dom_storage_context_(NULL) {
    184     dom_storage_context_ =
    185         content::BrowserContext::GetDefaultStoragePartition(profile)->
    186             GetDOMStorageContext();
    187   }
    188 
    189   // Returns true, if the given origin URL exists.
    190   bool DOMStorageExistsForOrigin(const GURL& origin) {
    191     GetLocalStorageUsage();
    192     await_completion_.BlockUntilNotified();
    193     for (size_t i = 0; i < infos_.size(); ++i) {
    194       if (origin == infos_[i].origin)
    195         return true;
    196     }
    197     return false;
    198   }
    199 
    200   void AddDOMStorageTestData() {
    201     // Note: This test depends on details of how the dom_storage library
    202     // stores data in the host file system.
    203     base::FilePath storage_path =
    204         profile_->GetPath().AppendASCII("Local Storage");
    205     base::CreateDirectory(storage_path);
    206 
    207     // Write some files.
    208     file_util::WriteFile(storage_path.Append(kDomStorageOrigin1), NULL, 0);
    209     file_util::WriteFile(storage_path.Append(kDomStorageOrigin2), NULL, 0);
    210     file_util::WriteFile(storage_path.Append(kDomStorageOrigin3), NULL, 0);
    211 
    212     // Tweak their dates.
    213     base::Time now = base::Time::Now();
    214     base::TouchFile(storage_path.Append(kDomStorageOrigin1), now, now);
    215 
    216     base::Time one_day_ago = now - base::TimeDelta::FromDays(1);
    217     base::TouchFile(storage_path.Append(kDomStorageOrigin2),
    218                     one_day_ago, one_day_ago);
    219 
    220     base::Time sixty_days_ago = now - base::TimeDelta::FromDays(60);
    221     base::TouchFile(storage_path.Append(kDomStorageOrigin3),
    222                     sixty_days_ago, sixty_days_ago);
    223   }
    224 
    225  private:
    226   void GetLocalStorageUsage() {
    227     dom_storage_context_->GetLocalStorageUsage(
    228         base::Bind(&RemoveLocalStorageTester::OnGotLocalStorageUsage,
    229                    base::Unretained(this)));
    230   }
    231   void OnGotLocalStorageUsage(
    232       const std::vector<content::LocalStorageUsageInfo>& infos) {
    233     infos_ = infos;
    234     await_completion_.Notify();
    235   }
    236 
    237   // We don't own these pointers.
    238   TestBrowserContext* profile_;
    239   content::DOMStorageContext* dom_storage_context_;
    240 
    241   std::vector<content::LocalStorageUsageInfo> infos_;
    242 
    243   AwaitCompletionHelper await_completion_;
    244 
    245   DISALLOW_COPY_AND_ASSIGN(RemoveLocalStorageTester);
    246 };
    247 
    248 bool IsWebSafeSchemeForTest(const std::string& scheme) {
    249   return scheme == "http";
    250 }
    251 
    252 bool DoesOriginMatchForUnprotectedWeb(
    253     const GURL& origin,
    254     quota::SpecialStoragePolicy* special_storage_policy) {
    255   if (IsWebSafeSchemeForTest(origin.scheme()))
    256     return !special_storage_policy->IsStorageProtected(origin.GetOrigin());
    257 
    258   return false;
    259 }
    260 
    261 bool DoesOriginMatchForBothProtectedAndUnprotectedWeb(
    262     const GURL& origin,
    263     quota::SpecialStoragePolicy* special_storage_policy) {
    264   return true;
    265 }
    266 
    267 bool DoesOriginMatchUnprotected(
    268     const GURL& origin,
    269     quota::SpecialStoragePolicy* special_storage_policy) {
    270   return origin.GetOrigin().scheme() != kOriginDevTools.scheme();
    271 }
    272 
    273 void ClearQuotaData(content::StoragePartition* storage_partition,
    274                     const base::Closure& cb) {
    275   storage_partition->ClearData(
    276       kAllQuotaRemoveMask,
    277       StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
    278       NULL, StoragePartition::OriginMatcherFunction(),
    279       base::Time(), base::Time::Max(), cb);
    280 }
    281 
    282 void ClearQuotaDataWithOriginMatcher(
    283     content::StoragePartition* storage_partition,
    284     const GURL& remove_origin,
    285     const StoragePartition::OriginMatcherFunction& origin_matcher,
    286     const base::Time delete_begin,
    287     const base::Closure& cb) {
    288   storage_partition->ClearData(kAllQuotaRemoveMask,
    289                                StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
    290                                &remove_origin, origin_matcher, delete_begin,
    291                                base::Time::Max(), cb);
    292 }
    293 
    294 void ClearQuotaDataForOrigin(
    295     content::StoragePartition* storage_partition,
    296     const GURL& remove_origin,
    297     const base::Time delete_begin,
    298     const base::Closure& cb) {
    299   ClearQuotaDataWithOriginMatcher(
    300       storage_partition, remove_origin,
    301       StoragePartition::OriginMatcherFunction(), delete_begin, cb);
    302 }
    303 
    304 void ClearQuotaDataForNonPersistent(
    305     content::StoragePartition* storage_partition,
    306     const base::Time delete_begin,
    307     const base::Closure& cb) {
    308   uint32 quota_storage_remove_mask_no_persistent =
    309       StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL &
    310       ~StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT;
    311   storage_partition->ClearData(
    312       kAllQuotaRemoveMask, quota_storage_remove_mask_no_persistent,
    313       NULL, StoragePartition::OriginMatcherFunction(),
    314       delete_begin, base::Time::Max(), cb);
    315 }
    316 
    317 void ClearCookies(content::StoragePartition* storage_partition,
    318                   const base::Time delete_begin,
    319                   const base::Time delete_end,
    320                   const base::Closure& cb) {
    321   storage_partition->ClearData(
    322       StoragePartition::REMOVE_DATA_MASK_COOKIES,
    323       StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
    324       NULL, StoragePartition::OriginMatcherFunction(),
    325       delete_begin, delete_end, cb);
    326 }
    327 
    328 void ClearStuff(uint32 remove_mask,
    329                 content::StoragePartition* storage_partition,
    330                 const base::Time delete_begin,
    331                 const base::Time delete_end,
    332                 const StoragePartition::OriginMatcherFunction& origin_matcher,
    333                 const base::Closure& cb) {
    334   storage_partition->ClearData(
    335       remove_mask, StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
    336       NULL, origin_matcher, delete_begin, delete_end, cb);
    337 }
    338 
    339 }  // namespace
    340 
    341 class StoragePartitionImplTest : public testing::Test {
    342  public:
    343   StoragePartitionImplTest()
    344       : browser_context_(new TestBrowserContext()),
    345         thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
    346   }
    347   virtual ~StoragePartitionImplTest() {}
    348 
    349   quota::MockQuotaManager* GetMockManager() {
    350     if (!quota_manager_.get()) {
    351       quota_manager_ = new quota::MockQuotaManager(
    352           browser_context_->IsOffTheRecord(),
    353           browser_context_->GetPath(),
    354           BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get(),
    355           BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB).get(),
    356           browser_context_->GetSpecialStoragePolicy());
    357     }
    358     return quota_manager_.get();
    359   }
    360 
    361   TestBrowserContext* GetBrowserContext() {
    362     return browser_context_.get();
    363   }
    364 
    365  private:
    366   scoped_ptr<TestBrowserContext> browser_context_;
    367   scoped_refptr<quota::MockQuotaManager> quota_manager_;
    368   content::TestBrowserThreadBundle thread_bundle_;
    369 
    370   DISALLOW_COPY_AND_ASSIGN(StoragePartitionImplTest);
    371 };
    372 
    373 class StoragePartitionShaderClearTest : public testing::Test {
    374  public:
    375   StoragePartitionShaderClearTest()
    376       : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
    377   }
    378 
    379   virtual ~StoragePartitionShaderClearTest() {}
    380 
    381   const base::FilePath& cache_path() { return temp_dir_.path(); }
    382 
    383   virtual void SetUp() OVERRIDE {
    384     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
    385     ShaderCacheFactory::GetInstance()->SetCacheInfo(kDefaultClientId,
    386                                                     cache_path());
    387 
    388     cache_ = ShaderCacheFactory::GetInstance()->Get(kDefaultClientId);
    389     ASSERT_TRUE(cache_.get() != NULL);
    390   }
    391 
    392   void InitCache() {
    393     net::TestCompletionCallback available_cb;
    394     int rv = cache_->SetAvailableCallback(available_cb.callback());
    395     ASSERT_EQ(net::OK, available_cb.GetResult(rv));
    396     EXPECT_EQ(0, cache_->Size());
    397 
    398     cache_->Cache(kCacheKey, kCacheValue);
    399 
    400     net::TestCompletionCallback complete_cb;
    401 
    402     rv = cache_->SetCacheCompleteCallback(complete_cb.callback());
    403     ASSERT_EQ(net::OK, complete_cb.GetResult(rv));
    404   }
    405 
    406   size_t Size() { return cache_->Size(); }
    407 
    408  private:
    409   virtual void TearDown() OVERRIDE {
    410     cache_ = NULL;
    411     ShaderCacheFactory::GetInstance()->RemoveCacheInfo(kDefaultClientId);
    412   }
    413 
    414   base::ScopedTempDir temp_dir_;
    415   content::TestBrowserThreadBundle thread_bundle_;
    416 
    417   scoped_refptr<ShaderDiskCache> cache_;
    418 };
    419 
    420 void ClearData(content::StoragePartitionImpl* sp,
    421                const base::Closure& cb) {
    422   base::Time time;
    423   sp->ClearData(
    424       StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE,
    425       StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
    426       NULL, StoragePartition::OriginMatcherFunction(),
    427       time, time, cb);
    428 }
    429 
    430 // Tests ---------------------------------------------------------------------
    431 
    432 TEST_F(StoragePartitionShaderClearTest, ClearShaderCache) {
    433   InitCache();
    434   EXPECT_EQ(1u, Size());
    435 
    436   TestClosureCallback clear_cb;
    437   StoragePartitionImpl storage_partition(
    438       cache_path(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    439   base::MessageLoop::current()->PostTask(
    440       FROM_HERE, base::Bind(&ClearData, &storage_partition,
    441                             clear_cb.callback()));
    442   clear_cb.WaitForResult();
    443   EXPECT_EQ(0u, Size());
    444 }
    445 
    446 TEST_F(StoragePartitionImplTest, QuotaClientMaskGeneration) {
    447   EXPECT_EQ(quota::QuotaClient::kFileSystem,
    448             StoragePartitionImpl::GenerateQuotaClientMask(
    449                 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS));
    450   EXPECT_EQ(quota::QuotaClient::kDatabase,
    451             StoragePartitionImpl::GenerateQuotaClientMask(
    452                 StoragePartition::REMOVE_DATA_MASK_WEBSQL));
    453   EXPECT_EQ(quota::QuotaClient::kAppcache,
    454             StoragePartitionImpl::GenerateQuotaClientMask(
    455                 StoragePartition::REMOVE_DATA_MASK_APPCACHE));
    456   EXPECT_EQ(quota::QuotaClient::kIndexedDatabase,
    457             StoragePartitionImpl::GenerateQuotaClientMask(
    458                 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB));
    459   EXPECT_EQ(quota::QuotaClient::kFileSystem |
    460             quota::QuotaClient::kDatabase |
    461             quota::QuotaClient::kAppcache |
    462             quota::QuotaClient::kIndexedDatabase,
    463             StoragePartitionImpl::GenerateQuotaClientMask(
    464                 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
    465                 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
    466                 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
    467                 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB));
    468 }
    469 
    470 void PopulateTestQuotaManagedPersistentData(quota::MockQuotaManager* manager) {
    471   manager->AddOrigin(kOrigin2, kPersistent, kClientFile, base::Time());
    472   manager->AddOrigin(kOrigin3, kPersistent, kClientFile,
    473       base::Time::Now() - base::TimeDelta::FromDays(1));
    474 
    475   EXPECT_FALSE(manager->OriginHasData(kOrigin1, kPersistent, kClientFile));
    476   EXPECT_TRUE(manager->OriginHasData(kOrigin2, kPersistent, kClientFile));
    477   EXPECT_TRUE(manager->OriginHasData(kOrigin3, kPersistent, kClientFile));
    478 }
    479 
    480 void PopulateTestQuotaManagedTemporaryData(quota::MockQuotaManager* manager) {
    481   manager->AddOrigin(kOrigin1, kTemporary, kClientFile, base::Time::Now());
    482   manager->AddOrigin(kOrigin3, kTemporary, kClientFile,
    483       base::Time::Now() - base::TimeDelta::FromDays(1));
    484 
    485   EXPECT_TRUE(manager->OriginHasData(kOrigin1, kTemporary, kClientFile));
    486   EXPECT_FALSE(manager->OriginHasData(kOrigin2, kTemporary, kClientFile));
    487   EXPECT_TRUE(manager->OriginHasData(kOrigin3, kTemporary, kClientFile));
    488 }
    489 
    490 void PopulateTestQuotaManagedData(quota::MockQuotaManager* manager) {
    491   // Set up kOrigin1 with a temporary quota, kOrigin2 with a persistent
    492   // quota, and kOrigin3 with both. kOrigin1 is modified now, kOrigin2
    493   // is modified at the beginning of time, and kOrigin3 is modified one day
    494   // ago.
    495   PopulateTestQuotaManagedPersistentData(manager);
    496   PopulateTestQuotaManagedTemporaryData(manager);
    497 }
    498 
    499 void PopulateTestQuotaManagedNonBrowsingData(quota::MockQuotaManager* manager) {
    500   manager->AddOrigin(kOriginDevTools, kTemporary, kClientFile, base::Time());
    501   manager->AddOrigin(kOriginDevTools, kPersistent, kClientFile, base::Time());
    502 }
    503 
    504 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForeverBoth) {
    505   PopulateTestQuotaManagedData(GetMockManager());
    506 
    507   TestClosureCallback clear_cb;
    508   StoragePartitionImpl sp(
    509       base::FilePath(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    510   static_cast<StoragePartitionImpl*>(&sp)->OverrideQuotaManagerForTesting(
    511       GetMockManager());
    512   base::MessageLoop::current()->PostTask(
    513       FROM_HERE, base::Bind(&ClearQuotaData, &sp, clear_cb.callback()));
    514   clear_cb.WaitForResult();
    515 
    516   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
    517       kClientFile));
    518   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
    519       kClientFile));
    520   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
    521       kClientFile));
    522   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
    523       kClientFile));
    524   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
    525       kClientFile));
    526   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
    527       kClientFile));
    528 }
    529 
    530 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForeverOnlyTemporary) {
    531   PopulateTestQuotaManagedTemporaryData(GetMockManager());
    532 
    533   TestClosureCallback clear_cb;
    534   StoragePartitionImpl sp(
    535       base::FilePath(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    536   static_cast<StoragePartitionImpl*>(&sp)->OverrideQuotaManagerForTesting(
    537       GetMockManager());
    538   base::MessageLoop::current()->PostTask(
    539       FROM_HERE, base::Bind(&ClearQuotaData, &sp, clear_cb.callback()));
    540   clear_cb.WaitForResult();
    541 
    542   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
    543       kClientFile));
    544   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
    545       kClientFile));
    546   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
    547       kClientFile));
    548   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
    549       kClientFile));
    550   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
    551       kClientFile));
    552   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
    553       kClientFile));
    554 }
    555 
    556 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForeverOnlyPersistent) {
    557   PopulateTestQuotaManagedPersistentData(GetMockManager());
    558 
    559   TestClosureCallback clear_cb;
    560   StoragePartitionImpl sp(
    561       base::FilePath(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    562   static_cast<StoragePartitionImpl*>(&sp)->OverrideQuotaManagerForTesting(
    563       GetMockManager());
    564   base::MessageLoop::current()->PostTask(
    565       FROM_HERE, base::Bind(&ClearQuotaData, &sp, clear_cb.callback()));
    566   clear_cb.WaitForResult();
    567 
    568   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
    569       kClientFile));
    570   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
    571       kClientFile));
    572   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
    573       kClientFile));
    574   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
    575       kClientFile));
    576   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
    577       kClientFile));
    578   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
    579       kClientFile));
    580 }
    581 
    582 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForeverNeither) {
    583   TestClosureCallback clear_cb;
    584   StoragePartitionImpl sp(
    585       base::FilePath(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    586   static_cast<StoragePartitionImpl*>(&sp)->OverrideQuotaManagerForTesting(
    587       GetMockManager());
    588   base::MessageLoop::current()->PostTask(
    589       FROM_HERE, base::Bind(&ClearQuotaData, &sp, clear_cb.callback()));
    590   clear_cb.WaitForResult();
    591 
    592   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
    593       kClientFile));
    594   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
    595       kClientFile));
    596   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
    597       kClientFile));
    598   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
    599       kClientFile));
    600   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
    601       kClientFile));
    602   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
    603       kClientFile));
    604 }
    605 
    606 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForeverSpecificOrigin) {
    607   PopulateTestQuotaManagedData(GetMockManager());
    608 
    609   TestClosureCallback clear_cb;
    610   StoragePartitionImpl sp(
    611       base::FilePath(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    612   static_cast<StoragePartitionImpl*>(&sp)->OverrideQuotaManagerForTesting(
    613       GetMockManager());
    614   base::MessageLoop::current()->PostTask(
    615       FROM_HERE, base::Bind(&ClearQuotaDataForOrigin,
    616                             &sp, kOrigin1, base::Time(), clear_cb.callback()));
    617   clear_cb.WaitForResult();
    618 
    619   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
    620       kClientFile));
    621   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
    622       kClientFile));
    623   EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
    624       kClientFile));
    625   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
    626       kClientFile));
    627   EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
    628       kClientFile));
    629   EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
    630       kClientFile));
    631 }
    632 
    633 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForLastHour) {
    634   PopulateTestQuotaManagedData(GetMockManager());
    635 
    636   TestClosureCallback clear_cb;
    637   StoragePartitionImpl sp(
    638       base::FilePath(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    639   static_cast<StoragePartitionImpl*>(&sp)->OverrideQuotaManagerForTesting(
    640       GetMockManager());
    641   base::MessageLoop::current()->PostTask(
    642       FROM_HERE, base::Bind(&ClearQuotaDataForOrigin,
    643                             &sp, GURL(),
    644                             base::Time::Now() - base::TimeDelta::FromHours(1),
    645                             clear_cb.callback()));
    646   clear_cb.WaitForResult();
    647 
    648   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
    649       kClientFile));
    650   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
    651       kClientFile));
    652   EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
    653       kClientFile));
    654   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
    655       kClientFile));
    656   EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
    657       kClientFile));
    658   EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
    659       kClientFile));
    660 }
    661 
    662 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForLastWeek) {
    663   PopulateTestQuotaManagedData(GetMockManager());
    664 
    665   TestClosureCallback clear_cb;
    666   StoragePartitionImpl sp(
    667       base::FilePath(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    668   static_cast<StoragePartitionImpl*>(&sp)->OverrideQuotaManagerForTesting(
    669       GetMockManager());
    670   base::MessageLoop::current()->PostTask(
    671       FROM_HERE, base::Bind(&ClearQuotaDataForNonPersistent,
    672                             &sp,
    673                             base::Time::Now() - base::TimeDelta::FromDays(7),
    674                             clear_cb.callback()));
    675   clear_cb.WaitForResult();
    676 
    677   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
    678       kClientFile));
    679   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
    680       kClientFile));
    681   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
    682       kClientFile));
    683   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
    684       kClientFile));
    685   EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
    686       kClientFile));
    687   EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
    688       kClientFile));
    689 }
    690 
    691 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedUnprotectedOrigins) {
    692   // Protect kOrigin1.
    693   scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy =
    694       new quota::MockSpecialStoragePolicy;
    695   mock_policy->AddProtected(kOrigin1.GetOrigin());
    696 
    697   PopulateTestQuotaManagedData(GetMockManager());
    698 
    699   TestClosureCallback clear_cb;
    700   StoragePartitionImpl sp(
    701       base::FilePath(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    702   static_cast<StoragePartitionImpl*>(&sp)->OverrideQuotaManagerForTesting(
    703       GetMockManager());
    704   static_cast<StoragePartitionImpl*>(
    705       &sp)->OverrideSpecialStoragePolicyForTesting(mock_policy);
    706   base::MessageLoop::current()->PostTask(
    707       FROM_HERE, base::Bind(&ClearQuotaDataWithOriginMatcher,
    708                             &sp, GURL(),
    709                             base::Bind(&DoesOriginMatchForUnprotectedWeb),
    710                             base::Time(), clear_cb.callback()));
    711   clear_cb.WaitForResult();
    712 
    713   EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
    714       kClientFile));
    715   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
    716       kClientFile));
    717   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
    718       kClientFile));
    719   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
    720       kClientFile));
    721   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
    722       kClientFile));
    723   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
    724       kClientFile));
    725 }
    726 
    727 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedProtectedSpecificOrigin) {
    728   // Protect kOrigin1.
    729   scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy =
    730       new quota::MockSpecialStoragePolicy;
    731   mock_policy->AddProtected(kOrigin1.GetOrigin());
    732 
    733   PopulateTestQuotaManagedData(GetMockManager());
    734 
    735   TestClosureCallback clear_cb;
    736   StoragePartitionImpl sp(
    737       base::FilePath(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    738   static_cast<StoragePartitionImpl*>(&sp)->OverrideQuotaManagerForTesting(
    739       GetMockManager());
    740   static_cast<StoragePartitionImpl*>(
    741       &sp)->OverrideSpecialStoragePolicyForTesting(mock_policy);
    742 
    743   // Try to remove kOrigin1. Expect failure.
    744   base::MessageLoop::current()->PostTask(
    745       FROM_HERE, base::Bind(&ClearQuotaDataWithOriginMatcher,
    746                             &sp, kOrigin1,
    747                             base::Bind(&DoesOriginMatchForUnprotectedWeb),
    748                             base::Time(), clear_cb.callback()));
    749   clear_cb.WaitForResult();
    750 
    751   EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
    752       kClientFile));
    753   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
    754       kClientFile));
    755   EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
    756       kClientFile));
    757   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
    758       kClientFile));
    759   EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
    760       kClientFile));
    761   EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
    762       kClientFile));
    763 }
    764 
    765 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedProtectedOrigins) {
    766   // Protect kOrigin1.
    767   scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy =
    768       new quota::MockSpecialStoragePolicy;
    769   mock_policy->AddProtected(kOrigin1.GetOrigin());
    770 
    771   PopulateTestQuotaManagedData(GetMockManager());
    772 
    773   // Try to remove kOrigin1. Expect success.
    774   TestClosureCallback clear_cb;
    775   StoragePartitionImpl sp(
    776       base::FilePath(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    777   static_cast<StoragePartitionImpl*>(&sp)->OverrideQuotaManagerForTesting(
    778       GetMockManager());
    779   static_cast<StoragePartitionImpl*>(
    780       &sp)->OverrideSpecialStoragePolicyForTesting(mock_policy);
    781   base::MessageLoop::current()->PostTask(
    782       FROM_HERE,
    783       base::Bind(&ClearQuotaDataWithOriginMatcher,
    784                  &sp, GURL(),
    785                  base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb),
    786                  base::Time(), clear_cb.callback()));
    787   clear_cb.WaitForResult();
    788 
    789   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
    790       kClientFile));
    791   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
    792       kClientFile));
    793   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
    794       kClientFile));
    795   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
    796       kClientFile));
    797   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
    798       kClientFile));
    799   EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
    800       kClientFile));
    801 }
    802 
    803 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedIgnoreDevTools) {
    804   PopulateTestQuotaManagedNonBrowsingData(GetMockManager());
    805 
    806   TestClosureCallback clear_cb;
    807   StoragePartitionImpl sp(
    808       base::FilePath(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    809   static_cast<StoragePartitionImpl*>(&sp)->OverrideQuotaManagerForTesting(
    810       GetMockManager());
    811   base::MessageLoop::current()->PostTask(
    812       FROM_HERE, base::Bind(&ClearQuotaDataWithOriginMatcher,
    813                             &sp, GURL(),
    814                             base::Bind(&DoesOriginMatchUnprotected),
    815                             base::Time(), clear_cb.callback()));
    816   clear_cb.WaitForResult();
    817 
    818   // Check that devtools data isn't removed.
    819   EXPECT_TRUE(GetMockManager()->OriginHasData(kOriginDevTools, kTemporary,
    820       kClientFile));
    821   EXPECT_TRUE(GetMockManager()->OriginHasData(kOriginDevTools, kPersistent,
    822       kClientFile));
    823 }
    824 
    825 TEST_F(StoragePartitionImplTest, RemoveCookieForever) {
    826   RemoveCookieTester tester(GetBrowserContext());
    827 
    828   tester.AddCookie();
    829   ASSERT_TRUE(tester.ContainsCookie());
    830 
    831   TestClosureCallback clear_cb;
    832   StoragePartitionImpl sp(
    833       base::FilePath(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    834   sp.SetURLRequestContext(GetBrowserContext()->GetRequestContext());
    835   base::MessageLoop::current()->PostTask(
    836       FROM_HERE, base::Bind(&ClearCookies,
    837                             &sp, base::Time(), base::Time::Max(),
    838                             clear_cb.callback()));
    839   clear_cb.WaitForResult();
    840 
    841   EXPECT_FALSE(tester.ContainsCookie());
    842 }
    843 
    844 TEST_F(StoragePartitionImplTest, RemoveCookieLastHour) {
    845   RemoveCookieTester tester(GetBrowserContext());
    846 
    847   tester.AddCookie();
    848   ASSERT_TRUE(tester.ContainsCookie());
    849 
    850   TestClosureCallback clear_cb;
    851   StoragePartitionImpl sp(
    852       base::FilePath(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    853   base::Time an_hour_ago = base::Time::Now() - base::TimeDelta::FromHours(1);
    854   sp.SetURLRequestContext(GetBrowserContext()->GetRequestContext());
    855   base::MessageLoop::current()->PostTask(
    856       FROM_HERE, base::Bind(&ClearCookies,
    857                             &sp, an_hour_ago, base::Time::Max(),
    858                             clear_cb.callback()));
    859   clear_cb.WaitForResult();
    860 
    861   EXPECT_FALSE(tester.ContainsCookie());
    862 }
    863 
    864 TEST_F(StoragePartitionImplTest, RemoveUnprotectedLocalStorageForever) {
    865   // Protect kOrigin1.
    866   scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy =
    867       new quota::MockSpecialStoragePolicy;
    868   mock_policy->AddProtected(kOrigin1.GetOrigin());
    869 
    870   RemoveLocalStorageTester tester(GetBrowserContext());
    871 
    872   tester.AddDOMStorageTestData();
    873   EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin1));
    874   EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin2));
    875   EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3));
    876 
    877   TestClosureCallback clear_cb;
    878   DOMStorageContextWrapper* dom_storage_context =
    879       static_cast<DOMStorageContextWrapper*>(
    880           content::BrowserContext::GetDefaultStoragePartition(
    881               GetBrowserContext())->GetDOMStorageContext());
    882   StoragePartitionImpl sp(base::FilePath(), NULL, NULL, NULL, NULL,
    883                           dom_storage_context, NULL, NULL, NULL, NULL);
    884   static_cast<StoragePartitionImpl*>(
    885       &sp)->OverrideSpecialStoragePolicyForTesting(mock_policy);
    886   base::MessageLoop::current()->PostTask(
    887       FROM_HERE,
    888       base::Bind(&ClearStuff,
    889                  StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE,
    890                  &sp, base::Time(), base::Time::Max(),
    891                  base::Bind(&DoesOriginMatchForUnprotectedWeb),
    892                  clear_cb.callback()));
    893   clear_cb.WaitForResult();
    894 
    895   EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin1));
    896   EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2));
    897   EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin3));
    898 }
    899 
    900 TEST_F(StoragePartitionImplTest, RemoveProtectedLocalStorageForever) {
    901   // Protect kOrigin1.
    902   scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy =
    903       new quota::MockSpecialStoragePolicy;
    904   mock_policy->AddProtected(kOrigin1.GetOrigin());
    905 
    906   RemoveLocalStorageTester tester(GetBrowserContext());
    907 
    908   tester.AddDOMStorageTestData();
    909   EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin1));
    910   EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin2));
    911   EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3));
    912 
    913   TestClosureCallback clear_cb;
    914   DOMStorageContextWrapper* dom_storage_context =
    915       static_cast<DOMStorageContextWrapper*>(
    916           content::BrowserContext::GetDefaultStoragePartition(
    917               GetBrowserContext())->GetDOMStorageContext());
    918   StoragePartitionImpl sp(base::FilePath(), NULL, NULL, NULL, NULL,
    919                           dom_storage_context, NULL, NULL, NULL, NULL);
    920   static_cast<StoragePartitionImpl*>(
    921       &sp)->OverrideSpecialStoragePolicyForTesting(mock_policy);
    922   base::MessageLoop::current()->PostTask(
    923       FROM_HERE,
    924       base::Bind(&ClearStuff,
    925                  StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE,
    926                  &sp, base::Time(), base::Time::Max(),
    927                  base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb),
    928                  clear_cb.callback()));
    929   clear_cb.WaitForResult();
    930 
    931   // Even if kOrigin1 is protected, it will be deleted since we specify
    932   // ClearData to delete protected data.
    933   EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin1));
    934   EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2));
    935   EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin3));
    936 }
    937 
    938 TEST_F(StoragePartitionImplTest, RemoveLocalStorageForLastWeek) {
    939   RemoveLocalStorageTester tester(GetBrowserContext());
    940 
    941   tester.AddDOMStorageTestData();
    942   EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin1));
    943   EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin2));
    944   EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3));
    945 
    946   TestClosureCallback clear_cb;
    947   DOMStorageContextWrapper* dom_storage_context =
    948       static_cast<DOMStorageContextWrapper*>(
    949           content::BrowserContext::GetDefaultStoragePartition(
    950               GetBrowserContext())->GetDOMStorageContext());
    951   StoragePartitionImpl sp(base::FilePath(), NULL, NULL, NULL, NULL,
    952                           dom_storage_context, NULL, NULL, NULL, NULL);
    953   base::Time a_week_ago = base::Time::Now() - base::TimeDelta::FromDays(7);
    954   base::MessageLoop::current()->PostTask(
    955       FROM_HERE,
    956       base::Bind(&ClearStuff,
    957                  StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE,
    958                  &sp, a_week_ago, base::Time::Max(),
    959                  base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb),
    960                  clear_cb.callback()));
    961   clear_cb.WaitForResult();
    962 
    963   // kOrigin1 and kOrigin2 do not have age more than a week.
    964   EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin1));
    965   EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2));
    966   EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3));
    967 }
    968 
    969 }  // namespace content
    970