Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Side by Side Diff: chrome/browser/autocomplete/keyword_provider.cc

Issue 460313002: No chrome dependencies in KeywordProvider (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rename setter Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/strings/string16.h" 10 #include "base/strings/string16.h"
11 #include "base/strings/string_util.h" 11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/autocomplete/keyword_extensions_delegate.h" 13 #include "chrome/browser/autocomplete/keyword_extensions_delegate.h"
14 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/search_engines/template_url_service_factory.h"
17 #include "components/metrics/proto/omnibox_input_type.pb.h" 14 #include "components/metrics/proto/omnibox_input_type.pb.h"
18 #include "components/omnibox/autocomplete_match.h" 15 #include "components/omnibox/autocomplete_match.h"
19 #include "components/omnibox/autocomplete_provider_listener.h" 16 #include "components/omnibox/autocomplete_provider_listener.h"
20 #include "components/search_engines/template_url.h" 17 #include "components/search_engines/template_url.h"
21 #include "components/search_engines/template_url_service.h" 18 #include "components/search_engines/template_url_service.h"
22 #include "content/public/browser/notification_details.h"
23 #include "content/public/browser/notification_source.h"
24 #include "extensions/browser/extension_system.h"
25 #include "grit/generated_resources.h" 19 #include "grit/generated_resources.h"
26 #include "net/base/escape.h" 20 #include "net/base/escape.h"
27 #include "net/base/net_util.h" 21 #include "net/base/net_util.h"
28 #include "ui/base/l10n/l10n_util.h" 22 #include "ui/base/l10n/l10n_util.h"
29 23
30 #if defined(ENABLE_EXTENSIONS)
31 #include "chrome/browser/autocomplete/keyword_extensions_delegate_impl.h"
32 #endif
33
34 namespace { 24 namespace {
35 25
36 // Helper functor for Start(), for sorting keyword matches by quality. 26 // Helper functor for Start(), for sorting keyword matches by quality.
37 class CompareQuality { 27 class CompareQuality {
38 public: 28 public:
39 // A keyword is of higher quality when a greater fraction of it has been 29 // A keyword is of higher quality when a greater fraction of it has been
40 // typed, that is, when it is shorter. 30 // typed, that is, when it is shorter.
41 // 31 //
42 // TODO(pkasting): Most recent and most frequent keywords are probably 32 // TODO(pkasting): Most recent and most frequent keywords are probably
43 // better rankings than the fraction of the keyword typed. We should 33 // better rankings than the fraction of the keyword typed. We should
(...skipping 28 matching lines...) Expand all
72 if (delegate_) 62 if (delegate_)
73 delegate_->MaybeEndExtensionKeywordMode(); 63 delegate_->MaybeEndExtensionKeywordMode();
74 } 64 }
75 65
76 void ScopedEndExtensionKeywordMode::StayInKeywordMode() { 66 void ScopedEndExtensionKeywordMode::StayInKeywordMode() {
77 delegate_ = NULL; 67 delegate_ = NULL;
78 } 68 }
79 69
80 } // namespace 70 } // namespace
81 71
82 KeywordProvider::KeywordProvider(AutocompleteProviderListener* listener, 72 KeywordProvider::KeywordProvider(
83 Profile* profile) 73 AutocompleteProviderListener* listener,
74 TemplateURLService* model)
84 : AutocompleteProvider(AutocompleteProvider::TYPE_KEYWORD), 75 : AutocompleteProvider(AutocompleteProvider::TYPE_KEYWORD),
85 listener_(listener), 76 listener_(listener),
86 profile_(profile),
87 model_(NULL) {
88 #if defined(ENABLE_EXTENSIONS)
89 extensions_delegate_.reset(new KeywordExtensionsDelegateImpl(this));
90 #endif
91 }
92
93 KeywordProvider::KeywordProvider(AutocompleteProviderListener* listener,
94 TemplateURLService* model)
95 : AutocompleteProvider(AutocompleteProvider::TYPE_KEYWORD),
96 listener_(listener),
97 profile_(NULL),
98 model_(model) { 77 model_(model) {
99 } 78 }
100 79
101 // static 80 // static
102 base::string16 KeywordProvider::SplitKeywordFromInput( 81 base::string16 KeywordProvider::SplitKeywordFromInput(
103 const base::string16& input, 82 const base::string16& input,
104 bool trim_leading_whitespace, 83 bool trim_leading_whitespace,
105 base::string16* remaining_input) { 84 base::string16* remaining_input) {
106 // Find end of first token. The AutocompleteController has trimmed leading 85 // Find end of first token. The AutocompleteController has trimmed leading
107 // whitespace, so we need not skip over that. 86 // whitespace, so we need not skip over that.
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 // Don't provide a keyword if it doesn't support replacement. 175 // Don't provide a keyword if it doesn't support replacement.
197 const TemplateURL* const template_url = 176 const TemplateURL* const template_url =
198 url_service->GetTemplateURLForKeyword(keyword); 177 url_service->GetTemplateURLForKeyword(keyword);
199 if (!template_url || 178 if (!template_url ||
200 !template_url->SupportsReplacement(url_service->search_terms_data())) 179 !template_url->SupportsReplacement(url_service->search_terms_data()))
201 return base::string16(); 180 return base::string16();
202 181
203 // Don't provide a keyword for inactive/disabled extension keywords. 182 // Don't provide a keyword for inactive/disabled extension keywords.
204 if ((template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) && 183 if ((template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) &&
205 extensions_delegate_ && 184 extensions_delegate_ &&
206 !extensions_delegate_->IsEnabledExtension( 185 !extensions_delegate_->IsEnabledExtension(template_url->GetExtensionId()))
207 profile_, template_url->GetExtensionId()))
208 return base::string16(); 186 return base::string16();
209 187
210 return keyword; 188 return keyword;
211 } 189 }
212 190
213 AutocompleteMatch KeywordProvider::CreateVerbatimMatch( 191 AutocompleteMatch KeywordProvider::CreateVerbatimMatch(
214 const base::string16& text, 192 const base::string16& text,
215 const base::string16& keyword, 193 const base::string16& keyword,
216 const AutocompleteInput& input) { 194 const AutocompleteInput& input) {
217 // A verbatim match is allowed to be the default match. 195 // A verbatim match is allowed to be the default match.
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 TemplateURLService::TemplateURLVector matches; 241 TemplateURLService::TemplateURLVector matches;
264 GetTemplateURLService()->FindMatchingKeywords( 242 GetTemplateURLService()->FindMatchingKeywords(
265 keyword, !remaining_input.empty(), &matches); 243 keyword, !remaining_input.empty(), &matches);
266 244
267 for (TemplateURLService::TemplateURLVector::iterator i(matches.begin()); 245 for (TemplateURLService::TemplateURLVector::iterator i(matches.begin());
268 i != matches.end(); ) { 246 i != matches.end(); ) {
269 const TemplateURL* template_url = *i; 247 const TemplateURL* template_url = *i;
270 248
271 // Prune any extension keywords that are disallowed in incognito mode (if 249 // Prune any extension keywords that are disallowed in incognito mode (if
272 // we're incognito), or disabled. 250 // we're incognito), or disabled.
273 if (profile_ && 251 if (template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION &&
274 (template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) &&
275 extensions_delegate_ && 252 extensions_delegate_ &&
276 !extensions_delegate_->IsEnabledExtension( 253 !extensions_delegate_->IsEnabledExtension(
277 profile_, template_url->GetExtensionId())) { 254 template_url->GetExtensionId())) {
278 i = matches.erase(i); 255 i = matches.erase(i);
279 continue; 256 continue;
280 } 257 }
281 258
282 // Prune any substituting keywords if there is no substitution. 259 // Prune any substituting keywords if there is no substitution.
283 if (template_url->SupportsReplacement( 260 if (template_url->SupportsReplacement(
284 GetTemplateURLService()->search_terms_data()) && 261 GetTemplateURLService()->search_terms_data()) &&
285 remaining_input.empty() && 262 remaining_input.empty() &&
286 !input.allow_exact_keyword_match()) { 263 !input.allow_exact_keyword_match()) {
287 i = matches.erase(i); 264 i = matches.erase(i);
(...skipping 25 matching lines...) Expand all
313 290
314 // TODO(pkasting): We should probably check that if the user explicitly 291 // TODO(pkasting): We should probably check that if the user explicitly
315 // typed a scheme, that scheme matches the one in |template_url|. 292 // typed a scheme, that scheme matches the one in |template_url|.
316 293
317 // When creating an exact match (either for the keyword itself, no 294 // When creating an exact match (either for the keyword itself, no
318 // remaining query or an extension keyword, possibly with remaining 295 // remaining query or an extension keyword, possibly with remaining
319 // input), allow the match to be the default match. 296 // input), allow the match to be the default match.
320 matches_.push_back(CreateAutocompleteMatch( 297 matches_.push_back(CreateAutocompleteMatch(
321 template_url, input, keyword.length(), remaining_input, true, -1)); 298 template_url, input, keyword.length(), remaining_input, true, -1));
322 299
323 if (profile_ && is_extension_keyword && extensions_delegate_) { 300 if (is_extension_keyword && extensions_delegate_) {
324 if (extensions_delegate_->Start(input, minimal_changes, template_url, 301 if (extensions_delegate_->Start(input, minimal_changes, template_url,
325 remaining_input)) 302 remaining_input))
326 keyword_mode_toggle.StayInKeywordMode(); 303 keyword_mode_toggle.StayInKeywordMode();
327 } 304 }
328 } else { 305 } else {
329 if (matches.size() > kMaxMatches) 306 if (matches.size() > kMaxMatches)
330 matches.erase(matches.begin() + kMaxMatches, matches.end()); 307 matches.erase(matches.begin() + kMaxMatches, matches.end());
331 for (TemplateURLService::TemplateURLVector::const_iterator i( 308 for (TemplateURLService::TemplateURLVector::const_iterator i(
332 matches.begin()); i != matches.end(); ++i) { 309 matches.begin()); i != matches.end(); ++i) {
333 matches_.push_back(CreateAutocompleteMatch( 310 matches_.push_back(CreateAutocompleteMatch(
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
480 remaining_input, 457 remaining_input,
481 &content_param_offsets)); 458 &content_param_offsets));
482 DCHECK_EQ(2U, content_param_offsets.size()); 459 DCHECK_EQ(2U, content_param_offsets.size());
483 AutocompleteMatch::ClassifyLocationInString(content_param_offsets[1], 460 AutocompleteMatch::ClassifyLocationInString(content_param_offsets[1],
484 remaining_input.length(), match->contents.length(), 461 remaining_input.length(), match->contents.length(),
485 ACMatchClassification::NONE, &match->contents_class); 462 ACMatchClassification::NONE, &match->contents_class);
486 } 463 }
487 } 464 }
488 465
489 TemplateURLService* KeywordProvider::GetTemplateURLService() const { 466 TemplateURLService* KeywordProvider::GetTemplateURLService() const {
490 TemplateURLService* service = profile_ ?
491 TemplateURLServiceFactory::GetForProfile(profile_) : model_;
492 // Make sure the model is loaded. This is cheap and quickly bails out if 467 // Make sure the model is loaded. This is cheap and quickly bails out if
493 // the model is already loaded. 468 // the model is already loaded.
494 DCHECK(service); 469 model_->Load();
495 service->Load(); 470 return model_;
496 return service;
497 } 471 }
OLDNEW
« no previous file with comments | « chrome/browser/autocomplete/keyword_provider.h ('k') | chrome/browser/autocomplete/keyword_provider_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698