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 enable_omnibox_start_margin(false),
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
),
204 TemplateURLRef::SearchTermsArgs::ContextualSearchParams::
205 ContextualSearchParams(
209 const std::string
& selection
,
210 const std::string
& content
,
211 const std::string
& base_page_url
,
212 const std::string
& encoding
)
216 selection(selection
),
218 base_page_url(base_page_url
),
223 TemplateURLRef::SearchTermsArgs::ContextualSearchParams::
224 ContextualSearchParams(
228 const std::string
& selection
,
229 const std::string
& content
,
230 const std::string
& base_page_url
,
231 const std::string
& encoding
,
236 selection(selection
),
238 base_page_url(base_page_url
),
243 TemplateURLRef::SearchTermsArgs::ContextualSearchParams::
244 ~ContextualSearchParams() {
247 // TemplateURLRef -------------------------------------------------------------
249 TemplateURLRef::TemplateURLRef(TemplateURL
* owner
, Type type
)
255 supports_replacements_(false),
256 search_term_key_location_(url::Parsed::QUERY
),
257 prepopulated_(false) {
259 DCHECK_NE(INDEXED
, type_
);
262 TemplateURLRef::TemplateURLRef(TemplateURL
* owner
, size_t index_in_owner
)
265 index_in_owner_(index_in_owner
),
268 supports_replacements_(false),
269 search_term_key_location_(url::Parsed::QUERY
),
270 prepopulated_(false) {
272 DCHECK_LT(index_in_owner_
, owner_
->URLCount());
275 TemplateURLRef::~TemplateURLRef() {
278 std::string
TemplateURLRef::GetURL() const {
280 case SEARCH
: return owner_
->url();
281 case SUGGEST
: return owner_
->suggestions_url();
282 case INSTANT
: return owner_
->instant_url();
283 case IMAGE
: return owner_
->image_url();
284 case NEW_TAB
: return owner_
->new_tab_url();
285 case CONTEXTUAL_SEARCH
: return owner_
->contextual_search_url();
286 case INDEXED
: return owner_
->GetURL(index_in_owner_
);
287 default: NOTREACHED(); return std::string(); // NOLINT
291 std::string
TemplateURLRef::GetPostParamsString() const {
294 case SEARCH
: return owner_
->search_url_post_params();
295 case SUGGEST
: return owner_
->suggestions_url_post_params();
296 case INSTANT
: return owner_
->instant_url_post_params();
297 case NEW_TAB
: return std::string();
298 case CONTEXTUAL_SEARCH
: return std::string();
299 case IMAGE
: return owner_
->image_url_post_params();
300 default: NOTREACHED(); return std::string(); // NOLINT
304 bool TemplateURLRef::UsesPOSTMethod(
305 const SearchTermsData
& search_terms_data
) const {
306 ParseIfNecessary(search_terms_data
);
307 return !post_params_
.empty();
310 bool TemplateURLRef::EncodeFormData(const PostParams
& post_params
,
311 PostContent
* post_content
) const {
312 if (post_params
.empty())
317 const char kUploadDataMIMEType
[] = "multipart/form-data; boundary=";
318 const char kMultipartBoundary
[] = "----+*+----%016" PRIx64
"----+*+----";
319 // Each name/value pair is stored in a body part which is preceded by a
320 // boundary delimiter line. Uses random number generator here to create
321 // a unique boundary delimiter for form data encoding.
322 std::string boundary
= base::StringPrintf(kMultipartBoundary
,
324 // Sets the content MIME type.
325 post_content
->first
= kUploadDataMIMEType
;
326 post_content
->first
+= boundary
;
327 // Encodes the post parameters.
328 std::string
* post_data
= &post_content
->second
;
330 for (PostParams::const_iterator param
= post_params
.begin();
331 param
!= post_params
.end(); ++param
) {
332 DCHECK(!param
->first
.empty());
333 net::AddMultipartValueForUpload(param
->first
, param
->second
, boundary
,
334 std::string(), post_data
);
336 net::AddMultipartFinalDelimiterForUpload(boundary
, post_data
);
340 bool TemplateURLRef::SupportsReplacement(
341 const SearchTermsData
& search_terms_data
) const {
342 ParseIfNecessary(search_terms_data
);
343 return valid_
&& supports_replacements_
;
346 std::string
TemplateURLRef::ReplaceSearchTerms(
347 const SearchTermsArgs
& search_terms_args
,
348 const SearchTermsData
& search_terms_data
,
349 PostContent
* post_content
) const {
350 ParseIfNecessary(search_terms_data
);
352 return std::string();
354 std::string
url(HandleReplacements(search_terms_args
, search_terms_data
,
358 if (!gurl
.is_valid())
361 std::vector
<std::string
> query_params
;
362 if (search_terms_args
.append_extra_query_params
) {
363 std::string
extra_params(
364 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
365 switches::kExtraSearchQueryParams
));
366 if (!extra_params
.empty())
367 query_params
.push_back(extra_params
);
369 if (!search_terms_args
.suggest_query_params
.empty())
370 query_params
.push_back(search_terms_args
.suggest_query_params
);
371 if (!gurl
.query().empty())
372 query_params
.push_back(gurl
.query());
374 if (query_params
.empty())
377 GURL::Replacements replacements
;
378 std::string query_str
= JoinString(query_params
, "&");
379 replacements
.SetQueryStr(query_str
);
380 return gurl
.ReplaceComponents(replacements
).possibly_invalid_spec();
383 bool TemplateURLRef::IsValid(const SearchTermsData
& search_terms_data
) const {
384 ParseIfNecessary(search_terms_data
);
388 base::string16
TemplateURLRef::DisplayURL(
389 const SearchTermsData
& search_terms_data
) const {
390 ParseIfNecessary(search_terms_data
);
391 base::string16
result(base::UTF8ToUTF16(GetURL()));
392 if (valid_
&& !replacements_
.empty()) {
393 ReplaceSubstringsAfterOffset(&result
, 0,
394 base::ASCIIToUTF16(kSearchTermsParameterFull
),
395 base::ASCIIToUTF16(kDisplaySearchTerms
));
396 ReplaceSubstringsAfterOffset(&result
, 0,
397 base::ASCIIToUTF16(kGoogleUnescapedSearchTermsParameterFull
),
398 base::ASCIIToUTF16(kDisplayUnescapedSearchTerms
));
404 std::string
TemplateURLRef::DisplayURLToURLRef(
405 const base::string16
& display_url
) {
406 base::string16 result
= display_url
;
407 ReplaceSubstringsAfterOffset(&result
, 0,
408 base::ASCIIToUTF16(kDisplaySearchTerms
),
409 base::ASCIIToUTF16(kSearchTermsParameterFull
));
410 ReplaceSubstringsAfterOffset(
412 base::ASCIIToUTF16(kDisplayUnescapedSearchTerms
),
413 base::ASCIIToUTF16(kGoogleUnescapedSearchTermsParameterFull
));
414 return base::UTF16ToUTF8(result
);
417 const std::string
& TemplateURLRef::GetHost(
418 const SearchTermsData
& search_terms_data
) const {
419 ParseIfNecessary(search_terms_data
);
423 const std::string
& TemplateURLRef::GetPath(
424 const SearchTermsData
& search_terms_data
) const {
425 ParseIfNecessary(search_terms_data
);
429 const std::string
& TemplateURLRef::GetSearchTermKey(
430 const SearchTermsData
& search_terms_data
) const {
431 ParseIfNecessary(search_terms_data
);
432 return search_term_key_
;
435 base::string16
TemplateURLRef::SearchTermToString16(
436 const std::string
& term
) const {
437 const std::vector
<std::string
>& encodings
= owner_
->input_encodings();
438 base::string16 result
;
440 std::string unescaped
= net::UnescapeURLComponent(
442 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE
|
443 net::UnescapeRule::URL_SPECIAL_CHARS
);
444 for (size_t i
= 0; i
< encodings
.size(); ++i
) {
445 if (base::CodepageToUTF16(unescaped
, encodings
[i
].c_str(),
446 base::OnStringConversionError::FAIL
, &result
))
450 // Always fall back on UTF-8 if it works.
451 if (base::CodepageToUTF16(unescaped
, base::kCodepageUTF8
,
452 base::OnStringConversionError::FAIL
, &result
))
455 // When nothing worked, just use the escaped text. We have no idea what the
456 // encoding is. We need to substitute spaces for pluses ourselves since we're
457 // not sending it through an unescaper.
458 result
= base::UTF8ToUTF16(term
);
459 std::replace(result
.begin(), result
.end(), '+', ' ');
463 bool TemplateURLRef::HasGoogleBaseURLs(
464 const SearchTermsData
& search_terms_data
) const {
465 ParseIfNecessary(search_terms_data
);
466 for (size_t i
= 0; i
< replacements_
.size(); ++i
) {
467 if ((replacements_
[i
].type
== GOOGLE_BASE_URL
) ||
468 (replacements_
[i
].type
== GOOGLE_BASE_SUGGEST_URL
))
474 bool TemplateURLRef::ExtractSearchTermsFromURL(
476 base::string16
* search_terms
,
477 const SearchTermsData
& search_terms_data
,
478 url::Parsed::ComponentType
* search_terms_component
,
479 url::Component
* search_terms_position
) const {
480 DCHECK(search_terms
);
481 search_terms
->clear();
483 ParseIfNecessary(search_terms_data
);
485 // We need a search term in the template URL to extract something.
486 if (search_term_key_
.empty())
489 // TODO(beaudoin): Support patterns of the form http://foo/{searchTerms}/
490 // See crbug.com/153798
492 // Fill-in the replacements. We don't care about search terms in the pattern,
493 // so we use the empty string.
494 // Currently we assume the search term only shows in URL, not in post params.
495 GURL
pattern(ReplaceSearchTerms(SearchTermsArgs(base::string16()),
496 search_terms_data
, NULL
));
497 // Host, path and port must match.
498 if (url
.port() != pattern
.port() ||
499 url
.host() != host_
||
500 url
.path() != path_
) {
504 // Parameter must be present either in the query or the ref.
505 const std::string
& params(
506 (search_term_key_location_
== url::Parsed::QUERY
) ?
507 url
.query() : url
.ref());
509 url::Component query
, key
, value
;
510 query
.len
= static_cast<int>(params
.size());
511 bool key_found
= false;
512 while (url::ExtractQueryKeyValue(params
.c_str(), &query
, &key
, &value
)) {
513 if (key
.is_nonempty()) {
514 if (params
.substr(key
.begin
, key
.len
) == search_term_key_
) {
515 // Fail if search term key is found twice.
517 search_terms
->clear();
521 // Extract the search term.
522 *search_terms
= net::UnescapeAndDecodeUTF8URLComponent(
523 params
.substr(value
.begin
, value
.len
),
524 net::UnescapeRule::SPACES
|
525 net::UnescapeRule::URL_SPECIAL_CHARS
|
526 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE
);
527 if (search_terms_component
)
528 *search_terms_component
= search_term_key_location_
;
529 if (search_terms_position
)
530 *search_terms_position
= value
;
537 void TemplateURLRef::InvalidateCachedValues() const {
538 supports_replacements_
= valid_
= parsed_
= false;
541 search_term_key_
.clear();
542 replacements_
.clear();
543 post_params_
.clear();
546 bool TemplateURLRef::ParseParameter(size_t start
,
549 Replacements
* replacements
) const {
550 DCHECK(start
!= std::string::npos
&&
551 end
!= std::string::npos
&& end
> start
);
552 size_t length
= end
- start
- 1;
553 bool optional
= false;
554 if ((*url
)[end
- 1] == kOptional
) {
558 std::string
parameter(url
->substr(start
+ 1, length
));
559 std::string
full_parameter(url
->substr(start
, end
- start
+ 1));
560 // Remove the parameter from the string. For parameters who replacement is
561 // constant and already known, just replace them directly. For other cases,
562 // like parameters whose values may change over time, use |replacements|.
563 url
->erase(start
, end
- start
+ 1);
564 if (parameter
== kSearchTermsParameter
) {
565 replacements
->push_back(Replacement(SEARCH_TERMS
, start
));
566 } else if (parameter
== kCountParameter
) {
568 url
->insert(start
, kDefaultCount
);
569 } else if (parameter
== kGoogleAssistedQueryStatsParameter
) {
570 replacements
->push_back(Replacement(GOOGLE_ASSISTED_QUERY_STATS
, start
));
571 } else if (parameter
== kGoogleBaseURLParameter
) {
572 replacements
->push_back(Replacement(GOOGLE_BASE_URL
, start
));
573 } else if (parameter
== kGoogleBaseSuggestURLParameter
) {
574 replacements
->push_back(Replacement(GOOGLE_BASE_SUGGEST_URL
, start
));
575 } else if (parameter
== kGoogleBookmarkBarPinnedParameter
) {
576 replacements
->push_back(Replacement(GOOGLE_BOOKMARK_BAR_PINNED
, start
));
577 } else if (parameter
== kGoogleCurrentPageUrlParameter
) {
578 replacements
->push_back(Replacement(GOOGLE_CURRENT_PAGE_URL
, start
));
579 } else if (parameter
== kGoogleCursorPositionParameter
) {
580 replacements
->push_back(Replacement(GOOGLE_CURSOR_POSITION
, start
));
581 } else if (parameter
== kGoogleForceInstantResultsParameter
) {
582 replacements
->push_back(Replacement(GOOGLE_FORCE_INSTANT_RESULTS
, start
));
583 } else if (parameter
== kGoogleImageOriginalHeight
) {
584 replacements
->push_back(
585 Replacement(TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_HEIGHT
, start
));
586 } else if (parameter
== kGoogleImageOriginalWidth
) {
587 replacements
->push_back(
588 Replacement(TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH
, start
));
589 } else if (parameter
== kGoogleImageSearchSource
) {
590 replacements
->push_back(
591 Replacement(TemplateURLRef::GOOGLE_IMAGE_SEARCH_SOURCE
, start
));
592 } else if (parameter
== kGoogleImageThumbnailParameter
) {
593 replacements
->push_back(
594 Replacement(TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL
, start
));
595 } else if (parameter
== kGoogleImageURLParameter
) {
596 replacements
->push_back(Replacement(TemplateURLRef::GOOGLE_IMAGE_URL
,
598 } else if (parameter
== kGoogleInputTypeParameter
) {
599 replacements
->push_back(Replacement(TemplateURLRef::GOOGLE_INPUT_TYPE
,
601 } else if (parameter
== kGoogleInstantExtendedEnabledParameter
) {
602 replacements
->push_back(Replacement(GOOGLE_INSTANT_EXTENDED_ENABLED
,
604 } else if (parameter
== kGoogleInstantExtendedEnabledKey
) {
605 url
->insert(start
, google_util::kInstantExtendedAPIParam
);
606 } else if (parameter
== kGoogleNTPIsThemedParameter
) {
607 replacements
->push_back(Replacement(GOOGLE_NTP_IS_THEMED
, start
));
608 } else if (parameter
== kGoogleOmniboxStartMarginParameter
) {
609 replacements
->push_back(Replacement(GOOGLE_OMNIBOX_START_MARGIN
, start
));
610 } else if (parameter
== kGoogleContextualSearchVersion
) {
611 replacements
->push_back(
612 Replacement(GOOGLE_CONTEXTUAL_SEARCH_VERSION
, start
));
613 } else if (parameter
== kGoogleContextualSearchContextData
) {
614 replacements
->push_back(
615 Replacement(GOOGLE_CONTEXTUAL_SEARCH_CONTEXT_DATA
, start
));
616 } else if (parameter
== kGoogleOriginalQueryForSuggestionParameter
) {
617 replacements
->push_back(Replacement(GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION
,
619 } else if (parameter
== kGooglePageClassificationParameter
) {
620 replacements
->push_back(Replacement(GOOGLE_PAGE_CLASSIFICATION
, start
));
621 } else if (parameter
== kGooglePrefetchQuery
) {
622 replacements
->push_back(Replacement(GOOGLE_PREFETCH_QUERY
, start
));
623 } else if (parameter
== kGoogleRLZParameter
) {
624 replacements
->push_back(Replacement(GOOGLE_RLZ
, start
));
625 } else if (parameter
== kGoogleSearchClient
) {
626 replacements
->push_back(Replacement(GOOGLE_SEARCH_CLIENT
, start
));
627 } else if (parameter
== kGoogleSearchFieldtrialParameter
) {
628 replacements
->push_back(Replacement(GOOGLE_SEARCH_FIELDTRIAL_GROUP
, start
));
629 } else if (parameter
== kGoogleSearchVersion
) {
630 replacements
->push_back(Replacement(GOOGLE_SEARCH_VERSION
, start
));
631 } else if (parameter
== kGoogleSessionToken
) {
632 replacements
->push_back(Replacement(GOOGLE_SESSION_TOKEN
, start
));
633 } else if (parameter
== kGoogleSourceIdParameter
) {
634 #if defined(OS_ANDROID)
635 url
->insert(start
, "sourceid=chrome-mobile&");
637 url
->insert(start
, "sourceid=chrome&");
639 } else if (parameter
== kGoogleSuggestAPIKeyParameter
) {
641 net::EscapeQueryParamValue(google_apis::GetAPIKey(), false));
642 } else if (parameter
== kGoogleSuggestClient
) {
643 replacements
->push_back(Replacement(GOOGLE_SUGGEST_CLIENT
, start
));
644 } else if (parameter
== kGoogleSuggestRequestId
) {
645 replacements
->push_back(Replacement(GOOGLE_SUGGEST_REQUEST_ID
, start
));
646 } else if (parameter
== kGoogleUnescapedSearchTermsParameter
) {
647 replacements
->push_back(Replacement(GOOGLE_UNESCAPED_SEARCH_TERMS
, start
));
648 } else if (parameter
== kInputEncodingParameter
) {
649 replacements
->push_back(Replacement(ENCODING
, start
));
650 } else if (parameter
== kLanguageParameter
) {
651 replacements
->push_back(Replacement(LANGUAGE
, start
));
652 } else if (parameter
== kOutputEncodingParameter
) {
654 url
->insert(start
, kOutputEncodingType
);
655 } else if ((parameter
== kStartIndexParameter
) ||
656 (parameter
== kStartPageParameter
)) {
657 // We don't support these.
659 url
->insert(start
, "1");
660 } else if (!prepopulated_
) {
661 // If it's a prepopulated URL, we know that it's safe to remove unknown
662 // parameters, so just ignore this and return true below. Otherwise it could
663 // be some garbage but can also be a javascript block. Put it back.
664 url
->insert(start
, full_parameter
);
670 std::string
TemplateURLRef::ParseURL(const std::string
& url
,
671 Replacements
* replacements
,
672 PostParams
* post_params
,
675 std::string parsed_url
= url
;
676 for (size_t last
= 0; last
!= std::string::npos
; ) {
677 last
= parsed_url
.find(kStartParameter
, last
);
678 if (last
!= std::string::npos
) {
679 size_t template_end
= parsed_url
.find(kEndParameter
, last
);
680 if (template_end
!= std::string::npos
) {
681 // Since we allow Javascript in the URL, {} pairs could be nested. Match
682 // only leaf pairs with supported parameters.
683 size_t next_template_start
= parsed_url
.find(kStartParameter
, last
+ 1);
684 if (next_template_start
== std::string::npos
||
685 next_template_start
> template_end
) {
686 // If successful, ParseParameter erases from the string as such no
687 // need to update |last|. If failed, move |last| to the end of pair.
688 if (!ParseParameter(last
, template_end
, &parsed_url
, replacements
)) {
689 // |template_end| + 1 may be beyond the end of the string.
693 last
= next_template_start
;
696 // Open brace without a closing brace, return.
697 return std::string();
702 // Handles the post parameters.
703 const std::string
& post_params_string
= GetPostParamsString();
704 if (!post_params_string
.empty()) {
705 typedef std::vector
<std::string
> Strings
;
707 base::SplitString(post_params_string
, ',', ¶m_list
);
709 for (Strings::const_iterator iterator
= param_list
.begin();
710 iterator
!= param_list
.end(); ++iterator
) {
712 // The '=' delimiter is required and the name must be not empty.
713 base::SplitString(*iterator
, '=', &parts
);
714 if ((parts
.size() != 2U) || parts
[0].empty())
715 return std::string();
717 std::string
& value
= parts
[1];
718 size_t replacements_size
= replacements
->size();
719 if (IsTemplateParameterString(value
))
720 ParseParameter(0, value
.length() - 1, &value
, replacements
);
721 post_params
->push_back(std::make_pair(parts
[0], value
));
722 // If there was a replacement added, points its index to last added
724 if (replacements
->size() > replacements_size
) {
725 DCHECK_EQ(replacements_size
+ 1, replacements
->size());
726 Replacement
* r
= &replacements
->back();
727 r
->is_post_param
= true;
728 r
->index
= post_params
->size() - 1;
731 DCHECK(!post_params
->empty());
738 void TemplateURLRef::ParseIfNecessary(
739 const SearchTermsData
& search_terms_data
) const {
741 InvalidateCachedValues();
743 parsed_url_
= ParseURL(GetURL(), &replacements_
, &post_params_
, &valid_
);
744 supports_replacements_
= false;
746 bool has_only_one_search_term
= false;
747 for (Replacements::const_iterator i
= replacements_
.begin();
748 i
!= replacements_
.end(); ++i
) {
749 if ((i
->type
== SEARCH_TERMS
) ||
750 (i
->type
== GOOGLE_UNESCAPED_SEARCH_TERMS
)) {
751 if (has_only_one_search_term
) {
752 has_only_one_search_term
= false;
755 has_only_one_search_term
= true;
756 supports_replacements_
= true;
759 // Only parse the host/key if there is one search term. Technically there
760 // could be more than one term, but it's uncommon; so we punt.
761 if (has_only_one_search_term
)
762 ParseHostAndSearchTermKey(search_terms_data
);
767 void TemplateURLRef::ParseHostAndSearchTermKey(
768 const SearchTermsData
& search_terms_data
) const {
769 std::string
url_string(GetURL());
770 ReplaceSubstringsAfterOffset(&url_string
, 0,
771 kGoogleBaseURLParameterFull
,
772 search_terms_data
.GoogleBaseURLValue());
773 ReplaceSubstringsAfterOffset(&url_string
, 0,
774 kGoogleBaseSuggestURLParameterFull
,
775 search_terms_data
.GoogleBaseSuggestURLValue());
777 search_term_key_
.clear();
780 search_term_key_location_
= url::Parsed::REF
;
782 GURL
url(url_string
);
786 std::string query_key
= FindSearchTermsKey(url
.query());
787 std::string ref_key
= FindSearchTermsKey(url
.ref());
788 if (query_key
.empty() == ref_key
.empty())
789 return; // No key or multiple keys found. We only handle having one key.
790 search_term_key_
= query_key
.empty() ? ref_key
: query_key
;
791 search_term_key_location_
=
792 query_key
.empty() ? url::Parsed::REF
: url::Parsed::QUERY
;
797 void TemplateURLRef::HandleReplacement(const std::string
& name
,
798 const std::string
& value
,
799 const Replacement
& replacement
,
800 std::string
* url
) const {
801 size_t pos
= replacement
.index
;
802 if (replacement
.is_post_param
) {
803 DCHECK_LT(pos
, post_params_
.size());
804 DCHECK(!post_params_
[pos
].first
.empty());
805 post_params_
[pos
].second
= value
;
807 url
->insert(pos
, name
.empty() ? value
: (name
+ "=" + value
+ "&"));
811 std::string
TemplateURLRef::HandleReplacements(
812 const SearchTermsArgs
& search_terms_args
,
813 const SearchTermsData
& search_terms_data
,
814 PostContent
* post_content
) const {
815 if (replacements_
.empty()) {
816 if (!post_params_
.empty())
817 EncodeFormData(post_params_
, post_content
);
821 // Determine if the search terms are in the query or before. We're escaping
822 // space as '+' in the former case and as '%20' in the latter case.
823 bool is_in_query
= true;
824 for (Replacements::iterator i
= replacements_
.begin();
825 i
!= replacements_
.end(); ++i
) {
826 if (i
->type
== SEARCH_TERMS
) {
827 base::string16::size_type query_start
= parsed_url_
.find('?');
828 is_in_query
= query_start
!= base::string16::npos
&&
829 (static_cast<base::string16::size_type
>(i
->index
) > query_start
);
834 std::string input_encoding
;
835 base::string16 encoded_terms
;
836 base::string16 encoded_original_query
;
837 owner_
->EncodeSearchTerms(search_terms_args
, is_in_query
, &input_encoding
,
838 &encoded_terms
, &encoded_original_query
);
840 std::string url
= parsed_url_
;
842 // replacements_ is ordered in ascending order, as such we need to iterate
844 for (Replacements::reverse_iterator i
= replacements_
.rbegin();
845 i
!= replacements_
.rend(); ++i
) {
848 HandleReplacement(std::string(), input_encoding
, *i
, &url
);
851 case GOOGLE_ASSISTED_QUERY_STATS
:
852 DCHECK(!i
->is_post_param
);
853 if (!search_terms_args
.assisted_query_stats
.empty()) {
854 // Get the base URL without substituting AQS to avoid infinite
855 // recursion. We need the URL to find out if it meets all
856 // AQS requirements (e.g. HTTPS protocol check).
857 // See TemplateURLRef::SearchTermsArgs for more details.
858 SearchTermsArgs
search_terms_args_without_aqs(search_terms_args
);
859 search_terms_args_without_aqs
.assisted_query_stats
.clear();
860 GURL
base_url(ReplaceSearchTerms(
861 search_terms_args_without_aqs
, search_terms_data
, NULL
));
862 if (base_url
.SchemeIs(url::kHttpsScheme
)) {
864 "aqs", search_terms_args
.assisted_query_stats
, *i
, &url
);
869 case GOOGLE_BASE_URL
:
870 DCHECK(!i
->is_post_param
);
872 std::string(), search_terms_data
.GoogleBaseURLValue(), *i
, &url
);
875 case GOOGLE_BASE_SUGGEST_URL
:
876 DCHECK(!i
->is_post_param
);
878 std::string(), search_terms_data
.GoogleBaseSuggestURLValue(), *i
,
882 case GOOGLE_BOOKMARK_BAR_PINNED
:
883 if (search_terms_data
.IsShowingSearchTermsOnSearchResultsPages()) {
884 // Log whether the bookmark bar is pinned when the user is seeing
885 // InstantExtended on the SRP.
886 DCHECK(!i
->is_post_param
);
888 "bmbp", search_terms_args
.bookmark_bar_pinned
? "1" : "0", *i
,
893 case GOOGLE_CURRENT_PAGE_URL
:
894 DCHECK(!i
->is_post_param
);
895 if (!search_terms_args
.current_page_url
.empty()) {
896 const std::string
& escaped_current_page_url
=
897 net::EscapeQueryParamValue(search_terms_args
.current_page_url
,
899 HandleReplacement("url", escaped_current_page_url
, *i
, &url
);
903 case GOOGLE_CURSOR_POSITION
:
904 DCHECK(!i
->is_post_param
);
905 if (search_terms_args
.cursor_position
!= base::string16::npos
)
908 base::StringPrintf("%" PRIuS
, search_terms_args
.cursor_position
),
913 case GOOGLE_FORCE_INSTANT_RESULTS
:
914 DCHECK(!i
->is_post_param
);
915 HandleReplacement(std::string(),
916 search_terms_data
.ForceInstantResultsParam(
917 search_terms_args
.force_instant_results
),
922 case GOOGLE_INPUT_TYPE
:
923 DCHECK(!i
->is_post_param
);
925 "oit", base::IntToString(search_terms_args
.input_type
), *i
, &url
);
928 case GOOGLE_INSTANT_EXTENDED_ENABLED
:
929 DCHECK(!i
->is_post_param
);
930 HandleReplacement(std::string(),
931 search_terms_data
.InstantExtendedEnabledParam(
937 case GOOGLE_NTP_IS_THEMED
:
938 DCHECK(!i
->is_post_param
);
940 std::string(), search_terms_data
.NTPIsThemedParam(), *i
, &url
);
943 case GOOGLE_OMNIBOX_START_MARGIN
:
944 DCHECK(!i
->is_post_param
);
945 if (search_terms_args
.enable_omnibox_start_margin
) {
946 int omnibox_start_margin
= search_terms_data
.OmniboxStartMargin();
947 if (omnibox_start_margin
>= 0) {
948 HandleReplacement("es_sm", base::IntToString(omnibox_start_margin
),
954 case GOOGLE_CONTEXTUAL_SEARCH_VERSION
:
955 if (search_terms_args
.contextual_search_params
.version
>= 0) {
959 search_terms_args
.contextual_search_params
.version
),
965 case GOOGLE_CONTEXTUAL_SEARCH_CONTEXT_DATA
: {
966 DCHECK(!i
->is_post_param
);
967 std::string context_data
;
969 const SearchTermsArgs::ContextualSearchParams
& params
=
970 search_terms_args
.contextual_search_params
;
972 if (params
.start
!= std::string::npos
) {
973 context_data
.append("ctxs_start=" + base::IntToString(
974 params
.start
) + "&");
977 if (params
.end
!= std::string::npos
) {
978 context_data
.append("ctxs_end=" + base::IntToString(
982 if (!params
.selection
.empty())
983 context_data
.append("q=" + params
.selection
+ "&");
985 if (!params
.content
.empty())
986 context_data
.append("ctxs_content=" + params
.content
+ "&");
988 if (!params
.base_page_url
.empty())
989 context_data
.append("ctxsl_url=" + params
.base_page_url
+ "&");
991 if (!params
.encoding
.empty()) {
992 context_data
.append("ctxs_encoding=" + params
.encoding
+ "&");
996 params
.resolve
? "ctxsl_resolve=1" : "ctxsl_resolve=0");
998 HandleReplacement(std::string(), context_data
, *i
, &url
);
1002 case GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION
:
1003 DCHECK(!i
->is_post_param
);
1004 if (search_terms_args
.accepted_suggestion
>= 0 ||
1005 !search_terms_args
.assisted_query_stats
.empty()) {
1007 "oq", base::UTF16ToUTF8(encoded_original_query
), *i
, &url
);
1011 case GOOGLE_PAGE_CLASSIFICATION
:
1012 if (search_terms_args
.page_classification
!=
1013 metrics::OmniboxEventProto::INVALID_SPEC
) {
1015 "pgcl", base::IntToString(search_terms_args
.page_classification
),
1020 case GOOGLE_PREFETCH_QUERY
: {
1021 const std::string
& query
= search_terms_args
.prefetch_query
;
1022 const std::string
& type
= search_terms_args
.prefetch_query_type
;
1023 if (!query
.empty() && !type
.empty()) {
1025 std::string(), "pfq=" + query
+ "&qha=" + type
+ "&", *i
, &url
);
1031 DCHECK(!i
->is_post_param
);
1032 // On platforms that don't have RLZ, we still want this branch
1033 // to happen so that we replace the RLZ template with the
1034 // empty string. (If we don't handle this case, we hit a
1035 // NOTREACHED below.)
1036 base::string16 rlz_string
= search_terms_data
.GetRlzParameterValue(
1037 search_terms_args
.from_app_list
);
1038 if (!rlz_string
.empty()) {
1039 HandleReplacement("rlz", base::UTF16ToUTF8(rlz_string
), *i
, &url
);
1044 case GOOGLE_SEARCH_CLIENT
: {
1045 DCHECK(!i
->is_post_param
);
1046 std::string client
= search_terms_data
.GetSearchClient();
1047 if (!client
.empty())
1048 HandleReplacement("client", client
, *i
, &url
);
1052 case GOOGLE_SEARCH_FIELDTRIAL_GROUP
:
1053 // We are not currently running any fieldtrials that modulate the search
1054 // url. If we do, then we'd have some conditional insert such as:
1055 // url.insert(i->index, used_www ? "gcx=w&" : "gcx=c&");
1058 case GOOGLE_SEARCH_VERSION
:
1059 if (search_terms_data
.EnableAnswersInSuggest())
1060 HandleReplacement("gs_rn", "42", *i
, &url
);
1063 case GOOGLE_SESSION_TOKEN
: {
1064 std::string token
= search_terms_args
.session_token
;
1066 HandleReplacement("psi", token
, *i
, &url
);
1070 case GOOGLE_SUGGEST_CLIENT
:
1072 std::string(), search_terms_data
.GetSuggestClient(), *i
, &url
);
1075 case GOOGLE_SUGGEST_REQUEST_ID
:
1077 std::string(), search_terms_data
.GetSuggestRequestIdentifier(), *i
,
1081 case GOOGLE_UNESCAPED_SEARCH_TERMS
: {
1082 std::string unescaped_terms
;
1083 base::UTF16ToCodepage(search_terms_args
.search_terms
,
1084 input_encoding
.c_str(),
1085 base::OnStringConversionError::SKIP
,
1087 HandleReplacement(std::string(), unescaped_terms
, *i
, &url
);
1093 std::string(), search_terms_data
.GetApplicationLocale(), *i
, &url
);
1098 std::string(), base::UTF16ToUTF8(encoded_terms
), *i
, &url
);
1101 case GOOGLE_IMAGE_THUMBNAIL
:
1103 std::string(), search_terms_args
.image_thumbnail_content
, *i
, &url
);
1106 case GOOGLE_IMAGE_URL
:
1107 if (search_terms_args
.image_url
.is_valid()) {
1109 std::string(), search_terms_args
.image_url
.spec(), *i
, &url
);
1113 case GOOGLE_IMAGE_ORIGINAL_WIDTH
:
1114 if (!search_terms_args
.image_original_size
.IsEmpty()) {
1117 base::IntToString(search_terms_args
.image_original_size
.width()),
1122 case GOOGLE_IMAGE_ORIGINAL_HEIGHT
:
1123 if (!search_terms_args
.image_original_size
.IsEmpty()) {
1126 base::IntToString(search_terms_args
.image_original_size
.height()),
1131 case GOOGLE_IMAGE_SEARCH_SOURCE
:
1133 std::string(), search_terms_data
.GoogleImageSearchSource(), *i
,
1143 if (!post_params_
.empty())
1144 EncodeFormData(post_params_
, post_content
);
1150 // TemplateURL ----------------------------------------------------------------
1152 TemplateURL::AssociatedExtensionInfo::AssociatedExtensionInfo(
1154 const std::string
& extension_id
)
1156 extension_id(extension_id
),
1157 wants_to_be_default_engine(false) {
1158 DCHECK_NE(NORMAL
, type
);
1161 TemplateURL::AssociatedExtensionInfo::~AssociatedExtensionInfo() {
1164 TemplateURL::TemplateURL(const TemplateURLData
& data
)
1166 url_ref_(this, TemplateURLRef::SEARCH
),
1167 suggestions_url_ref_(this,
1168 TemplateURLRef::SUGGEST
),
1169 instant_url_ref_(this,
1170 TemplateURLRef::INSTANT
),
1171 image_url_ref_(this, TemplateURLRef::IMAGE
),
1172 new_tab_url_ref_(this, TemplateURLRef::NEW_TAB
),
1173 contextual_search_url_ref_(this, TemplateURLRef::CONTEXTUAL_SEARCH
) {
1174 SetPrepopulateId(data_
.prepopulate_id
);
1176 if (data_
.search_terms_replacement_key
==
1177 kGoogleInstantExtendedEnabledKeyFull
) {
1178 data_
.search_terms_replacement_key
= google_util::kInstantExtendedAPIParam
;
1182 TemplateURL::~TemplateURL() {
1186 base::string16
TemplateURL::GenerateKeyword(const GURL
& url
) {
1187 DCHECK(url
.is_valid());
1188 // Strip "www." off the front of the keyword; otherwise the keyword won't work
1189 // properly. See http://code.google.com/p/chromium/issues/detail?id=6984 .
1190 // Special case: if the host was exactly "www." (not sure this can happen but
1191 // perhaps with some weird intranet and custom DNS server?), ensure we at
1192 // least don't return the empty string.
1193 base::string16
keyword(net::StripWWWFromHost(url
));
1194 return keyword
.empty() ? base::ASCIIToUTF16("www") : keyword
;
1198 GURL
TemplateURL::GenerateFaviconURL(const GURL
& url
) {
1199 DCHECK(url
.is_valid());
1200 GURL::Replacements rep
;
1202 const char favicon_path
[] = "/favicon.ico";
1203 int favicon_path_len
= arraysize(favicon_path
) - 1;
1205 rep
.SetPath(favicon_path
, url::Component(0, favicon_path_len
));
1206 rep
.ClearUsername();
1207 rep
.ClearPassword();
1210 return url
.ReplaceComponents(rep
);
1214 bool TemplateURL::MatchesData(const TemplateURL
* t_url
,
1215 const TemplateURLData
* data
,
1216 const SearchTermsData
& search_terms_data
) {
1217 if (!t_url
|| !data
)
1218 return !t_url
&& !data
;
1220 return (t_url
->short_name() == data
->short_name
) &&
1221 t_url
->HasSameKeywordAs(*data
, search_terms_data
) &&
1222 (t_url
->url() == data
->url()) &&
1223 (t_url
->suggestions_url() == data
->suggestions_url
) &&
1224 (t_url
->instant_url() == data
->instant_url
) &&
1225 (t_url
->image_url() == data
->image_url
) &&
1226 (t_url
->new_tab_url() == data
->new_tab_url
) &&
1227 (t_url
->search_url_post_params() == data
->search_url_post_params
) &&
1228 (t_url
->suggestions_url_post_params() ==
1229 data
->suggestions_url_post_params
) &&
1230 (t_url
->instant_url_post_params() == data
->instant_url_post_params
) &&
1231 (t_url
->image_url_post_params() == data
->image_url_post_params
) &&
1232 (t_url
->favicon_url() == data
->favicon_url
) &&
1233 (t_url
->safe_for_autoreplace() == data
->safe_for_autoreplace
) &&
1234 (t_url
->show_in_default_list() == data
->show_in_default_list
) &&
1235 (t_url
->input_encodings() == data
->input_encodings
) &&
1236 (t_url
->alternate_urls() == data
->alternate_urls
) &&
1237 (t_url
->search_terms_replacement_key() ==
1238 data
->search_terms_replacement_key
);
1241 base::string16
TemplateURL::AdjustedShortNameForLocaleDirection() const {
1242 base::string16 bidi_safe_short_name
= data_
.short_name
;
1243 base::i18n::AdjustStringForLocaleDirection(&bidi_safe_short_name
);
1244 return bidi_safe_short_name
;
1247 bool TemplateURL::ShowInDefaultList(
1248 const SearchTermsData
& search_terms_data
) const {
1249 return data_
.show_in_default_list
&&
1250 url_ref_
.SupportsReplacement(search_terms_data
);
1253 bool TemplateURL::SupportsReplacement(
1254 const SearchTermsData
& search_terms_data
) const {
1255 return url_ref_
.SupportsReplacement(search_terms_data
);
1258 bool TemplateURL::HasGoogleBaseURLs(
1259 const SearchTermsData
& search_terms_data
) const {
1260 return url_ref_
.HasGoogleBaseURLs(search_terms_data
) ||
1261 suggestions_url_ref_
.HasGoogleBaseURLs(search_terms_data
) ||
1262 instant_url_ref_
.HasGoogleBaseURLs(search_terms_data
) ||
1263 image_url_ref_
.HasGoogleBaseURLs(search_terms_data
) ||
1264 new_tab_url_ref_
.HasGoogleBaseURLs(search_terms_data
);
1267 bool TemplateURL::IsGoogleSearchURLWithReplaceableKeyword(
1268 const SearchTermsData
& search_terms_data
) const {
1269 return (GetType() == NORMAL
) &&
1270 url_ref_
.HasGoogleBaseURLs(search_terms_data
) &&
1271 google_util::IsGoogleHostname(base::UTF16ToUTF8(data_
.keyword()),
1272 google_util::DISALLOW_SUBDOMAIN
);
1275 bool TemplateURL::HasSameKeywordAs(
1276 const TemplateURLData
& other
,
1277 const SearchTermsData
& search_terms_data
) const {
1278 return (data_
.keyword() == other
.keyword()) ||
1279 (IsGoogleSearchURLWithReplaceableKeyword(search_terms_data
) &&
1280 TemplateURL(other
).IsGoogleSearchURLWithReplaceableKeyword(
1281 search_terms_data
));
1284 TemplateURL::Type
TemplateURL::GetType() const {
1285 return extension_info_
? extension_info_
->type
: NORMAL
;
1288 std::string
TemplateURL::GetExtensionId() const {
1289 DCHECK(extension_info_
);
1290 return extension_info_
->extension_id
;
1293 size_t TemplateURL::URLCount() const {
1294 // Add 1 for the regular search URL.
1295 return data_
.alternate_urls
.size() + 1;
1298 const std::string
& TemplateURL::GetURL(size_t index
) const {
1299 DCHECK_LT(index
, URLCount());
1301 return (index
< data_
.alternate_urls
.size()) ?
1302 data_
.alternate_urls
[index
] : url();
1305 bool TemplateURL::ExtractSearchTermsFromURL(
1307 const SearchTermsData
& search_terms_data
,
1308 base::string16
* search_terms
) {
1309 return FindSearchTermsInURL(url
, search_terms_data
, search_terms
, NULL
, NULL
);
1312 bool TemplateURL::IsSearchURL(
1314 const SearchTermsData
& search_terms_data
) {
1315 base::string16 search_terms
;
1316 return ExtractSearchTermsFromURL(url
, search_terms_data
, &search_terms
) &&
1317 !search_terms
.empty();
1320 bool TemplateURL::HasSearchTermsReplacementKey(const GURL
& url
) const {
1321 // Look for the key both in the query and the ref.
1322 std::string params
[] = {url
.query(), url
.ref()};
1324 for (int i
= 0; i
< 2; ++i
) {
1325 url::Component query
, key
, value
;
1326 query
.len
= static_cast<int>(params
[i
].size());
1327 while (url::ExtractQueryKeyValue(params
[i
].c_str(), &query
, &key
, &value
)) {
1328 if (key
.is_nonempty() &&
1329 params
[i
].substr(key
.begin
, key
.len
) ==
1330 search_terms_replacement_key()) {
1338 bool TemplateURL::ReplaceSearchTermsInURL(
1340 const TemplateURLRef::SearchTermsArgs
& search_terms_args
,
1341 const SearchTermsData
& search_terms_data
,
1343 // TODO(beaudoin): Use AQS from |search_terms_args| too.
1344 url::Parsed::ComponentType search_term_component
;
1345 url::Component search_terms_position
;
1346 base::string16 search_terms
;
1347 if (!FindSearchTermsInURL(url
, search_terms_data
, &search_terms
,
1348 &search_term_component
, &search_terms_position
)) {
1351 DCHECK(search_terms_position
.is_nonempty());
1353 // FindSearchTermsInURL only returns true for search terms in the query or
1354 // ref, so we can call EncodeSearchTerm with |is_in_query| = true, since query
1355 // and ref are encoded in the same way.
1356 std::string input_encoding
;
1357 base::string16 encoded_terms
;
1358 base::string16 encoded_original_query
;
1359 EncodeSearchTerms(search_terms_args
, true, &input_encoding
,
1360 &encoded_terms
, &encoded_original_query
);
1362 std::string
old_params(
1363 (search_term_component
== url::Parsed::REF
) ? url
.ref() : url
.query());
1364 std::string
new_params(old_params
, 0, search_terms_position
.begin
);
1365 new_params
+= base::UTF16ToUTF8(search_terms_args
.search_terms
);
1366 new_params
+= old_params
.substr(search_terms_position
.end());
1367 url::StdStringReplacements
<std::string
> replacements
;
1368 if (search_term_component
== url::Parsed::REF
)
1369 replacements
.SetRefStr(new_params
);
1371 replacements
.SetQueryStr(new_params
);
1372 *result
= url
.ReplaceComponents(replacements
);
1376 void TemplateURL::EncodeSearchTerms(
1377 const TemplateURLRef::SearchTermsArgs
& search_terms_args
,
1379 std::string
* input_encoding
,
1380 base::string16
* encoded_terms
,
1381 base::string16
* encoded_original_query
) const {
1383 std::vector
<std::string
> encodings(input_encodings());
1384 if (std::find(encodings
.begin(), encodings
.end(), "UTF-8") == encodings
.end())
1385 encodings
.push_back("UTF-8");
1386 for (std::vector
<std::string
>::const_iterator
i(encodings
.begin());
1387 i
!= encodings
.end(); ++i
) {
1388 if (TryEncoding(search_terms_args
.search_terms
,
1389 search_terms_args
.original_query
, i
->c_str(),
1390 is_in_query
, encoded_terms
, encoded_original_query
)) {
1391 *input_encoding
= *i
;
1398 GURL
TemplateURL::GenerateSearchURL(
1399 const SearchTermsData
& search_terms_data
) const {
1400 if (!url_ref_
.IsValid(search_terms_data
))
1403 if (!url_ref_
.SupportsReplacement(search_terms_data
))
1406 // Use something obscure for the search terms argument so that in the rare
1407 // case the term replaces the URL it's unlikely another keyword would have the
1409 // TODO(jnd): Add additional parameters to get post data when the search URL
1410 // has post parameters.
1411 return GURL(url_ref_
.ReplaceSearchTerms(
1412 TemplateURLRef::SearchTermsArgs(
1413 base::ASCIIToUTF16("blah.blah.blah.blah.blah")),
1414 search_terms_data
, NULL
));
1417 void TemplateURL::CopyFrom(const TemplateURL
& other
) {
1421 data_
= other
.data_
;
1422 url_ref_
.InvalidateCachedValues();
1423 suggestions_url_ref_
.InvalidateCachedValues();
1424 instant_url_ref_
.InvalidateCachedValues();
1425 SetPrepopulateId(other
.data_
.prepopulate_id
);
1428 void TemplateURL::SetURL(const std::string
& url
) {
1430 url_ref_
.InvalidateCachedValues();
1433 void TemplateURL::SetPrepopulateId(int id
) {
1434 data_
.prepopulate_id
= id
;
1435 const bool prepopulated
= id
> 0;
1436 url_ref_
.prepopulated_
= prepopulated
;
1437 suggestions_url_ref_
.prepopulated_
= prepopulated
;
1438 instant_url_ref_
.prepopulated_
= prepopulated
;
1441 void TemplateURL::ResetKeywordIfNecessary(
1442 const SearchTermsData
& search_terms_data
,
1444 if (IsGoogleSearchURLWithReplaceableKeyword(search_terms_data
) || force
) {
1445 DCHECK(GetType() != OMNIBOX_API_EXTENSION
);
1446 GURL
url(GenerateSearchURL(search_terms_data
));
1448 data_
.SetKeyword(GenerateKeyword(url
));
1452 bool TemplateURL::FindSearchTermsInURL(
1454 const SearchTermsData
& search_terms_data
,
1455 base::string16
* search_terms
,
1456 url::Parsed::ComponentType
* search_term_component
,
1457 url::Component
* search_terms_position
) {
1458 DCHECK(search_terms
);
1459 search_terms
->clear();
1461 // Try to match with every pattern.
1462 for (size_t i
= 0; i
< URLCount(); ++i
) {
1463 TemplateURLRef
ref(this, i
);
1464 if (ref
.ExtractSearchTermsFromURL(url
, search_terms
, search_terms_data
,
1465 search_term_component
, search_terms_position
)) {
1466 // If ExtractSearchTermsFromURL() returns true and |search_terms| is empty
1467 // it means the pattern matched but no search terms were present. In this
1468 // case we fail immediately without looking for matches in subsequent
1469 // patterns. This means that given patterns
1470 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ],
1471 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would
1472 // return false. This is important for at least Google, where such URLs
1474 return !search_terms
->empty();