• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #include "chrome/browser/autocomplete/autocomplete_classifier.h"
6 
7 #include "base/auto_reset.h"
8 #include "chrome/browser/autocomplete/autocomplete_controller.h"
9 #include "chrome/browser/autocomplete/autocomplete_input.h"
10 #include "chrome/browser/autocomplete/autocomplete_match.h"
11 #include "chrome/browser/autocomplete/autocomplete_provider.h"
12 #include "url/gurl.h"
13 
14 // static
15 const int AutocompleteClassifier::kDefaultOmniboxProviders =
16     AutocompleteProvider::TYPE_BOOKMARK |
17     AutocompleteProvider::TYPE_BUILTIN |
18     AutocompleteProvider::TYPE_HISTORY_QUICK |
19     AutocompleteProvider::TYPE_HISTORY_URL |
20     AutocompleteProvider::TYPE_KEYWORD |
21     AutocompleteProvider::TYPE_SEARCH |
22     AutocompleteProvider::TYPE_SHORTCUTS |
23     AutocompleteProvider::TYPE_ZERO_SUGGEST;
24 
AutocompleteClassifier(Profile * profile)25 AutocompleteClassifier::AutocompleteClassifier(Profile* profile)
26     : controller_(new AutocompleteController(profile, NULL,
27                                              kDefaultOmniboxProviders)),
28       inside_classify_(false) {
29 }
30 
~AutocompleteClassifier()31 AutocompleteClassifier::~AutocompleteClassifier() {
32   // We should only reach here after Shutdown() has been called.
33   DCHECK(!controller_.get());
34 }
35 
Classify(const base::string16 & text,bool prefer_keyword,bool allow_exact_keyword_match,AutocompleteMatch * match,GURL * alternate_nav_url)36 void AutocompleteClassifier::Classify(const base::string16& text,
37                                       bool prefer_keyword,
38                                       bool allow_exact_keyword_match,
39                                       AutocompleteMatch* match,
40                                       GURL* alternate_nav_url) {
41   DCHECK(!inside_classify_);
42   base::AutoReset<bool> reset(&inside_classify_, true);
43   controller_->Start(AutocompleteInput(
44       text, base::string16::npos, base::string16(), GURL(),
45       AutocompleteInput::INVALID_SPEC, true, prefer_keyword,
46       allow_exact_keyword_match, AutocompleteInput::BEST_MATCH));
47   DCHECK(controller_->done());
48   const AutocompleteResult& result = controller_->result();
49   if (result.empty()) {
50     if (alternate_nav_url)
51       *alternate_nav_url = GURL();
52     return;
53   }
54 
55   DCHECK(result.default_match() != result.end());
56   *match = *result.default_match();
57   if (alternate_nav_url)
58     *alternate_nav_url = result.alternate_nav_url();
59 }
60 
Shutdown()61 void AutocompleteClassifier::Shutdown() {
62   controller_.reset();
63 }
64