diff options
author | ivankr@chromium.org <ivankr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-22 13:08:26 +0000 |
---|---|---|
committer | ivankr@chromium.org <ivankr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-22 13:08:26 +0000 |
commit | be91c96444ef52ff074a27da913e1bf74d9d116d (patch) | |
tree | 24bd0ec992cde446d670c69d9fa72f51ee4b927f /chrome/browser/protector | |
parent | f3ae866d0a12c9a8ebc74dfb0192e52569a47d23 (diff) | |
download | chromium_src-be91c96444ef52ff074a27da913e1bf74d9d116d.zip chromium_src-be91c96444ef52ff074a27da913e1bf74d9d116d.tar.gz chromium_src-be91c96444ef52ff074a27da913e1bf74d9d116d.tar.bz2 |
[protector] Added UI for invalid Preferences backup case.
*) Added another BaseSettingChange to represent invalid backup case.
*) Few other corrections and strings cleanup.
BUG=114288
TEST=protector.py
Review URL: http://codereview.chromium.org/9748016
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@128200 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/protector')
7 files changed, 201 insertions, 12 deletions
diff --git a/chrome/browser/protector/base_setting_change.h b/chrome/browser/protector/base_setting_change.h index b0f99c6..a0ebe82 100644 --- a/chrome/browser/protector/base_setting_change.h +++ b/chrome/browser/protector/base_setting_change.h @@ -93,6 +93,10 @@ BaseSettingChange* CreateSessionStartupChange( const SessionStartupPref& actual, const SessionStartupPref& backup); +// Allocates and initializes BaseSettingChange implementation for an unknown +// preferences change with invalid backup. +BaseSettingChange* CreatePrefsBackupInvalidChange(); + } // namespace protector #endif // CHROME_BROWSER_PROTECTOR_BASE_SETTING_CHANGE_H_ diff --git a/chrome/browser/protector/prefs_backup_invalid_change.cc b/chrome/browser/protector/prefs_backup_invalid_change.cc new file mode 100644 index 0000000..cc3e5c6 --- /dev/null +++ b/chrome/browser/protector/prefs_backup_invalid_change.cc @@ -0,0 +1,130 @@ +// Copyright (c) 2012 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 "base/basictypes.h" +#include "base/compiler_specific.h" +#include "base/logging.h" +#include "base/metrics/histogram.h" +#include "base/utf_string_conversions.h" +#include "chrome/browser/prefs/session_startup_pref.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/protector/base_prefs_change.h" +#include "chrome/browser/protector/histograms.h" +#include "chrome/browser/protector/protector_service.h" +#include "chrome/browser/protector/protector_service_factory.h" +#include "chrome/common/pref_names.h" +#include "chrome/common/url_constants.h" +#include "googleurl/src/gurl.h" +#include "grit/chromium_strings.h" +#include "grit/generated_resources.h" +#include "grit/theme_resources.h" +#include "ui/base/l10n/l10n_util.h" + +namespace protector { + +// Unknown change to Preferences with invalid backup. +class PrefsBackupInvalidChange : public BasePrefsChange { + public: + PrefsBackupInvalidChange(); + + // BasePrefsChange overrides: + virtual bool Init(Profile* profile) OVERRIDE; + virtual void Apply(Browser* browser) OVERRIDE; + virtual void Discard(Browser* browser) OVERRIDE; + virtual void Timeout() OVERRIDE; + virtual int GetBadgeIconID() const OVERRIDE; + virtual int GetMenuItemIconID() const OVERRIDE; + virtual int GetBubbleIconID() const OVERRIDE; + virtual string16 GetBubbleTitle() const OVERRIDE; + virtual string16 GetBubbleMessage() const OVERRIDE; + virtual string16 GetApplyButtonText() const OVERRIDE; + virtual string16 GetDiscardButtonText() const OVERRIDE; + + private: + virtual ~PrefsBackupInvalidChange(); + + // Applies default settings values when appropriate. + void ApplyDefaults(Profile* profile); + + DISALLOW_COPY_AND_ASSIGN(PrefsBackupInvalidChange); +}; + +PrefsBackupInvalidChange::PrefsBackupInvalidChange() { +} + +PrefsBackupInvalidChange::~PrefsBackupInvalidChange() { +} + +bool PrefsBackupInvalidChange::Init(Profile* profile) { + if (!BasePrefsChange::Init(profile)) + return false; + ApplyDefaults(profile); + DismissOnPrefChange(prefs::kHomePageIsNewTabPage); + DismissOnPrefChange(prefs::kHomePage); + DismissOnPrefChange(prefs::kShowHomeButton); + DismissOnPrefChange(prefs::kRestoreOnStartup); + DismissOnPrefChange(prefs::kURLsToRestoreOnStartup); + return true; +} + +void PrefsBackupInvalidChange::Apply(Browser* browser) { + NOTREACHED(); +} + +void PrefsBackupInvalidChange::Discard(Browser* browser) { + // TODO(ivankr): highlight the protected prefs on the settings page + // (http://crbug.com/119088). + ProtectorServiceFactory::GetForProfile(profile())->OpenTab( + GURL(chrome::kChromeUISettingsURL), browser); +} + +void PrefsBackupInvalidChange::Timeout() { +} + +int PrefsBackupInvalidChange::GetBadgeIconID() const { + // Use icons for startup settings change. + return IDR_HOMEPAGE_CHANGE_BADGE; +} + +int PrefsBackupInvalidChange::GetMenuItemIconID() const { + return IDR_HOMEPAGE_CHANGE_MENU; +} + +int PrefsBackupInvalidChange::GetBubbleIconID() const { + return IDR_HOMEPAGE_CHANGE_ALERT; +} + +string16 PrefsBackupInvalidChange::GetBubbleTitle() const { + return l10n_util::GetStringUTF16(IDS_SETTING_CHANGE_NO_BACKUP_TITLE); +} + +string16 PrefsBackupInvalidChange::GetBubbleMessage() const { + return l10n_util::GetStringUTF16(IDS_SETTING_CHANGE_NO_BACKUP_BUBBLE_MESSAGE); +} + +string16 PrefsBackupInvalidChange::GetApplyButtonText() const { + // Don't show this button. + return string16(); +} + +string16 PrefsBackupInvalidChange::GetDiscardButtonText() const { + return l10n_util::GetStringUTF16(IDS_EDIT_SETTINGS); +} + +void PrefsBackupInvalidChange::ApplyDefaults(Profile* profile) { + PrefService* prefs = profile->GetPrefs(); + SessionStartupPref startup_pref = SessionStartupPref::GetStartupPref(prefs); + 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); + } +} + +BaseSettingChange* CreatePrefsBackupInvalidChange() { + return new PrefsBackupInvalidChange(); +} + +} // namespace protector diff --git a/chrome/browser/protector/prefs_backup_invalid_change_unittest.cc b/chrome/browser/protector/prefs_backup_invalid_change_unittest.cc new file mode 100644 index 0000000..2ac7e5d --- /dev/null +++ b/chrome/browser/protector/prefs_backup_invalid_change_unittest.cc @@ -0,0 +1,59 @@ +// Copyright (c) 2012 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 "base/memory/scoped_ptr.h" +#include "chrome/browser/prefs/session_startup_pref.h" +#include "chrome/browser/protector/base_setting_change.h" +#include "chrome/test/base/testing_profile.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace protector { + +namespace { + +const char kStartupUrl[] = "http://example.com/"; + +} // namespace + +class PrefsBackupInvalidChangeTest : public testing::Test { + protected: + TestingProfile profile_; +}; + +// Test that correct default values are applied by Init. +TEST_F(PrefsBackupInvalidChangeTest, Defaults) { + SessionStartupPref startup_pref(SessionStartupPref::URLS); + startup_pref.urls.push_back(GURL(kStartupUrl)); + SessionStartupPref::SetStartupPref(&profile_, startup_pref); + + scoped_ptr<BaseSettingChange> change(CreatePrefsBackupInvalidChange()); + change->Init(&profile_); + + SessionStartupPref new_startup_pref = + SessionStartupPref::GetStartupPref(&profile_); + // Startup type should be reset to default. + EXPECT_EQ(SessionStartupPref::GetDefaultStartupType(), new_startup_pref.type); + // 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()); +} + +// Test that "restore last session" setting is not changed by Init. +TEST_F(PrefsBackupInvalidChangeTest, DefaultsWithSessionRestore) { + SessionStartupPref startup_pref(SessionStartupPref::LAST); + startup_pref.urls.push_back(GURL(kStartupUrl)); + SessionStartupPref::SetStartupPref(&profile_, startup_pref); + + scoped_ptr<BaseSettingChange> change(CreatePrefsBackupInvalidChange()); + change->Init(&profile_); + + SessionStartupPref new_startup_pref = + SessionStartupPref::GetStartupPref(&profile_); + // Both startup type and startup URLs should be left unchanged. + EXPECT_EQ(SessionStartupPref::LAST, new_startup_pref.type); + EXPECT_EQ(1UL, new_startup_pref.urls.size()); + EXPECT_EQ(std::string(kStartupUrl), new_startup_pref.urls[0].spec()); +} + +} // namespace protector diff --git a/chrome/browser/protector/protected_prefs_watcher.cc b/chrome/browser/protector/protected_prefs_watcher.cc index 218a330..8fedbe0 100644 --- a/chrome/browser/protector/protected_prefs_watcher.cc +++ b/chrome/browser/protector/protected_prefs_watcher.cc @@ -232,8 +232,9 @@ void ProtectedPrefsWatcher::ValidateBackup() { kProtectorErrorCount); } else { LOG(WARNING) << "Invalid backup signature"; - // Further changes to protected prefs will overwrite the signature. is_backup_valid_ = false; + // The whole backup has been compromised, overwrite it. + InitBackup(); UMA_HISTOGRAM_ENUMERATION( kProtectorHistogramPrefs, kProtectorErrorBackupInvalid, diff --git a/chrome/browser/protector/protected_prefs_watcher.h b/chrome/browser/protector/protected_prefs_watcher.h index d5463a0..9e3ad48 100644 --- a/chrome/browser/protector/protected_prefs_watcher.h +++ b/chrome/browser/protector/protected_prefs_watcher.h @@ -39,6 +39,9 @@ class ProtectedPrefsWatcher : public content::NotificationObserver { // instance is owned by the PrefService. const base::Value* GetBackupForPref(const std::string& path) const; + // Updates the backup signature. + void UpdateBackupSignature(); + // True if the backup was valid at the profile load time. bool is_backup_valid() { return is_backup_valid_; } @@ -69,9 +72,6 @@ class ProtectedPrefsWatcher : public content::NotificationObserver { // backup has changed. bool UpdateBackupEntry(const std::string& pref_name); - // Updates the backup signature. - void UpdateBackupSignature(); - // 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 b1ec3ae..ef8c48e 100644 --- a/chrome/browser/protector/protected_prefs_watcher_unittest.cc +++ b/chrome/browser/protector/protected_prefs_watcher_unittest.cc @@ -53,7 +53,6 @@ class ProtectedPrefsWatcherTest : public testing::Test { TEST_F(ProtectedPrefsWatcherTest, ValidOnCleanProfile) { EXPECT_TRUE(HasBackup()); - EXPECT_TRUE(IsSignatureValid()); EXPECT_TRUE(prefs_watcher_->is_backup_valid()); } @@ -67,7 +66,6 @@ TEST_F(ProtectedPrefsWatcherTest, ValidAfterPrefChange) { prefs_->SetString(prefs::kHomePage, kNewHomePage); EXPECT_TRUE(HasBackup()); - EXPECT_TRUE(IsSignatureValid()); EXPECT_TRUE(prefs_watcher_->is_backup_valid()); EXPECT_EQ(prefs_->GetString(prefs::kHomePage), kNewHomePage); // Backup is updated accordingly. @@ -80,7 +78,6 @@ TEST_F(ProtectedPrefsWatcherTest, InvalidSignature) { prefs_->SetString("backup.homepage", kNewHomePage); RevalidateBackup(); EXPECT_TRUE(HasBackup()); - EXPECT_FALSE(IsSignatureValid()); EXPECT_FALSE(prefs_watcher_->is_backup_valid()); // No backup values available. EXPECT_FALSE(prefs_watcher_->GetBackupForPref(prefs::kHomePage)); @@ -115,12 +112,10 @@ TEST_F(ProtectedPrefsWatcherTest, ExtensionPrefChange) { sample_id, !extension_prefs->IsAppNotificationDisabled(sample_id)); // Backup is still valid. - EXPECT_TRUE(IsSignatureValid()); EXPECT_TRUE(prefs_watcher_->is_backup_valid()); - // Make backup invalid by changing one of its members directly. - prefs_->SetString("backup.homepage", kNewHomePage); - RevalidateBackup(); + // Make signature invalid by changing it directly. + prefs_->SetString("backup._signature", "INVALID"); EXPECT_FALSE(IsSignatureValid()); // Flip another pref value of that extension. diff --git a/chrome/browser/protector/session_startup_change.cc b/chrome/browser/protector/session_startup_change.cc index 275997c..9aa17ee 100644 --- a/chrome/browser/protector/session_startup_change.cc +++ b/chrome/browser/protector/session_startup_change.cc @@ -20,7 +20,7 @@ namespace protector { -// STARTUP_SETTING and session startup settings change tracked by Protector. +// Session startup settings change tracked by Protector. class SessionStartupChange : public BasePrefsChange { public: SessionStartupChange(const SessionStartupPref& actual, |