| 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 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 return base::string16(); | 181 return base::string16(); |
| 182 | 182 |
| 183 // Don't provide a keyword if it doesn't support replacement. | 183 // Don't provide a keyword if it doesn't support replacement. |
| 184 const TemplateURL* const template_url = | 184 const TemplateURL* const template_url = |
| 185 url_service->GetTemplateURLForKeyword(keyword); | 185 url_service->GetTemplateURLForKeyword(keyword); |
| 186 if (!template_url || | 186 if (!template_url || |
| 187 !template_url->SupportsReplacement(url_service->search_terms_data())) | 187 !template_url->SupportsReplacement(url_service->search_terms_data())) |
| 188 return base::string16(); | 188 return base::string16(); |
| 189 | 189 |
| 190 // Don't provide a keyword for inactive/disabled extension keywords. | 190 // Don't provide a keyword for inactive/disabled extension keywords. |
| 191 if ((template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) && | 191 if ((template_url->type() == TemplateURL::OMNIBOX_API_EXTENSION) && |
| 192 extensions_delegate_ && | 192 extensions_delegate_ && |
| 193 !extensions_delegate_->IsEnabledExtension(template_url->GetExtensionId())) | 193 !extensions_delegate_->IsEnabledExtension(template_url->GetExtensionId())) |
| 194 return base::string16(); | 194 return base::string16(); |
| 195 | 195 |
| 196 return keyword; | 196 return keyword; |
| 197 } | 197 } |
| 198 | 198 |
| 199 AutocompleteMatch KeywordProvider::CreateVerbatimMatch( | 199 AutocompleteMatch KeywordProvider::CreateVerbatimMatch( |
| 200 const base::string16& text, | 200 const base::string16& text, |
| 201 const base::string16& keyword, | 201 const base::string16& keyword, |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 GetTemplateURLService()->AddMatchingDomainKeywords( | 258 GetTemplateURLService()->AddMatchingDomainKeywords( |
| 259 keyword, !remaining_input.empty(), &matches); | 259 keyword, !remaining_input.empty(), &matches); |
| 260 } | 260 } |
| 261 | 261 |
| 262 for (TemplateURLService::TURLsAndMeaningfulLengths::iterator | 262 for (TemplateURLService::TURLsAndMeaningfulLengths::iterator |
| 263 i(matches.begin()); i != matches.end(); ) { | 263 i(matches.begin()); i != matches.end(); ) { |
| 264 const TemplateURL* template_url = i->first; | 264 const TemplateURL* template_url = i->first; |
| 265 | 265 |
| 266 // Prune any extension keywords that are disallowed in incognito mode (if | 266 // Prune any extension keywords that are disallowed in incognito mode (if |
| 267 // we're incognito), or disabled. | 267 // we're incognito), or disabled. |
| 268 if (template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION && | 268 if (template_url->type() == TemplateURL::OMNIBOX_API_EXTENSION && |
| 269 extensions_delegate_ && | 269 extensions_delegate_ && |
| 270 !extensions_delegate_->IsEnabledExtension( | 270 !extensions_delegate_->IsEnabledExtension( |
| 271 template_url->GetExtensionId())) { | 271 template_url->GetExtensionId())) { |
| 272 i = matches.erase(i); | 272 i = matches.erase(i); |
| 273 continue; | 273 continue; |
| 274 } | 274 } |
| 275 | 275 |
| 276 // Prune any substituting keywords if there is no substitution. | 276 // Prune any substituting keywords if there is no substitution. |
| 277 if (template_url->SupportsReplacement( | 277 if (template_url->SupportsReplacement( |
| 278 GetTemplateURLService()->search_terms_data()) && | 278 GetTemplateURLService()->search_terms_data()) && |
| (...skipping 10 matching lines...) Expand all Loading... |
| 289 std::sort(matches.begin(), matches.end(), CompareQuality()); | 289 std::sort(matches.begin(), matches.end(), CompareQuality()); |
| 290 | 290 |
| 291 // Limit to one exact or three inexact matches, and mark them up for display | 291 // Limit to one exact or three inexact matches, and mark them up for display |
| 292 // in the autocomplete popup. | 292 // in the autocomplete popup. |
| 293 // Any exact match is going to be the highest quality match, and thus at the | 293 // Any exact match is going to be the highest quality match, and thus at the |
| 294 // front of our vector. | 294 // front of our vector. |
| 295 if (matches.front().first->keyword() == keyword) { | 295 if (matches.front().first->keyword() == keyword) { |
| 296 const TemplateURL* template_url = matches.front().first; | 296 const TemplateURL* template_url = matches.front().first; |
| 297 const size_t meaningful_keyword_length = matches.front().second; | 297 const size_t meaningful_keyword_length = matches.front().second; |
| 298 const bool is_extension_keyword = | 298 const bool is_extension_keyword = |
| 299 template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION; | 299 template_url->type() == TemplateURL::OMNIBOX_API_EXTENSION; |
| 300 | 300 |
| 301 // Only create an exact match if |remaining_input| is empty or if | 301 // Only create an exact match if |remaining_input| is empty or if |
| 302 // this is an extension keyword. If |remaining_input| is a | 302 // this is an extension keyword. If |remaining_input| is a |
| 303 // non-empty non-extension keyword (i.e., a regular keyword that | 303 // non-empty non-extension keyword (i.e., a regular keyword that |
| 304 // supports replacement and that has extra text following it), | 304 // supports replacement and that has extra text following it), |
| 305 // then SearchProvider creates the exact (a.k.a. verbatim) match. | 305 // then SearchProvider creates the exact (a.k.a. verbatim) match. |
| 306 if (!remaining_input.empty() && !is_extension_keyword) | 306 if (!remaining_input.empty() && !is_extension_keyword) |
| 307 return; | 307 return; |
| 308 | 308 |
| 309 // TODO(pkasting): We should probably check that if the user explicitly | 309 // TODO(pkasting): We should probably check that if the user explicitly |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 AutocompleteMatch* match) const { | 449 AutocompleteMatch* match) const { |
| 450 DCHECK(!element->short_name().empty()); | 450 DCHECK(!element->short_name().empty()); |
| 451 const TemplateURLRef& element_ref = element->url_ref(); | 451 const TemplateURLRef& element_ref = element->url_ref(); |
| 452 DCHECK(element_ref.IsValid(GetTemplateURLService()->search_terms_data())); | 452 DCHECK(element_ref.IsValid(GetTemplateURLService()->search_terms_data())); |
| 453 if (remaining_input.empty()) { | 453 if (remaining_input.empty()) { |
| 454 // Allow extension keyword providers to accept empty string input. This is | 454 // Allow extension keyword providers to accept empty string input. This is |
| 455 // useful to allow extensions to do something in the case where no input is | 455 // useful to allow extensions to do something in the case where no input is |
| 456 // entered. | 456 // entered. |
| 457 if (element_ref.SupportsReplacement( | 457 if (element_ref.SupportsReplacement( |
| 458 GetTemplateURLService()->search_terms_data()) && | 458 GetTemplateURLService()->search_terms_data()) && |
| 459 (element->GetType() != TemplateURL::OMNIBOX_API_EXTENSION)) { | 459 (element->type() != TemplateURL::OMNIBOX_API_EXTENSION)) { |
| 460 // No query input; return a generic, no-destination placeholder. | 460 // No query input; return a generic, no-destination placeholder. |
| 461 match->contents.assign( | 461 match->contents.assign( |
| 462 l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE)); | 462 l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE)); |
| 463 match->contents_class.push_back( | 463 match->contents_class.push_back( |
| 464 ACMatchClassification(0, ACMatchClassification::DIM)); | 464 ACMatchClassification(0, ACMatchClassification::DIM)); |
| 465 } else { | 465 } else { |
| 466 // Keyword or extension that has no replacement text (aka a shorthand for | 466 // Keyword or extension that has no replacement text (aka a shorthand for |
| 467 // a URL). | 467 // a URL). |
| 468 match->destination_url = GURL(element->url()); | 468 match->destination_url = GURL(element->url()); |
| 469 match->contents.assign(element->short_name()); | 469 match->contents.assign(element->short_name()); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 488 ACMatchClassification(0, ACMatchClassification::NONE)); | 488 ACMatchClassification(0, ACMatchClassification::NONE)); |
| 489 } | 489 } |
| 490 } | 490 } |
| 491 | 491 |
| 492 TemplateURLService* KeywordProvider::GetTemplateURLService() const { | 492 TemplateURLService* KeywordProvider::GetTemplateURLService() const { |
| 493 // Make sure the model is loaded. This is cheap and quickly bails out if | 493 // Make sure the model is loaded. This is cheap and quickly bails out if |
| 494 // the model is already loaded. | 494 // the model is already loaded. |
| 495 model_->Load(); | 495 model_->Load(); |
| 496 return model_; | 496 return model_; |
| 497 } | 497 } |
| OLD | NEW |