15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
97dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include <utility>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/gtest_prod_util.h"
131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
14eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h"
15ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "chrome/browser/autocomplete/autocomplete_input.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/search_engines/template_url_id.h"
17424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "ui/gfx/size.h"
18eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "url/gurl.h"
19eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "url/url_parse.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Profile;
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class SearchTermsData;
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TemplateURL;
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TemplateURLRef -------------------------------------------------------------
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A TemplateURLRef represents a single URL within the larger TemplateURL class
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (which represents an entire "search engine", see below).  If
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// SupportsReplacement() is true, this URL has placeholders in it, for which
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// callers can substitute values to get a "real" URL using ReplaceSearchTerms().
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TemplateURLRefs always have a non-NULL |owner_| TemplateURL, which they
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// access in order to get at important data like the underlying URL string or
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the associated Profile.
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TemplateURLRef {
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Magic numbers to pass to ReplaceSearchTerms() for the |accepted_suggestion|
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // parameter.  Most callers aren't using Suggest capabilities and should just
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // pass NO_SUGGESTIONS_AVAILABLE.
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NOTE: Because positive values are meaningful, make sure these are negative!
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum AcceptedSuggestion {
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NO_SUGGESTION_CHOSEN = -1,
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NO_SUGGESTIONS_AVAILABLE = -2,
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Which kind of URL within our owner we are.  This allows us to get at the
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // correct string field. Use |INDEXED| to indicate that the numerical
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |index_in_owner_| should be used instead.
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum Type {
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SEARCH,
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SUGGEST,
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    INSTANT,
547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    IMAGE,
553551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    NEW_TAB,
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    INDEXED
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
59a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Type to store <content_type, post_data> pair for POST URLs.
60a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // The |content_type|(first part of the pair) is the content-type of
61a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // the |post_data|(second part of the pair) which is encoded in
62a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // "multipart/form-data" format, it also contains the MIME boundary used in
63a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // the |post_data|. See http://tools.ietf.org/html/rfc2046 for the details.
64a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  typedef std::pair<std::string, std::string> PostContent;
65a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This struct encapsulates arguments passed to
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TemplateURLRef::ReplaceSearchTerms methods.  By default, only search_terms
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is required and is passed in the constructor.
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  struct SearchTermsArgs {
70a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    explicit SearchTermsArgs(const base::string16& search_terms);
71c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    ~SearchTermsArgs();
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The search terms (query).
74a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    base::string16 search_terms;
75eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The original (input) query.
77a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    base::string16 original_query;
78eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The optional assisted query stats, aka AQS, used for logging purposes.
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // This string contains impressions of all autocomplete matches shown
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // at the query submission time.  For privacy reasons, we require the
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // search provider to support HTTPS protocol in order to receive the AQS
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // param.
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // For more details, see http://goto.google.com/binary-clients-logging .
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string assisted_query_stats;
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // TODO: Remove along with "aq" CGI param.
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int accepted_suggestion;
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // The 0-based position of the cursor within the query string at the time
91a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    // the request was issued.  Set to base::string16::npos if not used.
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    size_t cursor_position;
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // The start-edge margin of the omnibox in pixels, used in extended Instant
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // to align the preview contents with the omnibox.
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    int omnibox_start_margin;
97c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
98c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // The URL of the current webpage to be used for experimental zero-prefix
99c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // suggestions.
100f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    std::string current_page_url;
101eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
102ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    // Which omnibox the user used to type the prefix.
103ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    AutocompleteInput::PageClassification page_classification;
104ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
1054e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // True for searches issued with the bookmark bar pref set to shown.
1064e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    bool bookmark_bar_pinned;
1074e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1081e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // Additional query params provided by the suggest server.
1091e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    std::string suggest_query_params;
1101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
111eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // If set, ReplaceSearchTerms() will automatically append any extra query
112eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // params specified via the --extra-search-query-params command-line
113eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // argument.  Generally, this should be set when dealing with the search or
114eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // instant TemplateURLRefs of the default search engine and the caller cares
115eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // about the query portion of the URL.  Since neither TemplateURLRef nor
116eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // indeed TemplateURL know whether a TemplateURL is the default search
117eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // engine, callers instead must set this manually.
118eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    bool append_extra_query_params;
1197dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // The raw content of an image thumbnail that will be used as a query for
1217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // search-by-image frontend.
1227dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    std::string image_thumbnail_content;
1237dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1247dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // When searching for an image, the URL of the original image. Callers
1257dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // should leave this empty for images specified via data: URLs.
1267dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    GURL image_url;
127424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
128424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // When searching for an image, the original size of the image.
129424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    gfx::Size image_original_size;
1301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // If set, ReplaceSearchTerms() will append a param to the TemplateURLRef to
1321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // update the search results page incrementally even if that is otherwise
1331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // disabled by google.com preferences. See comments on
1341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // SearchTermsData::ForceInstantResultsParam().
1351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    bool force_instant_results;
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURLRef(TemplateURL* owner, Type type);
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURLRef(TemplateURL* owner, size_t index_in_owner);
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~TemplateURLRef();
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the raw URL. None of the parameters will have been replaced.
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string GetURL() const;
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1457dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Returns the raw string of the post params. Please see comments in
1467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // prepopulated_engines_schema.json for the format.
1477dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  std::string GetPostParamsString() const;
1487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1497dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Returns true if this URL supports search term replacement.
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool SupportsReplacement() const;
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Like SupportsReplacement but usable on threads other than the UI thread.
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool SupportsReplacementUsingTermsData(
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const SearchTermsData& search_terms_data) const;
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a string that is the result of replacing the search terms in
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the url with the specified arguments.  We use our owner's input encoding.
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If this TemplateURLRef does not support replacement (SupportsReplacement
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // returns false), an empty string is returned.
161a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // If this TemplateURLRef uses POST, and |post_content| is not NULL, the
1627dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // |post_params_| will be replaced, encoded in "multipart/form-data" format
163a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // and stored into |post_content|.
1647dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  std::string ReplaceSearchTerms(
1657dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      const SearchTermsArgs& search_terms_args,
166a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      PostContent* post_content) const;
1677dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // TODO(jnd): remove the following ReplaceSearchTerms definition which does
168a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // not have |post_content| parameter once all reference callers pass
169a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // |post_content| parameter.
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string ReplaceSearchTerms(
1717dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      const SearchTermsArgs& search_terms_args) const {
1727dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return ReplaceSearchTerms(search_terms_args, NULL);
1737dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Just like ReplaceSearchTerms except that it takes SearchTermsData to supply
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the data for some search terms. Most of the time ReplaceSearchTerms should
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // be called.
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string ReplaceSearchTermsUsingTermsData(
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const SearchTermsArgs& search_terms_args,
1807dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      const SearchTermsData& search_terms_data,
181a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      PostContent* post_content) const;
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the TemplateURLRef is valid. An invalid TemplateURLRef is
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // one that contains unknown terms, or invalid characters.
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsValid() const;
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Like IsValid but usable on threads other than the UI thread.
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsValidUsingTermsData(const SearchTermsData& search_terms_data) const;
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a string representation of this TemplateURLRef suitable for
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // display. The display format is the same as the format used by Firefox.
192a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 DisplayURL() const;
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Converts a string as returned by DisplayURL back into a string as
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // understood by TemplateURLRef.
196a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  static std::string DisplayURLToURLRef(const base::string16& display_url);
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If this TemplateURLRef is valid and contains one search term, this returns
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the host/path of the URL, otherwise this returns an empty string.
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& GetHost() const;
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& GetPath() const;
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If this TemplateURLRef is valid and contains one search term, this returns
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the key of the search term, otherwise this returns an empty string.
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& GetSearchTermKey() const;
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
207a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Converts the specified term in our owner's encoding to a base::string16.
208a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 SearchTermToString16(const std::string& term) const;
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if this TemplateURLRef has a replacement term of
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // {google:baseURL} or {google:baseSuggestURL}.
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool HasGoogleBaseURLs() const;
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Use the pattern referred to by this TemplateURLRef to match the provided
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |url| and extract |search_terms| from it. Returns true if the pattern
2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // matches, even if |search_terms| is empty. In this case
2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |search_term_component|, if not NULL, indicates whether the search terms
2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // were found in the query or the ref parameters; and |search_terms_position|,
2192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // if not NULL, contains the position of the search terms in the query or the
2202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // ref parameters. Returns false and an empty |search_terms| if the pattern
2212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // does not match.
2222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool ExtractSearchTermsFromURL(
2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const GURL& url,
224a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      base::string16* search_terms,
2252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const SearchTermsData& search_terms_data,
2262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      url_parse::Parsed::ComponentType* search_term_component,
2272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      url_parse::Component* search_terms_position) const;
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2297dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Whether the URL uses POST (as opposed to GET).
2307dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  bool UsesPOSTMethodUsingTermsData(
2317dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      const SearchTermsData* search_terms_data) const;
2327dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  bool UsesPOSTMethod() const {
2337dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return UsesPOSTMethodUsingTermsData(NULL);
2347dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
2357dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class TemplateURL;
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, SetPrepopulatedAndParse);
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseParameterKnown);
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseParameterUnknown);
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseURLEmpty);
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseURLNoTemplateEnd);
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseURLNoKnownParameters);
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseURLTwoParameters);
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseURLNestedParameter);
2467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, URLRefTestImageURLWithPOST);
2474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ReflectsBookmarkBarPinned);
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Enumeration of the known types.
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum ReplacementType {
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ENCODING,
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GOOGLE_ASSISTED_QUERY_STATS,
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GOOGLE_BASE_URL,
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GOOGLE_BASE_SUGGEST_URL,
2554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    GOOGLE_BOOKMARK_BAR_PINNED,
256f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    GOOGLE_CURRENT_PAGE_URL,
2572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    GOOGLE_CURSOR_POSITION,
258424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    GOOGLE_IMAGE_ORIGINAL_HEIGHT,
259424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    GOOGLE_IMAGE_ORIGINAL_WIDTH,
2607dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    GOOGLE_IMAGE_SEARCH_SOURCE,
2617dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    GOOGLE_IMAGE_THUMBNAIL,
2627dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    GOOGLE_IMAGE_URL,
2631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    GOOGLE_FORCE_INSTANT_RESULTS,
2642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    GOOGLE_INSTANT_EXTENDED_ENABLED,
2652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    GOOGLE_NTP_IS_THEMED,
2662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    GOOGLE_OMNIBOX_START_MARGIN,
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION,
268ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    GOOGLE_PAGE_CLASSIFICATION,
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GOOGLE_RLZ,
2702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    GOOGLE_SEARCH_CLIENT,
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GOOGLE_SEARCH_FIELDTRIAL_GROUP,
272eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    GOOGLE_SUGGEST_CLIENT,
273d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)    GOOGLE_SUGGEST_REQUEST_ID,
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GOOGLE_UNESCAPED_SEARCH_TERMS,
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LANGUAGE,
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SEARCH_TERMS,
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Used to identify an element of the raw url that can be replaced.
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  struct Replacement {
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Replacement(ReplacementType type, size_t index)
2827dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        : type(type), index(index), is_post_param(false) {}
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ReplacementType type;
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    size_t index;
2857dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // Indicates the location in where the replacement is replaced. If
2867dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // |is_post_param| is false, |index| indicates the byte position in
2877dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // |parsed_url_|. Otherwise, |index| is the index of |post_params_|.
2887dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    bool is_post_param;
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The list of elements to replace.
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef std::vector<struct Replacement> Replacements;
2937dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Type to store <key, value> pairs for POST URLs.
2947dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  typedef std::pair<std::string, std::string> PostParam;
2957dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  typedef std::vector<PostParam> PostParams;
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TemplateURLRef internally caches values to make replacement quick. This
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // method invalidates any cached values.
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void InvalidateCachedValues() const;
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Parses the parameter in url at the specified offset. start/end specify the
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // range of the parameter in the url, including the braces. If the parameter
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is valid, url is updated to reflect the appropriate parameter. If
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the parameter is one of the known parameters an element is added to
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // replacements indicating the type and range of the element. The original
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // parameter is erased from the url.
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the parameter is not a known parameter, false is returned. If this is a
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // prepopulated URL, the parameter is erased, otherwise it is left alone.
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool ParseParameter(size_t start,
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      size_t end,
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      std::string* url,
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      Replacements* replacements) const;
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Parses the specified url, replacing parameters as necessary. If
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // successful, valid is set to true, and the parsed url is returned. For all
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // known parameters that are encountered an entry is added to replacements.
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If there is an error parsing the url, valid is set to false, and an empty
3197dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // string is returned.  If the URL has the POST parameters, they will be
3207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // parsed into |post_params| which will be further replaced with real search
3217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // terms data and encoded in "multipart/form-data" format to generate the
3227dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // POST data.
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string ParseURL(const std::string& url,
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       Replacements* replacements,
3257dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                       PostParams* post_params,
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       bool* valid) const;
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the url has not yet been parsed, ParseURL is invoked.
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NOTE: While this is const, it modifies parsed_, valid_, parsed_url_ and
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // search_offset_.
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ParseIfNecessary() const;
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Like ParseIfNecessary but usable on threads other than the UI thread.
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ParseIfNecessaryUsingTermsData(
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const SearchTermsData& search_terms_data) const;
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Extracts the query key and host from the url.
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ParseHostAndSearchTermKey(
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const SearchTermsData& search_terms_data) const;
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3417dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Encode post parameters in "multipart/form-data" format and store it
342a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // inside |post_content|. Returns false if errors are encountered during
3437dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // encoding. This method is called each time ReplaceSearchTerms gets called.
3447dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  bool EncodeFormData(const PostParams& post_params,
345a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                      PostContent* post_content) const;
3467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
3477dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Handles a replacement by using real term data. If the replacement
3487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // belongs to a PostParam, the PostParam will be replaced by the term data.
3497dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Otherwise, the term data will be inserted at the place that the
3507dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // replacement points to.
3517dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  void HandleReplacement(const std::string& name,
3527dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                         const std::string& value,
3537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                         const Replacement& replacement,
3547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                         std::string* url) const;
3557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
356eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Replaces all replacements in |parsed_url_| with their actual values and
357eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // returns the result.  This is the main functionality of
358eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // ReplaceSearchTermsUsingTermsData().
359eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  std::string HandleReplacements(
360eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      const SearchTermsArgs& search_terms_args,
3617dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      const SearchTermsData& search_terms_data,
362a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      PostContent* post_content) const;
363eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The TemplateURL that contains us.  This should outlive us.
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURL* const owner_;
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // What kind of URL we are.
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const Type type_;
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If |type_| is |INDEXED|, this |index_in_owner_| is used instead to refer to
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // a url within our owner.
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const size_t index_in_owner_;
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Whether the URL has been parsed.
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable bool parsed_;
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Whether the url was successfully parsed.
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable bool valid_;
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The parsed URL. All terms have been stripped out of this with
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // replacements_ giving the index of the terms to replace.
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable std::string parsed_url_;
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3847dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Do we support search term replacement?
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable bool supports_replacements_;
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The replaceable parts of url (parsed_url_). These are ordered by index
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // into the string, and may be empty.
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable Replacements replacements_;
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Host, path, key and location of the search term. These are only set if the
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // url contains one search term.
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable std::string host_;
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable std::string path_;
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable std::string search_term_key_;
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable url_parse::Parsed::ComponentType search_term_key_location_;
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3987dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  mutable PostParams post_params_;
3997dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Whether the contained URL is a pre-populated URL.
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool prepopulated_;
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Whether search terms are shown in the omnibox on search results pages.
4044e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // This is kept as a member so it can be overridden by tests.
4054e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  bool showing_search_terms_;
4064e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(TemplateURLRef);
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TemplateURLData ------------------------------------------------------------
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The data for the TemplateURL.  Separating this into its own class allows most
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// users to do SSA-style usage of TemplateURL: construct a TemplateURLData with
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// whatever fields are desired, then create an immutable TemplateURL from it.
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct TemplateURLData {
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURLData();
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~TemplateURLData();
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A short description of the template. This is the name we show to the user
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // in various places that use TemplateURLs. For example, the location bar
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // shows this when the user selects a substituting match.
423a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 short_name;
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The shortcut for this TemplateURL.  |keyword| must be non-empty.
426a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  void SetKeyword(const base::string16& keyword);
427a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  const base::string16& keyword() const { return keyword_; }
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The raw URL for the TemplateURL, which may not be valid as-is (e.g. because
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // it requires substitutions first).  This must be non-empty.
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetURL(const std::string& url);
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& url() const { return url_; }
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Optional additional raw URLs.
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string suggestions_url;
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string instant_url;
4377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  std::string image_url;
4383551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  std::string new_tab_url;
4397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
4407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // The following post_params are comma-separated lists used to specify the
4417dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // post parameters for the corresponding URL.
4427dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  std::string search_url_post_params;
4437dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  std::string suggestions_url_post_params;
4447dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  std::string instant_url_post_params;
4457dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  std::string image_url_post_params;
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Optional favicon for the TemplateURL.
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GURL favicon_url;
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // URL to the OSD file this came from. May be empty.
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GURL originating_url;
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Whether this TemplateURL is shown in the default list of search providers.
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This is just a property and does not indicate whether the TemplateURL has a
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TemplateURLRef that supports replacement. Use
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TemplateURL::ShowInDefaultList() to test both.
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool show_in_default_list;
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Whether it's safe for auto-modification code (the autogenerator and the
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // code that imports data from other browsers) to replace the TemplateURL.
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This should be set to false for any TemplateURL the user edits, or any
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TemplateURL that the user clearly manually edited in the past, like a
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // bookmark keyword from another browser.
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool safe_for_autoreplace;
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The list of supported encodings for the search terms. This may be empty,
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // which indicates the terms should be encoded with UTF-8.
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<std::string> input_encodings;
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Unique identifier of this TemplateURL. The unique ID is set by the
4715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TemplateURLService when the TemplateURL is added to it.
4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURLID id;
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Date this TemplateURL was created.
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NOTE: this may be 0, which indicates the TemplateURL was created before we
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // started tracking creation time.
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::Time date_created;
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The last time this TemplateURL was modified by a user, since creation.
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NOTE: Like date_created above, this may be 0.
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::Time last_modified;
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // True if this TemplateURL was automatically created by the administrator via
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // group policy.
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool created_by_policy;
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Number of times this TemplateURL has been explicitly used to load a URL.
4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We don't increment this for uses as the "default search engine" since
4915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // that's not really "explicit" usage and incrementing would result in pinning
4925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the user's default search engine(s) to the top of the list of searches on
4935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the New Tab page, de-emphasizing the omnibox as "where you go to search".
4945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int usage_count;
4955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If this TemplateURL comes from prepopulated data the prepopulate_id is > 0.
4975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int prepopulate_id;
4985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The primary unique identifier for Sync. This set on all TemplateURLs
5005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // regardless of whether they have been associated with Sync.
5015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string sync_guid;
5025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A list of URL patterns that can be used, in addition to |url_|, to extract
5045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // search terms from a URL.
5055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<std::string> alternate_urls;
5065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // A parameter that, if present in the query or ref parameters of a search_url
5082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // or instant_url, causes Chrome to replace the URL with the search term.
5092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string search_terms_replacement_key;
5102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
5125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Private so we can enforce using the setters and thus enforce that these
5135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // fields are never empty.
514a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 keyword_;
5155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string url_;
5165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
5175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// AssociatedExtensionInfo ----------------------------------------------------
5201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
5211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// An AssociatedExtensionInfo represents information about the extension that
5221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// added the search engine using the Override Settings API.
5231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)struct AssociatedExtensionInfo {
5241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  std::string extension_id;
5251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
5261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Whether the search engine is supposed to be default.
5271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  bool wants_to_be_default_engine;
5281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
5291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Used to resolve conflicts when there are multiple extensions specifying the
5301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // default search engine. The most recently-installed wins.
5311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  base::Time install_time;
5321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)};
5331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
5341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
5355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TemplateURL ----------------------------------------------------------------
5365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A TemplateURL represents a single "search engine", defined primarily as a
5385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// subset of the Open Search Description Document
5395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (http://www.opensearch.org/Specifications/OpenSearch) plus some extensions.
5405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// One TemplateURL contains several TemplateURLRefs, which correspond to various
5415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// different capabilities (e.g. doing searches or getting suggestions), as well
5425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// as a TemplateURLData containing other details like the name, keyword, etc.
5435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
5445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TemplateURLs are intended to be read-only for most users; the only public
5455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// non-const method is the Profile getter, which returns a non-const Profile*.
5465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The TemplateURLService, which handles storing and manipulating TemplateURLs,
5475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// is made a friend so that it can be the exception to this pattern.
5485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TemplateURL {
5495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
5501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  enum Type {
5511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // Regular search engine.
5521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    NORMAL,
5531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // Installed by extension through Override Settings API.
5541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    NORMAL_CONTROLLED_BY_EXTENSION,
5551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // The keyword associated with an extension that uses the Omnibox API.
5561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    OMNIBOX_API_EXTENSION,
5571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  };
5585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |profile| may be NULL.  This will affect the results of e.g. calling
5595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ReplaceSearchTerms() on the member TemplateURLRefs.
5605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURL(Profile* profile, const TemplateURLData& data);
5615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~TemplateURL();
5625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Generates a favicon URL from the specified url.
5645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static GURL GenerateFaviconURL(const GURL& url);
5655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Profile* profile() { return profile_; }
5675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const TemplateURLData& data() const { return data_; }
5685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
569a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  const base::string16& short_name() const { return data_.short_name; }
5705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // An accessor for the short_name, but adjusted so it can be appropriately
5715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // displayed even if it is LTR and the UI is RTL.
572a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 AdjustedShortNameForLocaleDirection() const;
5735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
574a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  const base::string16& keyword() const { return data_.keyword(); }
5755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& url() const { return data_.url(); }
5775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& suggestions_url() const { return data_.suggestions_url; }
5785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& instant_url() const { return data_.instant_url; }
5797dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  const std::string& image_url() const { return data_.image_url; }
5803551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  const std::string& new_tab_url() const { return data_.new_tab_url; }
5817dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  const std::string& search_url_post_params() const {
5827dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return data_.search_url_post_params;
5837dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
5847dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  const std::string& suggestions_url_post_params() const {
5857dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return data_.suggestions_url_post_params;
5867dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
5877dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  const std::string& instant_url_post_params() const {
5887dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return data_.instant_url_post_params;
5897dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
5907dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  const std::string& image_url_post_params() const {
5917dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return data_.image_url_post_params;
5927dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
5935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::vector<std::string>& alternate_urls() const {
5945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return data_.alternate_urls;
5955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const GURL& favicon_url() const { return data_.favicon_url; }
5975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const GURL& originating_url() const { return data_.originating_url; }
5995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool show_in_default_list() const { return data_.show_in_default_list; }
6015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if show_in_default_list() is true and this TemplateURL has a
6025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TemplateURLRef that supports replacement.
6035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool ShowInDefaultList() const;
6045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool safe_for_autoreplace() const { return data_.safe_for_autoreplace; }
6065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::vector<std::string>& input_encodings() const {
6085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return data_.input_encodings;
6095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURLID id() const { return data_.id; }
6125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::Time date_created() const { return data_.date_created; }
6145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::Time last_modified() const { return data_.last_modified; }
6155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool created_by_policy() const { return data_.created_by_policy; }
6175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int usage_count() const { return data_.usage_count; }
6195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int prepopulate_id() const { return data_.prepopulate_id; }
6215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& sync_guid() const { return data_.sync_guid; }
6235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(beaudoin): Rename this when renaming HasSearchTermsReplacementKey().
6252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const std::string& search_terms_replacement_key() const {
6262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return data_.search_terms_replacement_key;
6272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
6282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const TemplateURLRef& url_ref() const { return url_ref_; }
6305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const TemplateURLRef& suggestions_url_ref() const {
6315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return suggestions_url_ref_;
6325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const TemplateURLRef& instant_url_ref() const { return instant_url_ref_; }
6347dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  const TemplateURLRef& image_url_ref() const { return image_url_ref_; }
6353551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  const TemplateURLRef& new_tab_url_ref() const { return new_tab_url_ref_; }
6365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if |url| supports replacement.
6385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool SupportsReplacement() const;
6395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Like SupportsReplacement but usable on threads other than the UI thread.
6415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool SupportsReplacementUsingTermsData(
6425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const SearchTermsData& search_terms_data) const;
6435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if this TemplateURL uses Google base URLs and has a keyword
6455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // of "google.TLD".  We use this to decide whether we can automatically
6465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // update the keyword to reflect the current Google base URL TLD.
6475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsGoogleSearchURLWithReplaceableKeyword() const;
6485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the keywords match or if
6505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // IsGoogleSearchURLWithReplaceableKeyword() is true for both TemplateURLs.
6515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool HasSameKeywordAs(const TemplateURL& other) const;
6525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  Type GetType() const;
6541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
6551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Returns the id of the extension that added this search engine. Only call
6561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // this for TemplateURLs of type NORMAL_CONTROLLED_BY_EXTENSION or
6571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // OMNIBOX_API_EXTENSION.
6585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string GetExtensionId() const;
6595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the total number of URLs comprised in this template, including
6615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // search and alternate URLs.
6625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t URLCount() const;
6635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Gets the search URL at the given index. The alternate URLs, if any, are
6655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // numbered starting at 0, and the primary search URL follows. This is used
6665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to decode the search term given a search URL (see
6675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ExtractSearchTermsFromURL()).
6685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& GetURL(size_t index) const;
6695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Use the alternate URLs and the search URL to match the provided |url|
6715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and extract |search_terms| from it. Returns false and an empty
6725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |search_terms| if no search terms can be matched. The order in which the
6732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // alternate URLs are listed dictates their priority, the URL at index 0 is
6742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // treated as the highest priority and the primary search URL is treated as
6752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // the lowest priority (see GetURL()).  For example, if a TemplateURL has
6765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // alternate URL "http://foo/#q={searchTerms}" and search URL
6775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // "http://foo/?q={searchTerms}", and the URL to be decoded is
6785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // "http://foo/?q=a#q=b", the alternate URL will match first and the decoded
6795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // search term will be "b".
680a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  bool ExtractSearchTermsFromURL(const GURL& url, base::string16* search_terms);
6815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Like ExtractSearchTermsFromURL but usable on threads other than the UI
6832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // thread.
6842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool ExtractSearchTermsFromURLUsingTermsData(
6852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const GURL& url,
686a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      base::string16* search_terms,
6872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const SearchTermsData& search_terms_data);
6882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns true if non-empty search terms could be extracted from |url| using
6902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // ExtractSearchTermsFromURL(). In other words, this returns whether |url|
6912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // could be the result of performing a search with |this|.
6922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool IsSearchURL(const GURL& url);
6932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Like IsSearchURL but usable on threads other than the UI thread.
6952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool IsSearchURLUsingTermsData(
6962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const GURL& url,
6972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const SearchTermsData& search_terms_data);
6982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns true if the specified |url| contains the search terms replacement
7002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // key in either the query or the ref. This method does not verify anything
7012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // else about the URL. In particular, it does not check that the domain
7022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // matches that of this TemplateURL.
7032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(beaudoin): Rename this to reflect that it really checks for an
7042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // InstantExtended capable URL.
7052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool HasSearchTermsReplacementKey(const GURL& url) const;
7062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Given a |url| corresponding to this TemplateURL, identifies the search
7082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // terms and replaces them with the ones in |search_terms_args|, leaving the
7092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // other parameters untouched. If the replacement fails, returns false and
7102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // leaves |result| untouched. This is used by mobile ports to perform query
7112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // refinement.
7122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool ReplaceSearchTermsInURL(
7132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const GURL& url,
7142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const TemplateURLRef::SearchTermsArgs& search_terms_args,
7152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      GURL* result);
7162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Encodes the search terms from |search_terms_args| so that we know the
7182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |input_encoding|. Returns the |encoded_terms| and the
7192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |encoded_original_query|. |encoded_terms| may be escaped as path or query
7202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // depending on |is_in_query|; |encoded_original_query| is always escaped as
7212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // query.
7222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void EncodeSearchTerms(
7232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const TemplateURLRef::SearchTermsArgs& search_terms_args,
7242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      bool is_in_query,
7252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      std::string* input_encoding,
726a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      base::string16* encoded_terms,
727a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      base::string16* encoded_original_query) const;
7282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
7305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class TemplateURLService;
7314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ReflectsBookmarkBarPinned);
7325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void CopyFrom(const TemplateURL& other);
7345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetURL(const std::string& url);
7365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetPrepopulateId(int id);
7375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Resets the keyword if IsGoogleSearchURLWithReplaceableKeyword() or |force|.
7395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The |force| parameter is useful when the existing keyword is known to be
7405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // a placeholder.  The resulting keyword is generated using
7415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TemplateURLService::GenerateSearchURL() and
7425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TemplateURLService::GenerateKeyword().
7435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ResetKeywordIfNecessary(bool force);
7445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Uses the alternate URLs and the search URL to match the provided |url|
7462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // and extract |search_terms| from it as well as the |search_terms_component|
7472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // (either REF or QUERY) and |search_terms_component| at which the
7482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |search_terms| are found in |url|. See also ExtractSearchTermsFromURL().
7492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool FindSearchTermsInURL(
7502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const GURL& url,
7512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const SearchTermsData& search_terms_data,
752a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      base::string16* search_terms,
7532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      url_parse::Parsed::ComponentType* search_terms_component,
7542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      url_parse::Component* search_terms_position);
7552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Profile* profile_;
7575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURLData data_;
7585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURLRef url_ref_;
7595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURLRef suggestions_url_ref_;
7605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURLRef instant_url_ref_;
7617dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  TemplateURLRef image_url_ref_;
7623551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  TemplateURLRef new_tab_url_ref_;
7631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  scoped_ptr<AssociatedExtensionInfo> extension_info_;
7645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(sky): Add date last parsed OSD file.
7665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(TemplateURL);
7685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
7695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_H_
771