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/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/platform_file.h" | 10 #include "base/platform_file.h" |
11 #include "chrome/browser/extensions/mock_extension_special_storage_policy.h" | 11 #include "chrome/browser/extensions/mock_extension_special_storage_policy.h" |
12 #include "chrome/browser/history/history.h" | 12 #include "chrome/browser/history/history.h" |
13 #include "chrome/test/testing_profile.h" | 13 #include "chrome/test/testing_profile.h" |
14 #include "content/browser/appcache/chrome_appcache_service.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "webkit/appcache/appcache_test_helper.h" | |
17 #include "webkit/fileapi/file_system_context.h" | 15 #include "webkit/fileapi/file_system_context.h" |
18 #include "webkit/fileapi/file_system_operation_context.h" | 16 #include "webkit/fileapi/file_system_operation_context.h" |
19 #include "webkit/fileapi/file_system_file_util.h" | 17 #include "webkit/fileapi/file_system_file_util.h" |
20 #include "webkit/fileapi/file_system_path_manager.h" | 18 #include "webkit/fileapi/file_system_path_manager.h" |
21 #include "webkit/fileapi/sandbox_mount_point_provider.h" | 19 #include "webkit/fileapi/sandbox_mount_point_provider.h" |
| 20 #include "webkit/quota/quota_manager.h" |
| 21 #include "webkit/quota/quota_types.h" |
| 22 #include "webkit/quota/mock_quota_manager.h" |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 const char kTestkOrigin1[] = "http://host1:1/"; | 26 const char kTestkOrigin1[] = "http://host1:1/"; |
26 const char kTestkOrigin2[] = "http://host2:1/"; | 27 const char kTestkOrigin2[] = "http://host2:1/"; |
27 const char kTestkOrigin3[] = "http://host3:1/"; | 28 const char kTestkOrigin3[] = "http://host3:1/"; |
28 | 29 |
29 const GURL kOrigin1(kTestkOrigin1); | 30 const GURL kOrigin1(kTestkOrigin1); |
30 const GURL kOrigin2(kTestkOrigin2); | 31 const GURL kOrigin2(kTestkOrigin2); |
31 const GURL kOrigin3(kTestkOrigin3); | 32 const GURL kOrigin3(kTestkOrigin3); |
(...skipping 17 matching lines...) Expand all Loading... |
49 } | 50 } |
50 | 51 |
51 void Notify() { | 52 void Notify() { |
52 MessageLoop::current()->Quit(); | 53 MessageLoop::current()->Quit(); |
53 } | 54 } |
54 | 55 |
55 private: | 56 private: |
56 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemoverTester); | 57 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemoverTester); |
57 }; | 58 }; |
58 | 59 |
| 60 // Testers ------------------------------------------------------------------- |
| 61 |
59 class RemoveHistoryTester : public BrowsingDataRemoverTester { | 62 class RemoveHistoryTester : public BrowsingDataRemoverTester { |
60 public: | 63 public: |
61 explicit RemoveHistoryTester(TestingProfile* profile) | 64 explicit RemoveHistoryTester(TestingProfile* profile) |
62 : query_url_success_(false) { | 65 : query_url_success_(false) { |
63 profile->CreateHistoryService(true, false); | 66 profile->CreateHistoryService(true, false); |
64 history_service_ = profile->GetHistoryService(Profile::EXPLICIT_ACCESS); | 67 history_service_ = profile->GetHistoryService(Profile::EXPLICIT_ACCESS); |
65 } | 68 } |
66 | 69 |
67 // Returns true, if the given URL exists in the history service. | 70 // Returns true, if the given URL exists in the history service. |
68 bool HistoryContainsURL(const GURL& url) { | 71 bool HistoryContainsURL(const GURL& url) { |
(...skipping 25 matching lines...) Expand all Loading... |
94 // For History requests. | 97 // For History requests. |
95 CancelableRequestConsumer consumer_; | 98 CancelableRequestConsumer consumer_; |
96 bool query_url_success_; | 99 bool query_url_success_; |
97 | 100 |
98 // TestingProfile owns the history service; we shouldn't delete it. | 101 // TestingProfile owns the history service; we shouldn't delete it. |
99 HistoryService* history_service_; | 102 HistoryService* history_service_; |
100 | 103 |
101 DISALLOW_COPY_AND_ASSIGN(RemoveHistoryTester); | 104 DISALLOW_COPY_AND_ASSIGN(RemoveHistoryTester); |
102 }; | 105 }; |
103 | 106 |
104 class RemoveFileSystemTester : public BrowsingDataRemoverTester { | 107 class RemoveQuotaManagedDataTester : public BrowsingDataRemoverTester { |
105 public: | 108 public: |
106 explicit RemoveFileSystemTester() {} | 109 explicit RemoveQuotaManagedDataTester() {} |
| 110 ~RemoveQuotaManagedDataTester() {} |
107 | 111 |
108 void FindFileSystemPathCallback(bool success, | 112 void PopulateTestQuotaManagedData(quota::MockQuotaManager* manager) { |
109 const FilePath& path, | 113 // Set up kOrigin1 with a temporary quota, kOrigin2 with a persistent |
110 const std::string& name) { | 114 // quota, and kOrigin3 with both. kOrigin1 is modified now, kOrigin2 |
111 found_file_system_ = success; | 115 // is modified at the beginning of time, and kOrigin3 is modified one day |
112 Notify(); | 116 // ago. |
113 } | 117 manager->AddOrigin(kOrigin1, quota::kStorageTypeTemporary, |
| 118 base::Time::Now()); |
| 119 manager->AddOrigin(kOrigin2, quota::kStorageTypePersistent, |
| 120 base::Time()); |
| 121 manager->AddOrigin(kOrigin3, quota::kStorageTypeTemporary, |
| 122 base::Time::Now() - base::TimeDelta::FromDays(1)); |
| 123 manager->AddOrigin(kOrigin3, quota::kStorageTypePersistent, |
| 124 base::Time::Now() - base::TimeDelta::FromDays(1)); |
114 | 125 |
115 bool FileSystemContainsOriginAndType(const GURL& origin, | 126 EXPECT_TRUE(manager->OriginHasData(kOrigin1, |
116 fileapi::FileSystemType type) { | 127 quota::kStorageTypeTemporary)); |
117 sandbox_->ValidateFileSystemRootAndGetURL( | 128 EXPECT_FALSE(manager->OriginHasData(kOrigin2, |
118 origin, type, false, NewCallback(this, | 129 quota::kStorageTypeTemporary)); |
119 &RemoveFileSystemTester::FindFileSystemPathCallback)); | 130 EXPECT_TRUE(manager->OriginHasData(kOrigin3, |
120 BlockUntilNotified(); | 131 quota::kStorageTypeTemporary)); |
121 return found_file_system_; | 132 EXPECT_FALSE(manager->OriginHasData(kOrigin1, |
122 } | 133 quota::kStorageTypePersistent)); |
123 | 134 EXPECT_TRUE(manager->OriginHasData(kOrigin2, |
124 virtual void PopulateTestFileSystemData(TestingProfile* profile) { | 135 quota::kStorageTypePersistent)); |
125 // Set up kOrigin1 with a temporary file system, kOrigin2 with a persistent | 136 EXPECT_TRUE(manager->OriginHasData(kOrigin3, |
126 // file system, and kOrigin3 with both. | 137 quota::kStorageTypePersistent)); |
127 sandbox_ = profile->GetFileSystemContext()->path_manager()-> | |
128 sandbox_provider(); | |
129 | |
130 CreateDirectoryForOriginAndType(kOrigin1, | |
131 fileapi::kFileSystemTypeTemporary); | |
132 CreateDirectoryForOriginAndType(kOrigin2, | |
133 fileapi::kFileSystemTypePersistent); | |
134 CreateDirectoryForOriginAndType(kOrigin3, | |
135 fileapi::kFileSystemTypeTemporary); | |
136 CreateDirectoryForOriginAndType(kOrigin3, | |
137 fileapi::kFileSystemTypePersistent); | |
138 | |
139 EXPECT_FALSE(FileSystemContainsOriginAndType(kOrigin1, | |
140 fileapi::kFileSystemTypePersistent)); | |
141 EXPECT_TRUE(FileSystemContainsOriginAndType(kOrigin1, | |
142 fileapi::kFileSystemTypeTemporary)); | |
143 EXPECT_TRUE(FileSystemContainsOriginAndType(kOrigin2, | |
144 fileapi::kFileSystemTypePersistent)); | |
145 EXPECT_FALSE(FileSystemContainsOriginAndType(kOrigin2, | |
146 fileapi::kFileSystemTypeTemporary)); | |
147 EXPECT_TRUE(FileSystemContainsOriginAndType(kOrigin3, | |
148 fileapi::kFileSystemTypePersistent)); | |
149 EXPECT_TRUE(FileSystemContainsOriginAndType(kOrigin3, | |
150 fileapi::kFileSystemTypeTemporary)); | |
151 } | |
152 | |
153 void CreateDirectoryForOriginAndType(const GURL& origin, | |
154 fileapi::FileSystemType type) { | |
155 FilePath target = sandbox_->ValidateFileSystemRootAndGetPathOnFileThread( | |
156 origin, type, FilePath(), true); | |
157 EXPECT_TRUE(file_util::DirectoryExists(target)); | |
158 } | 138 } |
159 | 139 |
160 private: | 140 private: |
161 fileapi::SandboxMountPointProvider* sandbox_; | 141 DISALLOW_COPY_AND_ASSIGN(RemoveQuotaManagedDataTester); |
162 bool found_file_system_; | 142 }; |
163 | 143 |
164 DISALLOW_COPY_AND_ASSIGN(RemoveFileSystemTester); | 144 // Test Class ---------------------------------------------------------------- |
165 }; | |
166 | 145 |
167 class BrowsingDataRemoverTest : public testing::Test { | 146 class BrowsingDataRemoverTest : public testing::Test { |
168 public: | 147 public: |
169 BrowsingDataRemoverTest() | 148 BrowsingDataRemoverTest() |
170 : ui_thread_(BrowserThread::UI, &message_loop_), | 149 : ui_thread_(BrowserThread::UI, &message_loop_), |
171 db_thread_(BrowserThread::DB, &message_loop_), | 150 db_thread_(BrowserThread::DB, &message_loop_), |
172 webkit_thread_(BrowserThread::WEBKIT, &message_loop_), | 151 webkit_thread_(BrowserThread::WEBKIT, &message_loop_), |
173 file_thread_(BrowserThread::FILE, &message_loop_), | 152 file_thread_(BrowserThread::FILE, &message_loop_), |
174 io_thread_(BrowserThread::IO, &message_loop_), | 153 io_thread_(BrowserThread::IO, &message_loop_), |
175 profile_(new TestingProfile()) { | 154 profile_(new TestingProfile()) { |
(...skipping 22 matching lines...) Expand all Loading... |
198 | 177 |
199 // BrowsingDataRemover deletes itself when it completes. | 178 // BrowsingDataRemover deletes itself when it completes. |
200 remover->Remove(remove_mask); | 179 remover->Remove(remove_mask); |
201 tester->BlockUntilNotified(); | 180 tester->BlockUntilNotified(); |
202 } | 181 } |
203 | 182 |
204 TestingProfile* GetProfile() { | 183 TestingProfile* GetProfile() { |
205 return profile_.get(); | 184 return profile_.get(); |
206 } | 185 } |
207 | 186 |
| 187 quota::MockQuotaManager* GetMockManager() { |
| 188 return (quota::MockQuotaManager*) profile_->GetQuotaManager(); |
| 189 } |
| 190 |
208 private: | 191 private: |
209 // message_loop_, as well as all the threads associated with it must be | 192 // message_loop_, as well as all the threads associated with it must be |
210 // defined before profile_ to prevent explosions. Oh how I love C++. | 193 // defined before profile_ to prevent explosions. Oh how I love C++. |
211 MessageLoopForUI message_loop_; | 194 MessageLoopForUI message_loop_; |
212 BrowserThread ui_thread_; | 195 BrowserThread ui_thread_; |
213 BrowserThread db_thread_; | 196 BrowserThread db_thread_; |
214 BrowserThread webkit_thread_; | 197 BrowserThread webkit_thread_; |
215 BrowserThread file_thread_; | 198 BrowserThread file_thread_; |
216 BrowserThread io_thread_; | 199 BrowserThread io_thread_; |
217 scoped_ptr<TestingProfile> profile_; | 200 scoped_ptr<TestingProfile> profile_; |
218 | 201 |
219 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemoverTest); | 202 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemoverTest); |
220 }; | 203 }; |
221 | 204 |
| 205 // Tests --------------------------------------------------------------------- |
| 206 |
222 TEST_F(BrowsingDataRemoverTest, RemoveHistoryForever) { | 207 TEST_F(BrowsingDataRemoverTest, RemoveHistoryForever) { |
223 scoped_ptr<RemoveHistoryTester> tester( | 208 scoped_ptr<RemoveHistoryTester> tester( |
224 new RemoveHistoryTester(GetProfile())); | 209 new RemoveHistoryTester(GetProfile())); |
225 | 210 |
226 tester->AddHistory(kOrigin1, base::Time::Now()); | 211 tester->AddHistory(kOrigin1, base::Time::Now()); |
227 ASSERT_TRUE(tester->HistoryContainsURL(kOrigin1)); | 212 ASSERT_TRUE(tester->HistoryContainsURL(kOrigin1)); |
228 | 213 |
229 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING, | 214 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING, |
230 base::Time::Now(), BrowsingDataRemover::REMOVE_HISTORY, tester.get()); | 215 base::Time::Now(), BrowsingDataRemover::REMOVE_HISTORY, tester.get()); |
231 | 216 |
(...skipping 11 matching lines...) Expand all Loading... |
243 ASSERT_TRUE(tester->HistoryContainsURL(kOrigin1)); | 228 ASSERT_TRUE(tester->HistoryContainsURL(kOrigin1)); |
244 ASSERT_TRUE(tester->HistoryContainsURL(kOrigin2)); | 229 ASSERT_TRUE(tester->HistoryContainsURL(kOrigin2)); |
245 | 230 |
246 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::LAST_HOUR, | 231 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::LAST_HOUR, |
247 base::Time::Now(), BrowsingDataRemover::REMOVE_HISTORY, tester.get()); | 232 base::Time::Now(), BrowsingDataRemover::REMOVE_HISTORY, tester.get()); |
248 | 233 |
249 EXPECT_FALSE(tester->HistoryContainsURL(kOrigin1)); | 234 EXPECT_FALSE(tester->HistoryContainsURL(kOrigin1)); |
250 EXPECT_TRUE(tester->HistoryContainsURL(kOrigin2)); | 235 EXPECT_TRUE(tester->HistoryContainsURL(kOrigin2)); |
251 } | 236 } |
252 | 237 |
253 TEST_F(BrowsingDataRemoverTest, RemoveFileSystemsForever) { | 238 TEST_F(BrowsingDataRemoverTest, RemoveQuotaManagedDataForever) { |
254 scoped_ptr<RemoveFileSystemTester> tester(new RemoveFileSystemTester()); | 239 scoped_ptr<RemoveQuotaManagedDataTester> tester( |
255 | 240 new RemoveQuotaManagedDataTester()); |
256 tester->PopulateTestFileSystemData(GetProfile()); | 241 tester->PopulateTestQuotaManagedData(GetMockManager()); |
257 | 242 |
258 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING, | 243 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING, |
259 base::Time::Now(), BrowsingDataRemover::REMOVE_COOKIES, tester.get()); | 244 base::Time::Now(), BrowsingDataRemover::REMOVE_COOKIES, tester.get()); |
260 | 245 |
261 EXPECT_FALSE(tester->FileSystemContainsOriginAndType(kOrigin1, | 246 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, |
262 fileapi::kFileSystemTypePersistent)); | 247 quota::kStorageTypeTemporary)); |
263 EXPECT_FALSE(tester->FileSystemContainsOriginAndType(kOrigin1, | 248 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, |
264 fileapi::kFileSystemTypeTemporary)); | 249 quota::kStorageTypeTemporary)); |
265 EXPECT_FALSE(tester->FileSystemContainsOriginAndType(kOrigin2, | 250 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, |
266 fileapi::kFileSystemTypePersistent)); | 251 quota::kStorageTypeTemporary)); |
267 EXPECT_FALSE(tester->FileSystemContainsOriginAndType(kOrigin2, | 252 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, |
268 fileapi::kFileSystemTypeTemporary)); | 253 quota::kStorageTypePersistent)); |
269 EXPECT_FALSE(tester->FileSystemContainsOriginAndType(kOrigin3, | 254 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, |
270 fileapi::kFileSystemTypePersistent)); | 255 quota::kStorageTypePersistent)); |
271 EXPECT_FALSE(tester->FileSystemContainsOriginAndType(kOrigin3, | 256 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, |
272 fileapi::kFileSystemTypeTemporary)); | 257 quota::kStorageTypePersistent)); |
273 } | 258 } |
274 | 259 |
275 TEST_F(BrowsingDataRemoverTest, RemoveAppCacheForever) { | 260 TEST_F(BrowsingDataRemoverTest, RemoveQuotaManagedDataForLastHour) { |
276 // Set up ChromeAppCacheService. | 261 scoped_ptr<RemoveQuotaManagedDataTester> tester( |
277 scoped_refptr<ChromeAppCacheService> appcache_service = | 262 new RemoveQuotaManagedDataTester()); |
278 new ChromeAppCacheService(NULL); | 263 tester->PopulateTestQuotaManagedData(GetMockManager()); |
279 const content::ResourceContext* resource_context = NULL; | |
280 scoped_refptr<MockExtensionSpecialStoragePolicy> mock_policy = | |
281 new MockExtensionSpecialStoragePolicy; | |
282 mock_policy->AddProtected(kProtectedManifest.GetOrigin()); | |
283 BrowserThread::PostTask( | |
284 BrowserThread::IO, FROM_HERE, | |
285 NewRunnableMethod(appcache_service.get(), | |
286 &ChromeAppCacheService::InitializeOnIOThread, | |
287 FilePath(), | |
288 resource_context, | |
289 mock_policy)); | |
290 MessageLoop::current()->RunAllPending(); | |
291 TestingProfile* profile = GetProfile(); | |
292 profile->SetAppCacheService(appcache_service); | |
293 profile->SetExtensionSpecialStoragePolicy(mock_policy); | |
294 | 264 |
295 // Add data into the AppCacheStorage. | 265 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::LAST_HOUR, |
296 appcache::AppCacheTestHelper appcache_helper; | 266 base::Time::Now(), BrowsingDataRemover::REMOVE_COOKIES, tester.get()); |
297 appcache_helper.AddGroupAndCache(appcache_service, kNormalManifest); | |
298 appcache_helper.AddGroupAndCache(appcache_service, kProtectedManifest); | |
299 | 267 |
300 // Verify that adding the data succeeded. | 268 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, |
301 std::set<GURL> origins; | 269 quota::kStorageTypeTemporary)); |
302 appcache_helper.GetOriginsWithCaches(appcache_service, &origins); | 270 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, |
303 EXPECT_EQ(2UL, origins.size()); | 271 quota::kStorageTypeTemporary)); |
304 EXPECT_TRUE(origins.find(kProtectedManifest.GetOrigin()) != origins.end()); | 272 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, |
305 EXPECT_TRUE(origins.find(kNormalManifest.GetOrigin()) != origins.end()); | 273 quota::kStorageTypeTemporary)); |
| 274 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, |
| 275 quota::kStorageTypePersistent)); |
| 276 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2, |
| 277 quota::kStorageTypePersistent)); |
| 278 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, |
| 279 quota::kStorageTypePersistent)); |
| 280 } |
306 | 281 |
307 // Set up the object to be tested. | 282 TEST_F(BrowsingDataRemoverTest, RemoveQuotaManagedDataForLastWeek) { |
308 scoped_ptr<BrowsingDataRemoverTester> tester(new BrowsingDataRemoverTester()); | 283 scoped_ptr<RemoveQuotaManagedDataTester> tester( |
309 BrowsingDataRemover* remover = new BrowsingDataRemover( | 284 new RemoveQuotaManagedDataTester()); |
310 profile, BrowsingDataRemover::EVERYTHING, base::Time::Now()); | 285 tester->PopulateTestQuotaManagedData(GetMockManager()); |
311 remover->AddObserver(tester.get()); | |
312 | 286 |
313 // Remove the appcaches and wait for it to complete. BrowsingDataRemover | 287 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::LAST_WEEK, |
314 // deletes itself when it completes. | 288 base::Time::Now(), BrowsingDataRemover::REMOVE_COOKIES, tester.get()); |
315 remover->Remove(BrowsingDataRemover::REMOVE_COOKIES); | |
316 tester->BlockUntilNotified(); | |
317 | 289 |
318 // Results: appcaches for the normal origin got deleted, appcaches for the | 290 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, |
319 // protected origin didn't. | 291 quota::kStorageTypeTemporary)); |
320 appcache_helper.GetOriginsWithCaches(appcache_service, &origins); | 292 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, |
321 EXPECT_EQ(1UL, origins.size()); | 293 quota::kStorageTypeTemporary)); |
322 EXPECT_TRUE(origins.find(kProtectedManifest.GetOrigin()) != origins.end()); | 294 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, |
| 295 quota::kStorageTypeTemporary)); |
| 296 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, |
| 297 quota::kStorageTypePersistent)); |
| 298 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2, |
| 299 quota::kStorageTypePersistent)); |
| 300 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, |
| 301 quota::kStorageTypePersistent)); |
323 } | 302 } |
324 | 303 |
325 } // namespace | 304 } // namespace |
OLD | NEW |