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" | |
10 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
11 #include "base/platform_file.h" | 10 #include "base/platform_file.h" |
12 #include "chrome/browser/extensions/mock_extension_special_storage_policy.h" | 11 #include "chrome/browser/extensions/mock_extension_special_storage_policy.h" |
13 #include "chrome/browser/history/history.h" | 12 #include "chrome/browser/history/history.h" |
14 #include "chrome/test/base/testing_profile.h" | 13 #include "chrome/test/base/testing_profile.h" |
15 #include "chrome/test/testing_browser_process_test.h" | 14 #include "chrome/test/testing_browser_process_test.h" |
16 #include "net/base/cookie_monster.h" | |
17 #include "net/url_request/url_request_context.h" | |
18 #include "net/url_request/url_request_context_getter.cc" | |
19 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
20 #include "webkit/fileapi/file_system_context.h" | 16 #include "webkit/fileapi/file_system_context.h" |
21 #include "webkit/fileapi/file_system_file_util.h" | 17 #include "webkit/fileapi/file_system_file_util.h" |
22 #include "webkit/fileapi/file_system_operation_context.h" | 18 #include "webkit/fileapi/file_system_operation_context.h" |
23 #include "webkit/fileapi/file_system_path_manager.h" | 19 #include "webkit/fileapi/file_system_path_manager.h" |
24 #include "webkit/fileapi/sandbox_mount_point_provider.h" | 20 #include "webkit/fileapi/sandbox_mount_point_provider.h" |
25 #include "webkit/quota/mock_quota_manager.h" | 21 #include "webkit/quota/mock_quota_manager.h" |
26 #include "webkit/quota/quota_manager.h" | 22 #include "webkit/quota/quota_manager.h" |
27 #include "webkit/quota/quota_types.h" | 23 #include "webkit/quota/quota_types.h" |
28 | 24 |
29 namespace { | 25 namespace { |
30 | 26 |
31 const char kTestkOrigin1[] = "http://host1:1/"; | 27 const char kTestkOrigin1[] = "http://host1:1/"; |
32 const char kTestkOrigin2[] = "http://host2:1/"; | 28 const char kTestkOrigin2[] = "http://host2:1/"; |
33 const char kTestkOrigin3[] = "http://host3:1/"; | 29 const char kTestkOrigin3[] = "http://host3:1/"; |
34 | 30 |
35 const GURL kOrigin1(kTestkOrigin1); | 31 const GURL kOrigin1(kTestkOrigin1); |
36 const GURL kOrigin2(kTestkOrigin2); | 32 const GURL kOrigin2(kTestkOrigin2); |
37 const GURL kOrigin3(kTestkOrigin3); | 33 const GURL kOrigin3(kTestkOrigin3); |
38 | 34 |
39 class BrowsingDataRemoverTester : public BrowsingDataRemover::Observer { | 35 class BrowsingDataRemoverTester : public BrowsingDataRemover::Observer { |
40 public: | 36 public: |
41 BrowsingDataRemoverTester() | 37 BrowsingDataRemoverTester() {} |
42 : start_(false), | |
43 already_quit_(false) {} | |
44 virtual ~BrowsingDataRemoverTester() {} | 38 virtual ~BrowsingDataRemoverTester() {} |
45 | 39 |
46 void BlockUntilNotified() { | 40 void BlockUntilNotified() { |
47 if (!already_quit_) { | 41 MessageLoop::current()->Run(); |
48 DCHECK(!start_); | |
49 start_ = true; | |
50 MessageLoop::current()->Run(); | |
51 } else { | |
52 DCHECK(!start_); | |
53 already_quit_ = false; | |
54 } | |
55 } | 42 } |
56 | 43 |
57 protected: | 44 protected: |
58 // BrowsingDataRemover::Observer implementation. | 45 // BrowsingDataRemover::Observer implementation. |
59 virtual void OnBrowsingDataRemoverDone() { | 46 virtual void OnBrowsingDataRemoverDone() { |
60 Notify(); | 47 Notify(); |
61 } | 48 } |
62 | 49 |
63 void Notify() { | 50 void Notify() { |
64 if (start_) { | 51 MessageLoop::current()->Quit(); |
65 DCHECK(!already_quit_); | |
66 MessageLoop::current()->Quit(); | |
67 start_ = false; | |
68 } else { | |
69 DCHECK(!already_quit_); | |
70 already_quit_ = true; | |
71 } | |
72 } | 52 } |
73 | 53 |
74 private: | 54 private: |
75 // Helps prevent from running message_loop, if the callback invoked | |
76 // immediately. | |
77 bool start_; | |
78 bool already_quit_; | |
79 | |
80 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemoverTester); | 55 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemoverTester); |
81 }; | 56 }; |
82 | 57 |
83 // Testers ------------------------------------------------------------------- | 58 // Testers ------------------------------------------------------------------- |
84 | 59 |
85 class RemoveCookieTester : public BrowsingDataRemoverTester { | |
86 public: | |
87 explicit RemoveCookieTester(TestingProfile* profile) | |
88 : get_cookie_success_(false) { | |
89 profile->CreateRequestContext(); | |
90 monster_ = profile->GetRequestContext()->GetURLRequestContext()-> | |
91 cookie_store()->GetCookieMonster(); | |
92 } | |
93 | |
94 // Returns true, if the given cookie exists in the cookie store. | |
95 bool ContainsCookie() { | |
96 get_cookie_success_ = false; | |
97 monster_->GetCookiesWithOptionsAsync( | |
98 kOrigin1, net::CookieOptions(), | |
99 base::Bind(&RemoveCookieTester::GetCookieCallback, | |
100 base::Unretained(this))); | |
101 BlockUntilNotified(); | |
102 return get_cookie_success_; | |
103 } | |
104 | |
105 void AddCookie() { | |
106 monster_->SetCookieWithOptionsAsync( | |
107 kOrigin1, "A=1", net::CookieOptions(), | |
108 base::Bind(&RemoveCookieTester::SetCookieCallback, | |
109 base::Unretained(this))); | |
110 BlockUntilNotified(); | |
111 } | |
112 | |
113 private: | |
114 void GetCookieCallback(const std::string& cookies) { | |
115 if (cookies == "A=1") { | |
116 get_cookie_success_ = true; | |
117 } else { | |
118 EXPECT_EQ(cookies, ""); | |
119 get_cookie_success_ = false; | |
120 } | |
121 Notify(); | |
122 } | |
123 | |
124 void SetCookieCallback(bool result) { | |
125 ASSERT_TRUE(result); | |
126 Notify(); | |
127 } | |
128 | |
129 bool get_cookie_success_; | |
130 | |
131 net::CookieStore* monster_; | |
132 | |
133 DISALLOW_COPY_AND_ASSIGN(RemoveCookieTester); | |
134 }; | |
135 | |
136 class RemoveHistoryTester : public BrowsingDataRemoverTester { | 60 class RemoveHistoryTester : public BrowsingDataRemoverTester { |
137 public: | 61 public: |
138 explicit RemoveHistoryTester(TestingProfile* profile) | 62 explicit RemoveHistoryTester(TestingProfile* profile) |
139 : query_url_success_(false) { | 63 : query_url_success_(false) { |
140 profile->CreateHistoryService(true, false); | 64 profile->CreateHistoryService(true, false); |
141 history_service_ = profile->GetHistoryService(Profile::EXPLICIT_ACCESS); | 65 history_service_ = profile->GetHistoryService(Profile::EXPLICIT_ACCESS); |
142 } | 66 } |
143 | 67 |
144 // Returns true, if the given URL exists in the history service. | 68 // Returns true, if the given URL exists in the history service. |
145 bool HistoryContainsURL(const GURL& url) { | 69 bool HistoryContainsURL(const GURL& url) { |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 BrowserThread webkit_thread_; | 213 BrowserThread webkit_thread_; |
290 BrowserThread file_thread_; | 214 BrowserThread file_thread_; |
291 BrowserThread io_thread_; | 215 BrowserThread io_thread_; |
292 scoped_ptr<TestingProfile> profile_; | 216 scoped_ptr<TestingProfile> profile_; |
293 | 217 |
294 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemoverTest); | 218 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemoverTest); |
295 }; | 219 }; |
296 | 220 |
297 // Tests --------------------------------------------------------------------- | 221 // Tests --------------------------------------------------------------------- |
298 | 222 |
299 TEST_F(BrowsingDataRemoverTest, RemoveCookieForever) { | |
300 scoped_ptr<RemoveCookieTester> tester( | |
301 new RemoveCookieTester(GetProfile())); | |
302 | |
303 tester->AddCookie(); | |
304 ASSERT_TRUE(tester->ContainsCookie()); | |
305 | |
306 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING, | |
307 base::Time::Now(), BrowsingDataRemover::REMOVE_COOKIES, tester.get()); | |
308 | |
309 EXPECT_FALSE(tester->ContainsCookie()); | |
310 } | |
311 | |
312 TEST_F(BrowsingDataRemoverTest, RemoveHistoryForever) { | 223 TEST_F(BrowsingDataRemoverTest, RemoveHistoryForever) { |
313 scoped_ptr<RemoveHistoryTester> tester( | 224 scoped_ptr<RemoveHistoryTester> tester( |
314 new RemoveHistoryTester(GetProfile())); | 225 new RemoveHistoryTester(GetProfile())); |
315 | 226 |
316 tester->AddHistory(kOrigin1, base::Time::Now()); | 227 tester->AddHistory(kOrigin1, base::Time::Now()); |
317 ASSERT_TRUE(tester->HistoryContainsURL(kOrigin1)); | 228 ASSERT_TRUE(tester->HistoryContainsURL(kOrigin1)); |
318 | 229 |
319 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING, | 230 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING, |
320 base::Time::Now(), BrowsingDataRemover::REMOVE_HISTORY, tester.get()); | 231 base::Time::Now(), BrowsingDataRemover::REMOVE_HISTORY, tester.get()); |
321 | 232 |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 quota::kStorageTypeTemporary)); | 405 quota::kStorageTypeTemporary)); |
495 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, | 406 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, |
496 quota::kStorageTypePersistent)); | 407 quota::kStorageTypePersistent)); |
497 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, | 408 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, |
498 quota::kStorageTypePersistent)); | 409 quota::kStorageTypePersistent)); |
499 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, | 410 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, |
500 quota::kStorageTypePersistent)); | 411 quota::kStorageTypePersistent)); |
501 } | 412 } |
502 | 413 |
503 } // namespace | 414 } // namespace |
OLD | NEW |