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"
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"
34 // The TemplateURLRef has any number of terms that need to be replaced. Each of
35 // the terms is enclosed in braces. If the character preceeding the final
36 // brace is a ?, it indicates the term is optional and can be replaced with
38 const char kStartParameter
= '{';
39 const char kEndParameter
= '}';
40 const char kOptional
= '?';
42 // Known parameters found in the URL.
43 const char kSearchTermsParameter
[] = "searchTerms";
44 const char kSearchTermsParameterFull
[] = "{searchTerms}";
45 const char kCountParameter
[] = "count";
46 const char kStartIndexParameter
[] = "startIndex";
47 const char kStartPageParameter
[] = "startPage";
48 const char kLanguageParameter
[] = "language";
49 const char kInputEncodingParameter
[] = "inputEncoding";
50 const char kOutputEncodingParameter
[] = "outputEncoding";
52 const char kGoogleAssistedQueryStatsParameter
[] = "google:assistedQueryStats";
54 // Host/Domain Google searches are relative to.
55 const char kGoogleBaseURLParameter
[] = "google:baseURL";
56 const char kGoogleBaseURLParameterFull
[] = "{google:baseURL}";
58 // Like google:baseURL, but for the Search Suggest capability.
59 const char kGoogleBaseSuggestURLParameter
[] = "google:baseSuggestURL";
60 const char kGoogleBaseSuggestURLParameterFull
[] = "{google:baseSuggestURL}";
61 const char kGoogleBookmarkBarPinnedParameter
[] = "google:bookmarkBarPinned";
62 const char kGoogleContextualSearchContextData
[] =
63 "google:contextualSearchContextData";
64 const char kGoogleContextualSearchVersion
[] = "google:contextualSearchVersion";
65 const char kGoogleCurrentPageUrlParameter
[] = "google:currentPageUrl";
66 const char kGoogleCursorPositionParameter
[] = "google:cursorPosition";
67 const char kGoogleForceInstantResultsParameter
[] = "google:forceInstantResults";
68 const char kGoogleImageSearchSource
[] = "google:imageSearchSource";
69 const char kGoogleImageThumbnailParameter
[] = "google:imageThumbnail";
70 const char kGoogleImageOriginalWidth
[] = "google:imageOriginalWidth";
71 const char kGoogleImageOriginalHeight
[] = "google:imageOriginalHeight";
72 const char kGoogleImageURLParameter
[] = "google:imageURL";
73 const char kGoogleInputTypeParameter
[] = "google:inputType";
74 const char kGoogleInstantExtendedEnabledParameter
[] =
75 "google:instantExtendedEnabledParameter";
76 const char kGoogleInstantExtendedEnabledKey
[] =
77 "google:instantExtendedEnabledKey";
78 const char kGoogleInstantExtendedEnabledKeyFull
[] =
79 "{google:instantExtendedEnabledKey}";
80 const char kGoogleNTPIsThemedParameter
[] = "google:ntpIsThemedParameter";
81 const char kGoogleOmniboxStartMarginParameter
[] =
82 "google:omniboxStartMarginParameter";
83 const char kGoogleOriginalQueryForSuggestionParameter
[] =
84 "google:originalQueryForSuggestion";
85 const char kGooglePageClassificationParameter
[] = "google:pageClassification";
86 const char kGooglePrefetchQuery
[] = "google:prefetchQuery";
87 const char kGoogleRLZParameter
[] = "google:RLZ";
88 const char kGoogleSearchClient
[] = "google:searchClient";
89 const char kGoogleSearchFieldtrialParameter
[] =
90 "google:searchFieldtrialParameter";
91 const char kGoogleSearchVersion
[] = "google:searchVersion";
92 const char kGoogleSessionToken
[] = "google:sessionToken";
93 const char kGoogleSourceIdParameter
[] = "google:sourceId";
94 const char kGoogleSuggestAPIKeyParameter
[] = "google:suggestAPIKeyParameter";
95 const char kGoogleSuggestClient
[] = "google:suggestClient";
96 const char kGoogleSuggestRequestId
[] = "google:suggestRid";
98 // Same as kSearchTermsParameter, with no escaping.
99 const char kGoogleUnescapedSearchTermsParameter
[] =
100 "google:unescapedSearchTerms";
101 const char kGoogleUnescapedSearchTermsParameterFull
[] =
102 "{google:unescapedSearchTerms}";
104 // Display value for kSearchTermsParameter.
105 const char kDisplaySearchTerms
[] = "%s";
107 // Display value for kGoogleUnescapedSearchTermsParameter.
108 const char kDisplayUnescapedSearchTerms
[] = "%S";
110 // Used if the count parameter is not optional. Indicates we want 10 search
112 const char kDefaultCount
[] = "10";
114 // Used if the parameter kOutputEncodingParameter is required.
115 const char kOutputEncodingType
[] = "UTF-8";
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
,
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
))
133 *escaped_terms
= base::UTF8ToUTF16(is_in_query
?
134 net::EscapeQueryParamValue(encoded_terms
, true) :
135 net::EscapePath(encoded_terms
));
136 if (original_query
.empty())
138 std::string encoded_original_query
;
139 if (!base::UTF16ToCodepage(original_query
, encoding
,
140 base::OnStringConversionError::SKIP
, &encoded_original_query
))
142 *escaped_original_query
= base::UTF8ToUTF16(
143 net::EscapeQueryParamValue(encoded_original_query
, true));
147 // Extract query key and host given a list of parameters coming from the URL
149 std::string
FindSearchTermsKey(const std::string
& params
) {
151 return std::string();
152 url::Component query
, key
, value
;
153 query
.len
= static_cast<int>(params
.size());
154 while (url::ExtractQueryKeyValue(params
.c_str(), &query
, &key
, &value
)) {
155 if (key
.is_nonempty() && value
.is_nonempty()) {
156 std::string value_string
= params
.substr(value
.begin
, value
.len
);
157 if (value_string
.find(kSearchTermsParameterFull
, 0) !=
159 value_string
.find(kGoogleUnescapedSearchTermsParameterFull
, 0) !=
161 return params
.substr(key
.begin
, key
.len
);
165 return std::string();
168 bool IsTemplateParameterString(const std::string
& param
) {
169 return (param
.length() > 2) && (*(param
.begin()) == kStartParameter
) &&
170 (*(param
.rbegin()) == kEndParameter
);
176 // TemplateURLRef::SearchTermsArgs --------------------------------------------
178 TemplateURLRef::SearchTermsArgs::SearchTermsArgs(
179 const base::string16
& search_terms
)
180 : search_terms(search_terms
),
181 input_type(metrics::OmniboxInputType::INVALID
),
182 accepted_suggestion(NO_SUGGESTIONS_AVAILABLE
),
183 cursor_position(base::string16::npos
),
184 omnibox_start_margin(-1),
185 page_classification(metrics::OmniboxEventProto::INVALID_SPEC
),
186 bookmark_bar_pinned(false),
187 append_extra_query_params(false),
188 force_instant_results(false),
189 from_app_list(false),
190 contextual_search_params(ContextualSearchParams()) {
193 TemplateURLRef::SearchTermsArgs::~SearchTermsArgs() {
196 TemplateURLRef::SearchTermsArgs::ContextualSearchParams::
197 ContextualSearchParams()
199 start(base::string16::npos
),
200 end(base::string16::npos
) {
203 TemplateURLRef::SearchTermsArgs::ContextualSearchParams::
204 ContextualSearchParams(
208 const std::string
& selection
,
209 const std::string
& content
,
210 const std::string
& base_page_url
,
211 const std::string
& encoding
)
215 selection(selection
),
217 base_page_url(base_page_url
),
221 TemplateURLRef::SearchTermsArgs::ContextualSearchParams::
222 ~ContextualSearchParams() {
225 // TemplateURLRef -------------------------------------------------------------
227 TemplateURLRef::TemplateURLRef(TemplateURL
* owner
, Type type
)
233 supports_replacements_(false),
234 search_term_key_location_(url::Parsed::QUERY
),
235 prepopulated_(false) {
237 DCHECK_NE(INDEXED
, type_
);
240 TemplateURLRef::TemplateURLRef(TemplateURL
* owner
, size_t index_in_owner
)
243 index_in_owner_(index_in_owner
),
246 supports_replacements_(false),
247 search_term_key_location_(url::Parsed::QUERY
),
248 prepopulated_(false) {
250 DCHECK_LT(index_in_owner_
, owner_
->URLCount());
253 TemplateURLRef::~TemplateURLRef() {
256 std::string
TemplateURLRef::GetURL() const {
258 case SEARCH
: return owner_
->url();
259 case SUGGEST
: return owner_
->suggestions_url();
260 case INSTANT
: return owner_
->instant_url();
261 case IMAGE
: return owner_
->image_url();
262 case NEW_TAB
: return owner_
->new_tab_url();
263 case CONTEXTUAL_SEARCH
: return owner_
->contextual_search_url();
264 case INDEXED
: return owner_
->GetURL(index_in_owner_
);
265 default: NOTREACHED(); return std::string(); // NOLINT
269 std::string
TemplateURLRef::GetPostParamsString() const {
272 case SEARCH
: return owner_
->search_url_post_params();
273 case SUGGEST
: return owner_
->suggestions_url_post_params();
274 case INSTANT
: return owner_
->instant_url_post_params();
275 case NEW_TAB
: return std::string();
276 case CONTEXTUAL_SEARCH
: return std::string();
277 case IMAGE
: return owner_
->image_url_post_params();
278 default: NOTREACHED(); return std::string(); // NOLINT
282 bool TemplateURLRef::UsesPOSTMethod(
283 const SearchTermsData
& search_terms_data
) const {
284 ParseIfNecessary(search_terms_data
);
285 return !post_params_
.empty();
288 bool TemplateURLRef::EncodeFormData(const PostParams
& post_params
,
289 PostContent
* post_content
) const {
290 if (post_params
.empty())
295 const char kUploadDataMIMEType
[] = "multipart/form-data; boundary=";
296 const char kMultipartBoundary
[] = "----+*+----%016" PRIx64
"----+*+----";
297 // Each name/value pair is stored in a body part which is preceded by a
298 // boundary delimiter line. Uses random number generator here to create
299 // a unique boundary delimiter for form data encoding.
300 std::string boundary
= base::StringPrintf(kMultipartBoundary
,
302 // Sets the content MIME type.
303 post_content
->first
= kUploadDataMIMEType
;
304 post_content
->first
+= boundary
;
305 // Encodes the post parameters.
306 std::string
* post_data
= &post_content
->second
;
308 for (PostParams::const_iterator param
= post_params
.begin();
309 param
!= post_params
.end(); ++param
) {
310 DCHECK(!param
->first
.empty());
311 net::AddMultipartValueForUpload(param
->first
, param
->second
, boundary
,
312 std::string(), post_data
);
314 net::AddMultipartFinalDelimiterForUpload(boundary
, post_data
);
318 bool TemplateURLRef::SupportsReplacement(
319 const SearchTermsData
& search_terms_data
) const {
320 ParseIfNecessary(search_terms_data
);
321 return valid_
&& supports_replacements_
;
324 std::string
TemplateURLRef::ReplaceSearchTerms(
325 const SearchTermsArgs
& search_terms_args
,
326 const SearchTermsData
& search_terms_data
,
327 PostContent
* post_content
) const {
328 ParseIfNecessary(search_terms_data
);
330 return std::string();
332 std::string
url(HandleReplacements(search_terms_args
, search_terms_data
,
336 if (!gurl
.is_valid())
339 std::vector
<std::string
> query_params
;
340 if (search_terms_args
.append_extra_query_params
) {
341 std::string
extra_params(
342 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
343 switches::kExtraSearchQueryParams
));
344 if (!extra_params
.empty())
345 query_params
.push_back(extra_params
);
347 if (!search_terms_args
.suggest_query_params
.empty())
348 query_params
.push_back(search_terms_args
.suggest_query_params
);
349 if (!gurl
.query().empty())
350 query_params
.push_back(gurl
.query());
352 if (query_params
.empty())
355 GURL::Replacements replacements
;
356 std::string query_str
= JoinString(query_params
, "&");
357 replacements
.SetQueryStr(query_str
);
358 return gurl
.ReplaceComponents(replacements
).possibly_invalid_spec();
361 bool TemplateURLRef::IsValid(const SearchTermsData
& search_terms_data
) const {
362 ParseIfNecessary(search_terms_data
);
366 base::string16
TemplateURLRef::DisplayURL(
367 const SearchTermsData
& search_terms_data
) const {
368 ParseIfNecessary(search_terms_data
);
369 base::string16
result(base::UTF8ToUTF16(GetURL()));
370 if (valid_
&& !replacements_
.empty()) {
371 ReplaceSubstringsAfterOffset(&result
, 0,
372 base::ASCIIToUTF16(kSearchTermsParameterFull
),
373 base::ASCIIToUTF16(kDisplaySearchTerms
));
374 ReplaceSubstringsAfterOffset(&result
, 0,
375 base::ASCIIToUTF16(kGoogleUnescapedSearchTermsParameterFull
),
376 base::ASCIIToUTF16(kDisplayUnescapedSearchTerms
));
382 std::string
TemplateURLRef::DisplayURLToURLRef(
383 const base::string16
& display_url
) {
384 base::string16 result
= display_url
;
385 ReplaceSubstringsAfterOffset(&result
, 0,
386 base::ASCIIToUTF16(kDisplaySearchTerms
),
387 base::ASCIIToUTF16(kSearchTermsParameterFull
));
388 ReplaceSubstringsAfterOffset(
390 base::ASCIIToUTF16(kDisplayUnescapedSearchTerms
),
391 base::ASCIIToUTF16(kGoogleUnescapedSearchTermsParameterFull
));
392 return base::UTF16ToUTF8(result
);
395 const std::string
& TemplateURLRef::GetHost(
396 const SearchTermsData
& search_terms_data
) const {
397 ParseIfNecessary(search_terms_data
);
401 const std::string
& TemplateURLRef::GetPath(
402 const SearchTermsData
& search_terms_data
) const {
403 ParseIfNecessary(search_terms_data
);
407 const std::string
& TemplateURLRef::GetSearchTermKey(
408 const SearchTermsData
& search_terms_data
) const {
409 ParseIfNecessary(search_terms_data
);
410 return search_term_key_
;
413 base::string16
TemplateURLRef::SearchTermToString16(
414 const std::string
& term
) const {
415 const std::vector
<std::string
>& encodings
= owner_
->input_encodings();
416 base::string16 result
;
418 std::string unescaped
= net::UnescapeURLComponent(
420 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE
|
421 net::UnescapeRule::URL_SPECIAL_CHARS
);
422 for (size_t i
= 0; i
< encodings
.size(); ++i
) {
423 if (base::CodepageToUTF16(unescaped
, encodings
[i
].c_str(),
424 base::OnStringConversionError::FAIL
, &result
))
428 // Always fall back on UTF-8 if it works.
429 if (base::CodepageToUTF16(unescaped
, base::kCodepageUTF8
,
430 base::OnStringConversionError::FAIL
, &result
))
433 // When nothing worked, just use the escaped text. We have no idea what the
434 // encoding is. We need to substitute spaces for pluses ourselves since we're
435 // not sending it through an unescaper.
436 result
= base::UTF8ToUTF16(term
);
437 std::replace(result
.begin(), result
.end(), '+', ' ');
441 bool TemplateURLRef::HasGoogleBaseURLs(
442 const SearchTermsData
& search_terms_data
) const {
443 ParseIfNecessary(search_terms_data
);
444 for (size_t i
= 0; i
< replacements_
.size(); ++i
) {
445 if ((replacements_
[i
].type
== GOOGLE_BASE_URL
) ||
446 (replacements_
[i
].type
== GOOGLE_BASE_SUGGEST_URL
))
452 bool TemplateURLRef::ExtractSearchTermsFromURL(
454 base::string16
* search_terms
,
455 const SearchTermsData
& search_terms_data
,
456 url::Parsed::ComponentType
* search_terms_component
,
457 url::Component
* search_terms_position
) const {
458 DCHECK(search_terms
);
459 search_terms
->clear();
461 ParseIfNecessary(search_terms_data
);
463 // We need a search term in the template URL to extract something.
464 if (search_term_key_
.empty())
467 // TODO(beaudoin): Support patterns of the form http://foo/{searchTerms}/
468 // See crbug.com/153798
470 // Fill-in the replacements. We don't care about search terms in the pattern,
471 // so we use the empty string.
472 // Currently we assume the search term only shows in URL, not in post params.
473 GURL
pattern(ReplaceSearchTerms(SearchTermsArgs(base::string16()),
474 search_terms_data
, NULL
));
475 // Host, path and port must match.
476 if (url
.port() != pattern
.port() ||
477 url
.host() != host_
||
478 url
.path() != path_
) {
482 // Parameter must be present either in the query or the ref.
483 const std::string
& params(
484 (search_term_key_location_
== url::Parsed::QUERY
) ?
485 url
.query() : url
.ref());
487 url::Component query
, key
, value
;
488 query
.len
= static_cast<int>(params
.size());
489 bool key_found
= false;
490 while (url::ExtractQueryKeyValue(params
.c_str(), &query
, &key
, &value
)) {
491 if (key
.is_nonempty()) {
492 if (params
.substr(key
.begin
, key
.len
) == search_term_key_
) {
493 // Fail if search term key is found twice.
495 search_terms
->clear();
499 // Extract the search term.
500 *search_terms
= net::UnescapeAndDecodeUTF8URLComponent(
501 params
.substr(value
.begin
, value
.len
),
502 net::UnescapeRule::SPACES
|
503 net::UnescapeRule::URL_SPECIAL_CHARS
|
504 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE
);
505 if (search_terms_component
)
506 *search_terms_component
= search_term_key_location_
;
507 if (search_terms_position
)
508 *search_terms_position
= value
;
515 void TemplateURLRef::InvalidateCachedValues() const {
516 supports_replacements_
= valid_
= parsed_
= false;
519 search_term_key_
.clear();
520 replacements_
.clear();
521 post_params_
.clear();
524 bool TemplateURLRef::ParseParameter(size_t start
,
527 Replacements
* replacements
) const {
528 DCHECK(start
!= std::string::npos
&&
529 end
!= std::string::npos
&& end
> start
);
530 size_t length
= end
- start
- 1;
531 bool optional
= false;
532 if ((*url
)[end
- 1] == kOptional
) {
536 std::string
parameter(url
->substr(start
+ 1, length
));
537 std::string
full_parameter(url
->substr(start
, end
- start
+ 1));
538 // Remove the parameter from the string. For parameters who replacement is
539 // constant and already known, just replace them directly. For other cases,
540 // like parameters whose values may change over time, use |replacements|.
541 url
->erase(start
, end
- start
+ 1);
542 if (parameter
== kSearchTermsParameter
) {
543 replacements
->push_back(Replacement(SEARCH_TERMS
, start
));
544 } else if (parameter
== kCountParameter
) {
546 url
->insert(start
, kDefaultCount
);
547 } else if (parameter
== kGoogleAssistedQueryStatsParameter
) {
548 replacements
->push_back(Replacement(GOOGLE_ASSISTED_QUERY_STATS
, start
));
549 } else if (parameter
== kGoogleBaseURLParameter
) {
550 replacements
->push_back(Replacement(GOOGLE_BASE_URL
, start
));
551 } else if (parameter
== kGoogleBaseSuggestURLParameter
) {
552 replacements
->push_back(Replacement(GOOGLE_BASE_SUGGEST_URL
, start
));
553 } else if (parameter
== kGoogleBookmarkBarPinnedParameter
) {
554 replacements
->push_back(Replacement(GOOGLE_BOOKMARK_BAR_PINNED
, start
));
555 } else if (parameter
== kGoogleCurrentPageUrlParameter
) {
556 replacements
->push_back(Replacement(GOOGLE_CURRENT_PAGE_URL
, start
));
557 } else if (parameter
== kGoogleCursorPositionParameter
) {
558 replacements
->push_back(Replacement(GOOGLE_CURSOR_POSITION
, start
));
559 } else if (parameter
== kGoogleForceInstantResultsParameter
) {
560 replacements
->push_back(Replacement(GOOGLE_FORCE_INSTANT_RESULTS
, start
));
561 } else if (parameter
== kGoogleImageOriginalHeight
) {
562 replacements
->push_back(
563 Replacement(TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_HEIGHT
, start
));
564 } else if (parameter
== kGoogleImageOriginalWidth
) {
565 replacements
->push_back(
566 Replacement(TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH
, start
));
567 } else if (parameter
== kGoogleImageSearchSource
) {
568 replacements
->push_back(
569 Replacement(TemplateURLRef::GOOGLE_IMAGE_SEARCH_SOURCE
, start
));
570 } else if (parameter
== kGoogleImageThumbnailParameter
) {
571 replacements
->push_back(
572 Replacement(TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL
, start
));
573 } else if (parameter
== kGoogleImageURLParameter
) {
574 replacements
->push_back(Replacement(TemplateURLRef::GOOGLE_IMAGE_URL
,
576 } else if (parameter
== kGoogleInputTypeParameter
) {
577 replacements
->push_back(Replacement(TemplateURLRef::GOOGLE_INPUT_TYPE
,
579 } else if (parameter
== kGoogleInstantExtendedEnabledParameter
) {
580 replacements
->push_back(Replacement(GOOGLE_INSTANT_EXTENDED_ENABLED
,
582 } else if (parameter
== kGoogleInstantExtendedEnabledKey
) {
583 url
->insert(start
, google_util::kInstantExtendedAPIParam
);
584 } else if (parameter
== kGoogleNTPIsThemedParameter
) {
585 replacements
->push_back(Replacement(GOOGLE_NTP_IS_THEMED
, start
));
586 } else if (parameter
== kGoogleOmniboxStartMarginParameter
) {
587 replacements
->push_back(Replacement(GOOGLE_OMNIBOX_START_MARGIN
, start
));
588 } else if (parameter
== kGoogleContextualSearchVersion
) {
589 replacements
->push_back(
590 Replacement(GOOGLE_CONTEXTUAL_SEARCH_VERSION
, start
));
591 } else if (parameter
== kGoogleContextualSearchContextData
) {
592 replacements
->push_back(
593 Replacement(GOOGLE_CONTEXTUAL_SEARCH_CONTEXT_DATA
, start
));
594 } else if (parameter
== kGoogleOriginalQueryForSuggestionParameter
) {
595 replacements
->push_back(Replacement(GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION
,
597 } else if (parameter
== kGooglePageClassificationParameter
) {
598 replacements
->push_back(Replacement(GOOGLE_PAGE_CLASSIFICATION
, start
));
599 } else if (parameter
== kGooglePrefetchQuery
) {
600 replacements
->push_back(Replacement(GOOGLE_PREFETCH_QUERY
, start
));
601 } else if (parameter
== kGoogleRLZParameter
) {
602 replacements
->push_back(Replacement(GOOGLE_RLZ
, start
));
603 } else if (parameter
== kGoogleSearchClient
) {
604 replacements
->push_back(Replacement(GOOGLE_SEARCH_CLIENT
, start
));
605 } else if (parameter
== kGoogleSearchFieldtrialParameter
) {
606 replacements
->push_back(Replacement(GOOGLE_SEARCH_FIELDTRIAL_GROUP
, start
));
607 } else if (parameter
== kGoogleSearchVersion
) {
608 replacements
->push_back(Replacement(GOOGLE_SEARCH_VERSION
, start
));
609 } else if (parameter
== kGoogleSessionToken
) {
610 replacements
->push_back(Replacement(GOOGLE_SESSION_TOKEN
, start
));
611 } else if (parameter
== kGoogleSourceIdParameter
) {
612 #if defined(OS_ANDROID)
613 url
->insert(start
, "sourceid=chrome-mobile&");
615 url
->insert(start
, "sourceid=chrome&");
617 } else if (parameter
== kGoogleSuggestAPIKeyParameter
) {
619 net::EscapeQueryParamValue(google_apis::GetAPIKey(), false));
620 } else if (parameter
== kGoogleSuggestClient
) {
621 replacements
->push_back(Replacement(GOOGLE_SUGGEST_CLIENT
, start
));
622 } else if (parameter
== kGoogleSuggestRequestId
) {
623 replacements
->push_back(Replacement(GOOGLE_SUGGEST_REQUEST_ID
, start
));
624 } else if (parameter
== kGoogleUnescapedSearchTermsParameter
) {
625 replacements
->push_back(Replacement(GOOGLE_UNESCAPED_SEARCH_TERMS
, start
));
626 } else if (parameter
== kInputEncodingParameter
) {
627 replacements
->push_back(Replacement(ENCODING
, start
));
628 } else if (parameter
== kLanguageParameter
) {
629 replacements
->push_back(Replacement(LANGUAGE
, start
));
630 } else if (parameter
== kOutputEncodingParameter
) {
632 url
->insert(start
, kOutputEncodingType
);
633 } else if ((parameter
== kStartIndexParameter
) ||
634 (parameter
== kStartPageParameter
)) {
635 // We don't support these.
637 url
->insert(start
, "1");
638 } else if (!prepopulated_
) {
639 // If it's a prepopulated URL, we know that it's safe to remove unknown
640 // parameters, so just ignore this and return true below. Otherwise it could
641 // be some garbage but can also be a javascript block. Put it back.
642 url
->insert(start
, full_parameter
);
648 std::string
TemplateURLRef::ParseURL(const std::string
& url
,
649 Replacements
* replacements
,
650 PostParams
* post_params
,
653 std::string parsed_url
= url
;
654 for (size_t last
= 0; last
!= std::string::npos
; ) {
655 last
= parsed_url
.find(kStartParameter
, last
);
656 if (last
!= std::string::npos
) {
657 size_t template_end
= parsed_url
.find(kEndParameter
, last
);
658 if (template_end
!= std::string::npos
) {
659 // Since we allow Javascript in the URL, {} pairs could be nested. Match
660 // only leaf pairs with supported parameters.
661 size_t next_template_start
= parsed_url
.find(kStartParameter
, last
+ 1);
662 if (next_template_start
== std::string::npos
||
663 next_template_start
> template_end
) {
664 // If successful, ParseParameter erases from the string as such no
665 // need to update |last|. If failed, move |last| to the end of pair.
666 if (!ParseParameter(last
, template_end
, &parsed_url
, replacements
)) {
667 // |template_end| + 1 may be beyond the end of the string.
671 last
= next_template_start
;
674 // Open brace without a closing brace, return.
675 return std::string();
680 // Handles the post parameters.
681 const std::string
& post_params_string
= GetPostParamsString();
682 if (!post_params_string
.empty()) {
683 typedef std::vector
<std::string
> Strings
;
685 base::SplitString(post_params_string
, ',', ¶m_list
);
687 for (Strings::const_iterator iterator
= param_list
.begin();
688 iterator
!= param_list
.end(); ++iterator
) {
690 // The '=' delimiter is required and the name must be not empty.
691 base::SplitString(*iterator
, '=', &parts
);
692 if ((parts
.size() != 2U) || parts
[0].empty())
693 return std::string();
695 std::string
& value
= parts
[1];
696 size_t replacements_size
= replacements
->size();
697 if (IsTemplateParameterString(value
))
698 ParseParameter(0, value
.length() - 1, &value
, replacements
);
699 post_params
->push_back(std::make_pair(parts
[0], value
));
700 // If there was a replacement added, points its index to last added
702 if (replacements
->size() > replacements_size
) {
703 DCHECK_EQ(replacements_size
+ 1, replacements
->size());
704 Replacement
* r
= &replacements
->back();
705 r
->is_post_param
= true;
706 r
->index
= post_params
->size() - 1;
709 DCHECK(!post_params
->empty());
716 void TemplateURLRef::ParseIfNecessary(
717 const SearchTermsData
& search_terms_data
) const {
719 InvalidateCachedValues();
721 parsed_url_
= ParseURL(GetURL(), &replacements_
, &post_params_
, &valid_
);
722 supports_replacements_
= false;
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;
733 has_only_one_search_term
= true;
734 supports_replacements_
= true;
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
);
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());
755 search_term_key_
.clear();
758 search_term_key_location_
= url::Parsed::REF
;
760 GURL
url(url_string
);
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_
=
770 query_key
.empty() ? url::Parsed::REF
: url::Parsed::QUERY
;
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
;
785 url
->insert(pos
, name
.empty() ? value
: (name
+ "=" + value
+ "&"));
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
);
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
);
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
);
818 std::string url
= parsed_url_
;
820 // replacements_ is ordered in ascending order, as such we need to iterate
822 for (Replacements::reverse_iterator i
= replacements_
.rbegin();
823 i
!= replacements_
.rend(); ++i
) {
826 HandleReplacement(std::string(), input_encoding
, *i
, &url
);
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(ReplaceSearchTerms(
839 search_terms_args_without_aqs
, search_terms_data
, NULL
));
840 if (base_url
.SchemeIs(url::kHttpsScheme
)) {
842 "aqs", search_terms_args
.assisted_query_stats
, *i
, &url
);
847 case GOOGLE_BASE_URL
:
848 DCHECK(!i
->is_post_param
);
850 std::string(), search_terms_data
.GoogleBaseURLValue(), *i
, &url
);
853 case GOOGLE_BASE_SUGGEST_URL
:
854 DCHECK(!i
->is_post_param
);
856 std::string(), search_terms_data
.GoogleBaseSuggestURLValue(), *i
,
860 case GOOGLE_BOOKMARK_BAR_PINNED
:
861 if (search_terms_data
.IsShowingSearchTermsOnSearchResultsPages()) {
862 // Log whether the bookmark bar is pinned when the user is seeing
863 // InstantExtended on the SRP.
864 DCHECK(!i
->is_post_param
);
866 "bmbp", search_terms_args
.bookmark_bar_pinned
? "1" : "0", *i
,
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
,
877 HandleReplacement("url", escaped_current_page_url
, *i
, &url
);
881 case GOOGLE_CURSOR_POSITION
:
882 DCHECK(!i
->is_post_param
);
883 if (search_terms_args
.cursor_position
!= base::string16::npos
)
886 base::StringPrintf("%" PRIuS
, search_terms_args
.cursor_position
),
891 case GOOGLE_FORCE_INSTANT_RESULTS
:
892 DCHECK(!i
->is_post_param
);
893 HandleReplacement(std::string(),
894 search_terms_data
.ForceInstantResultsParam(
895 search_terms_args
.force_instant_results
),
900 case GOOGLE_INPUT_TYPE
:
901 DCHECK(!i
->is_post_param
);
903 "oit", base::IntToString(search_terms_args
.input_type
), *i
, &url
);
906 case GOOGLE_INSTANT_EXTENDED_ENABLED
:
907 DCHECK(!i
->is_post_param
);
908 HandleReplacement(std::string(),
909 search_terms_data
.InstantExtendedEnabledParam(
915 case GOOGLE_NTP_IS_THEMED
:
916 DCHECK(!i
->is_post_param
);
918 std::string(), search_terms_data
.NTPIsThemedParam(), *i
, &url
);
921 case GOOGLE_OMNIBOX_START_MARGIN
:
922 DCHECK(!i
->is_post_param
);
923 if (search_terms_args
.omnibox_start_margin
>= 0) {
926 base::IntToString(search_terms_args
.omnibox_start_margin
),
932 case GOOGLE_CONTEXTUAL_SEARCH_VERSION
:
933 if (search_terms_args
.contextual_search_params
.version
>= 0) {
937 search_terms_args
.contextual_search_params
.version
),
943 case GOOGLE_CONTEXTUAL_SEARCH_CONTEXT_DATA
: {
944 DCHECK(!i
->is_post_param
);
945 std::string context_data
;
947 const SearchTermsArgs::ContextualSearchParams
& params
=
948 search_terms_args
.contextual_search_params
;
950 if (params
.start
!= std::string::npos
) {
951 context_data
.append("ctxs_start=" + base::IntToString(
952 params
.start
) + "&");
955 if (params
.end
!= std::string::npos
) {
956 context_data
.append("ctxs_end=" + base::IntToString(
960 if (!params
.selection
.empty())
961 context_data
.append("q=" + params
.selection
+ "&");
963 if (!params
.content
.empty())
964 context_data
.append("ctxs_content=" + params
.content
+ "&");
966 if (!params
.base_page_url
.empty())
967 context_data
.append("ctxs_url=" + params
.base_page_url
+ "&");
969 if (!params
.encoding
.empty()) {
970 context_data
.append("ctxs_encoding=" + params
.encoding
+ "&");
973 HandleReplacement(std::string(), context_data
, *i
, &url
);
977 case GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION
:
978 DCHECK(!i
->is_post_param
);
979 if (search_terms_args
.accepted_suggestion
>= 0 ||
980 !search_terms_args
.assisted_query_stats
.empty()) {
982 "oq", base::UTF16ToUTF8(encoded_original_query
), *i
, &url
);
986 case GOOGLE_PAGE_CLASSIFICATION
:
987 if (search_terms_args
.page_classification
!=
988 metrics::OmniboxEventProto::INVALID_SPEC
) {
990 "pgcl", base::IntToString(search_terms_args
.page_classification
),
995 case GOOGLE_PREFETCH_QUERY
: {
996 const std::string
& query
= search_terms_args
.prefetch_query
;
997 const std::string
& type
= search_terms_args
.prefetch_query_type
;
998 if (!query
.empty() && !type
.empty()) {
1000 std::string(), "pfq=" + query
+ "&qha=" + type
+ "&", *i
, &url
);
1006 DCHECK(!i
->is_post_param
);
1007 // On platforms that don't have RLZ, we still want this branch
1008 // to happen so that we replace the RLZ template with the
1009 // empty string. (If we don't handle this case, we hit a
1010 // NOTREACHED below.)
1011 base::string16 rlz_string
= search_terms_data
.GetRlzParameterValue(
1012 search_terms_args
.from_app_list
);
1013 if (!rlz_string
.empty()) {
1014 HandleReplacement("rlz", base::UTF16ToUTF8(rlz_string
), *i
, &url
);
1019 case GOOGLE_SEARCH_CLIENT
: {
1020 DCHECK(!i
->is_post_param
);
1021 std::string client
= search_terms_data
.GetSearchClient();
1022 if (!client
.empty())
1023 HandleReplacement("client", client
, *i
, &url
);
1027 case GOOGLE_SEARCH_FIELDTRIAL_GROUP
:
1028 // We are not currently running any fieldtrials that modulate the search
1029 // url. If we do, then we'd have some conditional insert such as:
1030 // url.insert(i->index, used_www ? "gcx=w&" : "gcx=c&");
1033 case GOOGLE_SEARCH_VERSION
:
1034 if (search_terms_data
.EnableAnswersInSuggest())
1035 HandleReplacement("gs_rn", "42", *i
, &url
);
1038 case GOOGLE_SESSION_TOKEN
: {
1039 std::string token
= search_terms_args
.session_token
;
1041 HandleReplacement("psi", token
, *i
, &url
);
1045 case GOOGLE_SUGGEST_CLIENT
:
1047 std::string(), search_terms_data
.GetSuggestClient(), *i
, &url
);
1050 case GOOGLE_SUGGEST_REQUEST_ID
:
1052 std::string(), search_terms_data
.GetSuggestRequestIdentifier(), *i
,
1056 case GOOGLE_UNESCAPED_SEARCH_TERMS
: {
1057 std::string unescaped_terms
;
1058 base::UTF16ToCodepage(search_terms_args
.search_terms
,
1059 input_encoding
.c_str(),
1060 base::OnStringConversionError::SKIP
,
1062 HandleReplacement(std::string(), unescaped_terms
, *i
, &url
);
1068 std::string(), search_terms_data
.GetApplicationLocale(), *i
, &url
);
1073 std::string(), base::UTF16ToUTF8(encoded_terms
), *i
, &url
);
1076 case GOOGLE_IMAGE_THUMBNAIL
:
1078 std::string(), search_terms_args
.image_thumbnail_content
, *i
, &url
);
1081 case GOOGLE_IMAGE_URL
:
1082 if (search_terms_args
.image_url
.is_valid()) {
1084 std::string(), search_terms_args
.image_url
.spec(), *i
, &url
);
1088 case GOOGLE_IMAGE_ORIGINAL_WIDTH
:
1089 if (!search_terms_args
.image_original_size
.IsEmpty()) {
1092 base::IntToString(search_terms_args
.image_original_size
.width()),
1097 case GOOGLE_IMAGE_ORIGINAL_HEIGHT
:
1098 if (!search_terms_args
.image_original_size
.IsEmpty()) {
1101 base::IntToString(search_terms_args
.image_original_size
.height()),
1106 case GOOGLE_IMAGE_SEARCH_SOURCE
:
1108 std::string(), search_terms_data
.GoogleImageSearchSource(), *i
,
1118 if (!post_params_
.empty())
1119 EncodeFormData(post_params_
, post_content
);
1125 // TemplateURL ----------------------------------------------------------------
1127 TemplateURL::AssociatedExtensionInfo::AssociatedExtensionInfo(
1129 const std::string
& extension_id
)
1131 extension_id(extension_id
),
1132 wants_to_be_default_engine(false) {
1133 DCHECK_NE(NORMAL
, type
);
1136 TemplateURL::AssociatedExtensionInfo::~AssociatedExtensionInfo() {
1139 TemplateURL::TemplateURL(const TemplateURLData
& data
)
1141 url_ref_(this, TemplateURLRef::SEARCH
),
1142 suggestions_url_ref_(this,
1143 TemplateURLRef::SUGGEST
),
1144 instant_url_ref_(this,
1145 TemplateURLRef::INSTANT
),
1146 image_url_ref_(this, TemplateURLRef::IMAGE
),
1147 new_tab_url_ref_(this, TemplateURLRef::NEW_TAB
),
1148 contextual_search_url_ref_(this, TemplateURLRef::CONTEXTUAL_SEARCH
) {
1149 SetPrepopulateId(data_
.prepopulate_id
);
1151 if (data_
.search_terms_replacement_key
==
1152 kGoogleInstantExtendedEnabledKeyFull
) {
1153 data_
.search_terms_replacement_key
= google_util::kInstantExtendedAPIParam
;
1157 TemplateURL::~TemplateURL() {
1161 base::string16
TemplateURL::GenerateKeyword(const GURL
& url
) {
1162 DCHECK(url
.is_valid());
1163 // Strip "www." off the front of the keyword; otherwise the keyword won't work
1164 // properly. See http://code.google.com/p/chromium/issues/detail?id=6984 .
1165 // Special case: if the host was exactly "www." (not sure this can happen but
1166 // perhaps with some weird intranet and custom DNS server?), ensure we at
1167 // least don't return the empty string.
1168 base::string16
keyword(net::StripWWWFromHost(url
));
1169 return keyword
.empty() ? base::ASCIIToUTF16("www") : keyword
;
1173 GURL
TemplateURL::GenerateFaviconURL(const GURL
& url
) {
1174 DCHECK(url
.is_valid());
1175 GURL::Replacements rep
;
1177 const char favicon_path
[] = "/favicon.ico";
1178 int favicon_path_len
= arraysize(favicon_path
) - 1;
1180 rep
.SetPath(favicon_path
, url::Component(0, favicon_path_len
));
1181 rep
.ClearUsername();
1182 rep
.ClearPassword();
1185 return url
.ReplaceComponents(rep
);
1189 bool TemplateURL::MatchesData(const TemplateURL
* t_url
,
1190 const TemplateURLData
* data
,
1191 const SearchTermsData
& search_terms_data
) {
1192 if (!t_url
|| !data
)
1193 return !t_url
&& !data
;
1195 return (t_url
->short_name() == data
->short_name
) &&
1196 t_url
->HasSameKeywordAs(*data
, search_terms_data
) &&
1197 (t_url
->url() == data
->url()) &&
1198 (t_url
->suggestions_url() == data
->suggestions_url
) &&
1199 (t_url
->instant_url() == data
->instant_url
) &&
1200 (t_url
->image_url() == data
->image_url
) &&
1201 (t_url
->new_tab_url() == data
->new_tab_url
) &&
1202 (t_url
->search_url_post_params() == data
->search_url_post_params
) &&
1203 (t_url
->suggestions_url_post_params() ==
1204 data
->suggestions_url_post_params
) &&
1205 (t_url
->instant_url_post_params() == data
->instant_url_post_params
) &&
1206 (t_url
->image_url_post_params() == data
->image_url_post_params
) &&
1207 (t_url
->favicon_url() == data
->favicon_url
) &&
1208 (t_url
->safe_for_autoreplace() == data
->safe_for_autoreplace
) &&
1209 (t_url
->show_in_default_list() == data
->show_in_default_list
) &&
1210 (t_url
->input_encodings() == data
->input_encodings
) &&
1211 (t_url
->alternate_urls() == data
->alternate_urls
) &&
1212 (t_url
->search_terms_replacement_key() ==
1213 data
->search_terms_replacement_key
);
1216 base::string16
TemplateURL::AdjustedShortNameForLocaleDirection() const {
1217 base::string16 bidi_safe_short_name
= data_
.short_name
;
1218 base::i18n::AdjustStringForLocaleDirection(&bidi_safe_short_name
);
1219 return bidi_safe_short_name
;
1222 bool TemplateURL::ShowInDefaultList(
1223 const SearchTermsData
& search_terms_data
) const {
1224 return data_
.show_in_default_list
&&
1225 url_ref_
.SupportsReplacement(search_terms_data
);
1228 bool TemplateURL::SupportsReplacement(
1229 const SearchTermsData
& search_terms_data
) const {
1230 return url_ref_
.SupportsReplacement(search_terms_data
);
1233 bool TemplateURL::HasGoogleBaseURLs(
1234 const SearchTermsData
& search_terms_data
) const {
1235 return url_ref_
.HasGoogleBaseURLs(search_terms_data
) ||
1236 suggestions_url_ref_
.HasGoogleBaseURLs(search_terms_data
) ||
1237 instant_url_ref_
.HasGoogleBaseURLs(search_terms_data
) ||
1238 image_url_ref_
.HasGoogleBaseURLs(search_terms_data
) ||
1239 new_tab_url_ref_
.HasGoogleBaseURLs(search_terms_data
);
1242 bool TemplateURL::IsGoogleSearchURLWithReplaceableKeyword(
1243 const SearchTermsData
& search_terms_data
) const {
1244 return (GetType() == NORMAL
) &&
1245 url_ref_
.HasGoogleBaseURLs(search_terms_data
) &&
1246 google_util::IsGoogleHostname(base::UTF16ToUTF8(data_
.keyword()),
1247 google_util::DISALLOW_SUBDOMAIN
);
1250 bool TemplateURL::HasSameKeywordAs(
1251 const TemplateURLData
& other
,
1252 const SearchTermsData
& search_terms_data
) const {
1253 return (data_
.keyword() == other
.keyword()) ||
1254 (IsGoogleSearchURLWithReplaceableKeyword(search_terms_data
) &&
1255 TemplateURL(other
).IsGoogleSearchURLWithReplaceableKeyword(
1256 search_terms_data
));
1259 TemplateURL::Type
TemplateURL::GetType() const {
1260 return extension_info_
? extension_info_
->type
: NORMAL
;
1263 std::string
TemplateURL::GetExtensionId() const {
1264 DCHECK(extension_info_
);
1265 return extension_info_
->extension_id
;
1268 size_t TemplateURL::URLCount() const {
1269 // Add 1 for the regular search URL.
1270 return data_
.alternate_urls
.size() + 1;
1273 const std::string
& TemplateURL::GetURL(size_t index
) const {
1274 DCHECK_LT(index
, URLCount());
1276 return (index
< data_
.alternate_urls
.size()) ?
1277 data_
.alternate_urls
[index
] : url();
1280 bool TemplateURL::ExtractSearchTermsFromURL(
1282 const SearchTermsData
& search_terms_data
,
1283 base::string16
* search_terms
) {
1284 return FindSearchTermsInURL(url
, search_terms_data
, search_terms
, NULL
, NULL
);
1287 bool TemplateURL::IsSearchURL(
1289 const SearchTermsData
& search_terms_data
) {
1290 base::string16 search_terms
;
1291 return ExtractSearchTermsFromURL(url
, search_terms_data
, &search_terms
) &&
1292 !search_terms
.empty();
1295 bool TemplateURL::HasSearchTermsReplacementKey(const GURL
& url
) const {
1296 // Look for the key both in the query and the ref.
1297 std::string params
[] = {url
.query(), url
.ref()};
1299 for (int i
= 0; i
< 2; ++i
) {
1300 url::Component query
, key
, value
;
1301 query
.len
= static_cast<int>(params
[i
].size());
1302 while (url::ExtractQueryKeyValue(params
[i
].c_str(), &query
, &key
, &value
)) {
1303 if (key
.is_nonempty() &&
1304 params
[i
].substr(key
.begin
, key
.len
) ==
1305 search_terms_replacement_key()) {
1313 bool TemplateURL::ReplaceSearchTermsInURL(
1315 const TemplateURLRef::SearchTermsArgs
& search_terms_args
,
1316 const SearchTermsData
& search_terms_data
,
1318 // TODO(beaudoin): Use AQS from |search_terms_args| too.
1319 url::Parsed::ComponentType search_term_component
;
1320 url::Component search_terms_position
;
1321 base::string16 search_terms
;
1322 if (!FindSearchTermsInURL(url
, search_terms_data
, &search_terms
,
1323 &search_term_component
, &search_terms_position
)) {
1326 DCHECK(search_terms_position
.is_nonempty());
1328 // FindSearchTermsInURL only returns true for search terms in the query or
1329 // ref, so we can call EncodeSearchTerm with |is_in_query| = true, since query
1330 // and ref are encoded in the same way.
1331 std::string input_encoding
;
1332 base::string16 encoded_terms
;
1333 base::string16 encoded_original_query
;
1334 EncodeSearchTerms(search_terms_args
, true, &input_encoding
,
1335 &encoded_terms
, &encoded_original_query
);
1337 std::string
old_params(
1338 (search_term_component
== url::Parsed::REF
) ? url
.ref() : url
.query());
1339 std::string
new_params(old_params
, 0, search_terms_position
.begin
);
1340 new_params
+= base::UTF16ToUTF8(search_terms_args
.search_terms
);
1341 new_params
+= old_params
.substr(search_terms_position
.end());
1342 url::StdStringReplacements
<std::string
> replacements
;
1343 if (search_term_component
== url::Parsed::REF
)
1344 replacements
.SetRefStr(new_params
);
1346 replacements
.SetQueryStr(new_params
);
1347 *result
= url
.ReplaceComponents(replacements
);
1351 void TemplateURL::EncodeSearchTerms(
1352 const TemplateURLRef::SearchTermsArgs
& search_terms_args
,
1354 std::string
* input_encoding
,
1355 base::string16
* encoded_terms
,
1356 base::string16
* encoded_original_query
) const {
1358 std::vector
<std::string
> encodings(input_encodings());
1359 if (std::find(encodings
.begin(), encodings
.end(), "UTF-8") == encodings
.end())
1360 encodings
.push_back("UTF-8");
1361 for (std::vector
<std::string
>::const_iterator
i(encodings
.begin());
1362 i
!= encodings
.end(); ++i
) {
1363 if (TryEncoding(search_terms_args
.search_terms
,
1364 search_terms_args
.original_query
, i
->c_str(),
1365 is_in_query
, encoded_terms
, encoded_original_query
)) {
1366 *input_encoding
= *i
;
1373 GURL
TemplateURL::GenerateSearchURL(
1374 const SearchTermsData
& search_terms_data
) const {
1375 if (!url_ref_
.IsValid(search_terms_data
))
1378 if (!url_ref_
.SupportsReplacement(search_terms_data
))
1381 // Use something obscure for the search terms argument so that in the rare
1382 // case the term replaces the URL it's unlikely another keyword would have the
1384 // TODO(jnd): Add additional parameters to get post data when the search URL
1385 // has post parameters.
1386 return GURL(url_ref_
.ReplaceSearchTerms(
1387 TemplateURLRef::SearchTermsArgs(
1388 base::ASCIIToUTF16("blah.blah.blah.blah.blah")),
1389 search_terms_data
, NULL
));
1392 void TemplateURL::CopyFrom(const TemplateURL
& other
) {
1396 data_
= other
.data_
;
1397 url_ref_
.InvalidateCachedValues();
1398 suggestions_url_ref_
.InvalidateCachedValues();
1399 instant_url_ref_
.InvalidateCachedValues();
1400 SetPrepopulateId(other
.data_
.prepopulate_id
);
1403 void TemplateURL::SetURL(const std::string
& url
) {
1405 url_ref_
.InvalidateCachedValues();
1408 void TemplateURL::SetPrepopulateId(int id
) {
1409 data_
.prepopulate_id
= id
;
1410 const bool prepopulated
= id
> 0;
1411 url_ref_
.prepopulated_
= prepopulated
;
1412 suggestions_url_ref_
.prepopulated_
= prepopulated
;
1413 instant_url_ref_
.prepopulated_
= prepopulated
;
1416 void TemplateURL::ResetKeywordIfNecessary(
1417 const SearchTermsData
& search_terms_data
,
1419 if (IsGoogleSearchURLWithReplaceableKeyword(search_terms_data
) || force
) {
1420 DCHECK(GetType() != OMNIBOX_API_EXTENSION
);
1421 GURL
url(GenerateSearchURL(search_terms_data
));
1423 data_
.SetKeyword(GenerateKeyword(url
));
1427 bool TemplateURL::FindSearchTermsInURL(
1429 const SearchTermsData
& search_terms_data
,
1430 base::string16
* search_terms
,
1431 url::Parsed::ComponentType
* search_term_component
,
1432 url::Component
* search_terms_position
) {
1433 DCHECK(search_terms
);
1434 search_terms
->clear();
1436 // Try to match with every pattern.
1437 for (size_t i
= 0; i
< URLCount(); ++i
) {
1438 TemplateURLRef
ref(this, i
);
1439 if (ref
.ExtractSearchTermsFromURL(url
, search_terms
, search_terms_data
,
1440 search_term_component
, search_terms_position
)) {
1441 // If ExtractSearchTermsFromURL() returns true and |search_terms| is empty
1442 // it means the pattern matched but no search terms were present. In this
1443 // case we fail immediately without looking for matches in subsequent
1444 // patterns. This means that given patterns
1445 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ],
1446 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would
1447 // return false. This is important for at least Google, where such URLs
1449 return !search_terms
->empty();