Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/ui/webui/options/search_engine_manager_handler.h" | 5 #include "chrome/browser/ui/webui/options/search_engine_manager_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 119 const TemplateURL* default_engine = | 119 const TemplateURL* default_engine = |
| 120 list_controller_->url_model()->GetDefaultSearchProvider(); | 120 list_controller_->url_model()->GetDefaultSearchProvider(); |
| 121 int default_index = list_controller_->table_model()->IndexOfTemplateURL( | 121 int default_index = list_controller_->table_model()->IndexOfTemplateURL( |
| 122 default_engine); | 122 default_engine); |
| 123 | 123 |
| 124 // Build the first list (default search engine options). | 124 // Build the first list (default search engine options). |
| 125 ListValue defaults_list; | 125 ListValue defaults_list; |
| 126 int last_default_engine_index = | 126 int last_default_engine_index = |
| 127 list_controller_->table_model()->last_search_engine_index(); | 127 list_controller_->table_model()->last_search_engine_index(); |
| 128 for (int i = 0; i < last_default_engine_index; ++i) { | 128 for (int i = 0; i < last_default_engine_index; ++i) { |
| 129 defaults_list.Append(CreateDictionaryForEngine(i, i == default_index)); | 129 defaults_list.Append(CreateDictionaryForEngine(i, i == default_index, |
|
Devlin
2013/05/24 02:42:54
each param on own line, or all three on newline.
Aaron Jacobs
2013/05/24 03:06:14
Done.
| |
| 130 false)); | |
|
Devlin
2013/05/24 02:42:54
please document the anonymous boolean (it's not to
Aaron Jacobs
2013/05/24 03:06:14
Done.
| |
| 130 } | 131 } |
| 131 | 132 |
| 132 // Build the second list (other search templates). | 133 // Build the second list (other search templates). |
| 133 ListValue others_list; | 134 ListValue others_list; |
| 135 int last_other_engine_index = | |
| 136 list_controller_->table_model()->last_other_engine_index(); | |
| 134 if (last_default_engine_index < 0) | 137 if (last_default_engine_index < 0) |
| 135 last_default_engine_index = 0; | 138 last_default_engine_index = 0; |
| 136 int engine_count = list_controller_->table_model()->RowCount(); | 139 for (int i = last_default_engine_index; i < last_other_engine_index; ++i) { |
| 137 for (int i = last_default_engine_index; i < engine_count; ++i) { | 140 others_list.Append(CreateDictionaryForEngine(i, i == default_index, |
|
Devlin
2013/05/24 02:42:54
see 129.
Aaron Jacobs
2013/05/24 03:06:14
Done.
| |
| 138 others_list.Append(CreateDictionaryForEngine(i, i == default_index)); | 141 false)); |
| 139 } | 142 } |
| 140 | 143 |
| 141 // Build the extension keywords list. | 144 // Build the extension keywords list. |
| 142 ListValue keyword_list; | 145 ListValue keyword_list; |
| 143 ExtensionService* extension_service = | 146 if (last_other_engine_index < 0) |
| 144 Profile::FromWebUI(web_ui())->GetExtensionService(); | 147 last_other_engine_index = 0; |
| 145 if (extension_service) { | 148 int engine_count = list_controller_->table_model()->RowCount(); |
| 146 const ExtensionSet* extensions = extension_service->extensions(); | 149 for (int i = last_other_engine_index; i < engine_count; ++i) { |
| 147 for (ExtensionSet::const_iterator it = extensions->begin(); | 150 keyword_list.Append(CreateDictionaryForEngine(i, i == default_index, |
| 148 it != extensions->end(); ++it) { | 151 true)); |
|
Devlin
2013/05/24 02:42:54
see 129
Aaron Jacobs
2013/05/24 03:06:14
Done.
| |
| 149 if (extensions::OmniboxInfo::GetKeyword(*it).size() > 0) | |
| 150 keyword_list.Append(CreateDictionaryForExtension(*(*it))); | |
| 151 } | |
| 152 } | 152 } |
| 153 | 153 |
| 154 web_ui()->CallJavascriptFunction("SearchEngineManager.updateSearchEngineList", | 154 web_ui()->CallJavascriptFunction("SearchEngineManager.updateSearchEngineList", |
| 155 defaults_list, others_list, keyword_list); | 155 defaults_list, others_list, keyword_list); |
| 156 } | 156 } |
| 157 | 157 |
| 158 void SearchEngineManagerHandler::OnItemsChanged(int start, int length) { | 158 void SearchEngineManagerHandler::OnItemsChanged(int start, int length) { |
| 159 OnModelChanged(); | 159 OnModelChanged(); |
| 160 } | 160 } |
| 161 | 161 |
| 162 void SearchEngineManagerHandler::OnItemsAdded(int start, int length) { | 162 void SearchEngineManagerHandler::OnItemsAdded(int start, int length) { |
| 163 OnModelChanged(); | 163 OnModelChanged(); |
| 164 } | 164 } |
| 165 | 165 |
| 166 void SearchEngineManagerHandler::OnItemsRemoved(int start, int length) { | 166 void SearchEngineManagerHandler::OnItemsRemoved(int start, int length) { |
| 167 OnModelChanged(); | 167 OnModelChanged(); |
| 168 } | 168 } |
| 169 | 169 |
| 170 base::DictionaryValue* SearchEngineManagerHandler::CreateDictionaryForExtension( | |
| 171 const extensions::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", | |
| 176 extensions::OmniboxInfo::GetKeyword(&extension)); | |
| 177 GURL icon = extensions::IconsInfo::GetIconURL( | |
| 178 &extension, 16, ExtensionIconSet::MATCH_BIGGER); | |
| 179 dict->SetString("iconURL", icon.spec()); | |
| 180 dict->SetString("url", string16()); | |
| 181 return dict; | |
| 182 } | |
| 183 | |
| 184 base::DictionaryValue* SearchEngineManagerHandler::CreateDictionaryForEngine( | 170 base::DictionaryValue* SearchEngineManagerHandler::CreateDictionaryForEngine( |
| 185 int index, bool is_default) { | 171 int index, bool is_default, bool is_extension) { |
| 186 TemplateURLTableModel* table_model = list_controller_->table_model(); | 172 TemplateURLTableModel* table_model = list_controller_->table_model(); |
| 187 const TemplateURL* template_url = list_controller_->GetTemplateURL(index); | 173 const TemplateURL* template_url = list_controller_->GetTemplateURL(index); |
| 188 | 174 |
| 189 base::DictionaryValue* dict = new base::DictionaryValue(); | 175 base::DictionaryValue* dict = new base::DictionaryValue(); |
| 190 dict->SetString("name", template_url->short_name()); | 176 dict->SetString("name", template_url->short_name()); |
| 191 dict->SetString("displayName", table_model->GetText( | 177 dict->SetString("displayName", table_model->GetText( |
| 192 index, IDS_SEARCH_ENGINES_EDITOR_DESCRIPTION_COLUMN)); | 178 index, IDS_SEARCH_ENGINES_EDITOR_DESCRIPTION_COLUMN)); |
| 193 dict->SetString("keyword", table_model->GetText( | 179 dict->SetString("keyword", table_model->GetText( |
| 194 index, IDS_SEARCH_ENGINES_EDITOR_KEYWORD_COLUMN)); | 180 index, IDS_SEARCH_ENGINES_EDITOR_KEYWORD_COLUMN)); |
| 195 dict->SetString("url", template_url->url_ref().DisplayURL()); | 181 dict->SetString("url", template_url->url_ref().DisplayURL()); |
| 196 dict->SetBoolean("urlLocked", template_url->prepopulate_id() > 0); | 182 dict->SetBoolean("urlLocked", template_url->prepopulate_id() > 0); |
| 197 GURL icon_url = template_url->favicon_url(); | 183 GURL icon_url = template_url->favicon_url(); |
| 198 if (icon_url.is_valid()) | 184 if (icon_url.is_valid()) |
| 199 dict->SetString("iconURL", icon_url.spec()); | 185 dict->SetString("iconURL", icon_url.spec()); |
| 200 dict->SetString("modelIndex", base::IntToString(index)); | 186 dict->SetString("modelIndex", base::IntToString(index)); |
| 201 | 187 |
| 202 if (list_controller_->CanRemove(template_url)) | 188 if (list_controller_->CanRemove(template_url) && !is_extension) |
| 203 dict->SetString("canBeRemoved", "1"); | 189 dict->SetString("canBeRemoved", "1"); |
| 204 if (list_controller_->CanMakeDefault(template_url)) | 190 if (list_controller_->CanMakeDefault(template_url) && !is_extension) |
| 205 dict->SetString("canBeDefault", "1"); | 191 dict->SetString("canBeDefault", "1"); |
| 206 if (is_default) | 192 if (is_default) |
| 207 dict->SetString("default", "1"); | 193 dict->SetString("default", "1"); |
| 208 if (list_controller_->CanEdit(template_url)) | 194 if (list_controller_->CanEdit(template_url)) |
| 209 dict->SetString("canBeEdited", "1"); | 195 dict->SetString("canBeEdited", "1"); |
| 210 | 196 |
| 197 if (is_extension) | |
| 198 dict->SetString("isExtension", "1"); | |
| 199 | |
| 211 return dict; | 200 return dict; |
| 212 } | 201 } |
| 213 | 202 |
| 214 void SearchEngineManagerHandler::SetDefaultSearchEngine(const ListValue* args) { | 203 void SearchEngineManagerHandler::SetDefaultSearchEngine(const ListValue* args) { |
| 215 int index; | 204 int index; |
| 216 if (!ExtractIntegerValue(args, &index)) { | 205 if (!ExtractIntegerValue(args, &index)) { |
| 217 NOTREACHED(); | 206 NOTREACHED(); |
| 218 return; | 207 return; |
| 219 } | 208 } |
| 220 if (index < 0 || index >= list_controller_->table_model()->RowCount()) | 209 if (index < 0 || index >= list_controller_->table_model()->RowCount()) |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 303 return; | 292 return; |
| 304 string16 name; | 293 string16 name; |
| 305 string16 keyword; | 294 string16 keyword; |
| 306 std::string url; | 295 std::string url; |
| 307 if (!args->GetString(ENGINE_NAME, &name) || | 296 if (!args->GetString(ENGINE_NAME, &name) || |
| 308 !args->GetString(ENGINE_KEYWORD, &keyword) || | 297 !args->GetString(ENGINE_KEYWORD, &keyword) || |
| 309 !args->GetString(ENGINE_URL, &url)) { | 298 !args->GetString(ENGINE_URL, &url)) { |
| 310 NOTREACHED(); | 299 NOTREACHED(); |
| 311 return; | 300 return; |
| 312 } | 301 } |
| 302 | |
| 313 // Recheck validity. It's possible to get here with invalid input if e.g. the | 303 // Recheck validity. It's possible to get here with invalid input if e.g. the |
| 314 // user calls the right JS functions directly from the web inspector. | 304 // user calls the right JS functions directly from the web inspector. |
| 315 if (edit_controller_->IsTitleValid(name) && | 305 if (edit_controller_->IsTitleValid(name) && |
| 316 edit_controller_->IsKeywordValid(keyword) && | 306 edit_controller_->IsKeywordValid(keyword) && |
| 317 edit_controller_->IsURLValid(url)) | 307 edit_controller_->IsURLValid(url)) |
| 318 edit_controller_->AcceptAddOrEdit(name, keyword, url); | 308 edit_controller_->AcceptAddOrEdit(name, keyword, url); |
| 319 } | 309 } |
| 320 | 310 |
| 321 } // namespace options | 311 } // namespace options |
| OLD | NEW |