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 // Third argument is false, as the engine is not from an extension. |
| 130 defaults_list.Append(CreateDictionaryForEngine( |
| 131 i, i == default_index, false)); |
130 } | 132 } |
131 | 133 |
132 // Build the second list (other search templates). | 134 // Build the second list (other search templates). |
133 ListValue others_list; | 135 ListValue others_list; |
| 136 int last_other_engine_index = |
| 137 list_controller_->table_model()->last_other_engine_index(); |
134 if (last_default_engine_index < 0) | 138 if (last_default_engine_index < 0) |
135 last_default_engine_index = 0; | 139 last_default_engine_index = 0; |
136 int engine_count = list_controller_->table_model()->RowCount(); | 140 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) { | 141 others_list.Append(CreateDictionaryForEngine(i, i == default_index, false)); |
138 others_list.Append(CreateDictionaryForEngine(i, i == default_index)); | |
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, true)); |
148 it != extensions->end(); ++it) { | |
149 if (extensions::OmniboxInfo::GetKeyword(*it).size() > 0) | |
150 keyword_list.Append(CreateDictionaryForExtension(*(*it))); | |
151 } | |
152 } | 151 } |
153 | 152 |
154 web_ui()->CallJavascriptFunction("SearchEngineManager.updateSearchEngineList", | 153 web_ui()->CallJavascriptFunction("SearchEngineManager.updateSearchEngineList", |
155 defaults_list, others_list, keyword_list); | 154 defaults_list, others_list, keyword_list); |
156 } | 155 } |
157 | 156 |
158 void SearchEngineManagerHandler::OnItemsChanged(int start, int length) { | 157 void SearchEngineManagerHandler::OnItemsChanged(int start, int length) { |
159 OnModelChanged(); | 158 OnModelChanged(); |
160 } | 159 } |
161 | 160 |
162 void SearchEngineManagerHandler::OnItemsAdded(int start, int length) { | 161 void SearchEngineManagerHandler::OnItemsAdded(int start, int length) { |
163 OnModelChanged(); | 162 OnModelChanged(); |
164 } | 163 } |
165 | 164 |
166 void SearchEngineManagerHandler::OnItemsRemoved(int start, int length) { | 165 void SearchEngineManagerHandler::OnItemsRemoved(int start, int length) { |
167 OnModelChanged(); | 166 OnModelChanged(); |
168 } | 167 } |
169 | 168 |
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( | 169 base::DictionaryValue* SearchEngineManagerHandler::CreateDictionaryForEngine( |
185 int index, bool is_default) { | 170 int index, bool is_default, bool is_extension) { |
186 TemplateURLTableModel* table_model = list_controller_->table_model(); | 171 TemplateURLTableModel* table_model = list_controller_->table_model(); |
187 const TemplateURL* template_url = list_controller_->GetTemplateURL(index); | 172 const TemplateURL* template_url = list_controller_->GetTemplateURL(index); |
188 | 173 |
189 base::DictionaryValue* dict = new base::DictionaryValue(); | 174 base::DictionaryValue* dict = new base::DictionaryValue(); |
190 dict->SetString("name", template_url->short_name()); | 175 dict->SetString("name", template_url->short_name()); |
191 dict->SetString("displayName", table_model->GetText( | 176 dict->SetString("displayName", table_model->GetText( |
192 index, IDS_SEARCH_ENGINES_EDITOR_DESCRIPTION_COLUMN)); | 177 index, IDS_SEARCH_ENGINES_EDITOR_DESCRIPTION_COLUMN)); |
193 dict->SetString("keyword", table_model->GetText( | 178 dict->SetString("keyword", table_model->GetText( |
194 index, IDS_SEARCH_ENGINES_EDITOR_KEYWORD_COLUMN)); | 179 index, IDS_SEARCH_ENGINES_EDITOR_KEYWORD_COLUMN)); |
195 dict->SetString("url", template_url->url_ref().DisplayURL()); | 180 dict->SetString("url", template_url->url_ref().DisplayURL()); |
196 dict->SetBoolean("urlLocked", template_url->prepopulate_id() > 0); | 181 dict->SetBoolean("urlLocked", template_url->prepopulate_id() > 0); |
197 GURL icon_url = template_url->favicon_url(); | 182 GURL icon_url = template_url->favicon_url(); |
198 if (icon_url.is_valid()) | 183 if (icon_url.is_valid()) |
199 dict->SetString("iconURL", icon_url.spec()); | 184 dict->SetString("iconURL", icon_url.spec()); |
200 dict->SetString("modelIndex", base::IntToString(index)); | 185 dict->SetString("modelIndex", base::IntToString(index)); |
201 | 186 |
202 if (list_controller_->CanRemove(template_url)) | 187 dict->SetBoolean("canBeRemoved", |
203 dict->SetString("canBeRemoved", "1"); | 188 list_controller_->CanRemove(template_url) && !is_extension); |
204 if (list_controller_->CanMakeDefault(template_url)) | 189 dict->SetBoolean("canBeDefault", |
205 dict->SetString("canBeDefault", "1"); | 190 list_controller_->CanMakeDefault(template_url) && !is_extension); |
206 if (is_default) | 191 dict->SetBoolean("default", is_default); |
207 dict->SetString("default", "1"); | 192 dict->SetBoolean("canBeEdited", list_controller_->CanEdit(template_url)); |
208 if (list_controller_->CanEdit(template_url)) | 193 dict->SetBoolean("isExtension", is_extension); |
209 dict->SetString("canBeEdited", "1"); | |
210 | 194 |
211 return dict; | 195 return dict; |
212 } | 196 } |
213 | 197 |
214 void SearchEngineManagerHandler::SetDefaultSearchEngine(const ListValue* args) { | 198 void SearchEngineManagerHandler::SetDefaultSearchEngine(const ListValue* args) { |
215 int index; | 199 int index; |
216 if (!ExtractIntegerValue(args, &index)) { | 200 if (!ExtractIntegerValue(args, &index)) { |
217 NOTREACHED(); | 201 NOTREACHED(); |
218 return; | 202 return; |
219 } | 203 } |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 return; | 287 return; |
304 string16 name; | 288 string16 name; |
305 string16 keyword; | 289 string16 keyword; |
306 std::string url; | 290 std::string url; |
307 if (!args->GetString(ENGINE_NAME, &name) || | 291 if (!args->GetString(ENGINE_NAME, &name) || |
308 !args->GetString(ENGINE_KEYWORD, &keyword) || | 292 !args->GetString(ENGINE_KEYWORD, &keyword) || |
309 !args->GetString(ENGINE_URL, &url)) { | 293 !args->GetString(ENGINE_URL, &url)) { |
310 NOTREACHED(); | 294 NOTREACHED(); |
311 return; | 295 return; |
312 } | 296 } |
| 297 |
313 // Recheck validity. It's possible to get here with invalid input if e.g. the | 298 // 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. | 299 // user calls the right JS functions directly from the web inspector. |
315 if (edit_controller_->IsTitleValid(name) && | 300 if (edit_controller_->IsTitleValid(name) && |
316 edit_controller_->IsKeywordValid(keyword) && | 301 edit_controller_->IsKeywordValid(keyword) && |
317 edit_controller_->IsURLValid(url)) | 302 edit_controller_->IsURLValid(url)) |
318 edit_controller_->AcceptAddOrEdit(name, keyword, url); | 303 edit_controller_->AcceptAddOrEdit(name, keyword, url); |
319 } | 304 } |
320 | 305 |
321 } // namespace options | 306 } // namespace options |
OLD | NEW |