1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/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/guid.h"
14 #include "base/i18n/case_conversion.h"
15 #include "base/i18n/icu_string_conversions.h"
16 #include "base/i18n/rtl.h"
17 #include "base/logging.h"
18 #include "base/metrics/field_trial.h"
19 #include "base/rand_util.h"
20 #include "base/strings/string_number_conversions.h"
21 #include "base/strings/string_split.h"
22 #include "base/strings/string_util.h"
23 #include "base/strings/stringprintf.h"
24 #include "base/strings/utf_string_conversions.h"
25 #include "chrome/browser/google/google_util.h"
26 #include "chrome/browser/search/search.h"
27 #include "chrome/browser/search_engines/search_terms_data.h"
28 #include "chrome/browser/search_engines/template_url_service.h"
29 #include "chrome/common/chrome_switches.h"
30 #include "chrome/common/chrome_version_info.h"
31 #include "chrome/common/url_constants.h"
32 #include "extensions/common/constants.h"
33 #include "google_apis/google_api_keys.h"
34 #include "net/base/escape.h"
35 #include "net/base/mime_util.h"
36 #include "ui/base/l10n/l10n_util.h"
40 // The TemplateURLRef has any number of terms that need to be replaced. Each of
41 // the terms is enclosed in braces. If the character preceeding the final
42 // brace is a ?, it indicates the term is optional and can be replaced with
44 const char kStartParameter
= '{';
45 const char kEndParameter
= '}';
46 const char kOptional
= '?';
48 // Known parameters found in the URL.
49 const char kSearchTermsParameter
[] = "searchTerms";
50 const char kSearchTermsParameterFull
[] = "{searchTerms}";
51 const char kCountParameter
[] = "count";
52 const char kStartIndexParameter
[] = "startIndex";
53 const char kStartPageParameter
[] = "startPage";
54 const char kLanguageParameter
[] = "language";
55 const char kInputEncodingParameter
[] = "inputEncoding";
56 const char kOutputEncodingParameter
[] = "outputEncoding";
58 const char kGoogleAssistedQueryStatsParameter
[] = "google:assistedQueryStats";
60 // Host/Domain Google searches are relative to.
61 const char kGoogleBaseURLParameter
[] = "google:baseURL";
62 const char kGoogleBaseURLParameterFull
[] = "{google:baseURL}";
64 // Like google:baseURL, but for the Search Suggest capability.
65 const char kGoogleBaseSuggestURLParameter
[] = "google:baseSuggestURL";
66 const char kGoogleBaseSuggestURLParameterFull
[] = "{google:baseSuggestURL}";
67 const char kGoogleBookmarkBarPinnedParameter
[] = "google:bookmarkBarPinned";
68 const char kGoogleCurrentPageUrlParameter
[] = "google:currentPageUrl";
69 const char kGoogleCursorPositionParameter
[] = "google:cursorPosition";
70 const char kGoogleForceInstantResultsParameter
[] = "google:forceInstantResults";
71 const char kGoogleInstantExtendedEnabledParameter
[] =
72 "google:instantExtendedEnabledParameter";
73 const char kGoogleInstantExtendedEnabledKey
[] =
74 "google:instantExtendedEnabledKey";
75 const char kGoogleInstantExtendedEnabledKeyFull
[] =
76 "{google:instantExtendedEnabledKey}";
77 const char kGoogleNTPIsThemedParameter
[] = "google:ntpIsThemedParameter";
78 const char kGoogleOmniboxStartMarginParameter
[] =
79 "google:omniboxStartMarginParameter";
80 const char kGoogleOriginalQueryForSuggestionParameter
[] =
81 "google:originalQueryForSuggestion";
82 const char kGooglePageClassificationParameter
[] = "google:pageClassification";
83 const char kGoogleRLZParameter
[] = "google:RLZ";
84 const char kGoogleSearchClient
[] = "google:searchClient";
85 const char kGoogleSearchFieldtrialParameter
[] =
86 "google:searchFieldtrialParameter";
87 const char kGoogleSearchVersion
[] = "google:searchVersion";
88 const char kGoogleSessionToken
[] = "google:sessionToken";
89 const char kGoogleSourceIdParameter
[] = "google:sourceId";
90 const char kGoogleSuggestAPIKeyParameter
[] = "google:suggestAPIKeyParameter";
91 const char kGoogleSuggestClient
[] = "google:suggestClient";
92 const char kGoogleSuggestRequestId
[] = "google:suggestRid";
94 // Same as kSearchTermsParameter, with no escaping.
95 const char kGoogleUnescapedSearchTermsParameter
[] =
96 "google:unescapedSearchTerms";
97 const char kGoogleUnescapedSearchTermsParameterFull
[] =
98 "{google:unescapedSearchTerms}";
100 const char kGoogleImageSearchSource
[] = "google:imageSearchSource";
101 const char kGoogleImageThumbnailParameter
[] = "google:imageThumbnail";
102 const char kGoogleImageURLParameter
[] = "google:imageURL";
103 const char kGoogleImageOriginalWidth
[] = "google:imageOriginalWidth";
104 const char kGoogleImageOriginalHeight
[] = "google:imageOriginalHeight";
106 // Display value for kSearchTermsParameter.
107 const char kDisplaySearchTerms
[] = "%s";
109 // Display value for kGoogleUnescapedSearchTermsParameter.
110 const char kDisplayUnescapedSearchTerms
[] = "%S";
112 // Used if the count parameter is not optional. Indicates we want 10 search
114 const char kDefaultCount
[] = "10";
116 // Used if the parameter kOutputEncodingParameter is required.
117 const char kOutputEncodingType
[] = "UTF-8";
119 // Attempts to encode |terms| and |original_query| in |encoding| and escape
120 // them. |terms| may be escaped as path or query depending on |is_in_query|;
121 // |original_query| is always escaped as query. Returns whether the encoding
122 // process succeeded.
123 bool TryEncoding(const base::string16
& terms
,
124 const base::string16
& original_query
,
125 const char* encoding
,
127 base::string16
* escaped_terms
,
128 base::string16
* escaped_original_query
) {
129 DCHECK(escaped_terms
);
130 DCHECK(escaped_original_query
);
131 std::string encoded_terms
;
132 if (!base::UTF16ToCodepage(terms
, encoding
,
133 base::OnStringConversionError::SKIP
, &encoded_terms
))
135 *escaped_terms
= base::UTF8ToUTF16(is_in_query
?
136 net::EscapeQueryParamValue(encoded_terms
, true) :
137 net::EscapePath(encoded_terms
));
138 if (original_query
.empty())
140 std::string encoded_original_query
;
141 if (!base::UTF16ToCodepage(original_query
, encoding
,
142 base::OnStringConversionError::SKIP
, &encoded_original_query
))
144 *escaped_original_query
= base::UTF8ToUTF16(
145 net::EscapeQueryParamValue(encoded_original_query
, true));
149 // Extract query key and host given a list of parameters coming from the URL
151 std::string
FindSearchTermsKey(const std::string
& params
) {
153 return std::string();
154 url::Component query
, key
, value
;
155 query
.len
= static_cast<int>(params
.size());
156 while (url::ExtractQueryKeyValue(params
.c_str(), &query
, &key
, &value
)) {
157 if (key
.is_nonempty() && value
.is_nonempty()) {
158 std::string value_string
= params
.substr(value
.begin
, value
.len
);
159 if (value_string
.find(kSearchTermsParameterFull
, 0) !=
161 value_string
.find(kGoogleUnescapedSearchTermsParameterFull
, 0) !=
163 return params
.substr(key
.begin
, key
.len
);
167 return std::string();
170 // Returns the string to use for replacements of type
171 // GOOGLE_IMAGE_SEARCH_SOURCE.
172 std::string
GetGoogleImageSearchSource() {
173 chrome::VersionInfo version_info
;
174 if (version_info
.is_valid()) {
175 std::string
version(version_info
.Name() + " " + version_info
.Version());
176 if (version_info
.IsOfficialBuild())
177 version
+= " (Official)";
178 version
+= " " + version_info
.OSType();
179 std::string
modifier(version_info
.GetVersionStringModifier());
180 if (!modifier
.empty())
181 version
+= " " + modifier
;
187 bool IsTemplateParameterString(const std::string
& param
) {
188 return (param
.length() > 2) && (*(param
.begin()) == kStartParameter
) &&
189 (*(param
.rbegin()) == kEndParameter
);
192 bool ShowingSearchTermsOnSRP() {
193 return chrome::IsInstantExtendedAPIEnabled() &&
194 chrome::IsQueryExtractionEnabled();
200 // TemplateURLRef::SearchTermsArgs --------------------------------------------
202 TemplateURLRef::SearchTermsArgs::SearchTermsArgs(
203 const base::string16
& search_terms
)
204 : search_terms(search_terms
),
205 accepted_suggestion(NO_SUGGESTIONS_AVAILABLE
),
206 cursor_position(base::string16::npos
),
207 omnibox_start_margin(-1),
208 page_classification(AutocompleteInput::INVALID_SPEC
),
209 bookmark_bar_pinned(false),
210 append_extra_query_params(false),
211 force_instant_results(false),
212 from_app_list(false) {
215 TemplateURLRef::SearchTermsArgs::~SearchTermsArgs() {
219 // TemplateURLRef -------------------------------------------------------------
221 TemplateURLRef::TemplateURLRef(TemplateURL
* owner
, Type type
)
227 supports_replacements_(false),
228 search_term_key_location_(url::Parsed::QUERY
),
229 prepopulated_(false),
230 showing_search_terms_(ShowingSearchTermsOnSRP()) {
232 DCHECK_NE(INDEXED
, type_
);
235 TemplateURLRef::TemplateURLRef(TemplateURL
* owner
, size_t index_in_owner
)
238 index_in_owner_(index_in_owner
),
241 supports_replacements_(false),
242 search_term_key_location_(url::Parsed::QUERY
),
243 prepopulated_(false),
244 showing_search_terms_(ShowingSearchTermsOnSRP()) {
246 DCHECK_LT(index_in_owner_
, owner_
->URLCount());
249 TemplateURLRef::~TemplateURLRef() {
252 std::string
TemplateURLRef::GetURL() const {
254 case SEARCH
: return owner_
->url();
255 case SUGGEST
: return owner_
->suggestions_url();
256 case INSTANT
: return owner_
->instant_url();
257 case IMAGE
: return owner_
->image_url();
258 case NEW_TAB
: return owner_
->new_tab_url();
259 case INDEXED
: return owner_
->GetURL(index_in_owner_
);
260 default: NOTREACHED(); return std::string(); // NOLINT
264 std::string
TemplateURLRef::GetPostParamsString() const {
267 case SEARCH
: return owner_
->search_url_post_params();
268 case SUGGEST
: return owner_
->suggestions_url_post_params();
269 case INSTANT
: return owner_
->instant_url_post_params();
270 case NEW_TAB
: return std::string();
271 case IMAGE
: return owner_
->image_url_post_params();
272 default: NOTREACHED(); return std::string(); // NOLINT
276 bool TemplateURLRef::UsesPOSTMethodUsingTermsData(
277 const SearchTermsData
* search_terms_data
) const {
278 if (search_terms_data
)
279 ParseIfNecessaryUsingTermsData(*search_terms_data
);
282 return !post_params_
.empty();
285 bool TemplateURLRef::EncodeFormData(const PostParams
& post_params
,
286 PostContent
* post_content
) const {
287 if (post_params
.empty())
292 const char kUploadDataMIMEType
[] = "multipart/form-data; boundary=";
293 const char kMultipartBoundary
[] = "----+*+----%016" PRIx64
"----+*+----";
294 // Each name/value pair is stored in a body part which is preceded by a
295 // boundary delimiter line. Uses random number generator here to create
296 // a unique boundary delimiter for form data encoding.
297 std::string boundary
= base::StringPrintf(kMultipartBoundary
,
299 // Sets the content MIME type.
300 post_content
->first
= kUploadDataMIMEType
;
301 post_content
->first
+= boundary
;
302 // Encodes the post parameters.
303 std::string
* post_data
= &post_content
->second
;
305 for (PostParams::const_iterator param
= post_params
.begin();
306 param
!= post_params
.end(); ++param
) {
307 DCHECK(!param
->first
.empty());
308 net::AddMultipartValueForUpload(param
->first
, param
->second
, boundary
,
309 std::string(), post_data
);
311 net::AddMultipartFinalDelimiterForUpload(boundary
, post_data
);
315 bool TemplateURLRef::SupportsReplacement() const {
316 UIThreadSearchTermsData
search_terms_data(owner_
->profile());
317 return SupportsReplacementUsingTermsData(search_terms_data
);
320 bool TemplateURLRef::SupportsReplacementUsingTermsData(
321 const SearchTermsData
& search_terms_data
) const {
322 ParseIfNecessaryUsingTermsData(search_terms_data
);
323 return valid_
&& supports_replacements_
;
326 std::string
TemplateURLRef::ReplaceSearchTerms(
327 const SearchTermsArgs
& search_terms_args
,
328 PostContent
* post_content
) const {
329 UIThreadSearchTermsData
search_terms_data(owner_
->profile());
330 return ReplaceSearchTermsUsingTermsData(search_terms_args
, search_terms_data
,
334 std::string
TemplateURLRef::ReplaceSearchTermsUsingTermsData(
335 const SearchTermsArgs
& search_terms_args
,
336 const SearchTermsData
& search_terms_data
,
337 PostContent
* post_content
) const {
338 ParseIfNecessaryUsingTermsData(search_terms_data
);
340 return std::string();
342 std::string
url(HandleReplacements(search_terms_args
, search_terms_data
,
346 if (!gurl
.is_valid())
349 std::vector
<std::string
> query_params
;
350 if (search_terms_args
.append_extra_query_params
) {
351 std::string
extra_params(
352 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
353 switches::kExtraSearchQueryParams
));
354 if (!extra_params
.empty())
355 query_params
.push_back(extra_params
);
357 if (!search_terms_args
.suggest_query_params
.empty())
358 query_params
.push_back(search_terms_args
.suggest_query_params
);
359 if (!gurl
.query().empty())
360 query_params
.push_back(gurl
.query());
362 if (query_params
.empty())
365 GURL::Replacements replacements
;
366 std::string query_str
= JoinString(query_params
, "&");
367 replacements
.SetQueryStr(query_str
);
368 return gurl
.ReplaceComponents(replacements
).possibly_invalid_spec();
371 bool TemplateURLRef::IsValid() const {
372 UIThreadSearchTermsData
search_terms_data(owner_
->profile());
373 return IsValidUsingTermsData(search_terms_data
);
376 bool TemplateURLRef::IsValidUsingTermsData(
377 const SearchTermsData
& search_terms_data
) const {
378 ParseIfNecessaryUsingTermsData(search_terms_data
);
382 base::string16
TemplateURLRef::DisplayURL() const {
384 base::string16
result(base::UTF8ToUTF16(GetURL()));
385 if (valid_
&& !replacements_
.empty()) {
386 ReplaceSubstringsAfterOffset(&result
, 0,
387 base::ASCIIToUTF16(kSearchTermsParameterFull
),
388 base::ASCIIToUTF16(kDisplaySearchTerms
));
389 ReplaceSubstringsAfterOffset(&result
, 0,
390 base::ASCIIToUTF16(kGoogleUnescapedSearchTermsParameterFull
),
391 base::ASCIIToUTF16(kDisplayUnescapedSearchTerms
));
397 std::string
TemplateURLRef::DisplayURLToURLRef(
398 const base::string16
& display_url
) {
399 base::string16 result
= display_url
;
400 ReplaceSubstringsAfterOffset(&result
, 0,
401 base::ASCIIToUTF16(kDisplaySearchTerms
),
402 base::ASCIIToUTF16(kSearchTermsParameterFull
));
403 ReplaceSubstringsAfterOffset(
405 base::ASCIIToUTF16(kDisplayUnescapedSearchTerms
),
406 base::ASCIIToUTF16(kGoogleUnescapedSearchTermsParameterFull
));
407 return base::UTF16ToUTF8(result
);
410 const std::string
& TemplateURLRef::GetHost() const {
415 const std::string
& TemplateURLRef::GetPath() const {
420 const std::string
& TemplateURLRef::GetSearchTermKey() const {
422 return search_term_key_
;
425 base::string16
TemplateURLRef::SearchTermToString16(
426 const std::string
& term
) const {
427 const std::vector
<std::string
>& encodings
= owner_
->input_encodings();
428 base::string16 result
;
430 std::string unescaped
= net::UnescapeURLComponent(
432 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE
|
433 net::UnescapeRule::URL_SPECIAL_CHARS
);
434 for (size_t i
= 0; i
< encodings
.size(); ++i
) {
435 if (base::CodepageToUTF16(unescaped
, encodings
[i
].c_str(),
436 base::OnStringConversionError::FAIL
, &result
))
440 // Always fall back on UTF-8 if it works.
441 if (base::CodepageToUTF16(unescaped
, base::kCodepageUTF8
,
442 base::OnStringConversionError::FAIL
, &result
))
445 // When nothing worked, just use the escaped text. We have no idea what the
446 // encoding is. We need to substitute spaces for pluses ourselves since we're
447 // not sending it through an unescaper.
448 result
= base::UTF8ToUTF16(term
);
449 std::replace(result
.begin(), result
.end(), '+', ' ');
453 bool TemplateURLRef::HasGoogleBaseURLs() const {
455 for (size_t i
= 0; i
< replacements_
.size(); ++i
) {
456 if ((replacements_
[i
].type
== GOOGLE_BASE_URL
) ||
457 (replacements_
[i
].type
== GOOGLE_BASE_SUGGEST_URL
))
463 bool TemplateURLRef::ExtractSearchTermsFromURL(
465 base::string16
* search_terms
,
466 const SearchTermsData
& search_terms_data
,
467 url::Parsed::ComponentType
* search_terms_component
,
468 url::Component
* search_terms_position
) const {
469 DCHECK(search_terms
);
470 search_terms
->clear();
472 ParseIfNecessaryUsingTermsData(search_terms_data
);
474 // We need a search term in the template URL to extract something.
475 if (search_term_key_
.empty())
478 // TODO(beaudoin): Support patterns of the form http://foo/{searchTerms}/
479 // See crbug.com/153798
481 // Fill-in the replacements. We don't care about search terms in the pattern,
482 // so we use the empty string.
483 // Currently we assume the search term only shows in URL, not in post params.
484 GURL
pattern(ReplaceSearchTermsUsingTermsData(
485 SearchTermsArgs(base::string16()), search_terms_data
, NULL
));
486 // Host, path and port must match.
487 if (url
.port() != pattern
.port() ||
488 url
.host() != host_
||
489 url
.path() != path_
) {
493 // Parameter must be present either in the query or the ref.
494 const std::string
& params(
495 (search_term_key_location_
== url::Parsed::QUERY
) ?
496 url
.query() : url
.ref());
498 url::Component query
, key
, value
;
499 query
.len
= static_cast<int>(params
.size());
500 bool key_found
= false;
501 while (url::ExtractQueryKeyValue(params
.c_str(), &query
, &key
, &value
)) {
502 if (key
.is_nonempty()) {
503 if (params
.substr(key
.begin
, key
.len
) == search_term_key_
) {
504 // Fail if search term key is found twice.
506 search_terms
->clear();
510 // Extract the search term.
511 *search_terms
= net::UnescapeAndDecodeUTF8URLComponent(
512 params
.substr(value
.begin
, value
.len
),
513 net::UnescapeRule::SPACES
|
514 net::UnescapeRule::URL_SPECIAL_CHARS
|
515 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE
);
516 if (search_terms_component
)
517 *search_terms_component
= search_term_key_location_
;
518 if (search_terms_position
)
519 *search_terms_position
= value
;
526 void TemplateURLRef::InvalidateCachedValues() const {
527 supports_replacements_
= valid_
= parsed_
= false;
530 search_term_key_
.clear();
531 replacements_
.clear();
532 post_params_
.clear();
535 bool TemplateURLRef::ParseParameter(size_t start
,
538 Replacements
* replacements
) const {
539 DCHECK(start
!= std::string::npos
&&
540 end
!= std::string::npos
&& end
> start
);
541 size_t length
= end
- start
- 1;
542 bool optional
= false;
543 if ((*url
)[end
- 1] == kOptional
) {
547 std::string
parameter(url
->substr(start
+ 1, length
));
548 std::string
full_parameter(url
->substr(start
, end
- start
+ 1));
549 // Remove the parameter from the string. For parameters who replacement is
550 // constant and already known, just replace them directly. For other cases,
551 // like parameters whose values may change over time, use |replacements|.
552 url
->erase(start
, end
- start
+ 1);
553 if (parameter
== kSearchTermsParameter
) {
554 replacements
->push_back(Replacement(SEARCH_TERMS
, start
));
555 } else if (parameter
== kCountParameter
) {
557 url
->insert(start
, kDefaultCount
);
558 } else if (parameter
== kGoogleAssistedQueryStatsParameter
) {
559 replacements
->push_back(Replacement(GOOGLE_ASSISTED_QUERY_STATS
, start
));
560 } else if (parameter
== kGoogleBaseURLParameter
) {
561 replacements
->push_back(Replacement(GOOGLE_BASE_URL
, start
));
562 } else if (parameter
== kGoogleBaseSuggestURLParameter
) {
563 replacements
->push_back(Replacement(GOOGLE_BASE_SUGGEST_URL
, start
));
564 } else if (parameter
== kGoogleBookmarkBarPinnedParameter
) {
565 replacements
->push_back(Replacement(GOOGLE_BOOKMARK_BAR_PINNED
, start
));
566 } else if (parameter
== kGoogleCurrentPageUrlParameter
) {
567 replacements
->push_back(Replacement(GOOGLE_CURRENT_PAGE_URL
, start
));
568 } else if (parameter
== kGoogleCursorPositionParameter
) {
569 replacements
->push_back(Replacement(GOOGLE_CURSOR_POSITION
, start
));
570 } else if (parameter
== kGoogleImageOriginalHeight
) {
571 replacements
->push_back(
572 Replacement(TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_HEIGHT
, start
));
573 } else if (parameter
== kGoogleImageOriginalWidth
) {
574 replacements
->push_back(
575 Replacement(TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH
, start
));
576 } else if (parameter
== kGoogleImageSearchSource
) {
577 url
->insert(start
, GetGoogleImageSearchSource());
578 } else if (parameter
== kGoogleImageThumbnailParameter
) {
579 replacements
->push_back(
580 Replacement(TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL
, start
));
581 } else if (parameter
== kGoogleImageURLParameter
) {
582 replacements
->push_back(Replacement(TemplateURLRef::GOOGLE_IMAGE_URL
,
584 } else if (parameter
== kGoogleForceInstantResultsParameter
) {
585 replacements
->push_back(Replacement(GOOGLE_FORCE_INSTANT_RESULTS
, start
));
586 } else if (parameter
== kGoogleInstantExtendedEnabledParameter
) {
587 replacements
->push_back(Replacement(GOOGLE_INSTANT_EXTENDED_ENABLED
,
589 } else if (parameter
== kGoogleInstantExtendedEnabledKey
) {
590 url
->insert(start
, google_util::kInstantExtendedAPIParam
);
591 } else if (parameter
== kGoogleNTPIsThemedParameter
) {
592 replacements
->push_back(Replacement(GOOGLE_NTP_IS_THEMED
, start
));
593 } else if (parameter
== kGoogleOmniboxStartMarginParameter
) {
594 replacements
->push_back(Replacement(GOOGLE_OMNIBOX_START_MARGIN
, start
));
595 } else if (parameter
== kGoogleOriginalQueryForSuggestionParameter
) {
596 replacements
->push_back(Replacement(GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION
,
598 } else if (parameter
== kGooglePageClassificationParameter
) {
599 replacements
->push_back(Replacement(GOOGLE_PAGE_CLASSIFICATION
, start
));
600 } else if (parameter
== kGoogleRLZParameter
) {
601 replacements
->push_back(Replacement(GOOGLE_RLZ
, start
));
602 } else if (parameter
== kGoogleSearchClient
) {
603 replacements
->push_back(Replacement(GOOGLE_SEARCH_CLIENT
, start
));
604 } else if (parameter
== kGoogleSearchFieldtrialParameter
) {
605 replacements
->push_back(Replacement(GOOGLE_SEARCH_FIELDTRIAL_GROUP
, start
));
606 } else if (parameter
== kGoogleSearchVersion
) {
607 if (CommandLine::ForCurrentProcess()->HasSwitch(
608 switches::kEnableAnswersInSuggest
))
609 url
->insert(start
, "gs_rn=42&");
610 } else if (parameter
== kGoogleSessionToken
) {
611 replacements
->push_back(Replacement(GOOGLE_SESSION_TOKEN
, start
));
612 } else if (parameter
== kGoogleSourceIdParameter
) {
613 #if defined(OS_ANDROID)
614 url
->insert(start
, "sourceid=chrome-mobile&");
616 url
->insert(start
, "sourceid=chrome&");
618 } else if (parameter
== kGoogleSuggestAPIKeyParameter
) {
620 net::EscapeQueryParamValue(google_apis::GetAPIKey(), false));
621 } else if (parameter
== kGoogleSuggestClient
) {
622 replacements
->push_back(Replacement(GOOGLE_SUGGEST_CLIENT
, start
));
623 } else if (parameter
== kGoogleSuggestRequestId
) {
624 replacements
->push_back(Replacement(GOOGLE_SUGGEST_REQUEST_ID
, start
));
625 } else if (parameter
== kGoogleUnescapedSearchTermsParameter
) {
626 replacements
->push_back(Replacement(GOOGLE_UNESCAPED_SEARCH_TERMS
, start
));
627 } else if (parameter
== kInputEncodingParameter
) {
628 replacements
->push_back(Replacement(ENCODING
, start
));
629 } else if (parameter
== kLanguageParameter
) {
630 replacements
->push_back(Replacement(LANGUAGE
, start
));
631 } else if (parameter
== kOutputEncodingParameter
) {
633 url
->insert(start
, kOutputEncodingType
);
634 } else if ((parameter
== kStartIndexParameter
) ||
635 (parameter
== kStartPageParameter
)) {
636 // We don't support these.
638 url
->insert(start
, "1");
639 } else if (!prepopulated_
) {
640 // If it's a prepopulated URL, we know that it's safe to remove unknown
641 // parameters, so just ignore this and return true below. Otherwise it could
642 // be some garbage but can also be a javascript block. Put it back.
643 url
->insert(start
, full_parameter
);
649 std::string
TemplateURLRef::ParseURL(const std::string
& url
,
650 Replacements
* replacements
,
651 PostParams
* post_params
,
654 std::string parsed_url
= url
;
655 for (size_t last
= 0; last
!= std::string::npos
; ) {
656 last
= parsed_url
.find(kStartParameter
, last
);
657 if (last
!= std::string::npos
) {
658 size_t template_end
= parsed_url
.find(kEndParameter
, last
);
659 if (template_end
!= std::string::npos
) {
660 // Since we allow Javascript in the URL, {} pairs could be nested. Match
661 // only leaf pairs with supported parameters.
662 size_t next_template_start
= parsed_url
.find(kStartParameter
, last
+ 1);
663 if (next_template_start
== std::string::npos
||
664 next_template_start
> template_end
) {
665 // If successful, ParseParameter erases from the string as such no
666 // need to update |last|. If failed, move |last| to the end of pair.
667 if (!ParseParameter(last
, template_end
, &parsed_url
, replacements
)) {
668 // |template_end| + 1 may be beyond the end of the string.
672 last
= next_template_start
;
675 // Open brace without a closing brace, return.
676 return std::string();
681 // Handles the post parameters.
682 const std::string
& post_params_string
= GetPostParamsString();
683 if (!post_params_string
.empty()) {
684 typedef std::vector
<std::string
> Strings
;
686 base::SplitString(post_params_string
, ',', ¶m_list
);
688 for (Strings::const_iterator iterator
= param_list
.begin();
689 iterator
!= param_list
.end(); ++iterator
) {
691 // The '=' delimiter is required and the name must be not empty.
692 base::SplitString(*iterator
, '=', &parts
);
693 if ((parts
.size() != 2U) || parts
[0].empty())
694 return std::string();
696 std::string
& value
= parts
[1];
697 size_t replacements_size
= replacements
->size();
698 if (IsTemplateParameterString(value
))
699 ParseParameter(0, value
.length() - 1, &value
, replacements
);
700 post_params
->push_back(std::make_pair(parts
[0], value
));
701 // If there was a replacement added, points its index to last added
703 if (replacements
->size() > replacements_size
) {
704 DCHECK_EQ(replacements_size
+ 1, replacements
->size());
705 Replacement
* r
= &replacements
->back();
706 r
->is_post_param
= true;
707 r
->index
= post_params
->size() - 1;
710 DCHECK(!post_params
->empty());
717 void TemplateURLRef::ParseIfNecessary() const {
718 UIThreadSearchTermsData
search_terms_data(owner_
->profile());
719 ParseIfNecessaryUsingTermsData(search_terms_data
);
722 void TemplateURLRef::ParseIfNecessaryUsingTermsData(
723 const SearchTermsData
& search_terms_data
) const {
725 InvalidateCachedValues();
727 parsed_url_
= ParseURL(GetURL(), &replacements_
, &post_params_
, &valid_
);
728 supports_replacements_
= false;
730 bool has_only_one_search_term
= false;
731 for (Replacements::const_iterator i
= replacements_
.begin();
732 i
!= replacements_
.end(); ++i
) {
733 if ((i
->type
== SEARCH_TERMS
) ||
734 (i
->type
== GOOGLE_UNESCAPED_SEARCH_TERMS
)) {
735 if (has_only_one_search_term
) {
736 has_only_one_search_term
= false;
739 has_only_one_search_term
= true;
740 supports_replacements_
= true;
743 // Only parse the host/key if there is one search term. Technically there
744 // could be more than one term, but it's uncommon; so we punt.
745 if (has_only_one_search_term
)
746 ParseHostAndSearchTermKey(search_terms_data
);
751 void TemplateURLRef::ParseHostAndSearchTermKey(
752 const SearchTermsData
& search_terms_data
) const {
753 std::string
url_string(GetURL());
754 ReplaceSubstringsAfterOffset(&url_string
, 0,
755 kGoogleBaseURLParameterFull
,
756 search_terms_data
.GoogleBaseURLValue());
757 ReplaceSubstringsAfterOffset(&url_string
, 0,
758 kGoogleBaseSuggestURLParameterFull
,
759 search_terms_data
.GoogleBaseSuggestURLValue());
761 search_term_key_
.clear();
764 search_term_key_location_
= url::Parsed::REF
;
766 GURL
url(url_string
);
770 std::string query_key
= FindSearchTermsKey(url
.query());
771 std::string ref_key
= FindSearchTermsKey(url
.ref());
772 if (query_key
.empty() == ref_key
.empty())
773 return; // No key or multiple keys found. We only handle having one key.
774 search_term_key_
= query_key
.empty() ? ref_key
: query_key
;
775 search_term_key_location_
=
776 query_key
.empty() ? url::Parsed::REF
: url::Parsed::QUERY
;
781 void TemplateURLRef::HandleReplacement(const std::string
& name
,
782 const std::string
& value
,
783 const Replacement
& replacement
,
784 std::string
* url
) const {
785 size_t pos
= replacement
.index
;
786 if (replacement
.is_post_param
) {
787 DCHECK_LT(pos
, post_params_
.size());
788 DCHECK(!post_params_
[pos
].first
.empty());
789 post_params_
[pos
].second
= value
;
791 url
->insert(pos
, name
.empty() ? value
: (name
+ "=" + value
+ "&"));
795 std::string
TemplateURLRef::HandleReplacements(
796 const SearchTermsArgs
& search_terms_args
,
797 const SearchTermsData
& search_terms_data
,
798 PostContent
* post_content
) const {
799 if (replacements_
.empty()) {
800 if (!post_params_
.empty())
801 EncodeFormData(post_params_
, post_content
);
805 // Determine if the search terms are in the query or before. We're escaping
806 // space as '+' in the former case and as '%20' in the latter case.
807 bool is_in_query
= true;
808 for (Replacements::iterator i
= replacements_
.begin();
809 i
!= replacements_
.end(); ++i
) {
810 if (i
->type
== SEARCH_TERMS
) {
811 base::string16::size_type query_start
= parsed_url_
.find('?');
812 is_in_query
= query_start
!= base::string16::npos
&&
813 (static_cast<base::string16::size_type
>(i
->index
) > query_start
);
818 std::string input_encoding
;
819 base::string16 encoded_terms
;
820 base::string16 encoded_original_query
;
821 owner_
->EncodeSearchTerms(search_terms_args
, is_in_query
, &input_encoding
,
822 &encoded_terms
, &encoded_original_query
);
824 std::string url
= parsed_url_
;
826 // replacements_ is ordered in ascending order, as such we need to iterate
828 for (Replacements::reverse_iterator i
= replacements_
.rbegin();
829 i
!= replacements_
.rend(); ++i
) {
832 HandleReplacement(std::string(), input_encoding
, *i
, &url
);
835 case GOOGLE_ASSISTED_QUERY_STATS
:
836 DCHECK(!i
->is_post_param
);
837 if (!search_terms_args
.assisted_query_stats
.empty()) {
838 // Get the base URL without substituting AQS to avoid infinite
839 // recursion. We need the URL to find out if it meets all
840 // AQS requirements (e.g. HTTPS protocol check).
841 // See TemplateURLRef::SearchTermsArgs for more details.
842 SearchTermsArgs
search_terms_args_without_aqs(search_terms_args
);
843 search_terms_args_without_aqs
.assisted_query_stats
.clear();
844 GURL
base_url(ReplaceSearchTermsUsingTermsData(
845 search_terms_args_without_aqs
, search_terms_data
, NULL
));
846 if (base_url
.SchemeIs(url::kHttpsScheme
)) {
848 "aqs", search_terms_args
.assisted_query_stats
, *i
, &url
);
853 case GOOGLE_BASE_URL
:
854 DCHECK(!i
->is_post_param
);
856 std::string(), search_terms_data
.GoogleBaseURLValue(), *i
, &url
);
859 case GOOGLE_BASE_SUGGEST_URL
:
860 DCHECK(!i
->is_post_param
);
862 std::string(), search_terms_data
.GoogleBaseSuggestURLValue(), *i
,
866 case GOOGLE_BOOKMARK_BAR_PINNED
:
867 if (showing_search_terms_
) {
868 // Log whether the bookmark bar is pinned when the user is seeing
869 // InstantExtended on the SRP.
870 DCHECK(!i
->is_post_param
);
872 "bmbp", search_terms_args
.bookmark_bar_pinned
? "1" : "0", *i
,
877 case GOOGLE_CURRENT_PAGE_URL
:
878 DCHECK(!i
->is_post_param
);
879 if (!search_terms_args
.current_page_url
.empty()) {
880 const std::string
& escaped_current_page_url
=
881 net::EscapeQueryParamValue(search_terms_args
.current_page_url
,
883 HandleReplacement("url", escaped_current_page_url
, *i
, &url
);
887 case GOOGLE_CURSOR_POSITION
:
888 DCHECK(!i
->is_post_param
);
889 if (search_terms_args
.cursor_position
!= base::string16::npos
)
892 base::StringPrintf("%" PRIuS
, search_terms_args
.cursor_position
),
897 case GOOGLE_FORCE_INSTANT_RESULTS
:
898 DCHECK(!i
->is_post_param
);
899 HandleReplacement(std::string(),
900 chrome::ForceInstantResultsParam(
901 search_terms_args
.force_instant_results
),
906 case GOOGLE_INSTANT_EXTENDED_ENABLED
:
907 DCHECK(!i
->is_post_param
);
908 HandleReplacement(std::string(),
909 chrome::InstantExtendedEnabledParam(type_
== SEARCH
),
914 case GOOGLE_NTP_IS_THEMED
:
915 DCHECK(!i
->is_post_param
);
917 std::string(), search_terms_data
.NTPIsThemedParam(), *i
, &url
);
920 case GOOGLE_OMNIBOX_START_MARGIN
:
921 DCHECK(!i
->is_post_param
);
922 if (search_terms_args
.omnibox_start_margin
>= 0) {
925 base::IntToString(search_terms_args
.omnibox_start_margin
),
931 case GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION
:
932 DCHECK(!i
->is_post_param
);
933 if (search_terms_args
.accepted_suggestion
>= 0 ||
934 !search_terms_args
.assisted_query_stats
.empty()) {
936 "oq", base::UTF16ToUTF8(encoded_original_query
), *i
, &url
);
940 case GOOGLE_PAGE_CLASSIFICATION
:
941 if (search_terms_args
.page_classification
!=
942 AutocompleteInput::INVALID_SPEC
) {
944 "pgcl", base::IntToString(search_terms_args
.page_classification
),
950 DCHECK(!i
->is_post_param
);
951 // On platforms that don't have RLZ, we still want this branch
952 // to happen so that we replace the RLZ template with the
953 // empty string. (If we don't handle this case, we hit a
954 // NOTREACHED below.)
955 base::string16 rlz_string
= search_terms_data
.GetRlzParameterValue(
956 search_terms_args
.from_app_list
);
957 if (!rlz_string
.empty()) {
958 HandleReplacement("rlz", base::UTF16ToUTF8(rlz_string
), *i
, &url
);
963 case GOOGLE_SEARCH_CLIENT
: {
964 DCHECK(!i
->is_post_param
);
965 std::string client
= search_terms_data
.GetSearchClient();
967 HandleReplacement("client", client
, *i
, &url
);
971 case GOOGLE_SEARCH_FIELDTRIAL_GROUP
:
972 // We are not currently running any fieldtrials that modulate the search
973 // url. If we do, then we'd have some conditional insert such as:
974 // url.insert(i->index, used_www ? "gcx=w&" : "gcx=c&");
977 case GOOGLE_SESSION_TOKEN
: {
978 std::string token
= search_terms_args
.session_token
;
980 HandleReplacement("psi", token
, *i
, &url
);
984 case GOOGLE_SUGGEST_CLIENT
:
986 std::string(), search_terms_data
.GetSuggestClient(), *i
, &url
);
989 case GOOGLE_SUGGEST_REQUEST_ID
:
991 std::string(), search_terms_data
.GetSuggestRequestIdentifier(), *i
,
995 case GOOGLE_UNESCAPED_SEARCH_TERMS
: {
996 std::string unescaped_terms
;
997 base::UTF16ToCodepage(search_terms_args
.search_terms
,
998 input_encoding
.c_str(),
999 base::OnStringConversionError::SKIP
,
1001 HandleReplacement(std::string(), unescaped_terms
, *i
, &url
);
1007 std::string(), search_terms_data
.GetApplicationLocale(), *i
, &url
);
1012 std::string(), base::UTF16ToUTF8(encoded_terms
), *i
, &url
);
1015 case GOOGLE_IMAGE_THUMBNAIL
:
1017 std::string(), search_terms_args
.image_thumbnail_content
, *i
, &url
);
1020 case GOOGLE_IMAGE_URL
:
1021 if (search_terms_args
.image_url
.is_valid()) {
1023 std::string(), search_terms_args
.image_url
.spec(), *i
, &url
);
1027 case GOOGLE_IMAGE_ORIGINAL_WIDTH
:
1028 if (!search_terms_args
.image_original_size
.IsEmpty()) {
1031 base::IntToString(search_terms_args
.image_original_size
.width()),
1036 case GOOGLE_IMAGE_ORIGINAL_HEIGHT
:
1037 if (!search_terms_args
.image_original_size
.IsEmpty()) {
1040 base::IntToString(search_terms_args
.image_original_size
.height()),
1051 if (!post_params_
.empty())
1052 EncodeFormData(post_params_
, post_content
);
1058 // TemplateURLData ------------------------------------------------------------
1060 TemplateURLData::TemplateURLData()
1061 : show_in_default_list(false),
1062 safe_for_autoreplace(false),
1064 date_created(base::Time::Now()),
1065 last_modified(base::Time::Now()),
1066 created_by_policy(false),
1069 sync_guid(base::GenerateGUID()),
1070 keyword_(base::ASCIIToUTF16("dummy")),
1074 TemplateURLData::~TemplateURLData() {
1077 void TemplateURLData::SetKeyword(const base::string16
& keyword
) {
1078 DCHECK(!keyword
.empty());
1080 // Case sensitive keyword matching is confusing. As such, we force all
1081 // keywords to be lower case.
1082 keyword_
= base::i18n::ToLower(keyword
);
1085 void TemplateURLData::SetURL(const std::string
& url
) {
1086 DCHECK(!url
.empty());
1091 // TemplateURL ----------------------------------------------------------------
1093 TemplateURL::TemplateURL(Profile
* profile
, const TemplateURLData
& data
)
1094 : profile_(profile
),
1096 url_ref_(this, TemplateURLRef::SEARCH
),
1097 suggestions_url_ref_(this,
1098 TemplateURLRef::SUGGEST
),
1099 instant_url_ref_(this,
1100 TemplateURLRef::INSTANT
),
1101 image_url_ref_(this, TemplateURLRef::IMAGE
),
1102 new_tab_url_ref_(this, TemplateURLRef::NEW_TAB
) {
1103 SetPrepopulateId(data_
.prepopulate_id
);
1105 if (data_
.search_terms_replacement_key
==
1106 kGoogleInstantExtendedEnabledKeyFull
) {
1107 data_
.search_terms_replacement_key
= google_util::kInstantExtendedAPIParam
;
1111 TemplateURL::~TemplateURL() {
1115 GURL
TemplateURL::GenerateFaviconURL(const GURL
& url
) {
1116 DCHECK(url
.is_valid());
1117 GURL::Replacements rep
;
1119 const char favicon_path
[] = "/favicon.ico";
1120 int favicon_path_len
= arraysize(favicon_path
) - 1;
1122 rep
.SetPath(favicon_path
, url::Component(0, favicon_path_len
));
1123 rep
.ClearUsername();
1124 rep
.ClearPassword();
1127 return url
.ReplaceComponents(rep
);
1131 bool TemplateURL::MatchesData(const TemplateURL
* t_url
,
1132 const TemplateURLData
* data
) {
1133 if (!t_url
|| !data
)
1134 return !t_url
&& !data
;
1136 return (t_url
->short_name() == data
->short_name
) &&
1137 t_url
->HasSameKeywordAs(*data
) &&
1138 (t_url
->url() == data
->url()) &&
1139 (t_url
->suggestions_url() == data
->suggestions_url
) &&
1140 (t_url
->instant_url() == data
->instant_url
) &&
1141 (t_url
->image_url() == data
->image_url
) &&
1142 (t_url
->new_tab_url() == data
->new_tab_url
) &&
1143 (t_url
->search_url_post_params() == data
->search_url_post_params
) &&
1144 (t_url
->suggestions_url_post_params() ==
1145 data
->suggestions_url_post_params
) &&
1146 (t_url
->instant_url_post_params() == data
->instant_url_post_params
) &&
1147 (t_url
->image_url_post_params() == data
->image_url_post_params
) &&
1148 (t_url
->favicon_url() == data
->favicon_url
) &&
1149 (t_url
->safe_for_autoreplace() == data
->safe_for_autoreplace
) &&
1150 (t_url
->show_in_default_list() == data
->show_in_default_list
) &&
1151 (t_url
->input_encodings() == data
->input_encodings
) &&
1152 (t_url
->alternate_urls() == data
->alternate_urls
) &&
1153 (t_url
->search_terms_replacement_key() ==
1154 data
->search_terms_replacement_key
);
1157 base::string16
TemplateURL::AdjustedShortNameForLocaleDirection() const {
1158 base::string16 bidi_safe_short_name
= data_
.short_name
;
1159 base::i18n::AdjustStringForLocaleDirection(&bidi_safe_short_name
);
1160 return bidi_safe_short_name
;
1163 bool TemplateURL::ShowInDefaultList() const {
1164 return data_
.show_in_default_list
&& url_ref_
.SupportsReplacement();
1167 bool TemplateURL::SupportsReplacement() const {
1168 UIThreadSearchTermsData
search_terms_data(profile_
);
1169 return SupportsReplacementUsingTermsData(search_terms_data
);
1172 bool TemplateURL::SupportsReplacementUsingTermsData(
1173 const SearchTermsData
& search_terms_data
) const {
1174 return url_ref_
.SupportsReplacementUsingTermsData(search_terms_data
);
1177 bool TemplateURL::HasGoogleBaseURLs() const {
1178 return url_ref_
.HasGoogleBaseURLs() ||
1179 suggestions_url_ref_
.HasGoogleBaseURLs() ||
1180 instant_url_ref_
.HasGoogleBaseURLs() ||
1181 image_url_ref_
.HasGoogleBaseURLs() ||
1182 new_tab_url_ref_
.HasGoogleBaseURLs();
1185 bool TemplateURL::IsGoogleSearchURLWithReplaceableKeyword() const {
1186 return (GetType() == NORMAL
) && url_ref_
.HasGoogleBaseURLs() &&
1187 google_util::IsGoogleHostname(base::UTF16ToUTF8(data_
.keyword()),
1188 google_util::DISALLOW_SUBDOMAIN
);
1191 bool TemplateURL::HasSameKeywordAs(const TemplateURLData
& other
) const {
1192 return (data_
.keyword() == other
.keyword()) ||
1193 (IsGoogleSearchURLWithReplaceableKeyword() &&
1194 TemplateURL(NULL
, other
).IsGoogleSearchURLWithReplaceableKeyword());
1197 TemplateURL::Type
TemplateURL::GetType() const {
1198 if (extension_info_
)
1199 return NORMAL_CONTROLLED_BY_EXTENSION
;
1200 return GURL(data_
.url()).SchemeIs(extensions::kExtensionScheme
) ?
1201 OMNIBOX_API_EXTENSION
: NORMAL
;
1204 std::string
TemplateURL::GetExtensionId() const {
1205 DCHECK_NE(NORMAL
, GetType());
1206 return extension_info_
?
1207 extension_info_
->extension_id
: GURL(data_
.url()).host();
1210 size_t TemplateURL::URLCount() const {
1211 // Add 1 for the regular search URL.
1212 return data_
.alternate_urls
.size() + 1;
1215 const std::string
& TemplateURL::GetURL(size_t index
) const {
1216 DCHECK_LT(index
, URLCount());
1218 return (index
< data_
.alternate_urls
.size()) ?
1219 data_
.alternate_urls
[index
] : url();
1222 bool TemplateURL::ExtractSearchTermsFromURL(
1224 base::string16
* search_terms
) {
1225 UIThreadSearchTermsData
search_terms_data(profile_
);
1226 return ExtractSearchTermsFromURLUsingTermsData(url
, search_terms
,
1230 bool TemplateURL::ExtractSearchTermsFromURLUsingTermsData(
1232 base::string16
* search_terms
,
1233 const SearchTermsData
& search_terms_data
) {
1234 return FindSearchTermsInURL(url
, search_terms_data
, search_terms
, NULL
, NULL
);
1238 bool TemplateURL::IsSearchURL(const GURL
& url
) {
1239 UIThreadSearchTermsData
search_terms_data(profile_
);
1240 return IsSearchURLUsingTermsData(url
, search_terms_data
);
1243 bool TemplateURL::IsSearchURLUsingTermsData(
1245 const SearchTermsData
& search_terms_data
) {
1246 base::string16 search_terms
;
1247 return ExtractSearchTermsFromURLUsingTermsData(
1248 url
, &search_terms
, search_terms_data
) && !search_terms
.empty();
1251 bool TemplateURL::HasSearchTermsReplacementKey(const GURL
& url
) const {
1252 // Look for the key both in the query and the ref.
1253 std::string params
[] = {url
.query(), url
.ref()};
1255 for (int i
= 0; i
< 2; ++i
) {
1256 url::Component query
, key
, value
;
1257 query
.len
= static_cast<int>(params
[i
].size());
1258 while (url::ExtractQueryKeyValue(params
[i
].c_str(), &query
, &key
, &value
)) {
1259 if (key
.is_nonempty() &&
1260 params
[i
].substr(key
.begin
, key
.len
) ==
1261 search_terms_replacement_key()) {
1269 bool TemplateURL::ReplaceSearchTermsInURL(
1271 const TemplateURLRef::SearchTermsArgs
& search_terms_args
,
1273 UIThreadSearchTermsData
search_terms_data(profile_
);
1274 // TODO(beaudoin): Use AQS from |search_terms_args| too.
1275 url::Parsed::ComponentType search_term_component
;
1276 url::Component search_terms_position
;
1277 base::string16 search_terms
;
1278 if (!FindSearchTermsInURL(url
, search_terms_data
, &search_terms
,
1279 &search_term_component
, &search_terms_position
)) {
1282 DCHECK(search_terms_position
.is_nonempty());
1284 // FindSearchTermsInURL only returns true for search terms in the query or
1285 // ref, so we can call EncodeSearchTerm with |is_in_query| = true, since query
1286 // and ref are encoded in the same way.
1287 std::string input_encoding
;
1288 base::string16 encoded_terms
;
1289 base::string16 encoded_original_query
;
1290 EncodeSearchTerms(search_terms_args
, true, &input_encoding
,
1291 &encoded_terms
, &encoded_original_query
);
1293 std::string
old_params(
1294 (search_term_component
== url::Parsed::REF
) ? url
.ref() : url
.query());
1295 std::string
new_params(old_params
, 0, search_terms_position
.begin
);
1296 new_params
+= base::UTF16ToUTF8(search_terms_args
.search_terms
);
1297 new_params
+= old_params
.substr(search_terms_position
.end());
1298 url::StdStringReplacements
<std::string
> replacements
;
1299 if (search_term_component
== url::Parsed::REF
)
1300 replacements
.SetRefStr(new_params
);
1302 replacements
.SetQueryStr(new_params
);
1303 *result
= url
.ReplaceComponents(replacements
);
1307 void TemplateURL::EncodeSearchTerms(
1308 const TemplateURLRef::SearchTermsArgs
& search_terms_args
,
1310 std::string
* input_encoding
,
1311 base::string16
* encoded_terms
,
1312 base::string16
* encoded_original_query
) const {
1314 std::vector
<std::string
> encodings(input_encodings());
1315 if (std::find(encodings
.begin(), encodings
.end(), "UTF-8") == encodings
.end())
1316 encodings
.push_back("UTF-8");
1317 for (std::vector
<std::string
>::const_iterator
i(encodings
.begin());
1318 i
!= encodings
.end(); ++i
) {
1319 if (TryEncoding(search_terms_args
.search_terms
,
1320 search_terms_args
.original_query
, i
->c_str(),
1321 is_in_query
, encoded_terms
, encoded_original_query
)) {
1322 *input_encoding
= *i
;
1329 void TemplateURL::CopyFrom(const TemplateURL
& other
) {
1333 profile_
= other
.profile_
;
1334 data_
= other
.data_
;
1335 url_ref_
.InvalidateCachedValues();
1336 suggestions_url_ref_
.InvalidateCachedValues();
1337 instant_url_ref_
.InvalidateCachedValues();
1338 SetPrepopulateId(other
.data_
.prepopulate_id
);
1341 void TemplateURL::SetURL(const std::string
& url
) {
1343 url_ref_
.InvalidateCachedValues();
1346 void TemplateURL::SetPrepopulateId(int id
) {
1347 data_
.prepopulate_id
= id
;
1348 const bool prepopulated
= id
> 0;
1349 url_ref_
.prepopulated_
= prepopulated
;
1350 suggestions_url_ref_
.prepopulated_
= prepopulated
;
1351 instant_url_ref_
.prepopulated_
= prepopulated
;
1354 void TemplateURL::ResetKeywordIfNecessary(bool force
) {
1355 if (IsGoogleSearchURLWithReplaceableKeyword() || force
) {
1356 DCHECK(GetType() != OMNIBOX_API_EXTENSION
);
1357 GURL
url(TemplateURLService::GenerateSearchURL(this));
1359 data_
.SetKeyword(TemplateURLService::GenerateKeyword(url
));
1363 bool TemplateURL::FindSearchTermsInURL(
1365 const SearchTermsData
& search_terms_data
,
1366 base::string16
* search_terms
,
1367 url::Parsed::ComponentType
* search_term_component
,
1368 url::Component
* search_terms_position
) {
1369 DCHECK(search_terms
);
1370 search_terms
->clear();
1372 // Try to match with every pattern.
1373 for (size_t i
= 0; i
< URLCount(); ++i
) {
1374 TemplateURLRef
ref(this, i
);
1375 if (ref
.ExtractSearchTermsFromURL(url
, search_terms
, search_terms_data
,
1376 search_term_component
, search_terms_position
)) {
1377 // If ExtractSearchTermsFromURL() returns true and |search_terms| is empty
1378 // it means the pattern matched but no search terms were present. In this
1379 // case we fail immediately without looking for matches in subsequent
1380 // patterns. This means that given patterns
1381 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ],
1382 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would
1383 // return false. This is important for at least Google, where such URLs
1385 return !search_terms
->empty();