Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/autocomplete/keyword_provider.h" | 5 #include "chrome/browser/autocomplete/keyword_provider.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/string16.h" | 10 #include "base/string16.h" |
| 11 #include "base/string_util.h" | |
| 11 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/autocomplete/autocomplete_match.h" | 13 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 13 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" | 14 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" |
| 14 #include "chrome/browser/extensions/api/omnibox/omnibox_api.h" | 15 #include "chrome/browser/extensions/api/omnibox/omnibox_api.h" |
| 15 #include "chrome/browser/extensions/extension_service.h" | 16 #include "chrome/browser/extensions/extension_service.h" |
| 16 #include "chrome/browser/extensions/extension_system.h" | 17 #include "chrome/browser/extensions/extension_system.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/search_engines/template_url.h" | 19 #include "chrome/browser/search_engines/template_url.h" |
| 19 #include "chrome/browser/search_engines/template_url_service.h" | 20 #include "chrome/browser/search_engines/template_url_service.h" |
| 20 #include "chrome/browser/search_engines/template_url_service_factory.h" | 21 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 129 | 130 |
| 130 // And extract the replacement string. | 131 // And extract the replacement string. |
| 131 string16 remaining_input; | 132 string16 remaining_input; |
| 132 SplitKeywordFromInput(trimmed_input, trim_leading_whitespace, | 133 SplitKeywordFromInput(trimmed_input, trim_leading_whitespace, |
| 133 &remaining_input); | 134 &remaining_input); |
| 134 return remaining_input; | 135 return remaining_input; |
| 135 } | 136 } |
| 136 | 137 |
| 137 // static | 138 // static |
| 138 const TemplateURL* KeywordProvider::GetSubstitutingTemplateURLForInput( | 139 const TemplateURL* KeywordProvider::GetSubstitutingTemplateURLForInput( |
| 139 Profile* profile, | 140 TemplateURLService* model, |
| 140 const AutocompleteInput& input, | 141 AutocompleteInput* input) { |
| 141 string16* remaining_input) { | 142 if (!input->allow_exact_keyword_match()) |
| 142 if (!input.allow_exact_keyword_match()) | |
| 143 return NULL; | 143 return NULL; |
| 144 | 144 |
| 145 string16 keyword; | 145 string16 keyword, remaining_input; |
| 146 if (!ExtractKeywordFromInput(input, &keyword, remaining_input)) | 146 if (!ExtractKeywordFromInput(*input, &keyword, &remaining_input)) |
| 147 return NULL; | 147 return NULL; |
| 148 | 148 |
| 149 // Make sure the model is loaded. This is cheap and quickly bails out if | |
| 150 // the model is already loaded. | |
| 151 TemplateURLService* model = TemplateURLServiceFactory::GetForProfile(profile); | |
| 152 DCHECK(model); | 149 DCHECK(model); |
| 153 model->Load(); | 150 const TemplateURL* template_url = model->GetTemplateURLForKeyword(keyword); |
| 151 if (template_url && template_url->SupportsReplacement()) { | |
| 152 // Adjust cursor position iff it was set before, otherwise leave it as is. | |
| 153 size_t cursor_position = input->cursor_position(); | |
| 154 // The adjustment assumes that the keyword was stripped from the beginning | |
| 155 // of the original input. | |
| 156 if (cursor_position != string16::npos && | |
| 157 EndsWith(input->text(), remaining_input, false)) { | |
|
Mark P
2013/01/25 21:19:15
Why you being case insensitive here?
Mark P
2013/01/25 21:19:15
Under what conditions does this EndsWith fail?
Bart N.
2013/01/29 21:28:17
Actually, the original reason is moot here. I've c
Bart N.
2013/01/29 21:28:17
When there is a trailing space in the original inp
| |
| 158 int offset = input->text().length() - cursor_position; | |
| 159 cursor_position = offset <= 0 ? remaining_input.length() : | |
|
Mark P
2013/01/25 21:19:15
Perhaps DCHECK that offset >= 0 before this line?
Bart N.
2013/01/29 21:28:17
Done.
| |
| 160 (offset > static_cast<int>(remaining_input.length()) ? 0u : | |
|
Mark P
2013/01/25 21:19:15
I think I understand why offset can be greater tha
Peter Kasting
2013/01/28 00:04:22
Please do not nest ?:s within a single statement.
Bart N.
2013/01/29 21:28:17
Done.
Bart N.
2013/01/29 21:28:17
Done.
| |
| 161 remaining_input.length() - offset); | |
| 162 } | |
| 163 input->UpdateText(remaining_input, cursor_position, input->parts()); | |
| 164 return template_url; | |
| 165 } | |
| 154 | 166 |
| 155 const TemplateURL* template_url = model->GetTemplateURLForKeyword(keyword); | 167 return NULL; |
| 156 return (template_url && template_url->SupportsReplacement()) ? | |
| 157 template_url : NULL; | |
| 158 } | 168 } |
| 159 | 169 |
| 160 string16 KeywordProvider::GetKeywordForText( | 170 string16 KeywordProvider::GetKeywordForText( |
| 161 const string16& text) const { | 171 const string16& text) const { |
| 162 const string16 keyword(TemplateURLService::CleanUserInputKeyword(text)); | 172 const string16 keyword(TemplateURLService::CleanUserInputKeyword(text)); |
| 163 | 173 |
| 164 if (keyword.empty()) | 174 if (keyword.empty()) |
| 165 return keyword; | 175 return keyword; |
| 166 | 176 |
| 167 TemplateURLService* url_service = GetTemplateURLService(); | 177 TemplateURLService* url_service = GetTemplateURLService(); |
| (...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 586 } | 596 } |
| 587 | 597 |
| 588 void KeywordProvider::MaybeEndExtensionKeywordMode() { | 598 void KeywordProvider::MaybeEndExtensionKeywordMode() { |
| 589 if (!current_keyword_extension_id_.empty()) { | 599 if (!current_keyword_extension_id_.empty()) { |
| 590 extensions::ExtensionOmniboxEventRouter::OnInputCancelled( | 600 extensions::ExtensionOmniboxEventRouter::OnInputCancelled( |
| 591 profile_, current_keyword_extension_id_); | 601 profile_, current_keyword_extension_id_); |
| 592 | 602 |
| 593 current_keyword_extension_id_.clear(); | 603 current_keyword_extension_id_.clear(); |
| 594 } | 604 } |
| 595 } | 605 } |
| OLD | NEW |