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