| 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> |
| 8 |
| 7 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 8 #include "base/platform_file.h" | 10 #include "base/platform_file.h" |
| 9 #include "chrome/browser/history/history.h" | 11 #include "chrome/browser/history/history.h" |
| 10 #include "chrome/test/testing_profile.h" | 12 #include "chrome/test/testing_profile.h" |
| 13 #include "content/browser/appcache/chrome_appcache_service.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "webkit/appcache/appcache.h" |
| 16 #include "webkit/appcache/appcache_group.h" |
| 17 #include "webkit/appcache/appcache_storage.h" |
| 12 #include "webkit/fileapi/file_system_context.h" | 18 #include "webkit/fileapi/file_system_context.h" |
| 13 #include "webkit/fileapi/file_system_operation_context.h" | 19 #include "webkit/fileapi/file_system_operation_context.h" |
| 14 #include "webkit/fileapi/file_system_file_util.h" | 20 #include "webkit/fileapi/file_system_file_util.h" |
| 15 #include "webkit/fileapi/file_system_path_manager.h" | 21 #include "webkit/fileapi/file_system_path_manager.h" |
| 16 #include "webkit/fileapi/sandbox_mount_point_provider.h" | 22 #include "webkit/fileapi/sandbox_mount_point_provider.h" |
| 23 #include "webkit/quota/mock_special_storage_policy.h" |
| 17 | 24 |
| 18 namespace { | 25 namespace browsing_data_remover_test { |
| 19 | 26 |
| 20 const char kTestkOrigin1[] = "http://host1:1/"; | 27 const char kTestkOrigin1[] = "http://host1:1/"; |
| 21 const char kTestkOrigin2[] = "http://host2:1/"; | 28 const char kTestkOrigin2[] = "http://host2:1/"; |
| 22 const char kTestkOrigin3[] = "http://host3:1/"; | 29 const char kTestkOrigin3[] = "http://host3:1/"; |
| 23 | 30 |
| 24 const GURL kOrigin1(kTestkOrigin1); | 31 const GURL kOrigin1(kTestkOrigin1); |
| 25 const GURL kOrigin2(kTestkOrigin2); | 32 const GURL kOrigin2(kTestkOrigin2); |
| 26 const GURL kOrigin3(kTestkOrigin3); | 33 const GURL kOrigin3(kTestkOrigin3); |
| 27 | 34 |
| 35 const GURL kProtectedManifest("http://www.protected.com/cache.manifest"); |
| 36 const GURL kNormalManifest("http://www.normal.com/cache.manifest"); |
| 37 |
| 28 class BrowsingDataRemoverTester : public BrowsingDataRemover::Observer { | 38 class BrowsingDataRemoverTester : public BrowsingDataRemover::Observer { |
| 29 public: | 39 public: |
| 30 BrowsingDataRemoverTester() {} | 40 BrowsingDataRemoverTester() {} |
| 31 virtual ~BrowsingDataRemoverTester() {} | 41 virtual ~BrowsingDataRemoverTester() {} |
| 32 | 42 |
| 33 void BlockUntilNotified() { | 43 void BlockUntilNotified() { |
| 34 MessageLoop::current()->Run(); | 44 MessageLoop::current()->Run(); |
| 35 } | 45 } |
| 36 | 46 |
| 37 protected: | 47 protected: |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 EXPECT_TRUE(file_util::DirectoryExists(target)); | 159 EXPECT_TRUE(file_util::DirectoryExists(target)); |
| 150 } | 160 } |
| 151 | 161 |
| 152 private: | 162 private: |
| 153 fileapi::SandboxMountPointProvider* sandbox_; | 163 fileapi::SandboxMountPointProvider* sandbox_; |
| 154 bool found_file_system_; | 164 bool found_file_system_; |
| 155 | 165 |
| 156 DISALLOW_COPY_AND_ASSIGN(RemoveFileSystemTester); | 166 DISALLOW_COPY_AND_ASSIGN(RemoveFileSystemTester); |
| 157 }; | 167 }; |
| 158 | 168 |
| 169 // Helper class for injecting data into AppCacheStorage. |
| 170 class DummyStorageDelegate : public appcache::AppCacheStorage::Delegate { |
| 171 public: |
| 172 DummyStorageDelegate() { } |
| 173 virtual void OnGroupAndNewestCacheStored( |
| 174 appcache::AppCacheGroup* /*group*/, appcache::AppCache* /*newest_cache*/, |
| 175 bool success, bool /*would_exceed_quota*/) { |
| 176 EXPECT_TRUE(success); |
| 177 MessageLoop::current()->Quit(); |
| 178 } |
| 179 }; |
| 180 |
| 181 void AddGroupAndCache(ChromeAppCacheService* appcache_service, |
| 182 const GURL& manifest_url) { |
| 183 |
| 184 using appcache::AppCache; |
| 185 using appcache::AppCacheEntry; |
| 186 using appcache::AppCacheGroup; |
| 187 static int group_id = 0; |
| 188 static int appcache_id = 0; |
| 189 static int response_id = 0; |
| 190 AppCacheGroup* appcache_group = |
| 191 new AppCacheGroup(appcache_service, manifest_url, ++group_id); |
| 192 AppCache* appcache = new appcache::AppCache(appcache_service, ++appcache_id); |
| 193 AppCacheEntry entry(AppCacheEntry::MANIFEST, ++response_id); |
| 194 appcache->AddEntry(manifest_url, entry); |
| 195 appcache->set_complete(true); |
| 196 appcache_group->AddCache(appcache); |
| 197 DummyStorageDelegate test_delegate; |
| 198 appcache_service->storage()->StoreGroupAndNewestCache(appcache_group, |
| 199 appcache, |
| 200 &test_delegate); |
| 201 // DummyStorageDelegate::OnGroupAndNewestCacheStored will quit the message |
| 202 // loop. |
| 203 MessageLoop::current()->Run(); |
| 204 } |
| 205 |
| 206 // Helper class for reading appcaches. |
| 207 class AppCacheReader { |
| 208 public: |
| 209 explicit AppCacheReader(ChromeAppCacheService* appcache_service) |
| 210 : appcache_service_(appcache_service), |
| 211 ALLOW_THIS_IN_INITIALIZER_LIST(appcache_got_info_callback_( |
| 212 this, &AppCacheReader::OnGotAppCacheInfo)) { |
| 213 } |
| 214 |
| 215 std::set<GURL>* ReadAppCaches() { |
| 216 appcache_info_ = new appcache::AppCacheInfoCollection; |
| 217 appcache_service_->GetAllAppCacheInfo( |
| 218 appcache_info_, &appcache_got_info_callback_); |
| 219 |
| 220 MessageLoop::current()->Run(); |
| 221 return &origins_; |
| 222 } |
| 223 |
| 224 private: |
| 225 void OnGotAppCacheInfo(int rv) { |
| 226 using appcache::AppCacheInfoVector; |
| 227 typedef std::map<GURL, AppCacheInfoVector> InfoByOrigin; |
| 228 |
| 229 origins_.clear(); |
| 230 for (InfoByOrigin::const_iterator origin = |
| 231 appcache_info_->infos_by_origin.begin(); |
| 232 origin != appcache_info_->infos_by_origin.end(); ++origin) { |
| 233 origins_.insert(origin->first); |
| 234 } |
| 235 MessageLoop::current()->Quit(); |
| 236 } |
| 237 |
| 238 scoped_refptr<ChromeAppCacheService> appcache_service_; |
| 239 net::CompletionCallbackImpl<AppCacheReader> |
| 240 appcache_got_info_callback_; |
| 241 scoped_refptr<appcache::AppCacheInfoCollection> appcache_info_; |
| 242 std::set<GURL> origins_; |
| 243 }; |
| 244 |
| 159 class BrowsingDataRemoverTest : public testing::Test { | 245 class BrowsingDataRemoverTest : public testing::Test { |
| 160 public: | 246 public: |
| 161 BrowsingDataRemoverTest() | 247 BrowsingDataRemoverTest() |
| 162 : ui_thread_(BrowserThread::UI, &message_loop_), | 248 : ui_thread_(BrowserThread::UI, &message_loop_), |
| 163 db_thread_(BrowserThread::DB, &message_loop_), | 249 db_thread_(BrowserThread::DB, &message_loop_), |
| 164 webkit_thread_(BrowserThread::WEBKIT, &message_loop_), | 250 webkit_thread_(BrowserThread::WEBKIT, &message_loop_), |
| 165 file_thread_(BrowserThread::FILE, &message_loop_), | 251 file_thread_(BrowserThread::FILE, &message_loop_), |
| 166 io_thread_(BrowserThread::IO, &message_loop_), | 252 io_thread_(BrowserThread::IO, &message_loop_), |
| 167 profile_(new TestingProfile()) { | 253 profile_(new TestingProfile()) { |
| 168 } | 254 } |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 EXPECT_FALSE(tester->FileSystemContainsOriginAndType(kOrigin2, | 343 EXPECT_FALSE(tester->FileSystemContainsOriginAndType(kOrigin2, |
| 258 fileapi::kFileSystemTypePersistent)); | 344 fileapi::kFileSystemTypePersistent)); |
| 259 EXPECT_FALSE(tester->FileSystemContainsOriginAndType(kOrigin2, | 345 EXPECT_FALSE(tester->FileSystemContainsOriginAndType(kOrigin2, |
| 260 fileapi::kFileSystemTypeTemporary)); | 346 fileapi::kFileSystemTypeTemporary)); |
| 261 EXPECT_FALSE(tester->FileSystemContainsOriginAndType(kOrigin3, | 347 EXPECT_FALSE(tester->FileSystemContainsOriginAndType(kOrigin3, |
| 262 fileapi::kFileSystemTypePersistent)); | 348 fileapi::kFileSystemTypePersistent)); |
| 263 EXPECT_FALSE(tester->FileSystemContainsOriginAndType(kOrigin3, | 349 EXPECT_FALSE(tester->FileSystemContainsOriginAndType(kOrigin3, |
| 264 fileapi::kFileSystemTypeTemporary)); | 350 fileapi::kFileSystemTypeTemporary)); |
| 265 } | 351 } |
| 266 | 352 |
| 267 } // namespace | 353 |
| 354 TEST_F(BrowsingDataRemoverTest, RemoveAppCacheForever) { |
| 355 // Set up ChromeAppCacheService. |
| 356 scoped_refptr<ChromeAppCacheService> appcache_service = |
| 357 new ChromeAppCacheService(NULL); |
| 358 const content::ResourceContext* resource_context = NULL; |
| 359 scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy = |
| 360 new quota::MockSpecialStoragePolicy; |
| 361 mock_policy->AddProtected(kProtectedManifest.GetOrigin()); |
| 362 BrowserThread::PostTask( |
| 363 BrowserThread::IO, FROM_HERE, |
| 364 NewRunnableMethod(appcache_service.get(), |
| 365 &ChromeAppCacheService::InitializeOnIOThread, |
| 366 FilePath(""), |
| 367 resource_context, |
| 368 mock_policy)); |
| 369 MessageLoop::current()->RunAllPending(); |
| 370 TestingProfile* profile = GetProfile(); |
| 371 profile->SetAppCacheService(appcache_service); |
| 372 |
| 373 // Add data into the AppCacheStorage. |
| 374 AddGroupAndCache(appcache_service, kNormalManifest); |
| 375 AddGroupAndCache(appcache_service, kProtectedManifest); |
| 376 |
| 377 // Verify that adding the data succeeded. |
| 378 scoped_ptr<AppCacheReader> reader(new AppCacheReader(appcache_service)); |
| 379 std::set<GURL>* origins = reader->ReadAppCaches(); |
| 380 EXPECT_EQ(2UL, origins->size()); |
| 381 EXPECT_NE(origins->end(), origins->find(kProtectedManifest.GetOrigin())); |
| 382 EXPECT_NE(origins->end(), origins->find(kNormalManifest.GetOrigin())); |
| 383 |
| 384 // Set up the object to be tested. |
| 385 scoped_ptr<BrowsingDataRemoverTester> tester(new BrowsingDataRemoverTester()); |
| 386 BrowsingDataRemover* remover = new BrowsingDataRemover( |
| 387 profile, BrowsingDataRemover::EVERYTHING, base::Time::Now()); |
| 388 remover->AddObserver(tester.get()); |
| 389 |
| 390 // Remove the appcaches and wait for it to complete. BrowsingDataRemover |
| 391 // deletes itself when it completes. |
| 392 remover->Remove(BrowsingDataRemover::REMOVE_COOKIES); |
| 393 tester->BlockUntilNotified(); |
| 394 |
| 395 // Results: appcaches for the normal origin got deleted, appcaches for the |
| 396 // protected origin didn't. |
| 397 origins = reader->ReadAppCaches(); |
| 398 EXPECT_EQ(1UL, origins->size()); |
| 399 EXPECT_NE(origins->end(), origins->find(kProtectedManifest.GetOrigin())); |
| 400 } |
| 401 |
| 402 } // namespace browsing_data_remover_test |
| OLD | NEW |