OLD | NEW |
---|---|
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" | |
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 |
21 namespace { | |
22 | |
23 // Maximum length of the search engine name to be displayed. | |
24 const size_t kMaxDisplayedNameLength = 10; | |
25 | |
26 } | |
whywhat
2011/11/18 08:18:20
// namespace
Ivan Korotkov
2011/11/18 10:44:25
Done.
| |
27 | |
18 class DefaultSearchProviderChange : public SettingChange { | 28 class DefaultSearchProviderChange : public SettingChange { |
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 // SettingChange overrides: |
24 virtual string16 GetOldSetting() const OVERRIDE; | 34 virtual void 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 GetTitle() const OVERRIDE; |
28 virtual void DoDefault(Protector* protector) OVERRIDE; | 38 virtual string16 GetMessage() 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. |
35 void SetDefaultSearchProvider(Protector* protector, int64 id); | 47 void SetDefaultSearchProvider(Protector* protector, int64 id); |
36 | 48 |
49 // Opens the Search engine settings page in a new tab. | |
50 void OpenSearchEngineSettings(Protector* protector); | |
51 | |
37 int64 old_id_; | 52 int64 old_id_; |
38 int64 new_id_; | 53 int64 new_id_; |
54 // ID of the search engine that we fall back to if the backup is lost. | |
55 int64 fallback_id_; | |
39 string16 old_name_; | 56 string16 old_name_; |
40 string16 new_name_; | 57 string16 new_name_; |
58 // Name of the search engine that we fall back to if the backup is lost. | |
59 string16 fallback_name_; | |
60 string16 product_name_; | |
41 | 61 |
42 DISALLOW_COPY_AND_ASSIGN(DefaultSearchProviderChange); | 62 DISALLOW_COPY_AND_ASSIGN(DefaultSearchProviderChange); |
43 }; | 63 }; |
44 | 64 |
45 DefaultSearchProviderChange::DefaultSearchProviderChange( | 65 DefaultSearchProviderChange::DefaultSearchProviderChange( |
46 const TemplateURL* old_url, | 66 const TemplateURL* old_url, |
47 const TemplateURL* new_url) | 67 const TemplateURL* new_url) |
48 : SettingChange(kSearchEngineChanged), | 68 : old_id_(0), |
49 old_id_(0), | 69 new_id_(0), |
50 new_id_(0) { | 70 fallback_id_(0), |
51 DCHECK(new_url); | 71 product_name_(l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)) { |
52 new_id_ = new_url->id(); | 72 if (new_url) { |
53 new_name_ = new_url->short_name(); | 73 new_id_ = new_url->id(); |
74 new_name_ = new_url->short_name(); | |
75 } | |
54 if (old_url) { | 76 if (old_url) { |
55 old_id_ = old_url->id(); | 77 old_id_ = old_url->id(); |
56 old_name_ = old_url->short_name(); | 78 old_name_ = old_url->short_name(); |
57 } | 79 } |
58 } | 80 } |
59 | 81 |
60 DefaultSearchProviderChange::~DefaultSearchProviderChange() { | 82 DefaultSearchProviderChange::~DefaultSearchProviderChange() { |
61 } | 83 } |
62 | 84 |
63 string16 DefaultSearchProviderChange::GetOldSetting() const { | 85 void DefaultSearchProviderChange::Init(Protector* protector) { |
64 return old_name_; | 86 // Initially reset the search engine to its previous setting. |
65 } | |
66 | |
67 string16 DefaultSearchProviderChange::GetNewSetting() const { | |
68 return new_name_; | |
69 } | |
70 | |
71 void DefaultSearchProviderChange::Accept(Protector* protector) { | |
72 SetDefaultSearchProvider(protector, new_id_); | |
73 // TODO(avayvod): Add histrogram. | |
74 } | |
75 | |
76 void DefaultSearchProviderChange::Revert(Protector* protector) { | |
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. | |
85 } | |
86 | |
87 void DefaultSearchProviderChange::DoDefault(Protector* protector) { | |
88 SetDefaultSearchProvider(protector, old_id_); | 87 SetDefaultSearchProvider(protector, old_id_); |
89 // TODO(avayvod): Add histrogram. | 88 // TODO(avayvod): Add histrogram. |
90 } | 89 } |
91 | 90 |
91 void DefaultSearchProviderChange::Apply(Protector* protector) { | |
92 // TODO(avayvod): Add histrogram. | |
93 if (!new_id_) { | |
94 // Open settings page in case the new setting is invalid. | |
95 OpenSearchEngineSettings(protector); | |
96 } else { | |
97 SetDefaultSearchProvider(protector, new_id_); | |
98 } | |
99 } | |
100 | |
101 void DefaultSearchProviderChange::Discard(Protector* protector) { | |
102 // TODO(avayvod): Add histrogram. | |
103 if (!old_id_) { | |
104 // Open settings page in case the old setting is invalid. | |
105 OpenSearchEngineSettings(protector); | |
106 } | |
107 // Nothing to do otherwise since we have already set the search engine | |
108 // to |old_id_| in |Init|. | |
109 } | |
110 | |
111 string16 DefaultSearchProviderChange::GetTitle() const { | |
112 return l10n_util::GetStringUTF16(IDS_SEARCH_ENGINE_CHANGE_TITLE); | |
113 } | |
114 | |
115 string16 DefaultSearchProviderChange::GetMessage() const { | |
116 if (fallback_name_.empty()) | |
117 return l10n_util::GetStringFUTF16( | |
118 IDS_SEARCH_ENGINE_CHANGE_MESSAGE, product_name_); | |
119 else | |
120 return l10n_util::GetStringFUTF16( | |
121 IDS_SEARCH_ENGINE_CHANGE_NO_BACKUP_MESSAGE, | |
122 product_name_, fallback_name_); | |
123 } | |
124 | |
125 string16 DefaultSearchProviderChange::GetApplyButtonText() const { | |
126 if (new_id_) { | |
127 if (new_id_ == fallback_id_) { | |
128 // Old search engine is lost, the fallback search engine is the same as | |
129 // the new one so no need to show this button. | |
130 return string16(); | |
131 } | |
132 if (new_name_.length() > kMaxDisplayedNameLength) | |
133 return l10n_util::GetStringUTF16(IDS_CHANGE_SEARCH_ENGINE_NO_NAME); | |
134 else | |
135 return l10n_util::GetStringFUTF16(IDS_CHANGE_SEARCH_ENGINE, new_name_); | |
136 } else if (old_id_) { | |
137 // New setting is lost, offer to go to settings. | |
138 return l10n_util::GetStringUTF16(IDS_SELECT_SEARCH_ENGINE); | |
139 } else { | |
140 // Both settings are lost: don't show this button. | |
141 return string16(); | |
142 } | |
143 } | |
144 | |
145 string16 DefaultSearchProviderChange::GetDiscardButtonText() const { | |
146 if (old_id_) { | |
147 if (new_name_.length() > kMaxDisplayedNameLength) | |
148 return l10n_util::GetStringUTF16(IDS_KEEP_SETTING); | |
149 else | |
150 return l10n_util::GetStringFUTF16(IDS_KEEP_SEARCH_ENGINE, old_name_); | |
151 } else { | |
152 // Old setting is old, offer to go to settings. | |
whywhat
2011/11/18 08:18:20
is old -> is lost
Ivan Korotkov
2011/11/18 10:44:25
Done.
| |
153 return l10n_util::GetStringUTF16(IDS_SELECT_SEARCH_ENGINE); | |
154 } | |
155 } | |
156 | |
92 void DefaultSearchProviderChange::SetDefaultSearchProvider( | 157 void DefaultSearchProviderChange::SetDefaultSearchProvider( |
93 Protector* protector, | 158 Protector* protector, |
94 int64 id) { | 159 int64 id) { |
95 DCHECK(protector); | 160 DCHECK(protector); |
96 TemplateURLService* url_service = protector->GetTemplateURLService(); | 161 TemplateURLService* url_service = protector->GetTemplateURLService(); |
97 if (!url_service) { | 162 if (!url_service) { |
98 LOG(WARNING) << "Can't get TemplateURLService object."; | 163 LOG(WARNING) << "Can't get TemplateURLService object."; |
99 return; | 164 return; |
100 } | 165 } |
101 const TemplateURL* url = NULL; | 166 const TemplateURL* url = NULL; |
102 const TemplateURLService::TemplateURLVector& urls = | 167 if (id) { |
103 url_service->GetTemplateURLs(); | 168 const TemplateURLService::TemplateURLVector& urls = |
104 for (size_t i = 0; i < urls.size(); ++i) | 169 url_service->GetTemplateURLs(); |
105 if (urls[i]->id() == id) { | 170 for (size_t i = 0; i < urls.size(); ++i) { |
106 url = urls[i]; | 171 if (urls[i]->id() == id) { |
107 break; | 172 url = urls[i]; |
173 break; | |
174 } | |
108 } | 175 } |
109 if (!url) | 176 } else { |
110 url = url_service->FindNewDefaultSearchProvider(); | 177 url = url_service->FindNewDefaultSearchProvider(); |
178 fallback_id_ = url->id(); | |
whywhat
2011/11/18 08:18:20
This code is logically part of initialization, not
Ivan Korotkov
2011/11/18 10:44:25
Makes sense.
| |
179 fallback_name_ = url->short_name(); | |
180 } | |
181 DCHECK(url); | |
111 url_service->SetDefaultSearchProvider(url); | 182 url_service->SetDefaultSearchProvider(url); |
183 VLOG(1) << "Default search provider set to: " << url->short_name(); | |
184 } | |
185 | |
186 void DefaultSearchProviderChange::OpenSearchEngineSettings( | |
187 Protector* protector) { | |
188 protector->OpenTab( | |
189 GURL(std::string(chrome::kChromeUISettingsURL) + | |
190 chrome::kSearchEnginesSubPage)); | |
112 } | 191 } |
113 | 192 |
114 SettingChange* CreateDefaultSearchProviderChange( | 193 SettingChange* CreateDefaultSearchProviderChange( |
115 const TemplateURL* actual, | 194 const TemplateURL* actual, |
116 const TemplateURL* backup) { | 195 const TemplateURL* backup) { |
117 return new DefaultSearchProviderChange(backup, actual); | 196 return new DefaultSearchProviderChange(backup, actual); |
118 } | 197 } |
119 | 198 |
120 } // namespace protector | 199 } // namespace protector |
OLD | NEW |