Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / search_engines / template_url.cc
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 = base::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 = base::UTF8ToUTF16(
143       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(base::UTF8ToUTF16(GetURL()));
383   if (valid_ && !replacements_.empty()) {
384     ReplaceSubstringsAfterOffset(&result, 0,
385                                  base::ASCIIToUTF16(kSearchTermsParameterFull),
386                                  base::ASCIIToUTF16(kDisplaySearchTerms));
387     ReplaceSubstringsAfterOffset(&result, 0,
388         base::ASCIIToUTF16(kGoogleUnescapedSearchTermsParameterFull),
389         base::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,
399                                base::ASCIIToUTF16(kDisplaySearchTerms),
400                                base::ASCIIToUTF16(kSearchTermsParameterFull));
401   ReplaceSubstringsAfterOffset(
402       &result, 0,
403       base::ASCIIToUTF16(kDisplayUnescapedSearchTerms),
404       base::ASCIIToUTF16(kGoogleUnescapedSearchTermsParameterFull));
405   return base::UTF16ToUTF8(result);
406 }
407
408 const std::string& TemplateURLRef::GetHost() const {
409   ParseIfNecessary();
410   return host_;
411 }
412
413 const std::string& TemplateURLRef::GetPath() const {
414   ParseIfNecessary();
415   return path_;
416 }
417
418 const std::string& TemplateURLRef::GetSearchTermKey() const {
419   ParseIfNecessary();
420   return search_term_key_;
421 }
422
423 base::string16 TemplateURLRef::SearchTermToString16(
424     const std::string& term) const {
425   const std::vector<std::string>& encodings = owner_->input_encodings();
426   base::string16 result;
427
428   std::string unescaped = net::UnescapeURLComponent(
429       term,
430       net::UnescapeRule::REPLACE_PLUS_WITH_SPACE |
431       net::UnescapeRule::URL_SPECIAL_CHARS);
432   for (size_t i = 0; i < encodings.size(); ++i) {
433     if (base::CodepageToUTF16(unescaped, encodings[i].c_str(),
434                               base::OnStringConversionError::FAIL, &result))
435       return result;
436   }
437
438   // Always fall back on UTF-8 if it works.
439   if (base::CodepageToUTF16(unescaped, base::kCodepageUTF8,
440                             base::OnStringConversionError::FAIL, &result))
441     return result;
442
443   // When nothing worked, just use the escaped text. We have no idea what the
444   // encoding is. We need to substitute spaces for pluses ourselves since we're
445   // not sending it through an unescaper.
446   result = base::UTF8ToUTF16(term);
447   std::replace(result.begin(), result.end(), '+', ' ');
448   return result;
449 }
450
451 bool TemplateURLRef::HasGoogleBaseURLs() const {
452   ParseIfNecessary();
453   for (size_t i = 0; i < replacements_.size(); ++i) {
454     if ((replacements_[i].type == GOOGLE_BASE_URL) ||
455         (replacements_[i].type == GOOGLE_BASE_SUGGEST_URL))
456       return true;
457   }
458   return false;
459 }
460
461 bool TemplateURLRef::ExtractSearchTermsFromURL(
462     const GURL& url,
463     base::string16* search_terms,
464     const SearchTermsData& search_terms_data,
465     url_parse::Parsed::ComponentType* search_terms_component,
466     url_parse::Component* search_terms_position) const {
467   DCHECK(search_terms);
468   search_terms->clear();
469
470   ParseIfNecessaryUsingTermsData(search_terms_data);
471
472   // We need a search term in the template URL to extract something.
473   if (search_term_key_.empty())
474     return false;
475
476   // TODO(beaudoin): Support patterns of the form http://foo/{searchTerms}/
477   // See crbug.com/153798
478
479   // Fill-in the replacements. We don't care about search terms in the pattern,
480   // so we use the empty string.
481   // Currently we assume the search term only shows in URL, not in post params.
482   GURL pattern(ReplaceSearchTermsUsingTermsData(
483       SearchTermsArgs(base::string16()), search_terms_data, NULL));
484   // Host, path and port must match.
485   if (url.port() != pattern.port() ||
486       url.host() != host_ ||
487       url.path() != path_) {
488     return false;
489   }
490
491   // Parameter must be present either in the query or the ref.
492   const std::string& params(
493       (search_term_key_location_ == url_parse::Parsed::QUERY) ?
494           url.query() : url.ref());
495
496   url_parse::Component query, key, value;
497   query.len = static_cast<int>(params.size());
498   bool key_found = false;
499   while (url_parse::ExtractQueryKeyValue(params.c_str(), &query, &key,
500                                          &value)) {
501     if (key.is_nonempty()) {
502       if (params.substr(key.begin, key.len) == search_term_key_) {
503         // Fail if search term key is found twice.
504         if (key_found) {
505           search_terms->clear();
506           return false;
507         }
508         key_found = true;
509         // Extract the search term.
510         *search_terms = net::UnescapeAndDecodeUTF8URLComponent(
511             params.substr(value.begin, value.len),
512             net::UnescapeRule::SPACES |
513                 net::UnescapeRule::URL_SPECIAL_CHARS |
514                 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE,
515             NULL);
516         if (search_terms_component)
517           *search_terms_component = search_term_key_location_;
518         if (search_terms_position)
519           *search_terms_position = value;
520       }
521     }
522   }
523   return key_found;
524 }
525
526 void TemplateURLRef::InvalidateCachedValues() const {
527   supports_replacements_ = valid_ = parsed_ = false;
528   host_.clear();
529   path_.clear();
530   search_term_key_.clear();
531   replacements_.clear();
532   post_params_.clear();
533 }
534
535 bool TemplateURLRef::ParseParameter(size_t start,
536                                     size_t end,
537                                     std::string* url,
538                                     Replacements* replacements) const {
539   DCHECK(start != std::string::npos &&
540          end != std::string::npos && end > start);
541   size_t length = end - start - 1;
542   bool optional = false;
543   if ((*url)[end - 1] == kOptional) {
544     optional = true;
545     length--;
546   }
547   std::string parameter(url->substr(start + 1, length));
548   std::string full_parameter(url->substr(start, end - start + 1));
549   // Remove the parameter from the string.  For parameters who replacement is
550   // constant and already known, just replace them directly.  For other cases,
551   // like parameters whose values may change over time, use |replacements|.
552   url->erase(start, end - start + 1);
553   if (parameter == kSearchTermsParameter) {
554     replacements->push_back(Replacement(SEARCH_TERMS, start));
555   } else if (parameter == kCountParameter) {
556     if (!optional)
557       url->insert(start, kDefaultCount);
558   } else if (parameter == kGoogleAssistedQueryStatsParameter) {
559     replacements->push_back(Replacement(GOOGLE_ASSISTED_QUERY_STATS, start));
560   } else if (parameter == kGoogleBaseURLParameter) {
561     replacements->push_back(Replacement(GOOGLE_BASE_URL, start));
562   } else if (parameter == kGoogleBaseSuggestURLParameter) {
563     replacements->push_back(Replacement(GOOGLE_BASE_SUGGEST_URL, start));
564   } else if (parameter == kGoogleBookmarkBarPinnedParameter) {
565     replacements->push_back(Replacement(GOOGLE_BOOKMARK_BAR_PINNED, start));
566   } else if (parameter == kGoogleCurrentPageUrlParameter) {
567     replacements->push_back(Replacement(GOOGLE_CURRENT_PAGE_URL, start));
568   } else if (parameter == kGoogleCursorPositionParameter) {
569     replacements->push_back(Replacement(GOOGLE_CURSOR_POSITION, start));
570   } else if (parameter == kGoogleImageOriginalHeight) {
571     replacements->push_back(
572         Replacement(TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_HEIGHT, start));
573   } else if (parameter == kGoogleImageOriginalWidth) {
574     replacements->push_back(
575         Replacement(TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH, start));
576   } else if (parameter == kGoogleImageSearchSource) {
577     url->insert(start, GetGoogleImageSearchSource());
578   } else if (parameter == kGoogleImageThumbnailParameter) {
579     replacements->push_back(
580         Replacement(TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL, start));
581   } else if (parameter == kGoogleImageURLParameter) {
582     replacements->push_back(Replacement(TemplateURLRef::GOOGLE_IMAGE_URL,
583                                         start));
584   } else if (parameter == kGoogleForceInstantResultsParameter) {
585     replacements->push_back(Replacement(GOOGLE_FORCE_INSTANT_RESULTS, start));
586   } else if (parameter == kGoogleInstantExtendedEnabledParameter) {
587     replacements->push_back(Replacement(GOOGLE_INSTANT_EXTENDED_ENABLED,
588                                         start));
589   } else if (parameter == kGoogleInstantExtendedEnabledKey) {
590     url->insert(start, google_util::kInstantExtendedAPIParam);
591   } else if (parameter == kGoogleNTPIsThemedParameter) {
592     replacements->push_back(Replacement(GOOGLE_NTP_IS_THEMED, start));
593   } else if (parameter == kGoogleOmniboxStartMarginParameter) {
594     replacements->push_back(Replacement(GOOGLE_OMNIBOX_START_MARGIN, start));
595   } else if (parameter == kGoogleOriginalQueryForSuggestionParameter) {
596     replacements->push_back(Replacement(GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION,
597                                         start));
598   } else if (parameter == kGooglePageClassificationParameter) {
599     replacements->push_back(Replacement(GOOGLE_PAGE_CLASSIFICATION, start));
600   } else if (parameter == kGoogleRLZParameter) {
601     replacements->push_back(Replacement(GOOGLE_RLZ, start));
602   } else if (parameter == kGoogleSearchClient) {
603     replacements->push_back(Replacement(GOOGLE_SEARCH_CLIENT, start));
604   } else if (parameter == kGoogleSearchFieldtrialParameter) {
605     replacements->push_back(Replacement(GOOGLE_SEARCH_FIELDTRIAL_GROUP, start));
606   } else if (parameter == kGoogleSourceIdParameter) {
607 #if defined(OS_ANDROID)
608     url->insert(start, "sourceid=chrome-mobile&");
609 #else
610     url->insert(start, "sourceid=chrome&");
611 #endif
612   } else if (parameter == kGoogleSuggestAPIKeyParameter) {
613     url->insert(start,
614                 net::EscapeQueryParamValue(google_apis::GetAPIKey(), false));
615   } else if (parameter == kGoogleSuggestClient) {
616     replacements->push_back(Replacement(GOOGLE_SUGGEST_CLIENT, start));
617   } else if (parameter == kGoogleSuggestRequestId) {
618     replacements->push_back(Replacement(GOOGLE_SUGGEST_REQUEST_ID, start));
619   } else if (parameter == kGoogleUnescapedSearchTermsParameter) {
620     replacements->push_back(Replacement(GOOGLE_UNESCAPED_SEARCH_TERMS, start));
621   } else if (parameter == kInputEncodingParameter) {
622     replacements->push_back(Replacement(ENCODING, start));
623   } else if (parameter == kLanguageParameter) {
624     replacements->push_back(Replacement(LANGUAGE, start));
625   } else if (parameter == kOutputEncodingParameter) {
626     if (!optional)
627       url->insert(start, kOutputEncodingType);
628   } else if ((parameter == kStartIndexParameter) ||
629              (parameter == kStartPageParameter)) {
630     // We don't support these.
631     if (!optional)
632       url->insert(start, "1");
633   } else if (!prepopulated_) {
634     // If it's a prepopulated URL, we know that it's safe to remove unknown
635     // parameters, so just ignore this and return true below. Otherwise it could
636     // be some garbage but can also be a javascript block. Put it back.
637     url->insert(start, full_parameter);
638     return false;
639   }
640   return true;
641 }
642
643 std::string TemplateURLRef::ParseURL(const std::string& url,
644                                      Replacements* replacements,
645                                      PostParams* post_params,
646                                      bool* valid) const {
647   *valid = false;
648   std::string parsed_url = url;
649   for (size_t last = 0; last != std::string::npos; ) {
650     last = parsed_url.find(kStartParameter, last);
651     if (last != std::string::npos) {
652       size_t template_end = parsed_url.find(kEndParameter, last);
653       if (template_end != std::string::npos) {
654         // Since we allow Javascript in the URL, {} pairs could be nested. Match
655         // only leaf pairs with supported parameters.
656         size_t next_template_start = parsed_url.find(kStartParameter, last + 1);
657         if (next_template_start == std::string::npos ||
658             next_template_start > template_end) {
659           // If successful, ParseParameter erases from the string as such no
660           // need to update |last|. If failed, move |last| to the end of pair.
661           if (!ParseParameter(last, template_end, &parsed_url, replacements)) {
662             // |template_end| + 1 may be beyond the end of the string.
663             last = template_end;
664           }
665         } else {
666           last = next_template_start;
667         }
668       } else {
669         // Open brace without a closing brace, return.
670         return std::string();
671       }
672     }
673   }
674
675   // Handles the post parameters.
676   const std::string& post_params_string = GetPostParamsString();
677   if (!post_params_string.empty()) {
678     typedef std::vector<std::string> Strings;
679     Strings param_list;
680     base::SplitString(post_params_string, ',', &param_list);
681
682     for (Strings::const_iterator iterator = param_list.begin();
683          iterator != param_list.end(); ++iterator) {
684       Strings parts;
685       // The '=' delimiter is required and the name must be not empty.
686       base::SplitString(*iterator, '=', &parts);
687       if ((parts.size() != 2U) || parts[0].empty())
688         return std::string();
689
690       std::string& value = parts[1];
691       size_t replacements_size = replacements->size();
692       if (IsTemplateParameterString(value))
693         ParseParameter(0, value.length() - 1, &value, replacements);
694       post_params->push_back(std::make_pair(parts[0], value));
695       // If there was a replacement added, points its index to last added
696       // PostParam.
697       if (replacements->size() > replacements_size) {
698         DCHECK_EQ(replacements_size + 1, replacements->size());
699         Replacement* r = &replacements->back();
700         r->is_post_param = true;
701         r->index = post_params->size() - 1;
702       }
703     }
704     DCHECK(!post_params->empty());
705   }
706
707   *valid = true;
708   return parsed_url;
709 }
710
711 void TemplateURLRef::ParseIfNecessary() const {
712   UIThreadSearchTermsData search_terms_data(owner_->profile());
713   ParseIfNecessaryUsingTermsData(search_terms_data);
714 }
715
716 void TemplateURLRef::ParseIfNecessaryUsingTermsData(
717     const SearchTermsData& search_terms_data) const {
718   if (!parsed_) {
719     InvalidateCachedValues();
720     parsed_ = true;
721     parsed_url_ = ParseURL(GetURL(), &replacements_, &post_params_, &valid_);
722     supports_replacements_ = false;
723     if (valid_) {
724       bool has_only_one_search_term = false;
725       for (Replacements::const_iterator i = replacements_.begin();
726            i != replacements_.end(); ++i) {
727         if ((i->type == SEARCH_TERMS) ||
728             (i->type == GOOGLE_UNESCAPED_SEARCH_TERMS)) {
729           if (has_only_one_search_term) {
730             has_only_one_search_term = false;
731             break;
732           }
733           has_only_one_search_term = true;
734           supports_replacements_ = true;
735         }
736       }
737       // Only parse the host/key if there is one search term. Technically there
738       // could be more than one term, but it's uncommon; so we punt.
739       if (has_only_one_search_term)
740         ParseHostAndSearchTermKey(search_terms_data);
741     }
742   }
743 }
744
745 void TemplateURLRef::ParseHostAndSearchTermKey(
746     const SearchTermsData& search_terms_data) const {
747   std::string url_string(GetURL());
748   ReplaceSubstringsAfterOffset(&url_string, 0,
749                                kGoogleBaseURLParameterFull,
750                                search_terms_data.GoogleBaseURLValue());
751   ReplaceSubstringsAfterOffset(&url_string, 0,
752                                kGoogleBaseSuggestURLParameterFull,
753                                search_terms_data.GoogleBaseSuggestURLValue());
754
755   search_term_key_.clear();
756   host_.clear();
757   path_.clear();
758   search_term_key_location_ = url_parse::Parsed::REF;
759
760   GURL url(url_string);
761   if (!url.is_valid())
762     return;
763
764   std::string query_key = FindSearchTermsKey(url.query());
765   std::string ref_key = FindSearchTermsKey(url.ref());
766   if (query_key.empty() == ref_key.empty())
767     return;  // No key or multiple keys found.  We only handle having one key.
768   search_term_key_ = query_key.empty() ? ref_key : query_key;
769   search_term_key_location_ = query_key.empty() ?
770       url_parse::Parsed::REF : url_parse::Parsed::QUERY;
771   host_ = url.host();
772   path_ = url.path();
773 }
774
775 void TemplateURLRef::HandleReplacement(const std::string& name,
776                                        const std::string& value,
777                                        const Replacement& replacement,
778                                        std::string* url) const {
779   size_t pos = replacement.index;
780   if (replacement.is_post_param) {
781     DCHECK_LT(pos, post_params_.size());
782     DCHECK(!post_params_[pos].first.empty());
783     post_params_[pos].second = value;
784   } else {
785     url->insert(pos, name.empty() ? value : (name + "=" + value + "&"));
786   }
787 }
788
789 std::string TemplateURLRef::HandleReplacements(
790     const SearchTermsArgs& search_terms_args,
791     const SearchTermsData& search_terms_data,
792     PostContent* post_content) const {
793   if (replacements_.empty()) {
794     if (!post_params_.empty())
795       EncodeFormData(post_params_, post_content);
796     return parsed_url_;
797   }
798
799   // Determine if the search terms are in the query or before. We're escaping
800   // space as '+' in the former case and as '%20' in the latter case.
801   bool is_in_query = true;
802   for (Replacements::iterator i = replacements_.begin();
803        i != replacements_.end(); ++i) {
804     if (i->type == SEARCH_TERMS) {
805       base::string16::size_type query_start = parsed_url_.find('?');
806       is_in_query = query_start != base::string16::npos &&
807           (static_cast<base::string16::size_type>(i->index) > query_start);
808       break;
809     }
810   }
811
812   std::string input_encoding;
813   base::string16 encoded_terms;
814   base::string16 encoded_original_query;
815   owner_->EncodeSearchTerms(search_terms_args, is_in_query, &input_encoding,
816                             &encoded_terms, &encoded_original_query);
817
818   std::string url = parsed_url_;
819
820   // replacements_ is ordered in ascending order, as such we need to iterate
821   // from the back.
822   for (Replacements::reverse_iterator i = replacements_.rbegin();
823        i != replacements_.rend(); ++i) {
824     switch (i->type) {
825       case ENCODING:
826         HandleReplacement(std::string(), input_encoding, *i, &url);
827         break;
828
829       case GOOGLE_ASSISTED_QUERY_STATS:
830         DCHECK(!i->is_post_param);
831         if (!search_terms_args.assisted_query_stats.empty()) {
832           // Get the base URL without substituting AQS to avoid infinite
833           // recursion.  We need the URL to find out if it meets all
834           // AQS requirements (e.g. HTTPS protocol check).
835           // See TemplateURLRef::SearchTermsArgs for more details.
836           SearchTermsArgs search_terms_args_without_aqs(search_terms_args);
837           search_terms_args_without_aqs.assisted_query_stats.clear();
838           GURL base_url(ReplaceSearchTermsUsingTermsData(
839               search_terms_args_without_aqs, search_terms_data, NULL));
840           if (base_url.SchemeIs(content::kHttpsScheme)) {
841             HandleReplacement(
842                 "aqs", search_terms_args.assisted_query_stats, *i, &url);
843           }
844         }
845         break;
846
847       case GOOGLE_BASE_URL:
848         DCHECK(!i->is_post_param);
849         HandleReplacement(
850             std::string(), search_terms_data.GoogleBaseURLValue(), *i, &url);
851         break;
852
853       case GOOGLE_BASE_SUGGEST_URL:
854         DCHECK(!i->is_post_param);
855         HandleReplacement(
856             std::string(), search_terms_data.GoogleBaseSuggestURLValue(), *i,
857             &url);
858         break;
859
860       case GOOGLE_BOOKMARK_BAR_PINNED:
861         if (showing_search_terms_) {
862           // Log whether the bookmark bar is pinned when the user is seeing
863           // InstantExtended on the SRP.
864           DCHECK(!i->is_post_param);
865           HandleReplacement(
866               "bmbp", search_terms_args.bookmark_bar_pinned ? "1" : "0", *i,
867               &url);
868         }
869         break;
870
871       case GOOGLE_CURRENT_PAGE_URL:
872         DCHECK(!i->is_post_param);
873         if (!search_terms_args.current_page_url.empty()) {
874           const std::string& escaped_current_page_url =
875               net::EscapeQueryParamValue(search_terms_args.current_page_url,
876                                          true);
877           HandleReplacement("url", escaped_current_page_url, *i, &url);
878         }
879         break;
880
881       case GOOGLE_CURSOR_POSITION:
882         DCHECK(!i->is_post_param);
883         if (search_terms_args.cursor_position != base::string16::npos)
884           HandleReplacement(
885               "cp",
886               base::StringPrintf("%" PRIuS, search_terms_args.cursor_position),
887               *i,
888               &url);
889         break;
890
891       case GOOGLE_FORCE_INSTANT_RESULTS:
892         DCHECK(!i->is_post_param);
893         HandleReplacement(std::string(),
894                           chrome::ForceInstantResultsParam(
895                               search_terms_args.force_instant_results),
896                           *i,
897                           &url);
898         break;
899
900       case GOOGLE_INSTANT_EXTENDED_ENABLED:
901         DCHECK(!i->is_post_param);
902         HandleReplacement(std::string(),
903                           chrome::InstantExtendedEnabledParam(type_ == SEARCH),
904                           *i,
905                           &url);
906         break;
907
908       case GOOGLE_NTP_IS_THEMED:
909         DCHECK(!i->is_post_param);
910         HandleReplacement(
911             std::string(), search_terms_data.NTPIsThemedParam(), *i, &url);
912         break;
913
914       case GOOGLE_OMNIBOX_START_MARGIN:
915         DCHECK(!i->is_post_param);
916         if (search_terms_args.omnibox_start_margin >= 0) {
917           HandleReplacement(
918               "es_sm",
919               base::IntToString(search_terms_args.omnibox_start_margin),
920               *i,
921               &url);
922         }
923         break;
924
925       case GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION:
926         DCHECK(!i->is_post_param);
927         if (search_terms_args.accepted_suggestion >= 0 ||
928             !search_terms_args.assisted_query_stats.empty()) {
929           HandleReplacement(
930               "oq", base::UTF16ToUTF8(encoded_original_query), *i, &url);
931         }
932         break;
933
934       case GOOGLE_PAGE_CLASSIFICATION:
935         if (search_terms_args.page_classification !=
936             AutocompleteInput::INVALID_SPEC) {
937           HandleReplacement(
938               "pgcl", base::IntToString(search_terms_args.page_classification),
939               *i, &url);
940         }
941         break;
942
943       case GOOGLE_RLZ: {
944         DCHECK(!i->is_post_param);
945         // On platforms that don't have RLZ, we still want this branch
946         // to happen so that we replace the RLZ template with the
947         // empty string.  (If we don't handle this case, we hit a
948         // NOTREACHED below.)
949         base::string16 rlz_string = search_terms_data.GetRlzParameterValue();
950         if (!rlz_string.empty()) {
951           HandleReplacement("rlz", base::UTF16ToUTF8(rlz_string), *i, &url);
952         }
953         break;
954       }
955
956       case GOOGLE_SEARCH_CLIENT: {
957         DCHECK(!i->is_post_param);
958         std::string client = search_terms_data.GetSearchClient();
959         if (!client.empty())
960           HandleReplacement("client", client, *i, &url);
961         break;
962       }
963
964       case GOOGLE_SEARCH_FIELDTRIAL_GROUP:
965         // We are not currently running any fieldtrials that modulate the search
966         // url.  If we do, then we'd have some conditional insert such as:
967         // url.insert(i->index, used_www ? "gcx=w&" : "gcx=c&");
968         break;
969
970       case GOOGLE_SUGGEST_CLIENT:
971         HandleReplacement(
972             std::string(), search_terms_data.GetSuggestClient(), *i, &url);
973         break;
974
975       case GOOGLE_SUGGEST_REQUEST_ID:
976         HandleReplacement(
977             std::string(), search_terms_data.GetSuggestRequestIdentifier(), *i,
978             &url);
979         break;
980
981       case GOOGLE_UNESCAPED_SEARCH_TERMS: {
982         std::string unescaped_terms;
983         base::UTF16ToCodepage(search_terms_args.search_terms,
984                               input_encoding.c_str(),
985                               base::OnStringConversionError::SKIP,
986                               &unescaped_terms);
987         HandleReplacement(std::string(), unescaped_terms, *i, &url);
988         break;
989       }
990
991       case LANGUAGE:
992         HandleReplacement(
993             std::string(), search_terms_data.GetApplicationLocale(), *i, &url);
994         break;
995
996       case SEARCH_TERMS:
997         HandleReplacement(
998             std::string(), base::UTF16ToUTF8(encoded_terms), *i, &url);
999         break;
1000
1001       case GOOGLE_IMAGE_THUMBNAIL:
1002         HandleReplacement(
1003             std::string(), search_terms_args.image_thumbnail_content, *i, &url);
1004         break;
1005
1006       case GOOGLE_IMAGE_URL:
1007         if (search_terms_args.image_url.is_valid()) {
1008           HandleReplacement(
1009               std::string(), search_terms_args.image_url.spec(), *i, &url);
1010         }
1011         break;
1012
1013       case GOOGLE_IMAGE_ORIGINAL_WIDTH:
1014         if (!search_terms_args.image_original_size.IsEmpty()) {
1015           HandleReplacement(
1016               std::string(),
1017               base::IntToString(search_terms_args.image_original_size.width()),
1018               *i, &url);
1019         }
1020         break;
1021
1022       case GOOGLE_IMAGE_ORIGINAL_HEIGHT:
1023         if (!search_terms_args.image_original_size.IsEmpty()) {
1024           HandleReplacement(
1025               std::string(),
1026               base::IntToString(search_terms_args.image_original_size.height()),
1027               *i, &url);
1028         }
1029         break;
1030
1031       default:
1032         NOTREACHED();
1033         break;
1034     }
1035   }
1036
1037   if (!post_params_.empty())
1038     EncodeFormData(post_params_, post_content);
1039
1040   return url;
1041 }
1042
1043
1044 // TemplateURLData ------------------------------------------------------------
1045
1046 TemplateURLData::TemplateURLData()
1047     : show_in_default_list(false),
1048       safe_for_autoreplace(false),
1049       id(0),
1050       date_created(base::Time::Now()),
1051       last_modified(base::Time::Now()),
1052       created_by_policy(false),
1053       usage_count(0),
1054       prepopulate_id(0),
1055       sync_guid(base::GenerateGUID()),
1056       keyword_(base::ASCIIToUTF16("dummy")),
1057       url_("x") {
1058 }
1059
1060 TemplateURLData::~TemplateURLData() {
1061 }
1062
1063 void TemplateURLData::SetKeyword(const base::string16& keyword) {
1064   DCHECK(!keyword.empty());
1065
1066   // Case sensitive keyword matching is confusing. As such, we force all
1067   // keywords to be lower case.
1068   keyword_ = base::i18n::ToLower(keyword);
1069 }
1070
1071 void TemplateURLData::SetURL(const std::string& url) {
1072   DCHECK(!url.empty());
1073   url_ = url;
1074 }
1075
1076
1077 // TemplateURL ----------------------------------------------------------------
1078
1079 TemplateURL::TemplateURL(Profile* profile, const TemplateURLData& data)
1080     : profile_(profile),
1081       data_(data),
1082       url_ref_(this, TemplateURLRef::SEARCH),
1083       suggestions_url_ref_(this,
1084                            TemplateURLRef::SUGGEST),
1085       instant_url_ref_(this,
1086                        TemplateURLRef::INSTANT),
1087       image_url_ref_(this, TemplateURLRef::IMAGE),
1088       new_tab_url_ref_(this, TemplateURLRef::NEW_TAB) {
1089   SetPrepopulateId(data_.prepopulate_id);
1090
1091   if (data_.search_terms_replacement_key ==
1092       kGoogleInstantExtendedEnabledKeyFull) {
1093     data_.search_terms_replacement_key = google_util::kInstantExtendedAPIParam;
1094   }
1095 }
1096
1097 TemplateURL::~TemplateURL() {
1098 }
1099
1100 // static
1101 GURL TemplateURL::GenerateFaviconURL(const GURL& url) {
1102   DCHECK(url.is_valid());
1103   GURL::Replacements rep;
1104
1105   const char favicon_path[] = "/favicon.ico";
1106   int favicon_path_len = arraysize(favicon_path) - 1;
1107
1108   rep.SetPath(favicon_path, url_parse::Component(0, favicon_path_len));
1109   rep.ClearUsername();
1110   rep.ClearPassword();
1111   rep.ClearQuery();
1112   rep.ClearRef();
1113   return url.ReplaceComponents(rep);
1114 }
1115
1116 base::string16 TemplateURL::AdjustedShortNameForLocaleDirection() const {
1117   base::string16 bidi_safe_short_name = data_.short_name;
1118   base::i18n::AdjustStringForLocaleDirection(&bidi_safe_short_name);
1119   return bidi_safe_short_name;
1120 }
1121
1122 bool TemplateURL::ShowInDefaultList() const {
1123   return data_.show_in_default_list && url_ref_.SupportsReplacement();
1124 }
1125
1126 bool TemplateURL::SupportsReplacement() const {
1127   UIThreadSearchTermsData search_terms_data(profile_);
1128   return SupportsReplacementUsingTermsData(search_terms_data);
1129 }
1130
1131 bool TemplateURL::SupportsReplacementUsingTermsData(
1132     const SearchTermsData& search_terms_data) const {
1133   return url_ref_.SupportsReplacementUsingTermsData(search_terms_data);
1134 }
1135
1136 bool TemplateURL::IsGoogleSearchURLWithReplaceableKeyword() const {
1137   return (GetType() == NORMAL) && url_ref_.HasGoogleBaseURLs() &&
1138       google_util::IsGoogleHostname(base::UTF16ToUTF8(data_.keyword()),
1139                                     google_util::DISALLOW_SUBDOMAIN);
1140 }
1141
1142 bool TemplateURL::HasSameKeywordAs(const TemplateURL& other) const {
1143   return (data_.keyword() == other.data_.keyword()) ||
1144       (IsGoogleSearchURLWithReplaceableKeyword() &&
1145        other.IsGoogleSearchURLWithReplaceableKeyword());
1146 }
1147
1148 TemplateURL::Type TemplateURL::GetType() const {
1149   if (extension_info_)
1150     return NORMAL_CONTROLLED_BY_EXTENSION;
1151   return GURL(data_.url()).SchemeIs(extensions::kExtensionScheme) ?
1152       OMNIBOX_API_EXTENSION : NORMAL;
1153 }
1154
1155 std::string TemplateURL::GetExtensionId() const {
1156   DCHECK_NE(NORMAL, GetType());
1157   return extension_info_ ?
1158       extension_info_->extension_id : GURL(data_.url()).host();
1159 }
1160
1161 size_t TemplateURL::URLCount() const {
1162   // Add 1 for the regular search URL.
1163   return data_.alternate_urls.size() + 1;
1164 }
1165
1166 const std::string& TemplateURL::GetURL(size_t index) const {
1167   DCHECK_LT(index, URLCount());
1168
1169   return (index < data_.alternate_urls.size()) ?
1170       data_.alternate_urls[index] : url();
1171 }
1172
1173 bool TemplateURL::ExtractSearchTermsFromURL(
1174     const GURL& url,
1175     base::string16* search_terms) {
1176   UIThreadSearchTermsData search_terms_data(profile_);
1177   return ExtractSearchTermsFromURLUsingTermsData(url, search_terms,
1178                                                  search_terms_data);
1179 }
1180
1181 bool TemplateURL::ExtractSearchTermsFromURLUsingTermsData(
1182     const GURL& url,
1183     base::string16* search_terms,
1184     const SearchTermsData& search_terms_data) {
1185   return FindSearchTermsInURL(url, search_terms_data, search_terms, NULL, NULL);
1186 }
1187
1188
1189 bool TemplateURL::IsSearchURL(const GURL& url) {
1190   UIThreadSearchTermsData search_terms_data(profile_);
1191   return IsSearchURLUsingTermsData(url, search_terms_data);
1192 }
1193
1194 bool TemplateURL::IsSearchURLUsingTermsData(
1195     const GURL& url,
1196     const SearchTermsData& search_terms_data) {
1197   base::string16 search_terms;
1198   return ExtractSearchTermsFromURLUsingTermsData(
1199       url, &search_terms, search_terms_data) && !search_terms.empty();
1200 }
1201
1202 bool TemplateURL::HasSearchTermsReplacementKey(const GURL& url) const {
1203   // Look for the key both in the query and the ref.
1204   std::string params[] = {url.query(), url.ref()};
1205
1206   for (int i = 0; i < 2; ++i) {
1207     url_parse::Component query, key, value;
1208     query.len = static_cast<int>(params[i].size());
1209     while (url_parse::ExtractQueryKeyValue(params[i].c_str(), &query, &key,
1210                                            &value)) {
1211       if (key.is_nonempty() &&
1212           params[i].substr(key.begin, key.len) ==
1213               search_terms_replacement_key()) {
1214         return true;
1215       }
1216     }
1217   }
1218   return false;
1219 }
1220
1221 bool TemplateURL::ReplaceSearchTermsInURL(
1222     const GURL& url,
1223     const TemplateURLRef::SearchTermsArgs& search_terms_args,
1224     GURL* result) {
1225   UIThreadSearchTermsData search_terms_data(profile_);
1226   // TODO(beaudoin): Use AQS from |search_terms_args| too.
1227   url_parse::Parsed::ComponentType search_term_component;
1228   url_parse::Component search_terms_position;
1229   base::string16 search_terms;
1230   if (!FindSearchTermsInURL(url, search_terms_data, &search_terms,
1231                             &search_term_component, &search_terms_position)) {
1232     return false;
1233   }
1234   DCHECK(search_terms_position.is_nonempty());
1235
1236   // FindSearchTermsInURL only returns true for search terms in the query or
1237   // ref, so we can call EncodeSearchTerm with |is_in_query| = true, since query
1238   // and ref are encoded in the same way.
1239   std::string input_encoding;
1240   base::string16 encoded_terms;
1241   base::string16 encoded_original_query;
1242   EncodeSearchTerms(search_terms_args, true, &input_encoding,
1243                     &encoded_terms, &encoded_original_query);
1244
1245   std::string old_params((search_term_component == url_parse::Parsed::REF) ?
1246       url.ref() : url.query());
1247   std::string new_params(old_params, 0, search_terms_position.begin);
1248   new_params += base::UTF16ToUTF8(search_terms_args.search_terms);
1249   new_params += old_params.substr(search_terms_position.end());
1250   url_canon::StdStringReplacements<std::string> replacements;
1251   if (search_term_component == url_parse::Parsed::REF)
1252     replacements.SetRefStr(new_params);
1253   else
1254     replacements.SetQueryStr(new_params);
1255   *result = url.ReplaceComponents(replacements);
1256   return true;
1257 }
1258
1259 void TemplateURL::EncodeSearchTerms(
1260     const TemplateURLRef::SearchTermsArgs& search_terms_args,
1261     bool is_in_query,
1262     std::string* input_encoding,
1263     base::string16* encoded_terms,
1264     base::string16* encoded_original_query) const {
1265
1266   std::vector<std::string> encodings(input_encodings());
1267   if (std::find(encodings.begin(), encodings.end(), "UTF-8") == encodings.end())
1268     encodings.push_back("UTF-8");
1269   for (std::vector<std::string>::const_iterator i(encodings.begin());
1270        i != encodings.end(); ++i) {
1271     if (TryEncoding(search_terms_args.search_terms,
1272                     search_terms_args.original_query, i->c_str(),
1273                     is_in_query, encoded_terms, encoded_original_query)) {
1274       *input_encoding = *i;
1275       return;
1276     }
1277   }
1278   NOTREACHED();
1279 }
1280
1281 void TemplateURL::CopyFrom(const TemplateURL& other) {
1282   if (this == &other)
1283     return;
1284
1285   profile_ = other.profile_;
1286   data_ = other.data_;
1287   url_ref_.InvalidateCachedValues();
1288   suggestions_url_ref_.InvalidateCachedValues();
1289   instant_url_ref_.InvalidateCachedValues();
1290   SetPrepopulateId(other.data_.prepopulate_id);
1291 }
1292
1293 void TemplateURL::SetURL(const std::string& url) {
1294   data_.SetURL(url);
1295   url_ref_.InvalidateCachedValues();
1296 }
1297
1298 void TemplateURL::SetPrepopulateId(int id) {
1299   data_.prepopulate_id = id;
1300   const bool prepopulated = id > 0;
1301   url_ref_.prepopulated_ = prepopulated;
1302   suggestions_url_ref_.prepopulated_ = prepopulated;
1303   instant_url_ref_.prepopulated_ = prepopulated;
1304 }
1305
1306 void TemplateURL::ResetKeywordIfNecessary(bool force) {
1307   if (IsGoogleSearchURLWithReplaceableKeyword() || force) {
1308     DCHECK(GetType() != OMNIBOX_API_EXTENSION);
1309     GURL url(TemplateURLService::GenerateSearchURL(this));
1310     if (url.is_valid())
1311       data_.SetKeyword(TemplateURLService::GenerateKeyword(url));
1312   }
1313 }
1314
1315 bool TemplateURL::FindSearchTermsInURL(
1316     const GURL& url,
1317     const SearchTermsData& search_terms_data,
1318     base::string16* search_terms,
1319     url_parse::Parsed::ComponentType* search_term_component,
1320     url_parse::Component* search_terms_position) {
1321   DCHECK(search_terms);
1322   search_terms->clear();
1323
1324   // Try to match with every pattern.
1325   for (size_t i = 0; i < URLCount(); ++i) {
1326     TemplateURLRef ref(this, i);
1327     if (ref.ExtractSearchTermsFromURL(url, search_terms, search_terms_data,
1328         search_term_component, search_terms_position)) {
1329       // If ExtractSearchTermsFromURL() returns true and |search_terms| is empty
1330       // it means the pattern matched but no search terms were present. In this
1331       // case we fail immediately without looking for matches in subsequent
1332       // patterns. This means that given patterns
1333       //    [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ],
1334       // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would
1335       // return false. This is important for at least Google, where such URLs
1336       // are invalid.
1337       return !search_terms->empty();
1338     }
1339   }
1340   return false;
1341 }