Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(87)

Side by Side Diff: chrome/browser/ui/webui/settings/search_engines_handler.cc

Issue 1988463002: MD Settings: Convert C++ handlers to be JavaScript-lifecycle aware. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/settings/search_engines_handler.h" 5 #include "chrome/browser/ui/webui/settings/search_engines_handler.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
11 #include "base/values.h" 11 #include "base/values.h"
12 #include "chrome/browser/extensions/extension_service.h" 12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/extensions/extension_util.h" 13 #include "chrome/browser/extensions/extension_util.h"
14 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" 15 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
16 #include "chrome/browser/ui/search_engines/keyword_editor_controller.h"
17 #include "chrome/browser/ui/search_engines/template_url_table_model.h" 16 #include "chrome/browser/ui/search_engines/template_url_table_model.h"
18 #include "chrome/common/url_constants.h" 17 #include "chrome/common/url_constants.h"
19 #include "chrome/grit/generated_resources.h" 18 #include "chrome/grit/generated_resources.h"
20 #include "chrome/grit/locale_settings.h" 19 #include "chrome/grit/locale_settings.h"
21 #include "components/search_engines/template_url.h" 20 #include "components/search_engines/template_url.h"
22 #include "components/search_engines/template_url_service.h" 21 #include "components/search_engines/template_url_service.h"
23 #include "content/public/browser/user_metrics.h" 22 #include "content/public/browser/user_metrics.h"
24 #include "content/public/browser/web_ui.h" 23 #include "content/public/browser/web_ui.h"
25 #include "extensions/browser/extension_registry.h" 24 #include "extensions/browser/extension_registry.h"
26 #include "extensions/browser/extension_system.h" 25 #include "extensions/browser/extension_system.h"
27 #include "extensions/common/extension.h" 26 #include "extensions/common/extension.h"
28 27
29 namespace { 28 namespace {
30 // The following strings need to match with the IDs of the paper-input elements 29 // The following strings need to match with the IDs of the paper-input elements
31 // at settings/search_engines_page/add_search_engine_dialog.html. 30 // at settings/search_engines_page/add_search_engine_dialog.html.
32 const char kSearchEngineField[] = "searchEngine"; 31 const char kSearchEngineField[] = "searchEngine";
33 const char kKeywordField[] = "keyword"; 32 const char kKeywordField[] = "keyword";
34 const char kQueryUrlField[] = "queryUrl"; 33 const char kQueryUrlField[] = "queryUrl";
35 34
36 // Dummy number used for indicating that a new search engine is added. 35 // Dummy number used for indicating that a new search engine is added.
37 const int kNewSearchEngineIndex = -1; 36 const int kNewSearchEngineIndex = -1;
38 } // namespace 37 } // namespace
39 38
40 namespace settings { 39 namespace settings {
41 40
42 SearchEnginesHandler::SearchEnginesHandler(Profile* profile) 41 SearchEnginesHandler::SearchEnginesHandler(Profile* profile)
43 : profile_(profile) { 42 : profile_(profile), list_controller_(profile) {}
44 list_controller_.reset(new KeywordEditorController(profile));
45 DCHECK(list_controller_);
46 list_controller_->table_model()->SetObserver(this);
47 }
48 43
49 SearchEnginesHandler::~SearchEnginesHandler() { 44 SearchEnginesHandler::~SearchEnginesHandler() {
50 if (list_controller_.get() && list_controller_->table_model()) 45 // TODO(tommycli): Refactor KeywordEditorController to be compatible with
51 list_controller_->table_model()->SetObserver(nullptr); 46 // ScopedObserver so this is no longer necessary.
47 list_controller_.table_model()->SetObserver(nullptr);
52 } 48 }
53 49
54 void SearchEnginesHandler::RegisterMessages() { 50 void SearchEnginesHandler::RegisterMessages() {
55 web_ui()->RegisterMessageCallback( 51 web_ui()->RegisterMessageCallback(
56 "getSearchEnginesList", 52 "getSearchEnginesList",
57 base::Bind(&SearchEnginesHandler::HandleGetSearchEnginesList, 53 base::Bind(&SearchEnginesHandler::HandleGetSearchEnginesList,
58 base::Unretained(this))); 54 base::Unretained(this)));
59 web_ui()->RegisterMessageCallback( 55 web_ui()->RegisterMessageCallback(
60 "setDefaultSearchEngine", 56 "setDefaultSearchEngine",
61 base::Bind(&SearchEnginesHandler::HandleSetDefaultSearchEngine, 57 base::Bind(&SearchEnginesHandler::HandleSetDefaultSearchEngine,
(...skipping 17 matching lines...) Expand all
79 web_ui()->RegisterMessageCallback( 75 web_ui()->RegisterMessageCallback(
80 "searchEngineEditCompleted", 76 "searchEngineEditCompleted",
81 base::Bind(&SearchEnginesHandler::HandleSearchEngineEditCompleted, 77 base::Bind(&SearchEnginesHandler::HandleSearchEngineEditCompleted,
82 base::Unretained(this))); 78 base::Unretained(this)));
83 web_ui()->RegisterMessageCallback( 79 web_ui()->RegisterMessageCallback(
84 "disableExtension", 80 "disableExtension",
85 base::Bind(&SearchEnginesHandler::HandleDisableExtension, 81 base::Bind(&SearchEnginesHandler::HandleDisableExtension,
86 base::Unretained(this))); 82 base::Unretained(this)));
87 } 83 }
88 84
85 void SearchEnginesHandler::OnJavascriptAllowed() {
86 list_controller_.table_model()->SetObserver(this);
87 }
88
89 void SearchEnginesHandler::OnJavascriptDisallowed() {
90 list_controller_.table_model()->SetObserver(nullptr);
91 }
92
89 std::unique_ptr<base::DictionaryValue> 93 std::unique_ptr<base::DictionaryValue>
90 SearchEnginesHandler::GetSearchEnginesList() { 94 SearchEnginesHandler::GetSearchEnginesList() {
91 DCHECK(list_controller_.get()); 95 AllowJavascript();
96
92 // Find the default engine. 97 // Find the default engine.
93 const TemplateURL* default_engine = 98 const TemplateURL* default_engine =
94 list_controller_->GetDefaultSearchProvider(); 99 list_controller_.GetDefaultSearchProvider();
95 int default_index = 100 int default_index =
96 list_controller_->table_model()->IndexOfTemplateURL(default_engine); 101 list_controller_.table_model()->IndexOfTemplateURL(default_engine);
97 102
98 // Build the first list (default search engines). 103 // Build the first list (default search engines).
99 std::unique_ptr<base::ListValue> defaults = 104 std::unique_ptr<base::ListValue> defaults =
100 base::WrapUnique(new base::ListValue()); 105 base::WrapUnique(new base::ListValue());
101 int last_default_engine_index = 106 int last_default_engine_index =
102 list_controller_->table_model()->last_search_engine_index(); 107 list_controller_.table_model()->last_search_engine_index();
103 for (int i = 0; i < last_default_engine_index; ++i) { 108 for (int i = 0; i < last_default_engine_index; ++i) {
104 // Third argument is false, as the engine is not from an extension. 109 // Third argument is false, as the engine is not from an extension.
105 defaults->Append(CreateDictionaryForEngine(i, i == default_index)); 110 defaults->Append(CreateDictionaryForEngine(i, i == default_index));
106 } 111 }
107 112
108 // Build the second list (other search engines). 113 // Build the second list (other search engines).
109 std::unique_ptr<base::ListValue> others = 114 std::unique_ptr<base::ListValue> others =
110 base::WrapUnique(new base::ListValue()); 115 base::WrapUnique(new base::ListValue());
111 int last_other_engine_index = 116 int last_other_engine_index =
112 list_controller_->table_model()->last_other_engine_index(); 117 list_controller_.table_model()->last_other_engine_index();
113 for (int i = std::max(last_default_engine_index, 0); 118 for (int i = std::max(last_default_engine_index, 0);
114 i < last_other_engine_index; ++i) { 119 i < last_other_engine_index; ++i) {
115 others->Append(CreateDictionaryForEngine(i, i == default_index)); 120 others->Append(CreateDictionaryForEngine(i, i == default_index));
116 } 121 }
117 122
118 // Build the third list (omnibox extensions). 123 // Build the third list (omnibox extensions).
119 std::unique_ptr<base::ListValue> extensions = 124 std::unique_ptr<base::ListValue> extensions =
120 base::WrapUnique(new base::ListValue()); 125 base::WrapUnique(new base::ListValue());
121 int engine_count = list_controller_->table_model()->RowCount(); 126 int engine_count = list_controller_.table_model()->RowCount();
122 for (int i = std::max(last_other_engine_index, 0); i < engine_count; ++i) { 127 for (int i = std::max(last_other_engine_index, 0); i < engine_count; ++i) {
123 extensions->Append(CreateDictionaryForEngine(i, i == default_index)); 128 extensions->Append(CreateDictionaryForEngine(i, i == default_index));
124 } 129 }
125 130
126 std::unique_ptr<base::DictionaryValue> search_engines_info( 131 std::unique_ptr<base::DictionaryValue> search_engines_info(
127 new base::DictionaryValue); 132 new base::DictionaryValue);
128 search_engines_info->Set("defaults", base::WrapUnique(defaults.release())); 133 search_engines_info->Set("defaults", base::WrapUnique(defaults.release()));
129 search_engines_info->Set("others", base::WrapUnique(others.release())); 134 search_engines_info->Set("others", base::WrapUnique(others.release()));
130 search_engines_info->Set("extensions", 135 search_engines_info->Set("extensions",
131 base::WrapUnique(extensions.release())); 136 base::WrapUnique(extensions.release()));
132 return search_engines_info; 137 return search_engines_info;
133 } 138 }
134 139
135 void SearchEnginesHandler::OnModelChanged() { 140 void SearchEnginesHandler::OnModelChanged() {
136 web_ui()->CallJavascriptFunction("cr.webUIListenerCallback", 141 CallJavascriptFunction("cr.webUIListenerCallback",
137 base::StringValue("search-engines-changed"), 142 base::StringValue("search-engines-changed"),
138 *GetSearchEnginesList()); 143 *GetSearchEnginesList());
139 } 144 }
140 145
141 void SearchEnginesHandler::OnItemsChanged(int start, int length) { 146 void SearchEnginesHandler::OnItemsChanged(int start, int length) {
142 OnModelChanged(); 147 OnModelChanged();
143 } 148 }
144 149
145 void SearchEnginesHandler::OnItemsAdded(int start, int length) { 150 void SearchEnginesHandler::OnItemsAdded(int start, int length) {
146 OnModelChanged(); 151 OnModelChanged();
147 } 152 }
148 153
149 void SearchEnginesHandler::OnItemsRemoved(int start, int length) { 154 void SearchEnginesHandler::OnItemsRemoved(int start, int length) {
150 OnModelChanged(); 155 OnModelChanged();
151 } 156 }
152 157
153 base::DictionaryValue* SearchEnginesHandler::CreateDictionaryForEngine( 158 base::DictionaryValue* SearchEnginesHandler::CreateDictionaryForEngine(
154 int index, 159 int index,
155 bool is_default) { 160 bool is_default) {
156 TemplateURLTableModel* table_model = list_controller_->table_model(); 161 TemplateURLTableModel* table_model = list_controller_.table_model();
157 const TemplateURL* template_url = list_controller_->GetTemplateURL(index); 162 const TemplateURL* template_url = list_controller_.GetTemplateURL(index);
158 163
159 // The items which are to be written into |dict| are also described in 164 // The items which are to be written into |dict| are also described in
160 // chrome/browser/resources/settings/search_engines_page/ 165 // chrome/browser/resources/settings/search_engines_page/
161 // in @typedef for SearchEngine. Please update it whenever you add or remove 166 // in @typedef for SearchEngine. Please update it whenever you add or remove
162 // any keys here. 167 // any keys here.
163 base::DictionaryValue* dict = new base::DictionaryValue(); 168 base::DictionaryValue* dict = new base::DictionaryValue();
164 dict->SetString("name", template_url->short_name()); 169 dict->SetString("name", template_url->short_name());
165 dict->SetString("displayName", 170 dict->SetString("displayName",
166 table_model->GetText( 171 table_model->GetText(
167 index, IDS_SEARCH_ENGINES_EDITOR_DESCRIPTION_COLUMN)); 172 index, IDS_SEARCH_ENGINES_EDITOR_DESCRIPTION_COLUMN));
168 dict->SetString( 173 dict->SetString(
169 "keyword", 174 "keyword",
170 table_model->GetText(index, IDS_SEARCH_ENGINES_EDITOR_KEYWORD_COLUMN)); 175 table_model->GetText(index, IDS_SEARCH_ENGINES_EDITOR_KEYWORD_COLUMN));
171 dict->SetString("url", 176 dict->SetString("url",
172 template_url->url_ref().DisplayURL( 177 template_url->url_ref().DisplayURL(
173 UIThreadSearchTermsData(Profile::FromWebUI(web_ui())))); 178 UIThreadSearchTermsData(Profile::FromWebUI(web_ui()))));
174 dict->SetBoolean("urlLocked", template_url->prepopulate_id() > 0); 179 dict->SetBoolean("urlLocked", template_url->prepopulate_id() > 0);
175 GURL icon_url = template_url->favicon_url(); 180 GURL icon_url = template_url->favicon_url();
176 if (icon_url.is_valid()) 181 if (icon_url.is_valid())
177 dict->SetString("iconURL", icon_url.spec()); 182 dict->SetString("iconURL", icon_url.spec());
178 dict->SetInteger("modelIndex", index); 183 dict->SetInteger("modelIndex", index);
179 184
180 dict->SetBoolean("canBeRemoved", list_controller_->CanRemove(template_url)); 185 dict->SetBoolean("canBeRemoved", list_controller_.CanRemove(template_url));
181 dict->SetBoolean("canBeDefault", 186 dict->SetBoolean("canBeDefault",
182 list_controller_->CanMakeDefault(template_url)); 187 list_controller_.CanMakeDefault(template_url));
183 dict->SetBoolean("default", is_default); 188 dict->SetBoolean("default", is_default);
184 dict->SetBoolean("canBeEdited", list_controller_->CanEdit(template_url)); 189 dict->SetBoolean("canBeEdited", list_controller_.CanEdit(template_url));
185 TemplateURL::Type type = template_url->GetType(); 190 TemplateURL::Type type = template_url->GetType();
186 dict->SetBoolean("isOmniboxExtension", 191 dict->SetBoolean("isOmniboxExtension",
187 type == TemplateURL::OMNIBOX_API_EXTENSION); 192 type == TemplateURL::OMNIBOX_API_EXTENSION);
188 if (type == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION || 193 if (type == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION ||
189 type == TemplateURL::OMNIBOX_API_EXTENSION) { 194 type == TemplateURL::OMNIBOX_API_EXTENSION) {
190 const extensions::Extension* extension = 195 const extensions::Extension* extension =
191 extensions::ExtensionRegistry::Get(Profile::FromWebUI(web_ui())) 196 extensions::ExtensionRegistry::Get(Profile::FromWebUI(web_ui()))
192 ->GetExtensionById(template_url->GetExtensionId(), 197 ->GetExtensionById(template_url->GetExtensionId(),
193 extensions::ExtensionRegistry::EVERYTHING); 198 extensions::ExtensionRegistry::EVERYTHING);
194 if (extension) { 199 if (extension) {
(...skipping 12 matching lines...) Expand all
207 ResolveJavascriptCallback(*callback_id, *GetSearchEnginesList()); 212 ResolveJavascriptCallback(*callback_id, *GetSearchEnginesList());
208 } 213 }
209 214
210 void SearchEnginesHandler::HandleSetDefaultSearchEngine( 215 void SearchEnginesHandler::HandleSetDefaultSearchEngine(
211 const base::ListValue* args) { 216 const base::ListValue* args) {
212 int index; 217 int index;
213 if (!ExtractIntegerValue(args, &index)) { 218 if (!ExtractIntegerValue(args, &index)) {
214 NOTREACHED(); 219 NOTREACHED();
215 return; 220 return;
216 } 221 }
217 if (index < 0 || index >= list_controller_->table_model()->RowCount()) 222 if (index < 0 || index >= list_controller_.table_model()->RowCount())
218 return; 223 return;
219 224
220 list_controller_->MakeDefaultTemplateURL(index); 225 list_controller_.MakeDefaultTemplateURL(index);
221 226
222 content::RecordAction( 227 content::RecordAction(
223 base::UserMetricsAction("Options_SearchEngineSetDefault")); 228 base::UserMetricsAction("Options_SearchEngineSetDefault"));
224 } 229 }
225 230
226 void SearchEnginesHandler::HandleRemoveSearchEngine( 231 void SearchEnginesHandler::HandleRemoveSearchEngine(
227 const base::ListValue* args) { 232 const base::ListValue* args) {
228 int index; 233 int index;
229 if (!ExtractIntegerValue(args, &index)) { 234 if (!ExtractIntegerValue(args, &index)) {
230 NOTREACHED(); 235 NOTREACHED();
231 return; 236 return;
232 } 237 }
233 if (index < 0 || index >= list_controller_->table_model()->RowCount()) 238 if (index < 0 || index >= list_controller_.table_model()->RowCount())
234 return; 239 return;
235 240
236 if (list_controller_->CanRemove(list_controller_->GetTemplateURL(index))) { 241 if (list_controller_.CanRemove(list_controller_.GetTemplateURL(index))) {
237 list_controller_->RemoveTemplateURL(index); 242 list_controller_.RemoveTemplateURL(index);
238 content::RecordAction( 243 content::RecordAction(
239 base::UserMetricsAction("Options_SearchEngineRemoved")); 244 base::UserMetricsAction("Options_SearchEngineRemoved"));
240 } 245 }
241 } 246 }
242 247
243 void SearchEnginesHandler::HandleSearchEngineEditStarted( 248 void SearchEnginesHandler::HandleSearchEngineEditStarted(
244 const base::ListValue* args) { 249 const base::ListValue* args) {
245 int index; 250 int index;
246 if (!ExtractIntegerValue(args, &index)) { 251 if (!ExtractIntegerValue(args, &index)) {
247 NOTREACHED(); 252 NOTREACHED();
248 return; 253 return;
249 } 254 }
250 255
251 // Allow -1, which means we are adding a new engine. 256 // Allow -1, which means we are adding a new engine.
252 if (index < kNewSearchEngineIndex || 257 if (index < kNewSearchEngineIndex ||
253 index >= list_controller_->table_model()->RowCount()) { 258 index >= list_controller_.table_model()->RowCount()) {
254 return; 259 return;
255 } 260 }
256 261
257 edit_controller_.reset(new EditSearchEngineController( 262 edit_controller_.reset(new EditSearchEngineController(
258 index == kNewSearchEngineIndex ? nullptr 263 index == kNewSearchEngineIndex ? nullptr
259 : list_controller_->GetTemplateURL(index), 264 : list_controller_.GetTemplateURL(index),
260 this, Profile::FromWebUI(web_ui()))); 265 this, Profile::FromWebUI(web_ui())));
261 } 266 }
262 267
263 void SearchEnginesHandler::OnEditedKeyword(TemplateURL* template_url, 268 void SearchEnginesHandler::OnEditedKeyword(TemplateURL* template_url,
264 const base::string16& title, 269 const base::string16& title,
265 const base::string16& keyword, 270 const base::string16& keyword,
266 const std::string& url) { 271 const std::string& url) {
267 DCHECK(!url.empty()); 272 DCHECK(!url.empty());
268 if (template_url) 273 if (template_url)
269 list_controller_->ModifyTemplateURL(template_url, title, keyword, url); 274 list_controller_.ModifyTemplateURL(template_url, title, keyword, url);
270 else 275 else
271 list_controller_->AddTemplateURL(title, keyword, url); 276 list_controller_.AddTemplateURL(title, keyword, url);
272 277
273 edit_controller_.reset(); 278 edit_controller_.reset();
274 } 279 }
275 280
276 void SearchEnginesHandler::HandleValidateSearchEngineInput( 281 void SearchEnginesHandler::HandleValidateSearchEngineInput(
277 const base::ListValue* args) { 282 const base::ListValue* args) {
278 CHECK_EQ(3U, args->GetSize()); 283 CHECK_EQ(3U, args->GetSize());
279 284
280 const base::Value* callback_id; 285 const base::Value* callback_id;
281 std::string field_name; 286 std::string field_name;
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 std::string extension_id; 345 std::string extension_id;
341 CHECK(args->GetString(0, &extension_id)); 346 CHECK(args->GetString(0, &extension_id));
342 ExtensionService* extension_service = 347 ExtensionService* extension_service =
343 extensions::ExtensionSystem::Get(profile_)->extension_service(); 348 extensions::ExtensionSystem::Get(profile_)->extension_service();
344 DCHECK(extension_service); 349 DCHECK(extension_service);
345 extension_service->DisableExtension( 350 extension_service->DisableExtension(
346 extension_id, extensions::Extension::DISABLE_USER_ACTION); 351 extension_id, extensions::Extension::DISABLE_USER_ACTION);
347 } 352 }
348 353
349 } // namespace settings 354 } // namespace settings
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698