template_url.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/gtest_prod_util.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/time.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/search_engines/template_url_id.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "googleurl/src/gurl.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "googleurl/src/url_parse.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Profile;
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class SearchTermsData;
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TemplateURL;
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TemplateURLRef -------------------------------------------------------------
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A TemplateURLRef represents a single URL within the larger TemplateURL class
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (which represents an entire "search engine", see below).  If
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// SupportsReplacement() is true, this URL has placeholders in it, for which
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// callers can substitute values to get a "real" URL using ReplaceSearchTerms().
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TemplateURLRefs always have a non-NULL |owner_| TemplateURL, which they
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// access in order to get at important data like the underlying URL string or
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the associated Profile.
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TemplateURLRef {
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Magic numbers to pass to ReplaceSearchTerms() for the |accepted_suggestion|
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // parameter.  Most callers aren't using Suggest capabilities and should just
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // pass NO_SUGGESTIONS_AVAILABLE.
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NOTE: Because positive values are meaningful, make sure these are negative!
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum AcceptedSuggestion {
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NO_SUGGESTION_CHOSEN = -1,
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NO_SUGGESTIONS_AVAILABLE = -2,
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Which kind of URL within our owner we are.  This allows us to get at the
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // correct string field. Use |INDEXED| to indicate that the numerical
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |index_in_owner_| should be used instead.
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum Type {
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SEARCH,
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SUGGEST,
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    INSTANT,
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    INDEXED
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This struct encapsulates arguments passed to
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TemplateURLRef::ReplaceSearchTerms methods.  By default, only search_terms
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is required and is passed in the constructor.
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  struct SearchTermsArgs {
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    explicit SearchTermsArgs(const string16& search_terms);
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The search terms (query).
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const string16 search_terms;
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The original (input) query.
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    string16 original_query;
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The optional assisted query stats, aka AQS, used for logging purposes.
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // This string contains impressions of all autocomplete matches shown
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // at the query submission time.  For privacy reasons, we require the
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // search provider to support HTTPS protocol in order to receive the AQS
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // param.
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // For more details, see http://goto.google.com/binary-clients-logging .
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string assisted_query_stats;
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // TODO: Remove along with "aq" CGI param.
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int accepted_suggestion;
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // The 0-based position of the cursor within the query string at the time
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // the request was issued.  Set to string16::npos if not used.
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    size_t cursor_position;
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // The start-edge margin of the omnibox in pixels, used in extended Instant
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // to align the preview contents with the omnibox.
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    int omnibox_start_margin;
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURLRef(TemplateURL* owner, Type type);
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURLRef(TemplateURL* owner, size_t index_in_owner);
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~TemplateURLRef();
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the raw URL. None of the parameters will have been replaced.
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string GetURL() const;
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if this URL supports replacement.
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool SupportsReplacement() const;
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Like SupportsReplacement but usable on threads other than the UI thread.
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool SupportsReplacementUsingTermsData(
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const SearchTermsData& search_terms_data) const;
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a string that is the result of replacing the search terms in
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the url with the specified arguments.  We use our owner's input encoding.
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If this TemplateURLRef does not support replacement (SupportsReplacement
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // returns false), an empty string is returned.
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string ReplaceSearchTerms(
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const SearchTermsArgs& search_terms_args) const;
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Just like ReplaceSearchTerms except that it takes SearchTermsData to supply
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the data for some search terms. Most of the time ReplaceSearchTerms should
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // be called.
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string ReplaceSearchTermsUsingTermsData(
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const SearchTermsArgs& search_terms_args,
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const SearchTermsData& search_terms_data) const;
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the TemplateURLRef is valid. An invalid TemplateURLRef is
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // one that contains unknown terms, or invalid characters.
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsValid() const;
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Like IsValid but usable on threads other than the UI thread.
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsValidUsingTermsData(const SearchTermsData& search_terms_data) const;
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a string representation of this TemplateURLRef suitable for
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // display. The display format is the same as the format used by Firefox.
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  string16 DisplayURL() const;
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Converts a string as returned by DisplayURL back into a string as
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // understood by TemplateURLRef.
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static std::string DisplayURLToURLRef(const string16& display_url);
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If this TemplateURLRef is valid and contains one search term, this returns
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the host/path of the URL, otherwise this returns an empty string.
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& GetHost() const;
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& GetPath() const;
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If this TemplateURLRef is valid and contains one search term, this returns
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the key of the search term, otherwise this returns an empty string.
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& GetSearchTermKey() const;
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Converts the specified term in our owner's encoding to a string16.
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  string16 SearchTermToString16(const std::string& term) const;
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if this TemplateURLRef has a replacement term of
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // {google:baseURL} or {google:baseSuggestURL}.
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool HasGoogleBaseURLs() const;
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Use the pattern referred to by this TemplateURLRef to match the provided
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |url| and extract |search_terms| from it. Returns true if the pattern
1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // matches, even if |search_terms| is empty. In this case
1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |search_term_component|, if not NULL, indicates whether the search terms
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // were found in the query or the ref parameters; and |search_terms_position|,
1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // if not NULL, contains the position of the search terms in the query or the
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // ref parameters. Returns false and an empty |search_terms| if the pattern
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // does not match.
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool ExtractSearchTermsFromURL(
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const GURL& url,
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      string16* search_terms,
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const SearchTermsData& search_terms_data,
1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      url_parse::Parsed::ComponentType* search_term_component,
1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      url_parse::Component* search_terms_position) const;
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class TemplateURL;
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, SetPrepopulatedAndParse);
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseParameterKnown);
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseParameterUnknown);
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseURLEmpty);
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseURLNoTemplateEnd);
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseURLNoKnownParameters);
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseURLTwoParameters);
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseURLNestedParameter);
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Enumeration of the known types.
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum ReplacementType {
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ENCODING,
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GOOGLE_ASSISTED_QUERY_STATS,
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GOOGLE_BASE_URL,
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GOOGLE_BASE_SUGGEST_URL,
1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    GOOGLE_CURSOR_POSITION,
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GOOGLE_INSTANT_ENABLED,
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    GOOGLE_INSTANT_EXTENDED_ENABLED,
1782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    GOOGLE_NTP_IS_THEMED,
1792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    GOOGLE_OMNIBOX_START_MARGIN,
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION,
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GOOGLE_RLZ,
1822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    GOOGLE_SEARCH_CLIENT,
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GOOGLE_SEARCH_FIELDTRIAL_GROUP,
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GOOGLE_UNESCAPED_SEARCH_TERMS,
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LANGUAGE,
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SEARCH_TERMS,
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Used to identify an element of the raw url that can be replaced.
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  struct Replacement {
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Replacement(ReplacementType type, size_t index)
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        : type(type), index(index) {}
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ReplacementType type;
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    size_t index;
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The list of elements to replace.
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef std::vector<struct Replacement> Replacements;
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TemplateURLRef internally caches values to make replacement quick. This
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // method invalidates any cached values.
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void InvalidateCachedValues() const;
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Parses the parameter in url at the specified offset. start/end specify the
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // range of the parameter in the url, including the braces. If the parameter
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is valid, url is updated to reflect the appropriate parameter. If
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the parameter is one of the known parameters an element is added to
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // replacements indicating the type and range of the element. The original
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // parameter is erased from the url.
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the parameter is not a known parameter, false is returned. If this is a
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // prepopulated URL, the parameter is erased, otherwise it is left alone.
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool ParseParameter(size_t start,
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      size_t end,
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      std::string* url,
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      Replacements* replacements) const;
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Parses the specified url, replacing parameters as necessary. If
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // successful, valid is set to true, and the parsed url is returned. For all
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // known parameters that are encountered an entry is added to replacements.
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If there is an error parsing the url, valid is set to false, and an empty
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // string is returned.
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string ParseURL(const std::string& url,
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       Replacements* replacements,
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       bool* valid) const;
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the url has not yet been parsed, ParseURL is invoked.
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NOTE: While this is const, it modifies parsed_, valid_, parsed_url_ and
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // search_offset_.
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ParseIfNecessary() const;
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Like ParseIfNecessary but usable on threads other than the UI thread.
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ParseIfNecessaryUsingTermsData(
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const SearchTermsData& search_terms_data) const;
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Extracts the query key and host from the url.
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ParseHostAndSearchTermKey(
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const SearchTermsData& search_terms_data) const;
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The TemplateURL that contains us.  This should outlive us.
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURL* const owner_;
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // What kind of URL we are.
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const Type type_;
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If |type_| is |INDEXED|, this |index_in_owner_| is used instead to refer to
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // a url within our owner.
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const size_t index_in_owner_;
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Whether the URL has been parsed.
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable bool parsed_;
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Whether the url was successfully parsed.
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable bool valid_;
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The parsed URL. All terms have been stripped out of this with
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // replacements_ giving the index of the terms to replace.
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable std::string parsed_url_;
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Do we support replacement?
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable bool supports_replacements_;
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The replaceable parts of url (parsed_url_). These are ordered by index
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // into the string, and may be empty.
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable Replacements replacements_;
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Host, path, key and location of the search term. These are only set if the
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // url contains one search term.
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable std::string host_;
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable std::string path_;
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable std::string search_term_key_;
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable url_parse::Parsed::ComponentType search_term_key_location_;
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Whether the contained URL is a pre-populated URL.
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool prepopulated_;
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(TemplateURLRef);
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TemplateURLData ------------------------------------------------------------
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The data for the TemplateURL.  Separating this into its own class allows most
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// users to do SSA-style usage of TemplateURL: construct a TemplateURLData with
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// whatever fields are desired, then create an immutable TemplateURL from it.
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct TemplateURLData {
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURLData();
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~TemplateURLData();
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A short description of the template. This is the name we show to the user
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // in various places that use TemplateURLs. For example, the location bar
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // shows this when the user selects a substituting match.
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  string16 short_name;
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The shortcut for this TemplateURL.  |keyword| must be non-empty.
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetKeyword(const string16& keyword);
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const string16& keyword() const { return keyword_; }
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The raw URL for the TemplateURL, which may not be valid as-is (e.g. because
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // it requires substitutions first).  This must be non-empty.
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetURL(const std::string& url);
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& url() const { return url_; }
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Optional additional raw URLs.
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string suggestions_url;
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string instant_url;
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Optional favicon for the TemplateURL.
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GURL favicon_url;
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // URL to the OSD file this came from. May be empty.
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GURL originating_url;
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Whether this TemplateURL is shown in the default list of search providers.
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This is just a property and does not indicate whether the TemplateURL has a
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TemplateURLRef that supports replacement. Use
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TemplateURL::ShowInDefaultList() to test both.
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool show_in_default_list;
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Whether it's safe for auto-modification code (the autogenerator and the
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // code that imports data from other browsers) to replace the TemplateURL.
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This should be set to false for any TemplateURL the user edits, or any
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TemplateURL that the user clearly manually edited in the past, like a
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // bookmark keyword from another browser.
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool safe_for_autoreplace;
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The list of supported encodings for the search terms. This may be empty,
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // which indicates the terms should be encoded with UTF-8.
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<std::string> input_encodings;
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Unique identifier of this TemplateURL. The unique ID is set by the
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TemplateURLService when the TemplateURL is added to it.
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURLID id;
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Date this TemplateURL was created.
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NOTE: this may be 0, which indicates the TemplateURL was created before we
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // started tracking creation time.
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::Time date_created;
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The last time this TemplateURL was modified by a user, since creation.
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NOTE: Like date_created above, this may be 0.
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::Time last_modified;
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // True if this TemplateURL was automatically created by the administrator via
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // group policy.
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool created_by_policy;
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Number of times this TemplateURL has been explicitly used to load a URL.
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We don't increment this for uses as the "default search engine" since
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // that's not really "explicit" usage and incrementing would result in pinning
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the user's default search engine(s) to the top of the list of searches on
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the New Tab page, de-emphasizing the omnibox as "where you go to search".
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int usage_count;
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If this TemplateURL comes from prepopulated data the prepopulate_id is > 0.
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int prepopulate_id;
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The primary unique identifier for Sync. This set on all TemplateURLs
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // regardless of whether they have been associated with Sync.
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string sync_guid;
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A list of URL patterns that can be used, in addition to |url_|, to extract
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // search terms from a URL.
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<std::string> alternate_urls;
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // A parameter that, if present in the query or ref parameters of a search_url
3692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // or instant_url, causes Chrome to replace the URL with the search term.
3702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string search_terms_replacement_key;
3712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Private so we can enforce using the setters and thus enforce that these
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // fields are never empty.
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  string16 keyword_;
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string url_;
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TemplateURL ----------------------------------------------------------------
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A TemplateURL represents a single "search engine", defined primarily as a
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// subset of the Open Search Description Document
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (http://www.opensearch.org/Specifications/OpenSearch) plus some extensions.
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// One TemplateURL contains several TemplateURLRefs, which correspond to various
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// different capabilities (e.g. doing searches or getting suggestions), as well
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// as a TemplateURLData containing other details like the name, keyword, etc.
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TemplateURLs are intended to be read-only for most users; the only public
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// non-const method is the Profile getter, which returns a non-const Profile*.
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The TemplateURLService, which handles storing and manipulating TemplateURLs,
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// is made a friend so that it can be the exception to this pattern.
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TemplateURL {
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |profile| may be NULL.  This will affect the results of e.g. calling
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ReplaceSearchTerms() on the member TemplateURLRefs.
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURL(Profile* profile, const TemplateURLData& data);
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~TemplateURL();
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Generates a favicon URL from the specified url.
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static GURL GenerateFaviconURL(const GURL& url);
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Profile* profile() { return profile_; }
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const TemplateURLData& data() const { return data_; }
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const string16& short_name() const { return data_.short_name; }
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // An accessor for the short_name, but adjusted so it can be appropriately
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // displayed even if it is LTR and the UI is RTL.
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  string16 AdjustedShortNameForLocaleDirection() const;
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const string16& keyword() const { return data_.keyword(); }
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& url() const { return data_.url(); }
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& suggestions_url() const { return data_.suggestions_url; }
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& instant_url() const { return data_.instant_url; }
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::vector<std::string>& alternate_urls() const {
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return data_.alternate_urls;
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const GURL& favicon_url() const { return data_.favicon_url; }
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const GURL& originating_url() const { return data_.originating_url; }
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool show_in_default_list() const { return data_.show_in_default_list; }
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if show_in_default_list() is true and this TemplateURL has a
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TemplateURLRef that supports replacement.
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool ShowInDefaultList() const;
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool safe_for_autoreplace() const { return data_.safe_for_autoreplace; }
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::vector<std::string>& input_encodings() const {
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return data_.input_encodings;
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURLID id() const { return data_.id; }
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::Time date_created() const { return data_.date_created; }
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::Time last_modified() const { return data_.last_modified; }
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool created_by_policy() const { return data_.created_by_policy; }
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int usage_count() const { return data_.usage_count; }
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int prepopulate_id() const { return data_.prepopulate_id; }
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& sync_guid() const { return data_.sync_guid; }
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(beaudoin): Rename this when renaming HasSearchTermsReplacementKey().
4482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const std::string& search_terms_replacement_key() const {
4492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return data_.search_terms_replacement_key;
4502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
4512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const TemplateURLRef& url_ref() const { return url_ref_; }
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const TemplateURLRef& suggestions_url_ref() const {
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return suggestions_url_ref_;
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const TemplateURLRef& instant_url_ref() const { return instant_url_ref_; }
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if |url| supports replacement.
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool SupportsReplacement() const;
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Like SupportsReplacement but usable on threads other than the UI thread.
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool SupportsReplacementUsingTermsData(
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const SearchTermsData& search_terms_data) const;
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if this TemplateURL uses Google base URLs and has a keyword
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // of "google.TLD".  We use this to decide whether we can automatically
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // update the keyword to reflect the current Google base URL TLD.
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsGoogleSearchURLWithReplaceableKeyword() const;
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the keywords match or if
4715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // IsGoogleSearchURLWithReplaceableKeyword() is true for both TemplateURLs.
4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool HasSameKeywordAs(const TemplateURL& other) const;
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string GetExtensionId() const;
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsExtensionKeyword() const;
4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the total number of URLs comprised in this template, including
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // search and alternate URLs.
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t URLCount() const;
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Gets the search URL at the given index. The alternate URLs, if any, are
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // numbered starting at 0, and the primary search URL follows. This is used
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to decode the search term given a search URL (see
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ExtractSearchTermsFromURL()).
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& GetURL(size_t index) const;
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Use the alternate URLs and the search URL to match the provided |url|
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and extract |search_terms| from it. Returns false and an empty
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |search_terms| if no search terms can be matched. The order in which the
4902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // alternate URLs are listed dictates their priority, the URL at index 0 is
4912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // treated as the highest priority and the primary search URL is treated as
4922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // the lowest priority (see GetURL()).  For example, if a TemplateURL has
4935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // alternate URL "http://foo/#q={searchTerms}" and search URL
4945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // "http://foo/?q={searchTerms}", and the URL to be decoded is
4955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // "http://foo/?q=a#q=b", the alternate URL will match first and the decoded
4965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // search term will be "b".
4975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool ExtractSearchTermsFromURL(const GURL& url, string16* search_terms);
4985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Like ExtractSearchTermsFromURL but usable on threads other than the UI
5002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // thread.
5012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool ExtractSearchTermsFromURLUsingTermsData(
5022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const GURL& url,
5032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      string16* search_terms,
5042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const SearchTermsData& search_terms_data);
5052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns true if non-empty search terms could be extracted from |url| using
5072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // ExtractSearchTermsFromURL(). In other words, this returns whether |url|
5082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // could be the result of performing a search with |this|.
5092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool IsSearchURL(const GURL& url);
5102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Like IsSearchURL but usable on threads other than the UI thread.
5122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool IsSearchURLUsingTermsData(
5132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const GURL& url,
5142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const SearchTermsData& search_terms_data);
5152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns true if the specified |url| contains the search terms replacement
5172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // key in either the query or the ref. This method does not verify anything
5182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // else about the URL. In particular, it does not check that the domain
5192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // matches that of this TemplateURL.
5202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(beaudoin): Rename this to reflect that it really checks for an
5212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // InstantExtended capable URL.
5222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool HasSearchTermsReplacementKey(const GURL& url) const;
5232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Given a |url| corresponding to this TemplateURL, identifies the search
5252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // terms and replaces them with the ones in |search_terms_args|, leaving the
5262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // other parameters untouched. If the replacement fails, returns false and
5272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // leaves |result| untouched. This is used by mobile ports to perform query
5282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // refinement.
5292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool ReplaceSearchTermsInURL(
5302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const GURL& url,
5312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const TemplateURLRef::SearchTermsArgs& search_terms_args,
5322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      GURL* result);
5332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Encodes the search terms from |search_terms_args| so that we know the
5352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |input_encoding|. Returns the |encoded_terms| and the
5362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |encoded_original_query|. |encoded_terms| may be escaped as path or query
5372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // depending on |is_in_query|; |encoded_original_query| is always escaped as
5382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // query.
5392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void EncodeSearchTerms(
5402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const TemplateURLRef::SearchTermsArgs& search_terms_args,
5412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      bool is_in_query,
5422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      std::string* input_encoding,
5432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      string16* encoded_terms,
5442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      string16* encoded_original_query) const;
5452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
5475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class TemplateURLService;
5485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void CopyFrom(const TemplateURL& other);
5505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetURL(const std::string& url);
5525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetPrepopulateId(int id);
5535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Resets the keyword if IsGoogleSearchURLWithReplaceableKeyword() or |force|.
5555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The |force| parameter is useful when the existing keyword is known to be
5565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // a placeholder.  The resulting keyword is generated using
5575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TemplateURLService::GenerateSearchURL() and
5585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TemplateURLService::GenerateKeyword().
5595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ResetKeywordIfNecessary(bool force);
5605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Uses the alternate URLs and the search URL to match the provided |url|
5622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // and extract |search_terms| from it as well as the |search_terms_component|
5632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // (either REF or QUERY) and |search_terms_component| at which the
5642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |search_terms| are found in |url|. See also ExtractSearchTermsFromURL().
5652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool FindSearchTermsInURL(
5662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const GURL& url,
5672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const SearchTermsData& search_terms_data,
5682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      string16* search_terms,
5692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      url_parse::Parsed::ComponentType* search_terms_component,
5702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      url_parse::Component* search_terms_position);
5712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Profile* profile_;
5735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURLData data_;
5745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURLRef url_ref_;
5755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURLRef suggestions_url_ref_;
5765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURLRef instant_url_ref_;
5775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(sky): Add date last parsed OSD file.
5795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(TemplateURL);
5815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
5825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_H_
584