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

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

Issue 8558020: Protector strings and bubble/SettingsChange code refactoring. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: This time it really should compile. 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/browser/protector/base_setting_change.cc ('k') | chrome/browser/protector/protector.h » ('j') | 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 "chrome/browser/protector/base_setting_change.h"
8 #include "chrome/browser/protector/protector.h" 9 #include "chrome/browser/protector/protector.h"
9 #include "chrome/browser/protector/setting_change.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/webdata/keyword_table.h" 12 #include "chrome/browser/webdata/keyword_table.h"
13 #include "chrome/common/url_constants.h" 13 #include "chrome/common/url_constants.h"
14 #include "grit/chromium_strings.h"
15 #include "grit/generated_resources.h"
14 #include "googleurl/src/gurl.h" 16 #include "googleurl/src/gurl.h"
17 #include "ui/base/l10n/l10n_util.h"
15 18
16 namespace protector { 19 namespace protector {
17 20
18 class DefaultSearchProviderChange : public SettingChange { 21 namespace {
22
23 // Maximum length of the search engine name to be displayed.
24 const size_t kMaxDisplayedNameLength = 10;
25
26 } // namespace
27
28 class DefaultSearchProviderChange : public BaseSettingChange {
19 public: 29 public:
20 DefaultSearchProviderChange(const TemplateURL* old_url, 30 DefaultSearchProviderChange(const TemplateURL* old_url,
21 const TemplateURL* new_url); 31 const TemplateURL* new_url);
22 32
23 // SettingChange overrides: 33 // BaseSettingChange overrides:
24 virtual string16 GetOldSetting() const OVERRIDE; 34 virtual bool Init(Protector* protector) OVERRIDE;
25 virtual string16 GetNewSetting() const OVERRIDE; 35 virtual void Apply(Protector* protector) OVERRIDE;
26 virtual void Accept(Protector* protector) OVERRIDE; 36 virtual void Discard(Protector* protector) OVERRIDE;
27 virtual void Revert(Protector* protector) OVERRIDE; 37 virtual string16 GetBubbleTitle() const OVERRIDE;
28 virtual void DoDefault(Protector* protector) OVERRIDE; 38 virtual string16 GetBubbleMessage() const OVERRIDE;
39 virtual string16 GetApplyButtonText() const OVERRIDE;
40 virtual string16 GetDiscardButtonText() const OVERRIDE;
29 41
30 private: 42 private:
31 virtual ~DefaultSearchProviderChange(); 43 virtual ~DefaultSearchProviderChange();
32 44
33 // Sets the given default search provider to profile that |protector| is 45 // Sets the given default search provider to profile that |protector| is
34 // guarding. 46 // guarding. Returns the |TemplateURL| instance the default search provider
35 void SetDefaultSearchProvider(Protector* protector, int64 id); 47 // has been set to. If no search provider with |id| exists and
48 // |allow_fallback| is true, sets one of the prepoluated search providers.
49 const TemplateURL* SetDefaultSearchProvider(Protector* protector,
50 int64 id,
51 bool allow_fallback);
52
53 // Opens the Search engine settings page in a new tab.
54 void OpenSearchEngineSettings(Protector* protector);
36 55
37 int64 old_id_; 56 int64 old_id_;
38 int64 new_id_; 57 int64 new_id_;
58 // ID of the search engine that we fall back to if the backup is lost.
59 int64 fallback_id_;
39 string16 old_name_; 60 string16 old_name_;
40 string16 new_name_; 61 string16 new_name_;
62 // Name of the search engine that we fall back to if the backup is lost.
63 string16 fallback_name_;
64 string16 product_name_;
41 65
42 DISALLOW_COPY_AND_ASSIGN(DefaultSearchProviderChange); 66 DISALLOW_COPY_AND_ASSIGN(DefaultSearchProviderChange);
43 }; 67 };
44 68
45 DefaultSearchProviderChange::DefaultSearchProviderChange( 69 DefaultSearchProviderChange::DefaultSearchProviderChange(
46 const TemplateURL* old_url, 70 const TemplateURL* old_url,
47 const TemplateURL* new_url) 71 const TemplateURL* new_url)
48 : SettingChange(kSearchEngineChanged), 72 : old_id_(0),
49 old_id_(0), 73 new_id_(0),
50 new_id_(0) { 74 fallback_id_(0),
51 DCHECK(new_url); 75 product_name_(l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)) {
52 new_id_ = new_url->id(); 76 if (new_url) {
53 new_name_ = new_url->short_name(); 77 new_id_ = new_url->id();
78 new_name_ = new_url->short_name();
79 }
54 if (old_url) { 80 if (old_url) {
55 old_id_ = old_url->id(); 81 old_id_ = old_url->id();
56 old_name_ = old_url->short_name(); 82 old_name_ = old_url->short_name();
57 } 83 }
58 } 84 }
59 85
60 DefaultSearchProviderChange::~DefaultSearchProviderChange() { 86 DefaultSearchProviderChange::~DefaultSearchProviderChange() {
61 } 87 }
62 88
63 string16 DefaultSearchProviderChange::GetOldSetting() const { 89 bool DefaultSearchProviderChange::Init(Protector* protector) {
64 return old_name_; 90 // Initially reset the search engine to its previous setting.
91 const TemplateURL* current_url =
92 SetDefaultSearchProvider(protector, old_id_, true);
93 if (!current_url)
94 return false;
95 if (!old_id_ || current_url->id() != old_id_) {
96 // Old settings is lost or invalid, so we had to fall back to one of the
97 // prepopulated search engines.
98 fallback_id_ = current_url->id();
99 fallback_name_ = current_url->short_name();
100 VLOG(1) << "Fallback to " << fallback_name_;
101 }
102 return true;
65 } 103 }
66 104
67 string16 DefaultSearchProviderChange::GetNewSetting() const { 105 void DefaultSearchProviderChange::Apply(Protector* protector) {
68 return new_name_; 106 // TODO(avayvod): Add histrogram.
107 if (!new_id_) {
108 // Open settings page in case the new setting is invalid.
109 OpenSearchEngineSettings(protector);
110 } else {
111 SetDefaultSearchProvider(protector, new_id_, false);
112 }
69 } 113 }
70 114
71 void DefaultSearchProviderChange::Accept(Protector* protector) { 115 void DefaultSearchProviderChange::Discard(Protector* protector) {
72 SetDefaultSearchProvider(protector, new_id_);
73 // TODO(avayvod): Add histrogram. 116 // TODO(avayvod): Add histrogram.
117 if (!old_id_) {
118 // Open settings page in case the old setting is invalid.
119 OpenSearchEngineSettings(protector);
120 }
121 // Nothing to do otherwise since we have already set the search engine
122 // to |old_id_| in |Init|.
74 } 123 }
75 124
76 void DefaultSearchProviderChange::Revert(Protector* protector) { 125 string16 DefaultSearchProviderChange::GetBubbleTitle() const {
77 SetDefaultSearchProvider(protector, old_id_); 126 return l10n_util::GetStringUTF16(IDS_SEARCH_ENGINE_CHANGE_TITLE);
78 if (!old_id_) {
79 // Open settings page in case the original setting was lost.
80 protector->OpenTab(
81 GURL(std::string(chrome::kChromeUISettingsURL) +
82 chrome::kSearchEnginesSubPage));
83 }
84 // TODO(avayvod): Add histrogram.
85 } 127 }
86 128
87 void DefaultSearchProviderChange::DoDefault(Protector* protector) { 129 string16 DefaultSearchProviderChange::GetBubbleMessage() const {
88 SetDefaultSearchProvider(protector, old_id_); 130 if (fallback_name_.empty())
89 // TODO(avayvod): Add histrogram. 131 return l10n_util::GetStringFUTF16(
132 IDS_SEARCH_ENGINE_CHANGE_MESSAGE, product_name_);
133 else
134 return l10n_util::GetStringFUTF16(
135 IDS_SEARCH_ENGINE_CHANGE_NO_BACKUP_MESSAGE,
136 product_name_, fallback_name_);
90 } 137 }
91 138
92 void DefaultSearchProviderChange::SetDefaultSearchProvider( 139 string16 DefaultSearchProviderChange::GetApplyButtonText() const {
140 if (new_id_) {
141 if (new_id_ == fallback_id_) {
142 // Old search engine is lost, the fallback search engine is the same as
143 // the new one so no need to show this button.
144 return string16();
145 }
146 if (new_name_.length() > kMaxDisplayedNameLength)
147 return l10n_util::GetStringUTF16(IDS_CHANGE_SEARCH_ENGINE_NO_NAME);
148 else
149 return l10n_util::GetStringFUTF16(IDS_CHANGE_SEARCH_ENGINE, new_name_);
150 } else if (old_id_) {
151 // New setting is lost, offer to go to settings.
152 return l10n_util::GetStringUTF16(IDS_SELECT_SEARCH_ENGINE);
153 } else {
154 // Both settings are lost: don't show this button.
155 return string16();
156 }
157 }
158
159 string16 DefaultSearchProviderChange::GetDiscardButtonText() const {
160 if (old_id_) {
161 if (new_name_.length() > kMaxDisplayedNameLength)
162 return l10n_util::GetStringUTF16(IDS_KEEP_SETTING);
163 else
164 return l10n_util::GetStringFUTF16(IDS_KEEP_SEARCH_ENGINE, old_name_);
165 } else {
166 // Old setting is lost, offer to go to settings.
167 return l10n_util::GetStringUTF16(IDS_SELECT_SEARCH_ENGINE);
168 }
169 }
170
171 const TemplateURL* DefaultSearchProviderChange::SetDefaultSearchProvider(
93 Protector* protector, 172 Protector* protector,
94 int64 id) { 173 int64 id,
95 DCHECK(protector); 174 bool allow_fallback) {
96 TemplateURLService* url_service = protector->GetTemplateURLService(); 175 TemplateURLService* url_service = protector->GetTemplateURLService();
97 if (!url_service) { 176 if (!url_service) {
98 LOG(WARNING) << "Can't get TemplateURLService object."; 177 NOTREACHED() << "Can't get TemplateURLService object.";
99 return; 178 return NULL;
100 } 179 }
101 const TemplateURL* url = NULL; 180 const TemplateURL* url = NULL;
102 const TemplateURLService::TemplateURLVector& urls = 181 if (id) {
103 url_service->GetTemplateURLs(); 182 const TemplateURLService::TemplateURLVector& urls =
104 for (size_t i = 0; i < urls.size(); ++i) 183 url_service->GetTemplateURLs();
105 if (urls[i]->id() == id) { 184 for (size_t i = 0; i < urls.size(); ++i) {
106 url = urls[i]; 185 if (urls[i]->id() == id) {
107 break; 186 url = urls[i];
187 break;
188 }
108 } 189 }
109 if (!url) 190 }
191 if (!url && allow_fallback) {
110 url = url_service->FindNewDefaultSearchProvider(); 192 url = url_service->FindNewDefaultSearchProvider();
111 url_service->SetDefaultSearchProvider(url); 193 DCHECK(url);
194 }
195 if (url) {
196 url_service->SetDefaultSearchProvider(url);
197 VLOG(1) << "Default search provider set to: " << url->short_name();
198 }
199 return url;
112 } 200 }
113 201
114 SettingChange* CreateDefaultSearchProviderChange( 202 void DefaultSearchProviderChange::OpenSearchEngineSettings(
203 Protector* protector) {
204 protector->OpenTab(
205 GURL(std::string(chrome::kChromeUISettingsURL) +
206 chrome::kSearchEnginesSubPage));
207 }
208
209 BaseSettingChange* CreateDefaultSearchProviderChange(
115 const TemplateURL* actual, 210 const TemplateURL* actual,
116 const TemplateURL* backup) { 211 const TemplateURL* backup) {
117 return new DefaultSearchProviderChange(backup, actual); 212 return new DefaultSearchProviderChange(backup, actual);
118 } 213 }
119 214
120 } // namespace protector 215 } // namespace protector
OLDNEW
« no previous file with comments | « chrome/browser/protector/base_setting_change.cc ('k') | chrome/browser/protector/protector.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698