• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef COMPONENTS_SEARCH_ENGINES_UTIL_H_
6 #define COMPONENTS_SEARCH_ENGINES_UTIL_H_
7 
8 // This file contains utility functions for search engine functionality.
9 #include <set>
10 #include <string>
11 #include <vector>
12 
13 #include "base/memory/scoped_ptr.h"
14 #include "base/strings/string16.h"
15 #include "components/search_engines/template_url_service.h"
16 
17 class KeywordWebDataService;
18 class PrefService;
19 class TemplateURL;
20 class WDTypedResult;
21 
22 // Returns the short name of the default search engine, or the empty string if
23 // none is set.
24 base::string16 GetDefaultSearchEngineName(TemplateURLService* service);
25 
26 // Returns a GURL that searches for |terms| using the default search engine of
27 // |service|.
28 GURL GetDefaultSearchURLForSearchTerms(TemplateURLService* service,
29                                        const base::string16& terms);
30 
31 // Returns matching URL from |template_urls| or NULL.
32 TemplateURL* FindURLByPrepopulateID(
33     const TemplateURLService::TemplateURLVector& template_urls,
34     int prepopulate_id);
35 
36 // Modifies |prepopulated_url| so that it contains user-modified fields from
37 // |original_turl|. Both URLs must have the same prepopulate_id.
38 void MergeIntoPrepopulatedEngineData(const TemplateURL* original_turl,
39                                      TemplateURLData* prepopulated_url);
40 
41 // CreateActionsFromCurrentPrepopulateData() (see below) takes in the current
42 // prepopulated URLs as well as the user's current URLs, and returns an instance
43 // of the following struct representing the changes necessary to bring the
44 // user's URLs in line with the prepopulated URLs.
45 //
46 // There are three types of changes:
47 // (1) Previous prepopulated engines that no longer exist in the current set of
48 //     prepopulated engines and thus should be removed from the user's current
49 //     URLs.
50 // (2) Previous prepopulated engines whose data has changed.  The existing
51 //     entries for these engines should be updated to reflect the new data,
52 //     except for any user-set names and keywords, which can be preserved.
53 // (3) New prepopulated engines not in the user's engine list, which should be
54 //     added.
55 
56 // The pair of current search engine and its new value.
57 typedef std::pair<TemplateURL*, TemplateURLData> EditedSearchEngine;
58 typedef std::vector<EditedSearchEngine> EditedEngines;
59 
60 struct ActionsFromPrepopulateData {
61   ActionsFromPrepopulateData();
62   ~ActionsFromPrepopulateData();
63 
64   TemplateURLService::TemplateURLVector removed_engines;
65   EditedEngines edited_engines;
66   std::vector<TemplateURLData> added_engines;
67 };
68 
69 // Given the user's current URLs and the current set of prepopulated URLs,
70 // produces the set of actions (see above) required to make the user's URLs
71 // reflect the prepopulate data.  |default_search_provider| is used to avoid
72 // placing the current default provider on the "to be removed" list.
73 //
74 // NOTE: Takes ownership of, and clears, |prepopulated_urls|.
75 ActionsFromPrepopulateData CreateActionsFromCurrentPrepopulateData(
76     ScopedVector<TemplateURLData>* prepopulated_urls,
77     const TemplateURLService::TemplateURLVector& existing_urls,
78     const TemplateURL* default_search_provider);
79 
80 // Processes the results of KeywordWebDataService::GetKeywords, combining it
81 // with prepopulated search providers to result in:
82 //  * a set of template_urls (search providers). The caller owns the
83 //    TemplateURL* returned in template_urls.
84 //  * whether there is a new resource keyword version (and the value).
85 //    |*new_resource_keyword_version| is set to 0 if no new value. Otherwise,
86 //    it is the new value.
87 // Only pass in a non-NULL value for service if the KeywordWebDataService should
88 // be updated. If |removed_keyword_guids| is not NULL, any TemplateURLs removed
89 // from the keyword table in the KeywordWebDataService will have their Sync
90 // GUIDs added to it. |default_search_provider| will be used to prevent removing
91 // the current user-selected DSE, regardless of changes in prepopulate data.
92 void GetSearchProvidersUsingKeywordResult(
93     const WDTypedResult& result,
94     KeywordWebDataService* service,
95     PrefService* prefs,
96     TemplateURLService::TemplateURLVector* template_urls,
97     TemplateURL* default_search_provider,
98     const SearchTermsData& search_terms_data,
99     int* new_resource_keyword_version,
100     std::set<std::string>* removed_keyword_guids);
101 
102 // Like GetSearchProvidersUsingKeywordResult(), but allows the caller to pass in
103 // engines in |template_urls| instead of getting them via processing a web data
104 // service request.
105 // |resource_keyword_version| should contain the version number of the current
106 // keyword data, i.e. the version number of the most recent prepopulate data
107 // that has been merged into the current keyword data.  On exit, this will be
108 // set as in GetSearchProvidersUsingKeywordResult().
109 void GetSearchProvidersUsingLoadedEngines(
110     KeywordWebDataService* service,
111     PrefService* prefs,
112     TemplateURLService::TemplateURLVector* template_urls,
113     TemplateURL* default_search_provider,
114     const SearchTermsData& search_terms_data,
115     int* resource_keyword_version,
116     std::set<std::string>* removed_keyword_guids);
117 
118 // Due to a bug, the |input_encodings| field of TemplateURLData could have
119 // contained duplicate entries.  This removes those entries and returns whether
120 // any were found.
121 bool DeDupeEncodings(std::vector<std::string>* encodings);
122 
123 // Removes (and deletes) TemplateURLs from |template_urls| and |service| if they
124 // have duplicate prepopulate ids. If |removed_keyword_guids| is not NULL, the
125 // Sync GUID of each item removed from the DB will be added to it. This is a
126 // helper used by GetSearchProvidersUsingKeywordResult(), but is declared here
127 // so it's accessible by unittests.
128 void RemoveDuplicatePrepopulateIDs(
129     KeywordWebDataService* service,
130     const ScopedVector<TemplateURLData>& prepopulated_urls,
131     TemplateURL* default_search_provider,
132     TemplateURLService::TemplateURLVector* template_urls,
133     const SearchTermsData& search_terms_data,
134     std::set<std::string>* removed_keyword_guids);
135 
136 #endif  // COMPONENTS_SEARCH_ENGINES_UTIL_H_
137