add-suggestions-url-field.patch 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. # Add suggestions URL text field to the search engine editing dialog
  2. # (chrome://settings/searchEngines).
  3. --- a/chrome/browser/resources/settings/search_engines_page/search_engine_dialog.html
  4. +++ b/chrome/browser/resources/settings/search_engines_page/search_engine_dialog.html
  5. @@ -31,6 +31,13 @@
  6. value="{{queryUrl_}}" on-focus="validate_" on-input="validate_"
  7. disabled$="[[model.urlLocked]]">
  8. </cr-input>
  9. + <cr-input id="suggestionsUrl"
  10. + label="Suggestions URL with %s in place of query"
  11. + error-message="$i18n{notValid}"
  12. + value="{{suggestionsUrl_}}"
  13. + on-focus="validate_" on-input="validate_"
  14. + disabled$="[[model.urlLocked]]">
  15. + </cr-input>
  16. </div>
  17. <div slot="button-container">
  18. <cr-button class="cancel-button" on-click="cancel_" id="cancel">
  19. --- a/chrome/browser/resources/settings/search_engines_page/search_engine_dialog.js
  20. +++ b/chrome/browser/resources/settings/search_engines_page/search_engine_dialog.js
  21. @@ -29,6 +29,9 @@ Polymer({
  22. queryUrl_: String,
  23. /** @private {string} */
  24. + suggestionsUrl_: String,
  25. +
  26. + /** @private {string} */
  27. dialogTitle_: String,
  28. /** @private {string} */
  29. @@ -62,6 +65,7 @@ Polymer({
  30. this.searchEngine_ = this.model.name;
  31. this.keyword_ = this.model.keyword;
  32. this.queryUrl_ = this.model.url;
  33. + this.suggestionsUrl_ = this.model.suggestionsUrl;
  34. } else {
  35. this.dialogTitle_ =
  36. loadTimeData.getString('searchEnginesAddSearchEngine');
  37. @@ -99,8 +103,12 @@ Polymer({
  38. }
  39. }
  40. - [this.$.searchEngine, this.$.keyword, this.$.queryUrl].forEach(
  41. - element => this.validateElement_(element));
  42. + [
  43. + this.$.searchEngine,
  44. + this.$.keyword,
  45. + this.$.queryUrl,
  46. + this.$.suggestionsUrl
  47. + ].forEach(element => this.validateElement_(element));
  48. },
  49. /** @private */
  50. @@ -111,7 +119,8 @@ Polymer({
  51. /** @private */
  52. onActionButtonTap_() {
  53. this.browserProxy_.searchEngineEditCompleted(
  54. - this.searchEngine_, this.keyword_, this.queryUrl_);
  55. + this.searchEngine_, this.keyword_, this.queryUrl_,
  56. + this.suggestionsUrl_);
  57. this.$.dialog.close();
  58. },
  59. @@ -148,9 +157,11 @@ Polymer({
  60. /** @private */
  61. updateActionButtonState_() {
  62. const allValid = [
  63. - this.$.searchEngine, this.$.keyword, this.$.queryUrl
  64. + this.$.searchEngine, this.$.keyword, this.$.queryUrl,
  65. + this.$.suggestionsUrl
  66. ].every(function(inputElement) {
  67. - return !inputElement.invalid && inputElement.value.length > 0;
  68. + return !inputElement.invalid && (inputElement.value.length > 0 ||
  69. + inputElement.id == 'suggestionsUrl');
  70. });
  71. this.$.actionButton.disabled = !allValid;
  72. },
  73. --- a/chrome/browser/resources/settings/search_engines_page/search_engines_browser_proxy.js
  74. +++ b/chrome/browser/resources/settings/search_engines_page/search_engines_browser_proxy.js
  75. @@ -28,6 +28,7 @@
  76. * modelIndex: number,
  77. * name: string,
  78. * url: string,
  79. + * suggestionsUrl: string,
  80. * urlLocked: boolean}}
  81. * @see chrome/browser/ui/webui/settings/search_engine_manager_handler.cc
  82. */
  83. @@ -60,8 +61,10 @@ cr.define('settings', function() {
  84. * @param {string} searchEngine
  85. * @param {string} keyword
  86. * @param {string} queryUrl
  87. + * @param {string} suggestionsUrl
  88. */
  89. - searchEngineEditCompleted(searchEngine, keyword, queryUrl) {}
  90. + searchEngineEditCompleted(
  91. + searchEngine, keyword, queryUrl, suggestionsUrl) {}
  92. /** @return {!Promise<!SearchEnginesInfo>} */
  93. getSearchEnginesList() {}
  94. @@ -99,11 +102,12 @@ cr.define('settings', function() {
  95. }
  96. /** @override */
  97. - searchEngineEditCompleted(searchEngine, keyword, queryUrl) {
  98. + searchEngineEditCompleted(searchEngine, keyword, queryUrl, suggestionsUrl) {
  99. chrome.send('searchEngineEditCompleted', [
  100. searchEngine,
  101. keyword,
  102. queryUrl,
  103. + suggestionsUrl
  104. ]);
  105. }
  106. --- a/chrome/browser/ui/search_engines/edit_search_engine_controller.cc
  107. +++ b/chrome/browser/ui/search_engines/edit_search_engine_controller.cc
  108. @@ -66,6 +66,15 @@ bool EditSearchEngineController::IsURLVa
  109. service->search_terms_data())).is_valid();
  110. }
  111. +bool EditSearchEngineController::IsSuggestionsURLValid(
  112. + const std::string& suggestions_url_input) const {
  113. + std::string suggestions_url = GetFixedUpURL(suggestions_url_input);
  114. + if (suggestions_url.empty())
  115. + return true;
  116. +
  117. + return IsURLValid(suggestions_url);
  118. +}
  119. +
  120. bool EditSearchEngineController::IsKeywordValid(
  121. const base::string16& keyword_input) const {
  122. base::string16 keyword_input_trimmed(
  123. @@ -88,10 +97,12 @@ bool EditSearchEngineController::IsKeywo
  124. void EditSearchEngineController::AcceptAddOrEdit(
  125. const base::string16& title_input,
  126. const base::string16& keyword_input,
  127. - const std::string& url_input) {
  128. + const std::string& url_input,
  129. + const std::string& suggestions_url_input) {
  130. DCHECK(!keyword_input.empty());
  131. std::string url_string = GetFixedUpURL(url_input);
  132. DCHECK(!url_string.empty());
  133. + std::string suggestions_url = GetFixedUpURL(suggestions_url_input);
  134. TemplateURLService* template_url_service =
  135. TemplateURLServiceFactory::GetForProfile(profile_);
  136. @@ -119,7 +130,8 @@ void EditSearchEngineController::AcceptA
  137. } else {
  138. // Adding or modifying an entry via the Delegate.
  139. edit_keyword_delegate_->OnEditedKeyword(template_url_, title_input,
  140. - keyword_input, url_string);
  141. + keyword_input, url_string,
  142. + suggestions_url);
  143. }
  144. }
  145. --- a/chrome/browser/ui/search_engines/edit_search_engine_controller.h
  146. +++ b/chrome/browser/ui/search_engines/edit_search_engine_controller.h
  147. @@ -24,7 +24,8 @@ class EditSearchEngineControllerDelegate
  148. virtual void OnEditedKeyword(TemplateURL* template_url,
  149. const base::string16& title,
  150. const base::string16& keyword,
  151. - const std::string& url) = 0;
  152. + const std::string& url,
  153. + const std::string& suggestions_url) = 0;
  154. protected:
  155. virtual ~EditSearchEngineControllerDelegate() {}
  156. @@ -50,6 +51,8 @@ class EditSearchEngineController {
  157. // character results in a valid url.
  158. bool IsURLValid(const std::string& url_input) const;
  159. + bool IsSuggestionsURLValid(const std::string& suggestions_url_input) const;
  160. +
  161. // Returns true if the value of |keyword_input| represents a valid keyword.
  162. // The keyword is valid if it is non-empty and does not conflict with an
  163. // existing entry. NOTE: this is just the keyword, not the title and url.
  164. @@ -58,7 +61,8 @@ class EditSearchEngineController {
  165. // Completes the add or edit of a search engine.
  166. void AcceptAddOrEdit(const base::string16& title_input,
  167. const base::string16& keyword_input,
  168. - const std::string& url_input);
  169. + const std::string& url_input,
  170. + const std::string& suggestions_url_input);
  171. // Deletes an unused TemplateURL, if its add was cancelled and it's not
  172. // already owned by the TemplateURLService.
  173. --- a/chrome/browser/ui/search_engines/keyword_editor_controller.cc
  174. +++ b/chrome/browser/ui/search_engines/keyword_editor_controller.cc
  175. @@ -21,23 +21,27 @@ KeywordEditorController::KeywordEditorCo
  176. KeywordEditorController::~KeywordEditorController() {
  177. }
  178. -int KeywordEditorController::AddTemplateURL(const base::string16& title,
  179. - const base::string16& keyword,
  180. - const std::string& url) {
  181. +int KeywordEditorController::AddTemplateURL(
  182. + const base::string16& title,
  183. + const base::string16& keyword,
  184. + const std::string& url,
  185. + const std::string& suggestions_url) {
  186. DCHECK(!url.empty());
  187. base::RecordAction(UserMetricsAction("KeywordEditor_AddKeyword"));
  188. const int new_index = table_model_->last_other_engine_index();
  189. - table_model_->Add(new_index, title, keyword, url);
  190. + table_model_->Add(new_index, title, keyword, url, suggestions_url);
  191. return new_index;
  192. }
  193. -void KeywordEditorController::ModifyTemplateURL(TemplateURL* template_url,
  194. - const base::string16& title,
  195. - const base::string16& keyword,
  196. - const std::string& url) {
  197. +void KeywordEditorController::ModifyTemplateURL(
  198. + TemplateURL* template_url,
  199. + const base::string16& title,
  200. + const base::string16& keyword,
  201. + const std::string& url,
  202. + const std::string& suggestions_url) {
  203. DCHECK(!url.empty());
  204. const int index = table_model_->IndexOfTemplateURL(template_url);
  205. if (index == -1) {
  206. @@ -48,10 +52,12 @@ void KeywordEditorController::ModifyTemp
  207. // Don't do anything if the entry didn't change.
  208. if ((template_url->short_name() == title) &&
  209. - (template_url->keyword() == keyword) && (template_url->url() == url))
  210. + (template_url->keyword() == keyword) &&
  211. + (template_url->url() == url) &&
  212. + (template_url->suggestions_url() == suggestions_url))
  213. return;
  214. - table_model_->ModifyTemplateURL(index, title, keyword, url);
  215. + table_model_->ModifyTemplateURL(index, title, keyword, url, suggestions_url);
  216. base::RecordAction(UserMetricsAction("KeywordEditor_ModifiedKeyword"));
  217. }
  218. --- a/chrome/browser/ui/search_engines/keyword_editor_controller.h
  219. +++ b/chrome/browser/ui/search_engines/keyword_editor_controller.h
  220. @@ -26,14 +26,16 @@ class KeywordEditorController {
  221. // model. Returns the index of the added URL.
  222. int AddTemplateURL(const base::string16& title,
  223. const base::string16& keyword,
  224. - const std::string& url);
  225. + const std::string& url,
  226. + const std::string& suggestions_url);
  227. // Invoked when the user modifies a TemplateURL. Updates the
  228. // TemplateURLService and table model appropriately.
  229. void ModifyTemplateURL(TemplateURL* template_url,
  230. const base::string16& title,
  231. const base::string16& keyword,
  232. - const std::string& url);
  233. + const std::string& url,
  234. + const std::string& suggestions_url);
  235. // Return true if the given |url| can be edited.
  236. bool CanEdit(const TemplateURL* url) const;
  237. --- a/chrome/browser/ui/search_engines/template_url_table_model.cc
  238. +++ b/chrome/browser/ui/search_engines/template_url_table_model.cc
  239. @@ -98,20 +98,24 @@ void TemplateURLTableModel::Remove(int i
  240. void TemplateURLTableModel::Add(int index,
  241. const base::string16& short_name,
  242. const base::string16& keyword,
  243. - const std::string& url) {
  244. + const std::string& url,
  245. + const std::string& suggestions_url) {
  246. DCHECK(index >= 0 && index <= RowCount());
  247. DCHECK(!url.empty());
  248. TemplateURLData data;
  249. data.SetShortName(short_name);
  250. data.SetKeyword(keyword);
  251. data.SetURL(url);
  252. + data.suggestions_url = suggestions_url;
  253. template_url_service_->Add(std::make_unique<TemplateURL>(data));
  254. }
  255. -void TemplateURLTableModel::ModifyTemplateURL(int index,
  256. - const base::string16& title,
  257. - const base::string16& keyword,
  258. - const std::string& url) {
  259. +void TemplateURLTableModel::ModifyTemplateURL(
  260. + int index,
  261. + const base::string16& title,
  262. + const base::string16& keyword,
  263. + const std::string& url,
  264. + const std::string& suggestions_url) {
  265. DCHECK(index >= 0 && index <= RowCount());
  266. DCHECK(!url.empty());
  267. TemplateURL* template_url = GetTemplateURL(index);
  268. @@ -120,7 +124,8 @@ void TemplateURLTableModel::ModifyTempla
  269. DCHECK(template_url_service_->GetDefaultSearchProvider() != template_url ||
  270. template_url->SupportsReplacement(
  271. template_url_service_->search_terms_data()));
  272. - template_url_service_->ResetTemplateURL(template_url, title, keyword, url);
  273. + template_url_service_->ResetTemplateURL(template_url, title, keyword, url,
  274. + suggestions_url);
  275. }
  276. TemplateURL* TemplateURLTableModel::GetTemplateURL(int index) {
  277. --- a/chrome/browser/ui/search_engines/template_url_table_model.h
  278. +++ b/chrome/browser/ui/search_engines/template_url_table_model.h
  279. @@ -52,13 +52,15 @@ class TemplateURLTableModel : public ui:
  280. void Add(int index,
  281. const base::string16& short_name,
  282. const base::string16& keyword,
  283. - const std::string& url);
  284. + const std::string& url,
  285. + const std::string& suggestions_url);
  286. // Update the entry at the specified index.
  287. void ModifyTemplateURL(int index,
  288. const base::string16& title,
  289. const base::string16& keyword,
  290. - const std::string& url);
  291. + const std::string& url,
  292. + const std::string& suggestions_url);
  293. // Reloads the icon at the specified index.
  294. void ReloadIcon(int index);
  295. --- a/chrome/browser/ui/webui/settings/search_engines_handler.cc
  296. +++ b/chrome/browser/ui/webui/settings/search_engines_handler.cc
  297. @@ -37,6 +37,7 @@ namespace {
  298. const char kSearchEngineField[] = "searchEngine";
  299. const char kKeywordField[] = "keyword";
  300. const char kQueryUrlField[] = "queryUrl";
  301. +const char kSuggestionsUrlField[] = "suggestionsUrl";
  302. // Dummy number used for indicating that a new search engine is added.
  303. const int kNewSearchEngineIndex = -1;
  304. @@ -194,6 +195,9 @@ SearchEnginesHandler::CreateDictionaryFo
  305. Profile* profile = Profile::FromWebUI(web_ui());
  306. dict->SetString(
  307. "url", template_url->url_ref().DisplayURL(UIThreadSearchTermsData()));
  308. + dict->SetString("suggestionsUrl",
  309. + template_url->suggestions_url_ref().DisplayURL(
  310. + UIThreadSearchTermsData()));
  311. dict->SetBoolean("urlLocked", template_url->prepopulate_id() > 0);
  312. GURL icon_url = template_url->favicon_url();
  313. if (icon_url.is_valid())
  314. @@ -290,12 +294,14 @@ void SearchEnginesHandler::HandleSearchE
  315. void SearchEnginesHandler::OnEditedKeyword(TemplateURL* template_url,
  316. const base::string16& title,
  317. const base::string16& keyword,
  318. - const std::string& url) {
  319. + const std::string& url,
  320. + const std::string& suggestions_url) {
  321. DCHECK(!url.empty());
  322. if (template_url)
  323. - list_controller_.ModifyTemplateURL(template_url, title, keyword, url);
  324. + list_controller_.ModifyTemplateURL(template_url, title, keyword, url,
  325. + suggestions_url);
  326. else
  327. - list_controller_.AddTemplateURL(title, keyword, url);
  328. + list_controller_.AddTemplateURL(title, keyword, url, suggestions_url);
  329. edit_controller_.reset();
  330. }
  331. @@ -326,6 +332,8 @@ bool SearchEnginesHandler::CheckFieldVal
  332. is_valid = edit_controller_->IsKeywordValid(base::UTF8ToUTF16(field_value));
  333. else if (field_name.compare(kQueryUrlField) == 0)
  334. is_valid = edit_controller_->IsURLValid(field_value);
  335. + else if (field_name.compare(kSuggestionsUrlField) == 0)
  336. + is_valid = edit_controller_->IsSuggestionsURLValid(field_value);
  337. else
  338. NOTREACHED();
  339. @@ -347,17 +355,21 @@ void SearchEnginesHandler::HandleSearchE
  340. std::string search_engine;
  341. std::string keyword;
  342. std::string query_url;
  343. + std::string suggestions_url;
  344. CHECK(args->GetString(0, &search_engine));
  345. CHECK(args->GetString(1, &keyword));
  346. CHECK(args->GetString(2, &query_url));
  347. + CHECK(args->GetString(3, &suggestions_url));
  348. // Recheck validity. It's possible to get here with invalid input if e.g. the
  349. // user calls the right JS functions directly from the web inspector.
  350. if (CheckFieldValidity(kSearchEngineField, search_engine) &&
  351. CheckFieldValidity(kKeywordField, keyword) &&
  352. - CheckFieldValidity(kQueryUrlField, query_url)) {
  353. + CheckFieldValidity(kQueryUrlField, query_url) &&
  354. + CheckFieldValidity(kSuggestionsUrlField, suggestions_url)) {
  355. edit_controller_->AcceptAddOrEdit(base::UTF8ToUTF16(search_engine),
  356. - base::UTF8ToUTF16(keyword), query_url);
  357. + base::UTF8ToUTF16(keyword),
  358. + query_url, suggestions_url);
  359. }
  360. }
  361. --- a/chrome/browser/ui/webui/settings/search_engines_handler.h
  362. +++ b/chrome/browser/ui/webui/settings/search_engines_handler.h
  363. @@ -46,7 +46,8 @@ class SearchEnginesHandler : public Sett
  364. void OnEditedKeyword(TemplateURL* template_url,
  365. const base::string16& title,
  366. const base::string16& keyword,
  367. - const std::string& url) override;
  368. + const std::string& url,
  369. + const std::string& suggestions_url) override;
  370. // SettingsPageUIHandler implementation.
  371. void RegisterMessages() override;
  372. @@ -74,8 +75,8 @@ class SearchEnginesHandler : public Sett
  373. // to WebUI. Called from WebUI.
  374. void HandleValidateSearchEngineInput(const base::ListValue* args);
  375. - // Checks whether the given user input field (searchEngine, keyword, queryUrl)
  376. - // is populated with a valid value.
  377. + // Checks whether the given user input field (searchEngine, keyword, queryUrl,
  378. + // suggestionsUrl) is populated with a valid value.
  379. bool CheckFieldValidity(const std::string& field_name,
  380. const std::string& field_value);
  381. --- a/components/search_engines/template_url_service.cc
  382. +++ b/components/search_engines/template_url_service.cc
  383. @@ -605,7 +605,8 @@ void TemplateURLService::IncrementUsageC
  384. void TemplateURLService::ResetTemplateURL(TemplateURL* url,
  385. const base::string16& title,
  386. const base::string16& keyword,
  387. - const std::string& search_url) {
  388. + const std::string& search_url,
  389. + const std::string& suggestions_url) {
  390. DCHECK(!IsCreatedByExtension(url));
  391. DCHECK(!keyword.empty());
  392. DCHECK(!search_url.empty());
  393. @@ -619,6 +620,7 @@ void TemplateURLService::ResetTemplateUR
  394. }
  395. data.safe_for_autoreplace = false;
  396. data.last_modified = clock_->Now();
  397. + data.suggestions_url = suggestions_url;
  398. Update(url, TemplateURL(data));
  399. }
  400. @@ -1978,7 +1980,7 @@ TemplateURL* TemplateURLService::Add(std
  401. // Neither engine can be replaced. Uniquify the existing keyword.
  402. base::string16 new_keyword = UniquifyKeyword(*existing_turl, false);
  403. ResetTemplateURL(existing_turl, existing_turl->short_name(),
  404. - new_keyword, existing_turl->url());
  405. + new_keyword, existing_turl->url(), "");
  406. DCHECK_EQ(new_keyword, existing_turl->keyword());
  407. }
  408. }
  409. --- a/components/search_engines/template_url_service.h
  410. +++ b/components/search_engines/template_url_service.h
  411. @@ -247,7 +247,8 @@ class TemplateURLService : public WebDat
  412. void ResetTemplateURL(TemplateURL* url,
  413. const base::string16& title,
  414. const base::string16& keyword,
  415. - const std::string& search_url);
  416. + const std::string& search_url,
  417. + const std::string& suggestions_url);
  418. // Creates TemplateURL, populating it with data from Play API. If TemplateURL
  419. // with matching keyword already exists then merges Play API data into it.