| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/omnibox/browser/keyword_provider.h" | 5 #include "components/omnibox/browser/keyword_provider.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 return match; | 444 return match; |
| 445 } | 445 } |
| 446 | 446 |
| 447 void KeywordProvider::FillInURLAndContents( | 447 void KeywordProvider::FillInURLAndContents( |
| 448 const base::string16& remaining_input, | 448 const base::string16& remaining_input, |
| 449 const TemplateURL* element, | 449 const TemplateURL* element, |
| 450 AutocompleteMatch* match) const { | 450 AutocompleteMatch* match) const { |
| 451 DCHECK(!element->short_name().empty()); | 451 DCHECK(!element->short_name().empty()); |
| 452 const TemplateURLRef& element_ref = element->url_ref(); | 452 const TemplateURLRef& element_ref = element->url_ref(); |
| 453 DCHECK(element_ref.IsValid(GetTemplateURLService()->search_terms_data())); | 453 DCHECK(element_ref.IsValid(GetTemplateURLService()->search_terms_data())); |
| 454 int message_id = (element->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) ? | |
| 455 IDS_EXTENSION_KEYWORD_COMMAND : IDS_KEYWORD_SEARCH; | |
| 456 if (remaining_input.empty()) { | 454 if (remaining_input.empty()) { |
| 457 // Allow extension keyword providers to accept empty string input. This is | 455 // Allow extension keyword providers to accept empty string input. This is |
| 458 // useful to allow extensions to do something in the case where no input is | 456 // useful to allow extensions to do something in the case where no input is |
| 459 // entered. | 457 // entered. |
| 460 if (element_ref.SupportsReplacement( | 458 if (element_ref.SupportsReplacement( |
| 461 GetTemplateURLService()->search_terms_data()) && | 459 GetTemplateURLService()->search_terms_data()) && |
| 462 (element->GetType() != TemplateURL::OMNIBOX_API_EXTENSION)) { | 460 (element->GetType() != TemplateURL::OMNIBOX_API_EXTENSION)) { |
| 463 // No query input; return a generic, no-destination placeholder. | 461 // No query input; return a generic, no-destination placeholder. |
| 464 match->contents.assign( | 462 match->contents.assign( |
| 465 l10n_util::GetStringFUTF16(message_id, | 463 l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE)); |
| 466 element->AdjustedShortNameForLocaleDirection(), | |
| 467 l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE))); | |
| 468 match->contents_class.push_back( | 464 match->contents_class.push_back( |
| 469 ACMatchClassification(0, ACMatchClassification::DIM)); | 465 ACMatchClassification(0, ACMatchClassification::DIM)); |
| 470 } else { | 466 } else { |
| 471 // Keyword that has no replacement text (aka a shorthand for a URL). | 467 // Keyword or extension that has no replacement text (aka a shorthand for |
| 468 // a URL). |
| 472 match->destination_url = GURL(element->url()); | 469 match->destination_url = GURL(element->url()); |
| 473 match->contents.assign(element->short_name()); | 470 match->contents.assign(element->short_name()); |
| 474 AutocompleteMatch::ClassifyLocationInString(0, match->contents.length(), | 471 AutocompleteMatch::ClassifyLocationInString(0, match->contents.length(), |
| 475 match->contents.length(), ACMatchClassification::NONE, | 472 match->contents.length(), ACMatchClassification::NONE, |
| 476 &match->contents_class); | 473 &match->contents_class); |
| 477 } | 474 } |
| 478 } else { | 475 } else { |
| 479 // Create destination URL by escaping user input and substituting into | 476 // Create destination URL by escaping user input and substituting into |
| 480 // keyword template URL. The escaping here handles whitespace in user | 477 // keyword template URL. The escaping here handles whitespace in user |
| 481 // input, but we rely on later canonicalization functions to do more | 478 // input, but we rely on later canonicalization functions to do more |
| 482 // fixup to make the URL valid if necessary. | 479 // fixup to make the URL valid if necessary. |
| 483 DCHECK(element_ref.SupportsReplacement( | 480 DCHECK(element_ref.SupportsReplacement( |
| 484 GetTemplateURLService()->search_terms_data())); | 481 GetTemplateURLService()->search_terms_data())); |
| 485 TemplateURLRef::SearchTermsArgs search_terms_args(remaining_input); | 482 TemplateURLRef::SearchTermsArgs search_terms_args(remaining_input); |
| 486 search_terms_args.append_extra_query_params = | 483 search_terms_args.append_extra_query_params = |
| 487 element == GetTemplateURLService()->GetDefaultSearchProvider(); | 484 element == GetTemplateURLService()->GetDefaultSearchProvider(); |
| 488 match->destination_url = GURL(element_ref.ReplaceSearchTerms( | 485 match->destination_url = GURL(element_ref.ReplaceSearchTerms( |
| 489 search_terms_args, GetTemplateURLService()->search_terms_data())); | 486 search_terms_args, GetTemplateURLService()->search_terms_data())); |
| 490 std::vector<size_t> content_param_offsets; | 487 match->contents = remaining_input; |
| 491 match->contents.assign(l10n_util::GetStringFUTF16(message_id, | 488 match->contents_class.push_back( |
| 492 element->short_name(), | 489 ACMatchClassification(0, ACMatchClassification::NONE)); |
| 493 remaining_input, | |
| 494 &content_param_offsets)); | |
| 495 DCHECK_EQ(2U, content_param_offsets.size()); | |
| 496 AutocompleteMatch::ClassifyLocationInString(content_param_offsets[1], | |
| 497 remaining_input.length(), match->contents.length(), | |
| 498 ACMatchClassification::NONE, &match->contents_class); | |
| 499 } | 490 } |
| 500 } | 491 } |
| 501 | 492 |
| 502 TemplateURLService* KeywordProvider::GetTemplateURLService() const { | 493 TemplateURLService* KeywordProvider::GetTemplateURLService() const { |
| 503 // Make sure the model is loaded. This is cheap and quickly bails out if | 494 // Make sure the model is loaded. This is cheap and quickly bails out if |
| 504 // the model is already loaded. | 495 // the model is already loaded. |
| 505 model_->Load(); | 496 model_->Load(); |
| 506 return model_; | 497 return model_; |
| 507 } | 498 } |
| OLD | NEW |