Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors |
thakis@chromium.org | 96788b0 | 2010-06-26 21:45:34 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
hashimoto@chromium.org | bf5c532d | 2014-07-05 00:29:53 | [diff] [blame] | 5 | #include "components/search_engines/util.h" |
thakis@chromium.org | 96788b0 | 2010-06-26 21:45:34 | [diff] [blame] | 6 | |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
Orin Jaworski | 28677486 | 2019-01-28 18:43:21 | [diff] [blame] | 10 | #include <limits> |
joi@chromium.org | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 11 | #include <map> |
levin@chromium.org | e1ddda0 | 2010-08-26 19:43:48 | [diff] [blame] | 12 | #include <set> |
stevet@chromium.org | 2eff6b1 | 2012-05-16 20:07:05 | [diff] [blame] | 13 | #include <string> |
Orin Jaworski | 28677486 | 2019-01-28 18:43:21 | [diff] [blame] | 14 | #include <unordered_map> |
levin@chromium.org | e1ddda0 | 2010-08-26 19:43:48 | [diff] [blame] | 15 | #include <vector> |
| 16 | |
Nicolas Dossou-Gbete | af259e7 | 2024-01-19 20:53:55 | [diff] [blame] | 17 | #include "base/check_is_test.h" |
Hans Wennborg | df87046c | 2020-04-28 11:06:24 | [diff] [blame] | 18 | #include "base/check_op.h" |
Jack Yammine | abb0ba2 | 2023-07-20 10:46:34 | [diff] [blame] | 19 | #include "base/feature_list.h" |
Peter Kasting | 03752ef | 2022-09-02 18:58:35 | [diff] [blame] | 20 | #include "base/ranges/algorithm.h" |
avi@chromium.org | cc86ccfe | 2013-06-28 00:10:50 | [diff] [blame] | 21 | #include "base/time/time.h" |
Nicolas Dossou-Gbete | 354c10a5 | 2024-01-22 11:18:37 | [diff] [blame] | 22 | #include "base/version_info/version_info.h" |
Nicolas Dossou-Gbete | af259e7 | 2024-01-19 20:53:55 | [diff] [blame] | 23 | #include "components/country_codes/country_codes.h" |
brettw | f00b9b4 | 2016-02-01 22:11:38 | [diff] [blame] | 24 | #include "components/prefs/pref_service.h" |
Nicolas Dossou-Gbete | af259e7 | 2024-01-19 20:53:55 | [diff] [blame] | 25 | #include "components/search_engines/keyword_web_data_service.h" |
Boris Sazonov | 2b77815a | 2024-01-17 15:35:04 | [diff] [blame] | 26 | #include "components/search_engines/search_engine_choice/search_engine_choice_service.h" |
Jens Mueller | 087e5c4 | 2023-10-05 11:36:05 | [diff] [blame] | 27 | #include "components/search_engines/search_engine_choice_utils.h" |
Jack Yammine | abb0ba2 | 2023-07-20 10:46:34 | [diff] [blame] | 28 | #include "components/search_engines/search_engines_pref_names.h" |
hashimoto@chromium.org | d550cb0 | 2014-06-25 06:48:11 | [diff] [blame] | 29 | #include "components/search_engines/template_url.h" |
hashimoto@chromium.org | 0915b35 | 2014-06-25 19:58:14 | [diff] [blame] | 30 | #include "components/search_engines/template_url_prepopulate_data.h" |
hashimoto@chromium.org | bf5c532d | 2014-07-05 00:29:53 | [diff] [blame] | 31 | #include "components/search_engines/template_url_service.h" |
Angela Yoeurng | d8e5493 | 2022-04-27 23:53:16 | [diff] [blame] | 32 | #include "components/search_engines/template_url_starter_pack_data.h" |
thakis@chromium.org | 96788b0 | 2010-06-26 21:45:34 | [diff] [blame] | 33 | |
Nicolas Dossou-Gbete | af259e7 | 2024-01-19 20:53:55 | [diff] [blame] | 34 | namespace { |
| 35 | |
| 36 | // Indicates whether updates to the search engines database are needed. |
| 37 | struct MergeEngineRequirements { |
| 38 | // `metadata.HasBuiltinKeywordUpdate()` and |
| 39 | // `metadata.HasStarterPackUpdate()` indicate the status for the |
| 40 | // two types of search engines, and when they are `true`, individual fields |
| 41 | // will contain the associated metadata that should be also added to the |
| 42 | // database. |
| 43 | WDKeywordsResult::Metadata metadata; |
| 44 | |
| 45 | // The status to which `prefs::kDefaultSearchProviderKeywordsUseExtendedList` |
| 46 | // should be set. |
| 47 | enum class ShouldKeywordsUseExtendedList { kUnknown, kYes, kNo }; |
| 48 | ShouldKeywordsUseExtendedList should_keywords_use_extended_list = |
| 49 | ShouldKeywordsUseExtendedList::kUnknown; |
| 50 | }; |
| 51 | |
| 52 | MergeEngineRequirements ComputeMergeEnginesRequirements( |
| 53 | PrefService* prefs, |
| 54 | search_engines::SearchEngineChoiceService* search_engine_choice_service, |
| 55 | const WDKeywordsResult::Metadata& keywords_metadata) { |
| 56 | if (!prefs) { |
| 57 | CHECK_IS_TEST(); |
| 58 | return {}; |
| 59 | } |
| 60 | if (!search_engine_choice_service) { |
| 61 | CHECK_IS_TEST(); |
| 62 | return {}; |
| 63 | } |
| 64 | |
| 65 | const int prepopulate_resource_keyword_version = |
| 66 | TemplateURLPrepopulateData::GetDataVersion(prefs); |
| 67 | const int country_id = search_engine_choice_service->GetCountryId(); |
| 68 | const bool should_keywords_use_extended_list = |
| 69 | search_engines::IsChoiceScreenFlagEnabled( |
| 70 | search_engines::ChoicePromo::kAny) && |
| 71 | search_engines::IsEeaChoiceCountry(country_id); |
Nicolas Dossou-Gbete | 354c10a5 | 2024-01-22 11:18:37 | [diff] [blame] | 72 | const int milestone = version_info::GetMajorVersionNumberAsInt(); |
Nicolas Dossou-Gbete | af259e7 | 2024-01-19 20:53:55 | [diff] [blame] | 73 | |
| 74 | bool update_builtin_keywords; |
Nicolas Dossou-Gbete | 354c10a5 | 2024-01-22 11:18:37 | [diff] [blame] | 75 | if (keywords_metadata.builtin_keyword_data_version > |
Nicolas Dossou-Gbete | af259e7 | 2024-01-19 20:53:55 | [diff] [blame] | 76 | prepopulate_resource_keyword_version) { |
| 77 | // The version in the database is more recent than the version in the Chrome |
| 78 | // binary. Downgrades are not supported, so don't update it. |
| 79 | update_builtin_keywords = false; |
Nicolas Dossou-Gbete | 354c10a5 | 2024-01-22 11:18:37 | [diff] [blame] | 80 | } else if (keywords_metadata.builtin_keyword_data_version < |
Nicolas Dossou-Gbete | af259e7 | 2024-01-19 20:53:55 | [diff] [blame] | 81 | prepopulate_resource_keyword_version) { |
| 82 | // The built-in data from `prepopulated_engines.json` has been updated. |
| 83 | update_builtin_keywords = true; |
Nicolas Dossou-Gbete | 354c10a5 | 2024-01-22 11:18:37 | [diff] [blame] | 84 | } else if (keywords_metadata.builtin_keyword_country != 0 && |
| 85 | keywords_metadata.builtin_keyword_country != country_id) { |
| 86 | // The country associated with the profile has changed. |
| 87 | // We skip cases where the country was not previously set to avoid |
| 88 | // unnecessary churn. We expect that by the time this might matter, the |
| 89 | // client will have this data populated when the search engine choice |
| 90 | // feature gets enabled. |
| 91 | update_builtin_keywords = true; |
Nicolas Dossou-Gbete | af259e7 | 2024-01-19 20:53:55 | [diff] [blame] | 92 | } else if (prefs->GetBoolean( |
| 93 | prefs::kDefaultSearchProviderKeywordsUseExtendedList) != |
| 94 | should_keywords_use_extended_list) { |
| 95 | // The state of the search engine choice feature has changed. |
| 96 | // We started writing the pref while we were not checking the country |
| 97 | // before. Once the feature flag is removed, we can clean up this pref. |
| 98 | update_builtin_keywords = true; |
Nicolas Dossou-Gbete | 354c10a5 | 2024-01-22 11:18:37 | [diff] [blame] | 99 | } else if (should_keywords_use_extended_list && |
| 100 | keywords_metadata.builtin_keyword_milestone != 0 && |
| 101 | keywords_metadata.builtin_keyword_milestone < milestone) { |
| 102 | // The milestone changed and we need to recompute the list of visible search |
| 103 | // engines. This is needed only in the EEA. |
| 104 | // We skip cases where the milestone was not previously set to avoid |
| 105 | // unnecessary churn. We expect that by the time this might matter, the |
| 106 | // client will have this data populated when the search engine choice |
| 107 | // feature gets enabled. |
| 108 | update_builtin_keywords = true; |
Nicolas Dossou-Gbete | af259e7 | 2024-01-19 20:53:55 | [diff] [blame] | 109 | } else { |
| 110 | update_builtin_keywords = false; |
| 111 | } |
| 112 | |
| 113 | MergeEngineRequirements merge_requirements; |
| 114 | |
| 115 | if (update_builtin_keywords) { |
Nicolas Dossou-Gbete | 354c10a5 | 2024-01-22 11:18:37 | [diff] [blame] | 116 | merge_requirements.metadata.builtin_keyword_data_version = |
Nicolas Dossou-Gbete | af259e7 | 2024-01-19 20:53:55 | [diff] [blame] | 117 | prepopulate_resource_keyword_version; |
Nicolas Dossou-Gbete | 354c10a5 | 2024-01-22 11:18:37 | [diff] [blame] | 118 | merge_requirements.metadata.builtin_keyword_milestone = milestone; |
| 119 | merge_requirements.metadata.builtin_keyword_country = country_id; |
Nicolas Dossou-Gbete | af259e7 | 2024-01-19 20:53:55 | [diff] [blame] | 120 | merge_requirements.should_keywords_use_extended_list = |
| 121 | should_keywords_use_extended_list |
| 122 | ? MergeEngineRequirements::ShouldKeywordsUseExtendedList::kYes |
| 123 | : MergeEngineRequirements::ShouldKeywordsUseExtendedList::kNo; |
| 124 | } |
| 125 | |
| 126 | const int starter_pack_data_version = |
| 127 | TemplateURLStarterPackData::GetDataVersion(); |
| 128 | if (keywords_metadata.starter_pack_version < starter_pack_data_version) { |
| 129 | merge_requirements.metadata.starter_pack_version = |
| 130 | starter_pack_data_version; |
| 131 | } |
| 132 | |
| 133 | return merge_requirements; |
| 134 | } |
| 135 | |
| 136 | } // namespace |
| 137 | |
Jan Wilken Dörrie | fa241ba | 2021-03-11 17:57:01 | [diff] [blame] | 138 | std::u16string GetDefaultSearchEngineName(TemplateURLService* service) { |
hashimoto@chromium.org | 1f8841b | 2014-06-27 02:31:40 | [diff] [blame] | 139 | DCHECK(service); |
thakis@chromium.org | 96788b0 | 2010-06-26 21:45:34 | [diff] [blame] | 140 | const TemplateURL* const default_provider = |
hashimoto@chromium.org | 1f8841b | 2014-06-27 02:31:40 | [diff] [blame] | 141 | service->GetDefaultSearchProvider(); |
thakis@chromium.org | 96788b0 | 2010-06-26 21:45:34 | [diff] [blame] | 142 | if (!default_provider) { |
| 143 | // TODO(cpu): bug 1187517. It is possible to have no default provider. |
| 144 | // returning an empty string is a stopgap measure for the crash |
| 145 | // http://code.google.com/p/chromium/issues/detail?id=2573 |
Jan Wilken Dörrie | fa241ba | 2021-03-11 17:57:01 | [diff] [blame] | 146 | return std::u16string(); |
thakis@chromium.org | 96788b0 | 2010-06-26 21:45:34 | [diff] [blame] | 147 | } |
avi@chromium.org | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 148 | return default_provider->short_name(); |
thakis@chromium.org | 96788b0 | 2010-06-26 21:45:34 | [diff] [blame] | 149 | } |
levin@chromium.org | e1ddda0 | 2010-08-26 19:43:48 | [diff] [blame] | 150 | |
hashimoto@chromium.org | 1f8841b | 2014-06-27 02:31:40 | [diff] [blame] | 151 | GURL GetDefaultSearchURLForSearchTerms(TemplateURLService* service, |
Jan Wilken Dörrie | fa241ba | 2021-03-11 17:57:01 | [diff] [blame] | 152 | const std::u16string& terms) { |
hashimoto@chromium.org | 1f8841b | 2014-06-27 02:31:40 | [diff] [blame] | 153 | DCHECK(service); |
hashimoto@chromium.org | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 154 | const TemplateURL* default_provider = service->GetDefaultSearchProvider(); |
pkasting@chromium.org | bba9e63 | 2013-06-28 22:52:19 | [diff] [blame] | 155 | if (!default_provider) |
| 156 | return GURL(); |
| 157 | const TemplateURLRef& search_url = default_provider->url_ref(); |
hashimoto@chromium.org | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 158 | DCHECK(search_url.SupportsReplacement(service->search_terms_data())); |
pkasting@chromium.org | bba9e63 | 2013-06-28 22:52:19 | [diff] [blame] | 159 | TemplateURLRef::SearchTermsArgs search_terms_args(terms); |
Mark Pearson | 247fb8a | 2018-08-30 05:12:26 | [diff] [blame] | 160 | search_terms_args.append_extra_query_params_from_command_line = true; |
hashimoto@chromium.org | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 161 | return GURL(search_url.ReplaceSearchTerms(search_terms_args, |
| 162 | service->search_terms_data())); |
pkasting@chromium.org | bba9e63 | 2013-06-28 22:52:19 | [diff] [blame] | 163 | } |
| 164 | |
stevet@chromium.org | 01ef4a66 | 2012-05-29 15:58:25 | [diff] [blame] | 165 | void RemoveDuplicatePrepopulateIDs( |
hashimoto@chromium.org | 37b32460 | 2014-07-02 07:30:49 | [diff] [blame] | 166 | KeywordWebDataService* service, |
avi | e8828f2 | 2016-09-02 18:07:58 | [diff] [blame] | 167 | const std::vector<std::unique_ptr<TemplateURLData>>& prepopulated_urls, |
stevet@chromium.org | 01ef4a66 | 2012-05-29 15:58:25 | [diff] [blame] | 168 | TemplateURL* default_search_provider, |
avi | 8a64b715b | 2016-09-02 17:30:04 | [diff] [blame] | 169 | TemplateURLService::OwnedTemplateURLVector* template_urls, |
hashimoto@chromium.org | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 170 | const SearchTermsData& search_terms_data, |
stevet@chromium.org | 2eff6b1 | 2012-05-16 20:07:05 | [diff] [blame] | 171 | std::set<std::string>* removed_keyword_guids) { |
stevet@chromium.org | 01ef4a66 | 2012-05-29 15:58:25 | [diff] [blame] | 172 | DCHECK(template_urls); |
Orin Jaworski | ceb5964 | 2019-01-25 21:29:53 | [diff] [blame] | 173 | TemplateURLService::OwnedTemplateURLVector checked_urls; |
levin@chromium.org | e1ddda0 | 2010-08-26 19:43:48 | [diff] [blame] | 174 | |
stevet@chromium.org | 01ef4a66 | 2012-05-29 15:58:25 | [diff] [blame] | 175 | // For convenience construct an ID->TemplateURL* map from |prepopulated_urls|. |
avi | 8a64b715b | 2016-09-02 17:30:04 | [diff] [blame] | 176 | std::map<int, TemplateURLData*> prepopulated_url_map; |
avi | e8828f2 | 2016-09-02 18:07:58 | [diff] [blame] | 177 | for (const auto& url : prepopulated_urls) |
| 178 | prepopulated_url_map[url->prepopulate_id] = url.get(); |
stevet@chromium.org | 01ef4a66 | 2012-05-29 15:58:25 | [diff] [blame] | 179 | |
Orin Jaworski | ceb5964 | 2019-01-25 21:29:53 | [diff] [blame] | 180 | constexpr size_t invalid_index = std::numeric_limits<size_t>::max(); |
| 181 | // A helper structure for deduplicating elements with the same prepopulate_id. |
| 182 | struct DuplicationData { |
| 183 | DuplicationData() : index_representative(invalid_index) {} |
| 184 | |
| 185 | // The index into checked_urls at which the best representative is stored. |
| 186 | size_t index_representative; |
| 187 | |
| 188 | // Proper duplicates for consideration during selection phase. This |
| 189 | // does not include the representative stored in checked_urls. |
| 190 | TemplateURLService::OwnedTemplateURLVector duplicates; |
| 191 | }; |
| 192 | // Map from prepopulate_id to data for deduplication and selection. |
| 193 | std::unordered_map<int, DuplicationData> duplication_map; |
| 194 | |
| 195 | const auto has_default_search_keyword = [&](const auto& turl) { |
| 196 | return default_search_provider && |
| 197 | (default_search_provider->prepopulate_id() == |
| 198 | turl->prepopulate_id()) && |
| 199 | default_search_provider->HasSameKeywordAs(turl->data(), |
| 200 | search_terms_data); |
| 201 | }; |
| 202 | |
| 203 | // Deduplication phase: move elements into new vector, preserving order while |
| 204 | // gathering duplicates into separate container for selection. |
avi | 8a64b715b | 2016-09-02 17:30:04 | [diff] [blame] | 205 | for (auto& turl : *template_urls) { |
Orin Jaworski | ceb5964 | 2019-01-25 21:29:53 | [diff] [blame] | 206 | const int prepopulate_id = turl->prepopulate_id(); |
| 207 | if (prepopulate_id) { |
| 208 | auto& duplication_data = duplication_map[prepopulate_id]; |
| 209 | if (duplication_data.index_representative == invalid_index) { |
| 210 | // This is the first found. |
| 211 | duplication_data.index_representative = checked_urls.size(); |
| 212 | checked_urls.push_back(std::move(turl)); |
| 213 | } else { |
| 214 | // This is a duplicate. |
| 215 | duplication_data.duplicates.push_back(std::move(turl)); |
| 216 | } |
| 217 | } else { |
avi | 8a64b715b | 2016-09-02 17:30:04 | [diff] [blame] | 218 | checked_urls.push_back(std::move(turl)); |
Orin Jaworski | ceb5964 | 2019-01-25 21:29:53 | [diff] [blame] | 219 | } |
levin@chromium.org | e1ddda0 | 2010-08-26 19:43:48 | [diff] [blame] | 220 | } |
stevet@chromium.org | 01ef4a66 | 2012-05-29 15:58:25 | [diff] [blame] | 221 | |
Orin Jaworski | ceb5964 | 2019-01-25 21:29:53 | [diff] [blame] | 222 | // Selection and cleanup phase: swap out elements if necessary to ensure new |
| 223 | // vector contains only the best representative for each prepopulate_id. |
| 224 | // Then delete the remaining duplicates. |
| 225 | for (auto& id_data : duplication_map) { |
| 226 | const auto prepopulated_url = prepopulated_url_map.find(id_data.first); |
| 227 | const auto has_prepopulated_keyword = [&](const auto& turl) { |
| 228 | return (prepopulated_url != prepopulated_url_map.end()) && |
| 229 | turl->HasSameKeywordAs(*prepopulated_url->second, |
| 230 | search_terms_data); |
| 231 | }; |
stevet@chromium.org | 01ef4a66 | 2012-05-29 15:58:25 | [diff] [blame] | 232 | |
Orin Jaworski | ceb5964 | 2019-01-25 21:29:53 | [diff] [blame] | 233 | // If the user-selected DSE is a prepopulated engine its properties will |
| 234 | // either come from the prepopulation origin or from the user preferences |
| 235 | // file (see DefaultSearchManager). Those properties will end up |
| 236 | // overwriting whatever we load now anyway. If we are eliminating |
| 237 | // duplicates, then, we err on the side of keeping the thing that looks |
| 238 | // more like the value we will end up with in the end. |
| 239 | // Otherwise, a URL is best if it matches the prepopulated data's keyword; |
| 240 | // if none match, just fall back to using the one with the lowest ID. |
| 241 | auto& best = checked_urls[id_data.second.index_representative]; |
| 242 | if (!has_default_search_keyword(best)) { |
| 243 | bool matched_keyword = has_prepopulated_keyword(best); |
| 244 | for (auto& duplicate : id_data.second.duplicates) { |
| 245 | if (has_default_search_keyword(duplicate)) { |
| 246 | best.swap(duplicate); |
| 247 | break; |
| 248 | } else if (matched_keyword) { |
| 249 | continue; |
| 250 | } else if (has_prepopulated_keyword(duplicate)) { |
| 251 | best.swap(duplicate); |
| 252 | matched_keyword = true; |
| 253 | } else if (duplicate->id() < best->id()) { |
| 254 | best.swap(duplicate); |
| 255 | } |
stevet@chromium.org | 01ef4a66 | 2012-05-29 15:58:25 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
Orin Jaworski | ceb5964 | 2019-01-25 21:29:53 | [diff] [blame] | 259 | // Clean up what's left. |
| 260 | for (const auto& duplicate : id_data.second.duplicates) { |
stevet@chromium.org | 01ef4a66 | 2012-05-29 15:58:25 | [diff] [blame] | 261 | if (service) { |
Orin Jaworski | ceb5964 | 2019-01-25 21:29:53 | [diff] [blame] | 262 | service->RemoveKeyword(duplicate->id()); |
stevet@chromium.org | 01ef4a66 | 2012-05-29 15:58:25 | [diff] [blame] | 263 | if (removed_keyword_guids) |
Orin Jaworski | ceb5964 | 2019-01-25 21:29:53 | [diff] [blame] | 264 | removed_keyword_guids->insert(duplicate->sync_guid()); |
stevet@chromium.org | 01ef4a66 | 2012-05-29 15:58:25 | [diff] [blame] | 265 | } |
stevet@chromium.org | 01ef4a66 | 2012-05-29 15:58:25 | [diff] [blame] | 266 | } |
stevet@chromium.org | 01ef4a66 | 2012-05-29 15:58:25 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | // Return the checked URLs. |
| 270 | template_urls->swap(checked_urls); |
levin@chromium.org | e1ddda0 | 2010-08-26 19:43:48 | [diff] [blame] | 271 | } |
| 272 | |
avayvod@chromium.org | 75a4eca | 2011-10-26 20:40:09 | [diff] [blame] | 273 | // Returns the TemplateURL with id specified from the list of TemplateURLs. |
| 274 | // If not found, returns NULL. |
| 275 | TemplateURL* GetTemplateURLByID( |
stevet@chromium.org | 01ef4a66 | 2012-05-29 15:58:25 | [diff] [blame] | 276 | const TemplateURLService::TemplateURLVector& template_urls, |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 277 | int64_t id) { |
jdoerrie | 3feb185 | 2018-10-05 12:16:44 | [diff] [blame] | 278 | for (auto i(template_urls.begin()); i != template_urls.end(); ++i) { |
avayvod@chromium.org | 75a4eca | 2011-10-26 20:40:09 | [diff] [blame] | 279 | if ((*i)->id() == id) { |
| 280 | return *i; |
| 281 | } |
| 282 | } |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 283 | return nullptr; |
avayvod@chromium.org | 75a4eca | 2011-10-26 20:40:09 | [diff] [blame] | 284 | } |
| 285 | |
vasilii@chromium.org | e937a970 | 2013-10-03 22:59:43 | [diff] [blame] | 286 | TemplateURL* FindURLByPrepopulateID( |
| 287 | const TemplateURLService::TemplateURLVector& template_urls, |
| 288 | int prepopulate_id) { |
jdoerrie | 3feb185 | 2018-10-05 12:16:44 | [diff] [blame] | 289 | for (auto i = template_urls.begin(); i < template_urls.end(); ++i) { |
vasilii@chromium.org | e937a970 | 2013-10-03 22:59:43 | [diff] [blame] | 290 | if ((*i)->prepopulate_id() == prepopulate_id) |
| 291 | return *i; |
| 292 | } |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 293 | return nullptr; |
vasilii@chromium.org | e937a970 | 2013-10-03 22:59:43 | [diff] [blame] | 294 | } |
| 295 | |
Angela Yoeurng | 6e3d6b7 | 2022-04-18 21:26:17 | [diff] [blame] | 296 | void MergeIntoEngineData(const TemplateURL* original_turl, |
Angela Yoeurng | d164d89d | 2022-07-27 01:25:43 | [diff] [blame] | 297 | TemplateURLData* url_to_update, |
Tommy C. Li | 7a3609a | 2022-12-02 23:11:35 | [diff] [blame] | 298 | TemplateURLMergeOption merge_option) { |
Pavel Yatsuk | a3e54982 | 2019-09-20 21:55:41 | [diff] [blame] | 299 | DCHECK(original_turl->prepopulate_id() == 0 || |
Angela Yoeurng | 6e3d6b7 | 2022-04-18 21:26:17 | [diff] [blame] | 300 | original_turl->prepopulate_id() == url_to_update->prepopulate_id); |
Angela Yoeurng | d8e5493 | 2022-04-27 23:53:16 | [diff] [blame] | 301 | DCHECK(original_turl->starter_pack_id() == 0 || |
| 302 | original_turl->starter_pack_id() == url_to_update->starter_pack_id); |
Pavel Yatsuk | bec91d2 | 2019-10-07 19:14:55 | [diff] [blame] | 303 | // When the user modified search engine's properties or search engine is |
| 304 | // imported from Play API data we need to preserve certain search engine |
| 305 | // properties from overriding with prepopulated data. |
Angela Yoeurng | d164d89d | 2022-07-27 01:25:43 | [diff] [blame] | 306 | bool preserve_user_edits = |
Tommy C. Li | 7a3609a | 2022-12-02 23:11:35 | [diff] [blame] | 307 | (merge_option != TemplateURLMergeOption::kOverwriteUserEdits && |
Angela Yoeurng | d164d89d | 2022-07-27 01:25:43 | [diff] [blame] | 308 | (!original_turl->safe_for_autoreplace() || |
| 309 | original_turl->created_from_play_api())); |
| 310 | if (preserve_user_edits) { |
Angela Yoeurng | 6e3d6b7 | 2022-04-18 21:26:17 | [diff] [blame] | 311 | url_to_update->safe_for_autoreplace = original_turl->safe_for_autoreplace(); |
| 312 | url_to_update->SetShortName(original_turl->short_name()); |
| 313 | url_to_update->SetKeyword(original_turl->keyword()); |
Pavel Yatsuk | bec91d2 | 2019-10-07 19:14:55 | [diff] [blame] | 314 | if (original_turl->created_from_play_api()) { |
| 315 | // TODO(crbug/1002271): Search url from Play API might contain attribution |
| 316 | // info and therefore should be preserved through prepopulated data |
| 317 | // update. In the future we might decide to take different approach to |
| 318 | // pass attribution info to search providers. |
Angela Yoeurng | 6e3d6b7 | 2022-04-18 21:26:17 | [diff] [blame] | 319 | url_to_update->SetURL(original_turl->url()); |
Pavel Yatsuk | bec91d2 | 2019-10-07 19:14:55 | [diff] [blame] | 320 | } |
beaudoin@chromium.org | 10aeaf1 | 2013-02-05 07:41:46 | [diff] [blame] | 321 | } |
Angela Yoeurng | 6e3d6b7 | 2022-04-18 21:26:17 | [diff] [blame] | 322 | url_to_update->id = original_turl->id(); |
| 323 | url_to_update->sync_guid = original_turl->sync_guid(); |
| 324 | url_to_update->date_created = original_turl->date_created(); |
| 325 | url_to_update->last_modified = original_turl->last_modified(); |
| 326 | url_to_update->created_from_play_api = original_turl->created_from_play_api(); |
beaudoin@chromium.org | 10aeaf1 | 2013-02-05 07:41:46 | [diff] [blame] | 327 | } |
| 328 | |
Angela Yoeurng | 6e3d6b7 | 2022-04-18 21:26:17 | [diff] [blame] | 329 | ActionsFromCurrentData::ActionsFromCurrentData() = default; |
vasilii@chromium.org | e937a970 | 2013-10-03 22:59:43 | [diff] [blame] | 330 | |
Angela Yoeurng | 6e3d6b7 | 2022-04-18 21:26:17 | [diff] [blame] | 331 | ActionsFromCurrentData::ActionsFromCurrentData( |
| 332 | const ActionsFromCurrentData& other) = default; |
vmpstr | b6449d51 | 2016-02-25 23:55:40 | [diff] [blame] | 333 | |
Angela Yoeurng | 6e3d6b7 | 2022-04-18 21:26:17 | [diff] [blame] | 334 | ActionsFromCurrentData::~ActionsFromCurrentData() = default; |
vasilii@chromium.org | e937a970 | 2013-10-03 22:59:43 | [diff] [blame] | 335 | |
levin@chromium.org | e1ddda0 | 2010-08-26 19:43:48 | [diff] [blame] | 336 | void MergeEnginesFromPrepopulateData( |
hashimoto@chromium.org | 37b32460 | 2014-07-02 07:30:49 | [diff] [blame] | 337 | KeywordWebDataService* service, |
avi | e8828f2 | 2016-09-02 18:07:58 | [diff] [blame] | 338 | std::vector<std::unique_ptr<TemplateURLData>>* prepopulated_urls, |
avi | 8a64b715b | 2016-09-02 17:30:04 | [diff] [blame] | 339 | TemplateURLService::OwnedTemplateURLVector* template_urls, |
erikwright@chromium.org | a80ec96 | 2014-05-12 00:55:38 | [diff] [blame] | 340 | TemplateURL* default_search_provider, |
stevet@chromium.org | 2eff6b1 | 2012-05-16 20:07:05 | [diff] [blame] | 341 | std::set<std::string>* removed_keyword_guids) { |
vasilii@chromium.org | e937a970 | 2013-10-03 22:59:43 | [diff] [blame] | 342 | DCHECK(prepopulated_urls); |
levin@chromium.org | e1ddda0 | 2010-08-26 19:43:48 | [diff] [blame] | 343 | DCHECK(template_urls); |
levin@chromium.org | e1ddda0 | 2010-08-26 19:43:48 | [diff] [blame] | 344 | |
Angela Yoeurng | 6e3d6b7 | 2022-04-18 21:26:17 | [diff] [blame] | 345 | ActionsFromCurrentData actions(CreateActionsFromCurrentPrepopulateData( |
erikwright@chromium.org | a80ec96 | 2014-05-12 00:55:38 | [diff] [blame] | 346 | prepopulated_urls, *template_urls, default_search_provider)); |
vasilii@chromium.org | e937a970 | 2013-10-03 22:59:43 | [diff] [blame] | 347 | |
Angela Yoeurng | 6e3d6b7 | 2022-04-18 21:26:17 | [diff] [blame] | 348 | ApplyActionsFromCurrentData(actions, service, template_urls, |
| 349 | default_search_provider, removed_keyword_guids); |
| 350 | } |
| 351 | |
| 352 | ActionsFromCurrentData CreateActionsFromCurrentPrepopulateData( |
| 353 | std::vector<std::unique_ptr<TemplateURLData>>* prepopulated_urls, |
| 354 | const TemplateURLService::OwnedTemplateURLVector& existing_urls, |
| 355 | const TemplateURL* default_search_provider) { |
| 356 | // Create a map to hold all provided |template_urls| that originally came from |
| 357 | // prepopulate data (i.e. have a non-zero prepopulate_id()). |
| 358 | TemplateURL* play_api_turl = nullptr; |
| 359 | std::map<int, TemplateURL*> id_to_turl; |
| 360 | for (auto& turl : existing_urls) { |
| 361 | if (turl->created_from_play_api()) { |
| 362 | DCHECK_EQ(nullptr, play_api_turl); |
| 363 | play_api_turl = turl.get(); |
| 364 | } |
| 365 | int prepopulate_id = turl->prepopulate_id(); |
| 366 | if (prepopulate_id > 0) |
| 367 | id_to_turl[prepopulate_id] = turl.get(); |
| 368 | } |
| 369 | |
| 370 | // For each current prepopulated URL, check whether |template_urls| contained |
| 371 | // a matching prepopulated URL. If so, update the passed-in URL to match the |
| 372 | // current data. (If the passed-in URL was user-edited, we persist the user's |
| 373 | // name and keyword.) If not, add the prepopulated URL. |
| 374 | ActionsFromCurrentData actions; |
| 375 | for (auto& prepopulated_url : *prepopulated_urls) { |
| 376 | const int prepopulated_id = prepopulated_url->prepopulate_id; |
| 377 | DCHECK_NE(0, prepopulated_id); |
| 378 | |
| 379 | auto existing_url_iter = id_to_turl.find(prepopulated_id); |
| 380 | TemplateURL* existing_url = nullptr; |
| 381 | if (existing_url_iter != id_to_turl.end()) { |
| 382 | existing_url = existing_url_iter->second; |
| 383 | id_to_turl.erase(existing_url_iter); |
| 384 | } else if (play_api_turl && |
| 385 | play_api_turl->keyword() == prepopulated_url->keyword()) { |
| 386 | existing_url = play_api_turl; |
| 387 | } |
| 388 | |
| 389 | if (existing_url != nullptr) { |
| 390 | // Update the data store with the new prepopulated data. Preserve user |
| 391 | // edits to the name and keyword. |
| 392 | MergeIntoEngineData(existing_url, prepopulated_url.get()); |
| 393 | // Update last_modified to ensure that if this entry is later merged with |
| 394 | // entries from Sync, the conflict resolution logic knows that this was |
| 395 | // updated and propagates the new values to the server. |
| 396 | prepopulated_url->last_modified = base::Time::Now(); |
| 397 | actions.edited_engines.push_back({existing_url, *prepopulated_url}); |
| 398 | } else { |
| 399 | actions.added_engines.push_back(*prepopulated_url); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | // The block above removed all the URLs from the |id_to_turl| map that were |
| 404 | // found in the prepopulate data. Any remaining URLs that haven't been |
| 405 | // user-edited or made default can be removed from the data store. |
| 406 | // We assume that this entry is equivalent to the DSE if its prepopulate ID |
| 407 | // and keyword both match. If the prepopulate ID _does_ match all properties |
| 408 | // will be replaced with those from |default_search_provider| anyway. |
| 409 | for (auto& i : id_to_turl) { |
| 410 | TemplateURL* template_url = i.second; |
| 411 | if ((template_url->safe_for_autoreplace()) && |
| 412 | (!default_search_provider || |
| 413 | (template_url->prepopulate_id() != |
| 414 | default_search_provider->prepopulate_id()) || |
| 415 | (template_url->keyword() != default_search_provider->keyword()))) { |
| 416 | if (template_url->created_from_play_api()) { |
| 417 | // Don't remove the entry created from Play API. Just reset |
| 418 | // prepopulate_id for it. |
| 419 | TemplateURLData data = template_url->data(); |
| 420 | data.prepopulate_id = 0; |
| 421 | actions.edited_engines.push_back({template_url, data}); |
| 422 | } else { |
| 423 | actions.removed_engines.push_back(template_url); |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | return actions; |
| 429 | } |
| 430 | |
Jack Yammine | abb0ba2 | 2023-07-20 10:46:34 | [diff] [blame] | 431 | const std::string& GetDefaultSearchProviderPrefValue(PrefService& prefs) { |
Jens Mueller | 087e5c4 | 2023-10-05 11:36:05 | [diff] [blame] | 432 | if (search_engines::IsChoiceScreenFlagEnabled( |
| 433 | search_engines::ChoicePromo::kAny)) { |
Jack Yammine | abb0ba2 | 2023-07-20 10:46:34 | [diff] [blame] | 434 | const auto& default_search_provider = |
| 435 | prefs.GetString(prefs::kDefaultSearchProviderGUID); |
| 436 | |
| 437 | if (!default_search_provider.empty()) { |
| 438 | return default_search_provider; |
| 439 | } |
| 440 | |
| 441 | const auto& synced_default_search_provider = |
| 442 | prefs.GetString(prefs::kSyncedDefaultSearchProviderGUID); |
| 443 | if (!synced_default_search_provider.empty()) { |
| 444 | prefs.SetString(prefs::kDefaultSearchProviderGUID, |
| 445 | synced_default_search_provider); |
| 446 | } |
| 447 | return synced_default_search_provider; |
| 448 | } |
| 449 | return prefs.GetString(prefs::kSyncedDefaultSearchProviderGUID); |
| 450 | } |
| 451 | |
| 452 | void SetDefaultSearchProviderPrefValue(PrefService& prefs, |
| 453 | const std::string& value) { |
Jens Mueller | 087e5c4 | 2023-10-05 11:36:05 | [diff] [blame] | 454 | if (search_engines::IsChoiceScreenFlagEnabled( |
| 455 | search_engines::ChoicePromo::kAny)) { |
Jack Yammine | abb0ba2 | 2023-07-20 10:46:34 | [diff] [blame] | 456 | prefs.SetString(prefs::kDefaultSearchProviderGUID, value); |
| 457 | } else { |
| 458 | prefs.SetString(prefs::kSyncedDefaultSearchProviderGUID, value); |
| 459 | } |
| 460 | } |
| 461 | |
Angela Yoeurng | d8e5493 | 2022-04-27 23:53:16 | [diff] [blame] | 462 | void MergeEnginesFromStarterPackData( |
| 463 | KeywordWebDataService* service, |
| 464 | TemplateURLService::OwnedTemplateURLVector* template_urls, |
| 465 | TemplateURL* default_search_provider, |
Angela Yoeurng | d164d89d | 2022-07-27 01:25:43 | [diff] [blame] | 466 | std::set<std::string>* removed_keyword_guids, |
Tommy C. Li | 7a3609a | 2022-12-02 23:11:35 | [diff] [blame] | 467 | TemplateURLMergeOption merge_option) { |
Angela Yoeurng | d8e5493 | 2022-04-27 23:53:16 | [diff] [blame] | 468 | DCHECK(template_urls); |
| 469 | |
| 470 | std::vector<std::unique_ptr<TemplateURLData>> starter_pack_urls = |
| 471 | TemplateURLStarterPackData::GetStarterPackEngines(); |
| 472 | |
| 473 | ActionsFromCurrentData actions(CreateActionsFromCurrentStarterPackData( |
Angela Yoeurng | d164d89d | 2022-07-27 01:25:43 | [diff] [blame] | 474 | &starter_pack_urls, *template_urls, merge_option)); |
Angela Yoeurng | d8e5493 | 2022-04-27 23:53:16 | [diff] [blame] | 475 | |
| 476 | ApplyActionsFromCurrentData(actions, service, template_urls, |
| 477 | default_search_provider, removed_keyword_guids); |
| 478 | } |
| 479 | |
| 480 | ActionsFromCurrentData CreateActionsFromCurrentStarterPackData( |
| 481 | std::vector<std::unique_ptr<TemplateURLData>>* starter_pack_urls, |
Angela Yoeurng | d164d89d | 2022-07-27 01:25:43 | [diff] [blame] | 482 | const TemplateURLService::OwnedTemplateURLVector& existing_urls, |
Tommy C. Li | 7a3609a | 2022-12-02 23:11:35 | [diff] [blame] | 483 | TemplateURLMergeOption merge_option) { |
Angela Yoeurng | d8e5493 | 2022-04-27 23:53:16 | [diff] [blame] | 484 | // Create a map to hold all provided |template_urls| that originally came from |
| 485 | // starter_pack data (i.e. have a non-zero starter_pack_id()). |
| 486 | std::map<int, TemplateURL*> id_to_turl; |
| 487 | for (auto& turl : existing_urls) { |
| 488 | int starter_pack_id = turl->starter_pack_id(); |
| 489 | if (starter_pack_id > 0) |
| 490 | id_to_turl[starter_pack_id] = turl.get(); |
| 491 | } |
| 492 | |
| 493 | // For each current starter pack URL, check whether |template_urls| contained |
| 494 | // a matching starter pack URL. If so, update the passed-in URL to match the |
| 495 | // current data. (If the passed-in URL was user-edited, we persist the user's |
| 496 | // name and keyword.) If not, add the prepopulated URL. |
| 497 | ActionsFromCurrentData actions; |
| 498 | for (auto& url : *starter_pack_urls) { |
| 499 | const int starter_pack_id = url->starter_pack_id; |
| 500 | DCHECK_NE(0, starter_pack_id); |
| 501 | |
| 502 | auto existing_url_iter = id_to_turl.find(starter_pack_id); |
| 503 | TemplateURL* existing_url = nullptr; |
| 504 | if (existing_url_iter != id_to_turl.end()) { |
| 505 | existing_url = existing_url_iter->second; |
| 506 | id_to_turl.erase(existing_url_iter); |
| 507 | } |
| 508 | |
| 509 | if (existing_url != nullptr) { |
| 510 | // Update the data store with the new prepopulated data. Preserve user |
Angela Yoeurng | d164d89d | 2022-07-27 01:25:43 | [diff] [blame] | 511 | // edits to the name and keyword unless `merge_option` is set to |
| 512 | // kOverwriteUserEdits. |
| 513 | MergeIntoEngineData(existing_url, url.get(), merge_option); |
Angela Yoeurng | d8e5493 | 2022-04-27 23:53:16 | [diff] [blame] | 514 | // Update last_modified to ensure that if this entry is later merged with |
| 515 | // entries from Sync, the conflict resolution logic knows that this was |
| 516 | // updated and propagates the new values to the server. |
| 517 | url->last_modified = base::Time::Now(); |
| 518 | actions.edited_engines.push_back({existing_url, *url}); |
| 519 | } else { |
| 520 | actions.added_engines.push_back(*url); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | // The block above removed all the URLs from the |id_to_turl| map that were |
| 525 | // found in the prepopulate data. Any remaining URLs that haven't been |
| 526 | // user-edited can be removed from the data store. |
| 527 | for (auto& i : id_to_turl) { |
| 528 | TemplateURL* template_url = i.second; |
| 529 | if (template_url->safe_for_autoreplace()) { |
| 530 | actions.removed_engines.push_back(template_url); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | return actions; |
| 535 | } |
| 536 | |
Angela Yoeurng | 6e3d6b7 | 2022-04-18 21:26:17 | [diff] [blame] | 537 | void ApplyActionsFromCurrentData( |
| 538 | ActionsFromCurrentData actions, |
| 539 | KeywordWebDataService* service, |
| 540 | TemplateURLService::OwnedTemplateURLVector* template_urls, |
| 541 | TemplateURL* default_search_provider, |
| 542 | std::set<std::string>* removed_keyword_guids) { |
| 543 | DCHECK(template_urls); |
| 544 | |
vasilii@chromium.org | e937a970 | 2013-10-03 22:59:43 | [diff] [blame] | 545 | // Remove items. |
Ali Hijazi | e63cbaf6 | 2023-12-20 19:29:35 | [diff] [blame] | 546 | for (const TemplateURL* removed_engine : actions.removed_engines) { |
avi | 8a64b715b | 2016-09-02 17:30:04 | [diff] [blame] | 547 | auto j = FindTemplateURL(template_urls, removed_engine); |
vasilii@chromium.org | e937a970 | 2013-10-03 22:59:43 | [diff] [blame] | 548 | DCHECK(j != template_urls->end()); |
erikwright@chromium.org | a80ec96 | 2014-05-12 00:55:38 | [diff] [blame] | 549 | DCHECK(!default_search_provider || |
| 550 | (*j)->prepopulate_id() != default_search_provider->prepopulate_id()); |
avi | b350dc8 | 2016-10-11 01:48:11 | [diff] [blame] | 551 | std::unique_ptr<TemplateURL> template_url = std::move(*j); |
vasilii@chromium.org | e937a970 | 2013-10-03 22:59:43 | [diff] [blame] | 552 | template_urls->erase(j); |
| 553 | if (service) { |
| 554 | service->RemoveKeyword(template_url->id()); |
| 555 | if (removed_keyword_guids) |
| 556 | removed_keyword_guids->insert(template_url->sync_guid()); |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | // Edit items. |
avi | 8a64b715b | 2016-09-02 17:30:04 | [diff] [blame] | 561 | for (const auto& edited_engine : actions.edited_engines) { |
| 562 | const TemplateURLData& data = edited_engine.second; |
vasilii@chromium.org | e937a970 | 2013-10-03 22:59:43 | [diff] [blame] | 563 | if (service) |
| 564 | service->UpdateKeyword(data); |
| 565 | |
| 566 | // Replace the entry in |template_urls| with the updated one. |
avi | 8a64b715b | 2016-09-02 17:30:04 | [diff] [blame] | 567 | auto j = FindTemplateURL(template_urls, edited_engine.first); |
Jinho Bang | fa6b575 | 2018-01-03 20:52:19 | [diff] [blame] | 568 | *j = std::make_unique<TemplateURL>(data); |
vasilii@chromium.org | e937a970 | 2013-10-03 22:59:43 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | // Add items. |
avi | 8a64b715b | 2016-09-02 17:30:04 | [diff] [blame] | 572 | for (const auto& added_engine : actions.added_engines) |
Jinho Bang | fa6b575 | 2018-01-03 20:52:19 | [diff] [blame] | 573 | template_urls->push_back(std::make_unique<TemplateURL>(added_engine)); |
vasilii@chromium.org | e937a970 | 2013-10-03 22:59:43 | [diff] [blame] | 574 | } |
| 575 | |
levin@chromium.org | e1ddda0 | 2010-08-26 19:43:48 | [diff] [blame] | 576 | void GetSearchProvidersUsingKeywordResult( |
| 577 | const WDTypedResult& result, |
hashimoto@chromium.org | 37b32460 | 2014-07-02 07:30:49 | [diff] [blame] | 578 | KeywordWebDataService* service, |
hashimoto@chromium.org | 1f8841b | 2014-06-27 02:31:40 | [diff] [blame] | 579 | PrefService* prefs, |
Boris Sazonov | 2b77815a | 2024-01-17 15:35:04 | [diff] [blame] | 580 | search_engines::SearchEngineChoiceService* search_engine_choice_service, |
avi | 8a64b715b | 2016-09-02 17:30:04 | [diff] [blame] | 581 | TemplateURLService::OwnedTemplateURLVector* template_urls, |
erikwright@chromium.org | a80ec96 | 2014-05-12 00:55:38 | [diff] [blame] | 582 | TemplateURL* default_search_provider, |
hashimoto@chromium.org | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 583 | const SearchTermsData& search_terms_data, |
Nicolas Dossou-Gbete | af259e7 | 2024-01-19 20:53:55 | [diff] [blame] | 584 | WDKeywordsResult::Metadata& out_updated_keywords_metadata, |
stevet@chromium.org | 2eff6b1 | 2012-05-16 20:07:05 | [diff] [blame] | 585 | std::set<std::string>* removed_keyword_guids) { |
levin@chromium.org | e1ddda0 | 2010-08-26 19:43:48 | [diff] [blame] | 586 | DCHECK(template_urls); |
| 587 | DCHECK(template_urls->empty()); |
pkasting@chromium.org | 317dec7 | 2012-06-05 20:58:30 | [diff] [blame] | 588 | DCHECK_EQ(KEYWORDS_RESULT, result.GetType()); |
levin@chromium.org | e1ddda0 | 2010-08-26 19:43:48 | [diff] [blame] | 589 | |
levin@chromium.org | e1ddda0 | 2010-08-26 19:43:48 | [diff] [blame] | 590 | WDKeywordsResult keyword_result = reinterpret_cast< |
| 591 | const WDResult<WDKeywordsResult>*>(&result)->GetValue(); |
| 592 | |
avi | 8a64b715b | 2016-09-02 17:30:04 | [diff] [blame] | 593 | for (auto& keyword : keyword_result.keywords) { |
pkasting@chromium.org | a262230 | 2012-06-27 03:36:07 | [diff] [blame] | 594 | // Fix any duplicate encodings in the local database. Note that we don't |
| 595 | // adjust the last_modified time of this keyword; this way, we won't later |
| 596 | // overwrite any changes on the sync server that happened to this keyword |
| 597 | // since the last time we synced. Instead, we also run a de-duping pass on |
| 598 | // the server-provided data in |
| 599 | // TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData() and |
| 600 | // update the server with the merged, de-duped results at that time. We |
| 601 | // still fix here, though, to correct problems in clients that have disabled |
| 602 | // search engine sync, since in that case that code will never be reached. |
avi | 8a64b715b | 2016-09-02 17:30:04 | [diff] [blame] | 603 | if (DeDupeEncodings(&keyword.input_encodings) && service) |
| 604 | service->UpdateKeyword(keyword); |
Jinho Bang | fa6b575 | 2018-01-03 20:52:19 | [diff] [blame] | 605 | template_urls->push_back(std::make_unique<TemplateURL>(keyword)); |
pkasting@chromium.org | a262230 | 2012-06-27 03:36:07 | [diff] [blame] | 606 | } |
levin@chromium.org | e1ddda0 | 2010-08-26 19:43:48 | [diff] [blame] | 607 | |
Nicolas Dossou-Gbete | af259e7 | 2024-01-19 20:53:55 | [diff] [blame] | 608 | out_updated_keywords_metadata = keyword_result.metadata; |
Angela Yoeurng | d8e5493 | 2022-04-27 23:53:16 | [diff] [blame] | 609 | GetSearchProvidersUsingLoadedEngines( |
Boris Sazonov | 2b77815a | 2024-01-17 15:35:04 | [diff] [blame] | 610 | service, prefs, search_engine_choice_service, template_urls, |
Nicolas Dossou-Gbete | af259e7 | 2024-01-19 20:53:55 | [diff] [blame] | 611 | default_search_provider, search_terms_data, out_updated_keywords_metadata, |
| 612 | removed_keyword_guids); |
Nicolas Dossou-Gbete | fec50c6 | 2023-10-12 22:29:23 | [diff] [blame] | 613 | |
Nicolas Dossou-Gbete | af259e7 | 2024-01-19 20:53:55 | [diff] [blame] | 614 | // If a data change happened, it should not cause a version downgrade. |
| 615 | // Upgrades (builtin > new) or feature-related merges (builtin == new) only |
| 616 | // are expected. |
| 617 | DCHECK(!out_updated_keywords_metadata.HasBuiltinKeywordData() || |
Nicolas Dossou-Gbete | 354c10a5 | 2024-01-22 11:18:37 | [diff] [blame] | 618 | out_updated_keywords_metadata.builtin_keyword_data_version >= |
| 619 | keyword_result.metadata.builtin_keyword_data_version); |
vasilii@chromium.org | 4a40facd | 2013-05-29 14:44:56 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | void GetSearchProvidersUsingLoadedEngines( |
hashimoto@chromium.org | 37b32460 | 2014-07-02 07:30:49 | [diff] [blame] | 623 | KeywordWebDataService* service, |
hashimoto@chromium.org | 1f8841b | 2014-06-27 02:31:40 | [diff] [blame] | 624 | PrefService* prefs, |
Boris Sazonov | 2b77815a | 2024-01-17 15:35:04 | [diff] [blame] | 625 | search_engines::SearchEngineChoiceService* search_engine_choice_service, |
avi | 8a64b715b | 2016-09-02 17:30:04 | [diff] [blame] | 626 | TemplateURLService::OwnedTemplateURLVector* template_urls, |
erikwright@chromium.org | a80ec96 | 2014-05-12 00:55:38 | [diff] [blame] | 627 | TemplateURL* default_search_provider, |
hashimoto@chromium.org | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 628 | const SearchTermsData& search_terms_data, |
Nicolas Dossou-Gbete | af259e7 | 2024-01-19 20:53:55 | [diff] [blame] | 629 | WDKeywordsResult::Metadata& in_out_keywords_metadata, |
vasilii@chromium.org | 4a40facd | 2013-05-29 14:44:56 | [diff] [blame] | 630 | std::set<std::string>* removed_keyword_guids) { |
vasilii@chromium.org | 4a40facd | 2013-05-29 14:44:56 | [diff] [blame] | 631 | DCHECK(template_urls); |
avi | e8828f2 | 2016-09-02 18:07:58 | [diff] [blame] | 632 | std::vector<std::unique_ptr<TemplateURLData>> prepopulated_urls = |
Boris Sazonov | 2b77815a | 2024-01-17 15:35:04 | [diff] [blame] | 633 | TemplateURLPrepopulateData::GetPrepopulatedEngines( |
| 634 | prefs, search_engine_choice_service, nullptr); |
stevet@chromium.org | 01ef4a66 | 2012-05-29 15:58:25 | [diff] [blame] | 635 | RemoveDuplicatePrepopulateIDs(service, prepopulated_urls, |
erikwright@chromium.org | a80ec96 | 2014-05-12 00:55:38 | [diff] [blame] | 636 | default_search_provider, template_urls, |
hashimoto@chromium.org | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 637 | search_terms_data, removed_keyword_guids); |
stevet@chromium.org | 01ef4a66 | 2012-05-29 15:58:25 | [diff] [blame] | 638 | |
Nicolas Dossou-Gbete | af259e7 | 2024-01-19 20:53:55 | [diff] [blame] | 639 | MergeEngineRequirements merge_requirements = ComputeMergeEnginesRequirements( |
| 640 | prefs, search_engine_choice_service, in_out_keywords_metadata); |
Nicolas Dossou-Gbete | fec50c6 | 2023-10-12 22:29:23 | [diff] [blame] | 641 | |
Nicolas Dossou-Gbete | af259e7 | 2024-01-19 20:53:55 | [diff] [blame] | 642 | if (merge_requirements.metadata.HasBuiltinKeywordData()) { |
Pavel Yatsuk | a3e54982 | 2019-09-20 21:55:41 | [diff] [blame] | 643 | MergeEnginesFromPrepopulateData(service, &prepopulated_urls, template_urls, |
| 644 | default_search_provider, |
| 645 | removed_keyword_guids); |
levin@chromium.org | e1ddda0 | 2010-08-26 19:43:48 | [diff] [blame] | 646 | } |
Angela Yoeurng | d8e5493 | 2022-04-27 23:53:16 | [diff] [blame] | 647 | |
Nicolas Dossou-Gbete | af259e7 | 2024-01-19 20:53:55 | [diff] [blame] | 648 | if (merge_requirements.metadata.HasStarterPackData()) { |
| 649 | bool overwrite_user_edits = |
| 650 | (in_out_keywords_metadata.starter_pack_version < |
| 651 | TemplateURLStarterPackData::GetFirstCompatibleDataVersion()); |
Angela Yoeurng | d8e5493 | 2022-04-27 23:53:16 | [diff] [blame] | 652 | MergeEnginesFromStarterPackData( |
Angela Yoeurng | d164d89d | 2022-07-27 01:25:43 | [diff] [blame] | 653 | service, template_urls, default_search_provider, removed_keyword_guids, |
Tommy C. Li | 7a3609a | 2022-12-02 23:11:35 | [diff] [blame] | 654 | (overwrite_user_edits ? TemplateURLMergeOption::kOverwriteUserEdits |
| 655 | : TemplateURLMergeOption::kDefault)); |
Nicolas Dossou-Gbete | af259e7 | 2024-01-19 20:53:55 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | in_out_keywords_metadata = merge_requirements.metadata; |
| 659 | switch (merge_requirements.should_keywords_use_extended_list) { |
| 660 | case MergeEngineRequirements::ShouldKeywordsUseExtendedList::kUnknown: |
| 661 | // Do nothing. |
| 662 | break; |
| 663 | case MergeEngineRequirements::ShouldKeywordsUseExtendedList::kYes: |
| 664 | prefs->SetBoolean(prefs::kDefaultSearchProviderKeywordsUseExtendedList, |
| 665 | true); |
| 666 | break; |
| 667 | case MergeEngineRequirements::ShouldKeywordsUseExtendedList::kNo: |
| 668 | prefs->ClearPref(prefs::kDefaultSearchProviderKeywordsUseExtendedList); |
| 669 | break; |
Angela Yoeurng | d8e5493 | 2022-04-27 23:53:16 | [diff] [blame] | 670 | } |
levin@chromium.org | e1ddda0 | 2010-08-26 19:43:48 | [diff] [blame] | 671 | } |
jeanluc@google.com | b6fd1fa7 | 2010-10-05 23:49:31 | [diff] [blame] | 672 | |
pkasting@chromium.org | a262230 | 2012-06-27 03:36:07 | [diff] [blame] | 673 | bool DeDupeEncodings(std::vector<std::string>* encodings) { |
| 674 | std::vector<std::string> deduped_encodings; |
| 675 | std::set<std::string> encoding_set; |
| 676 | for (std::vector<std::string>::const_iterator i(encodings->begin()); |
| 677 | i != encodings->end(); ++i) { |
| 678 | if (encoding_set.insert(*i).second) |
| 679 | deduped_encodings.push_back(*i); |
| 680 | } |
| 681 | encodings->swap(deduped_encodings); |
| 682 | return encodings->size() != deduped_encodings.size(); |
| 683 | } |
avi | 8a64b715b | 2016-09-02 17:30:04 | [diff] [blame] | 684 | |
| 685 | TemplateURLService::OwnedTemplateURLVector::iterator FindTemplateURL( |
| 686 | TemplateURLService::OwnedTemplateURLVector* urls, |
| 687 | const TemplateURL* url) { |
Peter Kasting | 03752ef | 2022-09-02 18:58:35 | [diff] [blame] | 688 | return base::ranges::find(*urls, url, &std::unique_ptr<TemplateURL>::get); |
avi | 8a64b715b | 2016-09-02 17:30:04 | [diff] [blame] | 689 | } |