diff options
author | asargent@chromium.org <asargent@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-27 21:21:34 +0000 |
---|---|---|
committer | asargent@chromium.org <asargent@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-27 21:21:34 +0000 |
commit | 63c64d193c42c0c5dd819395994031a16c9a3d71 (patch) | |
tree | a772e9b87c869649ada5f5a47215b77c92b8dfdd /chrome | |
parent | 2b39687f1feb296be488a75ef5286a908be81e3a (diff) | |
download | chromium_src-63c64d193c42c0c5dd819395994031a16c9a3d71.zip chromium_src-63c64d193c42c0c5dd819395994031a16c9a3d71.tar.gz chromium_src-63c64d193c42c0c5dd819395994031a16c9a3d71.tar.bz2 |
Adding ExtensionPrefs methods for storing update-when-idle data.
This includes adding a few methods to ExtensionPrefs that I'm going to need to
use inside ExtensionUpdater for implementing the "do updates at idle" feature.
Instead of adding more call-through stubs to the interface that
ExtensionsService providers to the ExtensionUpdater, I instead decided it was
time to do some refactoring to expose a ExtensionPrefs getter in the interface.
TEST=(Should be covered by unit/browser tests)
BUG=37971
Review URL: http://codereview.chromium.org/1695018
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@45738 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/extensions/extension_prefs.cc | 106 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_prefs.h | 23 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_prefs_unittest.cc | 204 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_updater.cc | 32 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_updater.h | 3 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_updater_unittest.cc | 171 | ||||
-rw-r--r-- | chrome/browser/extensions/extensions_service.cc | 18 | ||||
-rw-r--r-- | chrome/browser/extensions/extensions_service.h | 16 | ||||
-rw-r--r-- | chrome/browser/extensions/test_extension_prefs.cc | 69 | ||||
-rw-r--r-- | chrome/browser/extensions/test_extension_prefs.h | 57 | ||||
-rw-r--r-- | chrome/chrome_tests.gypi | 2 |
11 files changed, 462 insertions, 239 deletions
diff --git a/chrome/browser/extensions/extension_prefs.cc b/chrome/browser/extensions/extension_prefs.cc index a1bbf9b..bfa5c7c 100644 --- a/chrome/browser/extensions/extension_prefs.cc +++ b/chrome/browser/extensions/extension_prefs.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// 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. @@ -54,6 +54,13 @@ const wchar_t kLastPingDay[] = L"lastpingday"; // Path for settings specific to blacklist update. const wchar_t kExtensionsBlacklistUpdate[] = L"extensions.blacklistupdate"; +// Path and sub-keys for the idle install info dictionary preference. +const wchar_t kIdleInstallInfo[] = L"idle_install_info"; +const wchar_t kIdleInstallInfoCrxPath[] = L"crx_path"; +const wchar_t kIdleInstallInfoVersion[] = L"version"; +const wchar_t kIdleInstallInfoFetchTime[] = L"fetch_time"; + + // A preference that, if true, will allow this extension to run in incognito // mode. const wchar_t kPrefIncognitoEnabled[] = L"incognito"; @@ -662,6 +669,103 @@ ExtensionInfo* ExtensionPrefs::GetInstalledExtensionInfo( return NULL; } +void ExtensionPrefs::SetIdleInstallInfo(const std::string& extension_id, + const FilePath& crx_path, + const std::string& version, + const base::Time& fetch_time) { + DictionaryValue* extension_prefs = GetExtensionPref(extension_id); + if (!extension_prefs) { + NOTREACHED(); + return; + } + extension_prefs->Remove(kIdleInstallInfo, NULL); + DictionaryValue* info = new DictionaryValue(); + info->SetString(kIdleInstallInfoCrxPath, crx_path.value()); + info->SetString(kIdleInstallInfoVersion, version); + info->SetString(kIdleInstallInfoFetchTime, + Int64ToString(fetch_time.ToInternalValue())); + extension_prefs->Set(kIdleInstallInfo, info); + prefs_->ScheduleSavePersistentPrefs(); +} + +bool ExtensionPrefs::RemoveIdleInstallInfo(const std::string& extension_id) { + DictionaryValue* extension_prefs = GetExtensionPref(extension_id); + if (!extension_prefs) + return false; + bool result = extension_prefs->Remove(kIdleInstallInfo, NULL); + prefs_->ScheduleSavePersistentPrefs(); + return result; +} + +bool ExtensionPrefs::GetIdleInstallInfo(const std::string& extension_id, + FilePath* crx_path, + std::string* version, + base::Time* fetch_time) { + DictionaryValue* extension_prefs = GetExtensionPref(extension_id); + if (!extension_prefs) + return false; + + // Do all the reads from the prefs together, and don't do any assignment + // to the out parameters unless all the reads succeed. + DictionaryValue* info = NULL; + if (!extension_prefs->GetDictionary(kIdleInstallInfo, &info)) + return false; + + FilePath::StringType path_string; + if (!info->GetString(kIdleInstallInfoCrxPath, &path_string)) + return false; + + std::string tmp_version; + if (!info->GetString(kIdleInstallInfoVersion, &tmp_version)) + return false; + + std::string fetch_time_string; + if (!info->GetString(kIdleInstallInfoFetchTime, &fetch_time_string)) + return false; + + int64 fetch_time_value; + if (!StringToInt64(fetch_time_string, &fetch_time_value)) + return false; + + if (crx_path) + *crx_path = FilePath(path_string); + + if (version) + *version = tmp_version; + + if (fetch_time) + *fetch_time = base::Time::FromInternalValue(fetch_time_value); + + return true; +} + +std::set<std::string> ExtensionPrefs::GetIdleInstallInfoIds() { + std::set<std::string> result; + + const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); + if (!extensions) + return result; + + for (DictionaryValue::key_iterator iter = extensions->begin_keys(); + iter != extensions->end_keys(); ++iter) { + std::string id = WideToASCII(*iter); + if (!Extension::IdIsValid(id)) { + NOTREACHED(); + continue; + } + + DictionaryValue* extension_prefs = GetExtensionPref(id); + if (!extension_prefs) + continue; + + DictionaryValue* info = NULL; + if (extension_prefs->GetDictionary(kIdleInstallInfo, &info)) + result.insert(id); + } + return result; +} + + // static void ExtensionPrefs::RegisterUserPrefs(PrefService* prefs) { prefs->RegisterDictionaryPref(kExtensionsPref); diff --git a/chrome/browser/extensions/extension_prefs.h b/chrome/browser/extensions/extension_prefs.h index 45d142e..deba0ee 100644 --- a/chrome/browser/extensions/extension_prefs.h +++ b/chrome/browser/extensions/extension_prefs.h @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// 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. @@ -115,6 +115,27 @@ class ExtensionPrefs { // extension is not present, NULL is returned. ExtensionInfo* GetInstalledExtensionInfo(const std::string& extension_id); + // We've downloaded an updated .crx file for the extension, but are waiting + // for idle time to install it. + void SetIdleInstallInfo(const std::string& extension_id, + const FilePath& crx_path, + const std::string& version, + const base::Time& fetch_time); + + // Removes any idle install information we have for the given |extension_id|. + // Returns true if there was info to remove; false otherwise. + bool RemoveIdleInstallInfo(const std::string& extension_id); + + // If we have idle install information for |extension_id|, this puts it into + // the out parameters and returns true. Otherwise returns false. + bool GetIdleInstallInfo(const std::string& extension_id, + FilePath* crx_path, + std::string* version, + base::Time* fetch_time); + + // Returns the extension id's that have idle install information. + std::set<std::string> GetIdleInstallInfoIds(); + static void RegisterUserPrefs(PrefService* prefs); // The underlying PrefService. diff --git a/chrome/browser/extensions/extension_prefs_unittest.cc b/chrome/browser/extensions/extension_prefs_unittest.cc index 40ff72a..15e0bc9 100644 --- a/chrome/browser/extensions/extension_prefs_unittest.cc +++ b/chrome/browser/extensions/extension_prefs_unittest.cc @@ -3,12 +3,15 @@ // found in the LICENSE file. #include "base/message_loop.h" +#include "base/path_service.h" #include "base/scoped_temp_dir.h" +#include "base/stl_util-inl.h" #include "base/string_util.h" #include "chrome/browser/chrome_thread.h" #include "chrome/browser/extensions/extension_prefs.h" -#include "chrome/browser/json_pref_store.h" +#include "chrome/browser/extensions/test_extension_prefs.h" #include "chrome/browser/pref_service.h" +#include "chrome/common/chrome_paths.h" #include "chrome/common/extensions/extension_constants.h" #include "testing/gtest/include/gtest/gtest.h" @@ -18,14 +21,7 @@ using base::TimeDelta; // Base class for tests. class ExtensionPrefsTest : public testing::Test { public: - ExtensionPrefsTest() { - EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); - FilePath preferences_file_ = temp_dir_.path().AppendASCII("Preferences"); - pref_service_.reset(new PrefService( - new JsonPrefStore(preferences_file_))); - ExtensionPrefs::RegisterUserPrefs(pref_service_.get()); - CreateExtensionPrefs(); - } + ExtensionPrefsTest() {} // This function will get called once, and is the right place to do operations // on ExtensionPrefs that write data. @@ -44,43 +40,14 @@ class ExtensionPrefsTest : public testing::Test { Verify(); // Reset ExtensionPrefs, and re-verify. - CreateExtensionPrefs(); + prefs_.RecreateExtensionPrefs(); Verify(); } protected: - // Creates an ExtensionPrefs object. - void CreateExtensionPrefs() { - prefs_.reset(new ExtensionPrefs(pref_service_.get(), temp_dir_.path())); - } - - // Creates a new Extension with the given name in our temp dir, adds it to - // our ExtensionPrefs, and returns it. - Extension* AddExtension(std::string name) { - FilePath path = temp_dir_.path().AppendASCII(name); - Extension* extension = new Extension(path); - std::string errors; - DictionaryValue dictionary; - dictionary.SetString(extension_manifest_keys::kName, name); - dictionary.SetString(extension_manifest_keys::kVersion, "0.1"); - EXPECT_TRUE(extension->InitFromValue(dictionary, false, &errors)); - extension->set_location(Extension::INTERNAL); - EXPECT_TRUE(Extension::IdIsValid(extension->id())); - prefs_->OnExtensionInstalled(extension); - return extension; - } - - // Creates an Extension and adds it to our ExtensionPrefs. Returns the - // extension id it was assigned. - std::string AddExtensionAndReturnId(std::string name) { - scoped_ptr<Extension> extension(AddExtension(name)); - return extension->id(); - } - - ScopedTempDir temp_dir_; - FilePath preferences_file_; - scoped_ptr<PrefService> pref_service_; - scoped_ptr<ExtensionPrefs> prefs_; + ExtensionPrefs* prefs() { return prefs_.prefs(); } + + TestExtensionPrefs prefs_; private: DISALLOW_COPY_AND_ASSIGN(ExtensionPrefsTest); @@ -94,17 +61,17 @@ class ExtensionPrefsLastPingDay : public ExtensionPrefsTest { blacklist_time_(Time::Now() - TimeDelta::FromHours(2)) {} virtual void Initialize() { - extension_id_ = AddExtensionAndReturnId("last_ping_day"); - EXPECT_TRUE(prefs_->LastPingDay(extension_id_).is_null()); - prefs_->SetLastPingDay(extension_id_, extension_time_); - prefs_->SetBlacklistLastPingDay(blacklist_time_); + extension_id_ = prefs_.AddExtensionAndReturnId("last_ping_day"); + EXPECT_TRUE(prefs()->LastPingDay(extension_id_).is_null()); + prefs()->SetLastPingDay(extension_id_, extension_time_); + prefs()->SetBlacklistLastPingDay(blacklist_time_); } virtual void Verify() { - Time result = prefs_->LastPingDay(extension_id_); + Time result = prefs()->LastPingDay(extension_id_); EXPECT_FALSE(result.is_null()); EXPECT_TRUE(result == extension_time_); - result = prefs_->BlacklistLastPingDay(); + result = prefs()->BlacklistLastPingDay(); EXPECT_FALSE(result.is_null()); EXPECT_TRUE(result == blacklist_time_); } @@ -121,16 +88,16 @@ TEST_F(ExtensionPrefsLastPingDay, LastPingDay) {} class ExtensionPrefsToolbarOrder : public ExtensionPrefsTest { public: virtual void Initialize() { - list_.push_back(AddExtensionAndReturnId("1")); - list_.push_back(AddExtensionAndReturnId("2")); - list_.push_back(AddExtensionAndReturnId("3")); - std::vector<std::string> before_list = prefs_->GetToolbarOrder(); + list_.push_back(prefs_.AddExtensionAndReturnId("1")); + list_.push_back(prefs_.AddExtensionAndReturnId("2")); + list_.push_back(prefs_.AddExtensionAndReturnId("3")); + std::vector<std::string> before_list = prefs()->GetToolbarOrder(); EXPECT_TRUE(before_list.empty()); - prefs_->SetToolbarOrder(list_); + prefs()->SetToolbarOrder(list_); } virtual void Verify() { - std::vector<std::string> result = prefs_->GetToolbarOrder(); + std::vector<std::string> result = prefs()->GetToolbarOrder(); ASSERT_EQ(list_.size(), result.size()); for (size_t i = 0; i < list_.size(); i++) { EXPECT_EQ(list_[i], result[i]); @@ -147,12 +114,12 @@ TEST_F(ExtensionPrefsToolbarOrder, ToolbarOrder) {} class ExtensionPrefsExtensionState : public ExtensionPrefsTest { public: virtual void Initialize() { - extension.reset(AddExtension("test")); - prefs_->SetExtensionState(extension.get(), Extension::DISABLED); + extension.reset(prefs_.AddExtension("test")); + prefs()->SetExtensionState(extension.get(), Extension::DISABLED); } virtual void Verify() { - EXPECT_EQ(Extension::DISABLED, prefs_->GetExtensionState(extension->id())); + EXPECT_EQ(Extension::DISABLED, prefs()->GetExtensionState(extension->id())); } private: @@ -164,12 +131,12 @@ TEST_F(ExtensionPrefsExtensionState, ExtensionState) {} class ExtensionPrefsEscalatePermissions : public ExtensionPrefsTest { public: virtual void Initialize() { - extension.reset(AddExtension("test")); - prefs_->SetDidExtensionEscalatePermissions(extension.get(), true); + extension.reset(prefs_.AddExtension("test")); + prefs()->SetDidExtensionEscalatePermissions(extension.get(), true); } virtual void Verify() { - EXPECT_EQ(true, prefs_->DidExtensionEscalatePermissions(extension->id())); + EXPECT_EQ(true, prefs()->DidExtensionEscalatePermissions(extension->id())); } private: @@ -182,13 +149,14 @@ TEST_F(ExtensionPrefsEscalatePermissions, EscalatePermissions) {} class ExtensionPrefsVersionString : public ExtensionPrefsTest { public: virtual void Initialize() { - extension.reset(AddExtension("test")); - EXPECT_EQ("0.1", prefs_->GetVersionString(extension->id())); - prefs_->OnExtensionUninstalled(extension->id(), Extension::INTERNAL, false); + extension.reset(prefs_.AddExtension("test")); + EXPECT_EQ("0.1", prefs()->GetVersionString(extension->id())); + prefs()->OnExtensionUninstalled(extension->id(), + Extension::INTERNAL, false); } virtual void Verify() { - EXPECT_EQ("", prefs_->GetVersionString(extension->id())); + EXPECT_EQ("", prefs()->GetVersionString(extension->id())); } private: @@ -205,36 +173,36 @@ class ExtensionPrefsBlacklist : public ExtensionPrefsTest { // Install 5 extensions. for (int i = 0; i < 5; i++) { std::string name = "test" + IntToString(i); - extensions_.push_back(linked_ptr<Extension>(AddExtension(name))); + extensions_.push_back(linked_ptr<Extension>(prefs_.AddExtension(name))); } - EXPECT_EQ(NULL, prefs_->GetInstalledExtensionInfo(not_installed_id_)); + EXPECT_EQ(NULL, prefs()->GetInstalledExtensionInfo(not_installed_id_)); std::vector<linked_ptr<Extension> >::const_iterator iter; for (iter = extensions_.begin(); iter != extensions_.end(); ++iter) { - EXPECT_FALSE(prefs_->IsExtensionBlacklisted((*iter)->id())); + EXPECT_FALSE(prefs()->IsExtensionBlacklisted((*iter)->id())); } // Blacklist one installed and one not-installed extension id. std::set<std::string> blacklisted_ids; blacklisted_ids.insert(extensions_[0]->id()); blacklisted_ids.insert(not_installed_id_); - prefs_->UpdateBlacklist(blacklisted_ids); + prefs()->UpdateBlacklist(blacklisted_ids); } virtual void Verify() { // Make sure the two id's we expect to be blacklisted are. - EXPECT_TRUE(prefs_->IsExtensionBlacklisted(extensions_[0]->id())); - EXPECT_TRUE(prefs_->IsExtensionBlacklisted(not_installed_id_)); + EXPECT_TRUE(prefs()->IsExtensionBlacklisted(extensions_[0]->id())); + EXPECT_TRUE(prefs()->IsExtensionBlacklisted(not_installed_id_)); // Make sure the other id's are not blacklisted. std::vector<linked_ptr<Extension> >::const_iterator iter; for (iter = extensions_.begin() + 1; iter != extensions_.end(); ++iter) { - EXPECT_FALSE(prefs_->IsExtensionBlacklisted((*iter)->id())); + EXPECT_FALSE(prefs()->IsExtensionBlacklisted((*iter)->id())); } // Make sure GetInstalledExtensionsInfo returns only the non-blacklisted // extensions data. scoped_ptr<ExtensionPrefs::ExtensionsInfo> info( - prefs_->GetInstalledExtensionsInfo()); + prefs()->GetInstalledExtensionsInfo()); EXPECT_EQ(4u, info->size()); ExtensionPrefs::ExtensionsInfo::iterator info_iter; for (info_iter = info->begin(); info_iter != info->end(); ++info_iter) { @@ -250,3 +218,95 @@ class ExtensionPrefsBlacklist : public ExtensionPrefsTest { std::string not_installed_id_; }; TEST_F(ExtensionPrefsBlacklist, Blacklist) {} + + +// Tests the idle install information functions. +class ExtensionPrefsIdleInstallInfo : public ExtensionPrefsTest { + public: + // Sets idle install information for one test extension. + void SetIdleInfo(std::string id, int num) { + prefs()->SetIdleInstallInfo(id, basedir_.AppendASCII(IntToString(num)), + "1." + IntToString(num), + now_ + TimeDelta::FromSeconds(num)); + } + + // Verifies that we get back expected idle install information previously + // set by SetIdleInfo. + void VerifyIdleInfo(std::string id, int num) { + FilePath crx_path; + std::string version; + base::Time fetch_time; + ASSERT_TRUE(prefs()->GetIdleInstallInfo(id, &crx_path, &version, + &fetch_time)); + ASSERT_EQ(crx_path.value(), + basedir_.AppendASCII(IntToString(num)).value()); + ASSERT_EQ("1." + IntToString(num), version); + ASSERT_TRUE(fetch_time == now_ + TimeDelta::FromSeconds(num)); + } + + virtual void Initialize() { + PathService::Get(chrome::DIR_TEST_DATA, &basedir_); + now_ = Time::Now(); + id1_ = prefs_.AddExtensionAndReturnId("1"); + id2_ = prefs_.AddExtensionAndReturnId("2"); + id3_ = prefs_.AddExtensionAndReturnId("3"); + id4_ = prefs_.AddExtensionAndReturnId("4"); + + // Set info for two extensions, then remove it. + SetIdleInfo(id1_, 1); + SetIdleInfo(id2_, 2); + VerifyIdleInfo(id1_, 1); + VerifyIdleInfo(id2_, 2); + std::set<std::string> ids = prefs()->GetIdleInstallInfoIds(); + EXPECT_EQ(2u, ids.size()); + EXPECT_TRUE(ContainsKey(ids, id1_)); + EXPECT_TRUE(ContainsKey(ids, id2_)); + prefs()->RemoveIdleInstallInfo(id1_); + prefs()->RemoveIdleInstallInfo(id2_); + ids = prefs()->GetIdleInstallInfoIds(); + EXPECT_TRUE(ids.empty()); + + // Try getting/removing info for an id that used to have info set. + EXPECT_FALSE(prefs()->GetIdleInstallInfo(id1_, NULL, NULL, NULL)); + EXPECT_FALSE(prefs()->RemoveIdleInstallInfo(id1_)); + + // Try getting/removing info for an id that has not yet had any info set. + EXPECT_FALSE(prefs()->GetIdleInstallInfo(id3_, NULL, NULL, NULL)); + EXPECT_FALSE(prefs()->RemoveIdleInstallInfo(id3_)); + + // Set info for 4 extensions, then remove for one of them. + SetIdleInfo(id1_, 1); + SetIdleInfo(id2_, 2); + SetIdleInfo(id3_, 3); + SetIdleInfo(id4_, 4); + VerifyIdleInfo(id1_, 1); + VerifyIdleInfo(id2_, 2); + VerifyIdleInfo(id3_, 3); + VerifyIdleInfo(id4_, 4); + prefs()->RemoveIdleInstallInfo(id3_); + } + + virtual void Verify() { + // Make sure the info for the 3 extensions we expect is present. + std::set<std::string> ids = prefs()->GetIdleInstallInfoIds(); + EXPECT_EQ(3u, ids.size()); + EXPECT_TRUE(ContainsKey(ids, id1_)); + EXPECT_TRUE(ContainsKey(ids, id2_)); + EXPECT_TRUE(ContainsKey(ids, id4_)); + VerifyIdleInfo(id1_, 1); + VerifyIdleInfo(id2_, 2); + VerifyIdleInfo(id4_, 4); + + // Make sure there isn't info the for the one extension id we removed. + EXPECT_FALSE(prefs()->GetIdleInstallInfo(id3_, NULL, NULL, NULL)); + } + + protected: + Time now_; + FilePath basedir_; + std::string id1_; + std::string id2_; + std::string id3_; + std::string id4_; +}; +TEST_F(ExtensionPrefsIdleInstallInfo, IdleInstallInfo) {} diff --git a/chrome/browser/extensions/extension_updater.cc b/chrome/browser/extensions/extension_updater.cc index eec96d9..3d9ecea 100644 --- a/chrome/browser/extensions/extension_updater.cc +++ b/chrome/browser/extensions/extension_updater.cc @@ -258,7 +258,8 @@ void ManifestFetchesBuilder::AddExtensionData( fetches_.find(update_url); // Find or create a ManifestFetchData to add this extension to. - int ping_days = CalculatePingDays(service_->LastPingDay(id)); + int ping_days = + CalculatePingDays(service_->extension_prefs()->LastPingDay(id)); while (existing_iter != fetches_.end()) { if (existing_iter->second->AddExtension(id, version.GetString(), ping_days)) { @@ -300,8 +301,7 @@ class ExtensionUpdaterFileHandler return; } - // The ExtensionUpdater is now responsible for cleaning up the temp file - // from disk. + // The ExtensionUpdater now owns the temp file. ChromeThread::PostTask( ChromeThread::UI, FROM_HERE, NewRunnableMethod( @@ -309,13 +309,6 @@ class ExtensionUpdaterFileHandler path, download_url)); } - void DeleteFile(const FilePath& path) { - DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); - if (!file_util::Delete(path, false)) { - LOG(WARNING) << "Failed to delete temp file " << path.value(); - } - } - private: friend class base::RefCountedThreadSafe<ExtensionUpdaterFileHandler>; @@ -554,9 +547,9 @@ void ExtensionUpdater::HandleManifestResults( bool did_ping = fetch_data.DidPing(*i); if (did_ping) { if (*i == kBlacklistAppID) { - service_->SetBlacklistLastPingDay(daystart); + service_->extension_prefs()->SetBlacklistLastPingDay(daystart); } else if (service_->GetExtensionById(*i, true) != NULL) { - service_->SetLastPingDay(*i, daystart); + service_->extension_prefs()->SetLastPingDay(*i, daystart); } } } @@ -592,7 +585,7 @@ void ExtensionUpdater::OnCRXFetchComplete(const GURL& url, int response_code, const std::string& data) { if (status.status() == URLRequestStatus::SUCCESS && - response_code == 200) { + response_code == 200) { if (current_extension_fetch_.id == kBlacklistAppID) { ProcessBlacklist(data); } else { @@ -626,17 +619,11 @@ void ExtensionUpdater::OnCRXFetchComplete(const GURL& url, void ExtensionUpdater::OnCRXFileWritten(const std::string& id, const FilePath& path, const GURL& download_url) { + // The ExtensionsService is now responsible for cleaning up the temp file + // at |path|. service_->UpdateExtension(id, path, download_url); } -void ExtensionUpdater::OnExtensionInstallFinished(const FilePath& path, - Extension* extension) { - // Have the file_handler_ delete the temp file on the file I/O thread. - ChromeThread::PostTask( - ChromeThread::FILE, FROM_HERE, - NewRunnableMethod( - file_handler_.get(), &ExtensionUpdaterFileHandler::DeleteFile, path)); -} void ExtensionUpdater::ScheduleNextCheck(const TimeDelta& target_delay) { DCHECK(!timer_.IsRunning()); @@ -706,7 +693,8 @@ void ExtensionUpdater::CheckNow() { ManifestFetchData* blacklist_fetch = new ManifestFetchData(GURL(kBlacklistUpdateUrl)); std::wstring version = prefs_->GetString(kExtensionBlacklistUpdateVersion); - int ping_days = CalculatePingDays(service_->BlacklistLastPingDay()); + int ping_days = + CalculatePingDays(service_->extension_prefs()->BlacklistLastPingDay()); blacklist_fetch->AddExtension(kBlacklistAppID, WideToASCII(version), ping_days); StartUpdateCheck(blacklist_fetch); diff --git a/chrome/browser/extensions/extension_updater.h b/chrome/browser/extensions/extension_updater.h index eee0269..c1f036f 100644 --- a/chrome/browser/extensions/extension_updater.h +++ b/chrome/browser/extensions/extension_updater.h @@ -220,9 +220,6 @@ class ExtensionUpdater void OnCRXFileWritten(const std::string& id, const FilePath& path, const GURL& download_url); - // Callback for when ExtensionsService::Install is finished. - void OnExtensionInstallFinished(const FilePath& path, Extension* extension); - // Verifies downloaded blacklist. Based on the blacklist, calls extension // service to unload blacklisted extensions and update pref. void ProcessBlacklist(const std::string& data); diff --git a/chrome/browser/extensions/extension_updater_unittest.cc b/chrome/browser/extensions/extension_updater_unittest.cc index c909a66..9c54d7a 100644 --- a/chrome/browser/extensions/extension_updater_unittest.cc +++ b/chrome/browser/extensions/extension_updater_unittest.cc @@ -14,7 +14,7 @@ #include "chrome/browser/chrome_thread.h" #include "chrome/browser/extensions/extension_updater.h" #include "chrome/browser/extensions/extensions_service.h" -#include "chrome/browser/json_pref_store.h" +#include "chrome/browser/extensions/test_extension_prefs.h" #include "chrome/browser/net/test_url_fetcher_factory.h" #include "chrome/browser/pref_service.h" #include "chrome/common/extensions/extension.h" @@ -42,7 +42,7 @@ using base::TimeDelta; static int expected_load_flags = net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES; -// Do-nothing base class for further specialized test classes. +// Base class for further specialized test classes. class MockService : public ExtensionUpdateService { public: MockService() {} @@ -79,85 +79,42 @@ class MockService : public ExtensionUpdateService { return false; } - virtual void SetLastPingDay(const std::string& extension_id, - const Time& time) { - last_ping_days_[extension_id] = time; - } - - virtual Time LastPingDay(const std::string& extension_id) const { - std::map<std::string, Time>::const_iterator i = - last_ping_days_.find(extension_id); - if (i != last_ping_days_.end()) - return i->second; - - return Time(); - } - - virtual void SetBlacklistLastPingDay(const Time& time) { - blacklist_last_ping_day_ = time; - } - - virtual Time BlacklistLastPingDay() const { - return blacklist_last_ping_day_; + virtual ExtensionPrefs* extension_prefs() { return prefs_.prefs(); } + + PrefService* pref_service() { return prefs_.pref_service(); } + + // Creates test extensions and inserts them into list. The name and + // version are all based on their index. If |update_url| is non-null, it + // will be used as the update_url for each extension. + void CreateTestExtensions(int count, ExtensionList *list, + const std::string* update_url) { + for (int i = 1; i <= count; i++) { + DictionaryValue manifest; + manifest.SetString(extension_manifest_keys::kVersion, + StringPrintf("%d.0.0.0", i)); + manifest.SetString(extension_manifest_keys::kName, + StringPrintf("Extension %d", i)); + if (update_url) + manifest.SetString(extension_manifest_keys::kUpdateURL, *update_url); + Extension* e = prefs_.AddExtensionWithManifest(manifest); + ASSERT_TRUE(e != NULL); + list->push_back(e); + } } protected: PendingExtensionMap pending_extensions_; + TestExtensionPrefs prefs_; private: - std::map<std::string, Time> last_ping_days_; - Time blacklist_last_ping_day_; DISALLOW_COPY_AND_ASSIGN(MockService); }; -// Class that contains a PrefService and handles cleanup of a temporary file -// backing it. -class ScopedTempPrefService { - public: - ScopedTempPrefService() { - // Make sure different tests won't use the same prefs file. It will cause - // problem when different tests are running in parallel. - temp_dir_.CreateUniqueTempDir(); - FilePath pref_file = temp_dir_.path().AppendASCII("prefs"); - prefs_.reset(new PrefService(new JsonPrefStore(pref_file))); - } - - ~ScopedTempPrefService() {} - PrefService* get() { - return prefs_.get(); - } - - private: - // Ordering matters, we want |prefs_| to be destroyed before |temp_dir_|. - ScopedTempDir temp_dir_; - scoped_ptr<PrefService> prefs_; -}; - -// Creates test extensions and inserts them into list. The name and -// version are all based on their index. If |update_url| is non-null, it -// will be used as the update_url for each extension. -void CreateTestExtensions(int count, ExtensionList *list, - const std::string* update_url) { - for (int i = 1; i <= count; i++) { - DictionaryValue input; -#if defined(OS_WIN) - FilePath path(StringPrintf(L"c:\\extension%i", i)); -#else - FilePath path(StringPrintf("/extension%i", i)); -#endif - Extension* e = new Extension(path); - e->set_location(Extension::INTERNAL); - input.SetString(extension_manifest_keys::kVersion, - StringPrintf("%d.0.0.0", i)); - input.SetString(extension_manifest_keys::kName, - StringPrintf("Extension %d", i)); - if (update_url) - input.SetString(extension_manifest_keys::kUpdateURL, *update_url); - std::string error; - EXPECT_TRUE(e->InitFromValue(input, false, &error)); - list->push_back(e); - } +std::string GenerateId(std::string input) { + std::string result; + EXPECT_TRUE(Extension::GenerateId(input, &result)); + return result; } // Creates test pending extensions and inserts them into list. The @@ -170,7 +127,8 @@ void CreateTestPendingExtensions(int count, const GURL& update_url, scoped_ptr<Version> version( Version::GetVersionFromString(StringPrintf("%d.0.0.0", i))); ASSERT_TRUE(version.get()); - (*pending_extensions)[StringPrintf("extension%i", i)] = + std::string id = GenerateId(StringPrintf("extension%i", i)); + (*pending_extensions)[id] = PendingExtensionInfo(update_url, *version, is_theme, kInstallSilently); } @@ -336,7 +294,7 @@ class ExtensionUpdaterTest : public testing::Test { CreateTestPendingExtensions(1, GURL(update_url), &pending_extensions); service.set_pending_extensions(pending_extensions); } else { - CreateTestExtensions(1, &extensions, &update_url); + service.CreateTestExtensions(1, &extensions, &update_url); service.set_extensions(extensions); } @@ -347,9 +305,8 @@ class ExtensionUpdaterTest : public testing::Test { TestURLFetcherFactory factory; URLFetcher::set_factory(&factory); - ScopedTempPrefService prefs; scoped_refptr<ExtensionUpdater> updater = - new ExtensionUpdater(&service, prefs.get(), 60*60*24); + new ExtensionUpdater(&service, service.pref_service(), 60*60*24); updater->Start(); // Tell the update that it's time to do update checks. @@ -400,9 +357,8 @@ class ExtensionUpdaterTest : public testing::Test { TestURLFetcherFactory factory; URLFetcher::set_factory(&factory); - ScopedTempPrefService prefs; scoped_refptr<ExtensionUpdater> updater = - new ExtensionUpdater(&service, prefs.get(), 60*60*24); + new ExtensionUpdater(&service, service.pref_service(), 60*60*24); updater->Start(); // Tell the updater that it's time to do update checks. @@ -449,13 +405,13 @@ class ExtensionUpdaterTest : public testing::Test { // Create a set of test extensions ServiceForManifestTests service; ExtensionList tmp; - CreateTestExtensions(3, &tmp, NULL); + service.CreateTestExtensions(3, &tmp, NULL); service.set_extensions(tmp); MessageLoop message_loop; - ScopedTempPrefService prefs; scoped_refptr<ExtensionUpdater> updater = - new ExtensionUpdater(&service, prefs.get(), kUpdateFrequencySecs); + new ExtensionUpdater(&service, service.pref_service(), + kUpdateFrequencySecs); // Check passing an empty list of parse results to DetermineUpdates ManifestFetchData fetch_data(GURL("http://localhost/foo")); @@ -491,9 +447,9 @@ class ExtensionUpdaterTest : public testing::Test { service.set_pending_extensions(pending_extensions); MessageLoop message_loop; - ScopedTempPrefService prefs; scoped_refptr<ExtensionUpdater> updater = - new ExtensionUpdater(&service, prefs.get(), kUpdateFrequencySecs); + new ExtensionUpdater(&service, service.pref_service(), + kUpdateFrequencySecs); ManifestFetchData fetch_data(GURL("http://localhost/foo")); UpdateManifest::Results updates; @@ -526,9 +482,9 @@ class ExtensionUpdaterTest : public testing::Test { TestURLFetcher* fetcher = NULL; URLFetcher::set_factory(&factory); ServiceForDownloadTests service; - ScopedTempPrefService prefs; scoped_refptr<ExtensionUpdater> updater = - new ExtensionUpdater(&service, prefs.get(), kUpdateFrequencySecs); + new ExtensionUpdater(&service, service.pref_service(), + kUpdateFrequencySecs); GURL url1("http://localhost/manifest1"); GURL url2("http://localhost/manifest2"); @@ -590,9 +546,9 @@ class ExtensionUpdaterTest : public testing::Test { TestURLFetcher* fetcher = NULL; URLFetcher::set_factory(&factory); ServiceForDownloadTests service; - ScopedTempPrefService prefs; scoped_refptr<ExtensionUpdater> updater = - new ExtensionUpdater(&service, prefs.get(), kUpdateFrequencySecs); + new ExtensionUpdater(&service, service.pref_service(), + kUpdateFrequencySecs); GURL test_url("http://localhost/extension.crx"); @@ -648,10 +604,10 @@ class ExtensionUpdaterTest : public testing::Test { TestURLFetcher* fetcher = NULL; URLFetcher::set_factory(&factory); ServiceForBlacklistTests service; - ScopedTempPrefService prefs; scoped_refptr<ExtensionUpdater> updater = - new ExtensionUpdater(&service, prefs.get(), kUpdateFrequencySecs); - prefs.get()-> + new ExtensionUpdater(&service, service.pref_service(), + kUpdateFrequencySecs); + service.pref_service()-> RegisterStringPref(prefs::kExtensionBlacklistUpdateVersion, L"0"); GURL test_url("http://localhost/extension.crx"); @@ -661,7 +617,6 @@ class ExtensionUpdaterTest : public testing::Test { "2CE109E9D0FAF820B2434E166297934E6177B65AB9951DBC3E204CAD4689B39C"; std::string version = "0.0.1"; - updater->FetchUpdatedExtension(id, test_url, hash, version); // Call back the ExtensionUpdater with a 200 response and some test data @@ -679,7 +634,7 @@ class ExtensionUpdaterTest : public testing::Test { // blacklist. EXPECT_TRUE(service.processed_blacklist()); - EXPECT_EQ(version, WideToASCII(prefs.get()-> + EXPECT_EQ(version, WideToASCII(service.pref_service()-> GetString(prefs::kExtensionBlacklistUpdateVersion))); URLFetcher::set_factory(NULL); @@ -696,9 +651,9 @@ class ExtensionUpdaterTest : public testing::Test { TestURLFetcher* fetcher = NULL; URLFetcher::set_factory(&factory); ServiceForDownloadTests service; - ScopedTempPrefService prefs; scoped_refptr<ExtensionUpdater> updater = - new ExtensionUpdater(&service, prefs.get(), kUpdateFrequencySecs); + new ExtensionUpdater(&service, service.pref_service(), + kUpdateFrequencySecs); GURL url1("http://localhost/extension1.crx"); GURL url2("http://localhost/extension2.crx"); @@ -765,24 +720,25 @@ class ExtensionUpdaterTest : public testing::Test { ExtensionList tmp; GURL url1("http://clients2.google.com/service/update2/crx"); GURL url2("http://www.somewebsite.com"); - CreateTestExtensions(1, &tmp, &url1.possibly_invalid_spec()); - CreateTestExtensions(1, &tmp, &url2.possibly_invalid_spec()); + service.CreateTestExtensions(1, &tmp, &url1.possibly_invalid_spec()); + service.CreateTestExtensions(1, &tmp, &url2.possibly_invalid_spec()); EXPECT_EQ(2u, tmp.size()); service.set_extensions(tmp); Time now = Time::Now(); if (ping_days == 0) { - service.SetLastPingDay(tmp[0]->id(), now - TimeDelta::FromSeconds(15)); + service.extension_prefs()->SetLastPingDay( + tmp[0]->id(), now - TimeDelta::FromSeconds(15)); } else if (ping_days > 0) { Time last_ping_day = now - TimeDelta::FromDays(ping_days) - TimeDelta::FromSeconds(15); - service.SetLastPingDay(tmp[0]->id(), last_ping_day); + service.extension_prefs()->SetLastPingDay(tmp[0]->id(), last_ping_day); } MessageLoop message_loop; - ScopedTempPrefService prefs; scoped_refptr<ExtensionUpdater> updater = - new ExtensionUpdater(&service, prefs.get(), kUpdateFrequencySecs); + new ExtensionUpdater(&service, service.pref_service(), + kUpdateFrequencySecs); updater->set_blacklist_checks_enabled(false); // Make the updater do manifest fetching, and note the urls it tries to @@ -834,13 +790,13 @@ class ExtensionUpdaterTest : public testing::Test { // >= 1 day for the extension. static void TestHandleManifestResults() { ServiceForManifestTests service; - ScopedTempPrefService prefs; scoped_refptr<ExtensionUpdater> updater = - new ExtensionUpdater(&service, prefs.get(), kUpdateFrequencySecs); + new ExtensionUpdater(&service, service.pref_service(), + kUpdateFrequencySecs); GURL update_url("http://www.google.com/manifest"); ExtensionList tmp; - CreateTestExtensions(1, &tmp, &update_url.spec()); + service.CreateTestExtensions(1, &tmp, &update_url.spec()); service.set_extensions(tmp); ManifestFetchData fetch_data(update_url); @@ -851,7 +807,8 @@ class ExtensionUpdaterTest : public testing::Test { results.daystart_elapsed_seconds = 750; updater->HandleManifestResults(fetch_data, results); - Time last_ping_day = service.LastPingDay(extension->id()); + Time last_ping_day = + service.extension_prefs()->LastPingDay(extension->id()); EXPECT_FALSE(last_ping_day.is_null()); int64 seconds_diff = (Time::Now() - last_ping_day).InSeconds(); EXPECT_LT(seconds_diff - results.daystart_elapsed_seconds, 5); @@ -924,7 +881,7 @@ TEST(ExtensionUpdaterTest, TestManifestFetchesBuilderAddExtension) { // Non-internal non-external extensions should be rejected. { ExtensionList extensions; - CreateTestExtensions(1, &extensions, NULL); + service.CreateTestExtensions(1, &extensions, NULL); ASSERT_FALSE(extensions.empty()); extensions[0]->set_location(Extension::INVALID); builder.AddExtension(*extensions[0]); @@ -937,8 +894,8 @@ TEST(ExtensionUpdaterTest, TestManifestFetchesBuilderAddExtension) { // Extensions with invalid update URLs should be rejected. builder.AddPendingExtension( - "id", PendingExtensionInfo(GURL("http:google.com:foo"), - *version, false, false)); + GenerateId("foo"), PendingExtensionInfo(GURL("http:google.com:foo"), + *version, false, false)); EXPECT_TRUE(builder.GetFetches().empty()); // Extensions with empty IDs should be rejected. @@ -952,7 +909,7 @@ TEST(ExtensionUpdaterTest, TestManifestFetchesBuilderAddExtension) { // Extensions with empty update URLs should have a default one // filled in. builder.AddPendingExtension( - "id", PendingExtensionInfo(GURL(), *version, false, false)); + GenerateId("foo"), PendingExtensionInfo(GURL(), *version, false, false)); std::vector<ManifestFetchData*> fetches = builder.GetFetches(); ASSERT_EQ(1u, fetches.size()); scoped_ptr<ManifestFetchData> fetch(fetches[0]); diff --git a/chrome/browser/extensions/extensions_service.cc b/chrome/browser/extensions/extensions_service.cc index e9f6759..42c41ca 100644 --- a/chrome/browser/extensions/extensions_service.cc +++ b/chrome/browser/extensions/extensions_service.cc @@ -666,24 +666,6 @@ void ExtensionsService::UpdateExtensionBlacklist( } } -void ExtensionsService::SetLastPingDay(const std::string& extension_id, - const base::Time& time) { - extension_prefs_->SetLastPingDay(extension_id, time); -} - -base::Time ExtensionsService::LastPingDay( - const std::string& extension_id) const { - return extension_prefs_->LastPingDay(extension_id); -} - -void ExtensionsService::SetBlacklistLastPingDay(const base::Time& time) { - extension_prefs_->SetBlacklistLastPingDay(time); -} - -base::Time ExtensionsService::BlacklistLastPingDay() const { - return extension_prefs_->BlacklistLastPingDay(); -} - bool ExtensionsService::IsIncognitoEnabled(const Extension* extension) { // If this is a component extension we always allow it to work in incognito // mode. diff --git a/chrome/browser/extensions/extensions_service.h b/chrome/browser/extensions/extensions_service.h index e89547c..b443446 100644 --- a/chrome/browser/extensions/extensions_service.h +++ b/chrome/browser/extensions/extensions_service.h @@ -79,15 +79,7 @@ class ExtensionUpdateService { const std::vector<std::string>& blacklist) = 0; virtual bool HasInstalledExtensions() = 0; - // These set/get a server-provided time representing the start of the last day - // that we sent the 'ping' parameter during an update check. - virtual void SetLastPingDay(const std::string& extension_id, - const base::Time& time) = 0; - virtual base::Time LastPingDay(const std::string& extension_id) const = 0; - - // Similar to the 2 above, but for the extensions blacklist. - virtual void SetBlacklistLastPingDay(const base::Time& time) = 0; - virtual base::Time BlacklistLastPingDay() const = 0; + virtual ExtensionPrefs* extension_prefs() = 0; }; // Manages installed and running Chromium extensions. @@ -159,12 +151,6 @@ class ExtensionsService return !(extensions_.empty() && disabled_extensions_.empty()); } - virtual void SetLastPingDay(const std::string& extension_id, - const base::Time& time); - virtual base::Time LastPingDay(const std::string& extension_id) const; - virtual void SetBlacklistLastPingDay(const base::Time& time); - virtual base::Time BlacklistLastPingDay() const; - // Whether this extension can run in an incognito window. bool IsIncognitoEnabled(const Extension* extension); void SetIsIncognitoEnabled(Extension* extension, bool enabled); diff --git a/chrome/browser/extensions/test_extension_prefs.cc b/chrome/browser/extensions/test_extension_prefs.cc new file mode 100644 index 0000000..e9a1472 --- /dev/null +++ b/chrome/browser/extensions/test_extension_prefs.cc @@ -0,0 +1,69 @@ +// 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/extensions/test_extension_prefs.h" + +#include "base/file_util.h" +#include "base/logging.h" +#include "base/message_loop.h" +#include "chrome/browser/chrome_thread.h" +#include "chrome/browser/extensions/extension_prefs.h" +#include "chrome/browser/json_pref_store.h" +#include "chrome/browser/pref_service.h" +#include "chrome/common/extensions/extension.h" +#include "chrome/common/extensions/extension_constants.h" +#include "testing/gtest/include/gtest/gtest.h" + +TestExtensionPrefs::TestExtensionPrefs() { + EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); + preferences_file_ = temp_dir_.path().AppendASCII("Preferences"); + extensions_dir_ = temp_dir_.path().AppendASCII("Extensions"); + EXPECT_TRUE(file_util::CreateDirectory(extensions_dir_)); + + RecreateExtensionPrefs(); +} + +TestExtensionPrefs::~TestExtensionPrefs() {} + +void TestExtensionPrefs::RecreateExtensionPrefs() { + if (pref_service_.get()) { + // The PrefService writes its persistent file on the file thread, so we + // need to wait for any pending I/O to complete before creating a new + // PrefService. + MessageLoop file_loop; + ChromeThread file_thread(ChromeThread::FILE, &file_loop); + pref_service_->SavePersistentPrefs(); + file_loop.RunAllPending(); + } + + pref_service_.reset(new PrefService(new JsonPrefStore(preferences_file_))); + ExtensionPrefs::RegisterUserPrefs(pref_service_.get()); + prefs_.reset(new ExtensionPrefs(pref_service_.get(), temp_dir_.path())); +} + +Extension* TestExtensionPrefs::AddExtension(std::string name) { + DictionaryValue dictionary; + dictionary.SetString(extension_manifest_keys::kName, name); + dictionary.SetString(extension_manifest_keys::kVersion, "0.1"); + return AddExtensionWithManifest(dictionary); +} + +Extension* TestExtensionPrefs::AddExtensionWithManifest( + const DictionaryValue& manifest) { + std::string name; + EXPECT_TRUE(manifest.GetString(extension_manifest_keys::kName, &name)); + FilePath path = extensions_dir_.AppendASCII(name); + Extension* extension = new Extension(path); + std::string errors; + EXPECT_TRUE(extension->InitFromValue(manifest, false, &errors)); + extension->set_location(Extension::INTERNAL); + EXPECT_TRUE(Extension::IdIsValid(extension->id())); + prefs_->OnExtensionInstalled(extension); + return extension; +} + +std::string TestExtensionPrefs::AddExtensionAndReturnId(std::string name) { + scoped_ptr<Extension> extension(AddExtension(name)); + return extension->id(); +} diff --git a/chrome/browser/extensions/test_extension_prefs.h b/chrome/browser/extensions/test_extension_prefs.h new file mode 100644 index 0000000..0ad84c1 --- /dev/null +++ b/chrome/browser/extensions/test_extension_prefs.h @@ -0,0 +1,57 @@ +// 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. + +#ifndef CHROME_BROWSER_EXTENSIONS_TEST_EXTENSION_PREFS_H_ +#define CHROME_BROWSER_EXTENSIONS_TEST_EXTENSION_PREFS_H_ + +#include <string> + +#include "base/scoped_ptr.h" +#include "base/scoped_temp_dir.h" + +class DictionaryValue; +class Extension; +class ExtensionPrefs; +class PrefService; + + +// This is a test class intended to make it easier to work with ExtensionPrefs +// in tests. +class TestExtensionPrefs { + public: + TestExtensionPrefs(); + virtual ~TestExtensionPrefs(); + + ExtensionPrefs* prefs() { return prefs_.get(); } + PrefService* pref_service() { return pref_service_.get(); } + const FilePath& temp_dir() const { return temp_dir_.path(); } + + // This will cause the ExtensionPrefs to be deleted and recreated, based on + // any existing backing file we had previously created. + void RecreateExtensionPrefs(); + + // Creates a new Extension with the given name in our temp dir, adds it to + // our ExtensionPrefs, and returns it. + Extension* AddExtension(std::string name); + + // Similar to AddExtension, but takes a dictionary with manifest values. + Extension* AddExtensionWithManifest(const DictionaryValue& manifest); + + // Similar to AddExtension, this adds a new test Extension. This is useful for + // cases when you don't need the Extension object, but just the id it was + // assigned. + std::string AddExtensionAndReturnId(std::string name); + + protected: + ScopedTempDir temp_dir_; + FilePath preferences_file_; + FilePath extensions_dir_; + scoped_ptr<PrefService> pref_service_; + scoped_ptr<ExtensionPrefs> prefs_; + + private: + DISALLOW_COPY_AND_ASSIGN(TestExtensionPrefs); +}; + +#endif // CHROME_BROWSER_EXTENSIONS_TEST_EXTENSION_PREFS_H_ diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index 32b6bc7..3959643 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -70,6 +70,8 @@ 'browser/cocoa/browser_test_helper.h', 'browser/dummy_pref_store.cc', 'browser/dummy_pref_store.h', + 'browser/extensions/test_extension_prefs.cc', + 'browser/extensions/test_extension_prefs.h', 'browser/geolocation/mock_location_provider.cc', 'browser/geolocation/mock_location_provider.h', 'browser/mock_browsing_data_appcache_helper.cc', |