blob: 91a9c497b65e291a84c033c46a705c01e83ebfbc [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.
dchengd967d9502016-04-21 22:36:519#include <memory>
[email protected]2eff6b12012-05-16 20:07:0510#include <set>
11#include <string>
[email protected]e1ddda02010-08-26 19:43:4812#include <vector>
[email protected]96788b02010-06-26 21:45:3413
[email protected]24a555b62013-06-10 22:01:1714#include "base/strings/string16.h"
[email protected]bf5c532d2014-07-05 00:29:5315#include "components/search_engines/template_url_service.h"
[email protected]96788b02010-06-26 21:45:3416
thestig7790a352015-08-22 00:05:1117template <typename T>
18class ScopedVector;
19
[email protected]37b324602014-07-02 07:30:4920class KeywordWebDataService;
[email protected]1f8841b2014-06-27 02:31:4021class PrefService;
[email protected]e1ddda02010-08-26 19:43:4822class TemplateURL;
23class WDTypedResult;
[email protected]96788b02010-06-26 21:45:3424
25// Returns the short name of the default search engine, or the empty string if
[email protected]1f8841b2014-06-27 02:31:4026// none is set.
27base::string16 GetDefaultSearchEngineName(TemplateURLService* service);
[email protected]96788b02010-06-26 21:45:3428
[email protected]bba9e632013-06-28 22:52:1929// Returns a GURL that searches for |terms| using the default search engine of
[email protected]1f8841b2014-06-27 02:31:4030// |service|.
31GURL GetDefaultSearchURLForSearchTerms(TemplateURLService* service,
[email protected]0085863a2013-12-06 21:19:0332 const base::string16& terms);
[email protected]bba9e632013-06-28 22:52:1933
[email protected]e937a9702013-10-03 22:59:4334// Returns matching URL from |template_urls| or NULL.
35TemplateURL* FindURLByPrepopulateID(
36 const TemplateURLService::TemplateURLVector& template_urls,
37 int prepopulate_id);
38
[email protected]10aeaf12013-02-05 07:41:4639// Modifies |prepopulated_url| so that it contains user-modified fields from
40// |original_turl|. Both URLs must have the same prepopulate_id.
[email protected]c6dea6b2014-04-30 18:06:0441void MergeIntoPrepopulatedEngineData(const TemplateURL* original_turl,
42 TemplateURLData* prepopulated_url);
[email protected]10aeaf12013-02-05 07:41:4643
[email protected]e937a9702013-10-03 22:59:4344// CreateActionsFromCurrentPrepopulateData() (see below) takes in the current
45// prepopulated URLs as well as the user's current URLs, and returns an instance
46// of the following struct representing the changes necessary to bring the
47// user's URLs in line with the prepopulated URLs.
48//
49// There are three types of changes:
50// (1) Previous prepopulated engines that no longer exist in the current set of
51// prepopulated engines and thus should be removed from the user's current
52// URLs.
53// (2) Previous prepopulated engines whose data has changed. The existing
54// entries for these engines should be updated to reflect the new data,
55// except for any user-set names and keywords, which can be preserved.
56// (3) New prepopulated engines not in the user's engine list, which should be
57// added.
58
59// The pair of current search engine and its new value.
60typedef std::pair<TemplateURL*, TemplateURLData> EditedSearchEngine;
61typedef std::vector<EditedSearchEngine> EditedEngines;
62
63struct ActionsFromPrepopulateData {
64 ActionsFromPrepopulateData();
vmpstrb6449d512016-02-25 23:55:4065 ActionsFromPrepopulateData(const ActionsFromPrepopulateData& other);
[email protected]e937a9702013-10-03 22:59:4366 ~ActionsFromPrepopulateData();
67
68 TemplateURLService::TemplateURLVector removed_engines;
69 EditedEngines edited_engines;
[email protected]c6dea6b2014-04-30 18:06:0470 std::vector<TemplateURLData> added_engines;
[email protected]e937a9702013-10-03 22:59:4371};
72
73// Given the user's current URLs and the current set of prepopulated URLs,
74// produces the set of actions (see above) required to make the user's URLs
75// reflect the prepopulate data. |default_search_provider| is used to avoid
76// placing the current default provider on the "to be removed" list.
77//
78// NOTE: Takes ownership of, and clears, |prepopulated_urls|.
79ActionsFromPrepopulateData CreateActionsFromCurrentPrepopulateData(
[email protected]c6dea6b2014-04-30 18:06:0480 ScopedVector<TemplateURLData>* prepopulated_urls,
avi8a64b715b2016-09-02 17:30:0481 const TemplateURLService::OwnedTemplateURLVector& existing_urls,
[email protected]e937a9702013-10-03 22:59:4382 const TemplateURL* default_search_provider);
83
[email protected]37b324602014-07-02 07:30:4984// Processes the results of KeywordWebDataService::GetKeywords, combining it
85// with prepopulated search providers to result in:
[email protected]e1ddda02010-08-26 19:43:4886// * a set of template_urls (search providers). The caller owns the
87// TemplateURL* returned in template_urls.
[email protected]e1ddda02010-08-26 19:43:4888// * whether there is a new resource keyword version (and the value).
89// |*new_resource_keyword_version| is set to 0 if no new value. Otherwise,
90// it is the new value.
[email protected]37b324602014-07-02 07:30:4991// Only pass in a non-NULL value for service if the KeywordWebDataService should
92// be updated. If |removed_keyword_guids| is not NULL, any TemplateURLs removed
93// from the keyword table in the KeywordWebDataService will have their Sync
94// GUIDs added to it. |default_search_provider| will be used to prevent removing
95// the current user-selected DSE, regardless of changes in prepopulate data.
[email protected]e1ddda02010-08-26 19:43:4896void GetSearchProvidersUsingKeywordResult(
97 const WDTypedResult& result,
[email protected]37b324602014-07-02 07:30:4998 KeywordWebDataService* service,
[email protected]1f8841b2014-06-27 02:31:4099 PrefService* prefs,
avi8a64b715b2016-09-02 17:30:04100 TemplateURLService::OwnedTemplateURLVector* template_urls,
[email protected]a80ec962014-05-12 00:55:38101 TemplateURL* default_search_provider,
[email protected]ce7ee5f2014-06-16 23:41:19102 const SearchTermsData& search_terms_data,
[email protected]2eff6b12012-05-16 20:07:05103 int* new_resource_keyword_version,
104 std::set<std::string>* removed_keyword_guids);
[email protected]e1ddda02010-08-26 19:43:48105
[email protected]4a40facd2013-05-29 14:44:56106// Like GetSearchProvidersUsingKeywordResult(), but allows the caller to pass in
107// engines in |template_urls| instead of getting them via processing a web data
108// service request.
109// |resource_keyword_version| should contain the version number of the current
110// keyword data, i.e. the version number of the most recent prepopulate data
111// that has been merged into the current keyword data. On exit, this will be
112// set as in GetSearchProvidersUsingKeywordResult().
113void GetSearchProvidersUsingLoadedEngines(
[email protected]37b324602014-07-02 07:30:49114 KeywordWebDataService* service,
[email protected]1f8841b2014-06-27 02:31:40115 PrefService* prefs,
avi8a64b715b2016-09-02 17:30:04116 TemplateURLService::OwnedTemplateURLVector* template_urls,
[email protected]a80ec962014-05-12 00:55:38117 TemplateURL* default_search_provider,
[email protected]ce7ee5f2014-06-16 23:41:19118 const SearchTermsData& search_terms_data,
[email protected]4a40facd2013-05-29 14:44:56119 int* resource_keyword_version,
120 std::set<std::string>* removed_keyword_guids);
121
[email protected]a2622302012-06-27 03:36:07122// Due to a bug, the |input_encodings| field of TemplateURLData could have
123// contained duplicate entries. This removes those entries and returns whether
124// any were found.
125bool DeDupeEncodings(std::vector<std::string>* encodings);
126
[email protected]01ef4a662012-05-29 15:58:25127// Removes (and deletes) TemplateURLs from |template_urls| and |service| if they
128// have duplicate prepopulate ids. If |removed_keyword_guids| is not NULL, the
129// Sync GUID of each item removed from the DB will be added to it. This is a
130// helper used by GetSearchProvidersUsingKeywordResult(), but is declared here
131// so it's accessible by unittests.
132void RemoveDuplicatePrepopulateIDs(
[email protected]37b324602014-07-02 07:30:49133 KeywordWebDataService* service,
[email protected]c6dea6b2014-04-30 18:06:04134 const ScopedVector<TemplateURLData>& prepopulated_urls,
[email protected]01ef4a662012-05-29 15:58:25135 TemplateURL* default_search_provider,
avi8a64b715b2016-09-02 17:30:04136 TemplateURLService::OwnedTemplateURLVector* template_urls,
[email protected]ce7ee5f2014-06-16 23:41:19137 const SearchTermsData& search_terms_data,
[email protected]01ef4a662012-05-29 15:58:25138 std::set<std::string>* removed_keyword_guids);
139
avi8a64b715b2016-09-02 17:30:04140TemplateURLService::OwnedTemplateURLVector::iterator FindTemplateURL(
141 TemplateURLService::OwnedTemplateURLVector* urls,
142 const TemplateURL* url);
143
[email protected]bf5c532d2014-07-05 00:29:53144#endif // COMPONENTS_SEARCH_ENGINES_UTIL_H_