blob: 905ae6ec0dacbfa86b56d7eb22103618cfbdeff8 [file] [log] [blame]
[email protected]16fca9b82012-04-23 18:40:261// Copyright (c) 2012 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
5#ifndef CHROME_BROWSER_SEARCH_ENGINES_UTIL_H_
6#define CHROME_BROWSER_SEARCH_ENGINES_UTIL_H_
[email protected]96788b02010-06-26 21:45:347
8// This file contains utility functions for search engine functionality.
[email protected]2eff6b12012-05-16 20:07:059#include <set>
10#include <string>
[email protected]e1ddda02010-08-26 19:43:4811#include <vector>
[email protected]96788b02010-06-26 21:45:3412
[email protected]2ede21312011-12-22 13:31:2613#include "base/memory/scoped_ptr.h"
[email protected]24a555b62013-06-10 22:01:1714#include "base/strings/string16.h"
[email protected]01ef4a662012-05-29 15:58:2515#include "chrome/browser/search_engines/template_url_service.h"
[email protected]96788b02010-06-26 21:45:3416
17class Profile;
[email protected]e1ddda02010-08-26 19:43:4818class TemplateURL;
19class WDTypedResult;
20class WebDataService;
[email protected]96788b02010-06-26 21:45:3421
22// Returns the short name of the default search engine, or the empty string if
23// none is set. |profile| may be NULL.
[email protected]6a72a632013-12-12 22:22:0024base::string16 GetDefaultSearchEngineName(Profile* profile);
[email protected]96788b02010-06-26 21:45:3425
[email protected]bba9e632013-06-28 22:52:1926// Returns a GURL that searches for |terms| using the default search engine of
27// |profile|.
[email protected]0085863a2013-12-06 21:19:0328GURL GetDefaultSearchURLForSearchTerms(Profile* profile,
29 const base::string16& terms);
[email protected]bba9e632013-06-28 22:52:1930
[email protected]e937a9702013-10-03 22:59:4331// Returns matching URL from |template_urls| or NULL.
32TemplateURL* FindURLByPrepopulateID(
33 const TemplateURLService::TemplateURLVector& template_urls,
34 int prepopulate_id);
35
[email protected]10aeaf12013-02-05 07:41:4636// Modifies |prepopulated_url| so that it contains user-modified fields from
37// |original_turl|. Both URLs must have the same prepopulate_id.
[email protected]c6dea6b2014-04-30 18:06:0438void MergeIntoPrepopulatedEngineData(const TemplateURL* original_turl,
39 TemplateURLData* prepopulated_url);
[email protected]10aeaf12013-02-05 07:41:4640
[email protected]e937a9702013-10-03 22:59:4341// 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.
57typedef std::pair<TemplateURL*, TemplateURLData> EditedSearchEngine;
58typedef std::vector<EditedSearchEngine> EditedEngines;
59
60struct ActionsFromPrepopulateData {
61 ActionsFromPrepopulateData();
62 ~ActionsFromPrepopulateData();
63
64 TemplateURLService::TemplateURLVector removed_engines;
65 EditedEngines edited_engines;
[email protected]c6dea6b2014-04-30 18:06:0466 std::vector<TemplateURLData> added_engines;
[email protected]e937a9702013-10-03 22:59:4367};
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|.
75ActionsFromPrepopulateData CreateActionsFromCurrentPrepopulateData(
[email protected]c6dea6b2014-04-30 18:06:0476 ScopedVector<TemplateURLData>* prepopulated_urls,
[email protected]e937a9702013-10-03 22:59:4377 const TemplateURLService::TemplateURLVector& existing_urls,
78 const TemplateURL* default_search_provider);
79
[email protected]e1ddda02010-08-26 19:43:4880// Processes the results of WebDataService::GetKeywords, combining it with
81// prepopulated search providers to result in:
82// * a set of template_urls (search providers). The caller owns the
83// TemplateURL* returned in template_urls.
[email protected]e1ddda02010-08-26 19:43:4884// * 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 WebDataService should be
[email protected]2eff6b12012-05-16 20:07:0588// updated. If |removed_keyword_guids| is not NULL, any TemplateURLs removed
89// from the keyword table in the WebDataService will have their Sync GUIDs
[email protected]a80ec962014-05-12 00:55:3890// added to it. |default_search_provider| will be used to prevent removing the
91// current user-selected DSE, regardless of changes in prepopulate data.
[email protected]e1ddda02010-08-26 19:43:4892void GetSearchProvidersUsingKeywordResult(
93 const WDTypedResult& result,
94 WebDataService* service,
[email protected]16fca9b82012-04-23 18:40:2695 Profile* profile,
[email protected]01ef4a662012-05-29 15:58:2596 TemplateURLService::TemplateURLVector* template_urls,
[email protected]a80ec962014-05-12 00:55:3897 TemplateURL* default_search_provider,
[email protected]ce7ee5f2014-06-16 23:41:1998 const SearchTermsData& search_terms_data,
[email protected]2eff6b12012-05-16 20:07:0599 int* new_resource_keyword_version,
100 std::set<std::string>* removed_keyword_guids);
[email protected]e1ddda02010-08-26 19:43:48101
[email protected]4a40facd2013-05-29 14:44:56102// 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().
109void GetSearchProvidersUsingLoadedEngines(
110 WebDataService* service,
111 Profile* profile,
112 TemplateURLService::TemplateURLVector* template_urls,
[email protected]a80ec962014-05-12 00:55:38113 TemplateURL* default_search_provider,
[email protected]ce7ee5f2014-06-16 23:41:19114 const SearchTermsData& search_terms_data,
[email protected]4a40facd2013-05-29 14:44:56115 int* resource_keyword_version,
116 std::set<std::string>* removed_keyword_guids);
117
[email protected]a2622302012-06-27 03:36:07118// 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.
121bool DeDupeEncodings(std::vector<std::string>* encodings);
122
[email protected]01ef4a662012-05-29 15:58:25123// 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.
128void RemoveDuplicatePrepopulateIDs(
129 WebDataService* service,
[email protected]c6dea6b2014-04-30 18:06:04130 const ScopedVector<TemplateURLData>& prepopulated_urls,
[email protected]01ef4a662012-05-29 15:58:25131 TemplateURL* default_search_provider,
132 TemplateURLService::TemplateURLVector* template_urls,
[email protected]ce7ee5f2014-06-16 23:41:19133 const SearchTermsData& search_terms_data,
[email protected]01ef4a662012-05-29 15:58:25134 std::set<std::string>* removed_keyword_guids);
135
[email protected]96788b02010-06-26 21:45:34136#endif // CHROME_BROWSER_SEARCH_ENGINES_UTIL_H_