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