Add GetSigninScopedDeviceId() to SigninClient and pass it to GaiaAuthFetcher.
[chromium-blink-merge.git] / components / search_engines / template_url.cc
blob2906080370714e16d30df79a82b96c6a231398c3
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/search_engines/template_url.h"
7 #include <string>
8 #include <vector>
10 #include "base/basictypes.h"
11 #include "base/command_line.h"
12 #include "base/format_macros.h"
13 #include "base/i18n/icu_string_conversions.h"
14 #include "base/i18n/rtl.h"
15 #include "base/logging.h"
16 #include "base/metrics/field_trial.h"
17 #include "base/rand_util.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/string_split.h"
20 #include "base/strings/string_util.h"
21 #include "base/strings/stringprintf.h"
22 #include "base/strings/utf_string_conversions.h"
23 #include "components/google/core/browser/google_util.h"
24 #include "components/metrics/proto/omnibox_input_type.pb.h"
25 #include "components/search_engines/search_engines_switches.h"
26 #include "components/search_engines/search_terms_data.h"
27 #include "google_apis/google_api_keys.h"
28 #include "net/base/escape.h"
29 #include "net/base/mime_util.h"
30 #include "net/base/net_util.h"
32 namespace {
34 // The TemplateURLRef has any number of terms that need to be replaced. Each of
35 // the terms is enclosed in braces. If the character preceeding the final
36 // brace is a ?, it indicates the term is optional and can be replaced with
37 // an empty string.
38 const char kStartParameter = '{';
39 const char kEndParameter = '}';
40 const char kOptional = '?';
42 // Known parameters found in the URL.
43 const char kSearchTermsParameter[] = "searchTerms";
44 const char kSearchTermsParameterFull[] = "{searchTerms}";
45 const char kCountParameter[] = "count";
46 const char kStartIndexParameter[] = "startIndex";
47 const char kStartPageParameter[] = "startPage";
48 const char kLanguageParameter[] = "language";
49 const char kInputEncodingParameter[] = "inputEncoding";
50 const char kOutputEncodingParameter[] = "outputEncoding";
52 const char kGoogleAssistedQueryStatsParameter[] = "google:assistedQueryStats";
54 // Host/Domain Google searches are relative to.
55 const char kGoogleBaseURLParameter[] = "google:baseURL";
56 const char kGoogleBaseURLParameterFull[] = "{google:baseURL}";
58 // Like google:baseURL, but for the Search Suggest capability.
59 const char kGoogleBaseSuggestURLParameter[] = "google:baseSuggestURL";
60 const char kGoogleBaseSuggestURLParameterFull[] = "{google:baseSuggestURL}";
61 const char kGoogleBookmarkBarPinnedParameter[] = "google:bookmarkBarPinned";
62 const char kGoogleContextualSearchContextData[] =
63 "google:contextualSearchContextData";
64 const char kGoogleContextualSearchVersion[] = "google:contextualSearchVersion";
65 const char kGoogleCurrentPageUrlParameter[] = "google:currentPageUrl";
66 const char kGoogleCursorPositionParameter[] = "google:cursorPosition";
67 const char kGoogleForceInstantResultsParameter[] = "google:forceInstantResults";
68 const char kGoogleImageSearchSource[] = "google:imageSearchSource";
69 const char kGoogleImageThumbnailParameter[] = "google:imageThumbnail";
70 const char kGoogleImageOriginalWidth[] = "google:imageOriginalWidth";
71 const char kGoogleImageOriginalHeight[] = "google:imageOriginalHeight";
72 const char kGoogleImageURLParameter[] = "google:imageURL";
73 const char kGoogleInputTypeParameter[] = "google:inputType";
74 const char kGoogleInstantExtendedEnabledParameter[] =
75 "google:instantExtendedEnabledParameter";
76 const char kGoogleInstantExtendedEnabledKey[] =
77 "google:instantExtendedEnabledKey";
78 const char kGoogleInstantExtendedEnabledKeyFull[] =
79 "{google:instantExtendedEnabledKey}";
80 const char kGoogleNTPIsThemedParameter[] = "google:ntpIsThemedParameter";
81 const char kGoogleOmniboxStartMarginParameter[] =
82 "google:omniboxStartMarginParameter";
83 const char kGoogleOriginalQueryForSuggestionParameter[] =
84 "google:originalQueryForSuggestion";
85 const char kGooglePageClassificationParameter[] = "google:pageClassification";
86 const char kGooglePrefetchQuery[] = "google:prefetchQuery";
87 const char kGoogleRLZParameter[] = "google:RLZ";
88 const char kGoogleSearchClient[] = "google:searchClient";
89 const char kGoogleSearchFieldtrialParameter[] =
90 "google:searchFieldtrialParameter";
91 const char kGoogleSearchVersion[] = "google:searchVersion";
92 const char kGoogleSessionToken[] = "google:sessionToken";
93 const char kGoogleSourceIdParameter[] = "google:sourceId";
94 const char kGoogleSuggestAPIKeyParameter[] = "google:suggestAPIKeyParameter";
95 const char kGoogleSuggestClient[] = "google:suggestClient";
96 const char kGoogleSuggestRequestId[] = "google:suggestRid";
98 // Same as kSearchTermsParameter, with no escaping.
99 const char kGoogleUnescapedSearchTermsParameter[] =
100 "google:unescapedSearchTerms";
101 const char kGoogleUnescapedSearchTermsParameterFull[] =
102 "{google:unescapedSearchTerms}";
104 // Display value for kSearchTermsParameter.
105 const char kDisplaySearchTerms[] = "%s";
107 // Display value for kGoogleUnescapedSearchTermsParameter.
108 const char kDisplayUnescapedSearchTerms[] = "%S";
110 // Used if the count parameter is not optional. Indicates we want 10 search
111 // results.
112 const char kDefaultCount[] = "10";
114 // Used if the parameter kOutputEncodingParameter is required.
115 const char kOutputEncodingType[] = "UTF-8";
117 // Attempts to encode |terms| and |original_query| in |encoding| and escape
118 // them. |terms| may be escaped as path or query depending on |is_in_query|;
119 // |original_query| is always escaped as query. Returns whether the encoding
120 // process succeeded.
121 bool TryEncoding(const base::string16& terms,
122 const base::string16& original_query,
123 const char* encoding,
124 bool is_in_query,
125 base::string16* escaped_terms,
126 base::string16* escaped_original_query) {
127 DCHECK(escaped_terms);
128 DCHECK(escaped_original_query);
129 std::string encoded_terms;
130 if (!base::UTF16ToCodepage(terms, encoding,
131 base::OnStringConversionError::SKIP, &encoded_terms))
132 return false;
133 *escaped_terms = base::UTF8ToUTF16(is_in_query ?
134 net::EscapeQueryParamValue(encoded_terms, true) :
135 net::EscapePath(encoded_terms));
136 if (original_query.empty())
137 return true;
138 std::string encoded_original_query;
139 if (!base::UTF16ToCodepage(original_query, encoding,
140 base::OnStringConversionError::SKIP, &encoded_original_query))
141 return false;
142 *escaped_original_query = base::UTF8ToUTF16(
143 net::EscapeQueryParamValue(encoded_original_query, true));
144 return true;
147 // Extract query key and host given a list of parameters coming from the URL
148 // query or ref.
149 std::string FindSearchTermsKey(const std::string& params) {
150 if (params.empty())
151 return std::string();
152 url::Component query, key, value;
153 query.len = static_cast<int>(params.size());
154 while (url::ExtractQueryKeyValue(params.c_str(), &query, &key, &value)) {
155 if (key.is_nonempty() && value.is_nonempty()) {
156 std::string value_string = params.substr(value.begin, value.len);
157 if (value_string.find(kSearchTermsParameterFull, 0) !=
158 std::string::npos ||
159 value_string.find(kGoogleUnescapedSearchTermsParameterFull, 0) !=
160 std::string::npos) {
161 return params.substr(key.begin, key.len);
165 return std::string();
168 bool IsTemplateParameterString(const std::string& param) {
169 return (param.length() > 2) && (*(param.begin()) == kStartParameter) &&
170 (*(param.rbegin()) == kEndParameter);
173 } // namespace
176 // TemplateURLRef::SearchTermsArgs --------------------------------------------
178 TemplateURLRef::SearchTermsArgs::SearchTermsArgs(
179 const base::string16& search_terms)
180 : search_terms(search_terms),
181 input_type(metrics::OmniboxInputType::INVALID),
182 accepted_suggestion(NO_SUGGESTIONS_AVAILABLE),
183 cursor_position(base::string16::npos),
184 omnibox_start_margin(-1),
185 page_classification(metrics::OmniboxEventProto::INVALID_SPEC),
186 bookmark_bar_pinned(false),
187 append_extra_query_params(false),
188 force_instant_results(false),
189 from_app_list(false),
190 contextual_search_params(ContextualSearchParams()) {
193 TemplateURLRef::SearchTermsArgs::~SearchTermsArgs() {
196 TemplateURLRef::SearchTermsArgs::ContextualSearchParams::
197 ContextualSearchParams()
198 : version(-1),
199 start(base::string16::npos),
200 end(base::string16::npos) {
203 TemplateURLRef::SearchTermsArgs::ContextualSearchParams::
204 ContextualSearchParams(
205 const int version,
206 const size_t start,
207 const size_t end,
208 const std::string& selection,
209 const std::string& content,
210 const std::string& base_page_url,
211 const std::string& encoding)
212 : version(version),
213 start(start),
214 end(end),
215 selection(selection),
216 content(content),
217 base_page_url(base_page_url),
218 encoding(encoding) {
221 TemplateURLRef::SearchTermsArgs::ContextualSearchParams::
222 ~ContextualSearchParams() {
225 // TemplateURLRef -------------------------------------------------------------
227 TemplateURLRef::TemplateURLRef(TemplateURL* owner, Type type)
228 : owner_(owner),
229 type_(type),
230 index_in_owner_(-1),
231 parsed_(false),
232 valid_(false),
233 supports_replacements_(false),
234 search_term_key_location_(url::Parsed::QUERY),
235 prepopulated_(false) {
236 DCHECK(owner_);
237 DCHECK_NE(INDEXED, type_);
240 TemplateURLRef::TemplateURLRef(TemplateURL* owner, size_t index_in_owner)
241 : owner_(owner),
242 type_(INDEXED),
243 index_in_owner_(index_in_owner),
244 parsed_(false),
245 valid_(false),
246 supports_replacements_(false),
247 search_term_key_location_(url::Parsed::QUERY),
248 prepopulated_(false) {
249 DCHECK(owner_);
250 DCHECK_LT(index_in_owner_, owner_->URLCount());
253 TemplateURLRef::~TemplateURLRef() {
256 std::string TemplateURLRef::GetURL() const {
257 switch (type_) {
258 case SEARCH: return owner_->url();
259 case SUGGEST: return owner_->suggestions_url();
260 case INSTANT: return owner_->instant_url();
261 case IMAGE: return owner_->image_url();
262 case NEW_TAB: return owner_->new_tab_url();
263 case CONTEXTUAL_SEARCH: return owner_->contextual_search_url();
264 case INDEXED: return owner_->GetURL(index_in_owner_);
265 default: NOTREACHED(); return std::string(); // NOLINT
269 std::string TemplateURLRef::GetPostParamsString() const {
270 switch (type_) {
271 case INDEXED:
272 case SEARCH: return owner_->search_url_post_params();
273 case SUGGEST: return owner_->suggestions_url_post_params();
274 case INSTANT: return owner_->instant_url_post_params();
275 case NEW_TAB: return std::string();
276 case CONTEXTUAL_SEARCH: return std::string();
277 case IMAGE: return owner_->image_url_post_params();
278 default: NOTREACHED(); return std::string(); // NOLINT
282 bool TemplateURLRef::UsesPOSTMethod(
283 const SearchTermsData& search_terms_data) const {
284 ParseIfNecessary(search_terms_data);
285 return !post_params_.empty();
288 bool TemplateURLRef::EncodeFormData(const PostParams& post_params,
289 PostContent* post_content) const {
290 if (post_params.empty())
291 return true;
292 if (!post_content)
293 return false;
295 const char kUploadDataMIMEType[] = "multipart/form-data; boundary=";
296 const char kMultipartBoundary[] = "----+*+----%016" PRIx64 "----+*+----";
297 // Each name/value pair is stored in a body part which is preceded by a
298 // boundary delimiter line. Uses random number generator here to create
299 // a unique boundary delimiter for form data encoding.
300 std::string boundary = base::StringPrintf(kMultipartBoundary,
301 base::RandUint64());
302 // Sets the content MIME type.
303 post_content->first = kUploadDataMIMEType;
304 post_content->first += boundary;
305 // Encodes the post parameters.
306 std::string* post_data = &post_content->second;
307 post_data->clear();
308 for (PostParams::const_iterator param = post_params.begin();
309 param != post_params.end(); ++param) {
310 DCHECK(!param->first.empty());
311 net::AddMultipartValueForUpload(param->first, param->second, boundary,
312 std::string(), post_data);
314 net::AddMultipartFinalDelimiterForUpload(boundary, post_data);
315 return true;
318 bool TemplateURLRef::SupportsReplacement(
319 const SearchTermsData& search_terms_data) const {
320 ParseIfNecessary(search_terms_data);
321 return valid_ && supports_replacements_;
324 std::string TemplateURLRef::ReplaceSearchTerms(
325 const SearchTermsArgs& search_terms_args,
326 const SearchTermsData& search_terms_data,
327 PostContent* post_content) const {
328 ParseIfNecessary(search_terms_data);
329 if (!valid_)
330 return std::string();
332 std::string url(HandleReplacements(search_terms_args, search_terms_data,
333 post_content));
335 GURL gurl(url);
336 if (!gurl.is_valid())
337 return url;
339 std::vector<std::string> query_params;
340 if (search_terms_args.append_extra_query_params) {
341 std::string extra_params(
342 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
343 switches::kExtraSearchQueryParams));
344 if (!extra_params.empty())
345 query_params.push_back(extra_params);
347 if (!search_terms_args.suggest_query_params.empty())
348 query_params.push_back(search_terms_args.suggest_query_params);
349 if (!gurl.query().empty())
350 query_params.push_back(gurl.query());
352 if (query_params.empty())
353 return url;
355 GURL::Replacements replacements;
356 std::string query_str = JoinString(query_params, "&");
357 replacements.SetQueryStr(query_str);
358 return gurl.ReplaceComponents(replacements).possibly_invalid_spec();
361 bool TemplateURLRef::IsValid(const SearchTermsData& search_terms_data) const {
362 ParseIfNecessary(search_terms_data);
363 return valid_;
366 base::string16 TemplateURLRef::DisplayURL(
367 const SearchTermsData& search_terms_data) const {
368 ParseIfNecessary(search_terms_data);
369 base::string16 result(base::UTF8ToUTF16(GetURL()));
370 if (valid_ && !replacements_.empty()) {
371 ReplaceSubstringsAfterOffset(&result, 0,
372 base::ASCIIToUTF16(kSearchTermsParameterFull),
373 base::ASCIIToUTF16(kDisplaySearchTerms));
374 ReplaceSubstringsAfterOffset(&result, 0,
375 base::ASCIIToUTF16(kGoogleUnescapedSearchTermsParameterFull),
376 base::ASCIIToUTF16(kDisplayUnescapedSearchTerms));
378 return result;
381 // static
382 std::string TemplateURLRef::DisplayURLToURLRef(
383 const base::string16& display_url) {
384 base::string16 result = display_url;
385 ReplaceSubstringsAfterOffset(&result, 0,
386 base::ASCIIToUTF16(kDisplaySearchTerms),
387 base::ASCIIToUTF16(kSearchTermsParameterFull));
388 ReplaceSubstringsAfterOffset(
389 &result, 0,
390 base::ASCIIToUTF16(kDisplayUnescapedSearchTerms),
391 base::ASCIIToUTF16(kGoogleUnescapedSearchTermsParameterFull));
392 return base::UTF16ToUTF8(result);
395 const std::string& TemplateURLRef::GetHost(
396 const SearchTermsData& search_terms_data) const {
397 ParseIfNecessary(search_terms_data);
398 return host_;
401 const std::string& TemplateURLRef::GetPath(
402 const SearchTermsData& search_terms_data) const {
403 ParseIfNecessary(search_terms_data);
404 return path_;
407 const std::string& TemplateURLRef::GetSearchTermKey(
408 const SearchTermsData& search_terms_data) const {
409 ParseIfNecessary(search_terms_data);
410 return search_term_key_;
413 base::string16 TemplateURLRef::SearchTermToString16(
414 const std::string& term) const {
415 const std::vector<std::string>& encodings = owner_->input_encodings();
416 base::string16 result;
418 std::string unescaped = net::UnescapeURLComponent(
419 term,
420 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE |
421 net::UnescapeRule::URL_SPECIAL_CHARS);
422 for (size_t i = 0; i < encodings.size(); ++i) {
423 if (base::CodepageToUTF16(unescaped, encodings[i].c_str(),
424 base::OnStringConversionError::FAIL, &result))
425 return result;
428 // Always fall back on UTF-8 if it works.
429 if (base::CodepageToUTF16(unescaped, base::kCodepageUTF8,
430 base::OnStringConversionError::FAIL, &result))
431 return result;
433 // When nothing worked, just use the escaped text. We have no idea what the
434 // encoding is. We need to substitute spaces for pluses ourselves since we're
435 // not sending it through an unescaper.
436 result = base::UTF8ToUTF16(term);
437 std::replace(result.begin(), result.end(), '+', ' ');
438 return result;
441 bool TemplateURLRef::HasGoogleBaseURLs(
442 const SearchTermsData& search_terms_data) const {
443 ParseIfNecessary(search_terms_data);
444 for (size_t i = 0; i < replacements_.size(); ++i) {
445 if ((replacements_[i].type == GOOGLE_BASE_URL) ||
446 (replacements_[i].type == GOOGLE_BASE_SUGGEST_URL))
447 return true;
449 return false;
452 bool TemplateURLRef::ExtractSearchTermsFromURL(
453 const GURL& url,
454 base::string16* search_terms,
455 const SearchTermsData& search_terms_data,
456 url::Parsed::ComponentType* search_terms_component,
457 url::Component* search_terms_position) const {
458 DCHECK(search_terms);
459 search_terms->clear();
461 ParseIfNecessary(search_terms_data);
463 // We need a search term in the template URL to extract something.
464 if (search_term_key_.empty())
465 return false;
467 // TODO(beaudoin): Support patterns of the form http://foo/{searchTerms}/
468 // See crbug.com/153798
470 // Fill-in the replacements. We don't care about search terms in the pattern,
471 // so we use the empty string.
472 // Currently we assume the search term only shows in URL, not in post params.
473 GURL pattern(ReplaceSearchTerms(SearchTermsArgs(base::string16()),
474 search_terms_data, NULL));
475 // Host, path and port must match.
476 if (url.port() != pattern.port() ||
477 url.host() != host_ ||
478 url.path() != path_) {
479 return false;
482 // Parameter must be present either in the query or the ref.
483 const std::string& params(
484 (search_term_key_location_ == url::Parsed::QUERY) ?
485 url.query() : url.ref());
487 url::Component query, key, value;
488 query.len = static_cast<int>(params.size());
489 bool key_found = false;
490 while (url::ExtractQueryKeyValue(params.c_str(), &query, &key, &value)) {
491 if (key.is_nonempty()) {
492 if (params.substr(key.begin, key.len) == search_term_key_) {
493 // Fail if search term key is found twice.
494 if (key_found) {
495 search_terms->clear();
496 return false;
498 key_found = true;
499 // Extract the search term.
500 *search_terms = net::UnescapeAndDecodeUTF8URLComponent(
501 params.substr(value.begin, value.len),
502 net::UnescapeRule::SPACES |
503 net::UnescapeRule::URL_SPECIAL_CHARS |
504 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE);
505 if (search_terms_component)
506 *search_terms_component = search_term_key_location_;
507 if (search_terms_position)
508 *search_terms_position = value;
512 return key_found;
515 void TemplateURLRef::InvalidateCachedValues() const {
516 supports_replacements_ = valid_ = parsed_ = false;
517 host_.clear();
518 path_.clear();
519 search_term_key_.clear();
520 replacements_.clear();
521 post_params_.clear();
524 bool TemplateURLRef::ParseParameter(size_t start,
525 size_t end,
526 std::string* url,
527 Replacements* replacements) const {
528 DCHECK(start != std::string::npos &&
529 end != std::string::npos && end > start);
530 size_t length = end - start - 1;
531 bool optional = false;
532 if ((*url)[end - 1] == kOptional) {
533 optional = true;
534 length--;
536 std::string parameter(url->substr(start + 1, length));
537 std::string full_parameter(url->substr(start, end - start + 1));
538 // Remove the parameter from the string. For parameters who replacement is
539 // constant and already known, just replace them directly. For other cases,
540 // like parameters whose values may change over time, use |replacements|.
541 url->erase(start, end - start + 1);
542 if (parameter == kSearchTermsParameter) {
543 replacements->push_back(Replacement(SEARCH_TERMS, start));
544 } else if (parameter == kCountParameter) {
545 if (!optional)
546 url->insert(start, kDefaultCount);
547 } else if (parameter == kGoogleAssistedQueryStatsParameter) {
548 replacements->push_back(Replacement(GOOGLE_ASSISTED_QUERY_STATS, start));
549 } else if (parameter == kGoogleBaseURLParameter) {
550 replacements->push_back(Replacement(GOOGLE_BASE_URL, start));
551 } else if (parameter == kGoogleBaseSuggestURLParameter) {
552 replacements->push_back(Replacement(GOOGLE_BASE_SUGGEST_URL, start));
553 } else if (parameter == kGoogleBookmarkBarPinnedParameter) {
554 replacements->push_back(Replacement(GOOGLE_BOOKMARK_BAR_PINNED, start));
555 } else if (parameter == kGoogleCurrentPageUrlParameter) {
556 replacements->push_back(Replacement(GOOGLE_CURRENT_PAGE_URL, start));
557 } else if (parameter == kGoogleCursorPositionParameter) {
558 replacements->push_back(Replacement(GOOGLE_CURSOR_POSITION, start));
559 } else if (parameter == kGoogleForceInstantResultsParameter) {
560 replacements->push_back(Replacement(GOOGLE_FORCE_INSTANT_RESULTS, start));
561 } else if (parameter == kGoogleImageOriginalHeight) {
562 replacements->push_back(
563 Replacement(TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_HEIGHT, start));
564 } else if (parameter == kGoogleImageOriginalWidth) {
565 replacements->push_back(
566 Replacement(TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH, start));
567 } else if (parameter == kGoogleImageSearchSource) {
568 replacements->push_back(
569 Replacement(TemplateURLRef::GOOGLE_IMAGE_SEARCH_SOURCE, start));
570 } else if (parameter == kGoogleImageThumbnailParameter) {
571 replacements->push_back(
572 Replacement(TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL, start));
573 } else if (parameter == kGoogleImageURLParameter) {
574 replacements->push_back(Replacement(TemplateURLRef::GOOGLE_IMAGE_URL,
575 start));
576 } else if (parameter == kGoogleInputTypeParameter) {
577 replacements->push_back(Replacement(TemplateURLRef::GOOGLE_INPUT_TYPE,
578 start));
579 } else if (parameter == kGoogleInstantExtendedEnabledParameter) {
580 replacements->push_back(Replacement(GOOGLE_INSTANT_EXTENDED_ENABLED,
581 start));
582 } else if (parameter == kGoogleInstantExtendedEnabledKey) {
583 url->insert(start, google_util::kInstantExtendedAPIParam);
584 } else if (parameter == kGoogleNTPIsThemedParameter) {
585 replacements->push_back(Replacement(GOOGLE_NTP_IS_THEMED, start));
586 } else if (parameter == kGoogleOmniboxStartMarginParameter) {
587 replacements->push_back(Replacement(GOOGLE_OMNIBOX_START_MARGIN, start));
588 } else if (parameter == kGoogleContextualSearchVersion) {
589 replacements->push_back(
590 Replacement(GOOGLE_CONTEXTUAL_SEARCH_VERSION, start));
591 } else if (parameter == kGoogleContextualSearchContextData) {
592 replacements->push_back(
593 Replacement(GOOGLE_CONTEXTUAL_SEARCH_CONTEXT_DATA, start));
594 } else if (parameter == kGoogleOriginalQueryForSuggestionParameter) {
595 replacements->push_back(Replacement(GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION,
596 start));
597 } else if (parameter == kGooglePageClassificationParameter) {
598 replacements->push_back(Replacement(GOOGLE_PAGE_CLASSIFICATION, start));
599 } else if (parameter == kGooglePrefetchQuery) {
600 replacements->push_back(Replacement(GOOGLE_PREFETCH_QUERY, start));
601 } else if (parameter == kGoogleRLZParameter) {
602 replacements->push_back(Replacement(GOOGLE_RLZ, start));
603 } else if (parameter == kGoogleSearchClient) {
604 replacements->push_back(Replacement(GOOGLE_SEARCH_CLIENT, start));
605 } else if (parameter == kGoogleSearchFieldtrialParameter) {
606 replacements->push_back(Replacement(GOOGLE_SEARCH_FIELDTRIAL_GROUP, start));
607 } else if (parameter == kGoogleSearchVersion) {
608 replacements->push_back(Replacement(GOOGLE_SEARCH_VERSION, start));
609 } else if (parameter == kGoogleSessionToken) {
610 replacements->push_back(Replacement(GOOGLE_SESSION_TOKEN, start));
611 } else if (parameter == kGoogleSourceIdParameter) {
612 #if defined(OS_ANDROID)
613 url->insert(start, "sourceid=chrome-mobile&");
614 #else
615 url->insert(start, "sourceid=chrome&");
616 #endif
617 } else if (parameter == kGoogleSuggestAPIKeyParameter) {
618 url->insert(start,
619 net::EscapeQueryParamValue(google_apis::GetAPIKey(), false));
620 } else if (parameter == kGoogleSuggestClient) {
621 replacements->push_back(Replacement(GOOGLE_SUGGEST_CLIENT, start));
622 } else if (parameter == kGoogleSuggestRequestId) {
623 replacements->push_back(Replacement(GOOGLE_SUGGEST_REQUEST_ID, start));
624 } else if (parameter == kGoogleUnescapedSearchTermsParameter) {
625 replacements->push_back(Replacement(GOOGLE_UNESCAPED_SEARCH_TERMS, start));
626 } else if (parameter == kInputEncodingParameter) {
627 replacements->push_back(Replacement(ENCODING, start));
628 } else if (parameter == kLanguageParameter) {
629 replacements->push_back(Replacement(LANGUAGE, start));
630 } else if (parameter == kOutputEncodingParameter) {
631 if (!optional)
632 url->insert(start, kOutputEncodingType);
633 } else if ((parameter == kStartIndexParameter) ||
634 (parameter == kStartPageParameter)) {
635 // We don't support these.
636 if (!optional)
637 url->insert(start, "1");
638 } else if (!prepopulated_) {
639 // If it's a prepopulated URL, we know that it's safe to remove unknown
640 // parameters, so just ignore this and return true below. Otherwise it could
641 // be some garbage but can also be a javascript block. Put it back.
642 url->insert(start, full_parameter);
643 return false;
645 return true;
648 std::string TemplateURLRef::ParseURL(const std::string& url,
649 Replacements* replacements,
650 PostParams* post_params,
651 bool* valid) const {
652 *valid = false;
653 std::string parsed_url = url;
654 for (size_t last = 0; last != std::string::npos; ) {
655 last = parsed_url.find(kStartParameter, last);
656 if (last != std::string::npos) {
657 size_t template_end = parsed_url.find(kEndParameter, last);
658 if (template_end != std::string::npos) {
659 // Since we allow Javascript in the URL, {} pairs could be nested. Match
660 // only leaf pairs with supported parameters.
661 size_t next_template_start = parsed_url.find(kStartParameter, last + 1);
662 if (next_template_start == std::string::npos ||
663 next_template_start > template_end) {
664 // If successful, ParseParameter erases from the string as such no
665 // need to update |last|. If failed, move |last| to the end of pair.
666 if (!ParseParameter(last, template_end, &parsed_url, replacements)) {
667 // |template_end| + 1 may be beyond the end of the string.
668 last = template_end;
670 } else {
671 last = next_template_start;
673 } else {
674 // Open brace without a closing brace, return.
675 return std::string();
680 // Handles the post parameters.
681 const std::string& post_params_string = GetPostParamsString();
682 if (!post_params_string.empty()) {
683 typedef std::vector<std::string> Strings;
684 Strings param_list;
685 base::SplitString(post_params_string, ',', &param_list);
687 for (Strings::const_iterator iterator = param_list.begin();
688 iterator != param_list.end(); ++iterator) {
689 Strings parts;
690 // The '=' delimiter is required and the name must be not empty.
691 base::SplitString(*iterator, '=', &parts);
692 if ((parts.size() != 2U) || parts[0].empty())
693 return std::string();
695 std::string& value = parts[1];
696 size_t replacements_size = replacements->size();
697 if (IsTemplateParameterString(value))
698 ParseParameter(0, value.length() - 1, &value, replacements);
699 post_params->push_back(std::make_pair(parts[0], value));
700 // If there was a replacement added, points its index to last added
701 // PostParam.
702 if (replacements->size() > replacements_size) {
703 DCHECK_EQ(replacements_size + 1, replacements->size());
704 Replacement* r = &replacements->back();
705 r->is_post_param = true;
706 r->index = post_params->size() - 1;
709 DCHECK(!post_params->empty());
712 *valid = true;
713 return parsed_url;
716 void TemplateURLRef::ParseIfNecessary(
717 const SearchTermsData& search_terms_data) const {
718 if (!parsed_) {
719 InvalidateCachedValues();
720 parsed_ = true;
721 parsed_url_ = ParseURL(GetURL(), &replacements_, &post_params_, &valid_);
722 supports_replacements_ = false;
723 if (valid_) {
724 bool has_only_one_search_term = false;
725 for (Replacements::const_iterator i = replacements_.begin();
726 i != replacements_.end(); ++i) {
727 if ((i->type == SEARCH_TERMS) ||
728 (i->type == GOOGLE_UNESCAPED_SEARCH_TERMS)) {
729 if (has_only_one_search_term) {
730 has_only_one_search_term = false;
731 break;
733 has_only_one_search_term = true;
734 supports_replacements_ = true;
737 // Only parse the host/key if there is one search term. Technically there
738 // could be more than one term, but it's uncommon; so we punt.
739 if (has_only_one_search_term)
740 ParseHostAndSearchTermKey(search_terms_data);
745 void TemplateURLRef::ParseHostAndSearchTermKey(
746 const SearchTermsData& search_terms_data) const {
747 std::string url_string(GetURL());
748 ReplaceSubstringsAfterOffset(&url_string, 0,
749 kGoogleBaseURLParameterFull,
750 search_terms_data.GoogleBaseURLValue());
751 ReplaceSubstringsAfterOffset(&url_string, 0,
752 kGoogleBaseSuggestURLParameterFull,
753 search_terms_data.GoogleBaseSuggestURLValue());
755 search_term_key_.clear();
756 host_.clear();
757 path_.clear();
758 search_term_key_location_ = url::Parsed::REF;
760 GURL url(url_string);
761 if (!url.is_valid())
762 return;
764 std::string query_key = FindSearchTermsKey(url.query());
765 std::string ref_key = FindSearchTermsKey(url.ref());
766 if (query_key.empty() == ref_key.empty())
767 return; // No key or multiple keys found. We only handle having one key.
768 search_term_key_ = query_key.empty() ? ref_key : query_key;
769 search_term_key_location_ =
770 query_key.empty() ? url::Parsed::REF : url::Parsed::QUERY;
771 host_ = url.host();
772 path_ = url.path();
775 void TemplateURLRef::HandleReplacement(const std::string& name,
776 const std::string& value,
777 const Replacement& replacement,
778 std::string* url) const {
779 size_t pos = replacement.index;
780 if (replacement.is_post_param) {
781 DCHECK_LT(pos, post_params_.size());
782 DCHECK(!post_params_[pos].first.empty());
783 post_params_[pos].second = value;
784 } else {
785 url->insert(pos, name.empty() ? value : (name + "=" + value + "&"));
789 std::string TemplateURLRef::HandleReplacements(
790 const SearchTermsArgs& search_terms_args,
791 const SearchTermsData& search_terms_data,
792 PostContent* post_content) const {
793 if (replacements_.empty()) {
794 if (!post_params_.empty())
795 EncodeFormData(post_params_, post_content);
796 return parsed_url_;
799 // Determine if the search terms are in the query or before. We're escaping
800 // space as '+' in the former case and as '%20' in the latter case.
801 bool is_in_query = true;
802 for (Replacements::iterator i = replacements_.begin();
803 i != replacements_.end(); ++i) {
804 if (i->type == SEARCH_TERMS) {
805 base::string16::size_type query_start = parsed_url_.find('?');
806 is_in_query = query_start != base::string16::npos &&
807 (static_cast<base::string16::size_type>(i->index) > query_start);
808 break;
812 std::string input_encoding;
813 base::string16 encoded_terms;
814 base::string16 encoded_original_query;
815 owner_->EncodeSearchTerms(search_terms_args, is_in_query, &input_encoding,
816 &encoded_terms, &encoded_original_query);
818 std::string url = parsed_url_;
820 // replacements_ is ordered in ascending order, as such we need to iterate
821 // from the back.
822 for (Replacements::reverse_iterator i = replacements_.rbegin();
823 i != replacements_.rend(); ++i) {
824 switch (i->type) {
825 case ENCODING:
826 HandleReplacement(std::string(), input_encoding, *i, &url);
827 break;
829 case GOOGLE_ASSISTED_QUERY_STATS:
830 DCHECK(!i->is_post_param);
831 if (!search_terms_args.assisted_query_stats.empty()) {
832 // Get the base URL without substituting AQS to avoid infinite
833 // recursion. We need the URL to find out if it meets all
834 // AQS requirements (e.g. HTTPS protocol check).
835 // See TemplateURLRef::SearchTermsArgs for more details.
836 SearchTermsArgs search_terms_args_without_aqs(search_terms_args);
837 search_terms_args_without_aqs.assisted_query_stats.clear();
838 GURL base_url(ReplaceSearchTerms(
839 search_terms_args_without_aqs, search_terms_data, NULL));
840 if (base_url.SchemeIs(url::kHttpsScheme)) {
841 HandleReplacement(
842 "aqs", search_terms_args.assisted_query_stats, *i, &url);
845 break;
847 case GOOGLE_BASE_URL:
848 DCHECK(!i->is_post_param);
849 HandleReplacement(
850 std::string(), search_terms_data.GoogleBaseURLValue(), *i, &url);
851 break;
853 case GOOGLE_BASE_SUGGEST_URL:
854 DCHECK(!i->is_post_param);
855 HandleReplacement(
856 std::string(), search_terms_data.GoogleBaseSuggestURLValue(), *i,
857 &url);
858 break;
860 case GOOGLE_BOOKMARK_BAR_PINNED:
861 if (search_terms_data.IsShowingSearchTermsOnSearchResultsPages()) {
862 // Log whether the bookmark bar is pinned when the user is seeing
863 // InstantExtended on the SRP.
864 DCHECK(!i->is_post_param);
865 HandleReplacement(
866 "bmbp", search_terms_args.bookmark_bar_pinned ? "1" : "0", *i,
867 &url);
869 break;
871 case GOOGLE_CURRENT_PAGE_URL:
872 DCHECK(!i->is_post_param);
873 if (!search_terms_args.current_page_url.empty()) {
874 const std::string& escaped_current_page_url =
875 net::EscapeQueryParamValue(search_terms_args.current_page_url,
876 true);
877 HandleReplacement("url", escaped_current_page_url, *i, &url);
879 break;
881 case GOOGLE_CURSOR_POSITION:
882 DCHECK(!i->is_post_param);
883 if (search_terms_args.cursor_position != base::string16::npos)
884 HandleReplacement(
885 "cp",
886 base::StringPrintf("%" PRIuS, search_terms_args.cursor_position),
888 &url);
889 break;
891 case GOOGLE_FORCE_INSTANT_RESULTS:
892 DCHECK(!i->is_post_param);
893 HandleReplacement(std::string(),
894 search_terms_data.ForceInstantResultsParam(
895 search_terms_args.force_instant_results),
897 &url);
898 break;
900 case GOOGLE_INPUT_TYPE:
901 DCHECK(!i->is_post_param);
902 HandleReplacement(
903 "oit", base::IntToString(search_terms_args.input_type), *i, &url);
904 break;
906 case GOOGLE_INSTANT_EXTENDED_ENABLED:
907 DCHECK(!i->is_post_param);
908 HandleReplacement(std::string(),
909 search_terms_data.InstantExtendedEnabledParam(
910 type_ == SEARCH),
912 &url);
913 break;
915 case GOOGLE_NTP_IS_THEMED:
916 DCHECK(!i->is_post_param);
917 HandleReplacement(
918 std::string(), search_terms_data.NTPIsThemedParam(), *i, &url);
919 break;
921 case GOOGLE_OMNIBOX_START_MARGIN:
922 DCHECK(!i->is_post_param);
923 if (search_terms_args.omnibox_start_margin >= 0) {
924 HandleReplacement(
925 "es_sm",
926 base::IntToString(search_terms_args.omnibox_start_margin),
928 &url);
930 break;
932 case GOOGLE_CONTEXTUAL_SEARCH_VERSION:
933 if (search_terms_args.contextual_search_params.version >= 0) {
934 HandleReplacement(
935 "ctxs",
936 base::IntToString(
937 search_terms_args.contextual_search_params.version),
939 &url);
941 break;
943 case GOOGLE_CONTEXTUAL_SEARCH_CONTEXT_DATA: {
944 DCHECK(!i->is_post_param);
945 std::string context_data;
947 const SearchTermsArgs::ContextualSearchParams& params =
948 search_terms_args.contextual_search_params;
950 if (params.start != std::string::npos) {
951 context_data.append("ctxs_start=" + base::IntToString(
952 params.start) + "&");
955 if (params.end != std::string::npos) {
956 context_data.append("ctxs_end=" + base::IntToString(
957 params.end) + "&");
960 if (!params.selection.empty())
961 context_data.append("q=" + params.selection + "&");
963 if (!params.content.empty())
964 context_data.append("ctxs_content=" + params.content + "&");
966 if (!params.base_page_url.empty())
967 context_data.append("ctxs_url=" + params.base_page_url + "&");
969 if (!params.encoding.empty()) {
970 context_data.append("ctxs_encoding=" + params.encoding + "&");
973 HandleReplacement(std::string(), context_data, *i, &url);
974 break;
977 case GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION:
978 DCHECK(!i->is_post_param);
979 if (search_terms_args.accepted_suggestion >= 0 ||
980 !search_terms_args.assisted_query_stats.empty()) {
981 HandleReplacement(
982 "oq", base::UTF16ToUTF8(encoded_original_query), *i, &url);
984 break;
986 case GOOGLE_PAGE_CLASSIFICATION:
987 if (search_terms_args.page_classification !=
988 metrics::OmniboxEventProto::INVALID_SPEC) {
989 HandleReplacement(
990 "pgcl", base::IntToString(search_terms_args.page_classification),
991 *i, &url);
993 break;
995 case GOOGLE_PREFETCH_QUERY: {
996 const std::string& query = search_terms_args.prefetch_query;
997 const std::string& type = search_terms_args.prefetch_query_type;
998 if (!query.empty() && !type.empty()) {
999 HandleReplacement(
1000 std::string(), "pfq=" + query + "&qha=" + type + "&", *i, &url);
1002 break;
1005 case GOOGLE_RLZ: {
1006 DCHECK(!i->is_post_param);
1007 // On platforms that don't have RLZ, we still want this branch
1008 // to happen so that we replace the RLZ template with the
1009 // empty string. (If we don't handle this case, we hit a
1010 // NOTREACHED below.)
1011 base::string16 rlz_string = search_terms_data.GetRlzParameterValue(
1012 search_terms_args.from_app_list);
1013 if (!rlz_string.empty()) {
1014 HandleReplacement("rlz", base::UTF16ToUTF8(rlz_string), *i, &url);
1016 break;
1019 case GOOGLE_SEARCH_CLIENT: {
1020 DCHECK(!i->is_post_param);
1021 std::string client = search_terms_data.GetSearchClient();
1022 if (!client.empty())
1023 HandleReplacement("client", client, *i, &url);
1024 break;
1027 case GOOGLE_SEARCH_FIELDTRIAL_GROUP:
1028 // We are not currently running any fieldtrials that modulate the search
1029 // url. If we do, then we'd have some conditional insert such as:
1030 // url.insert(i->index, used_www ? "gcx=w&" : "gcx=c&");
1031 break;
1033 case GOOGLE_SEARCH_VERSION:
1034 if (search_terms_data.EnableAnswersInSuggest())
1035 HandleReplacement("gs_rn", "42", *i, &url);
1036 break;
1038 case GOOGLE_SESSION_TOKEN: {
1039 std::string token = search_terms_args.session_token;
1040 if (!token.empty())
1041 HandleReplacement("psi", token, *i, &url);
1042 break;
1045 case GOOGLE_SUGGEST_CLIENT:
1046 HandleReplacement(
1047 std::string(), search_terms_data.GetSuggestClient(), *i, &url);
1048 break;
1050 case GOOGLE_SUGGEST_REQUEST_ID:
1051 HandleReplacement(
1052 std::string(), search_terms_data.GetSuggestRequestIdentifier(), *i,
1053 &url);
1054 break;
1056 case GOOGLE_UNESCAPED_SEARCH_TERMS: {
1057 std::string unescaped_terms;
1058 base::UTF16ToCodepage(search_terms_args.search_terms,
1059 input_encoding.c_str(),
1060 base::OnStringConversionError::SKIP,
1061 &unescaped_terms);
1062 HandleReplacement(std::string(), unescaped_terms, *i, &url);
1063 break;
1066 case LANGUAGE:
1067 HandleReplacement(
1068 std::string(), search_terms_data.GetApplicationLocale(), *i, &url);
1069 break;
1071 case SEARCH_TERMS:
1072 HandleReplacement(
1073 std::string(), base::UTF16ToUTF8(encoded_terms), *i, &url);
1074 break;
1076 case GOOGLE_IMAGE_THUMBNAIL:
1077 HandleReplacement(
1078 std::string(), search_terms_args.image_thumbnail_content, *i, &url);
1079 break;
1081 case GOOGLE_IMAGE_URL:
1082 if (search_terms_args.image_url.is_valid()) {
1083 HandleReplacement(
1084 std::string(), search_terms_args.image_url.spec(), *i, &url);
1086 break;
1088 case GOOGLE_IMAGE_ORIGINAL_WIDTH:
1089 if (!search_terms_args.image_original_size.IsEmpty()) {
1090 HandleReplacement(
1091 std::string(),
1092 base::IntToString(search_terms_args.image_original_size.width()),
1093 *i, &url);
1095 break;
1097 case GOOGLE_IMAGE_ORIGINAL_HEIGHT:
1098 if (!search_terms_args.image_original_size.IsEmpty()) {
1099 HandleReplacement(
1100 std::string(),
1101 base::IntToString(search_terms_args.image_original_size.height()),
1102 *i, &url);
1104 break;
1106 case GOOGLE_IMAGE_SEARCH_SOURCE:
1107 HandleReplacement(
1108 std::string(), search_terms_data.GoogleImageSearchSource(), *i,
1109 &url);
1110 break;
1112 default:
1113 NOTREACHED();
1114 break;
1118 if (!post_params_.empty())
1119 EncodeFormData(post_params_, post_content);
1121 return url;
1125 // TemplateURL ----------------------------------------------------------------
1127 TemplateURL::AssociatedExtensionInfo::AssociatedExtensionInfo(
1128 Type type,
1129 const std::string& extension_id)
1130 : type(type),
1131 extension_id(extension_id),
1132 wants_to_be_default_engine(false) {
1133 DCHECK_NE(NORMAL, type);
1136 TemplateURL::AssociatedExtensionInfo::~AssociatedExtensionInfo() {
1139 TemplateURL::TemplateURL(const TemplateURLData& data)
1140 : data_(data),
1141 url_ref_(this, TemplateURLRef::SEARCH),
1142 suggestions_url_ref_(this,
1143 TemplateURLRef::SUGGEST),
1144 instant_url_ref_(this,
1145 TemplateURLRef::INSTANT),
1146 image_url_ref_(this, TemplateURLRef::IMAGE),
1147 new_tab_url_ref_(this, TemplateURLRef::NEW_TAB),
1148 contextual_search_url_ref_(this, TemplateURLRef::CONTEXTUAL_SEARCH) {
1149 SetPrepopulateId(data_.prepopulate_id);
1151 if (data_.search_terms_replacement_key ==
1152 kGoogleInstantExtendedEnabledKeyFull) {
1153 data_.search_terms_replacement_key = google_util::kInstantExtendedAPIParam;
1157 TemplateURL::~TemplateURL() {
1160 // static
1161 base::string16 TemplateURL::GenerateKeyword(const GURL& url) {
1162 DCHECK(url.is_valid());
1163 // Strip "www." off the front of the keyword; otherwise the keyword won't work
1164 // properly. See http://code.google.com/p/chromium/issues/detail?id=6984 .
1165 // Special case: if the host was exactly "www." (not sure this can happen but
1166 // perhaps with some weird intranet and custom DNS server?), ensure we at
1167 // least don't return the empty string.
1168 base::string16 keyword(net::StripWWWFromHost(url));
1169 return keyword.empty() ? base::ASCIIToUTF16("www") : keyword;
1172 // static
1173 GURL TemplateURL::GenerateFaviconURL(const GURL& url) {
1174 DCHECK(url.is_valid());
1175 GURL::Replacements rep;
1177 const char favicon_path[] = "/favicon.ico";
1178 int favicon_path_len = arraysize(favicon_path) - 1;
1180 rep.SetPath(favicon_path, url::Component(0, favicon_path_len));
1181 rep.ClearUsername();
1182 rep.ClearPassword();
1183 rep.ClearQuery();
1184 rep.ClearRef();
1185 return url.ReplaceComponents(rep);
1188 // static
1189 bool TemplateURL::MatchesData(const TemplateURL* t_url,
1190 const TemplateURLData* data,
1191 const SearchTermsData& search_terms_data) {
1192 if (!t_url || !data)
1193 return !t_url && !data;
1195 return (t_url->short_name() == data->short_name) &&
1196 t_url->HasSameKeywordAs(*data, search_terms_data) &&
1197 (t_url->url() == data->url()) &&
1198 (t_url->suggestions_url() == data->suggestions_url) &&
1199 (t_url->instant_url() == data->instant_url) &&
1200 (t_url->image_url() == data->image_url) &&
1201 (t_url->new_tab_url() == data->new_tab_url) &&
1202 (t_url->search_url_post_params() == data->search_url_post_params) &&
1203 (t_url->suggestions_url_post_params() ==
1204 data->suggestions_url_post_params) &&
1205 (t_url->instant_url_post_params() == data->instant_url_post_params) &&
1206 (t_url->image_url_post_params() == data->image_url_post_params) &&
1207 (t_url->favicon_url() == data->favicon_url) &&
1208 (t_url->safe_for_autoreplace() == data->safe_for_autoreplace) &&
1209 (t_url->show_in_default_list() == data->show_in_default_list) &&
1210 (t_url->input_encodings() == data->input_encodings) &&
1211 (t_url->alternate_urls() == data->alternate_urls) &&
1212 (t_url->search_terms_replacement_key() ==
1213 data->search_terms_replacement_key);
1216 base::string16 TemplateURL::AdjustedShortNameForLocaleDirection() const {
1217 base::string16 bidi_safe_short_name = data_.short_name;
1218 base::i18n::AdjustStringForLocaleDirection(&bidi_safe_short_name);
1219 return bidi_safe_short_name;
1222 bool TemplateURL::ShowInDefaultList(
1223 const SearchTermsData& search_terms_data) const {
1224 return data_.show_in_default_list &&
1225 url_ref_.SupportsReplacement(search_terms_data);
1228 bool TemplateURL::SupportsReplacement(
1229 const SearchTermsData& search_terms_data) const {
1230 return url_ref_.SupportsReplacement(search_terms_data);
1233 bool TemplateURL::HasGoogleBaseURLs(
1234 const SearchTermsData& search_terms_data) const {
1235 return url_ref_.HasGoogleBaseURLs(search_terms_data) ||
1236 suggestions_url_ref_.HasGoogleBaseURLs(search_terms_data) ||
1237 instant_url_ref_.HasGoogleBaseURLs(search_terms_data) ||
1238 image_url_ref_.HasGoogleBaseURLs(search_terms_data) ||
1239 new_tab_url_ref_.HasGoogleBaseURLs(search_terms_data);
1242 bool TemplateURL::IsGoogleSearchURLWithReplaceableKeyword(
1243 const SearchTermsData& search_terms_data) const {
1244 return (GetType() == NORMAL) &&
1245 url_ref_.HasGoogleBaseURLs(search_terms_data) &&
1246 google_util::IsGoogleHostname(base::UTF16ToUTF8(data_.keyword()),
1247 google_util::DISALLOW_SUBDOMAIN);
1250 bool TemplateURL::HasSameKeywordAs(
1251 const TemplateURLData& other,
1252 const SearchTermsData& search_terms_data) const {
1253 return (data_.keyword() == other.keyword()) ||
1254 (IsGoogleSearchURLWithReplaceableKeyword(search_terms_data) &&
1255 TemplateURL(other).IsGoogleSearchURLWithReplaceableKeyword(
1256 search_terms_data));
1259 TemplateURL::Type TemplateURL::GetType() const {
1260 return extension_info_ ? extension_info_->type : NORMAL;
1263 std::string TemplateURL::GetExtensionId() const {
1264 DCHECK(extension_info_);
1265 return extension_info_->extension_id;
1268 size_t TemplateURL::URLCount() const {
1269 // Add 1 for the regular search URL.
1270 return data_.alternate_urls.size() + 1;
1273 const std::string& TemplateURL::GetURL(size_t index) const {
1274 DCHECK_LT(index, URLCount());
1276 return (index < data_.alternate_urls.size()) ?
1277 data_.alternate_urls[index] : url();
1280 bool TemplateURL::ExtractSearchTermsFromURL(
1281 const GURL& url,
1282 const SearchTermsData& search_terms_data,
1283 base::string16* search_terms) {
1284 return FindSearchTermsInURL(url, search_terms_data, search_terms, NULL, NULL);
1287 bool TemplateURL::IsSearchURL(
1288 const GURL& url,
1289 const SearchTermsData& search_terms_data) {
1290 base::string16 search_terms;
1291 return ExtractSearchTermsFromURL(url, search_terms_data, &search_terms) &&
1292 !search_terms.empty();
1295 bool TemplateURL::HasSearchTermsReplacementKey(const GURL& url) const {
1296 // Look for the key both in the query and the ref.
1297 std::string params[] = {url.query(), url.ref()};
1299 for (int i = 0; i < 2; ++i) {
1300 url::Component query, key, value;
1301 query.len = static_cast<int>(params[i].size());
1302 while (url::ExtractQueryKeyValue(params[i].c_str(), &query, &key, &value)) {
1303 if (key.is_nonempty() &&
1304 params[i].substr(key.begin, key.len) ==
1305 search_terms_replacement_key()) {
1306 return true;
1310 return false;
1313 bool TemplateURL::ReplaceSearchTermsInURL(
1314 const GURL& url,
1315 const TemplateURLRef::SearchTermsArgs& search_terms_args,
1316 const SearchTermsData& search_terms_data,
1317 GURL* result) {
1318 // TODO(beaudoin): Use AQS from |search_terms_args| too.
1319 url::Parsed::ComponentType search_term_component;
1320 url::Component search_terms_position;
1321 base::string16 search_terms;
1322 if (!FindSearchTermsInURL(url, search_terms_data, &search_terms,
1323 &search_term_component, &search_terms_position)) {
1324 return false;
1326 DCHECK(search_terms_position.is_nonempty());
1328 // FindSearchTermsInURL only returns true for search terms in the query or
1329 // ref, so we can call EncodeSearchTerm with |is_in_query| = true, since query
1330 // and ref are encoded in the same way.
1331 std::string input_encoding;
1332 base::string16 encoded_terms;
1333 base::string16 encoded_original_query;
1334 EncodeSearchTerms(search_terms_args, true, &input_encoding,
1335 &encoded_terms, &encoded_original_query);
1337 std::string old_params(
1338 (search_term_component == url::Parsed::REF) ? url.ref() : url.query());
1339 std::string new_params(old_params, 0, search_terms_position.begin);
1340 new_params += base::UTF16ToUTF8(search_terms_args.search_terms);
1341 new_params += old_params.substr(search_terms_position.end());
1342 url::StdStringReplacements<std::string> replacements;
1343 if (search_term_component == url::Parsed::REF)
1344 replacements.SetRefStr(new_params);
1345 else
1346 replacements.SetQueryStr(new_params);
1347 *result = url.ReplaceComponents(replacements);
1348 return true;
1351 void TemplateURL::EncodeSearchTerms(
1352 const TemplateURLRef::SearchTermsArgs& search_terms_args,
1353 bool is_in_query,
1354 std::string* input_encoding,
1355 base::string16* encoded_terms,
1356 base::string16* encoded_original_query) const {
1358 std::vector<std::string> encodings(input_encodings());
1359 if (std::find(encodings.begin(), encodings.end(), "UTF-8") == encodings.end())
1360 encodings.push_back("UTF-8");
1361 for (std::vector<std::string>::const_iterator i(encodings.begin());
1362 i != encodings.end(); ++i) {
1363 if (TryEncoding(search_terms_args.search_terms,
1364 search_terms_args.original_query, i->c_str(),
1365 is_in_query, encoded_terms, encoded_original_query)) {
1366 *input_encoding = *i;
1367 return;
1370 NOTREACHED();
1373 GURL TemplateURL::GenerateSearchURL(
1374 const SearchTermsData& search_terms_data) const {
1375 if (!url_ref_.IsValid(search_terms_data))
1376 return GURL();
1378 if (!url_ref_.SupportsReplacement(search_terms_data))
1379 return GURL(url());
1381 // Use something obscure for the search terms argument so that in the rare
1382 // case the term replaces the URL it's unlikely another keyword would have the
1383 // same url.
1384 // TODO(jnd): Add additional parameters to get post data when the search URL
1385 // has post parameters.
1386 return GURL(url_ref_.ReplaceSearchTerms(
1387 TemplateURLRef::SearchTermsArgs(
1388 base::ASCIIToUTF16("blah.blah.blah.blah.blah")),
1389 search_terms_data, NULL));
1392 void TemplateURL::CopyFrom(const TemplateURL& other) {
1393 if (this == &other)
1394 return;
1396 data_ = other.data_;
1397 url_ref_.InvalidateCachedValues();
1398 suggestions_url_ref_.InvalidateCachedValues();
1399 instant_url_ref_.InvalidateCachedValues();
1400 SetPrepopulateId(other.data_.prepopulate_id);
1403 void TemplateURL::SetURL(const std::string& url) {
1404 data_.SetURL(url);
1405 url_ref_.InvalidateCachedValues();
1408 void TemplateURL::SetPrepopulateId(int id) {
1409 data_.prepopulate_id = id;
1410 const bool prepopulated = id > 0;
1411 url_ref_.prepopulated_ = prepopulated;
1412 suggestions_url_ref_.prepopulated_ = prepopulated;
1413 instant_url_ref_.prepopulated_ = prepopulated;
1416 void TemplateURL::ResetKeywordIfNecessary(
1417 const SearchTermsData& search_terms_data,
1418 bool force) {
1419 if (IsGoogleSearchURLWithReplaceableKeyword(search_terms_data) || force) {
1420 DCHECK(GetType() != OMNIBOX_API_EXTENSION);
1421 GURL url(GenerateSearchURL(search_terms_data));
1422 if (url.is_valid())
1423 data_.SetKeyword(GenerateKeyword(url));
1427 bool TemplateURL::FindSearchTermsInURL(
1428 const GURL& url,
1429 const SearchTermsData& search_terms_data,
1430 base::string16* search_terms,
1431 url::Parsed::ComponentType* search_term_component,
1432 url::Component* search_terms_position) {
1433 DCHECK(search_terms);
1434 search_terms->clear();
1436 // Try to match with every pattern.
1437 for (size_t i = 0; i < URLCount(); ++i) {
1438 TemplateURLRef ref(this, i);
1439 if (ref.ExtractSearchTermsFromURL(url, search_terms, search_terms_data,
1440 search_term_component, search_terms_position)) {
1441 // If ExtractSearchTermsFromURL() returns true and |search_terms| is empty
1442 // it means the pattern matched but no search terms were present. In this
1443 // case we fail immediately without looking for matches in subsequent
1444 // patterns. This means that given patterns
1445 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ],
1446 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would
1447 // return false. This is important for at least Google, where such URLs
1448 // are invalid.
1449 return !search_terms->empty();
1452 return false;