OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/ui/webui/options2/search_engine_manager_handler.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/string_number_conversions.h" | |
9 #include "base/utf_string_conversions.h" | |
10 #include "base/values.h" | |
11 #include "chrome/browser/extensions/extension_service.h" | |
12 #include "chrome/browser/profiles/profile.h" | |
13 #include "chrome/browser/search_engines/template_url.h" | |
14 #include "chrome/browser/search_engines/template_url_service.h" | |
15 #include "chrome/browser/ui/search_engines/keyword_editor_controller.h" | |
16 #include "chrome/browser/ui/search_engines/template_url_table_model.h" | |
17 #include "chrome/common/extensions/extension.h" | |
18 #include "chrome/common/url_constants.h" | |
19 #include "content/public/browser/web_ui.h" | |
20 #include "grit/generated_resources.h" | |
21 #include "grit/locale_settings.h" | |
22 #include "ui/base/l10n/l10n_util.h" | |
23 | |
24 namespace { | |
25 | |
26 enum EngineInfoIndexes { | |
27 ENGINE_NAME, | |
28 ENGINE_KEYWORD, | |
29 ENGINE_URL, | |
30 }; | |
31 | |
32 }; // namespace | |
33 | |
34 namespace options { | |
35 | |
36 SearchEngineManagerHandler::SearchEngineManagerHandler() { | |
37 } | |
38 | |
39 SearchEngineManagerHandler::~SearchEngineManagerHandler() { | |
40 if (list_controller_.get() && list_controller_->table_model()) | |
41 list_controller_->table_model()->SetObserver(NULL); | |
42 } | |
43 | |
44 void SearchEngineManagerHandler::InitializeHandler() { | |
45 list_controller_.reset( | |
46 new KeywordEditorController(Profile::FromWebUI(web_ui()))); | |
47 DCHECK(list_controller_.get()); | |
48 list_controller_->table_model()->SetObserver(this); | |
49 } | |
50 | |
51 void SearchEngineManagerHandler::InitializePage() { | |
52 OnModelChanged(); | |
53 } | |
54 | |
55 void SearchEngineManagerHandler::GetLocalizedValues( | |
56 base::DictionaryValue* localized_strings) { | |
57 DCHECK(localized_strings); | |
58 | |
59 RegisterTitle(localized_strings, "searchEngineManagerPage", | |
60 IDS_SEARCH_ENGINES_EDITOR_WINDOW_TITLE); | |
61 localized_strings->SetString("defaultSearchEngineListTitle", | |
62 l10n_util::GetStringUTF16(IDS_SEARCH_ENGINES_EDITOR_MAIN_SEPARATOR)); | |
63 localized_strings->SetString("otherSearchEngineListTitle", | |
64 l10n_util::GetStringUTF16(IDS_SEARCH_ENGINES_EDITOR_OTHER_SEPARATOR)); | |
65 localized_strings->SetString("extensionKeywordsListTitle", | |
66 l10n_util::GetStringUTF16( | |
67 IDS_SEARCH_ENGINES_EDITOR_EXTENSIONS_SEPARATOR)); | |
68 localized_strings->SetString("makeDefaultSearchEngineButton", | |
69 l10n_util::GetStringUTF16(IDS_SEARCH_ENGINES_EDITOR_MAKE_DEFAULT_BUTTON)); | |
70 localized_strings->SetString("searchEngineTableNamePlaceholder", | |
71 l10n_util::GetStringUTF16(IDS_SEARCH_ENGINE_ADD_NEW_NAME_PLACEHOLDER)); | |
72 localized_strings->SetString("searchEngineTableKeywordPlaceholder", | |
73 l10n_util::GetStringUTF16(IDS_SEARCH_ENGINE_ADD_NEW_KEYWORD_PLACEHOLDER)); | |
74 localized_strings->SetString("searchEngineTableURLPlaceholder", | |
75 l10n_util::GetStringUTF16(IDS_SEARCH_ENGINE_ADD_NEW_URL_PLACEHOLDER)); | |
76 localized_strings->SetString("editSearchEngineInvalidTitleToolTip", | |
77 l10n_util::GetStringUTF16(IDS_SEARCH_ENGINES_INVALID_TITLE_TT)); | |
78 localized_strings->SetString("editSearchEngineInvalidKeywordToolTip", | |
79 l10n_util::GetStringUTF16(IDS_SEARCH_ENGINES_INVALID_KEYWORD_TT)); | |
80 localized_strings->SetString("editSearchEngineInvalidURLToolTip", | |
81 l10n_util::GetStringUTF16(IDS_SEARCH_ENGINES_INVALID_URL_TT)); | |
82 } | |
83 | |
84 void SearchEngineManagerHandler::RegisterMessages() { | |
85 web_ui()->RegisterMessageCallback( | |
86 "managerSetDefaultSearchEngine", | |
87 base::Bind(&SearchEngineManagerHandler::SetDefaultSearchEngine, | |
88 base::Unretained(this))); | |
89 web_ui()->RegisterMessageCallback( | |
90 "removeSearchEngine", | |
91 base::Bind(&SearchEngineManagerHandler::RemoveSearchEngine, | |
92 base::Unretained(this))); | |
93 web_ui()->RegisterMessageCallback( | |
94 "editSearchEngine", | |
95 base::Bind(&SearchEngineManagerHandler::EditSearchEngine, | |
96 base::Unretained(this))); | |
97 web_ui()->RegisterMessageCallback( | |
98 "checkSearchEngineInfoValidity", | |
99 base::Bind(&SearchEngineManagerHandler::CheckSearchEngineInfoValidity, | |
100 base::Unretained(this))); | |
101 web_ui()->RegisterMessageCallback( | |
102 "searchEngineEditCancelled", | |
103 base::Bind(&SearchEngineManagerHandler::EditCancelled, | |
104 base::Unretained(this))); | |
105 web_ui()->RegisterMessageCallback( | |
106 "searchEngineEditCompleted", | |
107 base::Bind(&SearchEngineManagerHandler::EditCompleted, | |
108 base::Unretained(this))); | |
109 } | |
110 | |
111 void SearchEngineManagerHandler::OnModelChanged() { | |
112 DCHECK(list_controller_.get()); | |
113 if (!list_controller_->loaded()) | |
114 return; | |
115 | |
116 // Find the default engine. | |
117 const TemplateURL* default_engine = | |
118 list_controller_->url_model()->GetDefaultSearchProvider(); | |
119 int default_index = list_controller_->table_model()->IndexOfTemplateURL( | |
120 default_engine); | |
121 | |
122 // Build the first list (default search engine options). | |
123 ListValue defaults_list; | |
124 int last_default_engine_index = | |
125 list_controller_->table_model()->last_search_engine_index(); | |
126 for (int i = 0; i < last_default_engine_index; ++i) { | |
127 defaults_list.Append(CreateDictionaryForEngine(i, i == default_index)); | |
128 } | |
129 | |
130 // Build the second list (other search templates). | |
131 ListValue others_list; | |
132 if (last_default_engine_index < 0) | |
133 last_default_engine_index = 0; | |
134 int engine_count = list_controller_->table_model()->RowCount(); | |
135 for (int i = last_default_engine_index; i < engine_count; ++i) { | |
136 others_list.Append(CreateDictionaryForEngine(i, i == default_index)); | |
137 } | |
138 | |
139 // Build the extension keywords list. | |
140 ListValue keyword_list; | |
141 ExtensionService* extension_service = | |
142 Profile::FromWebUI(web_ui())->GetExtensionService(); | |
143 if (extension_service) { | |
144 const ExtensionSet* extensions = extension_service->extensions(); | |
145 for (ExtensionSet::const_iterator it = extensions->begin(); | |
146 it != extensions->end(); ++it) { | |
147 if ((*it)->omnibox_keyword().size() > 0) | |
148 keyword_list.Append(CreateDictionaryForExtension(*(*it))); | |
149 } | |
150 } | |
151 | |
152 web_ui()->CallJavascriptFunction("SearchEngineManager.updateSearchEngineList", | |
153 defaults_list, others_list, keyword_list); | |
154 } | |
155 | |
156 void SearchEngineManagerHandler::OnItemsChanged(int start, int length) { | |
157 OnModelChanged(); | |
158 } | |
159 | |
160 void SearchEngineManagerHandler::OnItemsAdded(int start, int length) { | |
161 OnModelChanged(); | |
162 } | |
163 | |
164 void SearchEngineManagerHandler::OnItemsRemoved(int start, int length) { | |
165 OnModelChanged(); | |
166 } | |
167 | |
168 base::DictionaryValue* SearchEngineManagerHandler::CreateDictionaryForExtension( | |
169 const extensions::Extension& extension) { | |
170 base::DictionaryValue* dict = new base::DictionaryValue(); | |
171 dict->SetString("name", extension.name()); | |
172 dict->SetString("displayName", extension.name()); | |
173 dict->SetString("keyword", extension.omnibox_keyword()); | |
174 GURL icon = extension.GetIconURL(16, ExtensionIconSet::MATCH_BIGGER); | |
175 dict->SetString("iconURL", icon.spec()); | |
176 dict->SetString("url", string16()); | |
177 return dict; | |
178 } | |
179 | |
180 base::DictionaryValue* SearchEngineManagerHandler::CreateDictionaryForEngine( | |
181 int index, bool is_default) { | |
182 TemplateURLTableModel* table_model = list_controller_->table_model(); | |
183 const TemplateURL* template_url = list_controller_->GetTemplateURL(index); | |
184 | |
185 base::DictionaryValue* dict = new base::DictionaryValue(); | |
186 dict->SetString("name", template_url->short_name()); | |
187 dict->SetString("displayName", table_model->GetText( | |
188 index, IDS_SEARCH_ENGINES_EDITOR_DESCRIPTION_COLUMN)); | |
189 dict->SetString("keyword", table_model->GetText( | |
190 index, IDS_SEARCH_ENGINES_EDITOR_KEYWORD_COLUMN)); | |
191 dict->SetString("url", template_url->url_ref().DisplayURL()); | |
192 dict->SetBoolean("urlLocked", template_url->prepopulate_id() > 0); | |
193 GURL icon_url = template_url->favicon_url(); | |
194 if (icon_url.is_valid()) | |
195 dict->SetString("iconURL", icon_url.spec()); | |
196 dict->SetString("modelIndex", base::IntToString(index)); | |
197 | |
198 if (list_controller_->CanRemove(template_url)) | |
199 dict->SetString("canBeRemoved", "1"); | |
200 if (list_controller_->CanMakeDefault(template_url)) | |
201 dict->SetString("canBeDefault", "1"); | |
202 if (is_default) | |
203 dict->SetString("default", "1"); | |
204 if (list_controller_->CanEdit(template_url)) | |
205 dict->SetString("canBeEdited", "1"); | |
206 | |
207 return dict; | |
208 } | |
209 | |
210 void SearchEngineManagerHandler::SetDefaultSearchEngine(const ListValue* args) { | |
211 int index; | |
212 if (!ExtractIntegerValue(args, &index)) { | |
213 NOTREACHED(); | |
214 return; | |
215 } | |
216 if (index < 0 || index >= list_controller_->table_model()->RowCount()) | |
217 return; | |
218 | |
219 list_controller_->MakeDefaultTemplateURL(index); | |
220 } | |
221 | |
222 void SearchEngineManagerHandler::RemoveSearchEngine(const ListValue* args) { | |
223 int index; | |
224 if (!ExtractIntegerValue(args, &index)) { | |
225 NOTREACHED(); | |
226 return; | |
227 } | |
228 if (index < 0 || index >= list_controller_->table_model()->RowCount()) | |
229 return; | |
230 | |
231 if (list_controller_->CanRemove(list_controller_->GetTemplateURL(index))) | |
232 list_controller_->RemoveTemplateURL(index); | |
233 } | |
234 | |
235 void SearchEngineManagerHandler::EditSearchEngine(const ListValue* args) { | |
236 int index; | |
237 if (!ExtractIntegerValue(args, &index)) { | |
238 NOTREACHED(); | |
239 return; | |
240 } | |
241 | |
242 // Allow -1, which means we are adding a new engine. | |
243 if (index < -1 || index >= list_controller_->table_model()->RowCount()) | |
244 return; | |
245 | |
246 edit_controller_.reset(new EditSearchEngineController( | |
247 (index == -1) ? NULL : list_controller_->GetTemplateURL(index), this, | |
248 Profile::FromWebUI(web_ui()))); | |
249 } | |
250 | |
251 void SearchEngineManagerHandler::OnEditedKeyword( | |
252 TemplateURL* template_url, | |
253 const string16& title, | |
254 const string16& keyword, | |
255 const std::string& url) { | |
256 DCHECK(!url.empty()); | |
257 if (template_url) | |
258 list_controller_->ModifyTemplateURL(template_url, title, keyword, url); | |
259 else | |
260 list_controller_->AddTemplateURL(title, keyword, url); | |
261 edit_controller_.reset(); | |
262 } | |
263 | |
264 void SearchEngineManagerHandler::CheckSearchEngineInfoValidity( | |
265 const ListValue* args) | |
266 { | |
267 if (!edit_controller_.get()) | |
268 return; | |
269 string16 name; | |
270 string16 keyword; | |
271 std::string url; | |
272 std::string modelIndex; | |
273 if (!args->GetString(ENGINE_NAME, &name) || | |
274 !args->GetString(ENGINE_KEYWORD, &keyword) || | |
275 !args->GetString(ENGINE_URL, &url) || | |
276 !args->GetString(3, &modelIndex)) { | |
277 NOTREACHED(); | |
278 return; | |
279 } | |
280 | |
281 base::DictionaryValue validity; | |
282 validity.SetBoolean("name", edit_controller_->IsTitleValid(name)); | |
283 validity.SetBoolean("keyword", edit_controller_->IsKeywordValid(keyword)); | |
284 validity.SetBoolean("url", edit_controller_->IsURLValid(url)); | |
285 StringValue indexValue(modelIndex); | |
286 web_ui()->CallJavascriptFunction("SearchEngineManager.validityCheckCallback", | |
287 validity, indexValue); | |
288 } | |
289 | |
290 void SearchEngineManagerHandler::EditCancelled(const ListValue* args) { | |
291 if (!edit_controller_.get()) | |
292 return; | |
293 edit_controller_->CleanUpCancelledAdd(); | |
294 edit_controller_.reset(); | |
295 } | |
296 | |
297 void SearchEngineManagerHandler::EditCompleted(const ListValue* args) { | |
298 if (!edit_controller_.get()) | |
299 return; | |
300 string16 name; | |
301 string16 keyword; | |
302 std::string url; | |
303 if (!args->GetString(ENGINE_NAME, &name) || | |
304 !args->GetString(ENGINE_KEYWORD, &keyword) || | |
305 !args->GetString(ENGINE_URL, &url)) { | |
306 NOTREACHED(); | |
307 return; | |
308 } | |
309 // Recheck validity. It's possible to get here with invalid input if e.g. the | |
310 // user calls the right JS functions directly from the web inspector. | |
311 if (edit_controller_->IsTitleValid(name) && | |
312 edit_controller_->IsKeywordValid(keyword) && | |
313 edit_controller_->IsURLValid(url)) | |
314 edit_controller_->AcceptAddOrEdit(name, keyword, url); | |
315 } | |
316 | |
317 } // namespace options | |
OLD | NEW |