Home | History | Annotate | Download | only in search_engines
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/search_engines/template_url.h"
      6 
      7 #include <string>
      8 #include <vector>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/command_line.h"
     12 #include "base/format_macros.h"
     13 #include "base/guid.h"
     14 #include "base/i18n/case_conversion.h"
     15 #include "base/i18n/icu_string_conversions.h"
     16 #include "base/i18n/rtl.h"
     17 #include "base/logging.h"
     18 #include "base/metrics/field_trial.h"
     19 #include "base/rand_util.h"
     20 #include "base/strings/string_number_conversions.h"
     21 #include "base/strings/string_split.h"
     22 #include "base/strings/string_util.h"
     23 #include "base/strings/stringprintf.h"
     24 #include "base/strings/utf_string_conversions.h"
     25 #include "chrome/browser/google/google_util.h"
     26 #include "chrome/browser/search/search.h"
     27 #include "chrome/browser/search_engines/search_terms_data.h"
     28 #include "chrome/browser/search_engines/template_url_service.h"
     29 #include "chrome/common/chrome_switches.h"
     30 #include "chrome/common/chrome_version_info.h"
     31 #include "chrome/common/url_constants.h"
     32 #include "extensions/common/constants.h"
     33 #include "google_apis/google_api_keys.h"
     34 #include "net/base/escape.h"
     35 #include "net/base/mime_util.h"
     36 #include "ui/base/l10n/l10n_util.h"
     37 
     38 namespace {
     39 
     40 // The TemplateURLRef has any number of terms that need to be replaced. Each of
     41 // the terms is enclosed in braces. If the character preceeding the final
     42 // brace is a ?, it indicates the term is optional and can be replaced with
     43 // an empty string.
     44 const char kStartParameter = '{';
     45 const char kEndParameter = '}';
     46 const char kOptional = '?';
     47 
     48 // Known parameters found in the URL.
     49 const char kSearchTermsParameter[] = "searchTerms";
     50 const char kSearchTermsParameterFull[] = "{searchTerms}";
     51 const char kCountParameter[] = "count";
     52 const char kStartIndexParameter[] = "startIndex";
     53 const char kStartPageParameter[] = "startPage";
     54 const char kLanguageParameter[] = "language";
     55 const char kInputEncodingParameter[] = "inputEncoding";
     56 const char kOutputEncodingParameter[] = "outputEncoding";
     57 
     58 const char kGoogleAssistedQueryStatsParameter[] = "google:assistedQueryStats";
     59 
     60 // Host/Domain Google searches are relative to.
     61 const char kGoogleBaseURLParameter[] = "google:baseURL";
     62 const char kGoogleBaseURLParameterFull[] = "{google:baseURL}";
     63 
     64 // Like google:baseURL, but for the Search Suggest capability.
     65 const char kGoogleBaseSuggestURLParameter[] = "google:baseSuggestURL";
     66 const char kGoogleBaseSuggestURLParameterFull[] = "{google:baseSuggestURL}";
     67 const char kGoogleBookmarkBarPinnedParameter[] = "google:bookmarkBarPinned";
     68 const char kGoogleCurrentPageUrlParameter[] = "google:currentPageUrl";
     69 const char kGoogleCursorPositionParameter[] = "google:cursorPosition";
     70 const char kGoogleForceInstantResultsParameter[] = "google:forceInstantResults";
     71 const char kGoogleInstantExtendedEnabledParameter[] =
     72     "google:instantExtendedEnabledParameter";
     73 const char kGoogleInstantExtendedEnabledKey[] =
     74     "google:instantExtendedEnabledKey";
     75 const char kGoogleInstantExtendedEnabledKeyFull[] =
     76     "{google:instantExtendedEnabledKey}";
     77 const char kGoogleNTPIsThemedParameter[] = "google:ntpIsThemedParameter";
     78 const char kGoogleOmniboxStartMarginParameter[] =
     79     "google:omniboxStartMarginParameter";
     80 const char kGoogleOriginalQueryForSuggestionParameter[] =
     81     "google:originalQueryForSuggestion";
     82 const char kGooglePageClassificationParameter[] = "google:pageClassification";
     83 const char kGoogleRLZParameter[] = "google:RLZ";
     84 const char kGoogleSearchClient[] = "google:searchClient";
     85 const char kGoogleSearchFieldtrialParameter[] =
     86     "google:searchFieldtrialParameter";
     87 const char kGoogleSourceIdParameter[] = "google:sourceId";
     88 const char kGoogleSuggestAPIKeyParameter[] = "google:suggestAPIKeyParameter";
     89 const char kGoogleSuggestClient[] = "google:suggestClient";
     90 const char kGoogleSuggestRequestId[] = "google:suggestRid";
     91 
     92 // Same as kSearchTermsParameter, with no escaping.
     93 const char kGoogleUnescapedSearchTermsParameter[] =
     94     "google:unescapedSearchTerms";
     95 const char kGoogleUnescapedSearchTermsParameterFull[] =
     96     "{google:unescapedSearchTerms}";
     97 
     98 const char kGoogleImageSearchSource[] = "google:imageSearchSource";
     99 const char kGoogleImageThumbnailParameter[] = "google:imageThumbnail";
    100 const char kGoogleImageURLParameter[] = "google:imageURL";
    101 const char kGoogleImageOriginalWidth[] = "google:imageOriginalWidth";
    102 const char kGoogleImageOriginalHeight[] = "google:imageOriginalHeight";
    103 
    104 // Display value for kSearchTermsParameter.
    105 const char kDisplaySearchTerms[] = "%s";
    106 
    107 // Display value for kGoogleUnescapedSearchTermsParameter.
    108 const char kDisplayUnescapedSearchTerms[] = "%S";
    109 
    110 // Used if the count parameter is not optional. Indicates we want 10 search
    111 // results.
    112 const char kDefaultCount[] = "10";
    113 
    114 // Used if the parameter kOutputEncodingParameter is required.
    115 const char kOutputEncodingType[] = "UTF-8";
    116 
    117 // Attempts to encode |terms| and |original_query| in |encoding| and escape
    118 // them.  |terms| may be escaped as path or query depending on |is_in_query|;
    119 // |original_query| is always escaped as query.  Returns whether the encoding
    120 // process succeeded.
    121 bool TryEncoding(const base::string16& terms,
    122                  const base::string16& original_query,
    123                  const char* encoding,
    124                  bool is_in_query,
    125                  base::string16* escaped_terms,
    126                  base::string16* escaped_original_query) {
    127   DCHECK(escaped_terms);
    128   DCHECK(escaped_original_query);
    129   std::string encoded_terms;
    130   if (!base::UTF16ToCodepage(terms, encoding,
    131       base::OnStringConversionError::SKIP, &encoded_terms))
    132     return false;
    133   *escaped_terms = UTF8ToUTF16(is_in_query ?
    134       net::EscapeQueryParamValue(encoded_terms, true) :
    135       net::EscapePath(encoded_terms));
    136   if (original_query.empty())
    137     return true;
    138   std::string encoded_original_query;
    139   if (!base::UTF16ToCodepage(original_query, encoding,
    140       base::OnStringConversionError::SKIP, &encoded_original_query))
    141     return false;
    142   *escaped_original_query =
    143       UTF8ToUTF16(net::EscapeQueryParamValue(encoded_original_query, true));
    144   return true;
    145 }
    146 
    147 // Extract query key and host given a list of parameters coming from the URL
    148 // query or ref.
    149 std::string FindSearchTermsKey(const std::string& params) {
    150   if (params.empty())
    151     return std::string();
    152   url_parse::Component query, key, value;
    153   query.len = static_cast<int>(params.size());
    154   while (url_parse::ExtractQueryKeyValue(params.c_str(), &query, &key,
    155                                          &value)) {
    156     if (key.is_nonempty() && value.is_nonempty()) {
    157       std::string value_string = params.substr(value.begin, value.len);
    158       if (value_string.find(kSearchTermsParameterFull, 0) !=
    159           std::string::npos ||
    160           value_string.find(kGoogleUnescapedSearchTermsParameterFull, 0) !=
    161           std::string::npos) {
    162         return params.substr(key.begin, key.len);
    163       }
    164     }
    165   }
    166   return std::string();
    167 }
    168 
    169 // Returns the string to use for replacements of type
    170 // GOOGLE_IMAGE_SEARCH_SOURCE.
    171 std::string GetGoogleImageSearchSource() {
    172   chrome::VersionInfo version_info;
    173   if (version_info.is_valid()) {
    174     std::string version(version_info.Name() + " " + version_info.Version());
    175     if (version_info.IsOfficialBuild())
    176       version += " (Official)";
    177     version += " " + version_info.OSType();
    178     std::string modifier(version_info.GetVersionStringModifier());
    179     if (!modifier.empty())
    180       version += " " + modifier;
    181     return version;
    182   }
    183   return "unknown";
    184 }
    185 
    186 bool IsTemplateParameterString(const std::string& param) {
    187   return (param.length() > 2) && (*(param.begin()) == kStartParameter) &&
    188       (*(param.rbegin()) == kEndParameter);
    189 }
    190 
    191 bool ShowingSearchTermsOnSRP() {
    192   return chrome::IsInstantExtendedAPIEnabled() &&
    193       chrome::IsQueryExtractionEnabled();
    194 }
    195 
    196 }  // namespace
    197 
    198 
    199 // TemplateURLRef::SearchTermsArgs --------------------------------------------
    200 
    201 TemplateURLRef::SearchTermsArgs::SearchTermsArgs(
    202     const base::string16& search_terms)
    203     : search_terms(search_terms),
    204       accepted_suggestion(NO_SUGGESTIONS_AVAILABLE),
    205       cursor_position(base::string16::npos),
    206       omnibox_start_margin(-1),
    207       page_classification(AutocompleteInput::INVALID_SPEC),
    208       bookmark_bar_pinned(false),
    209       append_extra_query_params(false),
    210       force_instant_results(false) {
    211 }
    212 
    213 TemplateURLRef::SearchTermsArgs::~SearchTermsArgs() {
    214 }
    215 
    216 
    217 // TemplateURLRef -------------------------------------------------------------
    218 
    219 TemplateURLRef::TemplateURLRef(TemplateURL* owner, Type type)
    220     : owner_(owner),
    221       type_(type),
    222       index_in_owner_(-1),
    223       parsed_(false),
    224       valid_(false),
    225       supports_replacements_(false),
    226       search_term_key_location_(url_parse::Parsed::QUERY),
    227       prepopulated_(false),
    228       showing_search_terms_(ShowingSearchTermsOnSRP()) {
    229   DCHECK(owner_);
    230   DCHECK_NE(INDEXED, type_);
    231 }
    232 
    233 TemplateURLRef::TemplateURLRef(TemplateURL* owner, size_t index_in_owner)
    234     : owner_(owner),
    235       type_(INDEXED),
    236       index_in_owner_(index_in_owner),
    237       parsed_(false),
    238       valid_(false),
    239       supports_replacements_(false),
    240       search_term_key_location_(url_parse::Parsed::QUERY),
    241       prepopulated_(false),
    242       showing_search_terms_(ShowingSearchTermsOnSRP()) {
    243   DCHECK(owner_);
    244   DCHECK_LT(index_in_owner_, owner_->URLCount());
    245 }
    246 
    247 TemplateURLRef::~TemplateURLRef() {
    248 }
    249 
    250 std::string TemplateURLRef::GetURL() const {
    251   switch (type_) {
    252     case SEARCH:  return owner_->url();
    253     case SUGGEST: return owner_->suggestions_url();
    254     case INSTANT: return owner_->instant_url();
    255     case IMAGE:   return owner_->image_url();
    256     case NEW_TAB: return owner_->new_tab_url();
    257     case INDEXED: return owner_->GetURL(index_in_owner_);
    258     default:      NOTREACHED(); return std::string();  // NOLINT
    259   }
    260 }
    261 
    262 std::string TemplateURLRef::GetPostParamsString() const {
    263   switch (type_) {
    264     case INDEXED:
    265     case SEARCH:  return owner_->search_url_post_params();
    266     case SUGGEST: return owner_->suggestions_url_post_params();
    267     case INSTANT: return owner_->instant_url_post_params();
    268     case NEW_TAB: return std::string();
    269     case IMAGE:   return owner_->image_url_post_params();
    270     default:      NOTREACHED(); return std::string();  // NOLINT
    271   }
    272 }
    273 
    274 bool TemplateURLRef::UsesPOSTMethodUsingTermsData(
    275     const SearchTermsData* search_terms_data) const {
    276   if (search_terms_data)
    277     ParseIfNecessaryUsingTermsData(*search_terms_data);
    278   else
    279     ParseIfNecessary();
    280   return !post_params_.empty();
    281 }
    282 
    283 bool TemplateURLRef::EncodeFormData(const PostParams& post_params,
    284                                     PostContent* post_content) const {
    285   if (post_params.empty())
    286     return true;
    287   if (!post_content)
    288     return false;
    289 
    290   const char kUploadDataMIMEType[] = "multipart/form-data; boundary=";
    291   const char kMultipartBoundary[] = "----+*+----%016" PRIx64 "----+*+----";
    292   // Each name/value pair is stored in a body part which is preceded by a
    293   // boundary delimiter line. Uses random number generator here to create
    294   // a unique boundary delimiter for form data encoding.
    295   std::string boundary = base::StringPrintf(kMultipartBoundary,
    296                                             base::RandUint64());
    297   // Sets the content MIME type.
    298   post_content->first = kUploadDataMIMEType;
    299   post_content->first += boundary;
    300   // Encodes the post parameters.
    301   std::string* post_data = &post_content->second;
    302   post_data->clear();
    303   for (PostParams::const_iterator param = post_params.begin();
    304        param != post_params.end(); ++param) {
    305     DCHECK(!param->first.empty());
    306     net::AddMultipartValueForUpload(param->first, param->second, boundary,
    307                                     std::string(), post_data);
    308   }
    309   net::AddMultipartFinalDelimiterForUpload(boundary, post_data);
    310   return true;
    311 }
    312 
    313 bool TemplateURLRef::SupportsReplacement() const {
    314   UIThreadSearchTermsData search_terms_data(owner_->profile());
    315   return SupportsReplacementUsingTermsData(search_terms_data);
    316 }
    317 
    318 bool TemplateURLRef::SupportsReplacementUsingTermsData(
    319     const SearchTermsData& search_terms_data) const {
    320   ParseIfNecessaryUsingTermsData(search_terms_data);
    321   return valid_ && supports_replacements_;
    322 }
    323 
    324 std::string TemplateURLRef::ReplaceSearchTerms(
    325     const SearchTermsArgs& search_terms_args,
    326     PostContent* post_content) const {
    327   UIThreadSearchTermsData search_terms_data(owner_->profile());
    328   return ReplaceSearchTermsUsingTermsData(search_terms_args, search_terms_data,
    329                                           post_content);
    330 }
    331 
    332 std::string TemplateURLRef::ReplaceSearchTermsUsingTermsData(
    333     const SearchTermsArgs& search_terms_args,
    334     const SearchTermsData& search_terms_data,
    335     PostContent* post_content) const {
    336   ParseIfNecessaryUsingTermsData(search_terms_data);
    337   if (!valid_)
    338     return std::string();
    339 
    340   std::string url(HandleReplacements(search_terms_args, search_terms_data,
    341                                      post_content));
    342 
    343   GURL gurl(url);
    344   if (!gurl.is_valid())
    345     return url;
    346 
    347   std::vector<std::string> query_params;
    348   if (search_terms_args.append_extra_query_params) {
    349     std::string extra_params(
    350         CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
    351             switches::kExtraSearchQueryParams));
    352     if (!extra_params.empty())
    353       query_params.push_back(extra_params);
    354   }
    355   if (!search_terms_args.suggest_query_params.empty())
    356     query_params.push_back(search_terms_args.suggest_query_params);
    357   if (!gurl.query().empty())
    358     query_params.push_back(gurl.query());
    359 
    360   if (query_params.empty())
    361     return url;
    362 
    363   GURL::Replacements replacements;
    364   std::string query_str = JoinString(query_params, "&");
    365   replacements.SetQueryStr(query_str);
    366   return gurl.ReplaceComponents(replacements).possibly_invalid_spec();
    367 }
    368 
    369 bool TemplateURLRef::IsValid() const {
    370   UIThreadSearchTermsData search_terms_data(owner_->profile());
    371   return IsValidUsingTermsData(search_terms_data);
    372 }
    373 
    374 bool TemplateURLRef::IsValidUsingTermsData(
    375     const SearchTermsData& search_terms_data) const {
    376   ParseIfNecessaryUsingTermsData(search_terms_data);
    377   return valid_;
    378 }
    379 
    380 base::string16 TemplateURLRef::DisplayURL() const {
    381   ParseIfNecessary();
    382   base::string16 result(UTF8ToUTF16(GetURL()));
    383   if (valid_ && !replacements_.empty()) {
    384     ReplaceSubstringsAfterOffset(&result, 0,
    385                                  ASCIIToUTF16(kSearchTermsParameterFull),
    386                                  ASCIIToUTF16(kDisplaySearchTerms));
    387     ReplaceSubstringsAfterOffset(&result, 0,
    388         ASCIIToUTF16(kGoogleUnescapedSearchTermsParameterFull),
    389         ASCIIToUTF16(kDisplayUnescapedSearchTerms));
    390   }
    391   return result;
    392 }
    393 
    394 // static
    395 std::string TemplateURLRef::DisplayURLToURLRef(
    396     const base::string16& display_url) {
    397   base::string16 result = display_url;
    398   ReplaceSubstringsAfterOffset(&result, 0, ASCIIToUTF16(kDisplaySearchTerms),
    399                                ASCIIToUTF16(kSearchTermsParameterFull));
    400   ReplaceSubstringsAfterOffset(
    401       &result, 0,
    402       ASCIIToUTF16(kDisplayUnescapedSearchTerms),
    403       ASCIIToUTF16(kGoogleUnescapedSearchTermsParameterFull));
    404   return UTF16ToUTF8(result);
    405 }
    406 
    407 const std::string& TemplateURLRef::GetHost() const {
    408   ParseIfNecessary();
    409   return host_;
    410 }
    411 
    412 const std::string& TemplateURLRef::GetPath() const {
    413   ParseIfNecessary();
    414   return path_;
    415 }
    416 
    417 const std::string& TemplateURLRef::GetSearchTermKey() const {
    418   ParseIfNecessary();
    419   return search_term_key_;
    420 }
    421 
    422 base::string16 TemplateURLRef::SearchTermToString16(
    423     const std::string& term) const {
    424   const std::vector<std::string>& encodings = owner_->input_encodings();
    425   base::string16 result;
    426 
    427   std::string unescaped = net::UnescapeURLComponent(
    428       term,
    429       net::UnescapeRule::REPLACE_PLUS_WITH_SPACE |
    430       net::UnescapeRule::URL_SPECIAL_CHARS);
    431   for (size_t i = 0; i < encodings.size(); ++i) {
    432     if (base::CodepageToUTF16(unescaped, encodings[i].c_str(),
    433                               base::OnStringConversionError::FAIL, &result))
    434       return result;
    435   }
    436 
    437   // Always fall back on UTF-8 if it works.
    438   if (base::CodepageToUTF16(unescaped, base::kCodepageUTF8,
    439                             base::OnStringConversionError::FAIL, &result))
    440     return result;
    441 
    442   // When nothing worked, just use the escaped text. We have no idea what the
    443   // encoding is. We need to substitute spaces for pluses ourselves since we're
    444   // not sending it through an unescaper.
    445   result = UTF8ToUTF16(term);
    446   std::replace(result.begin(), result.end(), '+', ' ');
    447   return result;
    448 }
    449 
    450 bool TemplateURLRef::HasGoogleBaseURLs() const {
    451   ParseIfNecessary();
    452   for (size_t i = 0; i < replacements_.size(); ++i) {
    453     if ((replacements_[i].type == GOOGLE_BASE_URL) ||
    454         (replacements_[i].type == GOOGLE_BASE_SUGGEST_URL))
    455       return true;
    456   }
    457   return false;
    458 }
    459 
    460 bool TemplateURLRef::ExtractSearchTermsFromURL(
    461     const GURL& url,
    462     base::string16* search_terms,
    463     const SearchTermsData& search_terms_data,
    464     url_parse::Parsed::ComponentType* search_terms_component,
    465     url_parse::Component* search_terms_position) const {
    466   DCHECK(search_terms);
    467   search_terms->clear();
    468 
    469   ParseIfNecessaryUsingTermsData(search_terms_data);
    470 
    471   // We need a search term in the template URL to extract something.
    472   if (search_term_key_.empty())
    473     return false;
    474 
    475   // TODO(beaudoin): Support patterns of the form http://foo/{searchTerms}/
    476   // See crbug.com/153798
    477 
    478   // Fill-in the replacements. We don't care about search terms in the pattern,
    479   // so we use the empty string.
    480   // Currently we assume the search term only shows in URL, not in post params.
    481   GURL pattern(ReplaceSearchTermsUsingTermsData(
    482       SearchTermsArgs(base::string16()), search_terms_data, NULL));
    483   // Host, path and port must match.
    484   if (url.port() != pattern.port() ||
    485       url.host() != host_ ||
    486       url.path() != path_) {
    487     return false;
    488   }
    489 
    490   // Parameter must be present either in the query or the ref.
    491   const std::string& params(
    492       (search_term_key_location_ == url_parse::Parsed::QUERY) ?
    493           url.query() : url.ref());
    494 
    495   url_parse::Component query, key, value;
    496   query.len = static_cast<int>(params.size());
    497   bool key_found = false;
    498   while (url_parse::ExtractQueryKeyValue(params.c_str(), &query, &key,
    499                                          &value)) {
    500     if (key.is_nonempty()) {
    501       if (params.substr(key.begin, key.len) == search_term_key_) {
    502         // Fail if search term key is found twice.
    503         if (key_found) {
    504           search_terms->clear();
    505           return false;
    506         }
    507         key_found = true;
    508         // Extract the search term.
    509         *search_terms = net::UnescapeAndDecodeUTF8URLComponent(
    510             params.substr(value.begin, value.len),
    511             net::UnescapeRule::SPACES |
    512                 net::UnescapeRule::URL_SPECIAL_CHARS |
    513                 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE,
    514             NULL);
    515         if (search_terms_component)
    516           *search_terms_component = search_term_key_location_;
    517         if (search_terms_position)
    518           *search_terms_position = value;
    519       }
    520     }
    521   }
    522   return key_found;
    523 }
    524 
    525 void TemplateURLRef::InvalidateCachedValues() const {
    526   supports_replacements_ = valid_ = parsed_ = false;
    527   host_.clear();
    528   path_.clear();
    529   search_term_key_.clear();
    530   replacements_.clear();
    531   post_params_.clear();
    532 }
    533 
    534 bool TemplateURLRef::ParseParameter(size_t start,
    535                                     size_t end,
    536                                     std::string* url,
    537                                     Replacements* replacements) const {
    538   DCHECK(start != std::string::npos &&
    539          end != std::string::npos && end > start);
    540   size_t length = end - start - 1;
    541   bool optional = false;
    542   if ((*url)[end - 1] == kOptional) {
    543     optional = true;
    544     length--;
    545   }
    546   std::string parameter(url->substr(start + 1, length));
    547   std::string full_parameter(url->substr(start, end - start + 1));
    548   // Remove the parameter from the string.  For parameters who replacement is
    549   // constant and already known, just replace them directly.  For other cases,
    550   // like parameters whose values may change over time, use |replacements|.
    551   url->erase(start, end - start + 1);
    552   if (parameter == kSearchTermsParameter) {
    553     replacements->push_back(Replacement(SEARCH_TERMS, start));
    554   } else if (parameter == kCountParameter) {
    555     if (!optional)
    556       url->insert(start, kDefaultCount);
    557   } else if (parameter == kGoogleAssistedQueryStatsParameter) {
    558     replacements->push_back(Replacement(GOOGLE_ASSISTED_QUERY_STATS, start));
    559   } else if (parameter == kGoogleBaseURLParameter) {
    560     replacements->push_back(Replacement(GOOGLE_BASE_URL, start));
    561   } else if (parameter == kGoogleBaseSuggestURLParameter) {
    562     replacements->push_back(Replacement(GOOGLE_BASE_SUGGEST_URL, start));
    563   } else if (parameter == kGoogleBookmarkBarPinnedParameter) {
    564     replacements->push_back(Replacement(GOOGLE_BOOKMARK_BAR_PINNED, start));
    565   } else if (parameter == kGoogleCurrentPageUrlParameter) {
    566     replacements->push_back(Replacement(GOOGLE_CURRENT_PAGE_URL, start));
    567   } else if (parameter == kGoogleCursorPositionParameter) {
    568     replacements->push_back(Replacement(GOOGLE_CURSOR_POSITION, start));
    569   } else if (parameter == kGoogleImageOriginalHeight) {
    570     replacements->push_back(
    571         Replacement(TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_HEIGHT, start));
    572   } else if (parameter == kGoogleImageOriginalWidth) {
    573     replacements->push_back(
    574         Replacement(TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH, start));
    575   } else if (parameter == kGoogleImageSearchSource) {
    576     url->insert(start, GetGoogleImageSearchSource());
    577   } else if (parameter == kGoogleImageThumbnailParameter) {
    578     replacements->push_back(
    579         Replacement(TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL, start));
    580   } else if (parameter == kGoogleImageURLParameter) {
    581     replacements->push_back(Replacement(TemplateURLRef::GOOGLE_IMAGE_URL,
    582                                         start));
    583   } else if (parameter == kGoogleForceInstantResultsParameter) {
    584     replacements->push_back(Replacement(GOOGLE_FORCE_INSTANT_RESULTS, start));
    585   } else if (parameter == kGoogleInstantExtendedEnabledParameter) {
    586     replacements->push_back(Replacement(GOOGLE_INSTANT_EXTENDED_ENABLED,
    587                                         start));
    588   } else if (parameter == kGoogleInstantExtendedEnabledKey) {
    589     url->insert(start, google_util::kInstantExtendedAPIParam);
    590   } else if (parameter == kGoogleNTPIsThemedParameter) {
    591     replacements->push_back(Replacement(GOOGLE_NTP_IS_THEMED, start));
    592   } else if (parameter == kGoogleOmniboxStartMarginParameter) {
    593     replacements->push_back(Replacement(GOOGLE_OMNIBOX_START_MARGIN, start));
    594   } else if (parameter == kGoogleOriginalQueryForSuggestionParameter) {
    595     replacements->push_back(Replacement(GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION,
    596                                         start));
    597   } else if (parameter == kGooglePageClassificationParameter) {
    598     replacements->push_back(Replacement(GOOGLE_PAGE_CLASSIFICATION, start));
    599   } else if (parameter == kGoogleRLZParameter) {
    600     replacements->push_back(Replacement(GOOGLE_RLZ, start));
    601   } else if (parameter == kGoogleSearchClient) {
    602     replacements->push_back(Replacement(GOOGLE_SEARCH_CLIENT, start));
    603   } else if (parameter == kGoogleSearchFieldtrialParameter) {
    604     replacements->push_back(Replacement(GOOGLE_SEARCH_FIELDTRIAL_GROUP, start));
    605   } else if (parameter == kGoogleSourceIdParameter) {
    606 #if defined(OS_ANDROID)
    607     url->insert(start, "sourceid=chrome-mobile&");
    608 #else
    609     url->insert(start, "sourceid=chrome&");
    610 #endif
    611   } else if (parameter == kGoogleSuggestAPIKeyParameter) {
    612     url->insert(start,
    613                 net::EscapeQueryParamValue(google_apis::GetAPIKey(), false));
    614   } else if (parameter == kGoogleSuggestClient) {
    615     replacements->push_back(Replacement(GOOGLE_SUGGEST_CLIENT, start));
    616   } else if (parameter == kGoogleSuggestRequestId) {
    617     replacements->push_back(Replacement(GOOGLE_SUGGEST_REQUEST_ID, start));
    618   } else if (parameter == kGoogleUnescapedSearchTermsParameter) {
    619     replacements->push_back(Replacement(GOOGLE_UNESCAPED_SEARCH_TERMS, start));
    620   } else if (parameter == kInputEncodingParameter) {
    621     replacements->push_back(Replacement(ENCODING, start));
    622   } else if (parameter == kLanguageParameter) {
    623     replacements->push_back(Replacement(LANGUAGE, start));
    624   } else if (parameter == kOutputEncodingParameter) {
    625     if (!optional)
    626       url->insert(start, kOutputEncodingType);
    627   } else if ((parameter == kStartIndexParameter) ||
    628              (parameter == kStartPageParameter)) {
    629     // We don't support these.
    630     if (!optional)
    631       url->insert(start, "1");
    632   } else if (!prepopulated_) {
    633     // If it's a prepopulated URL, we know that it's safe to remove unknown
    634     // parameters, so just ignore this and return true below. Otherwise it could
    635     // be some garbage but can also be a javascript block. Put it back.
    636     url->insert(start, full_parameter);
    637     return false;
    638   }
    639   return true;
    640 }
    641 
    642 std::string TemplateURLRef::ParseURL(const std::string& url,
    643                                      Replacements* replacements,
    644                                      PostParams* post_params,
    645                                      bool* valid) const {
    646   *valid = false;
    647   std::string parsed_url = url;
    648   for (size_t last = 0; last != std::string::npos; ) {
    649     last = parsed_url.find(kStartParameter, last);
    650     if (last != std::string::npos) {
    651       size_t template_end = parsed_url.find(kEndParameter, last);
    652       if (template_end != std::string::npos) {
    653         // Since we allow Javascript in the URL, {} pairs could be nested. Match
    654         // only leaf pairs with supported parameters.
    655         size_t next_template_start = parsed_url.find(kStartParameter, last + 1);
    656         if (next_template_start == std::string::npos ||
    657             next_template_start > template_end) {
    658           // If successful, ParseParameter erases from the string as such no
    659           // need to update |last|. If failed, move |last| to the end of pair.
    660           if (!ParseParameter(last, template_end, &parsed_url, replacements)) {
    661             // |template_end| + 1 may be beyond the end of the string.
    662             last = template_end;
    663           }
    664         } else {
    665           last = next_template_start;
    666         }
    667       } else {
    668         // Open brace without a closing brace, return.
    669         return std::string();
    670       }
    671     }
    672   }
    673 
    674   // Handles the post parameters.
    675   const std::string& post_params_string = GetPostParamsString();
    676   if (!post_params_string.empty()) {
    677     typedef std::vector<std::string> Strings;
    678     Strings param_list;
    679     base::SplitString(post_params_string, ',', &param_list);
    680 
    681     for (Strings::const_iterator iterator = param_list.begin();
    682          iterator != param_list.end(); ++iterator) {
    683       Strings parts;
    684       // The '=' delimiter is required and the name must be not empty.
    685       base::SplitString(*iterator, '=', &parts);
    686       if ((parts.size() != 2U) || parts[0].empty())
    687         return std::string();
    688 
    689       std::string& value = parts[1];
    690       size_t replacements_size = replacements->size();
    691       if (IsTemplateParameterString(value))
    692         ParseParameter(0, value.length() - 1, &value, replacements);
    693       post_params->push_back(std::make_pair(parts[0], value));
    694       // If there was a replacement added, points its index to last added
    695       // PostParam.
    696       if (replacements->size() > replacements_size) {
    697         DCHECK_EQ(replacements_size + 1, replacements->size());
    698         Replacement* r = &replacements->back();
    699         r->is_post_param = true;
    700         r->index = post_params->size() - 1;
    701       }
    702     }
    703     DCHECK(!post_params->empty());
    704   }
    705 
    706   *valid = true;
    707   return parsed_url;
    708 }
    709 
    710 void TemplateURLRef::ParseIfNecessary() const {
    711   UIThreadSearchTermsData search_terms_data(owner_->profile());
    712   ParseIfNecessaryUsingTermsData(search_terms_data);
    713 }
    714 
    715 void TemplateURLRef::ParseIfNecessaryUsingTermsData(
    716     const SearchTermsData& search_terms_data) const {
    717   if (!parsed_) {
    718     InvalidateCachedValues();
    719     parsed_ = true;
    720     parsed_url_ = ParseURL(GetURL(), &replacements_, &post_params_, &valid_);
    721     supports_replacements_ = false;
    722     if (valid_) {
    723       bool has_only_one_search_term = false;
    724       for (Replacements::const_iterator i = replacements_.begin();
    725            i != replacements_.end(); ++i) {
    726         if ((i->type == SEARCH_TERMS) ||
    727             (i->type == GOOGLE_UNESCAPED_SEARCH_TERMS)) {
    728           if (has_only_one_search_term) {
    729             has_only_one_search_term = false;
    730             break;
    731           }
    732           has_only_one_search_term = true;
    733           supports_replacements_ = true;
    734         }
    735       }
    736       // Only parse the host/key if there is one search term. Technically there
    737       // could be more than one term, but it's uncommon; so we punt.
    738       if (has_only_one_search_term)
    739         ParseHostAndSearchTermKey(search_terms_data);
    740     }
    741   }
    742 }
    743 
    744 void TemplateURLRef::ParseHostAndSearchTermKey(
    745     const SearchTermsData& search_terms_data) const {
    746   std::string url_string(GetURL());
    747   ReplaceSubstringsAfterOffset(&url_string, 0,
    748                                kGoogleBaseURLParameterFull,
    749                                search_terms_data.GoogleBaseURLValue());
    750   ReplaceSubstringsAfterOffset(&url_string, 0,
    751                                kGoogleBaseSuggestURLParameterFull,
    752                                search_terms_data.GoogleBaseSuggestURLValue());
    753 
    754   search_term_key_.clear();
    755   host_.clear();
    756   path_.clear();
    757   search_term_key_location_ = url_parse::Parsed::REF;
    758 
    759   GURL url(url_string);
    760   if (!url.is_valid())
    761     return;
    762 
    763   std::string query_key = FindSearchTermsKey(url.query());
    764   std::string ref_key = FindSearchTermsKey(url.ref());
    765   if (query_key.empty() == ref_key.empty())
    766     return;  // No key or multiple keys found.  We only handle having one key.
    767   search_term_key_ = query_key.empty() ? ref_key : query_key;
    768   search_term_key_location_ = query_key.empty() ?
    769       url_parse::Parsed::REF : url_parse::Parsed::QUERY;
    770   host_ = url.host();
    771   path_ = url.path();
    772 }
    773 
    774 void TemplateURLRef::HandleReplacement(const std::string& name,
    775                                        const std::string& value,
    776                                        const Replacement& replacement,
    777                                        std::string* url) const {
    778   size_t pos = replacement.index;
    779   if (replacement.is_post_param) {
    780     DCHECK_LT(pos, post_params_.size());
    781     DCHECK(!post_params_[pos].first.empty());
    782     post_params_[pos].second = value;
    783   } else {
    784     url->insert(pos, name.empty() ? value : (name + "=" + value + "&"));
    785   }
    786 }
    787 
    788 std::string TemplateURLRef::HandleReplacements(
    789     const SearchTermsArgs& search_terms_args,
    790     const SearchTermsData& search_terms_data,
    791     PostContent* post_content) const {
    792   if (replacements_.empty()) {
    793     if (!post_params_.empty())
    794       EncodeFormData(post_params_, post_content);
    795     return parsed_url_;
    796   }
    797 
    798   // Determine if the search terms are in the query or before. We're escaping
    799   // space as '+' in the former case and as '%20' in the latter case.
    800   bool is_in_query = true;
    801   for (Replacements::iterator i = replacements_.begin();
    802        i != replacements_.end(); ++i) {
    803     if (i->type == SEARCH_TERMS) {
    804       base::string16::size_type query_start = parsed_url_.find('?');
    805       is_in_query = query_start != base::string16::npos &&
    806           (static_cast<base::string16::size_type>(i->index) > query_start);
    807       break;
    808     }
    809   }
    810 
    811   std::string input_encoding;
    812   base::string16 encoded_terms;
    813   base::string16 encoded_original_query;
    814   owner_->EncodeSearchTerms(search_terms_args, is_in_query, &input_encoding,
    815                             &encoded_terms, &encoded_original_query);
    816 
    817   std::string url = parsed_url_;
    818 
    819   // replacements_ is ordered in ascending order, as such we need to iterate
    820   // from the back.
    821   for (Replacements::reverse_iterator i = replacements_.rbegin();
    822        i != replacements_.rend(); ++i) {
    823     switch (i->type) {
    824       case ENCODING:
    825         HandleReplacement(std::string(), input_encoding, *i, &url);
    826         break;
    827 
    828       case GOOGLE_ASSISTED_QUERY_STATS:
    829         DCHECK(!i->is_post_param);
    830         if (!search_terms_args.assisted_query_stats.empty()) {
    831           // Get the base URL without substituting AQS to avoid infinite
    832           // recursion.  We need the URL to find out if it meets all
    833           // AQS requirements (e.g. HTTPS protocol check).
    834           // See TemplateURLRef::SearchTermsArgs for more details.
    835           SearchTermsArgs search_terms_args_without_aqs(search_terms_args);
    836           search_terms_args_without_aqs.assisted_query_stats.clear();
    837           GURL base_url(ReplaceSearchTermsUsingTermsData(
    838               search_terms_args_without_aqs, search_terms_data, NULL));
    839           if (base_url.SchemeIs(content::kHttpsScheme)) {
    840             HandleReplacement(
    841                 "aqs", search_terms_args.assisted_query_stats, *i, &url);
    842           }
    843         }
    844         break;
    845 
    846       case GOOGLE_BASE_URL:
    847         DCHECK(!i->is_post_param);
    848         HandleReplacement(
    849             std::string(), search_terms_data.GoogleBaseURLValue(), *i, &url);
    850         break;
    851 
    852       case GOOGLE_BASE_SUGGEST_URL:
    853         DCHECK(!i->is_post_param);
    854         HandleReplacement(
    855             std::string(), search_terms_data.GoogleBaseSuggestURLValue(), *i,
    856             &url);
    857         break;
    858 
    859       case GOOGLE_BOOKMARK_BAR_PINNED:
    860         if (showing_search_terms_) {
    861           // Log whether the bookmark bar is pinned when the user is seeing
    862           // InstantExtended on the SRP.
    863           DCHECK(!i->is_post_param);
    864           HandleReplacement(
    865               "bmbp", search_terms_args.bookmark_bar_pinned ? "1" : "0", *i,
    866               &url);
    867         }
    868         break;
    869 
    870       case GOOGLE_CURRENT_PAGE_URL:
    871         DCHECK(!i->is_post_param);
    872         if (!search_terms_args.current_page_url.empty()) {
    873           const std::string& escaped_current_page_url =
    874               net::EscapeQueryParamValue(search_terms_args.current_page_url,
    875                                          true);
    876           HandleReplacement("url", escaped_current_page_url, *i, &url);
    877         }
    878         break;
    879 
    880       case GOOGLE_CURSOR_POSITION:
    881         DCHECK(!i->is_post_param);
    882         if (search_terms_args.cursor_position != base::string16::npos)
    883           HandleReplacement(
    884               "cp",
    885               base::StringPrintf("%" PRIuS, search_terms_args.cursor_position),
    886               *i,
    887               &url);
    888         break;
    889 
    890       case GOOGLE_FORCE_INSTANT_RESULTS:
    891         DCHECK(!i->is_post_param);
    892         HandleReplacement(std::string(),
    893                           search_terms_data.ForceInstantResultsParam(
    894                               search_terms_args.force_instant_results),
    895                           *i,
    896                           &url);
    897         break;
    898 
    899       case GOOGLE_INSTANT_EXTENDED_ENABLED:
    900         DCHECK(!i->is_post_param);
    901         HandleReplacement(
    902             std::string(), search_terms_data.InstantExtendedEnabledParam(), *i,
    903             &url);
    904         break;
    905 
    906       case GOOGLE_NTP_IS_THEMED:
    907         DCHECK(!i->is_post_param);
    908         HandleReplacement(
    909             std::string(), search_terms_data.NTPIsThemedParam(), *i, &url);
    910         break;
    911 
    912       case GOOGLE_OMNIBOX_START_MARGIN:
    913         DCHECK(!i->is_post_param);
    914         if (search_terms_args.omnibox_start_margin >= 0) {
    915           HandleReplacement(
    916               "es_sm",
    917               base::IntToString(search_terms_args.omnibox_start_margin),
    918               *i,
    919               &url);
    920         }
    921         break;
    922 
    923       case GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION:
    924         DCHECK(!i->is_post_param);
    925         if (search_terms_args.accepted_suggestion >= 0 ||
    926             !search_terms_args.assisted_query_stats.empty()) {
    927           HandleReplacement(
    928               "oq", UTF16ToUTF8(encoded_original_query), *i, &url);
    929         }
    930         break;
    931 
    932       case GOOGLE_PAGE_CLASSIFICATION:
    933         if (search_terms_args.page_classification !=
    934             AutocompleteInput::INVALID_SPEC) {
    935           HandleReplacement(
    936               "pgcl", base::IntToString(search_terms_args.page_classification),
    937               *i, &url);
    938         }
    939         break;
    940 
    941       case GOOGLE_RLZ: {
    942         DCHECK(!i->is_post_param);
    943         // On platforms that don't have RLZ, we still want this branch
    944         // to happen so that we replace the RLZ template with the
    945         // empty string.  (If we don't handle this case, we hit a
    946         // NOTREACHED below.)
    947         base::string16 rlz_string = search_terms_data.GetRlzParameterValue();
    948         if (!rlz_string.empty()) {
    949           HandleReplacement("rlz", UTF16ToUTF8(rlz_string), *i, &url);
    950         }
    951         break;
    952       }
    953 
    954       case GOOGLE_SEARCH_CLIENT: {
    955         DCHECK(!i->is_post_param);
    956         std::string client = search_terms_data.GetSearchClient();
    957         if (!client.empty())
    958           HandleReplacement("client", client, *i, &url);
    959         break;
    960       }
    961 
    962       case GOOGLE_SEARCH_FIELDTRIAL_GROUP:
    963         // We are not currently running any fieldtrials that modulate the search
    964         // url.  If we do, then we'd have some conditional insert such as:
    965         // url.insert(i->index, used_www ? "gcx=w&" : "gcx=c&");
    966         break;
    967 
    968       case GOOGLE_SUGGEST_CLIENT:
    969         HandleReplacement(
    970             std::string(), search_terms_data.GetSuggestClient(), *i, &url);
    971         break;
    972 
    973       case GOOGLE_SUGGEST_REQUEST_ID:
    974         HandleReplacement(
    975             std::string(), search_terms_data.GetSuggestRequestIdentifier(), *i,
    976             &url);
    977         break;
    978 
    979       case GOOGLE_UNESCAPED_SEARCH_TERMS: {
    980         std::string unescaped_terms;
    981         base::UTF16ToCodepage(search_terms_args.search_terms,
    982                               input_encoding.c_str(),
    983                               base::OnStringConversionError::SKIP,
    984                               &unescaped_terms);
    985         HandleReplacement(std::string(), unescaped_terms, *i, &url);
    986         break;
    987       }
    988 
    989       case LANGUAGE:
    990         HandleReplacement(
    991             std::string(), search_terms_data.GetApplicationLocale(), *i, &url);
    992         break;
    993 
    994       case SEARCH_TERMS:
    995         HandleReplacement(std::string(), UTF16ToUTF8(encoded_terms), *i, &url);
    996         break;
    997 
    998       case GOOGLE_IMAGE_THUMBNAIL:
    999         HandleReplacement(
   1000             std::string(), search_terms_args.image_thumbnail_content, *i, &url);
   1001         break;
   1002 
   1003       case GOOGLE_IMAGE_URL:
   1004         if (search_terms_args.image_url.is_valid()) {
   1005           HandleReplacement(
   1006               std::string(), search_terms_args.image_url.spec(), *i, &url);
   1007         }
   1008         break;
   1009 
   1010       case GOOGLE_IMAGE_ORIGINAL_WIDTH:
   1011         if (!search_terms_args.image_original_size.IsEmpty()) {
   1012           HandleReplacement(
   1013               std::string(),
   1014               base::IntToString(search_terms_args.image_original_size.width()),
   1015               *i, &url);
   1016         }
   1017         break;
   1018 
   1019       case GOOGLE_IMAGE_ORIGINAL_HEIGHT:
   1020         if (!search_terms_args.image_original_size.IsEmpty()) {
   1021           HandleReplacement(
   1022               std::string(),
   1023               base::IntToString(search_terms_args.image_original_size.height()),
   1024               *i, &url);
   1025         }
   1026         break;
   1027 
   1028       default:
   1029         NOTREACHED();
   1030         break;
   1031     }
   1032   }
   1033 
   1034   if (!post_params_.empty())
   1035     EncodeFormData(post_params_, post_content);
   1036 
   1037   return url;
   1038 }
   1039 
   1040 
   1041 // TemplateURLData ------------------------------------------------------------
   1042 
   1043 TemplateURLData::TemplateURLData()
   1044     : show_in_default_list(false),
   1045       safe_for_autoreplace(false),
   1046       id(0),
   1047       date_created(base::Time::Now()),
   1048       last_modified(base::Time::Now()),
   1049       created_by_policy(false),
   1050       usage_count(0),
   1051       prepopulate_id(0),
   1052       sync_guid(base::GenerateGUID()),
   1053       keyword_(ASCIIToUTF16("dummy")),
   1054       url_("x") {
   1055 }
   1056 
   1057 TemplateURLData::~TemplateURLData() {
   1058 }
   1059 
   1060 void TemplateURLData::SetKeyword(const base::string16& keyword) {
   1061   DCHECK(!keyword.empty());
   1062 
   1063   // Case sensitive keyword matching is confusing. As such, we force all
   1064   // keywords to be lower case.
   1065   keyword_ = base::i18n::ToLower(keyword);
   1066 }
   1067 
   1068 void TemplateURLData::SetURL(const std::string& url) {
   1069   DCHECK(!url.empty());
   1070   url_ = url;
   1071 }
   1072 
   1073 
   1074 // TemplateURL ----------------------------------------------------------------
   1075 
   1076 TemplateURL::TemplateURL(Profile* profile, const TemplateURLData& data)
   1077     : profile_(profile),
   1078       data_(data),
   1079       url_ref_(this, TemplateURLRef::SEARCH),
   1080       suggestions_url_ref_(this,
   1081                            TemplateURLRef::SUGGEST),
   1082       instant_url_ref_(this,
   1083                        TemplateURLRef::INSTANT),
   1084       image_url_ref_(this, TemplateURLRef::IMAGE),
   1085       new_tab_url_ref_(this, TemplateURLRef::NEW_TAB) {
   1086   SetPrepopulateId(data_.prepopulate_id);
   1087 
   1088   if (data_.search_terms_replacement_key ==
   1089       kGoogleInstantExtendedEnabledKeyFull) {
   1090     data_.search_terms_replacement_key = google_util::kInstantExtendedAPIParam;
   1091   }
   1092 }
   1093 
   1094 TemplateURL::~TemplateURL() {
   1095 }
   1096 
   1097 // static
   1098 GURL TemplateURL::GenerateFaviconURL(const GURL& url) {
   1099   DCHECK(url.is_valid());
   1100   GURL::Replacements rep;
   1101 
   1102   const char favicon_path[] = "/favicon.ico";
   1103   int favicon_path_len = arraysize(favicon_path) - 1;
   1104 
   1105   rep.SetPath(favicon_path, url_parse::Component(0, favicon_path_len));
   1106   rep.ClearUsername();
   1107   rep.ClearPassword();
   1108   rep.ClearQuery();
   1109   rep.ClearRef();
   1110   return url.ReplaceComponents(rep);
   1111 }
   1112 
   1113 base::string16 TemplateURL::AdjustedShortNameForLocaleDirection() const {
   1114   base::string16 bidi_safe_short_name = data_.short_name;
   1115   base::i18n::AdjustStringForLocaleDirection(&bidi_safe_short_name);
   1116   return bidi_safe_short_name;
   1117 }
   1118 
   1119 bool TemplateURL::ShowInDefaultList() const {
   1120   return data_.show_in_default_list && url_ref_.SupportsReplacement();
   1121 }
   1122 
   1123 bool TemplateURL::SupportsReplacement() const {
   1124   UIThreadSearchTermsData search_terms_data(profile_);
   1125   return SupportsReplacementUsingTermsData(search_terms_data);
   1126 }
   1127 
   1128 bool TemplateURL::SupportsReplacementUsingTermsData(
   1129     const SearchTermsData& search_terms_data) const {
   1130   return url_ref_.SupportsReplacementUsingTermsData(search_terms_data);
   1131 }
   1132 
   1133 bool TemplateURL::IsGoogleSearchURLWithReplaceableKeyword() const {
   1134   return (GetType() == NORMAL) && url_ref_.HasGoogleBaseURLs() &&
   1135       google_util::IsGoogleHostname(UTF16ToUTF8(data_.keyword()),
   1136                                     google_util::DISALLOW_SUBDOMAIN);
   1137 }
   1138 
   1139 bool TemplateURL::HasSameKeywordAs(const TemplateURL& other) const {
   1140   return (data_.keyword() == other.data_.keyword()) ||
   1141       (IsGoogleSearchURLWithReplaceableKeyword() &&
   1142        other.IsGoogleSearchURLWithReplaceableKeyword());
   1143 }
   1144 
   1145 TemplateURL::Type TemplateURL::GetType() const {
   1146   if (extension_info_)
   1147     return NORMAL_CONTROLLED_BY_EXTENSION;
   1148   return GURL(data_.url()).SchemeIs(extensions::kExtensionScheme) ?
   1149       OMNIBOX_API_EXTENSION : NORMAL;
   1150 }
   1151 
   1152 std::string TemplateURL::GetExtensionId() const {
   1153   DCHECK_NE(NORMAL, GetType());
   1154   return extension_info_ ?
   1155       extension_info_->extension_id : GURL(data_.url()).host();
   1156 }
   1157 
   1158 size_t TemplateURL::URLCount() const {
   1159   // Add 1 for the regular search URL.
   1160   return data_.alternate_urls.size() + 1;
   1161 }
   1162 
   1163 const std::string& TemplateURL::GetURL(size_t index) const {
   1164   DCHECK_LT(index, URLCount());
   1165 
   1166   return (index < data_.alternate_urls.size()) ?
   1167       data_.alternate_urls[index] : url();
   1168 }
   1169 
   1170 bool TemplateURL::ExtractSearchTermsFromURL(
   1171     const GURL& url,
   1172     base::string16* search_terms) {
   1173   UIThreadSearchTermsData search_terms_data(profile_);
   1174   return ExtractSearchTermsFromURLUsingTermsData(url, search_terms,
   1175                                                  search_terms_data);
   1176 }
   1177 
   1178 bool TemplateURL::ExtractSearchTermsFromURLUsingTermsData(
   1179     const GURL& url,
   1180     base::string16* search_terms,
   1181     const SearchTermsData& search_terms_data) {
   1182   return FindSearchTermsInURL(url, search_terms_data, search_terms, NULL, NULL);
   1183 }
   1184 
   1185 
   1186 bool TemplateURL::IsSearchURL(const GURL& url) {
   1187   UIThreadSearchTermsData search_terms_data(profile_);
   1188   return IsSearchURLUsingTermsData(url, search_terms_data);
   1189 }
   1190 
   1191 bool TemplateURL::IsSearchURLUsingTermsData(
   1192     const GURL& url,
   1193     const SearchTermsData& search_terms_data) {
   1194   base::string16 search_terms;
   1195   return ExtractSearchTermsFromURLUsingTermsData(
   1196       url, &search_terms, search_terms_data) && !search_terms.empty();
   1197 }
   1198 
   1199 bool TemplateURL::HasSearchTermsReplacementKey(const GURL& url) const {
   1200   // Look for the key both in the query and the ref.
   1201   std::string params[] = {url.query(), url.ref()};
   1202 
   1203   for (int i = 0; i < 2; ++i) {
   1204     url_parse::Component query, key, value;
   1205     query.len = static_cast<int>(params[i].size());
   1206     while (url_parse::ExtractQueryKeyValue(params[i].c_str(), &query, &key,
   1207                                            &value)) {
   1208       if (key.is_nonempty() &&
   1209           params[i].substr(key.begin, key.len) ==
   1210               search_terms_replacement_key()) {
   1211         return true;
   1212       }
   1213     }
   1214   }
   1215   return false;
   1216 }
   1217 
   1218 bool TemplateURL::ReplaceSearchTermsInURL(
   1219     const GURL& url,
   1220     const TemplateURLRef::SearchTermsArgs& search_terms_args,
   1221     GURL* result) {
   1222   UIThreadSearchTermsData search_terms_data(profile_);
   1223   // TODO(beaudoin): Use AQS from |search_terms_args| too.
   1224   url_parse::Parsed::ComponentType search_term_component;
   1225   url_parse::Component search_terms_position;
   1226   base::string16 search_terms;
   1227   if (!FindSearchTermsInURL(url, search_terms_data, &search_terms,
   1228                             &search_term_component, &search_terms_position)) {
   1229     return false;
   1230   }
   1231   DCHECK(search_terms_position.is_nonempty());
   1232 
   1233   // FindSearchTermsInURL only returns true for search terms in the query or
   1234   // ref, so we can call EncodeSearchTerm with |is_in_query| = true, since query
   1235   // and ref are encoded in the same way.
   1236   std::string input_encoding;
   1237   base::string16 encoded_terms;
   1238   base::string16 encoded_original_query;
   1239   EncodeSearchTerms(search_terms_args, true, &input_encoding,
   1240                     &encoded_terms, &encoded_original_query);
   1241 
   1242   std::string old_params((search_term_component == url_parse::Parsed::REF) ?
   1243       url.ref() : url.query());
   1244   std::string new_params(old_params, 0, search_terms_position.begin);
   1245   new_params += UTF16ToUTF8(search_terms_args.search_terms);
   1246   new_params += old_params.substr(search_terms_position.end());
   1247   url_canon::StdStringReplacements<std::string> replacements;
   1248   if (search_term_component == url_parse::Parsed::REF)
   1249     replacements.SetRefStr(new_params);
   1250   else
   1251     replacements.SetQueryStr(new_params);
   1252   *result = url.ReplaceComponents(replacements);
   1253   return true;
   1254 }
   1255 
   1256 void TemplateURL::EncodeSearchTerms(
   1257     const TemplateURLRef::SearchTermsArgs& search_terms_args,
   1258     bool is_in_query,
   1259     std::string* input_encoding,
   1260     base::string16* encoded_terms,
   1261     base::string16* encoded_original_query) const {
   1262 
   1263   std::vector<std::string> encodings(input_encodings());
   1264   if (std::find(encodings.begin(), encodings.end(), "UTF-8") == encodings.end())
   1265     encodings.push_back("UTF-8");
   1266   for (std::vector<std::string>::const_iterator i(encodings.begin());
   1267        i != encodings.end(); ++i) {
   1268     if (TryEncoding(search_terms_args.search_terms,
   1269                     search_terms_args.original_query, i->c_str(),
   1270                     is_in_query, encoded_terms, encoded_original_query)) {
   1271       *input_encoding = *i;
   1272       return;
   1273     }
   1274   }
   1275   NOTREACHED();
   1276 }
   1277 
   1278 void TemplateURL::CopyFrom(const TemplateURL& other) {
   1279   if (this == &other)
   1280     return;
   1281 
   1282   profile_ = other.profile_;
   1283   data_ = other.data_;
   1284   url_ref_.InvalidateCachedValues();
   1285   suggestions_url_ref_.InvalidateCachedValues();
   1286   instant_url_ref_.InvalidateCachedValues();
   1287   SetPrepopulateId(other.data_.prepopulate_id);
   1288 }
   1289 
   1290 void TemplateURL::SetURL(const std::string& url) {
   1291   data_.SetURL(url);
   1292   url_ref_.InvalidateCachedValues();
   1293 }
   1294 
   1295 void TemplateURL::SetPrepopulateId(int id) {
   1296   data_.prepopulate_id = id;
   1297   const bool prepopulated = id > 0;
   1298   url_ref_.prepopulated_ = prepopulated;
   1299   suggestions_url_ref_.prepopulated_ = prepopulated;
   1300   instant_url_ref_.prepopulated_ = prepopulated;
   1301 }
   1302 
   1303 void TemplateURL::ResetKeywordIfNecessary(bool force) {
   1304   if (IsGoogleSearchURLWithReplaceableKeyword() || force) {
   1305     DCHECK(GetType() != OMNIBOX_API_EXTENSION);
   1306     GURL url(TemplateURLService::GenerateSearchURL(this));
   1307     if (url.is_valid())
   1308       data_.SetKeyword(TemplateURLService::GenerateKeyword(url));
   1309   }
   1310 }
   1311 
   1312 bool TemplateURL::FindSearchTermsInURL(
   1313     const GURL& url,
   1314     const SearchTermsData& search_terms_data,
   1315     base::string16* search_terms,
   1316     url_parse::Parsed::ComponentType* search_term_component,
   1317     url_parse::Component* search_terms_position) {
   1318   DCHECK(search_terms);
   1319   search_terms->clear();
   1320 
   1321   // Try to match with every pattern.
   1322   for (size_t i = 0; i < URLCount(); ++i) {
   1323     TemplateURLRef ref(this, i);
   1324     if (ref.ExtractSearchTermsFromURL(url, search_terms, search_terms_data,
   1325         search_term_component, search_terms_position)) {
   1326       // If ExtractSearchTermsFromURL() returns true and |search_terms| is empty
   1327       // it means the pattern matched but no search terms were present. In this
   1328       // case we fail immediately without looking for matches in subsequent
   1329       // patterns. This means that given patterns
   1330       //    [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ],
   1331       // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would
   1332       // return false. This is important for at least Google, where such URLs
   1333       // are invalid.
   1334       return !search_terms->empty();
   1335     }
   1336   }
   1337   return false;
   1338 }
   1339