| 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/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 int default_index = list_controller_->table_model()->IndexOfTemplateURL( | 122 int default_index = list_controller_->table_model()->IndexOfTemplateURL( |
| 123 default_engine); | 123 default_engine); |
| 124 | 124 |
| 125 // Build the first list (default search engine options). | 125 // Build the first list (default search engine options). |
| 126 base::ListValue defaults_list; | 126 base::ListValue defaults_list; |
| 127 int last_default_engine_index = | 127 int last_default_engine_index = |
| 128 list_controller_->table_model()->last_search_engine_index(); | 128 list_controller_->table_model()->last_search_engine_index(); |
| 129 for (int i = 0; i < last_default_engine_index; ++i) { | 129 for (int i = 0; i < last_default_engine_index; ++i) { |
| 130 // Third argument is false, as the engine is not from an extension. | 130 // Third argument is false, as the engine is not from an extension. |
| 131 defaults_list.Append(CreateDictionaryForEngine( | 131 defaults_list.Append(CreateDictionaryForEngine( |
| 132 i, i == default_index, false)); | 132 i, i == default_index)); |
| 133 } | 133 } |
| 134 | 134 |
| 135 // Build the second list (other search templates). | 135 // Build the second list (other search templates). |
| 136 base::ListValue others_list; | 136 base::ListValue others_list; |
| 137 int last_other_engine_index = | 137 int last_other_engine_index = |
| 138 list_controller_->table_model()->last_other_engine_index(); | 138 list_controller_->table_model()->last_other_engine_index(); |
| 139 if (last_default_engine_index < 0) | 139 if (last_default_engine_index < 0) |
| 140 last_default_engine_index = 0; | 140 last_default_engine_index = 0; |
| 141 for (int i = last_default_engine_index; i < last_other_engine_index; ++i) { | 141 for (int i = last_default_engine_index; i < last_other_engine_index; ++i) { |
| 142 others_list.Append(CreateDictionaryForEngine(i, i == default_index, false)); | 142 others_list.Append(CreateDictionaryForEngine(i, i == default_index)); |
| 143 } | 143 } |
| 144 | 144 |
| 145 // Build the extension keywords list. | 145 // Build the extension keywords list. |
| 146 base::ListValue keyword_list; | 146 base::ListValue keyword_list; |
| 147 if (last_other_engine_index < 0) | 147 if (last_other_engine_index < 0) |
| 148 last_other_engine_index = 0; | 148 last_other_engine_index = 0; |
| 149 int engine_count = list_controller_->table_model()->RowCount(); | 149 int engine_count = list_controller_->table_model()->RowCount(); |
| 150 for (int i = last_other_engine_index; i < engine_count; ++i) { | 150 for (int i = last_other_engine_index; i < engine_count; ++i) { |
| 151 keyword_list.Append(CreateDictionaryForEngine(i, i == default_index, true)); | 151 keyword_list.Append(CreateDictionaryForEngine(i, i == default_index)); |
| 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::CreateDictionaryForEngine( | 170 base::DictionaryValue* SearchEngineManagerHandler::CreateDictionaryForEngine( |
| 171 int index, bool is_default, bool is_extension) { | 171 int index, bool is_default) { |
| 172 TemplateURLTableModel* table_model = list_controller_->table_model(); | 172 TemplateURLTableModel* table_model = list_controller_->table_model(); |
| 173 const TemplateURL* template_url = list_controller_->GetTemplateURL(index); | 173 const TemplateURL* template_url = list_controller_->GetTemplateURL(index); |
| 174 | 174 |
| 175 // The items which are to be written into |dict| are also described in | 175 // The items which are to be written into |dict| are also described in |
| 176 // chrome/browser/resources/options/search_engine_manager_engine_list.js | 176 // chrome/browser/resources/options/search_engine_manager_engine_list.js |
| 177 // in @typedef for SearchEngine. Please update it whenever you add or remove | 177 // in @typedef for SearchEngine. Please update it whenever you add or remove |
| 178 // any keys here. | 178 // any keys here. |
| 179 base::DictionaryValue* dict = new base::DictionaryValue(); | 179 base::DictionaryValue* dict = new base::DictionaryValue(); |
| 180 dict->SetString("name", template_url->short_name()); | 180 dict->SetString("name", template_url->short_name()); |
| 181 dict->SetString("displayName", table_model->GetText( | 181 dict->SetString("displayName", table_model->GetText( |
| 182 index, IDS_SEARCH_ENGINES_EDITOR_DESCRIPTION_COLUMN)); | 182 index, IDS_SEARCH_ENGINES_EDITOR_DESCRIPTION_COLUMN)); |
| 183 dict->SetString("keyword", table_model->GetText( | 183 dict->SetString("keyword", table_model->GetText( |
| 184 index, IDS_SEARCH_ENGINES_EDITOR_KEYWORD_COLUMN)); | 184 index, IDS_SEARCH_ENGINES_EDITOR_KEYWORD_COLUMN)); |
| 185 dict->SetString("url", template_url->url_ref().DisplayURL( | 185 dict->SetString("url", template_url->url_ref().DisplayURL( |
| 186 UIThreadSearchTermsData(Profile::FromWebUI(web_ui())))); | 186 UIThreadSearchTermsData(Profile::FromWebUI(web_ui())))); |
| 187 dict->SetBoolean("urlLocked", template_url->prepopulate_id() > 0); | 187 dict->SetBoolean("urlLocked", template_url->prepopulate_id() > 0); |
| 188 GURL icon_url = template_url->favicon_url(); | 188 GURL icon_url = template_url->favicon_url(); |
| 189 if (icon_url.is_valid()) | 189 if (icon_url.is_valid()) |
| 190 dict->SetString("iconURL", icon_url.spec()); | 190 dict->SetString("iconURL", icon_url.spec()); |
| 191 dict->SetString("modelIndex", base::IntToString(index)); | 191 dict->SetString("modelIndex", base::IntToString(index)); |
| 192 | 192 |
| 193 dict->SetBoolean("canBeRemoved", | 193 dict->SetBoolean("canBeRemoved", |
| 194 list_controller_->CanRemove(template_url) && !is_extension); | 194 list_controller_->CanRemove(template_url)); |
| 195 dict->SetBoolean("canBeDefault", | 195 dict->SetBoolean("canBeDefault", |
| 196 list_controller_->CanMakeDefault(template_url) && !is_extension); | 196 list_controller_->CanMakeDefault(template_url)); |
| 197 dict->SetBoolean("default", is_default); | 197 dict->SetBoolean("default", is_default); |
| 198 dict->SetBoolean("canBeEdited", list_controller_->CanEdit(template_url)); | 198 dict->SetBoolean("canBeEdited", list_controller_->CanEdit(template_url)); |
| 199 dict->SetBoolean("isExtension", is_extension); | 199 TemplateURL::Type type = template_url->GetType(); |
| 200 if (template_url->GetType() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) { | 200 dict->SetBoolean("isOmniboxExtension", |
| 201 type == TemplateURL::OMNIBOX_API_EXTENSION); |
| 202 if (type == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION || |
| 203 type == TemplateURL::OMNIBOX_API_EXTENSION) { |
| 201 const extensions::Extension* extension = | 204 const extensions::Extension* extension = |
| 202 extensions::ExtensionRegistry::Get(Profile::FromWebUI(web_ui())) | 205 extensions::ExtensionRegistry::Get(Profile::FromWebUI(web_ui())) |
| 203 ->GetExtensionById(template_url->GetExtensionId(), | 206 ->GetExtensionById(template_url->GetExtensionId(), |
| 204 extensions::ExtensionRegistry::EVERYTHING); | 207 extensions::ExtensionRegistry::EVERYTHING); |
| 205 if (extension) { | 208 if (extension) { |
| 206 dict->Set("extension", | 209 dict->Set("extension", |
| 207 extensions::util::GetExtensionInfo(extension).release()); | 210 extensions::util::GetExtensionInfo(extension).release()); |
| 208 } | 211 } |
| 209 } | 212 } |
| 210 return dict; | 213 return dict; |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 | 322 |
| 320 // Recheck validity. It's possible to get here with invalid input if e.g. the | 323 // Recheck validity. It's possible to get here with invalid input if e.g. the |
| 321 // user calls the right JS functions directly from the web inspector. | 324 // user calls the right JS functions directly from the web inspector. |
| 322 if (edit_controller_->IsTitleValid(name) && | 325 if (edit_controller_->IsTitleValid(name) && |
| 323 edit_controller_->IsKeywordValid(keyword) && | 326 edit_controller_->IsKeywordValid(keyword) && |
| 324 edit_controller_->IsURLValid(url)) | 327 edit_controller_->IsURLValid(url)) |
| 325 edit_controller_->AcceptAddOrEdit(name, keyword, url); | 328 edit_controller_->AcceptAddOrEdit(name, keyword, url); |
| 326 } | 329 } |
| 327 | 330 |
| 328 } // namespace options | 331 } // namespace options |
| OLD | NEW |