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 "components/metrics/proto/omnibox_event.pb.h"
13 #include "url/gurl.h"
14
15 // static
16 const int AutocompleteClassifier::kDefaultOmniboxProviders =
17 AutocompleteProvider::TYPE_BOOKMARK |
18 AutocompleteProvider::TYPE_BUILTIN |
19 AutocompleteProvider::TYPE_HISTORY_QUICK |
20 AutocompleteProvider::TYPE_HISTORY_URL |
21 AutocompleteProvider::TYPE_KEYWORD |
22 AutocompleteProvider::TYPE_SEARCH |
23 AutocompleteProvider::TYPE_SHORTCUTS |
24 AutocompleteProvider::TYPE_ZERO_SUGGEST;
25
AutocompleteClassifier(Profile * profile)26 AutocompleteClassifier::AutocompleteClassifier(Profile* profile)
27 : controller_(new AutocompleteController(profile, NULL,
28 kDefaultOmniboxProviders)),
29 inside_classify_(false) {
30 }
31
~AutocompleteClassifier()32 AutocompleteClassifier::~AutocompleteClassifier() {
33 // We should only reach here after Shutdown() has been called.
34 DCHECK(!controller_.get());
35 }
36
Classify(const base::string16 & text,bool prefer_keyword,bool allow_exact_keyword_match,metrics::OmniboxEventProto::PageClassification page_classification,AutocompleteMatch * match,GURL * alternate_nav_url)37 void AutocompleteClassifier::Classify(
38 const base::string16& text,
39 bool prefer_keyword,
40 bool allow_exact_keyword_match,
41 metrics::OmniboxEventProto::PageClassification page_classification,
42 AutocompleteMatch* match,
43 GURL* alternate_nav_url) {
44 DCHECK(!inside_classify_);
45 base::AutoReset<bool> reset(&inside_classify_, true);
46 controller_->Start(AutocompleteInput(
47 text, base::string16::npos, base::string16(), GURL(),
48 page_classification, true, prefer_keyword,
49 allow_exact_keyword_match, false));
50 DCHECK(controller_->done());
51 const AutocompleteResult& result = controller_->result();
52 if (result.empty()) {
53 if (alternate_nav_url)
54 *alternate_nav_url = GURL();
55 return;
56 }
57
58 DCHECK(result.default_match() != result.end());
59 *match = *result.default_match();
60 if (alternate_nav_url)
61 *alternate_nav_url = result.alternate_nav_url();
62 }
63
Shutdown()64 void AutocompleteClassifier::Shutdown() {
65 controller_.reset();
66 }
67