[email protected] | d550cb0 | 2014-06-25 06:48:11 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | d82443b | 2009-01-15 19:54:56 | [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] | d550cb0 | 2014-06-25 06:48:11 | [diff] [blame] | 5 | #ifndef COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_H_ |
| 6 | #define COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_H_ |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 7 | |
Denis Yaroshevskiy | 361a5932 | 2018-02-12 20:16:12 | [diff] [blame] | 8 | #include <cstddef> |
dcheng | d967d950 | 2016-04-21 22:36:51 | [diff] [blame] | 9 | #include <memory> |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 10 | #include <string> |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 11 | #include <utility> |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 12 | #include <vector> |
| 13 | |
[email protected] | a918f87 | 2010-06-01 14:30:51 | [diff] [blame] | 14 | #include "base/gtest_prod_util.h" |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 15 | #include "base/macros.h" |
[email protected] | cc86ccfe | 2013-06-28 00:10:50 | [diff] [blame] | 16 | #include "base/time/time.h" |
vitbar | da6a5018 | 2016-06-14 18:20:32 | [diff] [blame] | 17 | #include "components/search_engines/search_engine_type.h" |
[email protected] | fa817b1 | 2014-06-11 06:25:25 | [diff] [blame] | 18 | #include "components/search_engines/template_url_data.h" |
| 19 | #include "components/search_engines/template_url_id.h" |
Steven Holte | f9d5ed6 | 2017-10-21 02:02:30 | [diff] [blame] | 20 | #include "third_party/metrics_proto/omnibox_event.pb.h" |
| 21 | #include "third_party/metrics_proto/omnibox_input_type.pb.h" |
[email protected] | d550cb0 | 2014-06-25 06:48:11 | [diff] [blame] | 22 | #include "ui/gfx/geometry/size.h" |
[email protected] | 761fa470 | 2013-07-02 15:25:15 | [diff] [blame] | 23 | #include "url/gurl.h" |
tfarina | 018de6e | 2015-05-26 17:41:20 | [diff] [blame] | 24 | #include "url/third_party/mozilla/url_parse.h" |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 25 | |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 26 | class SearchTermsData; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 27 | class TemplateURL; |
| 28 | |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 29 | |
| 30 | // TemplateURLRef ------------------------------------------------------------- |
| 31 | |
| 32 | // A TemplateURLRef represents a single URL within the larger TemplateURL class |
| 33 | // (which represents an entire "search engine", see below). If |
| 34 | // SupportsReplacement() is true, this URL has placeholders in it, for which |
| 35 | // callers can substitute values to get a "real" URL using ReplaceSearchTerms(). |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 36 | // |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 37 | // TemplateURLRefs always have a non-NULL |owner_| TemplateURL, which they |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 38 | // access in order to get at important data like the underlying URL string or |
| 39 | // the associated Profile. |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 40 | class TemplateURLRef { |
| 41 | public: |
| 42 | // Magic numbers to pass to ReplaceSearchTerms() for the |accepted_suggestion| |
| 43 | // parameter. Most callers aren't using Suggest capabilities and should just |
| 44 | // pass NO_SUGGESTIONS_AVAILABLE. |
| 45 | // NOTE: Because positive values are meaningful, make sure these are negative! |
| 46 | enum AcceptedSuggestion { |
| 47 | NO_SUGGESTION_CHOSEN = -1, |
| 48 | NO_SUGGESTIONS_AVAILABLE = -2, |
| 49 | }; |
| 50 | |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 51 | // Which kind of URL within our owner we are. This allows us to get at the |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 52 | // correct string field. Use |INDEXED| to indicate that the numerical |
| 53 | // |index_in_owner_| should be used instead. |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 54 | enum Type { |
| 55 | SEARCH, |
| 56 | SUGGEST, |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 57 | IMAGE, |
[email protected] | 2767c0fd | 2013-08-16 17:44:16 | [diff] [blame] | 58 | NEW_TAB, |
[email protected] | 448b17f5 | 2014-06-13 01:08:03 | [diff] [blame] | 59 | CONTEXTUAL_SEARCH, |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 60 | INDEXED |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 61 | }; |
| 62 | |
[email protected] | 7c2bcc4 | 2013-08-01 04:03:48 | [diff] [blame] | 63 | // Type to store <content_type, post_data> pair for POST URLs. |
| 64 | // The |content_type|(first part of the pair) is the content-type of |
| 65 | // the |post_data|(second part of the pair) which is encoded in |
| 66 | // "multipart/form-data" format, it also contains the MIME boundary used in |
| 67 | // the |post_data|. See http://tools.ietf.org/html/rfc2046 for the details. |
| 68 | typedef std::pair<std::string, std::string> PostContent; |
| 69 | |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 70 | // This struct encapsulates arguments passed to |
| 71 | // TemplateURLRef::ReplaceSearchTerms methods. By default, only search_terms |
| 72 | // is required and is passed in the constructor. |
| 73 | struct SearchTermsArgs { |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 74 | explicit SearchTermsArgs(const base::string16& search_terms); |
vmpstr | b6449d51 | 2016-02-25 23:55:40 | [diff] [blame] | 75 | SearchTermsArgs(const SearchTermsArgs& other); |
[email protected] | 800569d | 2013-05-06 07:38:48 | [diff] [blame] | 76 | ~SearchTermsArgs(); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 77 | |
[email protected] | 448b17f5 | 2014-06-13 01:08:03 | [diff] [blame] | 78 | struct ContextualSearchParams { |
| 79 | ContextualSearchParams(); |
donnd | 0bc7329 | 2017-01-17 22:07:56 | [diff] [blame] | 80 | // Modern constructor, used when the content is sent in the HTTP header |
| 81 | // instead of as CGI parameters. |
| 82 | // The |home_country| is an ISO country code for the country that the user |
| 83 | // considers their permanent home (which may be different from the country |
| 84 | // they are currently visiting). Pass an empty string if none available. |
donnd | d9fc4d9 | 2016-09-16 00:30:49 | [diff] [blame] | 85 | ContextualSearchParams(int version, |
donnd | 0bc7329 | 2017-01-17 22:07:56 | [diff] [blame] | 86 | int contextual_cards_version, |
| 87 | const std::string& home_country); |
vmpstr | b6449d51 | 2016-02-25 23:55:40 | [diff] [blame] | 88 | ContextualSearchParams(const ContextualSearchParams& other); |
[email protected] | 448b17f5 | 2014-06-13 01:08:03 | [diff] [blame] | 89 | ~ContextualSearchParams(); |
| 90 | |
Denis Yaroshevskiy | 361a5932 | 2018-02-12 20:16:12 | [diff] [blame] | 91 | // Estimates dynamic memory usage. |
| 92 | // See base/trace_event/memory_usage_estimator.h for more info. |
| 93 | size_t EstimateMemoryUsage() const; |
| 94 | |
[email protected] | 448b17f5 | 2014-06-13 01:08:03 | [diff] [blame] | 95 | // The version of contextual search. |
| 96 | int version; |
| 97 | |
twellington | c0947da | 2016-09-26 16:09:44 | [diff] [blame] | 98 | // The version of Contextual Cards data to request. |
donnd | d9fc4d9 | 2016-09-16 00:30:49 | [diff] [blame] | 99 | // A value of 0 indicates no data needed. |
twellington | c0947da | 2016-09-26 16:09:44 | [diff] [blame] | 100 | int contextual_cards_version; |
donnd | 0bc7329 | 2017-01-17 22:07:56 | [diff] [blame] | 101 | |
| 102 | // The locale of the user's home country in an ISO country code format, |
| 103 | // or an empty string if not available. This indicates where the user |
| 104 | // resides, not where they currently are. |
| 105 | std::string home_country; |
[email protected] | 448b17f5 | 2014-06-13 01:08:03 | [diff] [blame] | 106 | }; |
| 107 | |
Denis Yaroshevskiy | 361a5932 | 2018-02-12 20:16:12 | [diff] [blame] | 108 | // Estimates dynamic memory usage. |
| 109 | // See base/trace_event/memory_usage_estimator.h for more info. |
| 110 | size_t EstimateMemoryUsage() const; |
| 111 | |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 112 | // The search terms (query). |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 113 | base::string16 search_terms; |
[email protected] | bba9e63 | 2013-06-28 22:52:19 | [diff] [blame] | 114 | |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 115 | // The original (input) query. |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 116 | base::string16 original_query; |
[email protected] | bba9e63 | 2013-06-28 22:52:19 | [diff] [blame] | 117 | |
[email protected] | 420472b2 | 2014-06-10 13:34:43 | [diff] [blame] | 118 | // The type the original input query was identified as. |
Steven Holte | 3696c941 | 2017-08-24 18:38:03 | [diff] [blame] | 119 | metrics::OmniboxInputType input_type; |
[email protected] | 420472b2 | 2014-06-10 13:34:43 | [diff] [blame] | 120 | |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 121 | // The optional assisted query stats, aka AQS, used for logging purposes. |
| 122 | // This string contains impressions of all autocomplete matches shown |
| 123 | // at the query submission time. For privacy reasons, we require the |
| 124 | // search provider to support HTTPS protocol in order to receive the AQS |
| 125 | // param. |
| 126 | // For more details, see http://goto.google.com/binary-clients-logging . |
| 127 | std::string assisted_query_stats; |
| 128 | |
| 129 | // TODO: Remove along with "aq" CGI param. |
| 130 | int accepted_suggestion; |
[email protected] | 0079056 | 2012-12-14 09:57:16 | [diff] [blame] | 131 | |
| 132 | // The 0-based position of the cursor within the query string at the time |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 133 | // the request was issued. Set to base::string16::npos if not used. |
[email protected] | 0079056 | 2012-12-14 09:57:16 | [diff] [blame] | 134 | size_t cursor_position; |
[email protected] | c55364c | 2013-03-06 00:24:43 | [diff] [blame] | 135 | |
[email protected] | 800569d | 2013-05-06 07:38:48 | [diff] [blame] | 136 | // The URL of the current webpage to be used for experimental zero-prefix |
| 137 | // suggestions. |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 138 | std::string current_page_url; |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 139 | |
[email protected] | d5015ca | 2013-08-08 22:04:18 | [diff] [blame] | 140 | // Which omnibox the user used to type the prefix. |
[email protected] | 332d17d2 | 2014-06-20 16:56:03 | [diff] [blame] | 141 | metrics::OmniboxEventProto::PageClassification page_classification; |
[email protected] | d5015ca | 2013-08-08 22:04:18 | [diff] [blame] | 142 | |
[email protected] | 2018424 | 2014-05-14 02:57:42 | [diff] [blame] | 143 | // Optional session token. |
| 144 | std::string session_token; |
| 145 | |
[email protected] | 9e9130ce | 2014-06-23 23:20:51 | [diff] [blame] | 146 | // Prefetch query and type. |
| 147 | std::string prefetch_query; |
| 148 | std::string prefetch_query_type; |
| 149 | |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 150 | // Additional query params provided by the suggest server. |
| 151 | std::string suggest_query_params; |
| 152 | |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 153 | // If set, ReplaceSearchTerms() will automatically append any extra query |
| 154 | // params specified via the --extra-search-query-params command-line |
Marc Treib | ec42aaaf | 2017-09-21 15:15:27 | [diff] [blame] | 155 | // argument. Generally, this should be set when dealing with the search |
| 156 | // TemplateURLRefs of the default search engine and the caller cares |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 157 | // about the query portion of the URL. Since neither TemplateURLRef nor |
| 158 | // indeed TemplateURL know whether a TemplateURL is the default search |
| 159 | // engine, callers instead must set this manually. |
| 160 | bool append_extra_query_params; |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 161 | |
| 162 | // The raw content of an image thumbnail that will be used as a query for |
| 163 | // search-by-image frontend. |
| 164 | std::string image_thumbnail_content; |
| 165 | |
| 166 | // When searching for an image, the URL of the original image. Callers |
| 167 | // should leave this empty for images specified via data: URLs. |
| 168 | GURL image_url; |
[email protected] | 2f3bc651 | 2013-08-28 03:56:27 | [diff] [blame] | 169 | |
| 170 | // When searching for an image, the original size of the image. |
| 171 | gfx::Size image_original_size; |
[email protected] | d5ebcf4 | 2013-10-30 00:48:18 | [diff] [blame] | 172 | |
[email protected] | c8ccc41d | 2014-04-10 04:42:12 | [diff] [blame] | 173 | // True if the search was made using the app list search box. Otherwise, the |
| 174 | // search was made using the omnibox. |
| 175 | bool from_app_list; |
[email protected] | 448b17f5 | 2014-06-13 01:08:03 | [diff] [blame] | 176 | |
| 177 | ContextualSearchParams contextual_search_params; |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 178 | }; |
| 179 | |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 180 | TemplateURLRef(const TemplateURL* owner, Type type); |
| 181 | TemplateURLRef(const TemplateURL* owner, size_t index_in_owner); |
[email protected] | dcd869c | 2010-08-30 20:15:25 | [diff] [blame] | 182 | ~TemplateURLRef(); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 183 | |
vitbar | 5bd8c25 | 2016-01-29 18:44:51 | [diff] [blame] | 184 | TemplateURLRef(const TemplateURLRef& source); |
| 185 | TemplateURLRef& operator=(const TemplateURLRef& source); |
| 186 | |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 187 | // Returns the raw URL. None of the parameters will have been replaced. |
| 188 | std::string GetURL() const; |
| 189 | |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 190 | // Returns the raw string of the post params. Please see comments in |
| 191 | // prepopulated_engines_schema.json for the format. |
| 192 | std::string GetPostParamsString() const; |
| 193 | |
| 194 | // Returns true if this URL supports search term replacement. |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 195 | bool SupportsReplacement(const SearchTermsData& search_terms_data) const; |
[email protected] | 923d733d1 | 2010-09-03 00:53:31 | [diff] [blame] | 196 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 197 | // Returns a string that is the result of replacing the search terms in |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 198 | // the url with the specified arguments. We use our owner's input encoding. |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 199 | // |
| 200 | // If this TemplateURLRef does not support replacement (SupportsReplacement |
| 201 | // returns false), an empty string is returned. |
[email protected] | 7c2bcc4 | 2013-08-01 04:03:48 | [diff] [blame] | 202 | // If this TemplateURLRef uses POST, and |post_content| is not NULL, the |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 203 | // |post_params_| will be replaced, encoded in "multipart/form-data" format |
[email protected] | 7c2bcc4 | 2013-08-01 04:03:48 | [diff] [blame] | 204 | // and stored into |post_content|. |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 205 | std::string ReplaceSearchTerms(const SearchTermsArgs& search_terms_args, |
| 206 | const SearchTermsData& search_terms_data, |
| 207 | PostContent* post_content) const; |
| 208 | |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 209 | // TODO(jnd): remove the following ReplaceSearchTerms definition which does |
[email protected] | 7c2bcc4 | 2013-08-01 04:03:48 | [diff] [blame] | 210 | // not have |post_content| parameter once all reference callers pass |
| 211 | // |post_content| parameter. |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 212 | std::string ReplaceSearchTerms( |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 213 | const SearchTermsArgs& search_terms_args, |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 214 | const SearchTermsData& search_terms_data) const { |
| 215 | return ReplaceSearchTerms(search_terms_args, search_terms_data, NULL); |
| 216 | } |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 217 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 218 | // Returns true if the TemplateURLRef is valid. An invalid TemplateURLRef is |
| 219 | // one that contains unknown terms, or invalid characters. |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 220 | bool IsValid(const SearchTermsData& search_terms_data) const; |
[email protected] | 923d733d1 | 2010-09-03 00:53:31 | [diff] [blame] | 221 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 222 | // Returns a string representation of this TemplateURLRef suitable for |
| 223 | // display. The display format is the same as the format used by Firefox. |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 224 | base::string16 DisplayURL(const SearchTermsData& search_terms_data) const; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 225 | |
| 226 | // Converts a string as returned by DisplayURL back into a string as |
| 227 | // understood by TemplateURLRef. |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 228 | static std::string DisplayURLToURLRef(const base::string16& display_url); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 229 | |
| 230 | // If this TemplateURLRef is valid and contains one search term, this returns |
[email protected] | 7c60f504 | 2013-02-14 03:39:32 | [diff] [blame] | 231 | // the host/path of the URL, otherwise this returns an empty string. |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 232 | const std::string& GetHost(const SearchTermsData& search_terms_data) const; |
| 233 | const std::string& GetPath(const SearchTermsData& search_terms_data) const; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 234 | |
alexmos | 294849f | 2015-03-14 00:55:06 | [diff] [blame] | 235 | // If this TemplateURLRef is valid and contains one search term |
| 236 | // in its query or ref, this returns the key of the search term, |
| 237 | // otherwise this returns an empty string. |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 238 | const std::string& GetSearchTermKey( |
| 239 | const SearchTermsData& search_terms_data) const; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 240 | |
alexmos | 294849f | 2015-03-14 00:55:06 | [diff] [blame] | 241 | // If this TemplateURLRef is valid and contains one search term, |
| 242 | // this returns the location of the search term, |
| 243 | // otherwise this returns url::Parsed::QUERY. |
| 244 | url::Parsed::ComponentType GetSearchTermKeyLocation( |
| 245 | const SearchTermsData& search_terms_data) const; |
| 246 | |
vitbar | f2a1156 | 2017-05-15 14:09:50 | [diff] [blame] | 247 | // If this TemplateURLRef is valid and contains one search term, |
| 248 | // this returns the fixed prefix before the search term, |
| 249 | // otherwise this returns an empty string. |
| 250 | const std::string& GetSearchTermValuePrefix( |
| 251 | const SearchTermsData& search_terms_data) const; |
| 252 | |
| 253 | // If this TemplateURLRef is valid and contains one search term, |
| 254 | // this returns the fixed suffix after the search term, |
| 255 | // otherwise this returns an empty string. |
| 256 | const std::string& GetSearchTermValueSuffix( |
| 257 | const SearchTermsData& search_terms_data) const; |
| 258 | |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 259 | // Converts the specified term in our owner's encoding to a base::string16. |
vitbar | f2a1156 | 2017-05-15 14:09:50 | [diff] [blame] | 260 | base::string16 SearchTermToString16(const base::StringPiece& term) const; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 261 | |
| 262 | // Returns true if this TemplateURLRef has a replacement term of |
| 263 | // {google:baseURL} or {google:baseSuggestURL}. |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 264 | bool HasGoogleBaseURLs(const SearchTermsData& search_terms_data) const; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 265 | |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 266 | // Use the pattern referred to by this TemplateURLRef to match the provided |
| 267 | // |url| and extract |search_terms| from it. Returns true if the pattern |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 268 | // matches, even if |search_terms| is empty. In this case |
| 269 | // |search_term_component|, if not NULL, indicates whether the search terms |
| 270 | // were found in the query or the ref parameters; and |search_terms_position|, |
| 271 | // if not NULL, contains the position of the search terms in the query or the |
| 272 | // ref parameters. Returns false and an empty |search_terms| if the pattern |
| 273 | // does not match. |
[email protected] | 1ff6ce4 | 2013-03-05 00:54:34 | [diff] [blame] | 274 | bool ExtractSearchTermsFromURL( |
| 275 | const GURL& url, |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 276 | base::string16* search_terms, |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 277 | const SearchTermsData& search_terms_data, |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 278 | url::Parsed::ComponentType* search_term_component, |
| 279 | url::Component* search_terms_position) const; |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 280 | |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 281 | // Whether the URL uses POST (as opposed to GET). |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 282 | bool UsesPOSTMethod(const SearchTermsData& search_terms_data) const; |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 283 | |
Denis Yaroshevskiy | 361a5932 | 2018-02-12 20:16:12 | [diff] [blame] | 284 | // Estimates dynamic memory usage. |
| 285 | // See base/trace_event/memory_usage_estimator.h for more info. |
| 286 | size_t EstimateMemoryUsage() const; |
| 287 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 288 | private: |
| 289 | friend class TemplateURL; |
vitbar | deb28539 | 2015-02-20 14:02:55 | [diff] [blame] | 290 | friend class TemplateURLTest; |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 291 | FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, SetPrepopulatedAndParse); |
[email protected] | a918f87 | 2010-06-01 14:30:51 | [diff] [blame] | 292 | FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseParameterKnown); |
| 293 | FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseParameterUnknown); |
| 294 | FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseURLEmpty); |
| 295 | FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseURLNoTemplateEnd); |
| 296 | FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseURLNoKnownParameters); |
| 297 | FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseURLTwoParameters); |
| 298 | FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseURLNestedParameter); |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 299 | FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, URLRefTestImageURLWithPOST); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 300 | |
| 301 | // Enumeration of the known types. |
| 302 | enum ReplacementType { |
| 303 | ENCODING, |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 304 | GOOGLE_ASSISTED_QUERY_STATS, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 305 | GOOGLE_BASE_URL, |
| 306 | GOOGLE_BASE_SUGGEST_URL, |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 307 | GOOGLE_CURRENT_PAGE_URL, |
[email protected] | 0079056 | 2012-12-14 09:57:16 | [diff] [blame] | 308 | GOOGLE_CURSOR_POSITION, |
[email protected] | 2f3bc651 | 2013-08-28 03:56:27 | [diff] [blame] | 309 | GOOGLE_IMAGE_ORIGINAL_HEIGHT, |
| 310 | GOOGLE_IMAGE_ORIGINAL_WIDTH, |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 311 | GOOGLE_IMAGE_SEARCH_SOURCE, |
| 312 | GOOGLE_IMAGE_THUMBNAIL, |
| 313 | GOOGLE_IMAGE_URL, |
[email protected] | 420472b2 | 2014-06-10 13:34:43 | [diff] [blame] | 314 | GOOGLE_INPUT_TYPE, |
blundell | a796b3c | 2016-01-22 15:15:26 | [diff] [blame] | 315 | GOOGLE_IOS_SEARCH_LANGUAGE, |
[email protected] | 7fe24d3 | 2013-03-19 21:27:20 | [diff] [blame] | 316 | GOOGLE_NTP_IS_THEMED, |
[email protected] | 448b17f5 | 2014-06-13 01:08:03 | [diff] [blame] | 317 | GOOGLE_CONTEXTUAL_SEARCH_VERSION, |
| 318 | GOOGLE_CONTEXTUAL_SEARCH_CONTEXT_DATA, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 319 | GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION, |
[email protected] | d5015ca | 2013-08-08 22:04:18 | [diff] [blame] | 320 | GOOGLE_PAGE_CLASSIFICATION, |
[email protected] | 9e9130ce | 2014-06-23 23:20:51 | [diff] [blame] | 321 | GOOGLE_PREFETCH_QUERY, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 322 | GOOGLE_RLZ, |
[email protected] | fd6d882 | 2012-12-08 06:56:11 | [diff] [blame] | 323 | GOOGLE_SEARCH_CLIENT, |
[email protected] | 6930c52 | 2011-07-28 22:15:19 | [diff] [blame] | 324 | GOOGLE_SEARCH_FIELDTRIAL_GROUP, |
[email protected] | 5c3c6e48 | 2014-06-23 09:47:18 | [diff] [blame] | 325 | GOOGLE_SEARCH_VERSION, |
[email protected] | 2018424 | 2014-05-14 02:57:42 | [diff] [blame] | 326 | GOOGLE_SESSION_TOKEN, |
[email protected] | 6b808af | 2013-06-25 04:31:55 | [diff] [blame] | 327 | GOOGLE_SUGGEST_CLIENT, |
[email protected] | b2cefdba | 2013-12-16 20:02:09 | [diff] [blame] | 328 | GOOGLE_SUGGEST_REQUEST_ID, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 329 | GOOGLE_UNESCAPED_SEARCH_TERMS, |
| 330 | LANGUAGE, |
Yusuf Ozuysal | 82ed511e | 2017-08-29 23:24:52 | [diff] [blame] | 331 | MAIL_RU_REFERRAL_ID, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 332 | SEARCH_TERMS, |
tedchoc | 2928c63 | 2017-05-11 18:31:28 | [diff] [blame] | 333 | YANDEX_REFERRAL_ID, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 334 | }; |
| 335 | |
| 336 | // Used to identify an element of the raw url that can be replaced. |
| 337 | struct Replacement { |
[email protected] | 4c66d2a | 2010-05-11 04:36:05 | [diff] [blame] | 338 | Replacement(ReplacementType type, size_t index) |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 339 | : type(type), index(index), is_post_param(false) {} |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 340 | ReplacementType type; |
[email protected] | 4c66d2a | 2010-05-11 04:36:05 | [diff] [blame] | 341 | size_t index; |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 342 | // Indicates the location in where the replacement is replaced. If |
| 343 | // |is_post_param| is false, |index| indicates the byte position in |
| 344 | // |parsed_url_|. Otherwise, |index| is the index of |post_params_|. |
| 345 | bool is_post_param; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 346 | }; |
| 347 | |
vitbar | deb28539 | 2015-02-20 14:02:55 | [diff] [blame] | 348 | // Stores a single parameter for a POST. |
| 349 | struct PostParam { |
| 350 | std::string name; |
| 351 | std::string value; |
| 352 | std::string content_type; |
Denis Yaroshevskiy | 361a5932 | 2018-02-12 20:16:12 | [diff] [blame] | 353 | |
| 354 | // Estimates dynamic memory usage. |
| 355 | // See base/trace_event/memory_usage_estimator.h for more info. |
| 356 | size_t EstimateMemoryUsage() const; |
vitbar | deb28539 | 2015-02-20 14:02:55 | [diff] [blame] | 357 | }; |
| 358 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 359 | // The list of elements to replace. |
| 360 | typedef std::vector<struct Replacement> Replacements; |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 361 | typedef std::vector<PostParam> PostParams; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 362 | |
| 363 | // TemplateURLRef internally caches values to make replacement quick. This |
| 364 | // method invalidates any cached values. |
| 365 | void InvalidateCachedValues() const; |
| 366 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 367 | // Parses the parameter in url at the specified offset. start/end specify the |
| 368 | // range of the parameter in the url, including the braces. If the parameter |
| 369 | // is valid, url is updated to reflect the appropriate parameter. If |
| 370 | // the parameter is one of the known parameters an element is added to |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 371 | // replacements indicating the type and range of the element. The original |
| 372 | // parameter is erased from the url. |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 373 | // |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 374 | // If the parameter is not a known parameter, false is returned. If this is a |
| 375 | // prepopulated URL, the parameter is erased, otherwise it is left alone. |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 376 | bool ParseParameter(size_t start, |
| 377 | size_t end, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 378 | std::string* url, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 379 | Replacements* replacements) const; |
| 380 | |
| 381 | // Parses the specified url, replacing parameters as necessary. If |
| 382 | // successful, valid is set to true, and the parsed url is returned. For all |
| 383 | // known parameters that are encountered an entry is added to replacements. |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 384 | // If there is an error parsing the url, valid is set to false, and an empty |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 385 | // string is returned. If the URL has the POST parameters, they will be |
| 386 | // parsed into |post_params| which will be further replaced with real search |
| 387 | // terms data and encoded in "multipart/form-data" format to generate the |
| 388 | // POST data. |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 389 | std::string ParseURL(const std::string& url, |
| 390 | Replacements* replacements, |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 391 | PostParams* post_params, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 392 | bool* valid) const; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 393 | |
| 394 | // If the url has not yet been parsed, ParseURL is invoked. |
| 395 | // NOTE: While this is const, it modifies parsed_, valid_, parsed_url_ and |
| 396 | // search_offset_. |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 397 | void ParseIfNecessary(const SearchTermsData& search_terms_data) const; |
[email protected] | 923d733d1 | 2010-09-03 00:53:31 | [diff] [blame] | 398 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 399 | // Extracts the query key and host from the url. |
[email protected] | 923d733d1 | 2010-09-03 00:53:31 | [diff] [blame] | 400 | void ParseHostAndSearchTermKey( |
| 401 | const SearchTermsData& search_terms_data) const; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 402 | |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 403 | // Encode post parameters in "multipart/form-data" format and store it |
[email protected] | 7c2bcc4 | 2013-08-01 04:03:48 | [diff] [blame] | 404 | // inside |post_content|. Returns false if errors are encountered during |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 405 | // encoding. This method is called each time ReplaceSearchTerms gets called. |
| 406 | bool EncodeFormData(const PostParams& post_params, |
[email protected] | 7c2bcc4 | 2013-08-01 04:03:48 | [diff] [blame] | 407 | PostContent* post_content) const; |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 408 | |
| 409 | // Handles a replacement by using real term data. If the replacement |
| 410 | // belongs to a PostParam, the PostParam will be replaced by the term data. |
| 411 | // Otherwise, the term data will be inserted at the place that the |
| 412 | // replacement points to. |
| 413 | void HandleReplacement(const std::string& name, |
| 414 | const std::string& value, |
| 415 | const Replacement& replacement, |
| 416 | std::string* url) const; |
| 417 | |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 418 | // Replaces all replacements in |parsed_url_| with their actual values and |
| 419 | // returns the result. This is the main functionality of |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 420 | // ReplaceSearchTerms(). |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 421 | std::string HandleReplacements( |
| 422 | const SearchTermsArgs& search_terms_args, |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 423 | const SearchTermsData& search_terms_data, |
[email protected] | 7c2bcc4 | 2013-08-01 04:03:48 | [diff] [blame] | 424 | PostContent* post_content) const; |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 425 | |
[email protected] | 9b74ab5 | 2012-03-30 16:08:07 | [diff] [blame] | 426 | // The TemplateURL that contains us. This should outlive us. |
vitbar | 5bd8c25 | 2016-01-29 18:44:51 | [diff] [blame] | 427 | const TemplateURL* owner_; |
[email protected] | 9b74ab5 | 2012-03-30 16:08:07 | [diff] [blame] | 428 | |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 429 | // What kind of URL we are. |
vitbar | 5bd8c25 | 2016-01-29 18:44:51 | [diff] [blame] | 430 | Type type_; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 431 | |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 432 | // If |type_| is |INDEXED|, this |index_in_owner_| is used instead to refer to |
| 433 | // a url within our owner. |
vitbar | 5bd8c25 | 2016-01-29 18:44:51 | [diff] [blame] | 434 | size_t index_in_owner_; |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 435 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 436 | // Whether the URL has been parsed. |
| 437 | mutable bool parsed_; |
| 438 | |
| 439 | // Whether the url was successfully parsed. |
| 440 | mutable bool valid_; |
| 441 | |
| 442 | // The parsed URL. All terms have been stripped out of this with |
| 443 | // replacements_ giving the index of the terms to replace. |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 444 | mutable std::string parsed_url_; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 445 | |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 446 | // Do we support search term replacement? |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 447 | mutable bool supports_replacements_; |
| 448 | |
| 449 | // The replaceable parts of url (parsed_url_). These are ordered by index |
| 450 | // into the string, and may be empty. |
| 451 | mutable Replacements replacements_; |
| 452 | |
mpearson | 93e29f7d | 2016-01-27 20:09:28 | [diff] [blame] | 453 | // Host, port, path, key and location of the search term. These are only set |
| 454 | // if the url contains one search term. |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 455 | mutable std::string host_; |
mpearson | 93e29f7d | 2016-01-27 20:09:28 | [diff] [blame] | 456 | mutable std::string port_; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 457 | mutable std::string path_; |
| 458 | mutable std::string search_term_key_; |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 459 | mutable url::Parsed::ComponentType search_term_key_location_; |
jbroman | 5083202 | 2016-04-21 23:53:00 | [diff] [blame] | 460 | mutable std::string search_term_value_prefix_; |
| 461 | mutable std::string search_term_value_suffix_; |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 462 | |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 463 | mutable PostParams post_params_; |
| 464 | |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 465 | // Whether the contained URL is a pre-populated URL. |
| 466 | bool prepopulated_; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 467 | }; |
| 468 | |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 469 | |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 470 | // TemplateURL ---------------------------------------------------------------- |
| 471 | |
| 472 | // A TemplateURL represents a single "search engine", defined primarily as a |
| 473 | // subset of the Open Search Description Document |
| 474 | // (http://www.opensearch.org/Specifications/OpenSearch) plus some extensions. |
| 475 | // One TemplateURL contains several TemplateURLRefs, which correspond to various |
| 476 | // different capabilities (e.g. doing searches or getting suggestions), as well |
| 477 | // as a TemplateURLData containing other details like the name, keyword, etc. |
| 478 | // |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 479 | // TemplateURLs are intended to be read-only for most users. |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 480 | // The TemplateURLService, which handles storing and manipulating TemplateURLs, |
| 481 | // is made a friend so that it can be the exception to this pattern. |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 482 | class TemplateURL { |
| 483 | public: |
[email protected] | bdcbcd8 | 2013-10-28 13:40:25 | [diff] [blame] | 484 | enum Type { |
| 485 | // Regular search engine. |
| 486 | NORMAL, |
| 487 | // Installed by extension through Override Settings API. |
| 488 | NORMAL_CONTROLLED_BY_EXTENSION, |
| 489 | // The keyword associated with an extension that uses the Omnibox API. |
| 490 | OMNIBOX_API_EXTENSION, |
ianwen | a27b1df | 2016-09-23 23:08:23 | [diff] [blame] | 491 | // Installed only on this device. Should not be synced. |
| 492 | LOCAL, |
[email protected] | bdcbcd8 | 2013-10-28 13:40:25 | [diff] [blame] | 493 | }; |
[email protected] | c73b2b5 | 2014-06-27 09:42:33 | [diff] [blame] | 494 | |
| 495 | // An AssociatedExtensionInfo represents information about the extension that |
| 496 | // added the search engine. |
| 497 | struct AssociatedExtensionInfo { |
a-v-y | c134e76 | 2017-04-18 15:31:31 | [diff] [blame] | 498 | AssociatedExtensionInfo(const std::string& extension_id, |
| 499 | base::Time install_time, |
| 500 | bool wants_to_be_default_engine); |
[email protected] | c73b2b5 | 2014-06-27 09:42:33 | [diff] [blame] | 501 | ~AssociatedExtensionInfo(); |
| 502 | |
Denis Yaroshevskiy | 361a5932 | 2018-02-12 20:16:12 | [diff] [blame] | 503 | // Estimates dynamic memory usage. |
| 504 | // See base/trace_event/memory_usage_estimator.h for more info. |
| 505 | size_t EstimateMemoryUsage() const; |
| 506 | |
[email protected] | c73b2b5 | 2014-06-27 09:42:33 | [diff] [blame] | 507 | std::string extension_id; |
| 508 | |
foolip | 8534d16 | 2017-01-10 16:51:03 | [diff] [blame] | 509 | // Used to resolve conflicts when there are multiple extensions specifying |
| 510 | // the default search engine. The most recently-installed wins. |
| 511 | base::Time install_time; |
a-v-y | c134e76 | 2017-04-18 15:31:31 | [diff] [blame] | 512 | |
| 513 | // Whether the search engine is supposed to be default. |
| 514 | bool wants_to_be_default_engine; |
[email protected] | c73b2b5 | 2014-06-27 09:42:33 | [diff] [blame] | 515 | }; |
| 516 | |
ianwen | a27b1df | 2016-09-23 23:08:23 | [diff] [blame] | 517 | explicit TemplateURL(const TemplateURLData& data, Type type = NORMAL); |
a-v-y | c134e76 | 2017-04-18 15:31:31 | [diff] [blame] | 518 | |
| 519 | // Constructor for extension controlled engine. |type| must be |
| 520 | // NORMAL_CONTROLLED_BY_EXTENSION or OMNIBOX_API_EXTENSION. |
| 521 | TemplateURL(const TemplateURLData& data, |
| 522 | Type type, |
| 523 | std::string extension_id, |
| 524 | base::Time install_time, |
| 525 | bool wants_to_be_default_engine); |
| 526 | |
[email protected] | dcd869c | 2010-08-30 20:15:25 | [diff] [blame] | 527 | ~TemplateURL(); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 528 | |
[email protected] | 44ccc9f | 2014-06-20 17:36:21 | [diff] [blame] | 529 | // Generates a suitable keyword for the specified url, which must be valid. |
| 530 | // This is guaranteed not to return an empty string, since TemplateURLs should |
| 531 | // never have an empty keyword. |
jshin | 1fb7646 | 2016-04-05 22:13:03 | [diff] [blame] | 532 | static base::string16 GenerateKeyword(const GURL& url); |
[email protected] | 44ccc9f | 2014-06-20 17:36:21 | [diff] [blame] | 533 | |
[email protected] | 9b74ab5 | 2012-03-30 16:08:07 | [diff] [blame] | 534 | // Generates a favicon URL from the specified url. |
| 535 | static GURL GenerateFaviconURL(const GURL& url); |
| 536 | |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 537 | // Returns true if |t_url| and |data| are equal in all meaningful respects. |
| 538 | // Static to allow either or both params to be NULL. |
| 539 | static bool MatchesData(const TemplateURL* t_url, |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 540 | const TemplateURLData* data, |
| 541 | const SearchTermsData& search_terms_data); |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 542 | |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 543 | const TemplateURLData& data() const { return data_; } |
| 544 | |
mpearson | 3c6d7af | 2015-05-13 23:59:53 | [diff] [blame] | 545 | const base::string16& short_name() const { return data_.short_name(); } |
[email protected] | 4973efc | 2009-06-05 23:16:29 | [diff] [blame] | 546 | // An accessor for the short_name, but adjusted so it can be appropriately |
| 547 | // displayed even if it is LTR and the UI is RTL. |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 548 | base::string16 AdjustedShortNameForLocaleDirection() const; |
[email protected] | bed9bd6c | 2009-04-21 17:27:47 | [diff] [blame] | 549 | |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 550 | const base::string16& keyword() const { return data_.keyword(); } |
[email protected] | c808593 | 2012-04-05 22:08:16 | [diff] [blame] | 551 | |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 552 | const std::string& url() const { return data_.url(); } |
| 553 | const std::string& suggestions_url() const { return data_.suggestions_url; } |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 554 | const std::string& image_url() const { return data_.image_url; } |
[email protected] | 2767c0fd | 2013-08-16 17:44:16 | [diff] [blame] | 555 | const std::string& new_tab_url() const { return data_.new_tab_url; } |
[email protected] | 448b17f5 | 2014-06-13 01:08:03 | [diff] [blame] | 556 | const std::string& contextual_search_url() const { |
| 557 | return data_.contextual_search_url; |
| 558 | } |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 559 | const std::string& search_url_post_params() const { |
| 560 | return data_.search_url_post_params; |
| 561 | } |
| 562 | const std::string& suggestions_url_post_params() const { |
| 563 | return data_.suggestions_url_post_params; |
| 564 | } |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 565 | const std::string& image_url_post_params() const { |
| 566 | return data_.image_url_post_params; |
| 567 | } |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 568 | const std::vector<std::string>& alternate_urls() const { |
| 569 | return data_.alternate_urls; |
| 570 | } |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 571 | const GURL& favicon_url() const { return data_.favicon_url; } |
[email protected] | 9e0ef858 | 2012-04-06 18:53:28 | [diff] [blame] | 572 | |
Bernhard Bauer | 41c477c1 | 2017-06-22 09:24:48 | [diff] [blame] | 573 | const GURL& logo_url() const { return data_.logo_url; } |
| 574 | |
Marc Treib | 41ed19d | 2017-08-22 09:17:13 | [diff] [blame] | 575 | const GURL& doodle_url() const { return data_.doodle_url; } |
| 576 | |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 577 | const GURL& originating_url() const { return data_.originating_url; } |
| 578 | |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 579 | bool safe_for_autoreplace() const { return data_.safe_for_autoreplace; } |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 580 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 581 | const std::vector<std::string>& input_encodings() const { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 582 | return data_.input_encodings; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 583 | } |
| 584 | |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 585 | TemplateURLID id() const { return data_.id; } |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 586 | |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 587 | base::Time date_created() const { return data_.date_created; } |
| 588 | base::Time last_modified() const { return data_.last_modified; } |
ltian | 8b5fc83 | 2016-12-02 23:43:35 | [diff] [blame] | 589 | base::Time last_visited() const { return data_.last_visited; } |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 590 | |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 591 | bool created_by_policy() const { return data_.created_by_policy; } |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 592 | |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 593 | int usage_count() const { return data_.usage_count; } |
| 594 | |
| 595 | int prepopulate_id() const { return data_.prepopulate_id; } |
| 596 | |
| 597 | const std::string& sync_guid() const { return data_.sync_guid; } |
[email protected] | 528539d | 2011-07-19 16:36:55 | [diff] [blame] | 598 | |
vitbar | 5bd8c25 | 2016-01-29 18:44:51 | [diff] [blame] | 599 | const std::vector<TemplateURLRef>& url_refs() const { return url_refs_; } |
Tommy C. Li | 6543780 | 2018-03-07 02:58:48 | [diff] [blame] | 600 | const TemplateURLRef& url_ref() const { |
| 601 | // Sanity check for https://crbug.com/781703. |
| 602 | CHECK(!url_refs_.empty()); |
| 603 | return url_refs_.back(); |
| 604 | } |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 605 | const TemplateURLRef& suggestions_url_ref() const { |
| 606 | return suggestions_url_ref_; |
| 607 | } |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 608 | const TemplateURLRef& image_url_ref() const { return image_url_ref_; } |
[email protected] | 2767c0fd | 2013-08-16 17:44:16 | [diff] [blame] | 609 | const TemplateURLRef& new_tab_url_ref() const { return new_tab_url_ref_; } |
[email protected] | 448b17f5 | 2014-06-13 01:08:03 | [diff] [blame] | 610 | const TemplateURLRef& contextual_search_url_ref() const { |
| 611 | return contextual_search_url_ref_; |
| 612 | } |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 613 | |
ianwen | a27b1df | 2016-09-23 23:08:23 | [diff] [blame] | 614 | Type type() const { return type_; } |
ianwen | a27b1df | 2016-09-23 23:08:23 | [diff] [blame] | 615 | |
a-v-y | 858416e | 2017-03-01 16:42:45 | [diff] [blame] | 616 | const AssociatedExtensionInfo* GetExtensionInfoForTesting() const { |
| 617 | return extension_info_.get(); |
[email protected] | e7f47c6 | 2014-07-24 13:23:19 | [diff] [blame] | 618 | } |
| 619 | |
[email protected] | 9b74ab5 | 2012-03-30 16:08:07 | [diff] [blame] | 620 | // Returns true if |url| supports replacement. |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 621 | bool SupportsReplacement(const SearchTermsData& search_terms_data) const; |
[email protected] | 9b74ab5 | 2012-03-30 16:08:07 | [diff] [blame] | 622 | |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 623 | // Returns true if any URLRefs use Googe base URLs. |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 624 | bool HasGoogleBaseURLs(const SearchTermsData& search_terms_data) const; |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 625 | |
[email protected] | 7b596f6 | 2012-05-08 01:34:25 | [diff] [blame] | 626 | // Returns true if this TemplateURL uses Google base URLs and has a keyword |
| 627 | // of "google.TLD". We use this to decide whether we can automatically |
| 628 | // update the keyword to reflect the current Google base URL TLD. |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 629 | bool IsGoogleSearchURLWithReplaceableKeyword( |
| 630 | const SearchTermsData& search_terms_data) const; |
[email protected] | 7b596f6 | 2012-05-08 01:34:25 | [diff] [blame] | 631 | |
| 632 | // Returns true if the keywords match or if |
[email protected] | c6dea6b | 2014-04-30 18:06:04 | [diff] [blame] | 633 | // IsGoogleSearchURLWithReplaceableKeyword() is true for both |this| and |
| 634 | // |other|. |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 635 | bool HasSameKeywordAs(const TemplateURLData& other, |
| 636 | const SearchTermsData& search_terms_data) const; |
[email protected] | 7b596f6 | 2012-05-08 01:34:25 | [diff] [blame] | 637 | |
[email protected] | bdcbcd8 | 2013-10-28 13:40:25 | [diff] [blame] | 638 | // Returns the id of the extension that added this search engine. Only call |
| 639 | // this for TemplateURLs of type NORMAL_CONTROLLED_BY_EXTENSION or |
| 640 | // OMNIBOX_API_EXTENSION. |
[email protected] | 9b74ab5 | 2012-03-30 16:08:07 | [diff] [blame] | 641 | std::string GetExtensionId() const; |
[email protected] | 9b74ab5 | 2012-03-30 16:08:07 | [diff] [blame] | 642 | |
vitbar | da6a5018 | 2016-06-14 18:20:32 | [diff] [blame] | 643 | // Returns the type of this search engine, or SEARCH_ENGINE_OTHER if no |
| 644 | // engines match. |
| 645 | SearchEngineType GetEngineType( |
| 646 | const SearchTermsData& search_terms_data) const; |
| 647 | |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 648 | // Use the alternate URLs and the search URL to match the provided |url| |
| 649 | // and extract |search_terms| from it. Returns false and an empty |
Peter Kasting | d0325408 | 2017-12-01 03:46:09 | [diff] [blame] | 650 | // |search_terms| if no search terms can be matched. The URLs are matched in |
| 651 | // the order listed in |url_refs_| (see comment there). |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 652 | bool ExtractSearchTermsFromURL(const GURL& url, |
| 653 | const SearchTermsData& search_terms_data, |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 654 | base::string16* search_terms) const; |
[email protected] | 1ff6ce4 | 2013-03-05 00:54:34 | [diff] [blame] | 655 | |
| 656 | // Returns true if non-empty search terms could be extracted from |url| using |
| 657 | // ExtractSearchTermsFromURL(). In other words, this returns whether |url| |
| 658 | // could be the result of performing a search with |this|. |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 659 | bool IsSearchURL(const GURL& url, |
| 660 | const SearchTermsData& search_terms_data) const; |
[email protected] | 1ff6ce4 | 2013-03-05 00:54:34 | [diff] [blame] | 661 | |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 662 | // Given a |url| corresponding to this TemplateURL, identifies the search |
| 663 | // terms and replaces them with the ones in |search_terms_args|, leaving the |
| 664 | // other parameters untouched. If the replacement fails, returns false and |
| 665 | // leaves |result| untouched. This is used by mobile ports to perform query |
| 666 | // refinement. |
| 667 | bool ReplaceSearchTermsInURL( |
| 668 | const GURL& url, |
| 669 | const TemplateURLRef::SearchTermsArgs& search_terms_args, |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 670 | const SearchTermsData& search_terms_data, |
jeffschiller | 8aa7a4e | 2017-04-23 02:22:10 | [diff] [blame] | 671 | GURL* result) const; |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 672 | |
| 673 | // Encodes the search terms from |search_terms_args| so that we know the |
| 674 | // |input_encoding|. Returns the |encoded_terms| and the |
| 675 | // |encoded_original_query|. |encoded_terms| may be escaped as path or query |
| 676 | // depending on |is_in_query|; |encoded_original_query| is always escaped as |
| 677 | // query. |
| 678 | void EncodeSearchTerms( |
| 679 | const TemplateURLRef::SearchTermsArgs& search_terms_args, |
| 680 | bool is_in_query, |
| 681 | std::string* input_encoding, |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 682 | base::string16* encoded_terms, |
| 683 | base::string16* encoded_original_query) const; |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 684 | |
[email protected] | 44ccc9f | 2014-06-20 17:36:21 | [diff] [blame] | 685 | // Returns the search url for this template URL. |
| 686 | // Returns an empty GURL if this template URL has no url(). |
| 687 | GURL GenerateSearchURL(const SearchTermsData& search_terms_data) const; |
| 688 | |
vitbar | 5bd8c25 | 2016-01-29 18:44:51 | [diff] [blame] | 689 | // TemplateURL internally caches values derived from a passed SearchTermsData |
| 690 | // to make its functions quick. This method invalidates any cached values and |
| 691 | // it should be called after SearchTermsData has been changed. |
| 692 | void InvalidateCachedValues() const; |
| 693 | |
Denis Yaroshevskiy | 361a5932 | 2018-02-12 20:16:12 | [diff] [blame] | 694 | // Estimates dynamic memory usage. |
| 695 | // See base/trace_event/memory_usage_estimator.h for more info. |
| 696 | size_t EstimateMemoryUsage() const; |
| 697 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 698 | private: |
[email protected] | 8e5c89a | 2011-06-07 18:13:33 | [diff] [blame] | 699 | friend class TemplateURLService; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 700 | |
[email protected] | 6b56478 | 2012-05-08 21:52:42 | [diff] [blame] | 701 | void CopyFrom(const TemplateURL& other); |
| 702 | |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 703 | void SetURL(const std::string& url); |
| 704 | void SetPrepopulateId(int id); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 705 | |
[email protected] | 7b596f6 | 2012-05-08 01:34:25 | [diff] [blame] | 706 | // Resets the keyword if IsGoogleSearchURLWithReplaceableKeyword() or |force|. |
| 707 | // The |force| parameter is useful when the existing keyword is known to be |
| 708 | // a placeholder. The resulting keyword is generated using |
[email protected] | 44ccc9f | 2014-06-20 17:36:21 | [diff] [blame] | 709 | // GenerateSearchURL() and GenerateKeyword(). |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 710 | void ResetKeywordIfNecessary(const SearchTermsData& search_terms_data, |
| 711 | bool force); |
[email protected] | 7b596f6 | 2012-05-08 01:34:25 | [diff] [blame] | 712 | |
Peter Kasting | d0325408 | 2017-12-01 03:46:09 | [diff] [blame] | 713 | // Resizes the |url_refs_| vector, which always holds the search URL as the |
| 714 | // last item. |
vitbar | 5bd8c25 | 2016-01-29 18:44:51 | [diff] [blame] | 715 | void ResizeURLRefVector(); |
| 716 | |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 717 | // Uses the alternate URLs and the search URL to match the provided |url| |
| 718 | // and extract |search_terms| from it as well as the |search_terms_component| |
| 719 | // (either REF or QUERY) and |search_terms_component| at which the |
| 720 | // |search_terms| are found in |url|. See also ExtractSearchTermsFromURL(). |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 721 | bool FindSearchTermsInURL(const GURL& url, |
| 722 | const SearchTermsData& search_terms_data, |
| 723 | base::string16* search_terms, |
| 724 | url::Parsed::ComponentType* search_terms_component, |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 725 | url::Component* search_terms_position) const; |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 726 | |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 727 | TemplateURLData data_; |
vitbar | 5bd8c25 | 2016-01-29 18:44:51 | [diff] [blame] | 728 | |
| 729 | // Contains TemplateURLRefs corresponding to the alternate URLs and the search |
Peter Kasting | d0325408 | 2017-12-01 03:46:09 | [diff] [blame] | 730 | // URL, in priority order: the URL at index 0 is treated as the highest |
| 731 | // priority and the primary search URL is treated as the lowest priority. For |
| 732 | // example, if a TemplateURL has alternate URL "http://foo/#q={searchTerms}" |
| 733 | // and search URL "http://foo/?q={searchTerms}", and the URL to be decoded is |
| 734 | // "http://foo/?q=a#q=b", the alternate URL will match first and the decoded |
| 735 | // search term will be "b". Note that since every TemplateURLRef has a |
| 736 | // primary search URL, this vector is never empty. |
vitbar | 5bd8c25 | 2016-01-29 18:44:51 | [diff] [blame] | 737 | std::vector<TemplateURLRef> url_refs_; |
| 738 | |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 739 | TemplateURLRef suggestions_url_ref_; |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 740 | TemplateURLRef image_url_ref_; |
[email protected] | 2767c0fd | 2013-08-16 17:44:16 | [diff] [blame] | 741 | TemplateURLRef new_tab_url_ref_; |
[email protected] | 448b17f5 | 2014-06-13 01:08:03 | [diff] [blame] | 742 | TemplateURLRef contextual_search_url_ref_; |
dcheng | d967d950 | 2016-04-21 22:36:51 | [diff] [blame] | 743 | std::unique_ptr<AssociatedExtensionInfo> extension_info_; |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 744 | |
vasilii | 2099119 | 2016-10-06 09:37:50 | [diff] [blame] | 745 | const Type type_; |
ianwen | a27b1df | 2016-09-23 23:08:23 | [diff] [blame] | 746 | |
vitbar | da6a5018 | 2016-06-14 18:20:32 | [diff] [blame] | 747 | // Caches the computed engine type across successive calls to GetEngineType(). |
| 748 | mutable SearchEngineType engine_type_; |
| 749 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 750 | // TODO(sky): Add date last parsed OSD file. |
[email protected] | 6b56478 | 2012-05-08 21:52:42 | [diff] [blame] | 751 | |
| 752 | DISALLOW_COPY_AND_ASSIGN(TemplateURL); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 753 | }; |
| 754 | |
[email protected] | d550cb0 | 2014-06-25 06:48:11 | [diff] [blame] | 755 | #endif // COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_H_ |