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