1 // Copyright (c) 2012 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 CHROME_BROWSER_SEARCH_ENGINES_UTIL_H_
6 #define CHROME_BROWSER_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 "chrome/browser/search_engines/template_url_service.h"
22 // Returns the short name of the default search engine, or the empty string if
23 // none is set. |profile| may be NULL.
24 string16 GetDefaultSearchEngineName(Profile* profile);
26 // Returns a GURL that searches for |terms| using the default search engine of
28 GURL GetDefaultSearchURLForSearchTerms(Profile* profile, const string16& terms);
30 // Returns matching URL from |template_urls| or NULL.
31 TemplateURL* FindURLByPrepopulateID(
32 const TemplateURLService::TemplateURLVector& template_urls,
35 // Modifies |prepopulated_url| so that it contains user-modified fields from
36 // |original_turl|. Both URLs must have the same prepopulate_id.
37 void MergeIntoPrepopulatedEngineData(TemplateURLData* prepopulated_url,
38 const TemplateURL* original_turl);
40 // CreateActionsFromCurrentPrepopulateData() (see below) takes in the current
41 // prepopulated URLs as well as the user's current URLs, and returns an instance
42 // of the following struct representing the changes necessary to bring the
43 // user's URLs in line with the prepopulated URLs.
45 // There are three types of changes:
46 // (1) Previous prepopulated engines that no longer exist in the current set of
47 // prepopulated engines and thus should be removed from the user's current
49 // (2) Previous prepopulated engines whose data has changed. The existing
50 // entries for these engines should be updated to reflect the new data,
51 // except for any user-set names and keywords, which can be preserved.
52 // (3) New prepopulated engines not in the user's engine list, which should be
55 // The pair of current search engine and its new value.
56 typedef std::pair<TemplateURL*, TemplateURLData> EditedSearchEngine;
57 typedef std::vector<EditedSearchEngine> EditedEngines;
59 struct ActionsFromPrepopulateData {
60 ActionsFromPrepopulateData();
61 ~ActionsFromPrepopulateData();
63 TemplateURLService::TemplateURLVector removed_engines;
64 EditedEngines edited_engines;
65 TemplateURLService::TemplateURLVector added_engines;
68 // Given the user's current URLs and the current set of prepopulated URLs,
69 // produces the set of actions (see above) required to make the user's URLs
70 // reflect the prepopulate data. |default_search_provider| is used to avoid
71 // placing the current default provider on the "to be removed" list.
73 // NOTE: Takes ownership of, and clears, |prepopulated_urls|.
74 ActionsFromPrepopulateData CreateActionsFromCurrentPrepopulateData(
75 ScopedVector<TemplateURL>* prepopulated_urls,
76 const TemplateURLService::TemplateURLVector& existing_urls,
77 const TemplateURL* default_search_provider);
79 // Processes the results of WebDataService::GetKeywords, combining it with
80 // prepopulated search providers to result in:
81 // * a set of template_urls (search providers). The caller owns the
82 // TemplateURL* returned in template_urls.
83 // * the default search provider (and if *default_search_provider is not NULL,
84 // it is contained in template_urls).
85 // * whether there is a new resource keyword version (and the value).
86 // |*new_resource_keyword_version| is set to 0 if no new value. Otherwise,
87 // it is the new value.
88 // Only pass in a non-NULL value for service if the WebDataService should be
89 // updated. If |removed_keyword_guids| is not NULL, any TemplateURLs removed
90 // from the keyword table in the WebDataService will have their Sync GUIDs
92 void GetSearchProvidersUsingKeywordResult(
93 const WDTypedResult& result,
94 WebDataService* service,
96 TemplateURLService::TemplateURLVector* template_urls,
97 TemplateURL** default_search_provider,
98 int* new_resource_keyword_version,
99 std::set<std::string>* removed_keyword_guids);
101 // Like GetSearchProvidersUsingKeywordResult(), but allows the caller to pass in
102 // engines in |template_urls| instead of getting them via processing a web data
104 // |resource_keyword_version| should contain the version number of the current
105 // keyword data, i.e. the version number of the most recent prepopulate data
106 // that has been merged into the current keyword data. On exit, this will be
107 // set as in GetSearchProvidersUsingKeywordResult().
108 void GetSearchProvidersUsingLoadedEngines(
109 WebDataService* service,
111 TemplateURLService::TemplateURLVector* template_urls,
112 TemplateURL** default_search_provider,
113 int* resource_keyword_version,
114 std::set<std::string>* removed_keyword_guids);
116 // Due to a bug, the |input_encodings| field of TemplateURLData could have
117 // contained duplicate entries. This removes those entries and returns whether
119 bool DeDupeEncodings(std::vector<std::string>* encodings);
121 // Removes (and deletes) TemplateURLs from |template_urls| and |service| if they
122 // have duplicate prepopulate ids. If |removed_keyword_guids| is not NULL, the
123 // Sync GUID of each item removed from the DB will be added to it. This is a
124 // helper used by GetSearchProvidersUsingKeywordResult(), but is declared here
125 // so it's accessible by unittests.
126 void RemoveDuplicatePrepopulateIDs(
127 WebDataService* service,
128 const ScopedVector<TemplateURL>& prepopulated_urls,
129 TemplateURL* default_search_provider,
130 TemplateURLService::TemplateURLVector* template_urls,
131 std::set<std::string>* removed_keyword_guids);
133 #endif // CHROME_BROWSER_SEARCH_ENGINES_UTIL_H_