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.
5 #include "base/file_util.h"
6 #include "base/message_loop/message_loop_proxy.h"
7 #include "base/run_loop.h"
8 #include "base/threading/thread.h"
9 #include "content/browser/browser_thread_impl.h"
10 #include "content/browser/gpu/shader_disk_cache.h"
11 #include "content/browser/quota/mock_quota_manager.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/mock_special_storage_policy.h"
16 #include "content/public/test/test_browser_context.h"
17 #include "content/public/test/test_browser_thread.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "net/base/test_completion_callback.h"
20 #include "net/cookies/cookie_monster.h"
21 #include "net/url_request/url_request_context.h"
22 #include "net/url_request/url_request_context_getter.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "webkit/browser/quota/quota_manager.h"
29 const int kDefaultClientId
= 42;
30 const char kCacheKey
[] = "key";
31 const char kCacheValue
[] = "cached value";
33 const char kTestOrigin1
[] = "http://host1:1/";
34 const char kTestOrigin2
[] = "http://host2:1/";
35 const char kTestOrigin3
[] = "http://host3:1/";
36 const char kTestOriginDevTools
[] = "chrome-devtools://abcdefghijklmnopqrstuvw/";
38 const GURL
kOrigin1(kTestOrigin1
);
39 const GURL
kOrigin2(kTestOrigin2
);
40 const GURL
kOrigin3(kTestOrigin3
);
41 const GURL
kOriginDevTools(kTestOriginDevTools
);
43 const base::FilePath::CharType kDomStorageOrigin1
[] =
44 FILE_PATH_LITERAL("http_host1_1.localstorage");
46 const base::FilePath::CharType kDomStorageOrigin2
[] =
47 FILE_PATH_LITERAL("http_host2_1.localstorage");
49 const base::FilePath::CharType kDomStorageOrigin3
[] =
50 FILE_PATH_LITERAL("http_host3_1.localstorage");
52 const storage::StorageType kTemporary
= storage::kStorageTypeTemporary
;
53 const storage::StorageType kPersistent
= storage::kStorageTypePersistent
;
55 const storage::QuotaClient::ID kClientFile
= storage::QuotaClient::kFileSystem
;
57 const uint32 kAllQuotaRemoveMask
=
58 StoragePartition::REMOVE_DATA_MASK_APPCACHE
|
59 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS
|
60 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB
|
61 StoragePartition::REMOVE_DATA_MASK_WEBSQL
;
63 class AwaitCompletionHelper
{
65 AwaitCompletionHelper() : start_(false), already_quit_(false) {}
66 virtual ~AwaitCompletionHelper() {}
68 void BlockUntilNotified() {
72 base::MessageLoop::current()->Run();
75 already_quit_
= false;
81 DCHECK(!already_quit_
);
82 base::MessageLoop::current()->Quit();
85 DCHECK(!already_quit_
);
91 // Helps prevent from running message_loop, if the callback invoked
96 DISALLOW_COPY_AND_ASSIGN(AwaitCompletionHelper
);
99 class RemoveCookieTester
{
101 explicit RemoveCookieTester(TestBrowserContext
* context
)
102 : get_cookie_success_(false), monster_(NULL
) {
103 SetMonster(context
->GetRequestContext()->GetURLRequestContext()->
104 cookie_store()->GetCookieMonster());
107 // Returns true, if the given cookie exists in the cookie store.
108 bool ContainsCookie() {
109 get_cookie_success_
= false;
110 monster_
->GetCookiesWithOptionsAsync(
111 kOrigin1
, net::CookieOptions(),
112 base::Bind(&RemoveCookieTester::GetCookieCallback
,
113 base::Unretained(this)));
114 await_completion_
.BlockUntilNotified();
115 return get_cookie_success_
;
119 monster_
->SetCookieWithOptionsAsync(
120 kOrigin1
, "A=1", net::CookieOptions(),
121 base::Bind(&RemoveCookieTester::SetCookieCallback
,
122 base::Unretained(this)));
123 await_completion_
.BlockUntilNotified();
127 void SetMonster(net::CookieStore
* monster
) {
132 void GetCookieCallback(const std::string
& cookies
) {
133 if (cookies
== "A=1") {
134 get_cookie_success_
= true;
136 EXPECT_EQ("", cookies
);
137 get_cookie_success_
= false;
139 await_completion_
.Notify();
142 void SetCookieCallback(bool result
) {
144 await_completion_
.Notify();
147 bool get_cookie_success_
;
148 AwaitCompletionHelper await_completion_
;
149 net::CookieStore
* monster_
;
151 DISALLOW_COPY_AND_ASSIGN(RemoveCookieTester
);
154 class RemoveLocalStorageTester
{
156 explicit RemoveLocalStorageTester(TestBrowserContext
* profile
)
157 : profile_(profile
), dom_storage_context_(NULL
) {
158 dom_storage_context_
=
159 content::BrowserContext::GetDefaultStoragePartition(profile
)->
160 GetDOMStorageContext();
163 // Returns true, if the given origin URL exists.
164 bool DOMStorageExistsForOrigin(const GURL
& origin
) {
165 GetLocalStorageUsage();
166 await_completion_
.BlockUntilNotified();
167 for (size_t i
= 0; i
< infos_
.size(); ++i
) {
168 if (origin
== infos_
[i
].origin
)
174 void AddDOMStorageTestData() {
175 // Note: This test depends on details of how the dom_storage library
176 // stores data in the host file system.
177 base::FilePath storage_path
=
178 profile_
->GetPath().AppendASCII("Local Storage");
179 base::CreateDirectory(storage_path
);
182 base::WriteFile(storage_path
.Append(kDomStorageOrigin1
), NULL
, 0);
183 base::WriteFile(storage_path
.Append(kDomStorageOrigin2
), NULL
, 0);
184 base::WriteFile(storage_path
.Append(kDomStorageOrigin3
), NULL
, 0);
186 // Tweak their dates.
187 base::Time now
= base::Time::Now();
188 base::TouchFile(storage_path
.Append(kDomStorageOrigin1
), now
, now
);
190 base::Time one_day_ago
= now
- base::TimeDelta::FromDays(1);
191 base::TouchFile(storage_path
.Append(kDomStorageOrigin2
),
192 one_day_ago
, one_day_ago
);
194 base::Time sixty_days_ago
= now
- base::TimeDelta::FromDays(60);
195 base::TouchFile(storage_path
.Append(kDomStorageOrigin3
),
196 sixty_days_ago
, sixty_days_ago
);
200 void GetLocalStorageUsage() {
201 dom_storage_context_
->GetLocalStorageUsage(
202 base::Bind(&RemoveLocalStorageTester::OnGotLocalStorageUsage
,
203 base::Unretained(this)));
205 void OnGotLocalStorageUsage(
206 const std::vector
<content::LocalStorageUsageInfo
>& infos
) {
208 await_completion_
.Notify();
211 // We don't own these pointers.
212 TestBrowserContext
* profile_
;
213 content::DOMStorageContext
* dom_storage_context_
;
215 std::vector
<content::LocalStorageUsageInfo
> infos_
;
217 AwaitCompletionHelper await_completion_
;
219 DISALLOW_COPY_AND_ASSIGN(RemoveLocalStorageTester
);
222 bool IsWebSafeSchemeForTest(const std::string
& scheme
) {
223 return scheme
== "http";
226 bool DoesOriginMatchForUnprotectedWeb(
228 storage::SpecialStoragePolicy
* special_storage_policy
) {
229 if (IsWebSafeSchemeForTest(origin
.scheme()))
230 return !special_storage_policy
->IsStorageProtected(origin
.GetOrigin());
235 bool DoesOriginMatchForBothProtectedAndUnprotectedWeb(
237 storage::SpecialStoragePolicy
* special_storage_policy
) {
241 bool DoesOriginMatchUnprotected(
243 storage::SpecialStoragePolicy
* special_storage_policy
) {
244 return origin
.GetOrigin().scheme() != kOriginDevTools
.scheme();
247 void ClearQuotaData(content::StoragePartition
* partition
,
248 base::RunLoop
* loop_to_quit
) {
249 partition
->ClearData(kAllQuotaRemoveMask
,
250 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL
, GURL(),
251 StoragePartition::OriginMatcherFunction(), base::Time(),
252 base::Time::Max(), loop_to_quit
->QuitClosure());
255 void ClearQuotaDataWithOriginMatcher(
256 content::StoragePartition
* partition
,
257 const GURL
& remove_origin
,
258 const StoragePartition::OriginMatcherFunction
& origin_matcher
,
259 const base::Time delete_begin
,
260 base::RunLoop
* loop_to_quit
) {
261 partition
->ClearData(kAllQuotaRemoveMask
,
262 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL
,
263 remove_origin
, origin_matcher
, delete_begin
,
264 base::Time::Max(), loop_to_quit
->QuitClosure());
267 void ClearQuotaDataForOrigin(
268 content::StoragePartition
* partition
,
269 const GURL
& remove_origin
,
270 const base::Time delete_begin
,
271 base::RunLoop
* loop_to_quit
) {
272 ClearQuotaDataWithOriginMatcher(
273 partition
, remove_origin
,
274 StoragePartition::OriginMatcherFunction(), delete_begin
,
278 void ClearQuotaDataForNonPersistent(
279 content::StoragePartition
* partition
,
280 const base::Time delete_begin
,
281 base::RunLoop
* loop_to_quit
) {
282 partition
->ClearData(
284 ~StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT
,
285 GURL(), StoragePartition::OriginMatcherFunction(), delete_begin
,
286 base::Time::Max(), loop_to_quit
->QuitClosure());
289 void ClearCookies(content::StoragePartition
* partition
,
290 const base::Time delete_begin
,
291 const base::Time delete_end
,
292 base::RunLoop
* run_loop
) {
293 partition
->ClearData(
294 StoragePartition::REMOVE_DATA_MASK_COOKIES
,
295 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL
,
296 GURL(), StoragePartition::OriginMatcherFunction(),
297 delete_begin
, delete_end
, run_loop
->QuitClosure());
300 void ClearStuff(uint32 remove_mask
,
301 content::StoragePartition
* partition
,
302 const base::Time delete_begin
,
303 const base::Time delete_end
,
304 const StoragePartition::OriginMatcherFunction
& origin_matcher
,
305 base::RunLoop
* run_loop
) {
306 partition
->ClearData(
307 remove_mask
, StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL
,
308 GURL(), origin_matcher
, delete_begin
, delete_end
,
309 run_loop
->QuitClosure());
312 void ClearData(content::StoragePartition
* partition
,
313 base::RunLoop
* run_loop
) {
315 partition
->ClearData(
316 StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE
,
317 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL
,
318 GURL(), StoragePartition::OriginMatcherFunction(),
319 time
, time
, run_loop
->QuitClosure());
324 class StoragePartitionImplTest
: public testing::Test
{
326 StoragePartitionImplTest()
327 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
),
328 browser_context_(new TestBrowserContext()) {
331 MockQuotaManager
* GetMockManager() {
332 if (!quota_manager_
.get()) {
333 quota_manager_
= new MockQuotaManager(
334 browser_context_
->IsOffTheRecord(),
335 browser_context_
->GetPath(),
336 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
).get(),
337 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB
).get(),
338 browser_context_
->GetSpecialStoragePolicy());
340 return quota_manager_
.get();
343 TestBrowserContext
* browser_context() {
344 return browser_context_
.get();
348 content::TestBrowserThreadBundle thread_bundle_
;
349 scoped_ptr
<TestBrowserContext
> browser_context_
;
350 scoped_refptr
<MockQuotaManager
> quota_manager_
;
352 DISALLOW_COPY_AND_ASSIGN(StoragePartitionImplTest
);
355 class StoragePartitionShaderClearTest
: public testing::Test
{
357 StoragePartitionShaderClearTest()
358 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
),
359 browser_context_(new TestBrowserContext()) {
360 ShaderCacheFactory::GetInstance()->SetCacheInfo(
362 BrowserContext::GetDefaultStoragePartition(
363 browser_context())->GetPath());
364 cache_
= ShaderCacheFactory::GetInstance()->Get(kDefaultClientId
);
367 virtual ~StoragePartitionShaderClearTest() {
369 ShaderCacheFactory::GetInstance()->RemoveCacheInfo(kDefaultClientId
);
373 net::TestCompletionCallback available_cb
;
374 int rv
= cache_
->SetAvailableCallback(available_cb
.callback());
375 ASSERT_EQ(net::OK
, available_cb
.GetResult(rv
));
376 EXPECT_EQ(0, cache_
->Size());
378 cache_
->Cache(kCacheKey
, kCacheValue
);
380 net::TestCompletionCallback complete_cb
;
382 rv
= cache_
->SetCacheCompleteCallback(complete_cb
.callback());
383 ASSERT_EQ(net::OK
, complete_cb
.GetResult(rv
));
386 size_t Size() { return cache_
->Size(); }
388 TestBrowserContext
* browser_context() {
389 return browser_context_
.get();
393 content::TestBrowserThreadBundle thread_bundle_
;
394 scoped_ptr
<TestBrowserContext
> browser_context_
;
396 scoped_refptr
<ShaderDiskCache
> cache_
;
399 // Tests ---------------------------------------------------------------------
401 TEST_F(StoragePartitionShaderClearTest
, ClearShaderCache
) {
403 EXPECT_EQ(1u, Size());
405 base::RunLoop run_loop
;
406 base::MessageLoop::current()->PostTask(
407 FROM_HERE
, base::Bind(
409 BrowserContext::GetDefaultStoragePartition(browser_context()),
412 EXPECT_EQ(0u, Size());
415 TEST_F(StoragePartitionImplTest
, QuotaClientMaskGeneration
) {
416 EXPECT_EQ(storage::QuotaClient::kFileSystem
,
417 StoragePartitionImpl::GenerateQuotaClientMask(
418 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS
));
419 EXPECT_EQ(storage::QuotaClient::kDatabase
,
420 StoragePartitionImpl::GenerateQuotaClientMask(
421 StoragePartition::REMOVE_DATA_MASK_WEBSQL
));
422 EXPECT_EQ(storage::QuotaClient::kAppcache
,
423 StoragePartitionImpl::GenerateQuotaClientMask(
424 StoragePartition::REMOVE_DATA_MASK_APPCACHE
));
425 EXPECT_EQ(storage::QuotaClient::kIndexedDatabase
,
426 StoragePartitionImpl::GenerateQuotaClientMask(
427 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB
));
428 EXPECT_EQ(storage::QuotaClient::kFileSystem
|
429 storage::QuotaClient::kDatabase
|
430 storage::QuotaClient::kAppcache
|
431 storage::QuotaClient::kIndexedDatabase
,
432 StoragePartitionImpl::GenerateQuotaClientMask(kAllQuotaRemoveMask
));
435 void PopulateTestQuotaManagedPersistentData(MockQuotaManager
* manager
) {
436 manager
->AddOrigin(kOrigin2
, kPersistent
, kClientFile
, base::Time());
437 manager
->AddOrigin(kOrigin3
, kPersistent
, kClientFile
,
438 base::Time::Now() - base::TimeDelta::FromDays(1));
440 EXPECT_FALSE(manager
->OriginHasData(kOrigin1
, kPersistent
, kClientFile
));
441 EXPECT_TRUE(manager
->OriginHasData(kOrigin2
, kPersistent
, kClientFile
));
442 EXPECT_TRUE(manager
->OriginHasData(kOrigin3
, kPersistent
, kClientFile
));
445 void PopulateTestQuotaManagedTemporaryData(MockQuotaManager
* manager
) {
446 manager
->AddOrigin(kOrigin1
, kTemporary
, kClientFile
, base::Time::Now());
447 manager
->AddOrigin(kOrigin3
, kTemporary
, kClientFile
,
448 base::Time::Now() - base::TimeDelta::FromDays(1));
450 EXPECT_TRUE(manager
->OriginHasData(kOrigin1
, kTemporary
, kClientFile
));
451 EXPECT_FALSE(manager
->OriginHasData(kOrigin2
, kTemporary
, kClientFile
));
452 EXPECT_TRUE(manager
->OriginHasData(kOrigin3
, kTemporary
, kClientFile
));
455 void PopulateTestQuotaManagedData(MockQuotaManager
* manager
) {
456 // Set up kOrigin1 with a temporary quota, kOrigin2 with a persistent
457 // quota, and kOrigin3 with both. kOrigin1 is modified now, kOrigin2
458 // is modified at the beginning of time, and kOrigin3 is modified one day
460 PopulateTestQuotaManagedPersistentData(manager
);
461 PopulateTestQuotaManagedTemporaryData(manager
);
464 void PopulateTestQuotaManagedNonBrowsingData(MockQuotaManager
* manager
) {
465 manager
->AddOrigin(kOriginDevTools
, kTemporary
, kClientFile
, base::Time());
466 manager
->AddOrigin(kOriginDevTools
, kPersistent
, kClientFile
, base::Time());
469 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedDataForeverBoth
) {
470 PopulateTestQuotaManagedData(GetMockManager());
472 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
473 BrowserContext::GetDefaultStoragePartition(browser_context()));
474 partition
->OverrideQuotaManagerForTesting(
477 base::RunLoop run_loop
;
478 base::MessageLoop::current()->PostTask(
479 FROM_HERE
, base::Bind(&ClearQuotaData
, partition
, &run_loop
));
482 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
484 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
486 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
488 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
490 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
492 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
496 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedDataForeverOnlyTemporary
) {
497 PopulateTestQuotaManagedTemporaryData(GetMockManager());
499 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
500 BrowserContext::GetDefaultStoragePartition(browser_context()));
501 partition
->OverrideQuotaManagerForTesting(
504 base::RunLoop run_loop
;
505 base::MessageLoop::current()->PostTask(
506 FROM_HERE
, base::Bind(&ClearQuotaData
, partition
, &run_loop
));
509 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
511 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
513 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
515 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
517 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
519 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
523 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedDataForeverOnlyPersistent
) {
524 PopulateTestQuotaManagedPersistentData(GetMockManager());
526 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
527 BrowserContext::GetDefaultStoragePartition(browser_context()));
528 partition
->OverrideQuotaManagerForTesting(
531 base::RunLoop run_loop
;
532 base::MessageLoop::current()->PostTask(
533 FROM_HERE
, base::Bind(&ClearQuotaData
, partition
, &run_loop
));
536 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
538 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
540 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
542 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
544 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
546 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
550 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedDataForeverNeither
) {
551 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
552 BrowserContext::GetDefaultStoragePartition(browser_context()));
553 partition
->OverrideQuotaManagerForTesting(
556 base::RunLoop run_loop
;
557 base::MessageLoop::current()->PostTask(
558 FROM_HERE
, base::Bind(&ClearQuotaData
, partition
, &run_loop
));
561 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
563 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
565 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
567 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
569 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
571 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
575 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedDataForeverSpecificOrigin
) {
576 PopulateTestQuotaManagedData(GetMockManager());
578 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
579 BrowserContext::GetDefaultStoragePartition(browser_context()));
580 partition
->OverrideQuotaManagerForTesting(
583 base::RunLoop run_loop
;
584 base::MessageLoop::current()->PostTask(
585 FROM_HERE
, base::Bind(&ClearQuotaDataForOrigin
,
586 partition
, kOrigin1
, base::Time(),
590 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
592 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
594 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
596 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
598 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
600 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
604 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedDataForLastHour
) {
605 PopulateTestQuotaManagedData(GetMockManager());
607 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
608 BrowserContext::GetDefaultStoragePartition(browser_context()));
609 partition
->OverrideQuotaManagerForTesting(
612 base::RunLoop run_loop
;
613 base::MessageLoop::current()->PostTask(
614 FROM_HERE
, base::Bind(&ClearQuotaDataForOrigin
,
616 base::Time::Now() - base::TimeDelta::FromHours(1),
620 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
622 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
624 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
626 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
628 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
630 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
634 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedDataForLastWeek
) {
635 PopulateTestQuotaManagedData(GetMockManager());
637 base::RunLoop run_loop
;
638 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
639 BrowserContext::GetDefaultStoragePartition(browser_context()));
640 partition
->OverrideQuotaManagerForTesting(
642 base::MessageLoop::current()->PostTask(
643 FROM_HERE
, base::Bind(&ClearQuotaDataForNonPersistent
,
645 base::Time::Now() - base::TimeDelta::FromDays(7),
649 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
651 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
653 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
655 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
657 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
659 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
663 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedUnprotectedOrigins
) {
665 scoped_refptr
<MockSpecialStoragePolicy
> mock_policy
=
666 new MockSpecialStoragePolicy
;
667 mock_policy
->AddProtected(kOrigin1
.GetOrigin());
669 PopulateTestQuotaManagedData(GetMockManager());
671 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
672 BrowserContext::GetDefaultStoragePartition(browser_context()));
673 partition
->OverrideQuotaManagerForTesting(
675 partition
->OverrideSpecialStoragePolicyForTesting(mock_policy
.get());
677 base::RunLoop run_loop
;
678 base::MessageLoop::current()->PostTask(
679 FROM_HERE
, base::Bind(&ClearQuotaDataWithOriginMatcher
,
681 base::Bind(&DoesOriginMatchForUnprotectedWeb
),
682 base::Time(), &run_loop
));
685 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
687 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
689 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
691 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
693 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
695 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
699 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedProtectedSpecificOrigin
) {
701 scoped_refptr
<MockSpecialStoragePolicy
> mock_policy
=
702 new MockSpecialStoragePolicy
;
703 mock_policy
->AddProtected(kOrigin1
.GetOrigin());
705 PopulateTestQuotaManagedData(GetMockManager());
707 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
708 BrowserContext::GetDefaultStoragePartition(browser_context()));
709 partition
->OverrideQuotaManagerForTesting(
711 partition
->OverrideSpecialStoragePolicyForTesting(mock_policy
.get());
713 // Try to remove kOrigin1. Expect failure.
714 base::RunLoop run_loop
;
715 base::MessageLoop::current()->PostTask(
716 FROM_HERE
, base::Bind(&ClearQuotaDataWithOriginMatcher
,
718 base::Bind(&DoesOriginMatchForUnprotectedWeb
),
719 base::Time(), &run_loop
));
722 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
724 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
726 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
728 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
730 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
732 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
736 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedProtectedOrigins
) {
738 scoped_refptr
<MockSpecialStoragePolicy
> mock_policy
=
739 new MockSpecialStoragePolicy
;
740 mock_policy
->AddProtected(kOrigin1
.GetOrigin());
742 PopulateTestQuotaManagedData(GetMockManager());
744 // Try to remove kOrigin1. Expect success.
745 base::RunLoop run_loop
;
746 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
747 BrowserContext::GetDefaultStoragePartition(browser_context()));
748 partition
->OverrideQuotaManagerForTesting(
750 partition
->OverrideSpecialStoragePolicyForTesting(mock_policy
.get());
751 base::MessageLoop::current()->PostTask(
753 base::Bind(&ClearQuotaDataWithOriginMatcher
,
755 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb
),
756 base::Time(), &run_loop
));
759 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
761 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
763 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
765 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
767 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
769 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
773 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedIgnoreDevTools
) {
774 PopulateTestQuotaManagedNonBrowsingData(GetMockManager());
776 base::RunLoop run_loop
;
777 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
778 BrowserContext::GetDefaultStoragePartition(browser_context()));
779 partition
->OverrideQuotaManagerForTesting(
781 base::MessageLoop::current()->PostTask(
782 FROM_HERE
, base::Bind(&ClearQuotaDataWithOriginMatcher
,
784 base::Bind(&DoesOriginMatchUnprotected
),
785 base::Time(), &run_loop
));
788 // Check that devtools data isn't removed.
789 EXPECT_TRUE(GetMockManager()->OriginHasData(kOriginDevTools
, kTemporary
,
791 EXPECT_TRUE(GetMockManager()->OriginHasData(kOriginDevTools
, kPersistent
,
795 TEST_F(StoragePartitionImplTest
, RemoveCookieForever
) {
796 RemoveCookieTester
tester(browser_context());
799 ASSERT_TRUE(tester
.ContainsCookie());
801 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
802 BrowserContext::GetDefaultStoragePartition(browser_context()));
803 partition
->SetURLRequestContext(browser_context()->GetRequestContext());
805 base::RunLoop run_loop
;
806 base::MessageLoop::current()->PostTask(
807 FROM_HERE
, base::Bind(&ClearCookies
,
808 partition
, base::Time(), base::Time::Max(),
812 EXPECT_FALSE(tester
.ContainsCookie());
815 TEST_F(StoragePartitionImplTest
, RemoveCookieLastHour
) {
816 RemoveCookieTester
tester(browser_context());
819 ASSERT_TRUE(tester
.ContainsCookie());
821 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
822 BrowserContext::GetDefaultStoragePartition(browser_context()));
823 base::Time an_hour_ago
= base::Time::Now() - base::TimeDelta::FromHours(1);
824 partition
->SetURLRequestContext(browser_context()->GetRequestContext());
826 base::RunLoop run_loop
;
827 base::MessageLoop::current()->PostTask(
828 FROM_HERE
, base::Bind(&ClearCookies
,
829 partition
, an_hour_ago
, base::Time::Max(),
833 EXPECT_FALSE(tester
.ContainsCookie());
836 TEST_F(StoragePartitionImplTest
, RemoveUnprotectedLocalStorageForever
) {
838 scoped_refptr
<MockSpecialStoragePolicy
> mock_policy
=
839 new MockSpecialStoragePolicy
;
840 mock_policy
->AddProtected(kOrigin1
.GetOrigin());
842 RemoveLocalStorageTester
tester(browser_context());
844 tester
.AddDOMStorageTestData();
845 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin1
));
846 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin2
));
847 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin3
));
849 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
850 BrowserContext::GetDefaultStoragePartition(browser_context()));
851 partition
->OverrideSpecialStoragePolicyForTesting(mock_policy
.get());
853 base::RunLoop run_loop
;
854 base::MessageLoop::current()->PostTask(
856 base::Bind(&ClearStuff
,
857 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE
,
858 partition
, base::Time(), base::Time::Max(),
859 base::Bind(&DoesOriginMatchForUnprotectedWeb
),
863 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin1
));
864 EXPECT_FALSE(tester
.DOMStorageExistsForOrigin(kOrigin2
));
865 EXPECT_FALSE(tester
.DOMStorageExistsForOrigin(kOrigin3
));
868 TEST_F(StoragePartitionImplTest
, RemoveProtectedLocalStorageForever
) {
870 scoped_refptr
<MockSpecialStoragePolicy
> mock_policy
=
871 new MockSpecialStoragePolicy
;
872 mock_policy
->AddProtected(kOrigin1
.GetOrigin());
874 RemoveLocalStorageTester
tester(browser_context());
876 tester
.AddDOMStorageTestData();
877 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin1
));
878 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin2
));
879 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin3
));
881 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
882 BrowserContext::GetDefaultStoragePartition(browser_context()));
883 partition
->OverrideSpecialStoragePolicyForTesting(mock_policy
.get());
885 base::RunLoop run_loop
;
886 base::MessageLoop::current()->PostTask(
888 base::Bind(&ClearStuff
,
889 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE
,
890 partition
, base::Time(), base::Time::Max(),
891 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb
),
895 // Even if kOrigin1 is protected, it will be deleted since we specify
896 // ClearData to delete protected data.
897 EXPECT_FALSE(tester
.DOMStorageExistsForOrigin(kOrigin1
));
898 EXPECT_FALSE(tester
.DOMStorageExistsForOrigin(kOrigin2
));
899 EXPECT_FALSE(tester
.DOMStorageExistsForOrigin(kOrigin3
));
902 TEST_F(StoragePartitionImplTest
, RemoveLocalStorageForLastWeek
) {
903 RemoveLocalStorageTester
tester(browser_context());
905 tester
.AddDOMStorageTestData();
906 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin1
));
907 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin2
));
908 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin3
));
910 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
911 BrowserContext::GetDefaultStoragePartition(browser_context()));
912 base::Time a_week_ago
= base::Time::Now() - base::TimeDelta::FromDays(7);
914 base::RunLoop run_loop
;
915 base::MessageLoop::current()->PostTask(
917 base::Bind(&ClearStuff
,
918 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE
,
919 partition
, a_week_ago
, base::Time::Max(),
920 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb
),
924 // kOrigin1 and kOrigin2 do not have age more than a week.
925 EXPECT_FALSE(tester
.DOMStorageExistsForOrigin(kOrigin1
));
926 EXPECT_FALSE(tester
.DOMStorageExistsForOrigin(kOrigin2
));
927 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin3
));
930 } // namespace content