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