From 69bb9e6e26fcd3e93edb969fb43672bf33b7834b Mon Sep 17 00:00:00 2001 From: "ivankr@chromium.org" Date: Fri, 20 Jan 2012 11:02:30 +0000 Subject: Converted Protector into a ProfileKeyedService. BUG=None TEST=browser_tests:ProtectorServiceTest.*,DefaultSearchProviderChangeTest.* Review URL: https://chromiumcodereview.appspot.com/9258001 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@118434 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome/browser/prefs/browser_prefs.cc | 4 +- chrome/browser/protector/base_setting_change.cc | 12 +- chrome/browser/protector/base_setting_change.h | 13 +- .../protector/default_search_provider_change.cc | 17 +-- .../default_search_provider_change_browsertest.cc | 44 +++--- chrome/browser/protector/mock_protector.cc | 17 --- chrome/browser/protector/mock_protector.h | 31 ----- chrome/browser/protector/mock_protector_service.cc | 34 +++++ chrome/browser/protector/mock_protector_service.h | 44 ++++++ chrome/browser/protector/mock_setting_change.cc | 6 +- chrome/browser/protector/mock_setting_change.h | 4 +- chrome/browser/protector/protector.cc | 115 ---------------- chrome/browser/protector/protector.h | 86 ------------ chrome/browser/protector/protector_browsertest.cc | 114 ---------------- chrome/browser/protector/protector_service.cc | 139 +++++++++++++++++++ chrome/browser/protector/protector_service.h | 97 ++++++++++++++ .../protector/protector_service_browsertest.cc | 149 +++++++++++++++++++++ .../browser/protector/protector_service_factory.cc | 43 ++++++ .../browser/protector/protector_service_factory.h | 41 ++++++ .../protector/settings_change_global_error.cc | 3 +- .../browser/search_engines/template_url_service.cc | 15 ++- chrome/browser/webdata/keyword_table.cc | 2 +- chrome/chrome_browser.gypi | 6 +- chrome/chrome_tests.gypi | 10 +- 24 files changed, 617 insertions(+), 429 deletions(-) delete mode 100644 chrome/browser/protector/mock_protector.cc delete mode 100644 chrome/browser/protector/mock_protector.h create mode 100644 chrome/browser/protector/mock_protector_service.cc create mode 100644 chrome/browser/protector/mock_protector_service.h delete mode 100644 chrome/browser/protector/protector.cc delete mode 100644 chrome/browser/protector/protector.h delete mode 100644 chrome/browser/protector/protector_browsertest.cc create mode 100644 chrome/browser/protector/protector_service.cc create mode 100644 chrome/browser/protector/protector_service.h create mode 100644 chrome/browser/protector/protector_service_browsertest.cc create mode 100644 chrome/browser/protector/protector_service_factory.cc create mode 100644 chrome/browser/protector/protector_service_factory.h (limited to 'chrome') diff --git a/chrome/browser/prefs/browser_prefs.cc b/chrome/browser/prefs/browser_prefs.cc index 1abb295..86d870c 100644 --- a/chrome/browser/prefs/browser_prefs.cc +++ b/chrome/browser/prefs/browser_prefs.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// 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. @@ -43,7 +43,7 @@ #include "chrome/browser/profiles/profile_impl.h" #include "chrome/browser/profiles/profile_info_cache.h" #include "chrome/browser/profiles/profile_manager.h" -#include "chrome/browser/protector/protector.h" +#include "chrome/browser/protector/protector_service.h" #include "chrome/browser/renderer_host/web_cache_manager.h" #include "chrome/browser/safe_browsing/safe_browsing_service.h" #include "chrome/browser/search_engines/template_url_prepopulate_data.h" diff --git a/chrome/browser/protector/base_setting_change.cc b/chrome/browser/protector/base_setting_change.cc index a622db0..3ee61d4 100644 --- a/chrome/browser/protector/base_setting_change.cc +++ b/chrome/browser/protector/base_setting_change.cc @@ -5,23 +5,19 @@ #include "chrome/browser/protector/base_setting_change.h" #include "base/logging.h" -#include "chrome/browser/protector/protector.h" namespace protector { BaseSettingChange::BaseSettingChange() - : profile_(NULL), - protector_(NULL) { + : profile_(NULL) { } BaseSettingChange::~BaseSettingChange() { } -bool BaseSettingChange::Init(Protector* protector) { - DCHECK(protector); - protector_ = protector; - profile_ = protector->profile(); - DCHECK(profile_); +bool BaseSettingChange::Init(Profile* profile) { + DCHECK(profile); + profile_ = profile; return true; } diff --git a/chrome/browser/protector/base_setting_change.h b/chrome/browser/protector/base_setting_change.h index 14d83bd..8601ca3 100644 --- a/chrome/browser/protector/base_setting_change.h +++ b/chrome/browser/protector/base_setting_change.h @@ -17,8 +17,6 @@ class TemplateURL; namespace protector { -class Protector; - // Base class for setting change tracked by Protector. class BaseSettingChange { public: @@ -28,9 +26,9 @@ class BaseSettingChange { // Applies initial actions to the setting if needed. Must be called before // any other calls are made, including text getters. // Returns true if initialization was successful. - // Associates this change with |protector_| instance so overrides must - // call the base method. - virtual bool Init(Protector* protector); + // Associates this change with |profile| instance so overrides must call the + // base method. + virtual bool Init(Profile* profile); // Persists new setting if needed. virtual void Apply(); @@ -63,15 +61,12 @@ class BaseSettingChange { // Returns text for the button to discard the change with |Discard|. virtual string16 GetDiscardButtonText() const = 0; + protected: // Profile instance we've been associated with by an |Init| call. Profile* profile() { return profile_; } - // Protector instance we've been associated with by an |Init| call. - Protector* protector() { return protector_; } - private: Profile* profile_; - Protector* protector_; DISALLOW_COPY_AND_ASSIGN(BaseSettingChange); }; diff --git a/chrome/browser/protector/default_search_provider_change.cc b/chrome/browser/protector/default_search_provider_change.cc index 1ce6346..6bfed3b 100644 --- a/chrome/browser/protector/default_search_provider_change.cc +++ b/chrome/browser/protector/default_search_provider_change.cc @@ -12,7 +12,8 @@ #include "chrome/browser/profiles/profile.h" #include "chrome/browser/protector/base_setting_change.h" #include "chrome/browser/protector/histograms.h" -#include "chrome/browser/protector/protector.h" +#include "chrome/browser/protector/protector_service.h" +#include "chrome/browser/protector/protector_service_factory.h" #include "chrome/browser/search_engines/template_url.h" #include "chrome/browser/search_engines/template_url_prepopulate_data.h" #include "chrome/browser/search_engines/template_url_service.h" @@ -87,7 +88,7 @@ class DefaultSearchProviderChange : public BaseSettingChange, TemplateURL* backup_search_provider); // BaseSettingChange overrides: - virtual bool Init(Protector* protector) OVERRIDE; + virtual bool Init(Profile* profile) OVERRIDE; virtual void Apply() OVERRIDE; virtual void Discard() OVERRIDE; virtual void Timeout() OVERRIDE; @@ -168,8 +169,8 @@ DefaultSearchProviderChange::~DefaultSearchProviderChange() { GetTemplateURLService()->RemoveObserver(this); } -bool DefaultSearchProviderChange::Init(Protector* protector) { - if (!BaseSettingChange::Init(protector)) +bool DefaultSearchProviderChange::Init(Profile* profile) { + if (!BaseSettingChange::Init(profile)) return false; if (backup_search_provider_.get()) { @@ -222,7 +223,7 @@ bool DefaultSearchProviderChange::Init(Protector* protector) { new_id_ = new_search_provider_->id(); registrar_.Add( this, chrome::NOTIFICATION_TEMPLATE_URL_REMOVED, - content::Source(profile()->GetOriginalProfile())); + content::Source(profile->GetOriginalProfile())); } return true; @@ -330,8 +331,8 @@ void DefaultSearchProviderChange::OnTemplateURLServiceChanged() { VLOG(1) << "Default search provider has been changed by user"; default_search_provider_ = NULL; url_service->RemoveObserver(this); - // This will delete the Protector instance and |this|. - protector()->DismissChange(); + // Will delete this DefaultSearchProviderChange instance. + ProtectorServiceFactory::GetForProfile(profile())->DismissChange(); } } @@ -382,7 +383,7 @@ const TemplateURL* DefaultSearchProviderChange::SetDefaultSearchProvider( } void DefaultSearchProviderChange::OpenSearchEngineSettings() { - protector()->OpenTab( + ProtectorServiceFactory::GetForProfile(profile())->OpenTab( GURL(std::string(chrome::kChromeUISettingsURL) + chrome::kSearchEnginesSubPage)); } diff --git a/chrome/browser/protector/default_search_provider_change_browsertest.cc b/chrome/browser/protector/default_search_provider_change_browsertest.cc index 75dd5a4..206da5b 100644 --- a/chrome/browser/protector/default_search_provider_change_browsertest.cc +++ b/chrome/browser/protector/default_search_provider_change_browsertest.cc @@ -5,7 +5,8 @@ #include "base/memory/scoped_ptr.h" #include "base/message_loop.h" #include "base/utf_string_conversions.h" -#include "chrome/browser/protector/mock_protector.h" +#include "chrome/browser/protector/mock_protector_service.h" +#include "chrome/browser/protector/protector_service_factory.h" #include "chrome/browser/search_engines/template_url.h" #include "chrome/browser/search_engines/template_url_prepopulate_data.h" #include "chrome/browser/search_engines/template_url_service.h" @@ -41,8 +42,9 @@ const std::string http_example_net = "http://example.net/%s"; class DefaultSearchProviderChangeTest : public InProcessBrowserTest { public: - virtual void SetUpOnMainThread() { - mock_protector_.reset(new MockProtector(browser()->profile())); + virtual void SetUpOnMainThread() OVERRIDE { + mock_protector_service_ = + MockProtectorService::BuildForProfile(browser()->profile()); // Ensure that TemplateURLService is loaded. turl_service_ = @@ -53,6 +55,10 @@ class DefaultSearchProviderChangeTest : public InProcessBrowserTest { TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(NULL)); } + virtual void CleanUpOnMainThread() OVERRIDE { + EXPECT_CALL(*mock_protector_service_, Shutdown()); + } + TemplateURL* MakeTemplateURL(const string16& short_name, const string16& keyword, const std::string& search_url) { @@ -83,9 +89,9 @@ class DefaultSearchProviderChangeTest : public InProcessBrowserTest { turl_service_->Add(turl_copy); } - void AddAndSetDefault(TemplateURL* t_url) { - turl_service_->Add(t_url); - turl_service_->SetDefaultSearchProvider(t_url); + void AddAndSetDefault(TemplateURL* turl) { + turl_service_->Add(turl); + turl_service_->SetDefaultSearchProvider(turl); } string16 GetBubbleMessage(const string16& short_name = string16()) { @@ -113,11 +119,11 @@ class DefaultSearchProviderChangeTest : public InProcessBrowserTest { void ExpectSettingsOpened(const std::string& subpage) { GURL settings_url(chrome::kChromeUISettingsURL + subpage); - EXPECT_CALL(*mock_protector_.get(), OpenTab(settings_url)); + EXPECT_CALL(*mock_protector_service_, OpenTab(settings_url)); } protected: - scoped_ptr mock_protector_; + MockProtectorService* mock_protector_service_; TemplateURLService* turl_service_; scoped_ptr prepopulated_url_; }; @@ -142,7 +148,7 @@ IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, BackupValid) { scoped_ptr change( CreateDefaultSearchProviderChange(current_url, backup_url)); ASSERT_TRUE(change.get()); - ASSERT_TRUE(change->Init(mock_protector_.get())); + ASSERT_TRUE(change->Init(browser()->profile())); // Verify that backup is active. EXPECT_EQ(FindTemplateURL(http_example_info), @@ -185,7 +191,7 @@ IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, BackupValidLongNames) { scoped_ptr change( CreateDefaultSearchProviderChange(current_url, backup_url_long)); ASSERT_TRUE(change.get()); - ASSERT_TRUE(change->Init(mock_protector_.get())); + ASSERT_TRUE(change->Init(browser()->profile())); // Verify text messages. EXPECT_EQ(GetBubbleMessage(), change->GetBubbleMessage()); @@ -202,7 +208,7 @@ IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, BackupValidLongNames) { scoped_ptr change( CreateDefaultSearchProviderChange(current_url_long, backup_url)); ASSERT_TRUE(change.get()); - ASSERT_TRUE(change->Init(mock_protector_.get())); + ASSERT_TRUE(change->Init(browser()->profile())); // Verify text messages. EXPECT_EQ(GetBubbleMessage(), change->GetBubbleMessage()); @@ -226,7 +232,7 @@ IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, BackupInvalid) { scoped_ptr change( CreateDefaultSearchProviderChange(current_url, NULL)); ASSERT_TRUE(change.get()); - ASSERT_TRUE(change->Init(mock_protector_.get())); + ASSERT_TRUE(change->Init(browser()->profile())); // Verify that the prepopulated default search is active. EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()->url()), @@ -270,7 +276,7 @@ IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, scoped_ptr change( CreateDefaultSearchProviderChange(current_url, NULL)); ASSERT_TRUE(change.get()); - ASSERT_TRUE(change->Init(mock_protector_.get())); + ASSERT_TRUE(change->Init(browser()->profile())); // Verify that the prepopulated default search is active. EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()->url()), @@ -307,7 +313,7 @@ IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, scoped_ptr change( CreateDefaultSearchProviderChange(NULL, backup_url)); ASSERT_TRUE(change.get()); - ASSERT_TRUE(change->Init(mock_protector_.get())); + ASSERT_TRUE(change->Init(browser()->profile())); // Verify that backup is active. EXPECT_EQ(FindTemplateURL(http_example_info), @@ -340,7 +346,7 @@ IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, scoped_ptr change( CreateDefaultSearchProviderChange(NULL, NULL)); ASSERT_TRUE(change.get()); - ASSERT_TRUE(change->Init(mock_protector_.get())); + ASSERT_TRUE(change->Init(browser()->profile())); // Verify that the prepopulated default search is active. EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()->url()), @@ -372,7 +378,7 @@ IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, scoped_ptr change( CreateDefaultSearchProviderChange(current_url, NULL)); ASSERT_TRUE(change.get()); - ASSERT_TRUE(change->Init(mock_protector_.get())); + ASSERT_TRUE(change->Init(browser()->profile())); // Verify that the default search has not changed. EXPECT_EQ(current_url, turl_service_->GetDefaultSearchProvider()); @@ -407,14 +413,14 @@ IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, scoped_ptr change( CreateDefaultSearchProviderChange(current_url, backup_url)); ASSERT_TRUE(change.get()); - ASSERT_TRUE(change->Init(mock_protector_.get())); + ASSERT_TRUE(change->Init(browser()->profile())); // Verify that backup is active. EXPECT_EQ(FindTemplateURL(http_example_info), turl_service_->GetDefaultSearchProvider()); // Verify that changing search provider externally dismissed the change. - EXPECT_CALL(*mock_protector_.get(), DismissChange()); + EXPECT_CALL(*mock_protector_service_, DismissChange()); AddAndSetDefault(new_url); } @@ -433,7 +439,7 @@ IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, scoped_ptr change( CreateDefaultSearchProviderChange(current_url, backup_url)); ASSERT_TRUE(change.get()); - ASSERT_TRUE(change->Init(mock_protector_.get())); + ASSERT_TRUE(change->Init(browser()->profile())); // Verify that backup is active. EXPECT_EQ(FindTemplateURL(http_example_info), diff --git a/chrome/browser/protector/mock_protector.cc b/chrome/browser/protector/mock_protector.cc deleted file mode 100644 index 868ed93..0000000 --- a/chrome/browser/protector/mock_protector.cc +++ /dev/null @@ -1,17 +0,0 @@ -// 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 "chrome/browser/protector/mock_protector.h" - -#include "googleurl/src/gurl.h" - -namespace protector { - -MockProtector::MockProtector(Profile* profile) : Protector(profile) { -} - -MockProtector::~MockProtector() { -} - -} // namespace protector diff --git a/chrome/browser/protector/mock_protector.h b/chrome/browser/protector/mock_protector.h deleted file mode 100644 index 513d6a5..0000000 --- a/chrome/browser/protector/mock_protector.h +++ /dev/null @@ -1,31 +0,0 @@ -// 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. - -#ifndef CHROME_BROWSER_PROTECTOR_MOCK_PROTECTOR_H_ -#define CHROME_BROWSER_PROTECTOR_MOCK_PROTECTOR_H_ -#pragma once - -#include "chrome/browser/protector/protector.h" -#include "testing/gmock/include/gmock/gmock.h" - -namespace protector { - -class MockProtector : public Protector { - public: - explicit MockProtector(Profile* profile); - virtual ~MockProtector(); - - MOCK_METHOD1(ShowChange, void(BaseSettingChange*)); - MOCK_METHOD0(DismissChange, void()); - MOCK_METHOD1(OpenTab, void(const GURL&)); - - MOCK_METHOD0(OnApplyChange, void()); - MOCK_METHOD0(OnDiscardChange, void()); - MOCK_METHOD0(OnDecisionTimeout, void()); - MOCK_METHOD0(OnRemovedFromProfile, void()); -}; - -} // namespace protector - -#endif // CHROME_BROWSER_PROTECTOR_MOCK_PROTECTOR_H_ diff --git a/chrome/browser/protector/mock_protector_service.cc b/chrome/browser/protector/mock_protector_service.cc new file mode 100644 index 0000000..a8caacb --- /dev/null +++ b/chrome/browser/protector/mock_protector_service.cc @@ -0,0 +1,34 @@ +// 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 "chrome/browser/protector/mock_protector_service.h" + +#include "chrome/browser/protector/protector_service_factory.h" +#include "googleurl/src/gurl.h" + +namespace protector { + +namespace { + +ProfileKeyedService* BuildMockProtectorService(Profile* profile) { + return new MockProtectorService(profile); +} + +} // namespace + +// static +MockProtectorService* MockProtectorService::BuildForProfile(Profile* profile) { + return static_cast( + ProtectorServiceFactory::GetInstance()->SetTestingFactoryAndUse( + profile, BuildMockProtectorService)); +} + +MockProtectorService::MockProtectorService(Profile* profile) + : ProtectorService(profile) { +} + +MockProtectorService::~MockProtectorService() { +} + +} // namespace protector diff --git a/chrome/browser/protector/mock_protector_service.h b/chrome/browser/protector/mock_protector_service.h new file mode 100644 index 0000000..a32bb92 --- /dev/null +++ b/chrome/browser/protector/mock_protector_service.h @@ -0,0 +1,44 @@ +// 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. + +#ifndef CHROME_BROWSER_PROTECTOR_MOCK_PROTECTOR_SERVICE_H_ +#define CHROME_BROWSER_PROTECTOR_MOCK_PROTECTOR_SERVICE_H_ +#pragma once + +#include "chrome/browser/protector/protector_service.h" +#include "testing/gmock/include/gmock/gmock.h" + +namespace protector { + +class MockProtectorService : public ProtectorService { + public: + // Creates and returns the MockProtectorService instance associated with + // |profile|. Should be called before any calls to + // ProtectorServiceFactory::GetForProfile are made, otherwise a (non-mocked) + // ProtectorService instance will be associated with |profile|. + static MockProtectorService* BuildForProfile(Profile* profile); + + explicit MockProtectorService(Profile* profile); + virtual ~MockProtectorService(); + + MOCK_METHOD1(ShowChange, void(BaseSettingChange*)); + MOCK_CONST_METHOD0(IsShowingChange, bool()); + + MOCK_METHOD0(DismissChange, void()); + MOCK_METHOD0(ApplyChange, void()); + MOCK_METHOD0(DiscardChange, void()); + + MOCK_METHOD1(OpenTab, void(const GURL&)); + + MOCK_METHOD0(OnApplyChange, void()); + MOCK_METHOD0(OnDiscardChange, void()); + MOCK_METHOD0(OnDecisionTimeout, void()); + MOCK_METHOD0(OnRemovedFromProfile, void()); + + MOCK_METHOD0(Shutdown, void()); +}; + +} // namespace protector + +#endif // CHROME_BROWSER_PROTECTOR_MOCK_PROTECTOR_SERVICE_H_ diff --git a/chrome/browser/protector/mock_setting_change.cc b/chrome/browser/protector/mock_setting_change.cc index 63726ec..53bc9f3 100644 --- a/chrome/browser/protector/mock_setting_change.cc +++ b/chrome/browser/protector/mock_setting_change.cc @@ -30,10 +30,10 @@ MockSettingChange::MockSettingChange() { MockSettingChange::~MockSettingChange() { } -bool MockSettingChange::Init(Protector* protector) { - if (!BaseSettingChange::Init(protector)) +bool MockSettingChange::Init(Profile* profile) { + if (!BaseSettingChange::Init(profile)) return false; - return MockInit(protector); + return MockInit(profile); } } // namespace protector diff --git a/chrome/browser/protector/mock_setting_change.h b/chrome/browser/protector/mock_setting_change.h index 1d29801..3f151ea 100644 --- a/chrome/browser/protector/mock_setting_change.h +++ b/chrome/browser/protector/mock_setting_change.h @@ -17,9 +17,9 @@ class MockSettingChange : public BaseSettingChange { MockSettingChange(); virtual ~MockSettingChange(); - virtual bool Init(Protector* protector) OVERRIDE; + virtual bool Init(Profile* profile) OVERRIDE; - MOCK_METHOD1(MockInit, bool(Protector* protector)); + MOCK_METHOD1(MockInit, bool(Profile* profile)); MOCK_METHOD0(Apply, void()); MOCK_METHOD0(Discard, void()); MOCK_METHOD0(Timeout, void()); diff --git a/chrome/browser/protector/protector.cc b/chrome/browser/protector/protector.cc deleted file mode 100644 index d80ce91..0000000 --- a/chrome/browser/protector/protector.cc +++ /dev/null @@ -1,115 +0,0 @@ -// 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 "chrome/browser/protector/protector.h" - -#include "base/bind.h" -#include "base/command_line.h" -#include "base/logging.h" -#include "chrome/browser/browser_process.h" -#include "chrome/browser/prefs/pref_service.h" -#include "chrome/browser/protector/settings_change_global_error.h" -#include "chrome/browser/protector/keys.h" -#include "chrome/browser/ui/browser.h" -#include "chrome/common/chrome_notification_types.h" -#include "chrome/common/chrome_switches.h" -#include "chrome/common/pref_names.h" -#include "content/public/browser/browser_thread.h" -#include "content/public/browser/notification_source.h" -#include "crypto/hmac.h" - -using content::BrowserThread; - -namespace protector { - -Protector::Protector(Profile* profile) - : profile_(profile) { -} - -Protector::~Protector() { -} - -void Protector::OpenTab(const GURL& url) { - if (!error_.get() || !error_->browser()) { - LOG(WARNING) << "Don't have browser to show tab in."; - return; - } - error_->browser()->ShowSingletonTab(url); -} - -void Protector::ShowChange(BaseSettingChange* change) { - DCHECK(change); - change_.reset(change); - DVLOG(1) << "Init change"; - if (!change->Init(this)) { - LOG(WARNING) << "Error while initializing, removing ourselves"; - BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, this); - return; - } - error_.reset(new SettingsChangeGlobalError(change, this)); - error_->ShowForProfile(profile_); -} - -void Protector::DismissChange() { - DCHECK(error_.get()); - error_->RemoveFromProfile(); -} - -void Protector::OnApplyChange() { - DVLOG(1) << "Apply change"; - change_->Apply(); -} - -void Protector::OnDiscardChange() { - DVLOG(1) << "Discard change"; - change_->Discard(); -} - -void Protector::OnDecisionTimeout() { - DVLOG(1) << "Timeout"; - change_->Timeout(); -} - -void Protector::OnRemovedFromProfile() { - BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, this); -} - - -std::string SignSetting(const std::string& value) { - crypto::HMAC hmac(crypto::HMAC::SHA256); - if (!hmac.Init(kProtectorSigningKey)) { - LOG(WARNING) << "Failed to initialize HMAC algorithm for signing"; - return std::string(); - } - - std::vector digest(hmac.DigestLength()); - if (!hmac.Sign(value, &digest[0], digest.size())) { - LOG(WARNING) << "Failed to sign setting"; - return std::string(); - } - - return std::string(&digest[0], &digest[0] + digest.size()); -} - -bool IsSettingValid(const std::string& value, const std::string& signature) { - crypto::HMAC hmac(crypto::HMAC::SHA256); - if (!hmac.Init(kProtectorSigningKey)) { - LOG(WARNING) << "Failed to initialize HMAC algorithm for verification."; - return false; - } - return hmac.Verify(value, signature); -} - -void RegisterPrefs(PrefService* prefs) { - prefs->RegisterBooleanPref(prefs::kProtectorEnabled, false); -} - -bool IsEnabled() { - if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableProtector)) - return false; - PrefService* local_state = g_browser_process->local_state(); - return local_state->GetBoolean(prefs::kProtectorEnabled); -} - -} // namespace protector diff --git a/chrome/browser/protector/protector.h b/chrome/browser/protector/protector.h deleted file mode 100644 index 3ded4a3..0000000 --- a/chrome/browser/protector/protector.h +++ /dev/null @@ -1,86 +0,0 @@ -// 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. - -#ifndef CHROME_BROWSER_PROTECTOR_PROTECTOR_H_ -#define CHROME_BROWSER_PROTECTOR_PROTECTOR_H_ -#pragma once - -#include "base/basictypes.h" -#include "base/compiler_specific.h" -#include "base/memory/scoped_ptr.h" -#include "base/message_loop_helpers.h" -#include "chrome/browser/protector/base_setting_change.h" -#include "chrome/browser/protector/settings_change_global_error_delegate.h" - -class GURL; -class PrefService; -class Profile; -class TemplateURLService; - -namespace protector { - -class SettingsChangeGlobalError; - -// Presents a SettingChange to user and handles possible user actions. -// Deletes itself after a user action is taken or timeout expires. -class Protector : public SettingsChangeGlobalErrorDelegate { - public: - explicit Protector(Profile* profile); - - // Shows global error about the specified change. Owns |change|. - virtual void ShowChange(BaseSettingChange* change); - - // Silently discards any change previously shown (without calling Discard), - // removes global error and deletes itself. - virtual void DismissChange(); - - // Opens a tab with specified URL in the browser window we've shown error - // bubble for. - virtual void OpenTab(const GURL& url); - - // Returns the Profile instance we've shown error bubble for. - Profile* profile() { return profile_; } - - // SettingsChangeGlobalErrorDelegate implementation. - virtual void OnApplyChange() OVERRIDE; - virtual void OnDiscardChange() OVERRIDE; - virtual void OnDecisionTimeout() OVERRIDE; - virtual void OnRemovedFromProfile() OVERRIDE; - - private: - friend class base::DeleteHelper; - friend class MockProtector; - friend class ProtectorTest; - - // The object can only be allocated and destroyed on heap. - virtual ~Protector(); - - // Pointer to error bubble controller. Indicates if we're showing change - // notification to user. - scoped_ptr error_; - - // Setting change which we're showing. - scoped_ptr change_; - - // Profile which settings we are protecting. - Profile* profile_; - - DISALLOW_COPY_AND_ASSIGN(Protector); -}; - -// Signs string value with protector's key. -std::string SignSetting(const std::string& value); - -// Returns true if the signature is valid for the specified key. -bool IsSettingValid(const std::string& value, const std::string& signature); - -// Register related preferences in Local State. -void RegisterPrefs(PrefService* prefs); - -// Whether the Protector feature is enabled. -bool IsEnabled(); - -} // namespace protector - -#endif // CHROME_BROWSER_PROTECTOR_PROTECTOR_H_ diff --git a/chrome/browser/protector/protector_browsertest.cc b/chrome/browser/protector/protector_browsertest.cc deleted file mode 100644 index 32b2474..0000000 --- a/chrome/browser/protector/protector_browsertest.cc +++ /dev/null @@ -1,114 +0,0 @@ -// 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 "base/message_loop.h" -#include "chrome/app/chrome_command_ids.h" -#include "chrome/browser/protector/mock_setting_change.h" -#include "chrome/browser/protector/protector.h" -#include "chrome/browser/protector/settings_change_global_error.h" -#include "chrome/browser/ui/browser.h" -#include "chrome/browser/ui/global_error.h" -#include "chrome/browser/ui/global_error_service.h" -#include "chrome/browser/ui/global_error_service_factory.h" -#include "chrome/test/base/in_process_browser_test.h" -#include "chrome/test/base/ui_test_utils.h" - -using ::testing::Invoke; -using ::testing::NiceMock; -using ::testing::Return; - -namespace protector { - -class ProtectorTest : public InProcessBrowserTest { - public: - virtual void SetUpOnMainThread() { - // Protect owns the change and deletes himself. - protector_ = new Protector(browser()->profile()); - mock_change_ = new NiceMock(); - } - - protected: - GlobalError* GetGlobalError() { - return protector_->error_.get(); - } - - void ExpectGlobalErrorActive(bool active) { - GlobalError* error = - GlobalErrorServiceFactory::GetForProfile(browser()->profile())-> - GetGlobalErrorByMenuItemCommandID(IDC_SHOW_SETTINGS_CHANGES); - EXPECT_EQ(active, static_cast(error)); - } - - Protector* protector_; - MockSettingChange* mock_change_; -}; - -IN_PROC_BROWSER_TEST_F(ProtectorTest, ChangeInitError) { - EXPECT_CALL(*mock_change_, MockInit(protector_)).WillOnce(Return(false)); - protector_->ShowChange(mock_change_); - ExpectGlobalErrorActive(false); - ui_test_utils::RunAllPendingInMessageLoop(); - ExpectGlobalErrorActive(false); -} - -IN_PROC_BROWSER_TEST_F(ProtectorTest, ShowAndDismiss) { - EXPECT_CALL(*mock_change_, MockInit(protector_)).WillOnce(Return(true)); - protector_->ShowChange(mock_change_); - ui_test_utils::RunAllPendingInMessageLoop(); - ASSERT_TRUE(GetGlobalError()); - ExpectGlobalErrorActive(true); - protector_->DismissChange(); - ui_test_utils::RunAllPendingInMessageLoop(); - ExpectGlobalErrorActive(false); -} - -IN_PROC_BROWSER_TEST_F(ProtectorTest, ShowAndApply) { - EXPECT_CALL(*mock_change_, MockInit(protector_)).WillOnce(Return(true)); - protector_->ShowChange(mock_change_); - ui_test_utils::RunAllPendingInMessageLoop(); - GlobalError* error = GetGlobalError(); - ASSERT_TRUE(error); - ExpectGlobalErrorActive(true); - EXPECT_CALL(*mock_change_, Apply()); - // Pressing Cancel applies the change. - error->BubbleViewCancelButtonPressed(); - error->BubbleViewDidClose(); - ui_test_utils::RunAllPendingInMessageLoop(); - ExpectGlobalErrorActive(false); -} - -IN_PROC_BROWSER_TEST_F(ProtectorTest, ShowAndDiscard) { - EXPECT_CALL(*mock_change_, MockInit(protector_)).WillOnce(Return(true)); - protector_->ShowChange(mock_change_); - ui_test_utils::RunAllPendingInMessageLoop(); - GlobalError* error = GetGlobalError(); - ASSERT_TRUE(error); - ExpectGlobalErrorActive(true); - EXPECT_CALL(*mock_change_, Discard()); - // Pressing Apply discards the change. - error->BubbleViewAcceptButtonPressed(); - error->BubbleViewDidClose(); - ui_test_utils::RunAllPendingInMessageLoop(); - ExpectGlobalErrorActive(false); -} - -IN_PROC_BROWSER_TEST_F(ProtectorTest, BubbleClosedInsideApply) { - EXPECT_CALL(*mock_change_, MockInit(protector_)).WillOnce(Return(true)); - protector_->ShowChange(mock_change_); - ui_test_utils::RunAllPendingInMessageLoop(); - GlobalError* error = GetGlobalError(); - ASSERT_TRUE(error); - ExpectGlobalErrorActive(true); - EXPECT_CALL(*mock_change_, Apply()). - WillOnce(Invoke(error, &GlobalError::BubbleViewDidClose)); - // Pressing Cancel applies the change. - error->BubbleViewCancelButtonPressed(); - ui_test_utils::RunAllPendingInMessageLoop(); - ExpectGlobalErrorActive(false); -} - -// TODO(ivankr): Timeout test. - -} // namespace protector diff --git a/chrome/browser/protector/protector_service.cc b/chrome/browser/protector/protector_service.cc new file mode 100644 index 0000000..64733b0 --- /dev/null +++ b/chrome/browser/protector/protector_service.cc @@ -0,0 +1,139 @@ +// 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 "chrome/browser/protector/protector_service.h" + +#include "base/command_line.h" +#include "base/logging.h" +#include "chrome/browser/browser_process.h" +#include "chrome/browser/prefs/pref_service.h" +#include "chrome/browser/protector/settings_change_global_error.h" +#include "chrome/browser/protector/keys.h" +#include "chrome/browser/ui/browser.h" +#include "chrome/common/chrome_notification_types.h" +#include "chrome/common/chrome_switches.h" +#include "chrome/common/pref_names.h" +#include "content/public/browser/notification_source.h" +#include "crypto/hmac.h" + +namespace protector { + +ProtectorService::ProtectorService(Profile* profile) + : profile_(profile) { +} + +ProtectorService::~ProtectorService() { + DCHECK(!IsShowingChange()); // Should have been dismissed by Shutdown. +} + +void ProtectorService::ShowChange(BaseSettingChange* change) { + DCHECK(change); + change_.reset(change); + DVLOG(1) << "Init change"; + if (!change->Init(profile_)) { + LOG(WARNING) << "Error while initializing, dismissing change"; + change_.reset(); + return; + } + error_.reset(new SettingsChangeGlobalError(change, this)); + error_->ShowForProfile(profile_); +} + +bool ProtectorService::IsShowingChange() const { + return change_.get() != NULL; +} + +void ProtectorService::ApplyChange() { + DCHECK(IsShowingChange()); + change_->Apply(); + DismissChange(); +} + +void ProtectorService::DiscardChange() { + DCHECK(IsShowingChange()); + change_->Discard(); + DismissChange(); +} + +void ProtectorService::DismissChange() { + DCHECK(IsShowingChange()); + error_->RemoveFromProfile(); + DCHECK(!IsShowingChange()); +} + +void ProtectorService::OpenTab(const GURL& url) { + if (!error_.get() || !error_->browser()) { + LOG(WARNING) << "Don't have browser to show tab in."; + return; + } + error_->browser()->ShowSingletonTab(url); +} + +void ProtectorService::Shutdown() { + if (IsShowingChange()) + DismissChange(); +} + +void ProtectorService::OnApplyChange() { + DVLOG(1) << "Apply change"; + DCHECK(IsShowingChange()); + change_->Apply(); +} + +void ProtectorService::OnDiscardChange() { + DVLOG(1) << "Discard change"; + DCHECK(IsShowingChange()); + change_->Discard(); +} + +void ProtectorService::OnDecisionTimeout() { + DVLOG(1) << "Timeout"; + DCHECK(IsShowingChange()); + change_->Timeout(); +} + +void ProtectorService::OnRemovedFromProfile() { + DCHECK(IsShowingChange()); + error_.reset(); + change_.reset(); +} + + +std::string SignSetting(const std::string& value) { + crypto::HMAC hmac(crypto::HMAC::SHA256); + if (!hmac.Init(kProtectorSigningKey)) { + LOG(WARNING) << "Failed to initialize HMAC algorithm for signing"; + return std::string(); + } + + std::vector digest(hmac.DigestLength()); + if (!hmac.Sign(value, &digest[0], digest.size())) { + LOG(WARNING) << "Failed to sign setting"; + return std::string(); + } + + return std::string(&digest[0], &digest[0] + digest.size()); +} + +bool IsSettingValid(const std::string& value, const std::string& signature) { + crypto::HMAC hmac(crypto::HMAC::SHA256); + if (!hmac.Init(kProtectorSigningKey)) { + LOG(WARNING) << "Failed to initialize HMAC algorithm for verification."; + return false; + } + return hmac.Verify(value, signature); +} + +void RegisterPrefs(PrefService* prefs) { + prefs->RegisterBooleanPref(prefs::kProtectorEnabled, false); +} + +bool IsEnabled() { + if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableProtector)) + return false; + PrefService* local_state = g_browser_process->local_state(); + return local_state->GetBoolean(prefs::kProtectorEnabled); +} + +} // namespace protector diff --git a/chrome/browser/protector/protector_service.h b/chrome/browser/protector/protector_service.h new file mode 100644 index 0000000..7d56166 --- /dev/null +++ b/chrome/browser/protector/protector_service.h @@ -0,0 +1,97 @@ +// 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. + +#ifndef CHROME_BROWSER_PROTECTOR_PROTECTOR_SERVICE_H_ +#define CHROME_BROWSER_PROTECTOR_PROTECTOR_SERVICE_H_ +#pragma once + +#include "base/basictypes.h" +#include "base/compiler_specific.h" +#include "base/memory/scoped_ptr.h" +#include "base/message_loop_helpers.h" +#include "chrome/browser/profiles/profile_keyed_service.h" +#include "chrome/browser/protector/base_setting_change.h" +#include "chrome/browser/protector/settings_change_global_error_delegate.h" + +class GURL; +class PrefService; +class Profile; +class TemplateURLService; + +namespace protector { + +class SettingsChangeGlobalError; + +// Presents a SettingChange to user and handles possible user actions. +class ProtectorService : public ProfileKeyedService, + public SettingsChangeGlobalErrorDelegate { + public: + explicit ProtectorService(Profile* profile); + virtual ~ProtectorService(); + + // Shows global error about the specified change. Owns |change|. + // TODO(ivankr): handle multiple subsequent changes. + virtual void ShowChange(BaseSettingChange* change); + + // Returns |true| if a change is currently active (shown by a ShowChange call + // and not yet applied or discarded). + virtual bool IsShowingChange() const; + + // Removes global error (including the bubbble if one is shown) and deletes + // the change instance (without calling Apply or Discard on it). + virtual void DismissChange(); + + // Persists the change that is currently active and removes global error. + virtual void ApplyChange(); + + // Discards the change that is currently active and removes global error. + virtual void DiscardChange(); + + // Opens a tab with specified URL in the browser window we've shown error + // bubble for. + virtual void OpenTab(const GURL& url); + + // Returns the Profile instance we've shown error bubble for. + Profile* profile() { return profile_; } + + private: + friend class ProtectorServiceTest; + + // ProfileKeyedService implementation. + virtual void Shutdown() OVERRIDE; + + // SettingsChangeGlobalErrorDelegate implementation. + virtual void OnApplyChange() OVERRIDE; + virtual void OnDiscardChange() OVERRIDE; + virtual void OnDecisionTimeout() OVERRIDE; + virtual void OnRemovedFromProfile() OVERRIDE; + + // Pointer to error bubble controller. Indicates if we're showing change + // notification to user. + scoped_ptr error_; + + // Setting change which we're showing. + scoped_ptr change_; + + // Profile which settings we are protecting. + Profile* profile_; + + DISALLOW_COPY_AND_ASSIGN(ProtectorService); +}; + +// Signs string value with protector's key. +std::string SignSetting(const std::string& value); + +// Returns true if the signature is valid for the specified key. +bool IsSettingValid(const std::string& value, const std::string& signature); + +// Register related preferences in Local State. +void RegisterPrefs(PrefService* prefs); + +// Whether the Protector feature is enabled. +bool IsEnabled(); + +} // namespace protector + +#endif // CHROME_BROWSER_PROTECTOR_PROTECTOR_SERVICE_H_ diff --git a/chrome/browser/protector/protector_service_browsertest.cc b/chrome/browser/protector/protector_service_browsertest.cc new file mode 100644 index 0000000..951dabb --- /dev/null +++ b/chrome/browser/protector/protector_service_browsertest.cc @@ -0,0 +1,149 @@ +// 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 "base/message_loop.h" +#include "chrome/app/chrome_command_ids.h" +#include "chrome/browser/protector/mock_setting_change.h" +#include "chrome/browser/protector/protector_service.h" +#include "chrome/browser/protector/protector_service_factory.h" +#include "chrome/browser/protector/settings_change_global_error.h" +#include "chrome/browser/ui/browser.h" +#include "chrome/browser/ui/global_error.h" +#include "chrome/browser/ui/global_error_service.h" +#include "chrome/browser/ui/global_error_service_factory.h" +#include "chrome/test/base/in_process_browser_test.h" +#include "chrome/test/base/ui_test_utils.h" + +using ::testing::Invoke; +using ::testing::NiceMock; +using ::testing::Return; + +namespace protector { + +class ProtectorServiceTest : public InProcessBrowserTest { + public: + virtual void SetUpOnMainThread() { + protector_service_ = + ProtectorServiceFactory::GetForProfile(browser()->profile()); + // ProtectService will own this change instance. + mock_change_ = new NiceMock(); + } + + protected: + GlobalError* GetGlobalError() { + return protector_service_->error_.get(); + } + + void ExpectGlobalErrorActive(bool active) { + GlobalError* error = + GlobalErrorServiceFactory::GetForProfile(browser()->profile())-> + GetGlobalErrorByMenuItemCommandID(IDC_SHOW_SETTINGS_CHANGES); + EXPECT_EQ(active, error != NULL); + EXPECT_EQ(active, GetGlobalError() != NULL); + EXPECT_EQ(active, protector_service_->IsShowingChange()); + } + + ProtectorService* protector_service_; + MockSettingChange* mock_change_; +}; + +IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ChangeInitError) { + // Init fails and causes the change to be ignored. + EXPECT_CALL(*mock_change_, MockInit(browser()->profile())). + WillOnce(Return(false)); + protector_service_->ShowChange(mock_change_); + ExpectGlobalErrorActive(false); + ui_test_utils::RunAllPendingInMessageLoop(); + ExpectGlobalErrorActive(false); +} + +IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowAndDismiss) { + // Show the change and immediately dismiss it. + EXPECT_CALL(*mock_change_, MockInit(browser()->profile())). + WillOnce(Return(true)); + protector_service_->ShowChange(mock_change_); + ui_test_utils::RunAllPendingInMessageLoop(); + ExpectGlobalErrorActive(true); + protector_service_->DismissChange(); + ui_test_utils::RunAllPendingInMessageLoop(); + ExpectGlobalErrorActive(false); +} + +IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowAndApply) { + // Show the change and apply it. + EXPECT_CALL(*mock_change_, MockInit(browser()->profile())). + WillOnce(Return(true)); + protector_service_->ShowChange(mock_change_); + ui_test_utils::RunAllPendingInMessageLoop(); + ExpectGlobalErrorActive(true); + EXPECT_CALL(*mock_change_, Apply()); + protector_service_->ApplyChange(); + ui_test_utils::RunAllPendingInMessageLoop(); + ExpectGlobalErrorActive(false); +} + +IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowAndApplyManually) { + // Show the change and apply it, mimicking a button click. + EXPECT_CALL(*mock_change_, MockInit(browser()->profile())). + WillOnce(Return(true)); + protector_service_->ShowChange(mock_change_); + ui_test_utils::RunAllPendingInMessageLoop(); + ExpectGlobalErrorActive(true); + EXPECT_CALL(*mock_change_, Apply()); + // Pressing Cancel applies the change. + GlobalError* error = GetGlobalError(); + error->BubbleViewCancelButtonPressed(); + error->BubbleViewDidClose(); + ui_test_utils::RunAllPendingInMessageLoop(); + ExpectGlobalErrorActive(false); +} + +IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowAndDiscard) { + // Show the change and discard it. + EXPECT_CALL(*mock_change_, MockInit(browser()->profile())). + WillOnce(Return(true)); + protector_service_->ShowChange(mock_change_); + ui_test_utils::RunAllPendingInMessageLoop(); + ExpectGlobalErrorActive(true); + EXPECT_CALL(*mock_change_, Discard()); + protector_service_->DiscardChange(); + ui_test_utils::RunAllPendingInMessageLoop(); + ExpectGlobalErrorActive(false); +} + +IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowAndDiscardManually) { + // Show the change and discard it, mimicking a button click. + EXPECT_CALL(*mock_change_, MockInit(browser()->profile())). + WillOnce(Return(true)); + protector_service_->ShowChange(mock_change_); + ui_test_utils::RunAllPendingInMessageLoop(); + ExpectGlobalErrorActive(true); + EXPECT_CALL(*mock_change_, Discard()); + // Pressing Apply discards the change. + GlobalError* error = GetGlobalError(); + error->BubbleViewAcceptButtonPressed(); + error->BubbleViewDidClose(); + ui_test_utils::RunAllPendingInMessageLoop(); + ExpectGlobalErrorActive(false); +} + +IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, BubbleClosedInsideApply) { + EXPECT_CALL(*mock_change_, MockInit(browser()->profile())). + WillOnce(Return(true)); + protector_service_->ShowChange(mock_change_); + ui_test_utils::RunAllPendingInMessageLoop(); + ExpectGlobalErrorActive(true); + GlobalError* error = GetGlobalError(); + EXPECT_CALL(*mock_change_, Apply()). + WillOnce(Invoke(error, &GlobalError::BubbleViewDidClose)); + // Pressing Cancel applies the change. + error->BubbleViewCancelButtonPressed(); + ui_test_utils::RunAllPendingInMessageLoop(); + ExpectGlobalErrorActive(false); +} + +// TODO(ivankr): Timeout test. + +} // namespace protector diff --git a/chrome/browser/protector/protector_service_factory.cc b/chrome/browser/protector/protector_service_factory.cc new file mode 100644 index 0000000..c11cbdc --- /dev/null +++ b/chrome/browser/protector/protector_service_factory.cc @@ -0,0 +1,43 @@ +// 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 "chrome/browser/protector/protector_service_factory.h" + +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/profiles/profile_dependency_manager.h" +#include "chrome/browser/protector/protector_service.h" +#include "chrome/browser/search_engines/template_url_service_factory.h" + +namespace protector { + +// static +ProtectorService* ProtectorServiceFactory::GetForProfile(Profile* profile) { + return static_cast( + GetInstance()->GetServiceForProfile(profile, true)); +} + +// static +ProtectorServiceFactory* ProtectorServiceFactory::GetInstance() { + return Singleton::get(); +} + +ProtectorServiceFactory::ProtectorServiceFactory() + : ProfileKeyedServiceFactory(ProfileDependencyManager::GetInstance()) { + DependsOn(GlobalErrorServiceFactory::GetInstance()); + DependsOn(TemplateURLServiceFactory::GetInstance()); +} + +ProtectorServiceFactory::~ProtectorServiceFactory() { +} + +ProfileKeyedService* ProtectorServiceFactory::BuildServiceInstanceFor( + Profile* profile) const { + return new ProtectorService(profile); +} + +bool ProtectorServiceFactory::ServiceRedirectedInIncognito() { + return true; +} + +} // namespace protector diff --git a/chrome/browser/protector/protector_service_factory.h b/chrome/browser/protector/protector_service_factory.h new file mode 100644 index 0000000..e78186a --- /dev/null +++ b/chrome/browser/protector/protector_service_factory.h @@ -0,0 +1,41 @@ +// 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. + +#ifndef CHROME_BROWSER_PROTECTOR_PROTECTOR_SERVICE_FACTORY_H_ +#define CHROME_BROWSER_PROTECTOR_PROTECTOR_SERVICE_FACTORY_H_ +#pragma once + +#include "base/basictypes.h" +#include "base/memory/singleton.h" +#include "chrome/browser/profiles/profile_keyed_service_factory.h" +#include "chrome/browser/ui/global_error_service_factory.h" + +namespace protector { + +class ProtectorService; + +class ProtectorServiceFactory : public ProfileKeyedServiceFactory { + public: + // Returns the ProtectorService instance for |profile|. + static ProtectorService* GetForProfile(Profile* profile); + + static ProtectorServiceFactory* GetInstance(); + + private: + friend struct DefaultSingletonTraits; + + ProtectorServiceFactory(); + virtual ~ProtectorServiceFactory(); + + // ProfileKeyedServiceFactory implementation. + virtual ProfileKeyedService* BuildServiceInstanceFor( + Profile* profile) const OVERRIDE; + virtual bool ServiceRedirectedInIncognito() OVERRIDE; + + DISALLOW_COPY_AND_ASSIGN(ProtectorServiceFactory); +}; + +} // namespace protector + +#endif // CHROME_BROWSER_PROTECTOR_PROTECTOR_SERVICE_FACTORY_H_ diff --git a/chrome/browser/protector/settings_change_global_error.cc b/chrome/browser/protector/settings_change_global_error.cc index 5b57584..4d22142 100644 --- a/chrome/browser/protector/settings_change_global_error.cc +++ b/chrome/browser/protector/settings_change_global_error.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// 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. @@ -116,6 +116,7 @@ void SettingsChangeGlobalError::BubbleViewCancelButtonPressed() { void SettingsChangeGlobalError::RemoveFromProfile() { if (profile_) GlobalErrorServiceFactory::GetForProfile(profile_)->RemoveGlobalError(this); + // This will delete |this|. delegate_->OnRemovedFromProfile(); } diff --git a/chrome/browser/search_engines/template_url_service.cc b/chrome/browser/search_engines/template_url_service.cc index 79677c5..d61aa68 100644 --- a/chrome/browser/search_engines/template_url_service.cc +++ b/chrome/browser/search_engines/template_url_service.cc @@ -23,7 +23,8 @@ #include "chrome/browser/prefs/pref_set_observer.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/protector/base_setting_change.h" -#include "chrome/browser/protector/protector.h" +#include "chrome/browser/protector/protector_service.h" +#include "chrome/browser/protector/protector_service_factory.h" #include "chrome/browser/rlz/rlz.h" #include "chrome/browser/search_engines/search_host_to_urls_map.h" #include "chrome/browser/search_engines/search_terms_data.h" @@ -624,11 +625,13 @@ void TemplateURLService::OnWebDataServiceRequestDone( if (is_default_search_hijacked && default_search_provider_ == hijacked_default_search_provider) { if (protector::IsEnabled()) { - // Protector instance will delete itself when it's needed no longer. - protector::Protector* protector = new protector::Protector(profile()); - protector->ShowChange(protector::CreateDefaultSearchProviderChange( - hijacked_default_search_provider, - backup_default_search_provider.release())); + protector::ProtectorService* protector_service = + protector::ProtectorServiceFactory::GetForProfile(profile()); + DCHECK(protector_service); + protector_service->ShowChange( + protector::CreateDefaultSearchProviderChange( + hijacked_default_search_provider, + backup_default_search_provider.release())); } else { // Protector is turned off: set the current default search to itself // to update the backup and sign it. Otherwise, change will be reported diff --git a/chrome/browser/webdata/keyword_table.cc b/chrome/browser/webdata/keyword_table.cc index beefc7b..0caf367 100644 --- a/chrome/browser/webdata/keyword_table.cc +++ b/chrome/browser/webdata/keyword_table.cc @@ -15,7 +15,7 @@ #include "base/utf_string_conversions.h" #include "chrome/browser/history/history_database.h" #include "chrome/browser/protector/histograms.h" -#include "chrome/browser/protector/protector.h" +#include "chrome/browser/protector/protector_service.h" #include "chrome/browser/search_engines/template_url.h" #include "googleurl/src/gurl.h" #include "sql/statement.h" diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 80cbb84..565cbb2 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -2047,8 +2047,10 @@ 'browser/protector/internal/keys_internal.cc', 'browser/protector/keys.cc', 'browser/protector/keys.h', - 'browser/protector/protector.cc', - 'browser/protector/protector.h', + 'browser/protector/protector_service.cc', + 'browser/protector/protector_service.h', + 'browser/protector/protector_service_factory.cc', + 'browser/protector/protector_service_factory.h', 'browser/protector/settings_change_global_error.cc', 'browser/protector/settings_change_global_error.h', 'browser/protector/settings_change_global_error_delegate.h', diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index e640f4f..a92cd57 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -146,8 +146,8 @@ 'browser/prefs/pref_service_mock_builder.h', 'browser/prefs/testing_pref_store.cc', 'browser/prefs/testing_pref_store.h', - 'browser/protector/mock_protector.cc', - 'browser/protector/mock_protector.h', + 'browser/protector/mock_protector_service.cc', + 'browser/protector/mock_protector_service.h', 'browser/protector/mock_setting_change.cc', 'browser/protector/mock_setting_change.h', 'browser/search_engines/template_url_service_test_util.cc', @@ -2682,7 +2682,7 @@ 'browser/printing/print_preview_tab_controller_browsertest.cc', 'browser/profiles/profile_manager_browsertest.cc', 'browser/protector/default_search_provider_change_browsertest.cc', - 'browser/protector/protector_browsertest.cc', + 'browser/protector/protector_service_browsertest.cc', 'browser/rlz/rlz_extension_apitest.cc', 'browser/referrer_policy_browsertest.cc', 'browser/renderer_host/render_process_host_chrome_browsertest.cc', @@ -3057,8 +3057,8 @@ '../content/renderer/external_popup_menu_browsertest.cc', ], 'sources!': [ - # TODO(sail): re-enable this once http://crbug.com/109728 is fixed. - 'browser/protector/protector_browsertest.cc', + # TODO(ivankr): enable this once http://crbug.com/109728 is fixed. + 'browser/protector/protector_service_browsertest.cc', # TODO(hbono): This test depends on hunspell and we cannot run it on # Mac, which does not use hunspell by default. 'browser/spellchecker/spellcheck_host_browsertest.cc', -- cgit v1.1