[email protected] | eaee4df | 2014-07-04 05:00:12 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 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 | |
[email protected] | bf5c532d | 2014-07-05 00:29:53 | [diff] [blame] | 5 | #ifndef COMPONENTS_SEARCH_ENGINES_UTIL_H_ |
| 6 | #define COMPONENTS_SEARCH_ENGINES_UTIL_H_ |
[email protected] | 96788b0 | 2010-06-26 21:45:34 | [diff] [blame] | 7 | |
| 8 | // This file contains utility functions for search engine functionality. |
dcheng | d967d950 | 2016-04-21 22:36:51 | [diff] [blame] | 9 | #include <memory> |
[email protected] | 2eff6b1 | 2012-05-16 20:07:05 | [diff] [blame] | 10 | #include <set> |
| 11 | #include <string> |
[email protected] | e1ddda0 | 2010-08-26 19:43:48 | [diff] [blame] | 12 | #include <vector> |
[email protected] | 96788b0 | 2010-06-26 21:45:34 | [diff] [blame] | 13 | |
[email protected] | 24a555b6 | 2013-06-10 22:01:17 | [diff] [blame] | 14 | #include "base/strings/string16.h" |
[email protected] | bf5c532d | 2014-07-05 00:29:53 | [diff] [blame] | 15 | #include "components/search_engines/template_url_service.h" |
[email protected] | 96788b0 | 2010-06-26 21:45:34 | [diff] [blame] | 16 | |
thestig | 7790a35 | 2015-08-22 00:05:11 | [diff] [blame] | 17 | template <typename T> |
| 18 | class ScopedVector; |
| 19 | |
[email protected] | 37b32460 | 2014-07-02 07:30:49 | [diff] [blame] | 20 | class KeywordWebDataService; |
[email protected] | 1f8841b | 2014-06-27 02:31:40 | [diff] [blame] | 21 | class PrefService; |
[email protected] | e1ddda0 | 2010-08-26 19:43:48 | [diff] [blame] | 22 | class TemplateURL; |
| 23 | class WDTypedResult; |
[email protected] | 96788b0 | 2010-06-26 21:45:34 | [diff] [blame] | 24 | |
| 25 | // Returns the short name of the default search engine, or the empty string if |
[email protected] | 1f8841b | 2014-06-27 02:31:40 | [diff] [blame] | 26 | // none is set. |
| 27 | base::string16 GetDefaultSearchEngineName(TemplateURLService* service); |
[email protected] | 96788b0 | 2010-06-26 21:45:34 | [diff] [blame] | 28 | |
[email protected] | bba9e63 | 2013-06-28 22:52:19 | [diff] [blame] | 29 | // Returns a GURL that searches for |terms| using the default search engine of |
[email protected] | 1f8841b | 2014-06-27 02:31:40 | [diff] [blame] | 30 | // |service|. |
| 31 | GURL GetDefaultSearchURLForSearchTerms(TemplateURLService* service, |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 32 | const base::string16& terms); |
[email protected] | bba9e63 | 2013-06-28 22:52:19 | [diff] [blame] | 33 | |
[email protected] | e937a970 | 2013-10-03 22:59:43 | [diff] [blame] | 34 | // Returns matching URL from |template_urls| or NULL. |
| 35 | TemplateURL* FindURLByPrepopulateID( |
| 36 | const TemplateURLService::TemplateURLVector& template_urls, |
| 37 | int prepopulate_id); |
| 38 | |
[email protected] | 10aeaf1 | 2013-02-05 07:41:46 | [diff] [blame] | 39 | // Modifies |prepopulated_url| so that it contains user-modified fields from |
| 40 | // |original_turl|. Both URLs must have the same prepopulate_id. |
[email protected] | c6dea6b | 2014-04-30 18:06:04 | [diff] [blame] | 41 | void MergeIntoPrepopulatedEngineData(const TemplateURL* original_turl, |
| 42 | TemplateURLData* prepopulated_url); |
[email protected] | 10aeaf1 | 2013-02-05 07:41:46 | [diff] [blame] | 43 | |
[email protected] | e937a970 | 2013-10-03 22:59:43 | [diff] [blame] | 44 | // 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. |
| 60 | typedef std::pair<TemplateURL*, TemplateURLData> EditedSearchEngine; |
| 61 | typedef std::vector<EditedSearchEngine> EditedEngines; |
| 62 | |
| 63 | struct ActionsFromPrepopulateData { |
| 64 | ActionsFromPrepopulateData(); |
vmpstr | b6449d51 | 2016-02-25 23:55:40 | [diff] [blame] | 65 | ActionsFromPrepopulateData(const ActionsFromPrepopulateData& other); |
[email protected] | e937a970 | 2013-10-03 22:59:43 | [diff] [blame] | 66 | ~ActionsFromPrepopulateData(); |
| 67 | |
| 68 | TemplateURLService::TemplateURLVector removed_engines; |
| 69 | EditedEngines edited_engines; |
[email protected] | c6dea6b | 2014-04-30 18:06:04 | [diff] [blame] | 70 | std::vector<TemplateURLData> added_engines; |
[email protected] | e937a970 | 2013-10-03 22:59:43 | [diff] [blame] | 71 | }; |
| 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|. |
| 79 | ActionsFromPrepopulateData CreateActionsFromCurrentPrepopulateData( |
[email protected] | c6dea6b | 2014-04-30 18:06:04 | [diff] [blame] | 80 | ScopedVector<TemplateURLData>* prepopulated_urls, |
avi | 8a64b715b | 2016-09-02 17:30:04 | [diff] [blame^] | 81 | const TemplateURLService::OwnedTemplateURLVector& existing_urls, |
[email protected] | e937a970 | 2013-10-03 22:59:43 | [diff] [blame] | 82 | const TemplateURL* default_search_provider); |
| 83 | |
[email protected] | 37b32460 | 2014-07-02 07:30:49 | [diff] [blame] | 84 | // Processes the results of KeywordWebDataService::GetKeywords, combining it |
| 85 | // with prepopulated search providers to result in: |
[email protected] | e1ddda0 | 2010-08-26 19:43:48 | [diff] [blame] | 86 | // * a set of template_urls (search providers). The caller owns the |
| 87 | // TemplateURL* returned in template_urls. |
[email protected] | e1ddda0 | 2010-08-26 19:43:48 | [diff] [blame] | 88 | // * 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] | 37b32460 | 2014-07-02 07:30:49 | [diff] [blame] | 91 | // 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] | e1ddda0 | 2010-08-26 19:43:48 | [diff] [blame] | 96 | void GetSearchProvidersUsingKeywordResult( |
| 97 | const WDTypedResult& result, |
[email protected] | 37b32460 | 2014-07-02 07:30:49 | [diff] [blame] | 98 | KeywordWebDataService* service, |
[email protected] | 1f8841b | 2014-06-27 02:31:40 | [diff] [blame] | 99 | PrefService* prefs, |
avi | 8a64b715b | 2016-09-02 17:30:04 | [diff] [blame^] | 100 | TemplateURLService::OwnedTemplateURLVector* template_urls, |
[email protected] | a80ec96 | 2014-05-12 00:55:38 | [diff] [blame] | 101 | TemplateURL* default_search_provider, |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 102 | const SearchTermsData& search_terms_data, |
[email protected] | 2eff6b1 | 2012-05-16 20:07:05 | [diff] [blame] | 103 | int* new_resource_keyword_version, |
| 104 | std::set<std::string>* removed_keyword_guids); |
[email protected] | e1ddda0 | 2010-08-26 19:43:48 | [diff] [blame] | 105 | |
[email protected] | 4a40facd | 2013-05-29 14:44:56 | [diff] [blame] | 106 | // 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(). |
| 113 | void GetSearchProvidersUsingLoadedEngines( |
[email protected] | 37b32460 | 2014-07-02 07:30:49 | [diff] [blame] | 114 | KeywordWebDataService* service, |
[email protected] | 1f8841b | 2014-06-27 02:31:40 | [diff] [blame] | 115 | PrefService* prefs, |
avi | 8a64b715b | 2016-09-02 17:30:04 | [diff] [blame^] | 116 | TemplateURLService::OwnedTemplateURLVector* template_urls, |
[email protected] | a80ec96 | 2014-05-12 00:55:38 | [diff] [blame] | 117 | TemplateURL* default_search_provider, |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 118 | const SearchTermsData& search_terms_data, |
[email protected] | 4a40facd | 2013-05-29 14:44:56 | [diff] [blame] | 119 | int* resource_keyword_version, |
| 120 | std::set<std::string>* removed_keyword_guids); |
| 121 | |
[email protected] | a262230 | 2012-06-27 03:36:07 | [diff] [blame] | 122 | // 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. |
| 125 | bool DeDupeEncodings(std::vector<std::string>* encodings); |
| 126 | |
[email protected] | 01ef4a66 | 2012-05-29 15:58:25 | [diff] [blame] | 127 | // 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. |
| 132 | void RemoveDuplicatePrepopulateIDs( |
[email protected] | 37b32460 | 2014-07-02 07:30:49 | [diff] [blame] | 133 | KeywordWebDataService* service, |
[email protected] | c6dea6b | 2014-04-30 18:06:04 | [diff] [blame] | 134 | const ScopedVector<TemplateURLData>& prepopulated_urls, |
[email protected] | 01ef4a66 | 2012-05-29 15:58:25 | [diff] [blame] | 135 | TemplateURL* default_search_provider, |
avi | 8a64b715b | 2016-09-02 17:30:04 | [diff] [blame^] | 136 | TemplateURLService::OwnedTemplateURLVector* template_urls, |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 137 | const SearchTermsData& search_terms_data, |
[email protected] | 01ef4a66 | 2012-05-29 15:58:25 | [diff] [blame] | 138 | std::set<std::string>* removed_keyword_guids); |
| 139 | |
avi | 8a64b715b | 2016-09-02 17:30:04 | [diff] [blame^] | 140 | TemplateURLService::OwnedTemplateURLVector::iterator FindTemplateURL( |
| 141 | TemplateURLService::OwnedTemplateURLVector* urls, |
| 142 | const TemplateURL* url); |
| 143 | |
[email protected] | bf5c532d | 2014-07-05 00:29:53 | [diff] [blame] | 144 | #endif // COMPONENTS_SEARCH_ENGINES_UTIL_H_ |