summaryrefslogtreecommitdiffstats
path: root/chrome/browser/protector/default_search_provider_change.cc
diff options
context:
space:
mode:
authorivankr@chromium.org <ivankr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-11-21 16:05:37 +0000
committerivankr@chromium.org <ivankr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-11-21 16:05:37 +0000
commit629918536d3620cb264f53bfb2992b8baca407a5 (patch)
treefa897c1af9630b238ec514f0334c3357acac7497 /chrome/browser/protector/default_search_provider_change.cc
parentdc8532ac514bdda1baa24c0047bf17ac1296730a (diff)
downloadchromium_src-629918536d3620cb264f53bfb2992b8baca407a5.zip
chromium_src-629918536d3620cb264f53bfb2992b8baca407a5.tar.gz
chromium_src-629918536d3620cb264f53bfb2992b8baca407a5.tar.bz2
Protector bubble cancels itself if user changes default search engine manually.
BUG=None TEST=Manual: trigger protector bubble, change search engine manually; bubble should vanish. Review URL: http://codereview.chromium.org/8612002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@110933 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/protector/default_search_provider_change.cc')
-rw-r--r--chrome/browser/protector/default_search_provider_change.cc83
1 files changed, 56 insertions, 27 deletions
diff --git a/chrome/browser/protector/default_search_provider_change.cc b/chrome/browser/protector/default_search_provider_change.cc
index db37e27..026788d 100644
--- a/chrome/browser/protector/default_search_provider_change.cc
+++ b/chrome/browser/protector/default_search_provider_change.cc
@@ -9,6 +9,7 @@
#include "chrome/browser/protector/protector.h"
#include "chrome/browser/search_engines/template_url.h"
#include "chrome/browser/search_engines/template_url_service.h"
+#include "chrome/browser/search_engines/template_url_service_observer.h"
#include "chrome/browser/webdata/keyword_table.h"
#include "chrome/common/url_constants.h"
#include "grit/chromium_strings.h"
@@ -25,33 +26,37 @@ const size_t kMaxDisplayedNameLength = 10;
} // namespace
-class DefaultSearchProviderChange : public BaseSettingChange {
+class DefaultSearchProviderChange : public BaseSettingChange,
+ public TemplateURLServiceObserver {
public:
DefaultSearchProviderChange(const TemplateURL* old_url,
const TemplateURL* new_url);
// BaseSettingChange overrides:
virtual bool Init(Protector* protector) OVERRIDE;
- virtual void Apply(Protector* protector) OVERRIDE;
- virtual void Discard(Protector* protector) OVERRIDE;
+ virtual void Apply() OVERRIDE;
+ virtual void Discard() OVERRIDE;
+ virtual void OnBeforeRemoved() OVERRIDE;
virtual string16 GetBubbleTitle() const OVERRIDE;
virtual string16 GetBubbleMessage() const OVERRIDE;
virtual string16 GetApplyButtonText() const OVERRIDE;
virtual string16 GetDiscardButtonText() const OVERRIDE;
+ // TemplateURLServiceObserver overrides:
+ virtual void OnTemplateURLServiceChanged() OVERRIDE;
+
private:
virtual ~DefaultSearchProviderChange();
- // Sets the given default search provider to profile that |protector| is
- // guarding. Returns the |TemplateURL| instance the default search provider
- // has been set to. If no search provider with |id| exists and
- // |allow_fallback| is true, sets one of the prepoluated search providers.
- const TemplateURL* SetDefaultSearchProvider(Protector* protector,
- int64 id,
+ // Sets the given default search provider to profile that this change is
+ // related to. Returns the |TemplateURL| instance of the new default search
+ // provider. If no search provider with |id| exists and |allow_fallback| is
+ // true, sets one of the prepopulated search providers.
+ const TemplateURL* SetDefaultSearchProvider(int64 id,
bool allow_fallback);
// Opens the Search engine settings page in a new tab.
- void OpenSearchEngineSettings(Protector* protector);
+ void OpenSearchEngineSettings();
int64 old_id_;
int64 new_id_;
@@ -62,6 +67,12 @@ class DefaultSearchProviderChange : public BaseSettingChange {
// Name of the search engine that we fall back to if the backup is lost.
string16 fallback_name_;
string16 product_name_;
+ // Default search provider set by |Init| for the period until user makes a
+ // choice and either |Apply| or |Discard| is performed. Should only be used
+ // for comparison with the current default search provider and never
+ // dereferenced other than in |Init| because it may be deallocated by
+ // TemplateURLService at any time.
+ const TemplateURL* default_search_provider_;
DISALLOW_COPY_AND_ASSIGN(DefaultSearchProviderChange);
};
@@ -72,7 +83,8 @@ DefaultSearchProviderChange::DefaultSearchProviderChange(
: old_id_(0),
new_id_(0),
fallback_id_(0),
- product_name_(l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)) {
+ product_name_(l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)),
+ default_search_provider_(NULL) {
if (new_url) {
new_id_ = new_url->id();
new_name_ = new_url->short_name();
@@ -87,41 +99,50 @@ DefaultSearchProviderChange::~DefaultSearchProviderChange() {
}
bool DefaultSearchProviderChange::Init(Protector* protector) {
+ BaseSettingChange::Init(protector);
+
// Initially reset the search engine to its previous setting.
- const TemplateURL* current_url =
- SetDefaultSearchProvider(protector, old_id_, true);
- if (!current_url)
+ default_search_provider_ = SetDefaultSearchProvider(old_id_, true);
+ if (!default_search_provider_)
return false;
- if (!old_id_ || current_url->id() != old_id_) {
+
+ if (!old_id_ || default_search_provider_->id() != old_id_) {
// Old settings is lost or invalid, so we had to fall back to one of the
// prepopulated search engines.
- fallback_id_ = current_url->id();
- fallback_name_ = current_url->short_name();
+ fallback_id_ = default_search_provider_->id();
+ fallback_name_ = default_search_provider_->short_name();
VLOG(1) << "Fallback to " << fallback_name_;
}
+
+ protector->GetTemplateURLService()->AddObserver(this);
+
return true;
}
-void DefaultSearchProviderChange::Apply(Protector* protector) {
+void DefaultSearchProviderChange::Apply() {
// TODO(avayvod): Add histrogram.
if (!new_id_) {
// Open settings page in case the new setting is invalid.
- OpenSearchEngineSettings(protector);
+ OpenSearchEngineSettings();
} else {
- SetDefaultSearchProvider(protector, new_id_, false);
+ SetDefaultSearchProvider(new_id_, false);
}
}
-void DefaultSearchProviderChange::Discard(Protector* protector) {
+void DefaultSearchProviderChange::Discard() {
// TODO(avayvod): Add histrogram.
if (!old_id_) {
// Open settings page in case the old setting is invalid.
- OpenSearchEngineSettings(protector);
+ OpenSearchEngineSettings();
}
// Nothing to do otherwise since we have already set the search engine
// to |old_id_| in |Init|.
}
+void DefaultSearchProviderChange::OnBeforeRemoved() {
+ protector()->GetTemplateURLService()->RemoveObserver(this);
+}
+
string16 DefaultSearchProviderChange::GetBubbleTitle() const {
return l10n_util::GetStringUTF16(IDS_SEARCH_ENGINE_CHANGE_TITLE);
}
@@ -168,11 +189,20 @@ string16 DefaultSearchProviderChange::GetDiscardButtonText() const {
}
}
+void DefaultSearchProviderChange::OnTemplateURLServiceChanged() {
+ if (protector()->GetTemplateURLService()->GetDefaultSearchProvider() !=
+ default_search_provider_) {
+ default_search_provider_ = NULL;
+ VLOG(1) << "Default search provider has been changed by user";
+ // This will delete the Protector instance and |this|.
+ protector()->DismissChange();
+ }
+}
+
const TemplateURL* DefaultSearchProviderChange::SetDefaultSearchProvider(
- Protector* protector,
int64 id,
bool allow_fallback) {
- TemplateURLService* url_service = protector->GetTemplateURLService();
+ TemplateURLService* url_service = protector()->GetTemplateURLService();
if (!url_service) {
NOTREACHED() << "Can't get TemplateURLService object.";
return NULL;
@@ -199,9 +229,8 @@ const TemplateURL* DefaultSearchProviderChange::SetDefaultSearchProvider(
return url;
}
-void DefaultSearchProviderChange::OpenSearchEngineSettings(
- Protector* protector) {
- protector->OpenTab(
+void DefaultSearchProviderChange::OpenSearchEngineSettings() {
+ protector()->OpenTab(
GURL(std::string(chrome::kChromeUISettingsURL) +
chrome::kSearchEnginesSubPage));
}