1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_SEARCH_ENGINES_UTIL_H_
6 #define COMPONENTS_SEARCH_ENGINES_UTIL_H_
8 // This file contains utility functions for search engine functionality.
13 #include "base/memory/scoped_ptr.h"
14 #include "base/strings/string16.h"
15 #include "components/search_engines/template_url_service.h"
20 class KeywordWebDataService
;
25 // Returns the short name of the default search engine, or the empty string if
27 base::string16
GetDefaultSearchEngineName(TemplateURLService
* service
);
29 // Returns a GURL that searches for |terms| using the default search engine of
31 GURL
GetDefaultSearchURLForSearchTerms(TemplateURLService
* service
,
32 const base::string16
& terms
);
34 // Returns matching URL from |template_urls| or NULL.
35 TemplateURL
* FindURLByPrepopulateID(
36 const TemplateURLService::TemplateURLVector
& template_urls
,
39 // Modifies |prepopulated_url| so that it contains user-modified fields from
40 // |original_turl|. Both URLs must have the same prepopulate_id.
41 void MergeIntoPrepopulatedEngineData(const TemplateURL
* original_turl
,
42 TemplateURLData
* prepopulated_url
);
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.
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
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
59 // The pair of current search engine and its new value.
60 typedef std::pair
<TemplateURL
*, TemplateURLData
> EditedSearchEngine
;
61 typedef std::vector
<EditedSearchEngine
> EditedEngines
;
63 struct ActionsFromPrepopulateData
{
64 ActionsFromPrepopulateData();
65 ~ActionsFromPrepopulateData();
67 TemplateURLService::TemplateURLVector removed_engines
;
68 EditedEngines edited_engines
;
69 std::vector
<TemplateURLData
> added_engines
;
72 // Given the user's current URLs and the current set of prepopulated URLs,
73 // produces the set of actions (see above) required to make the user's URLs
74 // reflect the prepopulate data. |default_search_provider| is used to avoid
75 // placing the current default provider on the "to be removed" list.
77 // NOTE: Takes ownership of, and clears, |prepopulated_urls|.
78 ActionsFromPrepopulateData
CreateActionsFromCurrentPrepopulateData(
79 ScopedVector
<TemplateURLData
>* prepopulated_urls
,
80 const TemplateURLService::TemplateURLVector
& existing_urls
,
81 const TemplateURL
* default_search_provider
);
83 // Processes the results of KeywordWebDataService::GetKeywords, combining it
84 // with prepopulated search providers to result in:
85 // * a set of template_urls (search providers). The caller owns the
86 // TemplateURL* returned in template_urls.
87 // * whether there is a new resource keyword version (and the value).
88 // |*new_resource_keyword_version| is set to 0 if no new value. Otherwise,
89 // it is the new value.
90 // Only pass in a non-NULL value for service if the KeywordWebDataService should
91 // be updated. If |removed_keyword_guids| is not NULL, any TemplateURLs removed
92 // from the keyword table in the KeywordWebDataService will have their Sync
93 // GUIDs added to it. |default_search_provider| will be used to prevent removing
94 // the current user-selected DSE, regardless of changes in prepopulate data.
95 void GetSearchProvidersUsingKeywordResult(
96 const WDTypedResult
& result
,
97 KeywordWebDataService
* service
,
99 TemplateURLService::TemplateURLVector
* template_urls
,
100 TemplateURL
* default_search_provider
,
101 const SearchTermsData
& search_terms_data
,
102 int* new_resource_keyword_version
,
103 std::set
<std::string
>* removed_keyword_guids
);
105 // Like GetSearchProvidersUsingKeywordResult(), but allows the caller to pass in
106 // engines in |template_urls| instead of getting them via processing a web data
108 // |resource_keyword_version| should contain the version number of the current
109 // keyword data, i.e. the version number of the most recent prepopulate data
110 // that has been merged into the current keyword data. On exit, this will be
111 // set as in GetSearchProvidersUsingKeywordResult().
112 void GetSearchProvidersUsingLoadedEngines(
113 KeywordWebDataService
* service
,
115 TemplateURLService::TemplateURLVector
* template_urls
,
116 TemplateURL
* default_search_provider
,
117 const SearchTermsData
& search_terms_data
,
118 int* resource_keyword_version
,
119 std::set
<std::string
>* removed_keyword_guids
);
121 // Due to a bug, the |input_encodings| field of TemplateURLData could have
122 // contained duplicate entries. This removes those entries and returns whether
124 bool DeDupeEncodings(std::vector
<std::string
>* encodings
);
126 // Removes (and deletes) TemplateURLs from |template_urls| and |service| if they
127 // have duplicate prepopulate ids. If |removed_keyword_guids| is not NULL, the
128 // Sync GUID of each item removed from the DB will be added to it. This is a
129 // helper used by GetSearchProvidersUsingKeywordResult(), but is declared here
130 // so it's accessible by unittests.
131 void RemoveDuplicatePrepopulateIDs(
132 KeywordWebDataService
* service
,
133 const ScopedVector
<TemplateURLData
>& prepopulated_urls
,
134 TemplateURL
* default_search_provider
,
135 TemplateURLService::TemplateURLVector
* template_urls
,
136 const SearchTermsData
& search_terms_data
,
137 std::set
<std::string
>* removed_keyword_guids
);
139 #endif // COMPONENTS_SEARCH_ENGINES_UTIL_H_