| 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 = string16::npos; |
| 154 // The adjustment assumes that the keyword was stripped from the beginning |
| 155 // of the original input. |
| 156 if (input->cursor_position() != string16::npos && |
| 157 !remaining_input.empty() && |
| 158 EndsWith(input->text(), remaining_input, true)) { |
| 159 int offset = input->text().length() - input->cursor_position(); |
| 160 // The cursor should never be past the last character. |
| 161 DCHECK_GE(offset, 0); |
| 162 // The cursor should never be in the keyword part, which is guaranteed |
| 163 // by OmniboxEditModel implementation (see omnibox_edit_model.cc). |
| 164 DCHECK_LE(offset, static_cast<int>(remaining_input.length())); |
| 165 if (offset <= 0) { |
| 166 // Normalize the cursor to be exactly after the last character. |
| 167 cursor_position = remaining_input.length(); |
| 168 } else { |
| 169 // If somehow the cursor was before the remaining text, set it to 0, |
| 170 // otherwise adjust it relative to the remaining text. |
| 171 cursor_position = offset > static_cast<int>(remaining_input.length()) ? |
| 172 0u : remaining_input.length() - offset; |
| 173 } |
| 174 } |
| 175 input->UpdateText(remaining_input, cursor_position, input->parts()); |
| 176 return template_url; |
| 177 } |
| 154 | 178 |
| 155 const TemplateURL* template_url = model->GetTemplateURLForKeyword(keyword); | 179 return NULL; |
| 156 return (template_url && template_url->SupportsReplacement()) ? | |
| 157 template_url : NULL; | |
| 158 } | 180 } |
| 159 | 181 |
| 160 string16 KeywordProvider::GetKeywordForText( | 182 string16 KeywordProvider::GetKeywordForText(const string16& text) const { |
| 161 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(); |
| 168 if (!url_service) | 189 if (!url_service) |
| 169 return string16(); | 190 return string16(); |
| 170 | 191 |
| 171 // Don't provide a keyword if it doesn't support replacement. | 192 // Don't provide a keyword if it doesn't support replacement. |
| (...skipping 414 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 |