util.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_SEARCH_ENGINES_UTIL_H_
  5. #define COMPONENTS_SEARCH_ENGINES_UTIL_H_
  6. // This file contains utility functions for search engine functionality.
  7. #include <memory>
  8. #include <set>
  9. #include <string>
  10. #include <vector>
  11. #include "components/search_engines/template_url_service.h"
  12. class KeywordWebDataService;
  13. class PrefService;
  14. class TemplateURL;
  15. class WDTypedResult;
  16. // Returns the short name of the default search engine, or the empty string if
  17. // none is set.
  18. std::u16string GetDefaultSearchEngineName(TemplateURLService* service);
  19. // Returns a GURL that searches for |terms| using the default search engine of
  20. // |service|.
  21. GURL GetDefaultSearchURLForSearchTerms(TemplateURLService* service,
  22. const std::u16string& terms);
  23. // Returns matching URL from |template_urls| or NULL.
  24. TemplateURL* FindURLByPrepopulateID(
  25. const TemplateURLService::TemplateURLVector& template_urls,
  26. int prepopulate_id);
  27. typedef enum {
  28. kDefault,
  29. kOverwriteUserEdits,
  30. } MergeOptions;
  31. // Modifies `url_to_update` so that it contains user-modified fields from
  32. // `original_turl`. Both URLs must have the same `prepopulate_id` or
  33. // `starter_pack_id`. If `merge_option` is set to kOverWriteUserEdits,
  34. // user-modified fields and `safe_for_autoreplace` are not preserved.
  35. //
  36. // WARNING: Changing merge_option from the default value will result in loss of
  37. // user data. It should be set to kDefault unless in very specific circumstances
  38. // where a reset to defaults is required.
  39. void MergeIntoEngineData(const TemplateURL* original_turl,
  40. TemplateURLData* url_to_update,
  41. MergeOptions merge_option = MergeOptions::kDefault);
  42. // CreateActionsFromCurrentPrepopulateData() and
  43. // CreateActionsFromStarterPackData() (see below) takes in the current built-in
  44. // (prepopulated or starter pack) URLs as well as the user's current URLs, and
  45. // returns an instance of the following struct representing the changes
  46. // necessary to bring the user's URLs in line with the built-in URLs.
  47. //
  48. // There are three types of changes:
  49. // (1) Previous built-in engines that no longer exist in the current set of
  50. // built-in engines and thus should be removed from the user's current
  51. // URLs.
  52. // (2) Previous built-in engines whose data has changed. The existing
  53. // entries for these engines should be updated to reflect the new data,
  54. // except for any user-set names and keywords, which can be preserved.
  55. // (3) New built-in engines not in the user's engine list, which should be
  56. // added.
  57. // The pair of current search engine and its new value.
  58. typedef std::pair<TemplateURL*, TemplateURLData> EditedSearchEngine;
  59. typedef std::vector<EditedSearchEngine> EditedEngines;
  60. struct ActionsFromCurrentData {
  61. ActionsFromCurrentData();
  62. ActionsFromCurrentData(const ActionsFromCurrentData& other);
  63. ~ActionsFromCurrentData();
  64. TemplateURLService::TemplateURLVector removed_engines;
  65. EditedEngines edited_engines;
  66. std::vector<TemplateURLData> added_engines;
  67. };
  68. // MergeEnginesFromPrepopulateData merges search engines from
  69. // |prepopulated_urls| into |template_urls|. Calls
  70. // CreateActionsFromCurrentPrepopulateData() to collect actions and then applies
  71. // them on |template_urls|. MergeEnginesFromPrepopulateData is invoked when the
  72. // version of the prepopulate data changes. If |removed_keyword_guids| is not
  73. // nullptr, the Sync GUID of each item removed from the DB will be added to it.
  74. // Note that this function will take ownership of |prepopulated_urls| and will
  75. // clear the vector.
  76. // The function is exposed in header file to provide access from unittests.
  77. void MergeEnginesFromPrepopulateData(
  78. KeywordWebDataService* service,
  79. std::vector<std::unique_ptr<TemplateURLData>>* prepopulated_urls,
  80. TemplateURLService::OwnedTemplateURLVector* template_urls,
  81. TemplateURL* default_search_provider,
  82. std::set<std::string>* removed_keyword_guids);
  83. // Given the user's current URLs and the current set of prepopulated URLs,
  84. // produces the set of actions (see above) required to make the user's URLs
  85. // reflect the prepopulate data. |default_search_provider| is used to avoid
  86. // placing the current default provider on the "to be removed" list.
  87. //
  88. // NOTE: Takes ownership of, and clears, |prepopulated_urls|.
  89. ActionsFromCurrentData CreateActionsFromCurrentPrepopulateData(
  90. std::vector<std::unique_ptr<TemplateURLData>>* prepopulated_urls,
  91. const TemplateURLService::OwnedTemplateURLVector& existing_urls,
  92. const TemplateURL* default_search_provider);
  93. // MergeEnginesFromStarterPackData merges search engines from the built-in
  94. // TemplateURLStarterPackData class into `template_urls`. Calls
  95. // CreateActionsFromCurrentStarterPackData() to collect actions and then applies
  96. // them on `template_urls`. MergeEgninesFromStarterPackData is invoked when the
  97. // version of the starter pack data changes. If `removed_keyword_guids` is not
  98. // nullptr, the Sync GUID of each item removed from the DB will be added to it.
  99. // `merge_option` specifies whether user-modified fields are preserved when
  100. // merging. It should be set to default except for very specific use cases
  101. // where a reset to defaults is required.
  102. void MergeEnginesFromStarterPackData(
  103. KeywordWebDataService* service,
  104. TemplateURLService::OwnedTemplateURLVector* template_urls,
  105. TemplateURL* default_search_provider,
  106. std::set<std::string>* removed_keyword_guids,
  107. MergeOptions merge_option = MergeOptions::kDefault);
  108. // Given the user's current URLs and the current set of Starter Pack URLs,
  109. // produces the set of actions (see above) required to make the user's URLs
  110. // reflect the starter pack data.
  111. // `merge_option` specifies whether user-modified fields are preserved when
  112. // merging. It should be set to default except for very specific use cases
  113. // where a reset to defaults is required.
  114. //
  115. // NOTE: Takes ownership of, and clears, |starter_pack_urls|.
  116. ActionsFromCurrentData CreateActionsFromCurrentStarterPackData(
  117. std::vector<std::unique_ptr<TemplateURLData>>* starter_pack_urls,
  118. const TemplateURLService::OwnedTemplateURLVector& existing_urls,
  119. MergeOptions merge_option = MergeOptions::kDefault);
  120. // Takes in an ActionsFromCurrentData (see above) and applies the actions (add,
  121. // edit, or remove) to the user's current URLs. This is called by
  122. // MergeEnginesFromPrepopulateData() and MergeEnginesFromStarterPackData().
  123. void ApplyActionsFromCurrentData(
  124. ActionsFromCurrentData actions,
  125. KeywordWebDataService* service,
  126. TemplateURLService::OwnedTemplateURLVector* template_urls,
  127. TemplateURL* default_search_provider,
  128. std::set<std::string>* removed_keyword_guids);
  129. // Processes the results of KeywordWebDataService::GetKeywords, combining it
  130. // with prepopulated search providers to result in:
  131. // * a set of template_urls (search providers). The caller owns the
  132. // TemplateURL* returned in template_urls.
  133. // * whether there is a new resource keyword version (and the value).
  134. // |*new_resource_keyword_version| is set to 0 if no new value. Otherwise,
  135. // it is the new value.
  136. // Only pass in a non-NULL value for service if the KeywordWebDataService should
  137. // be updated. If |removed_keyword_guids| is not NULL, any TemplateURLs removed
  138. // from the keyword table in the KeywordWebDataService will have their Sync
  139. // GUIDs added to it. |default_search_provider| will be used to prevent removing
  140. // the current user-selected DSE, regardless of changes in prepopulate data.
  141. void GetSearchProvidersUsingKeywordResult(
  142. const WDTypedResult& result,
  143. KeywordWebDataService* service,
  144. PrefService* prefs,
  145. TemplateURLService::OwnedTemplateURLVector* template_urls,
  146. TemplateURL* default_search_provider,
  147. const SearchTermsData& search_terms_data,
  148. int* new_resource_keyword_version,
  149. int* new_resource_starter_pack_version,
  150. std::set<std::string>* removed_keyword_guids);
  151. // Like GetSearchProvidersUsingKeywordResult(), but allows the caller to pass in
  152. // engines in |template_urls| instead of getting them via processing a web data
  153. // service request.
  154. // |resource_keyword_version| should contain the version number of the current
  155. // keyword data, i.e. the version number of the most recent prepopulate data
  156. // that has been merged into the current keyword data. On exit, this will be
  157. // set as in GetSearchProvidersUsingKeywordResult().
  158. void GetSearchProvidersUsingLoadedEngines(
  159. KeywordWebDataService* service,
  160. PrefService* prefs,
  161. TemplateURLService::OwnedTemplateURLVector* template_urls,
  162. TemplateURL* default_search_provider,
  163. const SearchTermsData& search_terms_data,
  164. int* resource_keyword_version,
  165. int* resource_starter_pack_version,
  166. std::set<std::string>* removed_keyword_guids);
  167. // Due to a bug, the |input_encodings| field of TemplateURLData could have
  168. // contained duplicate entries. This removes those entries and returns whether
  169. // any were found.
  170. bool DeDupeEncodings(std::vector<std::string>* encodings);
  171. // Removes (and deletes) TemplateURLs from |template_urls| and |service| if they
  172. // have duplicate prepopulate ids. If |removed_keyword_guids| is not NULL, the
  173. // Sync GUID of each item removed from the DB will be added to it. This is a
  174. // helper used by GetSearchProvidersUsingKeywordResult(), but is declared here
  175. // so it's accessible by unittests.
  176. // The order of template_urls is preserved (except for duplicates) because it
  177. // affects order of presentation in settings web-ui.
  178. // See https://crbug.com/924268 for details.
  179. void RemoveDuplicatePrepopulateIDs(
  180. KeywordWebDataService* service,
  181. const std::vector<std::unique_ptr<TemplateURLData>>& prepopulated_urls,
  182. TemplateURL* default_search_provider,
  183. TemplateURLService::OwnedTemplateURLVector* template_urls,
  184. const SearchTermsData& search_terms_data,
  185. std::set<std::string>* removed_keyword_guids);
  186. TemplateURLService::OwnedTemplateURLVector::iterator FindTemplateURL(
  187. TemplateURLService::OwnedTemplateURLVector* urls,
  188. const TemplateURL* url);
  189. #endif // COMPONENTS_SEARCH_ENGINES_UTIL_H_