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, true)) { | |
| 158 int offset = input->text().length() - cursor_position; | |
| 159 // The cursor should never be past the last character. | |
| 160 DCHECK_GE(offset, 0); | |
| 161 // The cursor should never be in the keyword part, which is guaranteed | |
| 162 // by OmniboxEditModel implementation (see omnibox_edit_model.cc). | |
| 163 DCHECK_LE(offset, static_cast<int>(remaining_input.length())); | |
| 164 if (offset <= 0) { | |
| 165 // Normalize the cursor to be exactly after the last character. | |
| 166 cursor_position = remaining_input.length(); | |
| 167 } else { | |
| 168 // If somehow the cursor was before the remaining text, set it to 0, | |
| 169 // otherwise adjust it with relatively to the remaining text. | |
|
Mark P
2013/01/29 22:25:37
with relatively
->
relative
Bart N.
2013/01/29 22:57:05
Done.
| |
| 170 cursor_position = offset > static_cast<int>(remaining_input.length()) ? | |
| 171 0u : remaining_input.length() - offset; | |
| 172 } | |
| 173 } | |
| 174 input->UpdateText(remaining_input, cursor_position, input->parts()); | |
| 175 return template_url; | |
| 176 } | |
| 154 | 177 |
| 155 const TemplateURL* template_url = model->GetTemplateURLForKeyword(keyword); | 178 return NULL; |
| 156 return (template_url && template_url->SupportsReplacement()) ? | |
| 157 template_url : NULL; | |
| 158 } | 179 } |
| 159 | 180 |
| 160 string16 KeywordProvider::GetKeywordForText( | 181 string16 KeywordProvider::GetKeywordForText( |
| 161 const string16& text) const { | 182 const string16& text) const { |
| 162 const string16 keyword(TemplateURLService::CleanUserInputKeyword(text)); | 183 const string16 keyword(TemplateURLService::CleanUserInputKeyword(text)); |
| 163 | 184 |
| 164 if (keyword.empty()) | 185 if (keyword.empty()) |
| 165 return keyword; | 186 return keyword; |
| 166 | 187 |
| 167 TemplateURLService* url_service = GetTemplateURLService(); | 188 TemplateURLService* url_service = GetTemplateURLService(); |
| (...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 586 } | 607 } |
| 587 | 608 |
| 588 void KeywordProvider::MaybeEndExtensionKeywordMode() { | 609 void KeywordProvider::MaybeEndExtensionKeywordMode() { |
| 589 if (!current_keyword_extension_id_.empty()) { | 610 if (!current_keyword_extension_id_.empty()) { |
| 590 extensions::ExtensionOmniboxEventRouter::OnInputCancelled( | 611 extensions::ExtensionOmniboxEventRouter::OnInputCancelled( |
| 591 profile_, current_keyword_extension_id_); | 612 profile_, current_keyword_extension_id_); |
| 592 | 613 |
| 593 current_keyword_extension_id_.clear(); | 614 current_keyword_extension_id_.clear(); |
| 594 } | 615 } |
| 595 } | 616 } |
| OLD | NEW |