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" | 14 #include "content/browser/appcache/chrome_appcache_service.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "webkit/appcache/appcache.h" | 16 #include "webkit/appcache/appcache_test_helper.h" |
17 #include "webkit/appcache/appcache_group.h" | |
18 #include "webkit/appcache/appcache_storage.h" | |
19 #include "webkit/fileapi/file_system_context.h" | 17 #include "webkit/fileapi/file_system_context.h" |
20 #include "webkit/fileapi/file_system_operation_context.h" | 18 #include "webkit/fileapi/file_system_operation_context.h" |
21 #include "webkit/fileapi/file_system_file_util.h" | 19 #include "webkit/fileapi/file_system_file_util.h" |
22 #include "webkit/fileapi/file_system_path_manager.h" | 20 #include "webkit/fileapi/file_system_path_manager.h" |
23 #include "webkit/fileapi/sandbox_mount_point_provider.h" | 21 #include "webkit/fileapi/sandbox_mount_point_provider.h" |
24 | 22 |
25 namespace { | 23 namespace { |
26 | 24 |
27 const char kTestkOrigin1[] = "http://host1:1/"; | 25 const char kTestkOrigin1[] = "http://host1:1/"; |
28 const char kTestkOrigin2[] = "http://host2:1/"; | 26 const char kTestkOrigin2[] = "http://host2:1/"; |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 EXPECT_TRUE(file_util::DirectoryExists(target)); | 157 EXPECT_TRUE(file_util::DirectoryExists(target)); |
160 } | 158 } |
161 | 159 |
162 private: | 160 private: |
163 fileapi::SandboxMountPointProvider* sandbox_; | 161 fileapi::SandboxMountPointProvider* sandbox_; |
164 bool found_file_system_; | 162 bool found_file_system_; |
165 | 163 |
166 DISALLOW_COPY_AND_ASSIGN(RemoveFileSystemTester); | 164 DISALLOW_COPY_AND_ASSIGN(RemoveFileSystemTester); |
167 }; | 165 }; |
168 | 166 |
169 // Helper class for injecting data into AppCacheStorage. | |
170 class AppCacheInserter : public appcache::AppCacheStorage::Delegate { | |
171 public: | |
172 AppCacheInserter() | |
173 : group_id_(0), | |
174 appcache_id_(0), | |
175 response_id_(0) {} | |
176 virtual ~AppCacheInserter() {} | |
177 | |
178 virtual void OnGroupAndNewestCacheStored( | |
179 appcache::AppCacheGroup* /*group*/, | |
180 appcache::AppCache* /*newest_cache*/, | |
181 bool success, | |
182 bool /*would_exceed_quota*/) { | |
183 ASSERT_TRUE(success); | |
184 MessageLoop::current()->Quit(); | |
185 } | |
186 | |
187 void AddGroupAndCache(ChromeAppCacheService* appcache_service, | |
188 const GURL& manifest_url) { | |
189 appcache::AppCacheGroup* appcache_group = | |
190 new appcache::AppCacheGroup(appcache_service, | |
191 manifest_url, | |
192 ++group_id_); | |
193 appcache::AppCache* appcache = new appcache::AppCache(appcache_service, | |
194 ++appcache_id_); | |
195 appcache::AppCacheEntry entry(appcache::AppCacheEntry::MANIFEST, | |
196 ++response_id_); | |
197 appcache->AddEntry(manifest_url, entry); | |
198 appcache->set_complete(true); | |
199 appcache_group->AddCache(appcache); | |
200 appcache_service->storage()->StoreGroupAndNewestCache(appcache_group, | |
201 appcache, | |
202 this); | |
203 // OnGroupAndNewestCacheStored will quit the message loop. | |
204 MessageLoop::current()->Run(); | |
205 } | |
206 | |
207 private: | |
208 int group_id_; | |
209 int appcache_id_; | |
210 int response_id_; | |
211 | |
212 DISALLOW_COPY_AND_ASSIGN(AppCacheInserter); | |
213 }; | |
214 | |
215 // Helper class for reading appcaches. | |
216 class AppCacheReader { | |
217 public: | |
218 explicit AppCacheReader(ChromeAppCacheService* appcache_service) | |
219 : appcache_service_(appcache_service), | |
220 ALLOW_THIS_IN_INITIALIZER_LIST(appcache_got_info_callback_( | |
221 this, &AppCacheReader::OnGotAppCacheInfo)) { | |
222 } | |
223 | |
224 std::set<GURL>* ReadAppCaches() { | |
225 appcache_info_ = new appcache::AppCacheInfoCollection; | |
226 appcache_service_->GetAllAppCacheInfo( | |
227 appcache_info_, &appcache_got_info_callback_); | |
228 | |
229 MessageLoop::current()->Run(); | |
230 return &origins_; | |
231 } | |
232 | |
233 private: | |
234 void OnGotAppCacheInfo(int rv) { | |
235 typedef std::map<GURL, appcache::AppCacheInfoVector> InfoByOrigin; | |
236 | |
237 origins_.clear(); | |
238 for (InfoByOrigin::const_iterator origin = | |
239 appcache_info_->infos_by_origin.begin(); | |
240 origin != appcache_info_->infos_by_origin.end(); ++origin) { | |
241 origins_.insert(origin->first); | |
242 } | |
243 MessageLoop::current()->Quit(); | |
244 } | |
245 | |
246 scoped_refptr<ChromeAppCacheService> appcache_service_; | |
247 net::CompletionCallbackImpl<AppCacheReader> | |
248 appcache_got_info_callback_; | |
249 scoped_refptr<appcache::AppCacheInfoCollection> appcache_info_; | |
250 std::set<GURL> origins_; | |
251 | |
252 DISALLOW_COPY_AND_ASSIGN(AppCacheReader); | |
253 }; | |
254 | |
255 class BrowsingDataRemoverTest : public testing::Test { | 167 class BrowsingDataRemoverTest : public testing::Test { |
256 public: | 168 public: |
257 BrowsingDataRemoverTest() | 169 BrowsingDataRemoverTest() |
258 : ui_thread_(BrowserThread::UI, &message_loop_), | 170 : ui_thread_(BrowserThread::UI, &message_loop_), |
259 db_thread_(BrowserThread::DB, &message_loop_), | 171 db_thread_(BrowserThread::DB, &message_loop_), |
260 webkit_thread_(BrowserThread::WEBKIT, &message_loop_), | 172 webkit_thread_(BrowserThread::WEBKIT, &message_loop_), |
261 file_thread_(BrowserThread::FILE, &message_loop_), | 173 file_thread_(BrowserThread::FILE, &message_loop_), |
262 io_thread_(BrowserThread::IO, &message_loop_), | 174 io_thread_(BrowserThread::IO, &message_loop_), |
263 profile_(new TestingProfile()) { | 175 profile_(new TestingProfile()) { |
264 } | 176 } |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 const content::ResourceContext* resource_context = NULL; | 279 const content::ResourceContext* resource_context = NULL; |
368 scoped_refptr<MockExtensionSpecialStoragePolicy> mock_policy = | 280 scoped_refptr<MockExtensionSpecialStoragePolicy> mock_policy = |
369 new MockExtensionSpecialStoragePolicy; | 281 new MockExtensionSpecialStoragePolicy; |
370 mock_policy->AddProtected(kProtectedManifest.GetOrigin()); | 282 mock_policy->AddProtected(kProtectedManifest.GetOrigin()); |
371 BrowserThread::PostTask( | 283 BrowserThread::PostTask( |
372 BrowserThread::IO, FROM_HERE, | 284 BrowserThread::IO, FROM_HERE, |
373 NewRunnableMethod(appcache_service.get(), | 285 NewRunnableMethod(appcache_service.get(), |
374 &ChromeAppCacheService::InitializeOnIOThread, | 286 &ChromeAppCacheService::InitializeOnIOThread, |
375 FilePath(), | 287 FilePath(), |
376 resource_context, | 288 resource_context, |
377 mock_policy, | 289 mock_policy)); |
378 false)); | |
379 MessageLoop::current()->RunAllPending(); | 290 MessageLoop::current()->RunAllPending(); |
380 TestingProfile* profile = GetProfile(); | 291 TestingProfile* profile = GetProfile(); |
381 profile->SetAppCacheService(appcache_service); | 292 profile->SetAppCacheService(appcache_service); |
382 profile->SetExtensionSpecialStoragePolicy(mock_policy); | 293 profile->SetExtensionSpecialStoragePolicy(mock_policy); |
383 | 294 |
384 // Add data into the AppCacheStorage. | 295 // Add data into the AppCacheStorage. |
385 AppCacheInserter appcache_inserter; | 296 appcache::AppCacheTestHelper appcache_helper; |
386 appcache_inserter.AddGroupAndCache(appcache_service, kNormalManifest); | 297 appcache_helper.AddGroupAndCache(appcache_service, kNormalManifest); |
387 appcache_inserter.AddGroupAndCache(appcache_service, kProtectedManifest); | 298 appcache_helper.AddGroupAndCache(appcache_service, kProtectedManifest); |
388 | 299 |
389 // Verify that adding the data succeeded. | 300 // Verify that adding the data succeeded. |
390 AppCacheReader reader(appcache_service); | 301 std::set<GURL> origins; |
391 std::set<GURL>* origins = reader.ReadAppCaches(); | 302 appcache_helper.GetOriginsWithCaches(appcache_service, &origins); |
392 EXPECT_EQ(2UL, origins->size()); | 303 EXPECT_EQ(2UL, origins.size()); |
393 EXPECT_TRUE(origins->find(kProtectedManifest.GetOrigin()) != origins->end()); | 304 EXPECT_TRUE(origins.find(kProtectedManifest.GetOrigin()) != origins.end()); |
394 EXPECT_TRUE(origins->find(kNormalManifest.GetOrigin()) != origins->end()); | 305 EXPECT_TRUE(origins.find(kNormalManifest.GetOrigin()) != origins.end()); |
395 | 306 |
396 // Set up the object to be tested. | 307 // Set up the object to be tested. |
397 scoped_ptr<BrowsingDataRemoverTester> tester(new BrowsingDataRemoverTester()); | 308 scoped_ptr<BrowsingDataRemoverTester> tester(new BrowsingDataRemoverTester()); |
398 BrowsingDataRemover* remover = new BrowsingDataRemover( | 309 BrowsingDataRemover* remover = new BrowsingDataRemover( |
399 profile, BrowsingDataRemover::EVERYTHING, base::Time::Now()); | 310 profile, BrowsingDataRemover::EVERYTHING, base::Time::Now()); |
400 remover->AddObserver(tester.get()); | 311 remover->AddObserver(tester.get()); |
401 | 312 |
402 // Remove the appcaches and wait for it to complete. BrowsingDataRemover | 313 // Remove the appcaches and wait for it to complete. BrowsingDataRemover |
403 // deletes itself when it completes. | 314 // deletes itself when it completes. |
404 remover->Remove(BrowsingDataRemover::REMOVE_COOKIES); | 315 remover->Remove(BrowsingDataRemover::REMOVE_COOKIES); |
405 tester->BlockUntilNotified(); | 316 tester->BlockUntilNotified(); |
406 | 317 |
407 // Results: appcaches for the normal origin got deleted, appcaches for the | 318 // Results: appcaches for the normal origin got deleted, appcaches for the |
408 // protected origin didn't. | 319 // protected origin didn't. |
409 origins = reader.ReadAppCaches(); | 320 appcache_helper.GetOriginsWithCaches(appcache_service, &origins); |
410 EXPECT_EQ(1UL, origins->size()); | 321 EXPECT_EQ(1UL, origins.size()); |
411 EXPECT_TRUE(origins->find(kProtectedManifest.GetOrigin()) != origins->end()); | 322 EXPECT_TRUE(origins.find(kProtectedManifest.GetOrigin()) != origins.end()); |
412 } | 323 } |
413 | 324 |
414 } // namespace | 325 } // namespace |
OLD | NEW |