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

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

Issue 8586050: Revert 110684 - Protector strings and bubble/SettingsChange code refactoring. (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
« no previous file with comments | « chrome/app/generated_resources.grd ('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/protector.h" 8 #include "chrome/browser/protector/protector.h"
9 #include "chrome/browser/protector/setting_change.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"
16 #include "googleurl/src/gurl.h" 14 #include "googleurl/src/gurl.h"
17 #include "ui/base/l10n/l10n_util.h"
18 15
19 namespace protector { 16 namespace protector {
20 17
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 SettingChange { 18 class DefaultSearchProviderChange : public SettingChange {
29 public: 19 public:
30 DefaultSearchProviderChange(const TemplateURL* old_url, 20 DefaultSearchProviderChange(const TemplateURL* old_url,
31 const TemplateURL* new_url); 21 const TemplateURL* new_url);
32 22
33 // SettingChange overrides: 23 // SettingChange overrides:
34 virtual bool Init(Protector* protector) OVERRIDE; 24 virtual string16 GetOldSetting() const OVERRIDE;
35 virtual void Apply(Protector* protector) OVERRIDE; 25 virtual string16 GetNewSetting() const OVERRIDE;
36 virtual void Discard(Protector* protector) OVERRIDE; 26 virtual void Accept(Protector* protector) OVERRIDE;
37 virtual string16 GetTitle() const OVERRIDE; 27 virtual void Revert(Protector* protector) OVERRIDE;
38 virtual string16 GetMessage() const OVERRIDE; 28 virtual void DoDefault(Protector* protector) OVERRIDE;
39 virtual string16 GetApplyButtonText() const OVERRIDE;
40 virtual string16 GetDiscardButtonText() const OVERRIDE;
41 29
42 private: 30 private:
43 virtual ~DefaultSearchProviderChange(); 31 virtual ~DefaultSearchProviderChange();
44 32
45 // Sets the given default search provider to profile that |protector| is 33 // Sets the given default search provider to profile that |protector| is
46 // guarding. Returns the |TemplateURL| instance the default search provider 34 // guarding.
47 // has been set to. If no search provider with |id| exists and 35 void SetDefaultSearchProvider(Protector* protector, int64 id);
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);
55 36
56 int64 old_id_; 37 int64 old_id_;
57 int64 new_id_; 38 int64 new_id_;
58 // ID of the search engine that we fall back to if the backup is lost.
59 int64 fallback_id_;
60 string16 old_name_; 39 string16 old_name_;
61 string16 new_name_; 40 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_;
65 41
66 DISALLOW_COPY_AND_ASSIGN(DefaultSearchProviderChange); 42 DISALLOW_COPY_AND_ASSIGN(DefaultSearchProviderChange);
67 }; 43 };
68 44
69 DefaultSearchProviderChange::DefaultSearchProviderChange( 45 DefaultSearchProviderChange::DefaultSearchProviderChange(
70 const TemplateURL* old_url, 46 const TemplateURL* old_url,
71 const TemplateURL* new_url) 47 const TemplateURL* new_url)
72 : old_id_(0), 48 : SettingChange(kSearchEngineChanged),
73 new_id_(0), 49 old_id_(0),
74 fallback_id_(0), 50 new_id_(0) {
75 product_name_(l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)) { 51 DCHECK(new_url);
76 if (new_url) { 52 new_id_ = new_url->id();
77 new_id_ = new_url->id(); 53 new_name_ = new_url->short_name();
78 new_name_ = new_url->short_name();
79 }
80 if (old_url) { 54 if (old_url) {
81 old_id_ = old_url->id(); 55 old_id_ = old_url->id();
82 old_name_ = old_url->short_name(); 56 old_name_ = old_url->short_name();
83 } 57 }
84 } 58 }
85 59
86 DefaultSearchProviderChange::~DefaultSearchProviderChange() { 60 DefaultSearchProviderChange::~DefaultSearchProviderChange() {
87 } 61 }
88 62
89 bool DefaultSearchProviderChange::Init(Protector* protector) { 63 string16 DefaultSearchProviderChange::GetOldSetting() const {
90 // Initially reset the search engine to its previous setting. 64 return old_name_;
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;
103 } 65 }
104 66
105 void DefaultSearchProviderChange::Apply(Protector* protector) { 67 string16 DefaultSearchProviderChange::GetNewSetting() const {
106 // TODO(avayvod): Add histrogram. 68 return new_name_;
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 }
113 } 69 }
114 70
115 void DefaultSearchProviderChange::Discard(Protector* protector) { 71 void DefaultSearchProviderChange::Accept(Protector* protector) {
72 SetDefaultSearchProvider(protector, new_id_);
116 // TODO(avayvod): Add histrogram. 73 // 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|.
123 } 74 }
124 75
125 string16 DefaultSearchProviderChange::GetTitle() const { 76 void DefaultSearchProviderChange::Revert(Protector* protector) {
126 return l10n_util::GetStringUTF16(IDS_SEARCH_ENGINE_CHANGE_TITLE); 77 SetDefaultSearchProvider(protector, old_id_);
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.
127 } 85 }
128 86
129 string16 DefaultSearchProviderChange::GetMessage() const { 87 void DefaultSearchProviderChange::DoDefault(Protector* protector) {
130 if (fallback_name_.empty()) 88 SetDefaultSearchProvider(protector, old_id_);
131 return l10n_util::GetStringFUTF16( 89 // TODO(avayvod): Add histrogram.
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_);
137 } 90 }
138 91
139 string16 DefaultSearchProviderChange::GetApplyButtonText() const { 92 void DefaultSearchProviderChange::SetDefaultSearchProvider(
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(
172 Protector* protector, 93 Protector* protector,
173 int64 id, 94 int64 id) {
174 bool allow_fallback) { 95 DCHECK(protector);
175 TemplateURLService* url_service = protector->GetTemplateURLService(); 96 TemplateURLService* url_service = protector->GetTemplateURLService();
176 if (!url_service) { 97 if (!url_service) {
177 NOTREACHED() << "Can't get TemplateURLService object."; 98 LOG(WARNING) << "Can't get TemplateURLService object.";
178 return NULL; 99 return;
179 } 100 }
180 const TemplateURL* url = NULL; 101 const TemplateURL* url = NULL;
181 if (id) { 102 const TemplateURLService::TemplateURLVector& urls =
182 const TemplateURLService::TemplateURLVector& urls = 103 url_service->GetTemplateURLs();
183 url_service->GetTemplateURLs(); 104 for (size_t i = 0; i < urls.size(); ++i)
184 for (size_t i = 0; i < urls.size(); ++i) { 105 if (urls[i]->id() == id) {
185 if (urls[i]->id() == id) { 106 url = urls[i];
186 url = urls[i]; 107 break;
187 break;
188 }
189 } 108 }
190 } 109 if (!url)
191 if (!url && allow_fallback) {
192 url = url_service->FindNewDefaultSearchProvider(); 110 url = url_service->FindNewDefaultSearchProvider();
193 DCHECK(url); 111 url_service->SetDefaultSearchProvider(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;
200 }
201
202 void DefaultSearchProviderChange::OpenSearchEngineSettings(
203 Protector* protector) {
204 protector->OpenTab(
205 GURL(std::string(chrome::kChromeUISettingsURL) +
206 chrome::kSearchEnginesSubPage));
207 } 112 }
208 113
209 SettingChange* CreateDefaultSearchProviderChange( 114 SettingChange* CreateDefaultSearchProviderChange(
210 const TemplateURL* actual, 115 const TemplateURL* actual,
211 const TemplateURL* backup) { 116 const TemplateURL* backup) {
212 return new DefaultSearchProviderChange(backup, actual); 117 return new DefaultSearchProviderChange(backup, actual);
213 } 118 }
214 119
215 } // namespace protector 120 } // namespace protector
OLDNEW
« no previous file with comments | « chrome/app/generated_resources.grd ('k') | chrome/browser/protector/protector.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698