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

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

Issue 8694002: Protector: string and fix segfault fix on button press. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comment fix. 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
« no previous file with comments | « chrome/app/generated_resources.grd ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "base/metrics/histogram.h" 8 #include "base/metrics/histogram.h"
9 #include "chrome/browser/protector/base_setting_change.h" 9 #include "chrome/browser/protector/base_setting_change.h"
10 #include "chrome/browser/protector/histograms.h" 10 #include "chrome/browser/protector/histograms.h"
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 void OpenSearchEngineSettings(); 62 void OpenSearchEngineSettings();
63 63
64 int64 old_id_; 64 int64 old_id_;
65 int64 new_id_; 65 int64 new_id_;
66 // ID of the search engine that we fall back to if the backup is lost. 66 // ID of the search engine that we fall back to if the backup is lost.
67 int64 fallback_id_; 67 int64 fallback_id_;
68 string16 old_name_; 68 string16 old_name_;
69 string16 new_name_; 69 string16 new_name_;
70 // Name of the search engine that we fall back to if the backup is lost. 70 // Name of the search engine that we fall back to if the backup is lost.
71 string16 fallback_name_; 71 string16 fallback_name_;
72 string16 product_name_;
73 // Histogram ID of the new search provider. 72 // Histogram ID of the new search provider.
74 int new_histogram_id_; 73 int new_histogram_id_;
75 // Default search provider set by |Init| for the period until user makes a 74 // Default search provider set by |Init| for the period until user makes a
76 // choice and either |Apply| or |Discard| is performed. Should only be used 75 // choice and either |Apply| or |Discard| is performed. Should only be used
77 // for comparison with the current default search provider and never 76 // for comparison with the current default search provider and never
78 // dereferenced other than in |Init| because it may be deallocated by 77 // dereferenced other than in |Init| because it may be deallocated by
79 // TemplateURLService at any time. 78 // TemplateURLService at any time.
80 const TemplateURL* default_search_provider_; 79 const TemplateURL* default_search_provider_;
81 80
82 DISALLOW_COPY_AND_ASSIGN(DefaultSearchProviderChange); 81 DISALLOW_COPY_AND_ASSIGN(DefaultSearchProviderChange);
83 }; 82 };
84 83
85 DefaultSearchProviderChange::DefaultSearchProviderChange( 84 DefaultSearchProviderChange::DefaultSearchProviderChange(
86 const TemplateURL* old_url, 85 const TemplateURL* old_url,
87 const TemplateURL* new_url) 86 const TemplateURL* new_url)
88 : old_id_(0), 87 : old_id_(0),
89 new_id_(0), 88 new_id_(0),
90 fallback_id_(0), 89 fallback_id_(0),
91 product_name_(l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)),
92 new_histogram_id_(GetSearchProviderHistogramID(new_url)), 90 new_histogram_id_(GetSearchProviderHistogramID(new_url)),
93 default_search_provider_(NULL) { 91 default_search_provider_(NULL) {
94 if (new_url) { 92 if (new_url) {
95 new_id_ = new_url->id(); 93 new_id_ = new_url->id();
96 new_name_ = new_url->short_name(); 94 new_name_ = new_url->short_name();
97 } 95 }
98 if (old_url) { 96 if (old_url) {
99 old_id_ = old_url->id(); 97 old_id_ = old_url->id();
100 old_name_ = old_url->short_name(); 98 old_name_ = old_url->short_name();
101 } 99 }
(...skipping 16 matching lines...) Expand all
118 return false; 116 return false;
119 117
120 if (!old_id_ || default_search_provider_->id() != old_id_) { 118 if (!old_id_ || default_search_provider_->id() != old_id_) {
121 // Old settings is lost or invalid, so we had to fall back to one of the 119 // Old settings is lost or invalid, so we had to fall back to one of the
122 // prepopulated search engines. 120 // prepopulated search engines.
123 fallback_id_ = default_search_provider_->id(); 121 fallback_id_ = default_search_provider_->id();
124 fallback_name_ = default_search_provider_->short_name(); 122 fallback_name_ = default_search_provider_->short_name();
125 VLOG(1) << "Fallback to " << fallback_name_; 123 VLOG(1) << "Fallback to " << fallback_name_;
126 } 124 }
127 125
126 // This must be called after the initial |SetDefaultSearchProvider| call
127 // because the latter will remove the observer.
128 protector->GetTemplateURLService()->AddObserver(this); 128 protector->GetTemplateURLService()->AddObserver(this);
129 129
130 return true; 130 return true;
131 } 131 }
132 132
133 void DefaultSearchProviderChange::Apply() { 133 void DefaultSearchProviderChange::Apply() {
134 UMA_HISTOGRAM_ENUMERATION( 134 UMA_HISTOGRAM_ENUMERATION(
135 kProtectorHistogramSearchProviderApplied, 135 kProtectorHistogramSearchProviderApplied,
136 new_histogram_id_, 136 new_histogram_id_,
137 kProtectorMaxSearchProviderID); 137 kProtectorMaxSearchProviderID);
(...skipping 30 matching lines...) Expand all
168 void DefaultSearchProviderChange::OnBeforeRemoved() { 168 void DefaultSearchProviderChange::OnBeforeRemoved() {
169 protector()->GetTemplateURLService()->RemoveObserver(this); 169 protector()->GetTemplateURLService()->RemoveObserver(this);
170 } 170 }
171 171
172 string16 DefaultSearchProviderChange::GetBubbleTitle() const { 172 string16 DefaultSearchProviderChange::GetBubbleTitle() const {
173 return l10n_util::GetStringUTF16(IDS_SEARCH_ENGINE_CHANGE_TITLE); 173 return l10n_util::GetStringUTF16(IDS_SEARCH_ENGINE_CHANGE_TITLE);
174 } 174 }
175 175
176 string16 DefaultSearchProviderChange::GetBubbleMessage() const { 176 string16 DefaultSearchProviderChange::GetBubbleMessage() const {
177 if (fallback_name_.empty()) 177 if (fallback_name_.empty())
178 return l10n_util::GetStringFUTF16( 178 return l10n_util::GetStringUTF16(IDS_SEARCH_ENGINE_CHANGE_MESSAGE);
179 IDS_SEARCH_ENGINE_CHANGE_MESSAGE, product_name_);
180 else 179 else
181 return l10n_util::GetStringFUTF16( 180 return l10n_util::GetStringFUTF16(
182 IDS_SEARCH_ENGINE_CHANGE_NO_BACKUP_MESSAGE, 181 IDS_SEARCH_ENGINE_CHANGE_NO_BACKUP_MESSAGE, fallback_name_);
183 product_name_, fallback_name_);
184 } 182 }
185 183
186 string16 DefaultSearchProviderChange::GetApplyButtonText() const { 184 string16 DefaultSearchProviderChange::GetApplyButtonText() const {
187 if (new_id_) { 185 if (new_id_) {
188 if (new_id_ == fallback_id_) { 186 if (new_id_ == fallback_id_) {
189 // Old search engine is lost, the fallback search engine is the same as 187 // Old search engine is lost, the fallback search engine is the same as
190 // the new one so no need to show this button. 188 // the new one so no need to show this button.
191 return string16(); 189 return string16();
192 } 190 }
193 if (new_name_.length() > kMaxDisplayedNameLength) 191 if (new_name_.length() > kMaxDisplayedNameLength)
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 url = urls[i]; 240 url = urls[i];
243 break; 241 break;
244 } 242 }
245 } 243 }
246 } 244 }
247 if (!url && allow_fallback) { 245 if (!url && allow_fallback) {
248 url = url_service->FindNewDefaultSearchProvider(); 246 url = url_service->FindNewDefaultSearchProvider();
249 DCHECK(url); 247 DCHECK(url);
250 } 248 }
251 if (url) { 249 if (url) {
250 // Remove ourselves from the observer list to prevent from catching our own
251 // change. It is safe to do this multiple times or before adding ourselves.
252 url_service->RemoveObserver(this);
252 url_service->SetDefaultSearchProvider(url); 253 url_service->SetDefaultSearchProvider(url);
253 VLOG(1) << "Default search provider set to: " << url->short_name(); 254 VLOG(1) << "Default search provider set to: " << url->short_name();
255 // No need to re-add observer again because any further changes to the
256 // default search provider are of no interest.
254 } 257 }
255 return url; 258 return url;
256 } 259 }
257 260
258 void DefaultSearchProviderChange::OpenSearchEngineSettings() { 261 void DefaultSearchProviderChange::OpenSearchEngineSettings() {
259 protector()->OpenTab( 262 protector()->OpenTab(
260 GURL(std::string(chrome::kChromeUISettingsURL) + 263 GURL(std::string(chrome::kChromeUISettingsURL) +
261 chrome::kSearchEnginesSubPage)); 264 chrome::kSearchEnginesSubPage));
262 } 265 }
263 266
264 BaseSettingChange* CreateDefaultSearchProviderChange( 267 BaseSettingChange* CreateDefaultSearchProviderChange(
265 const TemplateURL* actual, 268 const TemplateURL* actual,
266 const TemplateURL* backup) { 269 const TemplateURL* backup) {
267 return new DefaultSearchProviderChange(backup, actual); 270 return new DefaultSearchProviderChange(backup, actual);
268 } 271 }
269 272
270 } // namespace protector 273 } // namespace protector
OLDNEW
« no previous file with comments | « chrome/app/generated_resources.grd ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698