OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/browsing_data_remover.h" | 5 #include "chrome/browser/browsing_data_remover.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
| 9 #include "base/bind.h" |
9 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
10 #include "base/platform_file.h" | 11 #include "base/platform_file.h" |
11 #include "chrome/browser/extensions/mock_extension_special_storage_policy.h" | 12 #include "chrome/browser/extensions/mock_extension_special_storage_policy.h" |
12 #include "chrome/browser/history/history.h" | 13 #include "chrome/browser/history/history.h" |
| 14 #include "chrome/test/test_url_request_context_getter.h" |
13 #include "chrome/test/testing_profile.h" | 15 #include "chrome/test/testing_profile.h" |
14 #include "content/browser/appcache/chrome_appcache_service.h" | 16 #include "content/browser/appcache/chrome_appcache_service.h" |
| 17 #include "net/base/cookie_monster.h" |
| 18 #include "net/url_request/url_request_context.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "webkit/appcache/appcache.h" | 20 #include "webkit/appcache/appcache.h" |
17 #include "webkit/appcache/appcache_group.h" | 21 #include "webkit/appcache/appcache_group.h" |
18 #include "webkit/appcache/appcache_storage.h" | 22 #include "webkit/appcache/appcache_storage.h" |
19 #include "webkit/fileapi/file_system_context.h" | 23 #include "webkit/fileapi/file_system_context.h" |
20 #include "webkit/fileapi/file_system_operation_context.h" | 24 #include "webkit/fileapi/file_system_operation_context.h" |
21 #include "webkit/fileapi/file_system_file_util.h" | 25 #include "webkit/fileapi/file_system_file_util.h" |
22 #include "webkit/fileapi/file_system_path_manager.h" | 26 #include "webkit/fileapi/file_system_path_manager.h" |
23 #include "webkit/fileapi/sandbox_mount_point_provider.h" | 27 #include "webkit/fileapi/sandbox_mount_point_provider.h" |
24 | 28 |
25 namespace { | 29 namespace { |
26 | 30 |
27 const char kTestkOrigin1[] = "http://host1:1/"; | 31 const char kTestkOrigin1[] = "http://host1:1/"; |
28 const char kTestkOrigin2[] = "http://host2:1/"; | 32 const char kTestkOrigin2[] = "http://host2:1/"; |
29 const char kTestkOrigin3[] = "http://host3:1/"; | 33 const char kTestkOrigin3[] = "http://host3:1/"; |
30 | 34 |
31 const GURL kOrigin1(kTestkOrigin1); | 35 const GURL kOrigin1(kTestkOrigin1); |
32 const GURL kOrigin2(kTestkOrigin2); | 36 const GURL kOrigin2(kTestkOrigin2); |
33 const GURL kOrigin3(kTestkOrigin3); | 37 const GURL kOrigin3(kTestkOrigin3); |
34 | 38 |
35 const GURL kProtectedManifest("http://www.protected.com/cache.manifest"); | 39 const GURL kProtectedManifest("http://www.protected.com/cache.manifest"); |
36 const GURL kNormalManifest("http://www.normal.com/cache.manifest"); | 40 const GURL kNormalManifest("http://www.normal.com/cache.manifest"); |
37 | 41 |
38 class BrowsingDataRemoverTester : public BrowsingDataRemover::Observer { | 42 class BrowsingDataRemoverTester : public BrowsingDataRemover::Observer { |
39 public: | 43 public: |
40 BrowsingDataRemoverTester() {} | 44 BrowsingDataRemoverTester() |
| 45 : start_(false), |
| 46 already_quit_(false) {} |
41 virtual ~BrowsingDataRemoverTester() {} | 47 virtual ~BrowsingDataRemoverTester() {} |
42 | 48 |
43 void BlockUntilNotified() { | 49 void BlockUntilNotified() { |
44 MessageLoop::current()->Run(); | 50 if (!already_quit_) { |
| 51 start_ = true; |
| 52 MessageLoop::current()->Run(); |
| 53 } else { |
| 54 already_quit_ = false; |
| 55 } |
45 } | 56 } |
46 | 57 |
47 protected: | 58 protected: |
48 // BrowsingDataRemover::Observer implementation. | 59 // BrowsingDataRemover::Observer implementation. |
49 virtual void OnBrowsingDataRemoverDone() { | 60 virtual void OnBrowsingDataRemoverDone() { |
50 Notify(); | 61 Notify(); |
51 } | 62 } |
52 | 63 |
53 void Notify() { | 64 void Notify() { |
54 MessageLoop::current()->Quit(); | 65 if (start_) { |
| 66 MessageLoop::current()->Quit(); |
| 67 start_ = false; |
| 68 } else { |
| 69 already_quit_ = true; |
| 70 } |
55 } | 71 } |
56 | 72 |
57 private: | 73 private: |
| 74 // Helps prevent from running message_loop, if the callback invoked |
| 75 // immediately. |
| 76 bool start_; |
| 77 bool already_quit_; |
| 78 |
58 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemoverTester); | 79 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemoverTester); |
59 }; | 80 }; |
60 | 81 |
| 82 class RemoveCookieTester : public BrowsingDataRemoverTester { |
| 83 public: |
| 84 explicit RemoveCookieTester(TestingProfile* profile) { |
| 85 profile->CreateRequestContext(); |
| 86 monster_ = profile->GetRequestContext()->GetURLRequestContext()-> |
| 87 cookie_store()->GetCookieMonster(); |
| 88 } |
| 89 |
| 90 // Returns true, if the given cookie exists in the cookie store. |
| 91 bool ContainsCookie() { |
| 92 monster_->GetCookiesAsync(kOrigin1, |
| 93 base::Bind(&RemoveCookieTester::GetCookieCallback, |
| 94 base::Unretained(this))); |
| 95 BlockUntilNotified(); |
| 96 return get_cookie_success_; |
| 97 } |
| 98 |
| 99 void AddCookie() { |
| 100 monster_->SetCookieWithOptionsAsync( |
| 101 kOrigin1, "A=1", net::CookieOptions(), |
| 102 net::CookieStore::SetCookiesCallback()); |
| 103 } |
| 104 |
| 105 private: |
| 106 void GetCookieCallback(const std::string& cookies) { |
| 107 if (cookies == "A=1") { |
| 108 get_cookie_success_ = true; |
| 109 } else { |
| 110 EXPECT_EQ(cookies, ""); |
| 111 get_cookie_success_ = false; |
| 112 } |
| 113 Notify(); |
| 114 } |
| 115 |
| 116 bool get_cookie_success_; |
| 117 |
| 118 net::CookieStore* monster_; |
| 119 |
| 120 DISALLOW_COPY_AND_ASSIGN(RemoveCookieTester); |
| 121 }; |
| 122 |
61 class RemoveHistoryTester : public BrowsingDataRemoverTester { | 123 class RemoveHistoryTester : public BrowsingDataRemoverTester { |
62 public: | 124 public: |
63 explicit RemoveHistoryTester(TestingProfile* profile) | 125 explicit RemoveHistoryTester(TestingProfile* profile) |
64 : query_url_success_(false) { | 126 : query_url_success_(false) { |
65 profile->CreateHistoryService(true, false); | 127 profile->CreateHistoryService(true, false); |
66 history_service_ = profile->GetHistoryService(Profile::EXPLICIT_ACCESS); | 128 history_service_ = profile->GetHistoryService(Profile::EXPLICIT_ACCESS); |
67 } | 129 } |
68 | 130 |
69 // Returns true, if the given URL exists in the history service. | 131 // Returns true, if the given URL exists in the history service. |
70 bool HistoryContainsURL(const GURL& url) { | 132 bool HistoryContainsURL(const GURL& url) { |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 BrowserThread ui_thread_; | 362 BrowserThread ui_thread_; |
301 BrowserThread db_thread_; | 363 BrowserThread db_thread_; |
302 BrowserThread webkit_thread_; | 364 BrowserThread webkit_thread_; |
303 BrowserThread file_thread_; | 365 BrowserThread file_thread_; |
304 BrowserThread io_thread_; | 366 BrowserThread io_thread_; |
305 scoped_ptr<TestingProfile> profile_; | 367 scoped_ptr<TestingProfile> profile_; |
306 | 368 |
307 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemoverTest); | 369 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemoverTest); |
308 }; | 370 }; |
309 | 371 |
| 372 TEST_F(BrowsingDataRemoverTest, RemoveCookieForever) { |
| 373 scoped_ptr<RemoveCookieTester> tester( |
| 374 new RemoveCookieTester(GetProfile())); |
| 375 |
| 376 tester->AddCookie(); |
| 377 ASSERT_TRUE(tester->ContainsCookie()); |
| 378 |
| 379 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING, |
| 380 base::Time::Now(), BrowsingDataRemover::REMOVE_COOKIES, tester.get()); |
| 381 |
| 382 EXPECT_FALSE(tester->ContainsCookie()); |
| 383 } |
| 384 |
310 TEST_F(BrowsingDataRemoverTest, RemoveHistoryForever) { | 385 TEST_F(BrowsingDataRemoverTest, RemoveHistoryForever) { |
311 scoped_ptr<RemoveHistoryTester> tester( | 386 scoped_ptr<RemoveHistoryTester> tester( |
312 new RemoveHistoryTester(GetProfile())); | 387 new RemoveHistoryTester(GetProfile())); |
313 | 388 |
314 tester->AddHistory(kOrigin1, base::Time::Now()); | 389 tester->AddHistory(kOrigin1, base::Time::Now()); |
315 ASSERT_TRUE(tester->HistoryContainsURL(kOrigin1)); | 390 ASSERT_TRUE(tester->HistoryContainsURL(kOrigin1)); |
316 | 391 |
317 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING, | 392 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING, |
318 base::Time::Now(), BrowsingDataRemover::REMOVE_HISTORY, tester.get()); | 393 base::Time::Now(), BrowsingDataRemover::REMOVE_HISTORY, tester.get()); |
319 | 394 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 tester->BlockUntilNotified(); | 480 tester->BlockUntilNotified(); |
406 | 481 |
407 // Results: appcaches for the normal origin got deleted, appcaches for the | 482 // Results: appcaches for the normal origin got deleted, appcaches for the |
408 // protected origin didn't. | 483 // protected origin didn't. |
409 origins = reader.ReadAppCaches(); | 484 origins = reader.ReadAppCaches(); |
410 EXPECT_EQ(1UL, origins->size()); | 485 EXPECT_EQ(1UL, origins->size()); |
411 EXPECT_TRUE(origins->find(kProtectedManifest.GetOrigin()) != origins->end()); | 486 EXPECT_TRUE(origins->find(kProtectedManifest.GetOrigin()) != origins->end()); |
412 } | 487 } |
413 | 488 |
414 } // namespace | 489 } // namespace |
OLD | NEW |