Fix crash on app list start page keyboard navigation with <4 apps.
[chromium-blink-merge.git] / components / search_engines / template_url.cc
bloba9b6c9b6d7d521de9cd8bf928d73090505dc5d7b
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/search_engines/template_url.h"
7 #include <string>
8 #include <vector>
10 #include "base/basictypes.h"
11 #include "base/command_line.h"
12 #include "base/format_macros.h"
13 #include "base/i18n/icu_string_conversions.h"
14 #include "base/i18n/rtl.h"
15 #include "base/logging.h"
16 #include "base/metrics/field_trial.h"
17 #include "base/rand_util.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/string_split.h"
20 #include "base/strings/string_util.h"
21 #include "base/strings/stringprintf.h"
22 #include "base/strings/utf_string_conversions.h"
23 #include "components/google/core/browser/google_util.h"
24 #include "components/metrics/proto/omnibox_input_type.pb.h"
25 #include "components/search_engines/search_engines_switches.h"
26 #include "components/search_engines/search_terms_data.h"
27 #include "google_apis/google_api_keys.h"
28 #include "net/base/escape.h"
29 #include "net/base/mime_util.h"
30 #include "net/base/net_util.h"
31 #include "url/gurl.h"
33 namespace {
35 // The TemplateURLRef has any number of terms that need to be replaced. Each of
36 // the terms is enclosed in braces. If the character preceeding the final
37 // brace is a ?, it indicates the term is optional and can be replaced with
38 // an empty string.
39 const char kStartParameter = '{';
40 const char kEndParameter = '}';
41 const char kOptional = '?';
43 // Known parameters found in the URL.
44 const char kSearchTermsParameter[] = "searchTerms";
45 const char kSearchTermsParameterFull[] = "{searchTerms}";
46 const char kCountParameter[] = "count";
47 const char kStartIndexParameter[] = "startIndex";
48 const char kStartPageParameter[] = "startPage";
49 const char kLanguageParameter[] = "language";
50 const char kInputEncodingParameter[] = "inputEncoding";
51 const char kOutputEncodingParameter[] = "outputEncoding";
53 const char kGoogleAssistedQueryStatsParameter[] = "google:assistedQueryStats";
55 // Host/Domain Google searches are relative to.
56 const char kGoogleBaseURLParameter[] = "google:baseURL";
57 const char kGoogleBaseURLParameterFull[] = "{google:baseURL}";
59 // Like google:baseURL, but for the Search Suggest capability.
60 const char kGoogleBaseSuggestURLParameter[] = "google:baseSuggestURL";
61 const char kGoogleBaseSuggestURLParameterFull[] = "{google:baseSuggestURL}";
62 const char kGoogleBookmarkBarPinnedParameter[] = "google:bookmarkBarPinned";
63 const char kGoogleContextualSearchContextData[] =
64 "google:contextualSearchContextData";
65 const char kGoogleContextualSearchVersion[] = "google:contextualSearchVersion";
66 const char kGoogleCurrentPageUrlParameter[] = "google:currentPageUrl";
67 const char kGoogleCursorPositionParameter[] = "google:cursorPosition";
68 const char kGoogleForceInstantResultsParameter[] = "google:forceInstantResults";
69 const char kGoogleImageSearchSource[] = "google:imageSearchSource";
70 const char kGoogleImageThumbnailParameter[] = "google:imageThumbnail";
71 const char kGoogleImageOriginalWidth[] = "google:imageOriginalWidth";
72 const char kGoogleImageOriginalHeight[] = "google:imageOriginalHeight";
73 const char kGoogleImageURLParameter[] = "google:imageURL";
74 const char kGoogleInputTypeParameter[] = "google:inputType";
75 const char kGoogleInstantExtendedEnabledParameter[] =
76 "google:instantExtendedEnabledParameter";
77 const char kGoogleInstantExtendedEnabledKey[] =
78 "google:instantExtendedEnabledKey";
79 const char kGoogleInstantExtendedEnabledKeyFull[] =
80 "{google:instantExtendedEnabledKey}";
81 const char kGoogleNTPIsThemedParameter[] = "google:ntpIsThemedParameter";
82 const char kGoogleOmniboxStartMarginParameter[] =
83 "google:omniboxStartMarginParameter";
84 const char kGoogleOriginalQueryForSuggestionParameter[] =
85 "google:originalQueryForSuggestion";
86 const char kGooglePageClassificationParameter[] = "google:pageClassification";
87 const char kGooglePrefetchQuery[] = "google:prefetchQuery";
88 const char kGoogleRLZParameter[] = "google:RLZ";
89 const char kGoogleSearchClient[] = "google:searchClient";
90 const char kGoogleSearchFieldtrialParameter[] =
91 "google:searchFieldtrialParameter";
92 const char kGoogleSearchVersion[] = "google:searchVersion";
93 const char kGoogleSessionToken[] = "google:sessionToken";
94 const char kGoogleSourceIdParameter[] = "google:sourceId";
95 const char kGoogleSuggestAPIKeyParameter[] = "google:suggestAPIKeyParameter";
96 const char kGoogleSuggestClient[] = "google:suggestClient";
97 const char kGoogleSuggestRequestId[] = "google:suggestRid";
99 // Same as kSearchTermsParameter, with no escaping.
100 const char kGoogleUnescapedSearchTermsParameter[] =
101 "google:unescapedSearchTerms";
102 const char kGoogleUnescapedSearchTermsParameterFull[] =
103 "{google:unescapedSearchTerms}";
105 // Display value for kSearchTermsParameter.
106 const char kDisplaySearchTerms[] = "%s";
108 // Display value for kGoogleUnescapedSearchTermsParameter.
109 const char kDisplayUnescapedSearchTerms[] = "%S";
111 // Used if the count parameter is not optional. Indicates we want 10 search
112 // results.
113 const char kDefaultCount[] = "10";
115 // Used if the parameter kOutputEncodingParameter is required.
116 const char kOutputEncodingType[] = "UTF-8";
118 // Attempts to encode |terms| and |original_query| in |encoding| and escape
119 // them. |terms| may be escaped as path or query depending on |is_in_query|;
120 // |original_query| is always escaped as query. Returns whether the encoding
121 // process succeeded.
122 bool TryEncoding(const base::string16& terms,
123 const base::string16& original_query,
124 const char* encoding,
125 bool is_in_query,
126 base::string16* escaped_terms,
127 base::string16* escaped_original_query) {
128 DCHECK(escaped_terms);
129 DCHECK(escaped_original_query);
130 std::string encoded_terms;
131 if (!base::UTF16ToCodepage(terms, encoding,
132 base::OnStringConversionError::SKIP, &encoded_terms))
133 return false;
134 *escaped_terms = base::UTF8ToUTF16(is_in_query ?
135 net::EscapeQueryParamValue(encoded_terms, true) :
136 net::EscapePath(encoded_terms));
137 if (original_query.empty())
138 return true;
139 std::string encoded_original_query;
140 if (!base::UTF16ToCodepage(original_query, encoding,
141 base::OnStringConversionError::SKIP, &encoded_original_query))
142 return false;
143 *escaped_original_query = base::UTF8ToUTF16(
144 net::EscapeQueryParamValue(encoded_original_query, true));
145 return true;
148 // Extract query key and host given a list of parameters coming from the URL
149 // query or ref.
150 std::string FindSearchTermsKey(const std::string& params) {
151 if (params.empty())
152 return std::string();
153 url::Component query, key, value;
154 query.len = static_cast<int>(params.size());
155 while (url::ExtractQueryKeyValue(params.c_str(), &query, &key, &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);
166 return std::string();
169 bool IsTemplateParameterString(const std::string& param) {
170 return (param.length() > 2) && (*(param.begin()) == kStartParameter) &&
171 (*(param.rbegin()) == kEndParameter);
174 } // namespace
177 // TemplateURLRef::SearchTermsArgs --------------------------------------------
179 TemplateURLRef::SearchTermsArgs::SearchTermsArgs(
180 const base::string16& search_terms)
181 : search_terms(search_terms),
182 input_type(metrics::OmniboxInputType::INVALID),
183 accepted_suggestion(NO_SUGGESTIONS_AVAILABLE),
184 cursor_position(base::string16::npos),
185 enable_omnibox_start_margin(false),
186 page_classification(metrics::OmniboxEventProto::INVALID_SPEC),
187 bookmark_bar_pinned(false),
188 append_extra_query_params(false),
189 force_instant_results(false),
190 from_app_list(false),
191 contextual_search_params(ContextualSearchParams()) {
194 TemplateURLRef::SearchTermsArgs::~SearchTermsArgs() {
197 TemplateURLRef::SearchTermsArgs::ContextualSearchParams::
198 ContextualSearchParams()
199 : version(-1),
200 start(base::string16::npos),
201 end(base::string16::npos),
202 resolve(true) {
205 TemplateURLRef::SearchTermsArgs::ContextualSearchParams::
206 ContextualSearchParams(
207 const int version,
208 const std::string& selection,
209 const std::string& base_page_url,
210 const bool resolve)
211 : version(version),
212 start(base::string16::npos),
213 end(base::string16::npos),
214 selection(selection),
215 base_page_url(base_page_url),
216 resolve(resolve) {
219 TemplateURLRef::SearchTermsArgs::ContextualSearchParams::
220 ContextualSearchParams(
221 const int version,
222 const size_t start,
223 const size_t end,
224 const std::string& selection,
225 const std::string& content,
226 const std::string& base_page_url,
227 const std::string& encoding,
228 const bool resolve)
229 : version(version),
230 start(start),
231 end(end),
232 selection(selection),
233 content(content),
234 base_page_url(base_page_url),
235 encoding(encoding),
236 resolve(resolve) {
239 TemplateURLRef::SearchTermsArgs::ContextualSearchParams::
240 ~ContextualSearchParams() {
243 // TemplateURLRef -------------------------------------------------------------
245 TemplateURLRef::TemplateURLRef(TemplateURL* owner, Type type)
246 : owner_(owner),
247 type_(type),
248 index_in_owner_(0),
249 parsed_(false),
250 valid_(false),
251 supports_replacements_(false),
252 search_term_key_location_(url::Parsed::QUERY),
253 prepopulated_(false) {
254 DCHECK(owner_);
255 DCHECK_NE(INDEXED, type_);
258 TemplateURLRef::TemplateURLRef(TemplateURL* owner, size_t index_in_owner)
259 : owner_(owner),
260 type_(INDEXED),
261 index_in_owner_(index_in_owner),
262 parsed_(false),
263 valid_(false),
264 supports_replacements_(false),
265 search_term_key_location_(url::Parsed::QUERY),
266 prepopulated_(false) {
267 DCHECK(owner_);
268 DCHECK_LT(index_in_owner_, owner_->URLCount());
271 TemplateURLRef::~TemplateURLRef() {
274 std::string TemplateURLRef::GetURL() const {
275 switch (type_) {
276 case SEARCH: return owner_->url();
277 case SUGGEST: return owner_->suggestions_url();
278 case INSTANT: return owner_->instant_url();
279 case IMAGE: return owner_->image_url();
280 case NEW_TAB: return owner_->new_tab_url();
281 case CONTEXTUAL_SEARCH: return owner_->contextual_search_url();
282 case INDEXED: return owner_->GetURL(index_in_owner_);
283 default: NOTREACHED(); return std::string(); // NOLINT
287 std::string TemplateURLRef::GetPostParamsString() const {
288 switch (type_) {
289 case INDEXED:
290 case SEARCH: return owner_->search_url_post_params();
291 case SUGGEST: return owner_->suggestions_url_post_params();
292 case INSTANT: return owner_->instant_url_post_params();
293 case NEW_TAB: return std::string();
294 case CONTEXTUAL_SEARCH: return std::string();
295 case IMAGE: return owner_->image_url_post_params();
296 default: NOTREACHED(); return std::string(); // NOLINT
300 bool TemplateURLRef::UsesPOSTMethod(
301 const SearchTermsData& search_terms_data) const {
302 ParseIfNecessary(search_terms_data);
303 return !post_params_.empty();
306 bool TemplateURLRef::EncodeFormData(const PostParams& post_params,
307 PostContent* post_content) const {
308 if (post_params.empty())
309 return true;
310 if (!post_content)
311 return false;
313 const char kUploadDataMIMEType[] = "multipart/form-data; boundary=";
314 const char kMultipartBoundary[] = "----+*+----%016" PRIx64 "----+*+----";
315 // Each name/value pair is stored in a body part which is preceded by a
316 // boundary delimiter line. Uses random number generator here to create
317 // a unique boundary delimiter for form data encoding.
318 std::string boundary = base::StringPrintf(kMultipartBoundary,
319 base::RandUint64());
320 // Sets the content MIME type.
321 post_content->first = kUploadDataMIMEType;
322 post_content->first += boundary;
323 // Encodes the post parameters.
324 std::string* post_data = &post_content->second;
325 post_data->clear();
326 for (PostParams::const_iterator param = post_params.begin();
327 param != post_params.end(); ++param) {
328 DCHECK(!param->first.empty());
329 net::AddMultipartValueForUpload(param->first, param->second, boundary,
330 std::string(), post_data);
332 net::AddMultipartFinalDelimiterForUpload(boundary, post_data);
333 return true;
336 bool TemplateURLRef::SupportsReplacement(
337 const SearchTermsData& search_terms_data) const {
338 ParseIfNecessary(search_terms_data);
339 return valid_ && supports_replacements_;
342 std::string TemplateURLRef::ReplaceSearchTerms(
343 const SearchTermsArgs& search_terms_args,
344 const SearchTermsData& search_terms_data,
345 PostContent* post_content) const {
346 ParseIfNecessary(search_terms_data);
347 if (!valid_)
348 return std::string();
350 std::string url(HandleReplacements(search_terms_args, search_terms_data,
351 post_content));
353 GURL gurl(url);
354 if (!gurl.is_valid())
355 return url;
357 std::vector<std::string> query_params;
358 if (search_terms_args.append_extra_query_params) {
359 std::string extra_params(
360 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
361 switches::kExtraSearchQueryParams));
362 if (!extra_params.empty())
363 query_params.push_back(extra_params);
365 if (!search_terms_args.suggest_query_params.empty())
366 query_params.push_back(search_terms_args.suggest_query_params);
367 if (!gurl.query().empty())
368 query_params.push_back(gurl.query());
370 if (query_params.empty())
371 return url;
373 GURL::Replacements replacements;
374 std::string query_str = JoinString(query_params, "&");
375 replacements.SetQueryStr(query_str);
376 return gurl.ReplaceComponents(replacements).possibly_invalid_spec();
379 bool TemplateURLRef::IsValid(const SearchTermsData& search_terms_data) const {
380 ParseIfNecessary(search_terms_data);
381 return valid_;
384 base::string16 TemplateURLRef::DisplayURL(
385 const SearchTermsData& search_terms_data) const {
386 ParseIfNecessary(search_terms_data);
387 base::string16 result(base::UTF8ToUTF16(GetURL()));
388 if (valid_ && !replacements_.empty()) {
389 ReplaceSubstringsAfterOffset(&result, 0,
390 base::ASCIIToUTF16(kSearchTermsParameterFull),
391 base::ASCIIToUTF16(kDisplaySearchTerms));
392 ReplaceSubstringsAfterOffset(&result, 0,
393 base::ASCIIToUTF16(kGoogleUnescapedSearchTermsParameterFull),
394 base::ASCIIToUTF16(kDisplayUnescapedSearchTerms));
396 return result;
399 // static
400 std::string TemplateURLRef::DisplayURLToURLRef(
401 const base::string16& display_url) {
402 base::string16 result = display_url;
403 ReplaceSubstringsAfterOffset(&result, 0,
404 base::ASCIIToUTF16(kDisplaySearchTerms),
405 base::ASCIIToUTF16(kSearchTermsParameterFull));
406 ReplaceSubstringsAfterOffset(
407 &result, 0,
408 base::ASCIIToUTF16(kDisplayUnescapedSearchTerms),
409 base::ASCIIToUTF16(kGoogleUnescapedSearchTermsParameterFull));
410 return base::UTF16ToUTF8(result);
413 const std::string& TemplateURLRef::GetHost(
414 const SearchTermsData& search_terms_data) const {
415 ParseIfNecessary(search_terms_data);
416 return host_;
419 const std::string& TemplateURLRef::GetPath(
420 const SearchTermsData& search_terms_data) const {
421 ParseIfNecessary(search_terms_data);
422 return path_;
425 const std::string& TemplateURLRef::GetSearchTermKey(
426 const SearchTermsData& search_terms_data) const {
427 ParseIfNecessary(search_terms_data);
428 return search_term_key_;
431 base::string16 TemplateURLRef::SearchTermToString16(
432 const std::string& term) const {
433 const std::vector<std::string>& encodings = owner_->input_encodings();
434 base::string16 result;
436 std::string unescaped = net::UnescapeURLComponent(
437 term,
438 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE |
439 net::UnescapeRule::URL_SPECIAL_CHARS);
440 for (size_t i = 0; i < encodings.size(); ++i) {
441 if (base::CodepageToUTF16(unescaped, encodings[i].c_str(),
442 base::OnStringConversionError::FAIL, &result))
443 return result;
446 // Always fall back on UTF-8 if it works.
447 if (base::CodepageToUTF16(unescaped, base::kCodepageUTF8,
448 base::OnStringConversionError::FAIL, &result))
449 return result;
451 // When nothing worked, just use the escaped text. We have no idea what the
452 // encoding is. We need to substitute spaces for pluses ourselves since we're
453 // not sending it through an unescaper.
454 result = base::UTF8ToUTF16(term);
455 std::replace(result.begin(), result.end(), '+', ' ');
456 return result;
459 bool TemplateURLRef::HasGoogleBaseURLs(
460 const SearchTermsData& search_terms_data) const {
461 ParseIfNecessary(search_terms_data);
462 for (size_t i = 0; i < replacements_.size(); ++i) {
463 if ((replacements_[i].type == GOOGLE_BASE_URL) ||
464 (replacements_[i].type == GOOGLE_BASE_SUGGEST_URL))
465 return true;
467 return false;
470 bool TemplateURLRef::ExtractSearchTermsFromURL(
471 const GURL& url,
472 base::string16* search_terms,
473 const SearchTermsData& search_terms_data,
474 url::Parsed::ComponentType* search_terms_component,
475 url::Component* search_terms_position) const {
476 DCHECK(search_terms);
477 search_terms->clear();
479 ParseIfNecessary(search_terms_data);
481 // We need a search term in the template URL to extract something.
482 if (search_term_key_.empty())
483 return false;
485 // TODO(beaudoin): Support patterns of the form http://foo/{searchTerms}/
486 // See crbug.com/153798
488 // Fill-in the replacements. We don't care about search terms in the pattern,
489 // so we use the empty string.
490 // Currently we assume the search term only shows in URL, not in post params.
491 GURL pattern(ReplaceSearchTerms(SearchTermsArgs(base::string16()),
492 search_terms_data, NULL));
493 // Host, path and port must match.
494 if (url.port() != pattern.port() ||
495 url.host() != host_ ||
496 url.path() != path_) {
497 return false;
500 // Parameter must be present either in the query or the ref.
501 const std::string& params(
502 (search_term_key_location_ == url::Parsed::QUERY) ?
503 url.query() : url.ref());
505 url::Component query, key, value;
506 query.len = static_cast<int>(params.size());
507 bool key_found = false;
508 while (url::ExtractQueryKeyValue(params.c_str(), &query, &key, &value)) {
509 if (key.is_nonempty()) {
510 if (params.substr(key.begin, key.len) == search_term_key_) {
511 // Fail if search term key is found twice.
512 if (key_found) {
513 search_terms->clear();
514 return false;
516 key_found = true;
517 // Extract the search term.
518 *search_terms = net::UnescapeAndDecodeUTF8URLComponent(
519 params.substr(value.begin, value.len),
520 net::UnescapeRule::SPACES |
521 net::UnescapeRule::URL_SPECIAL_CHARS |
522 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE);
523 if (search_terms_component)
524 *search_terms_component = search_term_key_location_;
525 if (search_terms_position)
526 *search_terms_position = value;
530 return key_found;
533 void TemplateURLRef::InvalidateCachedValues() const {
534 supports_replacements_ = valid_ = parsed_ = false;
535 host_.clear();
536 path_.clear();
537 search_term_key_.clear();
538 replacements_.clear();
539 post_params_.clear();
542 bool TemplateURLRef::ParseParameter(size_t start,
543 size_t end,
544 std::string* url,
545 Replacements* replacements) const {
546 DCHECK(start != std::string::npos &&
547 end != std::string::npos && end > start);
548 size_t length = end - start - 1;
549 bool optional = false;
550 if ((*url)[end - 1] == kOptional) {
551 optional = true;
552 length--;
554 std::string parameter(url->substr(start + 1, length));
555 std::string full_parameter(url->substr(start, end - start + 1));
556 // Remove the parameter from the string. For parameters who replacement is
557 // constant and already known, just replace them directly. For other cases,
558 // like parameters whose values may change over time, use |replacements|.
559 url->erase(start, end - start + 1);
560 if (parameter == kSearchTermsParameter) {
561 replacements->push_back(Replacement(SEARCH_TERMS, start));
562 } else if (parameter == kCountParameter) {
563 if (!optional)
564 url->insert(start, kDefaultCount);
565 } else if (parameter == kGoogleAssistedQueryStatsParameter) {
566 replacements->push_back(Replacement(GOOGLE_ASSISTED_QUERY_STATS, start));
567 } else if (parameter == kGoogleBaseURLParameter) {
568 replacements->push_back(Replacement(GOOGLE_BASE_URL, start));
569 } else if (parameter == kGoogleBaseSuggestURLParameter) {
570 replacements->push_back(Replacement(GOOGLE_BASE_SUGGEST_URL, start));
571 } else if (parameter == kGoogleBookmarkBarPinnedParameter) {
572 replacements->push_back(Replacement(GOOGLE_BOOKMARK_BAR_PINNED, start));
573 } else if (parameter == kGoogleCurrentPageUrlParameter) {
574 replacements->push_back(Replacement(GOOGLE_CURRENT_PAGE_URL, start));
575 } else if (parameter == kGoogleCursorPositionParameter) {
576 replacements->push_back(Replacement(GOOGLE_CURSOR_POSITION, start));
577 } else if (parameter == kGoogleForceInstantResultsParameter) {
578 replacements->push_back(Replacement(GOOGLE_FORCE_INSTANT_RESULTS, start));
579 } else if (parameter == kGoogleImageOriginalHeight) {
580 replacements->push_back(
581 Replacement(TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_HEIGHT, start));
582 } else if (parameter == kGoogleImageOriginalWidth) {
583 replacements->push_back(
584 Replacement(TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH, start));
585 } else if (parameter == kGoogleImageSearchSource) {
586 replacements->push_back(
587 Replacement(TemplateURLRef::GOOGLE_IMAGE_SEARCH_SOURCE, start));
588 } else if (parameter == kGoogleImageThumbnailParameter) {
589 replacements->push_back(
590 Replacement(TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL, start));
591 } else if (parameter == kGoogleImageURLParameter) {
592 replacements->push_back(Replacement(TemplateURLRef::GOOGLE_IMAGE_URL,
593 start));
594 } else if (parameter == kGoogleInputTypeParameter) {
595 replacements->push_back(Replacement(TemplateURLRef::GOOGLE_INPUT_TYPE,
596 start));
597 } else if (parameter == kGoogleInstantExtendedEnabledParameter) {
598 replacements->push_back(Replacement(GOOGLE_INSTANT_EXTENDED_ENABLED,
599 start));
600 } else if (parameter == kGoogleInstantExtendedEnabledKey) {
601 url->insert(start, google_util::kInstantExtendedAPIParam);
602 } else if (parameter == kGoogleNTPIsThemedParameter) {
603 replacements->push_back(Replacement(GOOGLE_NTP_IS_THEMED, start));
604 } else if (parameter == kGoogleOmniboxStartMarginParameter) {
605 replacements->push_back(Replacement(GOOGLE_OMNIBOX_START_MARGIN, start));
606 } else if (parameter == kGoogleContextualSearchVersion) {
607 replacements->push_back(
608 Replacement(GOOGLE_CONTEXTUAL_SEARCH_VERSION, start));
609 } else if (parameter == kGoogleContextualSearchContextData) {
610 replacements->push_back(
611 Replacement(GOOGLE_CONTEXTUAL_SEARCH_CONTEXT_DATA, start));
612 } else if (parameter == kGoogleOriginalQueryForSuggestionParameter) {
613 replacements->push_back(Replacement(GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION,
614 start));
615 } else if (parameter == kGooglePageClassificationParameter) {
616 replacements->push_back(Replacement(GOOGLE_PAGE_CLASSIFICATION, start));
617 } else if (parameter == kGooglePrefetchQuery) {
618 replacements->push_back(Replacement(GOOGLE_PREFETCH_QUERY, start));
619 } else if (parameter == kGoogleRLZParameter) {
620 replacements->push_back(Replacement(GOOGLE_RLZ, start));
621 } else if (parameter == kGoogleSearchClient) {
622 replacements->push_back(Replacement(GOOGLE_SEARCH_CLIENT, start));
623 } else if (parameter == kGoogleSearchFieldtrialParameter) {
624 replacements->push_back(Replacement(GOOGLE_SEARCH_FIELDTRIAL_GROUP, start));
625 } else if (parameter == kGoogleSearchVersion) {
626 replacements->push_back(Replacement(GOOGLE_SEARCH_VERSION, start));
627 } else if (parameter == kGoogleSessionToken) {
628 replacements->push_back(Replacement(GOOGLE_SESSION_TOKEN, start));
629 } else if (parameter == kGoogleSourceIdParameter) {
630 #if defined(OS_ANDROID)
631 url->insert(start, "sourceid=chrome-mobile&");
632 #else
633 url->insert(start, "sourceid=chrome&");
634 #endif
635 } else if (parameter == kGoogleSuggestAPIKeyParameter) {
636 url->insert(start,
637 net::EscapeQueryParamValue(google_apis::GetAPIKey(), false));
638 } else if (parameter == kGoogleSuggestClient) {
639 replacements->push_back(Replacement(GOOGLE_SUGGEST_CLIENT, start));
640 } else if (parameter == kGoogleSuggestRequestId) {
641 replacements->push_back(Replacement(GOOGLE_SUGGEST_REQUEST_ID, start));
642 } else if (parameter == kGoogleUnescapedSearchTermsParameter) {
643 replacements->push_back(Replacement(GOOGLE_UNESCAPED_SEARCH_TERMS, start));
644 } else if (parameter == kInputEncodingParameter) {
645 replacements->push_back(Replacement(ENCODING, start));
646 } else if (parameter == kLanguageParameter) {
647 replacements->push_back(Replacement(LANGUAGE, start));
648 } else if (parameter == kOutputEncodingParameter) {
649 if (!optional)
650 url->insert(start, kOutputEncodingType);
651 } else if ((parameter == kStartIndexParameter) ||
652 (parameter == kStartPageParameter)) {
653 // We don't support these.
654 if (!optional)
655 url->insert(start, "1");
656 } else if (!prepopulated_) {
657 // If it's a prepopulated URL, we know that it's safe to remove unknown
658 // parameters, so just ignore this and return true below. Otherwise it could
659 // be some garbage but can also be a javascript block. Put it back.
660 url->insert(start, full_parameter);
661 return false;
663 return true;
666 std::string TemplateURLRef::ParseURL(const std::string& url,
667 Replacements* replacements,
668 PostParams* post_params,
669 bool* valid) const {
670 *valid = false;
671 std::string parsed_url = url;
672 for (size_t last = 0; last != std::string::npos; ) {
673 last = parsed_url.find(kStartParameter, last);
674 if (last != std::string::npos) {
675 size_t template_end = parsed_url.find(kEndParameter, last);
676 if (template_end != std::string::npos) {
677 // Since we allow Javascript in the URL, {} pairs could be nested. Match
678 // only leaf pairs with supported parameters.
679 size_t next_template_start = parsed_url.find(kStartParameter, last + 1);
680 if (next_template_start == std::string::npos ||
681 next_template_start > template_end) {
682 // If successful, ParseParameter erases from the string as such no
683 // need to update |last|. If failed, move |last| to the end of pair.
684 if (!ParseParameter(last, template_end, &parsed_url, replacements)) {
685 // |template_end| + 1 may be beyond the end of the string.
686 last = template_end;
688 } else {
689 last = next_template_start;
691 } else {
692 // Open brace without a closing brace, return.
693 return std::string();
698 // Handles the post parameters.
699 const std::string& post_params_string = GetPostParamsString();
700 if (!post_params_string.empty()) {
701 typedef std::vector<std::string> Strings;
702 Strings param_list;
703 base::SplitString(post_params_string, ',', &param_list);
705 for (Strings::const_iterator iterator = param_list.begin();
706 iterator != param_list.end(); ++iterator) {
707 Strings parts;
708 // The '=' delimiter is required and the name must be not empty.
709 base::SplitString(*iterator, '=', &parts);
710 if ((parts.size() != 2U) || parts[0].empty())
711 return std::string();
713 std::string& value = parts[1];
714 size_t replacements_size = replacements->size();
715 if (IsTemplateParameterString(value))
716 ParseParameter(0, value.length() - 1, &value, replacements);
717 post_params->push_back(std::make_pair(parts[0], value));
718 // If there was a replacement added, points its index to last added
719 // PostParam.
720 if (replacements->size() > replacements_size) {
721 DCHECK_EQ(replacements_size + 1, replacements->size());
722 Replacement* r = &replacements->back();
723 r->is_post_param = true;
724 r->index = post_params->size() - 1;
727 DCHECK(!post_params->empty());
730 *valid = true;
731 return parsed_url;
734 void TemplateURLRef::ParseIfNecessary(
735 const SearchTermsData& search_terms_data) const {
736 if (!parsed_) {
737 InvalidateCachedValues();
738 parsed_ = true;
739 parsed_url_ = ParseURL(GetURL(), &replacements_, &post_params_, &valid_);
740 supports_replacements_ = false;
741 if (valid_) {
742 bool has_only_one_search_term = false;
743 for (Replacements::const_iterator i = replacements_.begin();
744 i != replacements_.end(); ++i) {
745 if ((i->type == SEARCH_TERMS) ||
746 (i->type == GOOGLE_UNESCAPED_SEARCH_TERMS)) {
747 if (has_only_one_search_term) {
748 has_only_one_search_term = false;
749 break;
751 has_only_one_search_term = true;
752 supports_replacements_ = true;
755 // Only parse the host/key if there is one search term. Technically there
756 // could be more than one term, but it's uncommon; so we punt.
757 if (has_only_one_search_term)
758 ParseHostAndSearchTermKey(search_terms_data);
763 void TemplateURLRef::ParseHostAndSearchTermKey(
764 const SearchTermsData& search_terms_data) const {
765 std::string url_string(GetURL());
766 ReplaceSubstringsAfterOffset(&url_string, 0,
767 kGoogleBaseURLParameterFull,
768 search_terms_data.GoogleBaseURLValue());
769 ReplaceSubstringsAfterOffset(&url_string, 0,
770 kGoogleBaseSuggestURLParameterFull,
771 search_terms_data.GoogleBaseSuggestURLValue());
773 search_term_key_.clear();
774 host_.clear();
775 path_.clear();
776 search_term_key_location_ = url::Parsed::REF;
778 GURL url(url_string);
779 if (!url.is_valid())
780 return;
782 std::string query_key = FindSearchTermsKey(url.query());
783 std::string ref_key = FindSearchTermsKey(url.ref());
784 if (query_key.empty() == ref_key.empty())
785 return; // No key or multiple keys found. We only handle having one key.
786 search_term_key_ = query_key.empty() ? ref_key : query_key;
787 search_term_key_location_ =
788 query_key.empty() ? url::Parsed::REF : url::Parsed::QUERY;
789 host_ = url.host();
790 path_ = url.path();
793 void TemplateURLRef::HandleReplacement(const std::string& name,
794 const std::string& value,
795 const Replacement& replacement,
796 std::string* url) const {
797 size_t pos = replacement.index;
798 if (replacement.is_post_param) {
799 DCHECK_LT(pos, post_params_.size());
800 DCHECK(!post_params_[pos].first.empty());
801 post_params_[pos].second = value;
802 } else {
803 url->insert(pos, name.empty() ? value : (name + "=" + value + "&"));
807 std::string TemplateURLRef::HandleReplacements(
808 const SearchTermsArgs& search_terms_args,
809 const SearchTermsData& search_terms_data,
810 PostContent* post_content) const {
811 if (replacements_.empty()) {
812 if (!post_params_.empty())
813 EncodeFormData(post_params_, post_content);
814 return parsed_url_;
817 // Determine if the search terms are in the query or before. We're escaping
818 // space as '+' in the former case and as '%20' in the latter case.
819 bool is_in_query = true;
820 for (Replacements::iterator i = replacements_.begin();
821 i != replacements_.end(); ++i) {
822 if (i->type == SEARCH_TERMS) {
823 base::string16::size_type query_start = parsed_url_.find('?');
824 is_in_query = query_start != base::string16::npos &&
825 (static_cast<base::string16::size_type>(i->index) > query_start);
826 break;
830 std::string input_encoding;
831 base::string16 encoded_terms;
832 base::string16 encoded_original_query;
833 owner_->EncodeSearchTerms(search_terms_args, is_in_query, &input_encoding,
834 &encoded_terms, &encoded_original_query);
836 std::string url = parsed_url_;
838 // replacements_ is ordered in ascending order, as such we need to iterate
839 // from the back.
840 for (Replacements::reverse_iterator i = replacements_.rbegin();
841 i != replacements_.rend(); ++i) {
842 switch (i->type) {
843 case ENCODING:
844 HandleReplacement(std::string(), input_encoding, *i, &url);
845 break;
847 case GOOGLE_ASSISTED_QUERY_STATS:
848 DCHECK(!i->is_post_param);
849 if (!search_terms_args.assisted_query_stats.empty()) {
850 // Get the base URL without substituting AQS to avoid infinite
851 // recursion. We need the URL to find out if it meets all
852 // AQS requirements (e.g. HTTPS protocol check).
853 // See TemplateURLRef::SearchTermsArgs for more details.
854 SearchTermsArgs search_terms_args_without_aqs(search_terms_args);
855 search_terms_args_without_aqs.assisted_query_stats.clear();
856 GURL base_url(ReplaceSearchTerms(
857 search_terms_args_without_aqs, search_terms_data, NULL));
858 if (base_url.SchemeIs(url::kHttpsScheme)) {
859 HandleReplacement(
860 "aqs", search_terms_args.assisted_query_stats, *i, &url);
863 break;
865 case GOOGLE_BASE_URL:
866 DCHECK(!i->is_post_param);
867 HandleReplacement(
868 std::string(), search_terms_data.GoogleBaseURLValue(), *i, &url);
869 break;
871 case GOOGLE_BASE_SUGGEST_URL:
872 DCHECK(!i->is_post_param);
873 HandleReplacement(
874 std::string(), search_terms_data.GoogleBaseSuggestURLValue(), *i,
875 &url);
876 break;
878 case GOOGLE_BOOKMARK_BAR_PINNED:
879 if (search_terms_data.IsShowingSearchTermsOnSearchResultsPages()) {
880 // Log whether the bookmark bar is pinned when the user is seeing
881 // InstantExtended on the SRP.
882 DCHECK(!i->is_post_param);
883 HandleReplacement(
884 "bmbp", search_terms_args.bookmark_bar_pinned ? "1" : "0", *i,
885 &url);
887 break;
889 case GOOGLE_CURRENT_PAGE_URL:
890 DCHECK(!i->is_post_param);
891 if (!search_terms_args.current_page_url.empty()) {
892 const std::string& escaped_current_page_url =
893 net::EscapeQueryParamValue(search_terms_args.current_page_url,
894 true);
895 HandleReplacement("url", escaped_current_page_url, *i, &url);
897 break;
899 case GOOGLE_CURSOR_POSITION:
900 DCHECK(!i->is_post_param);
901 if (search_terms_args.cursor_position != base::string16::npos)
902 HandleReplacement(
903 "cp",
904 base::StringPrintf("%" PRIuS, search_terms_args.cursor_position),
906 &url);
907 break;
909 case GOOGLE_FORCE_INSTANT_RESULTS:
910 DCHECK(!i->is_post_param);
911 HandleReplacement(std::string(),
912 search_terms_data.ForceInstantResultsParam(
913 search_terms_args.force_instant_results),
915 &url);
916 break;
918 case GOOGLE_INPUT_TYPE:
919 DCHECK(!i->is_post_param);
920 HandleReplacement(
921 "oit", base::IntToString(search_terms_args.input_type), *i, &url);
922 break;
924 case GOOGLE_INSTANT_EXTENDED_ENABLED:
925 DCHECK(!i->is_post_param);
926 HandleReplacement(std::string(),
927 search_terms_data.InstantExtendedEnabledParam(
928 type_ == SEARCH),
930 &url);
931 break;
933 case GOOGLE_NTP_IS_THEMED:
934 DCHECK(!i->is_post_param);
935 HandleReplacement(
936 std::string(), search_terms_data.NTPIsThemedParam(), *i, &url);
937 break;
939 case GOOGLE_OMNIBOX_START_MARGIN:
940 DCHECK(!i->is_post_param);
941 if (search_terms_args.enable_omnibox_start_margin) {
942 int omnibox_start_margin = search_terms_data.OmniboxStartMargin();
943 if (omnibox_start_margin >= 0) {
944 HandleReplacement("es_sm", base::IntToString(omnibox_start_margin),
945 *i, &url);
948 break;
950 case GOOGLE_CONTEXTUAL_SEARCH_VERSION:
951 if (search_terms_args.contextual_search_params.version >= 0) {
952 HandleReplacement(
953 "ctxs",
954 base::IntToString(
955 search_terms_args.contextual_search_params.version),
957 &url);
959 break;
961 case GOOGLE_CONTEXTUAL_SEARCH_CONTEXT_DATA: {
962 DCHECK(!i->is_post_param);
963 std::string context_data;
965 const SearchTermsArgs::ContextualSearchParams& params =
966 search_terms_args.contextual_search_params;
968 if (params.start != std::string::npos) {
969 context_data.append("ctxs_start=" + base::IntToString(
970 params.start) + "&");
973 if (params.end != std::string::npos) {
974 context_data.append("ctxs_end=" + base::IntToString(
975 params.end) + "&");
978 if (!params.selection.empty())
979 context_data.append("q=" + params.selection + "&");
981 if (!params.content.empty())
982 context_data.append("ctxs_content=" + params.content + "&");
984 if (!params.base_page_url.empty())
985 context_data.append("ctxsl_url=" + params.base_page_url + "&");
987 if (!params.encoding.empty()) {
988 context_data.append("ctxs_encoding=" + params.encoding + "&");
991 context_data.append(
992 params.resolve ? "ctxsl_resolve=1" : "ctxsl_resolve=0");
994 HandleReplacement(std::string(), context_data, *i, &url);
995 break;
998 case GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION:
999 DCHECK(!i->is_post_param);
1000 if (search_terms_args.accepted_suggestion >= 0 ||
1001 !search_terms_args.assisted_query_stats.empty()) {
1002 HandleReplacement(
1003 "oq", base::UTF16ToUTF8(encoded_original_query), *i, &url);
1005 break;
1007 case GOOGLE_PAGE_CLASSIFICATION:
1008 if (search_terms_args.page_classification !=
1009 metrics::OmniboxEventProto::INVALID_SPEC) {
1010 HandleReplacement(
1011 "pgcl", base::IntToString(search_terms_args.page_classification),
1012 *i, &url);
1014 break;
1016 case GOOGLE_PREFETCH_QUERY: {
1017 const std::string& query = search_terms_args.prefetch_query;
1018 const std::string& type = search_terms_args.prefetch_query_type;
1019 if (!query.empty() && !type.empty()) {
1020 HandleReplacement(
1021 std::string(), "pfq=" + query + "&qha=" + type + "&", *i, &url);
1023 break;
1026 case GOOGLE_RLZ: {
1027 DCHECK(!i->is_post_param);
1028 // On platforms that don't have RLZ, we still want this branch
1029 // to happen so that we replace the RLZ template with the
1030 // empty string. (If we don't handle this case, we hit a
1031 // NOTREACHED below.)
1032 base::string16 rlz_string = search_terms_data.GetRlzParameterValue(
1033 search_terms_args.from_app_list);
1034 if (!rlz_string.empty()) {
1035 HandleReplacement("rlz", base::UTF16ToUTF8(rlz_string), *i, &url);
1037 break;
1040 case GOOGLE_SEARCH_CLIENT: {
1041 DCHECK(!i->is_post_param);
1042 std::string client = search_terms_data.GetSearchClient();
1043 if (!client.empty())
1044 HandleReplacement("client", client, *i, &url);
1045 break;
1048 case GOOGLE_SEARCH_FIELDTRIAL_GROUP:
1049 // We are not currently running any fieldtrials that modulate the search
1050 // url. If we do, then we'd have some conditional insert such as:
1051 // url.insert(i->index, used_www ? "gcx=w&" : "gcx=c&");
1052 break;
1054 case GOOGLE_SEARCH_VERSION:
1055 if (search_terms_data.EnableAnswersInSuggest())
1056 HandleReplacement("gs_rn", "42", *i, &url);
1057 break;
1059 case GOOGLE_SESSION_TOKEN: {
1060 std::string token = search_terms_args.session_token;
1061 if (!token.empty())
1062 HandleReplacement("psi", token, *i, &url);
1063 break;
1066 case GOOGLE_SUGGEST_CLIENT:
1067 HandleReplacement(
1068 std::string(), search_terms_data.GetSuggestClient(), *i, &url);
1069 break;
1071 case GOOGLE_SUGGEST_REQUEST_ID:
1072 HandleReplacement(
1073 std::string(), search_terms_data.GetSuggestRequestIdentifier(), *i,
1074 &url);
1075 break;
1077 case GOOGLE_UNESCAPED_SEARCH_TERMS: {
1078 std::string unescaped_terms;
1079 base::UTF16ToCodepage(search_terms_args.search_terms,
1080 input_encoding.c_str(),
1081 base::OnStringConversionError::SKIP,
1082 &unescaped_terms);
1083 HandleReplacement(std::string(), unescaped_terms, *i, &url);
1084 break;
1087 case LANGUAGE:
1088 HandleReplacement(
1089 std::string(), search_terms_data.GetApplicationLocale(), *i, &url);
1090 break;
1092 case SEARCH_TERMS:
1093 HandleReplacement(
1094 std::string(), base::UTF16ToUTF8(encoded_terms), *i, &url);
1095 break;
1097 case GOOGLE_IMAGE_THUMBNAIL:
1098 HandleReplacement(
1099 std::string(), search_terms_args.image_thumbnail_content, *i, &url);
1100 break;
1102 case GOOGLE_IMAGE_URL:
1103 if (search_terms_args.image_url.is_valid()) {
1104 HandleReplacement(
1105 std::string(), search_terms_args.image_url.spec(), *i, &url);
1107 break;
1109 case GOOGLE_IMAGE_ORIGINAL_WIDTH:
1110 if (!search_terms_args.image_original_size.IsEmpty()) {
1111 HandleReplacement(
1112 std::string(),
1113 base::IntToString(search_terms_args.image_original_size.width()),
1114 *i, &url);
1116 break;
1118 case GOOGLE_IMAGE_ORIGINAL_HEIGHT:
1119 if (!search_terms_args.image_original_size.IsEmpty()) {
1120 HandleReplacement(
1121 std::string(),
1122 base::IntToString(search_terms_args.image_original_size.height()),
1123 *i, &url);
1125 break;
1127 case GOOGLE_IMAGE_SEARCH_SOURCE:
1128 HandleReplacement(
1129 std::string(), search_terms_data.GoogleImageSearchSource(), *i,
1130 &url);
1131 break;
1133 default:
1134 NOTREACHED();
1135 break;
1139 if (!post_params_.empty())
1140 EncodeFormData(post_params_, post_content);
1142 return url;
1146 // TemplateURL ----------------------------------------------------------------
1148 TemplateURL::AssociatedExtensionInfo::AssociatedExtensionInfo(
1149 Type type,
1150 const std::string& extension_id)
1151 : type(type),
1152 extension_id(extension_id),
1153 wants_to_be_default_engine(false) {
1154 DCHECK_NE(NORMAL, type);
1157 TemplateURL::AssociatedExtensionInfo::~AssociatedExtensionInfo() {
1160 TemplateURL::TemplateURL(const TemplateURLData& data)
1161 : data_(data),
1162 url_ref_(this, TemplateURLRef::SEARCH),
1163 suggestions_url_ref_(this,
1164 TemplateURLRef::SUGGEST),
1165 instant_url_ref_(this,
1166 TemplateURLRef::INSTANT),
1167 image_url_ref_(this, TemplateURLRef::IMAGE),
1168 new_tab_url_ref_(this, TemplateURLRef::NEW_TAB),
1169 contextual_search_url_ref_(this, TemplateURLRef::CONTEXTUAL_SEARCH) {
1170 SetPrepopulateId(data_.prepopulate_id);
1172 if (data_.search_terms_replacement_key ==
1173 kGoogleInstantExtendedEnabledKeyFull) {
1174 data_.search_terms_replacement_key = google_util::kInstantExtendedAPIParam;
1178 TemplateURL::~TemplateURL() {
1181 // static
1182 base::string16 TemplateURL::GenerateKeyword(const GURL& url) {
1183 DCHECK(url.is_valid());
1184 // Strip "www." off the front of the keyword; otherwise the keyword won't work
1185 // properly. See http://code.google.com/p/chromium/issues/detail?id=6984 .
1186 // Special case: if the host was exactly "www." (not sure this can happen but
1187 // perhaps with some weird intranet and custom DNS server?), ensure we at
1188 // least don't return the empty string.
1189 base::string16 keyword(net::StripWWWFromHost(url));
1190 return keyword.empty() ? base::ASCIIToUTF16("www") : keyword;
1193 // static
1194 GURL TemplateURL::GenerateFaviconURL(const GURL& url) {
1195 DCHECK(url.is_valid());
1196 GURL::Replacements rep;
1198 const char favicon_path[] = "/favicon.ico";
1199 int favicon_path_len = arraysize(favicon_path) - 1;
1201 rep.SetPath(favicon_path, url::Component(0, favicon_path_len));
1202 rep.ClearUsername();
1203 rep.ClearPassword();
1204 rep.ClearQuery();
1205 rep.ClearRef();
1206 return url.ReplaceComponents(rep);
1209 // static
1210 bool TemplateURL::MatchesData(const TemplateURL* t_url,
1211 const TemplateURLData* data,
1212 const SearchTermsData& search_terms_data) {
1213 if (!t_url || !data)
1214 return !t_url && !data;
1216 return (t_url->short_name() == data->short_name) &&
1217 t_url->HasSameKeywordAs(*data, search_terms_data) &&
1218 (t_url->url() == data->url()) &&
1219 (t_url->suggestions_url() == data->suggestions_url) &&
1220 (t_url->instant_url() == data->instant_url) &&
1221 (t_url->image_url() == data->image_url) &&
1222 (t_url->new_tab_url() == data->new_tab_url) &&
1223 (t_url->search_url_post_params() == data->search_url_post_params) &&
1224 (t_url->suggestions_url_post_params() ==
1225 data->suggestions_url_post_params) &&
1226 (t_url->instant_url_post_params() == data->instant_url_post_params) &&
1227 (t_url->image_url_post_params() == data->image_url_post_params) &&
1228 (t_url->favicon_url() == data->favicon_url) &&
1229 (t_url->safe_for_autoreplace() == data->safe_for_autoreplace) &&
1230 (t_url->show_in_default_list() == data->show_in_default_list) &&
1231 (t_url->input_encodings() == data->input_encodings) &&
1232 (t_url->alternate_urls() == data->alternate_urls) &&
1233 (t_url->search_terms_replacement_key() ==
1234 data->search_terms_replacement_key);
1237 base::string16 TemplateURL::AdjustedShortNameForLocaleDirection() const {
1238 base::string16 bidi_safe_short_name = data_.short_name;
1239 base::i18n::AdjustStringForLocaleDirection(&bidi_safe_short_name);
1240 return bidi_safe_short_name;
1243 bool TemplateURL::ShowInDefaultList(
1244 const SearchTermsData& search_terms_data) const {
1245 return data_.show_in_default_list &&
1246 url_ref_.SupportsReplacement(search_terms_data);
1249 bool TemplateURL::SupportsReplacement(
1250 const SearchTermsData& search_terms_data) const {
1251 return url_ref_.SupportsReplacement(search_terms_data);
1254 bool TemplateURL::HasGoogleBaseURLs(
1255 const SearchTermsData& search_terms_data) const {
1256 return url_ref_.HasGoogleBaseURLs(search_terms_data) ||
1257 suggestions_url_ref_.HasGoogleBaseURLs(search_terms_data) ||
1258 instant_url_ref_.HasGoogleBaseURLs(search_terms_data) ||
1259 image_url_ref_.HasGoogleBaseURLs(search_terms_data) ||
1260 new_tab_url_ref_.HasGoogleBaseURLs(search_terms_data);
1263 bool TemplateURL::IsGoogleSearchURLWithReplaceableKeyword(
1264 const SearchTermsData& search_terms_data) const {
1265 return (GetType() == NORMAL) &&
1266 url_ref_.HasGoogleBaseURLs(search_terms_data) &&
1267 google_util::IsGoogleHostname(base::UTF16ToUTF8(data_.keyword()),
1268 google_util::DISALLOW_SUBDOMAIN);
1271 bool TemplateURL::HasSameKeywordAs(
1272 const TemplateURLData& other,
1273 const SearchTermsData& search_terms_data) const {
1274 return (data_.keyword() == other.keyword()) ||
1275 (IsGoogleSearchURLWithReplaceableKeyword(search_terms_data) &&
1276 TemplateURL(other).IsGoogleSearchURLWithReplaceableKeyword(
1277 search_terms_data));
1280 TemplateURL::Type TemplateURL::GetType() const {
1281 return extension_info_ ? extension_info_->type : NORMAL;
1284 std::string TemplateURL::GetExtensionId() const {
1285 DCHECK(extension_info_);
1286 return extension_info_->extension_id;
1289 size_t TemplateURL::URLCount() const {
1290 // Add 1 for the regular search URL.
1291 return data_.alternate_urls.size() + 1;
1294 const std::string& TemplateURL::GetURL(size_t index) const {
1295 DCHECK_LT(index, URLCount());
1297 return (index < data_.alternate_urls.size()) ?
1298 data_.alternate_urls[index] : url();
1301 bool TemplateURL::ExtractSearchTermsFromURL(
1302 const GURL& url,
1303 const SearchTermsData& search_terms_data,
1304 base::string16* search_terms) {
1305 return FindSearchTermsInURL(url, search_terms_data, search_terms, NULL, NULL);
1308 bool TemplateURL::IsSearchURL(
1309 const GURL& url,
1310 const SearchTermsData& search_terms_data) {
1311 base::string16 search_terms;
1312 return ExtractSearchTermsFromURL(url, search_terms_data, &search_terms) &&
1313 !search_terms.empty();
1316 bool TemplateURL::HasSearchTermsReplacementKey(const GURL& url) const {
1317 // Look for the key both in the query and the ref.
1318 std::string params[] = {url.query(), url.ref()};
1320 for (int i = 0; i < 2; ++i) {
1321 url::Component query, key, value;
1322 query.len = static_cast<int>(params[i].size());
1323 while (url::ExtractQueryKeyValue(params[i].c_str(), &query, &key, &value)) {
1324 if (key.is_nonempty() &&
1325 params[i].substr(key.begin, key.len) ==
1326 search_terms_replacement_key()) {
1327 return true;
1331 return false;
1334 bool TemplateURL::ReplaceSearchTermsInURL(
1335 const GURL& url,
1336 const TemplateURLRef::SearchTermsArgs& search_terms_args,
1337 const SearchTermsData& search_terms_data,
1338 GURL* result) {
1339 // TODO(beaudoin): Use AQS from |search_terms_args| too.
1340 url::Parsed::ComponentType search_term_component;
1341 url::Component search_terms_position;
1342 base::string16 search_terms;
1343 if (!FindSearchTermsInURL(url, search_terms_data, &search_terms,
1344 &search_term_component, &search_terms_position)) {
1345 return false;
1347 DCHECK(search_terms_position.is_nonempty());
1349 // FindSearchTermsInURL only returns true for search terms in the query or
1350 // ref, so we can call EncodeSearchTerm with |is_in_query| = true, since query
1351 // and ref are encoded in the same way.
1352 std::string input_encoding;
1353 base::string16 encoded_terms;
1354 base::string16 encoded_original_query;
1355 EncodeSearchTerms(search_terms_args, true, &input_encoding,
1356 &encoded_terms, &encoded_original_query);
1358 std::string old_params(
1359 (search_term_component == url::Parsed::REF) ? url.ref() : url.query());
1360 std::string new_params(old_params, 0, search_terms_position.begin);
1361 new_params += base::UTF16ToUTF8(search_terms_args.search_terms);
1362 new_params += old_params.substr(search_terms_position.end());
1363 GURL::Replacements replacements;
1364 if (search_term_component == url::Parsed::REF)
1365 replacements.SetRefStr(new_params);
1366 else
1367 replacements.SetQueryStr(new_params);
1368 *result = url.ReplaceComponents(replacements);
1369 return true;
1372 void TemplateURL::EncodeSearchTerms(
1373 const TemplateURLRef::SearchTermsArgs& search_terms_args,
1374 bool is_in_query,
1375 std::string* input_encoding,
1376 base::string16* encoded_terms,
1377 base::string16* encoded_original_query) const {
1379 std::vector<std::string> encodings(input_encodings());
1380 if (std::find(encodings.begin(), encodings.end(), "UTF-8") == encodings.end())
1381 encodings.push_back("UTF-8");
1382 for (std::vector<std::string>::const_iterator i(encodings.begin());
1383 i != encodings.end(); ++i) {
1384 if (TryEncoding(search_terms_args.search_terms,
1385 search_terms_args.original_query, i->c_str(),
1386 is_in_query, encoded_terms, encoded_original_query)) {
1387 *input_encoding = *i;
1388 return;
1391 NOTREACHED();
1394 GURL TemplateURL::GenerateSearchURL(
1395 const SearchTermsData& search_terms_data) const {
1396 if (!url_ref_.IsValid(search_terms_data))
1397 return GURL();
1399 if (!url_ref_.SupportsReplacement(search_terms_data))
1400 return GURL(url());
1402 // Use something obscure for the search terms argument so that in the rare
1403 // case the term replaces the URL it's unlikely another keyword would have the
1404 // same url.
1405 // TODO(jnd): Add additional parameters to get post data when the search URL
1406 // has post parameters.
1407 return GURL(url_ref_.ReplaceSearchTerms(
1408 TemplateURLRef::SearchTermsArgs(
1409 base::ASCIIToUTF16("blah.blah.blah.blah.blah")),
1410 search_terms_data, NULL));
1413 void TemplateURL::CopyFrom(const TemplateURL& other) {
1414 if (this == &other)
1415 return;
1417 data_ = other.data_;
1418 url_ref_.InvalidateCachedValues();
1419 suggestions_url_ref_.InvalidateCachedValues();
1420 instant_url_ref_.InvalidateCachedValues();
1421 SetPrepopulateId(other.data_.prepopulate_id);
1424 void TemplateURL::SetURL(const std::string& url) {
1425 data_.SetURL(url);
1426 url_ref_.InvalidateCachedValues();
1429 void TemplateURL::SetPrepopulateId(int id) {
1430 data_.prepopulate_id = id;
1431 const bool prepopulated = id > 0;
1432 url_ref_.prepopulated_ = prepopulated;
1433 suggestions_url_ref_.prepopulated_ = prepopulated;
1434 instant_url_ref_.prepopulated_ = prepopulated;
1437 void TemplateURL::ResetKeywordIfNecessary(
1438 const SearchTermsData& search_terms_data,
1439 bool force) {
1440 if (IsGoogleSearchURLWithReplaceableKeyword(search_terms_data) || force) {
1441 DCHECK(GetType() != OMNIBOX_API_EXTENSION);
1442 GURL url(GenerateSearchURL(search_terms_data));
1443 if (url.is_valid())
1444 data_.SetKeyword(GenerateKeyword(url));
1448 bool TemplateURL::FindSearchTermsInURL(
1449 const GURL& url,
1450 const SearchTermsData& search_terms_data,
1451 base::string16* search_terms,
1452 url::Parsed::ComponentType* search_term_component,
1453 url::Component* search_terms_position) {
1454 DCHECK(search_terms);
1455 search_terms->clear();
1457 // Try to match with every pattern.
1458 for (size_t i = 0; i < URLCount(); ++i) {
1459 TemplateURLRef ref(this, i);
1460 if (ref.ExtractSearchTermsFromURL(url, search_terms, search_terms_data,
1461 search_term_component, search_terms_position)) {
1462 // If ExtractSearchTermsFromURL() returns true and |search_terms| is empty
1463 // it means the pattern matched but no search terms were present. In this
1464 // case we fail immediately without looking for matches in subsequent
1465 // patterns. This means that given patterns
1466 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ],
1467 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would
1468 // return false. This is important for at least Google, where such URLs
1469 // are invalid.
1470 return !search_terms->empty();
1473 return false;