blob: 39034e5b6fd997e51e959dffcc2984e5e9b9389c [file] [log] [blame]
[email protected]eaee4df2014-07-04 05:00:121// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]96788b02010-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
[email protected]bf5c532d2014-07-05 00:29:535#ifndef COMPONENTS_SEARCH_ENGINES_UTIL_H_
6#define COMPONENTS_SEARCH_ENGINES_UTIL_H_
[email protected]96788b02010-06-26 21:45:347
8// This file contains utility functions for search engine functionality.
avie8828f22016-09-02 18:07:589
dchengd967d9502016-04-21 22:36:5110#include <memory>
[email protected]2eff6b12012-05-16 20:07:0511#include <set>
12#include <string>
[email protected]e1ddda02010-08-26 19:43:4813#include <vector>
[email protected]96788b02010-06-26 21:45:3414
[email protected]24a555b62013-06-10 22:01:1715#include "base/strings/string16.h"
[email protected]bf5c532d2014-07-05 00:29:5316#include "components/search_engines/template_url_service.h"
[email protected]96788b02010-06-26 21:45:3417
[email protected]37b324602014-07-02 07:30:4918class KeywordWebDataService;
[email protected]1f8841b2014-06-27 02:31:4019class PrefService;
[email protected]e1ddda02010-08-26 19:43:4820class TemplateURL;
21class WDTypedResult;
[email protected]96788b02010-06-26 21:45:3422
23// Returns the short name of the default search engine, or the empty string if
[email protected]1f8841b2014-06-27 02:31:4024// none is set.
25base::string16 GetDefaultSearchEngineName(TemplateURLService* service);
[email protected]96788b02010-06-26 21:45:3426
[email protected]bba9e632013-06-28 22:52:1927// Returns a GURL that searches for |terms| using the default search engine of
[email protected]1f8841b2014-06-27 02:31:4028// |service|.
29GURL GetDefaultSearchURLForSearchTerms(TemplateURLService* service,
[email protected]0085863a2013-12-06 21:19:0330 const base::string16& terms);
[email protected]bba9e632013-06-28 22:52:1931
[email protected]e937a9702013-10-03 22:59:4332// Returns matching URL from |template_urls| or NULL.
33TemplateURL* FindURLByPrepopulateID(
34 const TemplateURLService::TemplateURLVector& template_urls,
35 int prepopulate_id);
36
[email protected]10aeaf12013-02-05 07:41:4637// Modifies |prepopulated_url| so that it contains user-modified fields from
38// |original_turl|. Both URLs must have the same prepopulate_id.
[email protected]c6dea6b2014-04-30 18:06:0439void MergeIntoPrepopulatedEngineData(const TemplateURL* original_turl,
40 TemplateURLData* prepopulated_url);
[email protected]10aeaf12013-02-05 07:41:4641
[email protected]e937a9702013-10-03 22:59:4342// CreateActionsFromCurrentPrepopulateData() (see below) takes in the current
43// prepopulated URLs as well as the user's current URLs, and returns an instance
44// of the following struct representing the changes necessary to bring the
45// user's URLs in line with the prepopulated URLs.
46//
47// There are three types of changes:
48// (1) Previous prepopulated engines that no longer exist in the current set of
49// prepopulated engines and thus should be removed from the user's current
50// URLs.
51// (2) Previous prepopulated engines whose data has changed. The existing
52// entries for these engines should be updated to reflect the new data,
53// except for any user-set names and keywords, which can be preserved.
54// (3) New prepopulated engines not in the user's engine list, which should be
55// added.
56
57// The pair of current search engine and its new value.
58typedef std::pair<TemplateURL*, TemplateURLData> EditedSearchEngine;
59typedef std::vector<EditedSearchEngine> EditedEngines;
60
61struct ActionsFromPrepopulateData {
62 ActionsFromPrepopulateData();
vmpstrb6449d512016-02-25 23:55:4063 ActionsFromPrepopulateData(const ActionsFromPrepopulateData& other);
[email protected]e937a9702013-10-03 22:59:4364 ~ActionsFromPrepopulateData();
65
66 TemplateURLService::TemplateURLVector removed_engines;
67 EditedEngines edited_engines;
[email protected]c6dea6b2014-04-30 18:06:0468 std::vector<TemplateURLData> added_engines;
[email protected]e937a9702013-10-03 22:59:4369};
70
71// Given the user's current URLs and the current set of prepopulated URLs,
72// produces the set of actions (see above) required to make the user's URLs
73// reflect the prepopulate data. |default_search_provider| is used to avoid
74// placing the current default provider on the "to be removed" list.
75//
76// NOTE: Takes ownership of, and clears, |prepopulated_urls|.
77ActionsFromPrepopulateData CreateActionsFromCurrentPrepopulateData(
avie8828f22016-09-02 18:07:5878 std::vector<std::unique_ptr<TemplateURLData>>* prepopulated_urls,
avi8a64b715b2016-09-02 17:30:0479 const TemplateURLService::OwnedTemplateURLVector& existing_urls,
[email protected]e937a9702013-10-03 22:59:4380 const TemplateURL* default_search_provider);
81
[email protected]37b324602014-07-02 07:30:4982// Processes the results of KeywordWebDataService::GetKeywords, combining it
83// with prepopulated search providers to result in:
[email protected]e1ddda02010-08-26 19:43:4884// * a set of template_urls (search providers). The caller owns the
85// TemplateURL* returned in template_urls.
[email protected]e1ddda02010-08-26 19:43:4886// * whether there is a new resource keyword version (and the value).
87// |*new_resource_keyword_version| is set to 0 if no new value. Otherwise,
88// it is the new value.
[email protected]37b324602014-07-02 07:30:4989// Only pass in a non-NULL value for service if the KeywordWebDataService should
90// be updated. If |removed_keyword_guids| is not NULL, any TemplateURLs removed
91// from the keyword table in the KeywordWebDataService will have their Sync
92// GUIDs added to it. |default_search_provider| will be used to prevent removing
93// the current user-selected DSE, regardless of changes in prepopulate data.
[email protected]e1ddda02010-08-26 19:43:4894void GetSearchProvidersUsingKeywordResult(
95 const WDTypedResult& result,
[email protected]37b324602014-07-02 07:30:4996 KeywordWebDataService* service,
[email protected]1f8841b2014-06-27 02:31:4097 PrefService* prefs,
avi8a64b715b2016-09-02 17:30:0498 TemplateURLService::OwnedTemplateURLVector* template_urls,
[email protected]a80ec962014-05-12 00:55:3899 TemplateURL* default_search_provider,
[email protected]ce7ee5f2014-06-16 23:41:19100 const SearchTermsData& search_terms_data,
[email protected]2eff6b12012-05-16 20:07:05101 int* new_resource_keyword_version,
102 std::set<std::string>* removed_keyword_guids);
[email protected]e1ddda02010-08-26 19:43:48103
[email protected]4a40facd2013-05-29 14:44:56104// Like GetSearchProvidersUsingKeywordResult(), but allows the caller to pass in
105// engines in |template_urls| instead of getting them via processing a web data
106// service request.
107// |resource_keyword_version| should contain the version number of the current
108// keyword data, i.e. the version number of the most recent prepopulate data
109// that has been merged into the current keyword data. On exit, this will be
110// set as in GetSearchProvidersUsingKeywordResult().
111void GetSearchProvidersUsingLoadedEngines(
[email protected]37b324602014-07-02 07:30:49112 KeywordWebDataService* service,
[email protected]1f8841b2014-06-27 02:31:40113 PrefService* prefs,
avi8a64b715b2016-09-02 17:30:04114 TemplateURLService::OwnedTemplateURLVector* template_urls,
[email protected]a80ec962014-05-12 00:55:38115 TemplateURL* default_search_provider,
[email protected]ce7ee5f2014-06-16 23:41:19116 const SearchTermsData& search_terms_data,
[email protected]4a40facd2013-05-29 14:44:56117 int* resource_keyword_version,
118 std::set<std::string>* removed_keyword_guids);
119
[email protected]a2622302012-06-27 03:36:07120// Due to a bug, the |input_encodings| field of TemplateURLData could have
121// contained duplicate entries. This removes those entries and returns whether
122// any were found.
123bool DeDupeEncodings(std::vector<std::string>* encodings);
124
[email protected]01ef4a662012-05-29 15:58:25125// Removes (and deletes) TemplateURLs from |template_urls| and |service| if they
126// have duplicate prepopulate ids. If |removed_keyword_guids| is not NULL, the
127// Sync GUID of each item removed from the DB will be added to it. This is a
128// helper used by GetSearchProvidersUsingKeywordResult(), but is declared here
129// so it's accessible by unittests.
130void RemoveDuplicatePrepopulateIDs(
[email protected]37b324602014-07-02 07:30:49131 KeywordWebDataService* service,
avie8828f22016-09-02 18:07:58132 const std::vector<std::unique_ptr<TemplateURLData>>& prepopulated_urls,
[email protected]01ef4a662012-05-29 15:58:25133 TemplateURL* default_search_provider,
avi8a64b715b2016-09-02 17:30:04134 TemplateURLService::OwnedTemplateURLVector* template_urls,
[email protected]ce7ee5f2014-06-16 23:41:19135 const SearchTermsData& search_terms_data,
[email protected]01ef4a662012-05-29 15:58:25136 std::set<std::string>* removed_keyword_guids);
137
avi8a64b715b2016-09-02 17:30:04138TemplateURLService::OwnedTemplateURLVector::iterator FindTemplateURL(
139 TemplateURLService::OwnedTemplateURLVector* urls,
140 const TemplateURL* url);
141
[email protected]bf5c532d2014-07-05 00:29:53142#endif // COMPONENTS_SEARCH_ENGINES_UTIL_H_