summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorivankr@chromium.org <ivankr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-05-28 18:08:08 +0000
committerivankr@chromium.org <ivankr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-05-28 18:08:08 +0000
commit35a6fd1817d0c50fa40545c45711623627133954 (patch)
tree4f93dab72015393442f24035ef49eab522feee43
parentb07ad6c679a2a9adee38fa6225ac33f94a1c266b (diff)
downloadchromium_src-35a6fd1817d0c50fa40545c45711623627133954.zip
chromium_src-35a6fd1817d0c50fa40545c45711623627133954.tar.gz
chromium_src-35a6fd1817d0c50fa40545c45711623627133954.tar.bz2
[protector] Specific handling of default values of protected prefs.
BUG=124194 TEST=ProtectedPrefsWatcherTest.* Review URL: https://chromiumcodereview.appspot.com/10399125 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@139252 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/prefs/pref_service.cc18
-rw-r--r--chrome/browser/prefs/pref_service.h4
-rw-r--r--chrome/browser/prefs/session_startup_pref.cc12
-rw-r--r--chrome/browser/protector/prefs_backup_invalid_change.cc7
-rw-r--r--chrome/browser/protector/prefs_backup_invalid_change_unittest.cc7
-rw-r--r--chrome/browser/protector/protected_prefs_watcher.cc154
-rw-r--r--chrome/browser/protector/protected_prefs_watcher.h6
-rw-r--r--chrome/browser/protector/protected_prefs_watcher_unittest.cc81
8 files changed, 208 insertions, 81 deletions
diff --git a/chrome/browser/prefs/pref_service.cc b/chrome/browser/prefs/pref_service.cc
index ea1d705..6427c0e 100644
--- a/chrome/browser/prefs/pref_service.cc
+++ b/chrome/browser/prefs/pref_service.cc
@@ -696,10 +696,8 @@ const base::Value* PrefService::GetUserPrefValue(const char* path) const {
// Look for an existing preference in the user store. If it doesn't
// exist, return NULL.
base::Value* value = NULL;
- if (user_pref_store_->GetMutableValue(path, &value) !=
- PersistentPrefStore::READ_OK) {
+ if (user_pref_store_->GetMutableValue(path, &value) != PrefStore::READ_OK)
return NULL;
- }
if (!value->IsType(pref->GetType())) {
NOTREACHED() << "Pref value type doesn't match registered type.";
@@ -709,6 +707,17 @@ const base::Value* PrefService::GetUserPrefValue(const char* path) const {
return value;
}
+const base::Value* PrefService::GetDefaultPrefValue(const char* path) const {
+ DCHECK(CalledOnValidThread());
+ // Lookup the preference in the default store.
+ const base::Value* value = NULL;
+ if (default_store_->GetValue(path, &value) != PrefStore::READ_OK) {
+ NOTREACHED() << "Default value missing for pref: " << path;
+ return NULL;
+ }
+ return value;
+}
+
const ListValue* PrefService::GetList(const char* path) const {
DCHECK(CalledOnValidThread());
@@ -870,8 +879,7 @@ Value* PrefService::GetMutableUserPref(const char* path,
// Look for an existing preference in the user store. If it doesn't
// exist or isn't the correct type, create a new user preference.
Value* value = NULL;
- if (user_pref_store_->GetMutableValue(path, &value)
- != PersistentPrefStore::READ_OK ||
+ if (user_pref_store_->GetMutableValue(path, &value) != PrefStore::READ_OK ||
!value->IsType(type)) {
if (type == Value::TYPE_DICTIONARY) {
value = new DictionaryValue;
diff --git a/chrome/browser/prefs/pref_service.h b/chrome/browser/prefs/pref_service.h
index 5502cb3..a486917 100644
--- a/chrome/browser/prefs/pref_service.h
+++ b/chrome/browser/prefs/pref_service.h
@@ -271,6 +271,10 @@ class PrefService : public base::NonThreadSafe {
// the preference is not set in the user pref store, returns NULL.
const base::Value* GetUserPrefValue(const char* path) const;
+ // Returns the default value of the given preference. |path| must point to a
+ // registered preference. In that case, will never return NULL.
+ const base::Value* GetDefaultPrefValue(const char* path) const;
+
// Removes a user pref and restores the pref to its default value.
void ClearPref(const char* path);
diff --git a/chrome/browser/prefs/session_startup_pref.cc b/chrome/browser/prefs/session_startup_pref.cc
index 6ca978e..477c73e 100644
--- a/chrome/browser/prefs/session_startup_pref.cc
+++ b/chrome/browser/prefs/session_startup_pref.cc
@@ -224,16 +224,8 @@ SessionStartupPref::Type SessionStartupPref::PrefValueToType(int pref_value) {
bool SessionStartupPref::DidStartupPrefChange(Profile* profile) {
ProtectedPrefsWatcher* prefs_watcher =
ProtectorServiceFactory::GetForProfile(profile)->GetPrefsWatcher();
- if (prefs_watcher->DidPrefChange(prefs::kRestoreOnStartup))
- return true;
-#if defined(OS_MACOSX)
- // On Mac OS, default value for |kRestoreOnStartup| depends on system
- // settings and may be different from one run to another.
- PrefService* prefs = profile->GetPrefs();
- if (prefs->FindPreference(prefs::kRestoreOnStartup)->IsDefaultValue())
- return false;
-#endif
- return prefs_watcher->DidPrefChange(prefs::kURLsToRestoreOnStartup);
+ return prefs_watcher->DidPrefChange(prefs::kRestoreOnStartup) ||
+ prefs_watcher->DidPrefChange(prefs::kURLsToRestoreOnStartup);
}
// static
diff --git a/chrome/browser/protector/prefs_backup_invalid_change.cc b/chrome/browser/protector/prefs_backup_invalid_change.cc
index a30ba4c..e29edf7 100644
--- a/chrome/browser/protector/prefs_backup_invalid_change.cc
+++ b/chrome/browser/protector/prefs_backup_invalid_change.cc
@@ -7,6 +7,7 @@
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/utf_string_conversions.h"
+#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/prefs/session_startup_pref.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/protector/base_prefs_change.h"
@@ -131,10 +132,12 @@ void PrefsBackupInvalidChange::ApplyDefaults(Profile* profile) {
if (startup_pref.type != SessionStartupPref::LAST) {
// If startup type is LAST, resetting it is dangerous (the whole previous
// session will be lost).
- startup_pref.type = SessionStartupPref::GetDefaultStartupType();
- SessionStartupPref::SetStartupPref(prefs, startup_pref);
+ prefs->ClearPref(prefs::kRestoreOnStartup);
startup_pref_reset_ = true;
}
+ prefs->ClearPref(prefs::kHomePageIsNewTabPage);
+ prefs->ClearPref(prefs::kHomePage);
+ prefs->ClearPref(prefs::kShowHomeButton);
}
bool PrefsBackupInvalidChange::CanBeMerged() const {
diff --git a/chrome/browser/protector/prefs_backup_invalid_change_unittest.cc b/chrome/browser/protector/prefs_backup_invalid_change_unittest.cc
index 2ac7e5d..ba56e90 100644
--- a/chrome/browser/protector/prefs_backup_invalid_change_unittest.cc
+++ b/chrome/browser/protector/prefs_backup_invalid_change_unittest.cc
@@ -3,8 +3,10 @@
// found in the LICENSE file.
#include "base/memory/scoped_ptr.h"
+#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/prefs/session_startup_pref.h"
#include "chrome/browser/protector/base_setting_change.h"
+#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_profile.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -37,6 +39,11 @@ TEST_F(PrefsBackupInvalidChangeTest, Defaults) {
// Startup URLs should be left unchanged.
EXPECT_EQ(1UL, new_startup_pref.urls.size());
EXPECT_EQ(std::string(kStartupUrl), new_startup_pref.urls[0].spec());
+ // Homepage prefs are reset to defaults.
+ PrefService* prefs = profile_.GetPrefs();
+ EXPECT_FALSE(prefs->HasPrefPath(prefs::kHomePageIsNewTabPage));
+ EXPECT_FALSE(prefs->HasPrefPath(prefs::kHomePage));
+ EXPECT_FALSE(prefs->HasPrefPath(prefs::kShowHomeButton));
}
// Test that "restore last session" setting is not changed by Init.
diff --git a/chrome/browser/protector/protected_prefs_watcher.cc b/chrome/browser/protector/protected_prefs_watcher.cc
index 5f98e15..81a2339 100644
--- a/chrome/browser/protector/protected_prefs_watcher.cc
+++ b/chrome/browser/protector/protected_prefs_watcher.cc
@@ -24,8 +24,19 @@ namespace protector {
namespace {
+// Prefix added to names of backup entries.
const char kBackupPrefsPrefix[] = "backup.";
+// Names of prefs that are backed up.
+const char* const kProtectedPrefNames[] = {
+ prefs::kHomePage,
+ prefs::kHomePageIsNewTabPage,
+ prefs::kShowHomeButton,
+ prefs::kRestoreOnStartup,
+ prefs::kURLsToRestoreOnStartup,
+ prefs::kPinnedTabs
+};
+
// Backup pref names.
const char kBackupHomePage[] = "backup.homepage";
const char kBackupHomePageIsNewTabPage[] = "backup.homepage_is_newtabpage";
@@ -34,10 +45,17 @@ const char kBackupRestoreOnStartup[] = "backup.session.restore_on_startup";
const char kBackupURLsToRestoreOnStartup[] =
"backup.session.urls_to_restore_on_startup";
const char kBackupPinnedTabs[] = "backup.pinned_tabs";
+
+// Special backup entries.
const char kBackupExtensionsIDs[] = "backup.extensions.ids";
const char kBackupSignature[] = "backup._signature";
const char kBackupVersion[] = "backup._version";
+// Returns name of the backup entry for pref |pref_name|.
+std::string GetBackupNameFor(const std::string& pref_name) {
+ return kBackupPrefsPrefix + pref_name;
+}
+
// Appends a list of strings to |out|.
void StringAppendStringList(const base::ListValue* list, std::string* out) {
for (base::ListValue::const_iterator it = list->begin(); it != list->end();
@@ -60,10 +78,28 @@ void StringAppendStringDictionary(const base::DictionaryValue* dict,
}
}
+void StringAppendBoolean(PrefService* prefs,
+ const char* path,
+ std::string* out) {
+ if (prefs->HasPrefPath(path))
+ base::StringAppendF(out, "|%d", prefs->GetBoolean(path) ? 1 : 0);
+ else
+ base::StringAppendF(out, "|");
+}
+
+void StringAppendInteger(PrefService* prefs,
+ const char* path,
+ std::string* out) {
+ if (prefs->HasPrefPath(path))
+ base::StringAppendF(out, "|%d", prefs->GetInteger(path));
+ else
+ base::StringAppendF(out, "|");
+}
+
} // namespace
// static
-const int ProtectedPrefsWatcher::kCurrentVersionNumber = 3;
+const int ProtectedPrefsWatcher::kCurrentVersionNumber = 4;
ProtectedPrefsWatcher::ProtectedPrefsWatcher(Profile* profile)
: is_backup_valid_(true),
@@ -104,30 +140,37 @@ void ProtectedPrefsWatcher::RegisterUserPrefs(PrefService* prefs) {
}
bool ProtectedPrefsWatcher::DidPrefChange(const std::string& path) const {
- const base::Value* backup_value = GetBackupForPref(path);
- if (!backup_value) {
- LOG(WARNING) << "No backup for " << path;
- return false;
- }
- const PrefService::Preference* new_pref =
- profile_->GetPrefs()->FindPreference(path.c_str());
+ std::string backup_path = GetBackupNameFor(path);
+ PrefService* prefs = profile_->GetPrefs();
+ const PrefService::Preference* new_pref = prefs->FindPreference(path.c_str());
DCHECK(new_pref);
- if (new_pref->IsManaged())
+ const PrefService::Preference* backup_pref =
+ profile_->GetPrefs()->FindPreference(backup_path.c_str());
+ DCHECK(backup_pref);
+ if (new_pref->IsDefaultValue())
+ return !backup_pref->IsDefaultValue();
+ if (!new_pref->IsUserControlled())
return false;
- return !backup_value->Equals(new_pref->GetValue());
+ return !backup_pref->GetValue()->Equals(new_pref->GetValue());
}
const base::Value* ProtectedPrefsWatcher::GetBackupForPref(
const std::string& path) const {
if (!is_backup_valid_)
return NULL;
- std::string backup_path = std::string(kBackupPrefsPrefix) + path;
+ std::string backup_path = GetBackupNameFor(path);
+ // These do not directly correspond to any real preference.
+ DCHECK(backup_path != kBackupExtensionsIDs &&
+ backup_path != kBackupSignature);
+ PrefService* prefs = profile_->GetPrefs();
+ // If backup is not set, return the default value of the actual pref.
+ // TODO(ivankr): return NULL instead and handle appropriately in SettingChange
+ // classes.
+ if (!prefs->HasPrefPath(backup_path.c_str()))
+ return prefs->GetDefaultPrefValue(path.c_str());
const PrefService::Preference* backup_pref =
profile_->GetPrefs()->FindPreference(backup_path.c_str());
- DCHECK(backup_pref &&
- // These do not directly correspond to any real preference.
- backup_path != kBackupExtensionsIDs &&
- backup_path != kBackupSignature);
+ DCHECK(backup_pref);
return backup_pref->GetValue();
}
@@ -157,9 +200,15 @@ void ProtectedPrefsWatcher::EnsurePrefsMigration() {
bool ProtectedPrefsWatcher::UpdateCachedPrefs() {
// Direct access to the extensions prefs is required becase ExtensionService
// may not yet have been initialized.
+ const base::DictionaryValue* extension_prefs;
+ const base::Value* extension_prefs_value =
+ profile_->GetPrefs()->GetUserPrefValue(ExtensionPrefs::kExtensionsPref);
+ if (!extension_prefs_value ||
+ !extension_prefs_value->GetAsDictionary(&extension_prefs)) {
+ return false;
+ }
ExtensionPrefs::ExtensionIdSet extension_ids =
- ExtensionPrefs::GetExtensionsFrom(
- profile_->GetPrefs()->GetDictionary(ExtensionPrefs::kExtensionsPref));
+ ExtensionPrefs::GetExtensionsFrom(extension_prefs);
if (extension_ids == cached_extension_ids_)
return false;
cached_extension_ids_.swap(extension_ids);
@@ -176,16 +225,12 @@ bool ProtectedPrefsWatcher::HasBackup() const {
void ProtectedPrefsWatcher::InitBackup() {
PrefService* prefs = profile_->GetPrefs();
- prefs->SetString(kBackupHomePage, prefs->GetString(prefs::kHomePage));
- prefs->SetBoolean(kBackupHomePageIsNewTabPage,
- prefs->GetBoolean(prefs::kHomePageIsNewTabPage));
- prefs->SetBoolean(kBackupShowHomeButton,
- prefs->GetBoolean(prefs::kShowHomeButton));
- prefs->SetInteger(kBackupRestoreOnStartup,
- prefs->GetInteger(prefs::kRestoreOnStartup));
- prefs->Set(kBackupURLsToRestoreOnStartup,
- *prefs->GetList(prefs::kURLsToRestoreOnStartup));
- prefs->Set(kBackupPinnedTabs, *prefs->GetList(prefs::kPinnedTabs));
+ for (size_t i = 0; i < arraysize(kProtectedPrefNames); ++i) {
+ if (prefs->HasPrefPath(kProtectedPrefNames[i])) {
+ prefs->Set(GetBackupNameFor(kProtectedPrefNames[i]).c_str(),
+ *prefs->GetUserPrefValue(kProtectedPrefNames[i]));
+ }
+ }
ListPrefUpdate extension_ids_update(prefs, kBackupExtensionsIDs);
base::ListValue* extension_ids = extension_ids_update.Get();
extension_ids->Clear();
@@ -208,8 +253,9 @@ void ProtectedPrefsWatcher::MigrateOldBackupIfNeeded() {
switch (current_version) {
case 1:
- // Add pinned tabs.
- prefs->Set(kBackupPinnedTabs, *prefs->GetList(prefs::kPinnedTabs));
+ // Add pinned tabs backup.
+ prefs->Set(kBackupPinnedTabs,
+ *prefs->GetUserPrefValue(prefs::kPinnedTabs));
// FALL THROUGH
case 2:
@@ -220,15 +266,25 @@ void ProtectedPrefsWatcher::MigrateOldBackupIfNeeded() {
prefs->Set(kBackupURLsToRestoreOnStartup,
*prefs->GetList(prefs::kURLsToRestoreOnStartup));
// FALL THROUGH
+
+ case 3:
+ // Reset to default values backup prefs whose actual prefs are not set.
+ for (size_t i = 0; i < arraysize(kProtectedPrefNames); ++i) {
+ if (!prefs->HasPrefPath(kProtectedPrefNames[i]))
+ prefs->ClearPref(GetBackupNameFor(kProtectedPrefNames[i]).c_str());
+ }
+ // FALL THROUGH
}
prefs->SetInteger(kBackupVersion, kCurrentVersionNumber);
UpdateBackupSignature();
}
-bool ProtectedPrefsWatcher::UpdateBackupEntry(const std::string& pref_name) {
+bool ProtectedPrefsWatcher::UpdateBackupEntry(const std::string& path) {
+ std::string backup_path = GetBackupNameFor(path);
PrefService* prefs = profile_->GetPrefs();
- if (pref_name == ExtensionPrefs::kExtensionsPref) {
+ const PrefService::Preference* pref = prefs->FindPreference(path.c_str());
+ if (path == ExtensionPrefs::kExtensionsPref) {
// For changes in extension dictionary, do nothing if the IDs list remained
// the same.
if (!UpdateCachedPrefs())
@@ -241,27 +297,15 @@ bool ProtectedPrefsWatcher::UpdateBackupEntry(const std::string& pref_name) {
it != cached_extension_ids_.end(); ++it) {
extension_ids->Append(base::Value::CreateStringValue(*it));
}
- } else if (pref_name == prefs::kHomePage) {
- prefs->SetString(kBackupHomePage, prefs->GetString(prefs::kHomePage));
- } else if (pref_name == prefs::kHomePageIsNewTabPage) {
- prefs->SetBoolean(kBackupHomePageIsNewTabPage,
- prefs->GetBoolean(prefs::kHomePageIsNewTabPage));
- } else if (pref_name == prefs::kShowHomeButton) {
- prefs->SetBoolean(kBackupShowHomeButton,
- prefs->GetBoolean(prefs::kShowHomeButton));
- } else if (pref_name == prefs::kRestoreOnStartup) {
- prefs->SetInteger(kBackupRestoreOnStartup,
- prefs->GetInteger(prefs::kRestoreOnStartup));
- } else if (pref_name == prefs::kURLsToRestoreOnStartup) {
- prefs->Set(kBackupURLsToRestoreOnStartup,
- *prefs->GetList(prefs::kURLsToRestoreOnStartup));
- } else if (pref_name == prefs::kPinnedTabs) {
- prefs->Set(kBackupPinnedTabs, *prefs->GetList(prefs::kPinnedTabs));
- } else {
- NOTREACHED();
+ } else if (!prefs->HasPrefPath(path.c_str())) {
+ // Preference has been removed, remove the backup as well.
+ prefs->ClearPref(backup_path.c_str());
+ } else if (!pref->IsUserControlled()) {
return false;
+ } else {
+ prefs->Set(backup_path.c_str(), *pref->GetValue());
}
- VLOG(1) << "Updated backup entry for: " << pref_name;
+ VLOG(1) << "Updated backup entry for: " << path;
return true;
}
@@ -321,12 +365,10 @@ std::string ProtectedPrefsWatcher::GetSignatureData(PrefService* prefs) const {
int current_version = prefs->GetInteger(kBackupVersion);
// TODO(ivankr): replace this with some existing reliable serializer.
// JSONWriter isn't a good choice because JSON formatting may change suddenly.
- std::string data = base::StringPrintf(
- "%s|%d|%d|%d",
- prefs->GetString(kBackupHomePage).c_str(),
- prefs->GetBoolean(kBackupHomePageIsNewTabPage) ? 1 : 0,
- prefs->GetBoolean(kBackupShowHomeButton) ? 1 : 0,
- prefs->GetInteger(kBackupRestoreOnStartup));
+ std::string data = prefs->GetString(kBackupHomePage);
+ StringAppendBoolean(prefs, kBackupHomePageIsNewTabPage, &data);
+ StringAppendBoolean(prefs, kBackupShowHomeButton, &data);
+ StringAppendInteger(prefs, kBackupRestoreOnStartup, &data);
StringAppendStringList(prefs->GetList(kBackupURLsToRestoreOnStartup), &data);
StringAppendStringList(prefs->GetList(kBackupExtensionsIDs), &data);
if (current_version >= 2) {
diff --git a/chrome/browser/protector/protected_prefs_watcher.h b/chrome/browser/protector/protected_prefs_watcher.h
index 979ca44..46cf5ca 100644
--- a/chrome/browser/protector/protected_prefs_watcher.h
+++ b/chrome/browser/protector/protected_prefs_watcher.h
@@ -75,9 +75,9 @@ class ProtectedPrefsWatcher : public content::NotificationObserver {
// Migrates backup if it is an older version.
void MigrateOldBackupIfNeeded();
- // Updates the backup утекн for |pref_name| and кeturns |true| if the
- // backup has changed.
- bool UpdateBackupEntry(const std::string& pref_name);
+ // Updates the backup entry for |path| and returns |true| if the backup has
+ // changed.
+ bool UpdateBackupEntry(const std::string& path);
// Perform a check that backup is valid and settings have not been modified.
void ValidateBackup();
diff --git a/chrome/browser/protector/protected_prefs_watcher_unittest.cc b/chrome/browser/protector/protected_prefs_watcher_unittest.cc
index bf7f1fa..7ce50a0 100644
--- a/chrome/browser/protector/protected_prefs_watcher_unittest.cc
+++ b/chrome/browser/protector/protected_prefs_watcher_unittest.cc
@@ -48,8 +48,8 @@ class ProtectedPrefsWatcherTest : public testing::Test {
prefs_watcher_->ValidateBackup();
}
- void ForceUpdateSignature() {
- prefs_watcher_->UpdateBackupSignature();
+ void ForceUpdateSignature(ProtectedPrefsWatcher* prefs_watcher) {
+ prefs_watcher->UpdateBackupSignature();
}
protected:
@@ -174,7 +174,7 @@ TEST_F(ProtectedPrefsWatcherTest, MigrationToVersion2) {
// Store the old signature (without "pinned_tabs").
prefs_->ClearPref("backup._version");
prefs_->ClearPref("backup.pinned_tabs");
- ForceUpdateSignature();
+ ForceUpdateSignature(prefs_watcher_);
EXPECT_TRUE(IsSignatureValid());
// This will migrate backup to the latest version.
@@ -196,12 +196,12 @@ TEST_F(ProtectedPrefsWatcherTest, MigrationToVersion3) {
// Bring startup prefs to an old (pre-migration) version.
prefs_->SetBoolean(prefs::kHomePageIsNewTabPage, false);
- prefs_->SetString(prefs::kHomePage, "http://example.com/");
+ prefs_->SetString(prefs::kHomePage, kNewHomePage);
prefs_->ClearPref(prefs::kRestoreOnStartupMigrated);
// Reset version to 2 and overwrite the signature.
prefs_->SetInteger("backup._version", 2);
- ForceUpdateSignature();
+ ForceUpdateSignature(prefs_watcher_);
EXPECT_TRUE(IsSignatureValid());
// Take down the old instance and create a new ProtectedPrefsWatcher from
@@ -219,4 +219,75 @@ TEST_F(ProtectedPrefsWatcherTest, MigrationToVersion3) {
prefs_->GetInteger("backup._version"));
}
+// Verify that migration to version 4 removes backups with default values.
+TEST_F(ProtectedPrefsWatcherTest, MigrationToVersion4) {
+ EXPECT_TRUE(prefs_watcher_->is_backup_valid());
+
+ prefs_->SetString(prefs::kHomePage, kNewHomePage);
+ EXPECT_TRUE(prefs_->HasPrefPath("backup.homepage"));
+
+ // Reset version to 3 and overwrite the signature.
+ prefs_->SetInteger("backup._version", 3);
+ ForceUpdateSignature(prefs_watcher_);
+ EXPECT_TRUE(IsSignatureValid());
+
+ ProtectorServiceFactory::GetForProfile(&profile_)->
+ StopWatchingPrefsForTesting();
+
+ // Restore |kHomePage| to default value.
+ prefs_->ClearPref(prefs::kHomePage);
+
+ scoped_ptr<ProtectedPrefsWatcher> prefs_watcher2(
+ new ProtectedPrefsWatcher(&profile_));
+ EXPECT_TRUE(prefs_watcher2->is_backup_valid());
+
+ // Backup for |kHomePage| should now be restored to the default value, too.
+ EXPECT_FALSE(prefs_->HasPrefPath("backup.homepage"));
+ EXPECT_FALSE(prefs_watcher2->DidPrefChange(prefs::kHomePage));
+ EXPECT_EQ(ProtectedPrefsWatcher::kCurrentVersionNumber,
+ prefs_->GetInteger("backup._version"));
+}
+
+// Verify handling of default values of protected prefs.
+TEST_F(ProtectedPrefsWatcherTest, DefaultValues) {
+ EXPECT_TRUE(prefs_watcher_->is_backup_valid());
+
+ EXPECT_FALSE(prefs_->HasPrefPath(prefs::kHomePage));
+ EXPECT_FALSE(prefs_watcher_->DidPrefChange(prefs::kHomePage));
+ prefs_->SetString(prefs::kHomePage, kNewHomePage);
+ EXPECT_FALSE(prefs_watcher_->DidPrefChange(prefs::kHomePage));
+
+ ProtectorServiceFactory::GetForProfile(&profile_)->
+ StopWatchingPrefsForTesting();
+
+ // Restore |kHomePage| to default value.
+ prefs_->ClearPref(prefs::kHomePage);
+
+ scoped_ptr<ProtectedPrefsWatcher> prefs_watcher2(
+ new ProtectedPrefsWatcher(&profile_));
+ EXPECT_TRUE(prefs_watcher2->is_backup_valid());
+ EXPECT_TRUE(prefs_watcher2->DidPrefChange(prefs::kHomePage));
+
+ prefs_->ClearPref("backup.homepage");
+ ForceUpdateSignature(prefs_watcher2.get());
+
+ EXPECT_TRUE(prefs_watcher2->is_backup_valid());
+ EXPECT_FALSE(prefs_watcher2->DidPrefChange(prefs::kHomePage));
+}
+
+TEST_F(ProtectedPrefsWatcherTest, CheckPrefNames) {
+ // If any of these preference names change, add corresponding migration code
+ // to ProtectedPrefsWatcher.
+ // DO NOT simply fix these literals!
+ EXPECT_EQ("homepage", std::string(prefs::kHomePage));
+ EXPECT_EQ("homepage_is_newtabpage",
+ std::string(prefs::kHomePageIsNewTabPage));
+ EXPECT_EQ("browser.show_home_button", std::string(prefs::kShowHomeButton));
+ EXPECT_EQ("session.restore_on_startup",
+ std::string(prefs::kRestoreOnStartup));
+ EXPECT_EQ("session.urls_to_restore_on_startup",
+ std::string(prefs::kURLsToRestoreOnStartup));
+ EXPECT_EQ("pinned_tabs", std::string(prefs::kPinnedTabs));
+}
+
} // namespace protector