[go: nahoru, domu]

blob: f5fb7657f3c408c999091268036a320011336391 [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2014 The Chromium Authors
thakis@chromium.org96788b02010-06-26 21:45:342// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
hashimoto@chromium.orgbf5c532d2014-07-05 00:29:535#include "components/search_engines/util.h"
thakis@chromium.org96788b02010-06-26 21:45:346
avif57136c12015-12-25 23:27:457#include <stddef.h>
8#include <stdint.h>
9
Orin Jaworski286774862019-01-28 18:43:2110#include <limits>
joi@chromium.org3853a4c2013-02-11 17:15:5711#include <map>
levin@chromium.orge1ddda02010-08-26 19:43:4812#include <set>
stevet@chromium.org2eff6b12012-05-16 20:07:0513#include <string>
Orin Jaworski286774862019-01-28 18:43:2114#include <unordered_map>
levin@chromium.orge1ddda02010-08-26 19:43:4815#include <vector>
16
Nicolas Dossou-Gbeteaf259e72024-01-19 20:53:5517#include "base/check_is_test.h"
Hans Wennborgdf87046c2020-04-28 11:06:2418#include "base/check_op.h"
Jack Yammineabb0ba22023-07-20 10:46:3419#include "base/feature_list.h"
Peter Kasting03752ef2022-09-02 18:58:3520#include "base/ranges/algorithm.h"
avi@chromium.orgcc86ccfe2013-06-28 00:10:5021#include "base/time/time.h"
Nicolas Dossou-Gbete354c10a52024-01-22 11:18:3722#include "base/version_info/version_info.h"
Nicolas Dossou-Gbeteaf259e72024-01-19 20:53:5523#include "components/country_codes/country_codes.h"
brettwf00b9b42016-02-01 22:11:3824#include "components/prefs/pref_service.h"
Nicolas Dossou-Gbeteaf259e72024-01-19 20:53:5525#include "components/search_engines/keyword_web_data_service.h"
Boris Sazonov2b77815a2024-01-17 15:35:0426#include "components/search_engines/search_engine_choice/search_engine_choice_service.h"
Jens Mueller087e5c42023-10-05 11:36:0527#include "components/search_engines/search_engine_choice_utils.h"
Jack Yammineabb0ba22023-07-20 10:46:3428#include "components/search_engines/search_engines_pref_names.h"
hashimoto@chromium.orgd550cb02014-06-25 06:48:1129#include "components/search_engines/template_url.h"
hashimoto@chromium.org0915b352014-06-25 19:58:1430#include "components/search_engines/template_url_prepopulate_data.h"
hashimoto@chromium.orgbf5c532d2014-07-05 00:29:5331#include "components/search_engines/template_url_service.h"
Angela Yoeurngd8e54932022-04-27 23:53:1632#include "components/search_engines/template_url_starter_pack_data.h"
thakis@chromium.org96788b02010-06-26 21:45:3433
Nicolas Dossou-Gbeteaf259e72024-01-19 20:53:5534namespace {
35
36// Indicates whether updates to the search engines database are needed.
37struct 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
52MergeEngineRequirements 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-Gbete354c10a52024-01-22 11:18:3772 const int milestone = version_info::GetMajorVersionNumberAsInt();
Nicolas Dossou-Gbeteaf259e72024-01-19 20:53:5573
74 bool update_builtin_keywords;
Nicolas Dossou-Gbete354c10a52024-01-22 11:18:3775 if (keywords_metadata.builtin_keyword_data_version >
Nicolas Dossou-Gbeteaf259e72024-01-19 20:53:5576 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-Gbete354c10a52024-01-22 11:18:3780 } else if (keywords_metadata.builtin_keyword_data_version <
Nicolas Dossou-Gbeteaf259e72024-01-19 20:53:5581 prepopulate_resource_keyword_version) {
82 // The built-in data from `prepopulated_engines.json` has been updated.
83 update_builtin_keywords = true;
Nicolas Dossou-Gbete354c10a52024-01-22 11:18:3784 } 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-Gbeteaf259e72024-01-19 20:53:5592 } 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-Gbete354c10a52024-01-22 11:18:3799 } 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-Gbeteaf259e72024-01-19 20:53:55109 } else {
110 update_builtin_keywords = false;
111 }
112
113 MergeEngineRequirements merge_requirements;
114
115 if (update_builtin_keywords) {
Nicolas Dossou-Gbete354c10a52024-01-22 11:18:37116 merge_requirements.metadata.builtin_keyword_data_version =
Nicolas Dossou-Gbeteaf259e72024-01-19 20:53:55117 prepopulate_resource_keyword_version;
Nicolas Dossou-Gbete354c10a52024-01-22 11:18:37118 merge_requirements.metadata.builtin_keyword_milestone = milestone;
119 merge_requirements.metadata.builtin_keyword_country = country_id;
Nicolas Dossou-Gbeteaf259e72024-01-19 20:53:55120 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örriefa241ba2021-03-11 17:57:01138std::u16string GetDefaultSearchEngineName(TemplateURLService* service) {
hashimoto@chromium.org1f8841b2014-06-27 02:31:40139 DCHECK(service);
thakis@chromium.org96788b02010-06-26 21:45:34140 const TemplateURL* const default_provider =
hashimoto@chromium.org1f8841b2014-06-27 02:31:40141 service->GetDefaultSearchProvider();
thakis@chromium.org96788b02010-06-26 21:45:34142 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örriefa241ba2021-03-11 17:57:01146 return std::u16string();
thakis@chromium.org96788b02010-06-26 21:45:34147 }
avi@chromium.org400b133f2011-01-19 18:32:30148 return default_provider->short_name();
thakis@chromium.org96788b02010-06-26 21:45:34149}
levin@chromium.orge1ddda02010-08-26 19:43:48150
hashimoto@chromium.org1f8841b2014-06-27 02:31:40151GURL GetDefaultSearchURLForSearchTerms(TemplateURLService* service,
Jan Wilken Dörriefa241ba2021-03-11 17:57:01152 const std::u16string& terms) {
hashimoto@chromium.org1f8841b2014-06-27 02:31:40153 DCHECK(service);
hashimoto@chromium.orgce7ee5f2014-06-16 23:41:19154 const TemplateURL* default_provider = service->GetDefaultSearchProvider();
pkasting@chromium.orgbba9e632013-06-28 22:52:19155 if (!default_provider)
156 return GURL();
157 const TemplateURLRef& search_url = default_provider->url_ref();
hashimoto@chromium.orgce7ee5f2014-06-16 23:41:19158 DCHECK(search_url.SupportsReplacement(service->search_terms_data()));
pkasting@chromium.orgbba9e632013-06-28 22:52:19159 TemplateURLRef::SearchTermsArgs search_terms_args(terms);
Mark Pearson247fb8a2018-08-30 05:12:26160 search_terms_args.append_extra_query_params_from_command_line = true;
hashimoto@chromium.orgce7ee5f2014-06-16 23:41:19161 return GURL(search_url.ReplaceSearchTerms(search_terms_args,
162 service->search_terms_data()));
pkasting@chromium.orgbba9e632013-06-28 22:52:19163}
164
stevet@chromium.org01ef4a662012-05-29 15:58:25165void RemoveDuplicatePrepopulateIDs(
hashimoto@chromium.org37b324602014-07-02 07:30:49166 KeywordWebDataService* service,
avie8828f22016-09-02 18:07:58167 const std::vector<std::unique_ptr<TemplateURLData>>& prepopulated_urls,
stevet@chromium.org01ef4a662012-05-29 15:58:25168 TemplateURL* default_search_provider,
avi8a64b715b2016-09-02 17:30:04169 TemplateURLService::OwnedTemplateURLVector* template_urls,
hashimoto@chromium.orgce7ee5f2014-06-16 23:41:19170 const SearchTermsData& search_terms_data,
stevet@chromium.org2eff6b12012-05-16 20:07:05171 std::set<std::string>* removed_keyword_guids) {
stevet@chromium.org01ef4a662012-05-29 15:58:25172 DCHECK(template_urls);
Orin Jaworskiceb59642019-01-25 21:29:53173 TemplateURLService::OwnedTemplateURLVector checked_urls;
levin@chromium.orge1ddda02010-08-26 19:43:48174
stevet@chromium.org01ef4a662012-05-29 15:58:25175 // For convenience construct an ID->TemplateURL* map from |prepopulated_urls|.
avi8a64b715b2016-09-02 17:30:04176 std::map<int, TemplateURLData*> prepopulated_url_map;
avie8828f22016-09-02 18:07:58177 for (const auto& url : prepopulated_urls)
178 prepopulated_url_map[url->prepopulate_id] = url.get();
stevet@chromium.org01ef4a662012-05-29 15:58:25179
Orin Jaworskiceb59642019-01-25 21:29:53180 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.
avi8a64b715b2016-09-02 17:30:04205 for (auto& turl : *template_urls) {
Orin Jaworskiceb59642019-01-25 21:29:53206 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 {
avi8a64b715b2016-09-02 17:30:04218 checked_urls.push_back(std::move(turl));
Orin Jaworskiceb59642019-01-25 21:29:53219 }
levin@chromium.orge1ddda02010-08-26 19:43:48220 }
stevet@chromium.org01ef4a662012-05-29 15:58:25221
Orin Jaworskiceb59642019-01-25 21:29:53222 // 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.org01ef4a662012-05-29 15:58:25232
Orin Jaworskiceb59642019-01-25 21:29:53233 // 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.org01ef4a662012-05-29 15:58:25256 }
257 }
258
Orin Jaworskiceb59642019-01-25 21:29:53259 // Clean up what's left.
260 for (const auto& duplicate : id_data.second.duplicates) {
stevet@chromium.org01ef4a662012-05-29 15:58:25261 if (service) {
Orin Jaworskiceb59642019-01-25 21:29:53262 service->RemoveKeyword(duplicate->id());
stevet@chromium.org01ef4a662012-05-29 15:58:25263 if (removed_keyword_guids)
Orin Jaworskiceb59642019-01-25 21:29:53264 removed_keyword_guids->insert(duplicate->sync_guid());
stevet@chromium.org01ef4a662012-05-29 15:58:25265 }
stevet@chromium.org01ef4a662012-05-29 15:58:25266 }
stevet@chromium.org01ef4a662012-05-29 15:58:25267 }
268
269 // Return the checked URLs.
270 template_urls->swap(checked_urls);
levin@chromium.orge1ddda02010-08-26 19:43:48271}
272
avayvod@chromium.org75a4eca2011-10-26 20:40:09273// Returns the TemplateURL with id specified from the list of TemplateURLs.
274// If not found, returns NULL.
275TemplateURL* GetTemplateURLByID(
stevet@chromium.org01ef4a662012-05-29 15:58:25276 const TemplateURLService::TemplateURLVector& template_urls,
avif57136c12015-12-25 23:27:45277 int64_t id) {
jdoerrie3feb1852018-10-05 12:16:44278 for (auto i(template_urls.begin()); i != template_urls.end(); ++i) {
avayvod@chromium.org75a4eca2011-10-26 20:40:09279 if ((*i)->id() == id) {
280 return *i;
281 }
282 }
Ivan Kotenkov75b1c3a2017-10-24 14:47:24283 return nullptr;
avayvod@chromium.org75a4eca2011-10-26 20:40:09284}
285
vasilii@chromium.orge937a9702013-10-03 22:59:43286TemplateURL* FindURLByPrepopulateID(
287 const TemplateURLService::TemplateURLVector& template_urls,
288 int prepopulate_id) {
jdoerrie3feb1852018-10-05 12:16:44289 for (auto i = template_urls.begin(); i < template_urls.end(); ++i) {
vasilii@chromium.orge937a9702013-10-03 22:59:43290 if ((*i)->prepopulate_id() == prepopulate_id)
291 return *i;
292 }
Ivan Kotenkov75b1c3a2017-10-24 14:47:24293 return nullptr;
vasilii@chromium.orge937a9702013-10-03 22:59:43294}
295
Angela Yoeurng6e3d6b72022-04-18 21:26:17296void MergeIntoEngineData(const TemplateURL* original_turl,
Angela Yoeurngd164d89d2022-07-27 01:25:43297 TemplateURLData* url_to_update,
Tommy C. Li7a3609a2022-12-02 23:11:35298 TemplateURLMergeOption merge_option) {
Pavel Yatsuka3e549822019-09-20 21:55:41299 DCHECK(original_turl->prepopulate_id() == 0 ||
Angela Yoeurng6e3d6b72022-04-18 21:26:17300 original_turl->prepopulate_id() == url_to_update->prepopulate_id);
Angela Yoeurngd8e54932022-04-27 23:53:16301 DCHECK(original_turl->starter_pack_id() == 0 ||
302 original_turl->starter_pack_id() == url_to_update->starter_pack_id);
Pavel Yatsukbec91d22019-10-07 19:14:55303 // 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 Yoeurngd164d89d2022-07-27 01:25:43306 bool preserve_user_edits =
Tommy C. Li7a3609a2022-12-02 23:11:35307 (merge_option != TemplateURLMergeOption::kOverwriteUserEdits &&
Angela Yoeurngd164d89d2022-07-27 01:25:43308 (!original_turl->safe_for_autoreplace() ||
309 original_turl->created_from_play_api()));
310 if (preserve_user_edits) {
Angela Yoeurng6e3d6b72022-04-18 21:26:17311 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 Yatsukbec91d22019-10-07 19:14:55314 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 Yoeurng6e3d6b72022-04-18 21:26:17319 url_to_update->SetURL(original_turl->url());
Pavel Yatsukbec91d22019-10-07 19:14:55320 }
beaudoin@chromium.org10aeaf12013-02-05 07:41:46321 }
Angela Yoeurng6e3d6b72022-04-18 21:26:17322 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.org10aeaf12013-02-05 07:41:46327}
328
Angela Yoeurng6e3d6b72022-04-18 21:26:17329ActionsFromCurrentData::ActionsFromCurrentData() = default;
vasilii@chromium.orge937a9702013-10-03 22:59:43330
Angela Yoeurng6e3d6b72022-04-18 21:26:17331ActionsFromCurrentData::ActionsFromCurrentData(
332 const ActionsFromCurrentData& other) = default;
vmpstrb6449d512016-02-25 23:55:40333
Angela Yoeurng6e3d6b72022-04-18 21:26:17334ActionsFromCurrentData::~ActionsFromCurrentData() = default;
vasilii@chromium.orge937a9702013-10-03 22:59:43335
levin@chromium.orge1ddda02010-08-26 19:43:48336void MergeEnginesFromPrepopulateData(
hashimoto@chromium.org37b324602014-07-02 07:30:49337 KeywordWebDataService* service,
avie8828f22016-09-02 18:07:58338 std::vector<std::unique_ptr<TemplateURLData>>* prepopulated_urls,
avi8a64b715b2016-09-02 17:30:04339 TemplateURLService::OwnedTemplateURLVector* template_urls,
erikwright@chromium.orga80ec962014-05-12 00:55:38340 TemplateURL* default_search_provider,
stevet@chromium.org2eff6b12012-05-16 20:07:05341 std::set<std::string>* removed_keyword_guids) {
vasilii@chromium.orge937a9702013-10-03 22:59:43342 DCHECK(prepopulated_urls);
levin@chromium.orge1ddda02010-08-26 19:43:48343 DCHECK(template_urls);
levin@chromium.orge1ddda02010-08-26 19:43:48344
Angela Yoeurng6e3d6b72022-04-18 21:26:17345 ActionsFromCurrentData actions(CreateActionsFromCurrentPrepopulateData(
erikwright@chromium.orga80ec962014-05-12 00:55:38346 prepopulated_urls, *template_urls, default_search_provider));
vasilii@chromium.orge937a9702013-10-03 22:59:43347
Angela Yoeurng6e3d6b72022-04-18 21:26:17348 ApplyActionsFromCurrentData(actions, service, template_urls,
349 default_search_provider, removed_keyword_guids);
350}
351
352ActionsFromCurrentData 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 Yammineabb0ba22023-07-20 10:46:34431const std::string& GetDefaultSearchProviderPrefValue(PrefService& prefs) {
Jens Mueller087e5c42023-10-05 11:36:05432 if (search_engines::IsChoiceScreenFlagEnabled(
433 search_engines::ChoicePromo::kAny)) {
Jack Yammineabb0ba22023-07-20 10:46:34434 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
452void SetDefaultSearchProviderPrefValue(PrefService& prefs,
453 const std::string& value) {
Jens Mueller087e5c42023-10-05 11:36:05454 if (search_engines::IsChoiceScreenFlagEnabled(
455 search_engines::ChoicePromo::kAny)) {
Jack Yammineabb0ba22023-07-20 10:46:34456 prefs.SetString(prefs::kDefaultSearchProviderGUID, value);
457 } else {
458 prefs.SetString(prefs::kSyncedDefaultSearchProviderGUID, value);
459 }
460}
461
Angela Yoeurngd8e54932022-04-27 23:53:16462void MergeEnginesFromStarterPackData(
463 KeywordWebDataService* service,
464 TemplateURLService::OwnedTemplateURLVector* template_urls,
465 TemplateURL* default_search_provider,
Angela Yoeurngd164d89d2022-07-27 01:25:43466 std::set<std::string>* removed_keyword_guids,
Tommy C. Li7a3609a2022-12-02 23:11:35467 TemplateURLMergeOption merge_option) {
Angela Yoeurngd8e54932022-04-27 23:53:16468 DCHECK(template_urls);
469
470 std::vector<std::unique_ptr<TemplateURLData>> starter_pack_urls =
471 TemplateURLStarterPackData::GetStarterPackEngines();
472
473 ActionsFromCurrentData actions(CreateActionsFromCurrentStarterPackData(
Angela Yoeurngd164d89d2022-07-27 01:25:43474 &starter_pack_urls, *template_urls, merge_option));
Angela Yoeurngd8e54932022-04-27 23:53:16475
476 ApplyActionsFromCurrentData(actions, service, template_urls,
477 default_search_provider, removed_keyword_guids);
478}
479
480ActionsFromCurrentData CreateActionsFromCurrentStarterPackData(
481 std::vector<std::unique_ptr<TemplateURLData>>* starter_pack_urls,
Angela Yoeurngd164d89d2022-07-27 01:25:43482 const TemplateURLService::OwnedTemplateURLVector& existing_urls,
Tommy C. Li7a3609a2022-12-02 23:11:35483 TemplateURLMergeOption merge_option) {
Angela Yoeurngd8e54932022-04-27 23:53:16484 // 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 Yoeurngd164d89d2022-07-27 01:25:43511 // edits to the name and keyword unless `merge_option` is set to
512 // kOverwriteUserEdits.
513 MergeIntoEngineData(existing_url, url.get(), merge_option);
Angela Yoeurngd8e54932022-04-27 23:53:16514 // 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 Yoeurng6e3d6b72022-04-18 21:26:17537void 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.orge937a9702013-10-03 22:59:43545 // Remove items.
Ali Hijazie63cbaf62023-12-20 19:29:35546 for (const TemplateURL* removed_engine : actions.removed_engines) {
avi8a64b715b2016-09-02 17:30:04547 auto j = FindTemplateURL(template_urls, removed_engine);
vasilii@chromium.orge937a9702013-10-03 22:59:43548 DCHECK(j != template_urls->end());
erikwright@chromium.orga80ec962014-05-12 00:55:38549 DCHECK(!default_search_provider ||
550 (*j)->prepopulate_id() != default_search_provider->prepopulate_id());
avib350dc82016-10-11 01:48:11551 std::unique_ptr<TemplateURL> template_url = std::move(*j);
vasilii@chromium.orge937a9702013-10-03 22:59:43552 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.
avi8a64b715b2016-09-02 17:30:04561 for (const auto& edited_engine : actions.edited_engines) {
562 const TemplateURLData& data = edited_engine.second;
vasilii@chromium.orge937a9702013-10-03 22:59:43563 if (service)
564 service->UpdateKeyword(data);
565
566 // Replace the entry in |template_urls| with the updated one.
avi8a64b715b2016-09-02 17:30:04567 auto j = FindTemplateURL(template_urls, edited_engine.first);
Jinho Bangfa6b5752018-01-03 20:52:19568 *j = std::make_unique<TemplateURL>(data);
vasilii@chromium.orge937a9702013-10-03 22:59:43569 }
570
571 // Add items.
avi8a64b715b2016-09-02 17:30:04572 for (const auto& added_engine : actions.added_engines)
Jinho Bangfa6b5752018-01-03 20:52:19573 template_urls->push_back(std::make_unique<TemplateURL>(added_engine));
vasilii@chromium.orge937a9702013-10-03 22:59:43574}
575
levin@chromium.orge1ddda02010-08-26 19:43:48576void GetSearchProvidersUsingKeywordResult(
577 const WDTypedResult& result,
hashimoto@chromium.org37b324602014-07-02 07:30:49578 KeywordWebDataService* service,
hashimoto@chromium.org1f8841b2014-06-27 02:31:40579 PrefService* prefs,
Boris Sazonov2b77815a2024-01-17 15:35:04580 search_engines::SearchEngineChoiceService* search_engine_choice_service,
avi8a64b715b2016-09-02 17:30:04581 TemplateURLService::OwnedTemplateURLVector* template_urls,
erikwright@chromium.orga80ec962014-05-12 00:55:38582 TemplateURL* default_search_provider,
hashimoto@chromium.orgce7ee5f2014-06-16 23:41:19583 const SearchTermsData& search_terms_data,
Nicolas Dossou-Gbeteaf259e72024-01-19 20:53:55584 WDKeywordsResult::Metadata& out_updated_keywords_metadata,
stevet@chromium.org2eff6b12012-05-16 20:07:05585 std::set<std::string>* removed_keyword_guids) {
levin@chromium.orge1ddda02010-08-26 19:43:48586 DCHECK(template_urls);
587 DCHECK(template_urls->empty());
pkasting@chromium.org317dec72012-06-05 20:58:30588 DCHECK_EQ(KEYWORDS_RESULT, result.GetType());
levin@chromium.orge1ddda02010-08-26 19:43:48589
levin@chromium.orge1ddda02010-08-26 19:43:48590 WDKeywordsResult keyword_result = reinterpret_cast<
591 const WDResult<WDKeywordsResult>*>(&result)->GetValue();
592
avi8a64b715b2016-09-02 17:30:04593 for (auto& keyword : keyword_result.keywords) {
pkasting@chromium.orga2622302012-06-27 03:36:07594 // 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.
avi8a64b715b2016-09-02 17:30:04603 if (DeDupeEncodings(&keyword.input_encodings) && service)
604 service->UpdateKeyword(keyword);
Jinho Bangfa6b5752018-01-03 20:52:19605 template_urls->push_back(std::make_unique<TemplateURL>(keyword));
pkasting@chromium.orga2622302012-06-27 03:36:07606 }
levin@chromium.orge1ddda02010-08-26 19:43:48607
Nicolas Dossou-Gbeteaf259e72024-01-19 20:53:55608 out_updated_keywords_metadata = keyword_result.metadata;
Angela Yoeurngd8e54932022-04-27 23:53:16609 GetSearchProvidersUsingLoadedEngines(
Boris Sazonov2b77815a2024-01-17 15:35:04610 service, prefs, search_engine_choice_service, template_urls,
Nicolas Dossou-Gbeteaf259e72024-01-19 20:53:55611 default_search_provider, search_terms_data, out_updated_keywords_metadata,
612 removed_keyword_guids);
Nicolas Dossou-Gbetefec50c62023-10-12 22:29:23613
Nicolas Dossou-Gbeteaf259e72024-01-19 20:53:55614 // 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-Gbete354c10a52024-01-22 11:18:37618 out_updated_keywords_metadata.builtin_keyword_data_version >=
619 keyword_result.metadata.builtin_keyword_data_version);
vasilii@chromium.org4a40facd2013-05-29 14:44:56620}
621
622void GetSearchProvidersUsingLoadedEngines(
hashimoto@chromium.org37b324602014-07-02 07:30:49623 KeywordWebDataService* service,
hashimoto@chromium.org1f8841b2014-06-27 02:31:40624 PrefService* prefs,
Boris Sazonov2b77815a2024-01-17 15:35:04625 search_engines::SearchEngineChoiceService* search_engine_choice_service,
avi8a64b715b2016-09-02 17:30:04626 TemplateURLService::OwnedTemplateURLVector* template_urls,
erikwright@chromium.orga80ec962014-05-12 00:55:38627 TemplateURL* default_search_provider,
hashimoto@chromium.orgce7ee5f2014-06-16 23:41:19628 const SearchTermsData& search_terms_data,
Nicolas Dossou-Gbeteaf259e72024-01-19 20:53:55629 WDKeywordsResult::Metadata& in_out_keywords_metadata,
vasilii@chromium.org4a40facd2013-05-29 14:44:56630 std::set<std::string>* removed_keyword_guids) {
vasilii@chromium.org4a40facd2013-05-29 14:44:56631 DCHECK(template_urls);
avie8828f22016-09-02 18:07:58632 std::vector<std::unique_ptr<TemplateURLData>> prepopulated_urls =
Boris Sazonov2b77815a2024-01-17 15:35:04633 TemplateURLPrepopulateData::GetPrepopulatedEngines(
634 prefs, search_engine_choice_service, nullptr);
stevet@chromium.org01ef4a662012-05-29 15:58:25635 RemoveDuplicatePrepopulateIDs(service, prepopulated_urls,
erikwright@chromium.orga80ec962014-05-12 00:55:38636 default_search_provider, template_urls,
hashimoto@chromium.orgce7ee5f2014-06-16 23:41:19637 search_terms_data, removed_keyword_guids);
stevet@chromium.org01ef4a662012-05-29 15:58:25638
Nicolas Dossou-Gbeteaf259e72024-01-19 20:53:55639 MergeEngineRequirements merge_requirements = ComputeMergeEnginesRequirements(
640 prefs, search_engine_choice_service, in_out_keywords_metadata);
Nicolas Dossou-Gbetefec50c62023-10-12 22:29:23641
Nicolas Dossou-Gbeteaf259e72024-01-19 20:53:55642 if (merge_requirements.metadata.HasBuiltinKeywordData()) {
Pavel Yatsuka3e549822019-09-20 21:55:41643 MergeEnginesFromPrepopulateData(service, &prepopulated_urls, template_urls,
644 default_search_provider,
645 removed_keyword_guids);
levin@chromium.orge1ddda02010-08-26 19:43:48646 }
Angela Yoeurngd8e54932022-04-27 23:53:16647
Nicolas Dossou-Gbeteaf259e72024-01-19 20:53:55648 if (merge_requirements.metadata.HasStarterPackData()) {
649 bool overwrite_user_edits =
650 (in_out_keywords_metadata.starter_pack_version <
651 TemplateURLStarterPackData::GetFirstCompatibleDataVersion());
Angela Yoeurngd8e54932022-04-27 23:53:16652 MergeEnginesFromStarterPackData(
Angela Yoeurngd164d89d2022-07-27 01:25:43653 service, template_urls, default_search_provider, removed_keyword_guids,
Tommy C. Li7a3609a2022-12-02 23:11:35654 (overwrite_user_edits ? TemplateURLMergeOption::kOverwriteUserEdits
655 : TemplateURLMergeOption::kDefault));
Nicolas Dossou-Gbeteaf259e72024-01-19 20:53:55656 }
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 Yoeurngd8e54932022-04-27 23:53:16670 }
levin@chromium.orge1ddda02010-08-26 19:43:48671}
jeanluc@google.comb6fd1fa72010-10-05 23:49:31672
pkasting@chromium.orga2622302012-06-27 03:36:07673bool 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}
avi8a64b715b2016-09-02 17:30:04684
685TemplateURLService::OwnedTemplateURLVector::iterator FindTemplateURL(
686 TemplateURLService::OwnedTemplateURLVector* urls,
687 const TemplateURL* url) {
Peter Kasting03752ef2022-09-02 18:58:35688 return base::ranges::find(*urls, url, &std::unique_ptr<TemplateURL>::get);
avi8a64b715b2016-09-02 17:30:04689}