Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Side by Side Diff: chrome/browser/protector/default_search_provider_change.cc

Issue 8612002: Protector bubble cancels itself if user changes default search engine manually. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/compiler_specific.h" 6 #include "base/compiler_specific.h"
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "chrome/browser/protector/base_setting_change.h" 8 #include "chrome/browser/protector/base_setting_change.h"
9 #include "chrome/browser/protector/protector.h" 9 #include "chrome/browser/protector/protector.h"
10 #include "chrome/browser/search_engines/template_url.h" 10 #include "chrome/browser/search_engines/template_url.h"
11 #include "chrome/browser/search_engines/template_url_service.h" 11 #include "chrome/browser/search_engines/template_url_service.h"
12 #include "chrome/browser/search_engines/template_url_service_observer.h"
12 #include "chrome/browser/webdata/keyword_table.h" 13 #include "chrome/browser/webdata/keyword_table.h"
13 #include "chrome/common/url_constants.h" 14 #include "chrome/common/url_constants.h"
14 #include "grit/chromium_strings.h" 15 #include "grit/chromium_strings.h"
15 #include "grit/generated_resources.h" 16 #include "grit/generated_resources.h"
16 #include "googleurl/src/gurl.h" 17 #include "googleurl/src/gurl.h"
17 #include "ui/base/l10n/l10n_util.h" 18 #include "ui/base/l10n/l10n_util.h"
18 19
19 namespace protector { 20 namespace protector {
20 21
21 namespace { 22 namespace {
22 23
23 // Maximum length of the search engine name to be displayed. 24 // Maximum length of the search engine name to be displayed.
24 const size_t kMaxDisplayedNameLength = 10; 25 const size_t kMaxDisplayedNameLength = 10;
25 26
26 } // namespace 27 } // namespace
27 28
28 class DefaultSearchProviderChange : public BaseSettingChange { 29 class DefaultSearchProviderChange : public BaseSettingChange,
30 public TemplateURLServiceObserver {
29 public: 31 public:
30 DefaultSearchProviderChange(const TemplateURL* old_url, 32 DefaultSearchProviderChange(const TemplateURL* old_url,
31 const TemplateURL* new_url); 33 const TemplateURL* new_url);
32 34
33 // BaseSettingChange overrides: 35 // BaseSettingChange overrides:
34 virtual bool Init(Protector* protector) OVERRIDE; 36 virtual bool Init(Protector* protector) OVERRIDE;
35 virtual void Apply(Protector* protector) OVERRIDE; 37 virtual void Apply() OVERRIDE;
36 virtual void Discard(Protector* protector) OVERRIDE; 38 virtual void Discard() OVERRIDE;
39 virtual void OnBeforeRemoved() OVERRIDE;
37 virtual string16 GetBubbleTitle() const OVERRIDE; 40 virtual string16 GetBubbleTitle() const OVERRIDE;
38 virtual string16 GetBubbleMessage() const OVERRIDE; 41 virtual string16 GetBubbleMessage() const OVERRIDE;
39 virtual string16 GetApplyButtonText() const OVERRIDE; 42 virtual string16 GetApplyButtonText() const OVERRIDE;
40 virtual string16 GetDiscardButtonText() const OVERRIDE; 43 virtual string16 GetDiscardButtonText() const OVERRIDE;
41 44
45 // TemplateURLServiceObserver overrides:
46 virtual void OnTemplateURLServiceChanged() OVERRIDE;
47
42 private: 48 private:
43 virtual ~DefaultSearchProviderChange(); 49 virtual ~DefaultSearchProviderChange();
44 50
45 // Sets the given default search provider to profile that |protector| is 51 // Sets the given default search provider to profile that this change is
46 // guarding. Returns the |TemplateURL| instance the default search provider 52 // related to. Returns the |TemplateURL| instance of the new default search
47 // has been set to. If no search provider with |id| exists and 53 // provider. If no search provider with |id| exists and |allow_fallback| is
48 // |allow_fallback| is true, sets one of the prepoluated search providers. 54 // true, sets one of the prepopulated search providers.
49 const TemplateURL* SetDefaultSearchProvider(Protector* protector, 55 const TemplateURL* SetDefaultSearchProvider(int64 id,
50 int64 id,
51 bool allow_fallback); 56 bool allow_fallback);
52 57
53 // Opens the Search engine settings page in a new tab. 58 // Opens the Search engine settings page in a new tab.
54 void OpenSearchEngineSettings(Protector* protector); 59 void OpenSearchEngineSettings();
55 60
56 int64 old_id_; 61 int64 old_id_;
57 int64 new_id_; 62 int64 new_id_;
58 // ID of the search engine that we fall back to if the backup is lost. 63 // ID of the search engine that we fall back to if the backup is lost.
59 int64 fallback_id_; 64 int64 fallback_id_;
60 string16 old_name_; 65 string16 old_name_;
61 string16 new_name_; 66 string16 new_name_;
62 // Name of the search engine that we fall back to if the backup is lost. 67 // Name of the search engine that we fall back to if the backup is lost.
63 string16 fallback_name_; 68 string16 fallback_name_;
64 string16 product_name_; 69 string16 product_name_;
70 // Default search provider set by |Init| for the period until user makes
71 // a choice and either |Apply| or |Discard| is performed.
72 const TemplateURL* default_search_provider_;
65 73
66 DISALLOW_COPY_AND_ASSIGN(DefaultSearchProviderChange); 74 DISALLOW_COPY_AND_ASSIGN(DefaultSearchProviderChange);
67 }; 75 };
68 76
69 DefaultSearchProviderChange::DefaultSearchProviderChange( 77 DefaultSearchProviderChange::DefaultSearchProviderChange(
70 const TemplateURL* old_url, 78 const TemplateURL* old_url,
71 const TemplateURL* new_url) 79 const TemplateURL* new_url)
72 : old_id_(0), 80 : old_id_(0),
73 new_id_(0), 81 new_id_(0),
74 fallback_id_(0), 82 fallback_id_(0),
75 product_name_(l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)) { 83 product_name_(l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)),
84 default_search_provider_(NULL) {
76 if (new_url) { 85 if (new_url) {
77 new_id_ = new_url->id(); 86 new_id_ = new_url->id();
78 new_name_ = new_url->short_name(); 87 new_name_ = new_url->short_name();
79 } 88 }
80 if (old_url) { 89 if (old_url) {
81 old_id_ = old_url->id(); 90 old_id_ = old_url->id();
82 old_name_ = old_url->short_name(); 91 old_name_ = old_url->short_name();
83 } 92 }
84 } 93 }
85 94
86 DefaultSearchProviderChange::~DefaultSearchProviderChange() { 95 DefaultSearchProviderChange::~DefaultSearchProviderChange() {
87 } 96 }
88 97
89 bool DefaultSearchProviderChange::Init(Protector* protector) { 98 bool DefaultSearchProviderChange::Init(Protector* protector) {
99 BaseSettingChange::Init(protector);
100
90 // Initially reset the search engine to its previous setting. 101 // Initially reset the search engine to its previous setting.
91 const TemplateURL* current_url = 102 default_search_provider_ = SetDefaultSearchProvider(old_id_, true);
92 SetDefaultSearchProvider(protector, old_id_, true); 103 if (!default_search_provider_)
93 if (!current_url)
94 return false; 104 return false;
95 if (!old_id_ || current_url->id() != old_id_) { 105
106 if (!old_id_ || default_search_provider_->id() != old_id_) {
96 // Old settings is lost or invalid, so we had to fall back to one of the 107 // Old settings is lost or invalid, so we had to fall back to one of the
97 // prepopulated search engines. 108 // prepopulated search engines.
98 fallback_id_ = current_url->id(); 109 fallback_id_ = default_search_provider_->id();
99 fallback_name_ = current_url->short_name(); 110 fallback_name_ = default_search_provider_->short_name();
100 VLOG(1) << "Fallback to " << fallback_name_; 111 VLOG(1) << "Fallback to " << fallback_name_;
101 } 112 }
113
114 protector->GetTemplateURLService()->AddObserver(this);
115
102 return true; 116 return true;
103 } 117 }
104 118
105 void DefaultSearchProviderChange::Apply(Protector* protector) { 119 void DefaultSearchProviderChange::Apply() {
106 // TODO(avayvod): Add histrogram. 120 // TODO(avayvod): Add histrogram.
107 if (!new_id_) { 121 if (!new_id_) {
108 // Open settings page in case the new setting is invalid. 122 // Open settings page in case the new setting is invalid.
109 OpenSearchEngineSettings(protector); 123 OpenSearchEngineSettings();
110 } else { 124 } else {
111 SetDefaultSearchProvider(protector, new_id_, false); 125 SetDefaultSearchProvider(new_id_, false);
112 } 126 }
113 } 127 }
114 128
115 void DefaultSearchProviderChange::Discard(Protector* protector) { 129 void DefaultSearchProviderChange::Discard() {
116 // TODO(avayvod): Add histrogram. 130 // TODO(avayvod): Add histrogram.
117 if (!old_id_) { 131 if (!old_id_) {
118 // Open settings page in case the old setting is invalid. 132 // Open settings page in case the old setting is invalid.
119 OpenSearchEngineSettings(protector); 133 OpenSearchEngineSettings();
120 } 134 }
121 // Nothing to do otherwise since we have already set the search engine 135 // Nothing to do otherwise since we have already set the search engine
122 // to |old_id_| in |Init|. 136 // to |old_id_| in |Init|.
123 } 137 }
124 138
139 void DefaultSearchProviderChange::OnBeforeRemoved() {
140 protector()->GetTemplateURLService()->RemoveObserver(this);
141 }
142
125 string16 DefaultSearchProviderChange::GetBubbleTitle() const { 143 string16 DefaultSearchProviderChange::GetBubbleTitle() const {
126 return l10n_util::GetStringUTF16(IDS_SEARCH_ENGINE_CHANGE_TITLE); 144 return l10n_util::GetStringUTF16(IDS_SEARCH_ENGINE_CHANGE_TITLE);
127 } 145 }
128 146
129 string16 DefaultSearchProviderChange::GetBubbleMessage() const { 147 string16 DefaultSearchProviderChange::GetBubbleMessage() const {
130 if (fallback_name_.empty()) 148 if (fallback_name_.empty())
131 return l10n_util::GetStringFUTF16( 149 return l10n_util::GetStringFUTF16(
132 IDS_SEARCH_ENGINE_CHANGE_MESSAGE, product_name_); 150 IDS_SEARCH_ENGINE_CHANGE_MESSAGE, product_name_);
133 else 151 else
134 return l10n_util::GetStringFUTF16( 152 return l10n_util::GetStringFUTF16(
(...skipping 26 matching lines...) Expand all
161 if (new_name_.length() > kMaxDisplayedNameLength) 179 if (new_name_.length() > kMaxDisplayedNameLength)
162 return l10n_util::GetStringUTF16(IDS_KEEP_SETTING); 180 return l10n_util::GetStringUTF16(IDS_KEEP_SETTING);
163 else 181 else
164 return l10n_util::GetStringFUTF16(IDS_KEEP_SEARCH_ENGINE, old_name_); 182 return l10n_util::GetStringFUTF16(IDS_KEEP_SEARCH_ENGINE, old_name_);
165 } else { 183 } else {
166 // Old setting is lost, offer to go to settings. 184 // Old setting is lost, offer to go to settings.
167 return l10n_util::GetStringUTF16(IDS_SELECT_SEARCH_ENGINE); 185 return l10n_util::GetStringUTF16(IDS_SELECT_SEARCH_ENGINE);
168 } 186 }
169 } 187 }
170 188
189 void DefaultSearchProviderChange::OnTemplateURLServiceChanged() {
190 if (protector()->GetTemplateURLService()->GetDefaultSearchProvider() !=
191 default_search_provider_) {
whywhat 2011/11/21 14:51:21 Note: at this moment default_search_provider_ may
Ivan Korotkov 2011/11/21 15:27:38 Nice catch, thanks.
192 VLOG(1) << "Default search provider has been changed by user";
193 // This will delete the Protector instance and |this|.
194 protector()->DismissChange();
195 }
196 }
197
171 const TemplateURL* DefaultSearchProviderChange::SetDefaultSearchProvider( 198 const TemplateURL* DefaultSearchProviderChange::SetDefaultSearchProvider(
172 Protector* protector,
173 int64 id, 199 int64 id,
174 bool allow_fallback) { 200 bool allow_fallback) {
175 TemplateURLService* url_service = protector->GetTemplateURLService(); 201 TemplateURLService* url_service = protector()->GetTemplateURLService();
176 if (!url_service) { 202 if (!url_service) {
177 NOTREACHED() << "Can't get TemplateURLService object."; 203 NOTREACHED() << "Can't get TemplateURLService object.";
178 return NULL; 204 return NULL;
179 } 205 }
180 const TemplateURL* url = NULL; 206 const TemplateURL* url = NULL;
181 if (id) { 207 if (id) {
182 const TemplateURLService::TemplateURLVector& urls = 208 const TemplateURLService::TemplateURLVector& urls =
183 url_service->GetTemplateURLs(); 209 url_service->GetTemplateURLs();
184 for (size_t i = 0; i < urls.size(); ++i) { 210 for (size_t i = 0; i < urls.size(); ++i) {
185 if (urls[i]->id() == id) { 211 if (urls[i]->id() == id) {
186 url = urls[i]; 212 url = urls[i];
187 break; 213 break;
188 } 214 }
189 } 215 }
190 } 216 }
191 if (!url && allow_fallback) { 217 if (!url && allow_fallback) {
192 url = url_service->FindNewDefaultSearchProvider(); 218 url = url_service->FindNewDefaultSearchProvider();
193 DCHECK(url); 219 DCHECK(url);
194 } 220 }
195 if (url) { 221 if (url) {
196 url_service->SetDefaultSearchProvider(url); 222 url_service->SetDefaultSearchProvider(url);
197 VLOG(1) << "Default search provider set to: " << url->short_name(); 223 VLOG(1) << "Default search provider set to: " << url->short_name();
198 } 224 }
199 return url; 225 return url;
200 } 226 }
201 227
202 void DefaultSearchProviderChange::OpenSearchEngineSettings( 228 void DefaultSearchProviderChange::OpenSearchEngineSettings() {
203 Protector* protector) { 229 protector()->OpenTab(
204 protector->OpenTab(
205 GURL(std::string(chrome::kChromeUISettingsURL) + 230 GURL(std::string(chrome::kChromeUISettingsURL) +
206 chrome::kSearchEnginesSubPage)); 231 chrome::kSearchEnginesSubPage));
207 } 232 }
208 233
209 BaseSettingChange* CreateDefaultSearchProviderChange( 234 BaseSettingChange* CreateDefaultSearchProviderChange(
210 const TemplateURL* actual, 235 const TemplateURL* actual,
211 const TemplateURL* backup) { 236 const TemplateURL* backup) {
212 return new DefaultSearchProviderChange(backup, actual); 237 return new DefaultSearchProviderChange(backup, actual);
213 } 238 }
214 239
215 } // namespace protector 240 } // namespace protector
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698