diff options
author | jochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-10 12:55:00 +0000 |
---|---|---|
committer | jochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-10 12:55:00 +0000 |
commit | 33b61bcc0c64b4d44377f86df8746b54f2bce01b (patch) | |
tree | 75ce8e196f085e3799e8c0ac48ec31b2545b8a09 | |
parent | ded71afbd762da9b5a00fceaa1b75758d317fe2b (diff) | |
download | chromium_src-33b61bcc0c64b4d44377f86df8746b54f2bce01b.zip chromium_src-33b61bcc0c64b4d44377f86df8746b54f2bce01b.tar.gz chromium_src-33b61bcc0c64b4d44377f86df8746b54f2bce01b.tar.bz2 |
Wrappers around BrowsingDataHelpers that returned canned responses.
BUG=45230
TEST=CannedBrowsingData*HelperTest.*
Review URL: http://codereview.chromium.org/2707001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@49388 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/browsing_data_appcache_helper.cc | 23 | ||||
-rw-r--r-- | chrome/browser/browsing_data_appcache_helper.h | 33 | ||||
-rw-r--r-- | chrome/browser/browsing_data_appcache_helper_unittest.cc | 61 | ||||
-rw-r--r-- | chrome/browser/browsing_data_database_helper.cc | 26 | ||||
-rw-r--r-- | chrome/browser/browsing_data_database_helper.h | 36 | ||||
-rw-r--r-- | chrome/browser/browsing_data_database_helper_unittest.cc | 71 | ||||
-rw-r--r-- | chrome/browser/browsing_data_local_storage_helper.cc | 30 | ||||
-rw-r--r-- | chrome/browser/browsing_data_local_storage_helper.h | 36 | ||||
-rw-r--r-- | chrome/browser/browsing_data_local_storage_helper_unittest.cc | 66 | ||||
-rwxr-xr-x | chrome/chrome_tests.gypi | 3 |
10 files changed, 366 insertions, 19 deletions
diff --git a/chrome/browser/browsing_data_appcache_helper.cc b/chrome/browser/browsing_data_appcache_helper.cc index adfa2c6..f684e13 100644 --- a/chrome/browser/browsing_data_appcache_helper.cc +++ b/chrome/browser/browsing_data_appcache_helper.cc @@ -16,7 +16,6 @@ using appcache::AppCacheDatabase; BrowsingDataAppCacheHelper::BrowsingDataAppCacheHelper(Profile* profile) : request_context_getter_(profile->GetRequestContext()), is_fetching_(false) { - DCHECK(request_context_getter_.get()); } void BrowsingDataAppCacheHelper::StartFetching(Callback0::Type* callback) { @@ -99,3 +98,25 @@ ChromeAppCacheService* BrowsingDataAppCacheHelper::GetAppCacheService() { return request_context ? request_context->appcache_service() : NULL; } + +CannedBrowsingDataAppCacheHelper::CannedBrowsingDataAppCacheHelper( + Profile* profile) + : BrowsingDataAppCacheHelper(profile) { + info_collection_ = new appcache::AppCacheInfoCollection; +} + +void CannedBrowsingDataAppCacheHelper::AddAppCache(const GURL& manifest_url) { + typedef std::map<GURL, appcache::AppCacheInfoVector> InfoByOrigin; + InfoByOrigin& origin_map = info_collection_->infos_by_origin; + origin_map[manifest_url.GetOrigin()].push_back( + appcache::AppCacheInfo(manifest_url, + 0, + base::Time(), + base::Time(), + base::Time())); +} + +void CannedBrowsingDataAppCacheHelper::StartFetching( + Callback0::Type* completion_callback) { + completion_callback->Run(); +} diff --git a/chrome/browser/browsing_data_appcache_helper.h b/chrome/browser/browsing_data_appcache_helper.h index f5abf35..226567da 100644 --- a/chrome/browser/browsing_data_appcache_helper.h +++ b/chrome/browser/browsing_data_appcache_helper.h @@ -6,12 +6,12 @@ #define CHROME_BROWSER_BROWSING_DATA_APPCACHE_HELPER_H_ #include <string> -#include <vector> #include "base/scoped_ptr.h" #include "base/task.h" #include "chrome/browser/appcache/chrome_appcache_service.h" #include "chrome/common/net/url_request_context_getter.h" +#include "googleurl/src/gurl.h" class Profile; @@ -31,23 +31,44 @@ class BrowsingDataAppCacheHelper return info_collection_; } - private: + protected: friend class base::RefCountedThreadSafe<BrowsingDataAppCacheHelper>; - friend class MockBrowsingDataAppCacheHelper; - virtual ~BrowsingDataAppCacheHelper() {} + scoped_ptr<Callback0::Type> completion_callback_; + scoped_refptr<appcache::AppCacheInfoCollection> info_collection_; + + private: void OnFetchComplete(int rv); ChromeAppCacheService* GetAppCacheService(); scoped_refptr<URLRequestContextGetter> request_context_getter_; bool is_fetching_; - scoped_ptr<Callback0::Type> completion_callback_; - scoped_refptr<appcache::AppCacheInfoCollection> info_collection_; scoped_refptr<net::CancelableCompletionCallback<BrowsingDataAppCacheHelper> > appcache_info_callback_; DISALLOW_COPY_AND_ASSIGN(BrowsingDataAppCacheHelper); }; +// This class is a thin wrapper around BrowsingDataAppCacheHelper that does not +// fetch its information from the appcache service, but gets them passed as +// a parameter during construction. +class CannedBrowsingDataAppCacheHelper : public BrowsingDataAppCacheHelper { + public: + explicit CannedBrowsingDataAppCacheHelper(Profile* profile); + + // Add an appcache to the set of canned caches that is returned by this + // helper. + void AddAppCache(const GURL& manifest_url); + + // BrowsingDataAppCacheHelper methods. + virtual void StartFetching(Callback0::Type* completion_callback); + virtual void CancelNotification() {} + + private: + virtual ~CannedBrowsingDataAppCacheHelper() {} + + DISALLOW_COPY_AND_ASSIGN(CannedBrowsingDataAppCacheHelper); +}; + #endif // CHROME_BROWSER_BROWSING_DATA_APPCACHE_HELPER_H_ diff --git a/chrome/browser/browsing_data_appcache_helper_unittest.cc b/chrome/browser/browsing_data_appcache_helper_unittest.cc new file mode 100644 index 0000000..fe30c7c --- /dev/null +++ b/chrome/browser/browsing_data_appcache_helper_unittest.cc @@ -0,0 +1,61 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/browsing_data_appcache_helper.h" + +#include "base/stl_util-inl.h" +#include "chrome/test/testing_profile.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { +class TestCompletionCallback : public CallbackRunner<Tuple0> { + public: + TestCompletionCallback() + : have_result_(false) { + } + + bool have_result() const { return have_result_; } + + virtual void RunWithParams(const Tuple0& params) { + have_result_ = true; + } + private: + bool have_result_; +}; + +} // namespace + +TEST(CannedBrowsingDataAppCacheHelperTest, SetInfo) { + TestingProfile profile; + + GURL manifest1("http://example1.com/manifest.xml"); + GURL manifest2("http://example2.com/path1/manifest.xml"); + GURL manifest3("http://example2.com/path2/manifest.xml"); + + scoped_refptr<CannedBrowsingDataAppCacheHelper> helper = + new CannedBrowsingDataAppCacheHelper(&profile); + helper->AddAppCache(manifest1); + helper->AddAppCache(manifest2); + helper->AddAppCache(manifest3); + + TestCompletionCallback callback; + helper->StartFetching(&callback); + ASSERT_TRUE(callback.have_result()); + + std::map<GURL, appcache::AppCacheInfoVector>& collection = + helper->info_collection()->infos_by_origin; + + ASSERT_EQ(2u, collection.size()); + EXPECT_TRUE(ContainsKey(collection, manifest1.GetOrigin())); + ASSERT_EQ(1u, collection[manifest1.GetOrigin()].size()); + EXPECT_EQ(manifest1, collection[manifest1.GetOrigin()].at(0).manifest_url); + + EXPECT_TRUE(ContainsKey(collection, manifest2.GetOrigin())); + EXPECT_EQ(2u, collection[manifest2.GetOrigin()].size()); + std::set<GURL> manifest_results; + manifest_results.insert(collection[manifest2.GetOrigin()].at(0).manifest_url); + manifest_results.insert(collection[manifest2.GetOrigin()].at(1).manifest_url); + EXPECT_TRUE(ContainsKey(manifest_results, manifest2)); + EXPECT_TRUE(ContainsKey(manifest_results, manifest3)); +} diff --git a/chrome/browser/browsing_data_database_helper.cc b/chrome/browser/browsing_data_database_helper.cc index 7ff2428..27549f5 100644 --- a/chrome/browser/browsing_data_database_helper.cc +++ b/chrome/browser/browsing_data_database_helper.cc @@ -110,3 +110,29 @@ void BrowsingDataDatabaseHelper::DeleteDatabaseInFileThread( return; tracker_->DeleteDatabase(UTF8ToUTF16(origin), UTF8ToUTF16(name), NULL); } + +CannedBrowsingDataDatabaseHelper::CannedBrowsingDataDatabaseHelper( + Profile* profile) + : BrowsingDataDatabaseHelper(profile) { +} + +void CannedBrowsingDataDatabaseHelper::AddDatabase( + const GURL& origin, + const std::string& name, + const std::string& description) { + WebKit::WebSecurityOrigin web_security_origin = + WebKit::WebSecurityOrigin::createFromString( + UTF8ToUTF16(origin.spec())); + database_info_.push_back(DatabaseInfo( + web_security_origin.host().utf8(), + name, + web_security_origin.databaseIdentifier().utf8(), + description, + 0, + base::Time())); +} + +void CannedBrowsingDataDatabaseHelper::StartFetching( + Callback1<const std::vector<DatabaseInfo>& >::Type* callback) { + callback->Run(database_info_); +} diff --git a/chrome/browser/browsing_data_database_helper.h b/chrome/browser/browsing_data_database_helper.h index 9f48b49..a0b39c6 100644 --- a/chrome/browser/browsing_data_database_helper.h +++ b/chrome/browser/browsing_data_database_helper.h @@ -11,6 +11,7 @@ #include "base/callback.h" #include "base/scoped_ptr.h" #include "chrome/common/url_constants.h" +#include "googleurl/src/gurl.h" #include "webkit/database/database_tracker.h" class Profile; @@ -74,12 +75,14 @@ class BrowsingDataDatabaseHelper virtual void DeleteDatabase(const std::string& origin, const std::string& name); - private: + protected: friend class base::RefCountedThreadSafe<BrowsingDataDatabaseHelper>; - friend class MockBrowsingDataDatabaseHelper; - virtual ~BrowsingDataDatabaseHelper(); + // This only mutates in the FILE thread. + std::vector<DatabaseInfo> database_info_; + + private: // Enumerates all databases. This must be called in the FILE thread. void FetchDatabaseInfoInFileThread(); @@ -102,10 +105,31 @@ class BrowsingDataDatabaseHelper // This only mutates on the UI thread. bool is_fetching_; - // This only mutates in the FILE thread. - std::vector<DatabaseInfo> database_info_; - DISALLOW_COPY_AND_ASSIGN(BrowsingDataDatabaseHelper); }; +// This class is a thin wrapper around BrowsingDataDatabaseHelper that does not +// fetch its information from the database tracker, but gets them passed as +// a parameter during construction. +class CannedBrowsingDataDatabaseHelper : public BrowsingDataDatabaseHelper { + public: + explicit CannedBrowsingDataDatabaseHelper(Profile* profile); + + // Add a database to the set of canned databases that is returned by this + // helper. + void AddDatabase(const GURL& origin, + const std::string& name, + const std::string& description); + + // BrowsingDataDatabaseHelper methods. + virtual void StartFetching( + Callback1<const std::vector<DatabaseInfo>& >::Type* callback); + virtual void CancelNotification() {} + + private: + virtual ~CannedBrowsingDataDatabaseHelper() {} + + DISALLOW_COPY_AND_ASSIGN(CannedBrowsingDataDatabaseHelper); +}; + #endif // CHROME_BROWSER_BROWSING_DATA_DATABASE_HELPER_H_ diff --git a/chrome/browser/browsing_data_database_helper_unittest.cc b/chrome/browser/browsing_data_database_helper_unittest.cc new file mode 100644 index 0000000..5b6a8d5 --- /dev/null +++ b/chrome/browser/browsing_data_database_helper_unittest.cc @@ -0,0 +1,71 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/browsing_data_database_helper.h" + +#include "chrome/test/testing_profile.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { +class TestCompletionCallback + : public CallbackRunner<Tuple1< + const std::vector<BrowsingDataDatabaseHelper::DatabaseInfo>& > > { + public: + TestCompletionCallback() + : have_result_(false) { + } + + bool have_result() const { return have_result_; } + + const std::vector<BrowsingDataDatabaseHelper::DatabaseInfo>& result() { + return result_; + } + + virtual void RunWithParams( + const Tuple1<const std::vector< + BrowsingDataDatabaseHelper::DatabaseInfo>& >& params) { + have_result_ = true; + result_ = params.a; + } + + private: + bool have_result_; + std::vector<BrowsingDataDatabaseHelper::DatabaseInfo> result_; + + DISALLOW_COPY_AND_ASSIGN(TestCompletionCallback); +}; +} // namespace + +TEST(CannedBrowsingDataDatabaseTest, AddDatabase) { + TestingProfile profile; + + const GURL origin1("http://host1:1/"); + const GURL origin2("http://host2:1/"); + const char origin_str1[] = "http_host1_1"; + const char origin_str2[] = "http_host2_1"; + const char db1[] = "db1"; + const char db2[] = "db2"; + const char db3[] = "db3"; + + scoped_refptr<CannedBrowsingDataDatabaseHelper> helper = + new CannedBrowsingDataDatabaseHelper(&profile); + helper->AddDatabase(origin1, db1, ""); + helper->AddDatabase(origin1, db2, ""); + helper->AddDatabase(origin2, db3, ""); + + TestCompletionCallback callback; + helper->StartFetching(&callback); + ASSERT_TRUE(callback.have_result()); + + std::vector<BrowsingDataDatabaseHelper::DatabaseInfo> result = + callback.result(); + + ASSERT_EQ(3u, result.size()); + EXPECT_STREQ(origin_str1, result[0].origin_identifier.c_str()); + EXPECT_STREQ(db1, result[0].database_name.c_str()); + EXPECT_STREQ(origin_str1, result[1].origin_identifier.c_str()); + EXPECT_STREQ(db2, result[1].database_name.c_str()); + EXPECT_STREQ(origin_str2, result[2].origin_identifier.c_str()); + EXPECT_STREQ(db3, result[2].database_name.c_str()); +} diff --git a/chrome/browser/browsing_data_local_storage_helper.cc b/chrome/browser/browsing_data_local_storage_helper.cc index 091f9a0..4a9d517 100644 --- a/chrome/browser/browsing_data_local_storage_helper.cc +++ b/chrome/browser/browsing_data_local_storage_helper.cc @@ -5,8 +5,9 @@ #include "chrome/browser/browsing_data_local_storage_helper.h" #include "base/file_util.h" -#include "base/string_util.h" #include "base/message_loop.h" +#include "base/string_util.h" +#include "base/utf_string_conversions.h" #include "chrome/browser/chrome_thread.h" #include "chrome/browser/in_process_webkit/webkit_context.h" #include "chrome/browser/profile.h" @@ -115,3 +116,30 @@ void BrowsingDataLocalStorageHelper::DeleteLocalStorageFileInWebKitThread( profile_->GetWebKitContext()->dom_storage_context()->DeleteLocalStorageFile( file_path); } + +CannedBrowsingDataLocalStorageHelper::CannedBrowsingDataLocalStorageHelper( + Profile* profile) + : BrowsingDataLocalStorageHelper(profile) { +} + +void CannedBrowsingDataLocalStorageHelper::AddLocalStorage( + const GURL& origin) { + WebKit::WebSecurityOrigin web_security_origin = + WebKit::WebSecurityOrigin::createFromString( + UTF8ToUTF16(origin.spec())); + local_storage_info_.push_back(LocalStorageInfo( + web_security_origin.protocol().utf8(), + web_security_origin.host().utf8(), + web_security_origin.port(), + web_security_origin.databaseIdentifier().utf8(), + web_security_origin.toString().utf8(), + profile_->GetWebKitContext()->dom_storage_context()-> + GetLocalStorageFilePath(web_security_origin.databaseIdentifier()), + 0, + base::Time())); +} + +void CannedBrowsingDataLocalStorageHelper::StartFetching( + Callback1<const std::vector<LocalStorageInfo>& >::Type* callback) { + callback->Run(local_storage_info_); +} diff --git a/chrome/browser/browsing_data_local_storage_helper.h b/chrome/browser/browsing_data_local_storage_helper.h index 2022498..682d737 100644 --- a/chrome/browser/browsing_data_local_storage_helper.h +++ b/chrome/browser/browsing_data_local_storage_helper.h @@ -12,6 +12,7 @@ #include "base/scoped_ptr.h" #include "base/time.h" #include "chrome/common/url_constants.h" +#include "googleurl/src/gurl.h" class Profile; @@ -75,11 +76,16 @@ class BrowsingDataLocalStorageHelper // Requests a single local storage file to be deleted in the WEBKIT thread. virtual void DeleteLocalStorageFile(const FilePath& file_path); - private: + protected: friend class base::RefCountedThreadSafe<BrowsingDataLocalStorageHelper>; - friend class MockBrowsingDataLocalStorageHelper; virtual ~BrowsingDataLocalStorageHelper(); + Profile* profile_; + + // This only mutates in the WEBKIT thread. + std::vector<LocalStorageInfo> local_storage_info_; + + private: // Enumerates all local storage files in the WEBKIT thread. void FetchLocalStorageInfoInWebKitThread(); // Notifies the completion callback in the UI thread. @@ -87,7 +93,6 @@ class BrowsingDataLocalStorageHelper // Delete a single local storage file in the WEBKIT thread. void DeleteLocalStorageFileInWebKitThread(const FilePath& file_path); - Profile* profile_; // This only mutates on the UI thread. scoped_ptr<Callback1<const std::vector<LocalStorageInfo>& >::Type > completion_callback_; @@ -96,10 +101,31 @@ class BrowsingDataLocalStorageHelper // after we notified the callback in the UI thread. // This only mutates on the UI thread. bool is_fetching_; - // This only mutates in the WEBKIT thread. - std::vector<LocalStorageInfo> local_storage_info_; DISALLOW_COPY_AND_ASSIGN(BrowsingDataLocalStorageHelper); }; +// This class is a thin wrapper around BrowsingDataLocalStorageHelper that does +// not fetch its information from the local storage tracker, but gets them +// passed as a parameter during construction. +class CannedBrowsingDataLocalStorageHelper + : public BrowsingDataLocalStorageHelper { + public: + explicit CannedBrowsingDataLocalStorageHelper(Profile* profile); + + // Add a local storage to the set of canned local storages that is returned + // by this helper. + void AddLocalStorage(const GURL& origin); + + // BrowsingDataLocalStorageHelper methods. + virtual void StartFetching( + Callback1<const std::vector<LocalStorageInfo>& >::Type* callback); + virtual void CancelNotification() {} + + private: + virtual ~CannedBrowsingDataLocalStorageHelper() {} + + DISALLOW_COPY_AND_ASSIGN(CannedBrowsingDataLocalStorageHelper); +}; + #endif // CHROME_BROWSER_BROWSING_DATA_LOCAL_STORAGE_HELPER_H_ diff --git a/chrome/browser/browsing_data_local_storage_helper_unittest.cc b/chrome/browser/browsing_data_local_storage_helper_unittest.cc new file mode 100644 index 0000000..959cf5a --- /dev/null +++ b/chrome/browser/browsing_data_local_storage_helper_unittest.cc @@ -0,0 +1,66 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/browsing_data_local_storage_helper.h" + +#include "chrome/test/testing_profile.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { +class TestCompletionCallback + : public CallbackRunner<Tuple1<const std::vector< + BrowsingDataLocalStorageHelper::LocalStorageInfo>& > > { + public: + TestCompletionCallback() + : have_result_(false) { + } + + bool have_result() const { return have_result_; } + + const std::vector<BrowsingDataLocalStorageHelper::LocalStorageInfo>& result() + { + return result_; + } + + virtual void RunWithParams( + const Tuple1<const std::vector< + BrowsingDataLocalStorageHelper::LocalStorageInfo>& >& params) { + have_result_ = true; + result_ = params.a; + } + + private: + bool have_result_; + std::vector<BrowsingDataLocalStorageHelper::LocalStorageInfo> result_; + + DISALLOW_COPY_AND_ASSIGN(TestCompletionCallback); +}; +} // namespace + +TEST(CannedBrowsingDataLocalStorageTest, AddLocalStorage) { + TestingProfile profile; + + const GURL origin1("http://host1:1/"); + const GURL origin2("http://host2:1/"); + const FilePath::CharType file1[] = + FILE_PATH_LITERAL("http_host1_1.localstorage"); + const FilePath::CharType file2[] = + FILE_PATH_LITERAL("http_host2_1.localstorage"); + + scoped_refptr<CannedBrowsingDataLocalStorageHelper> helper = + new CannedBrowsingDataLocalStorageHelper(&profile); + helper->AddLocalStorage(origin1); + helper->AddLocalStorage(origin2); + + TestCompletionCallback callback; + helper->StartFetching(&callback); + ASSERT_TRUE(callback.have_result()); + + std::vector<BrowsingDataLocalStorageHelper::LocalStorageInfo> result = + callback.result(); + + ASSERT_EQ(2u, result.size()); + EXPECT_EQ(FilePath(file1).value(), result[0].file_path.BaseName().value()); + EXPECT_EQ(FilePath(file2).value(), result[1].file_path.BaseName().value()); +} diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index a214780..3239dd2 100755 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -591,6 +591,9 @@ 'browser/browser_theme_pack_unittest.cc', 'browser/browser_theme_provider_unittest.cc', 'browser/browser_unittest.cc', + 'browser/browsing_data_appcache_helper_unittest.cc', + 'browser/browsing_data_database_helper_unittest.cc', + 'browser/browsing_data_local_storage_helper_unittest.cc', 'browser/child_process_security_policy_unittest.cc', 'browser/chrome_browser_application_mac_unittest.mm', 'browser/chrome_plugin_unittest.cc', |