Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / omnibox / browser / autocomplete_classifier.cc
blob26e349a6c402199f22b9dfe421b127d1334b5258
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.
5 #include "components/omnibox/browser/autocomplete_classifier.h"
7 #include "base/auto_reset.h"
8 #include "build/build_config.h"
9 #include "components/metrics/proto/omnibox_event.pb.h"
10 #include "components/omnibox/browser/autocomplete_controller.h"
11 #include "components/omnibox/browser/autocomplete_input.h"
12 #include "components/omnibox/browser/autocomplete_match.h"
13 #include "components/omnibox/browser/autocomplete_provider.h"
14 #include "url/gurl.h"
16 // static
17 const int AutocompleteClassifier::kDefaultOmniboxProviders =
18 #if !defined(OS_ANDROID) && !defined(OS_IOS)
19 // Custom search engines cannot be used on mobile..
20 AutocompleteProvider::TYPE_KEYWORD |
21 #endif
22 #if !defined(OS_IOS)
23 // "Builtin", "Shortcuts" and "Zero Suggest" are not supported on iOS.
24 AutocompleteProvider::TYPE_BUILTIN |
25 AutocompleteProvider::TYPE_SHORTCUTS |
26 AutocompleteProvider::TYPE_ZERO_SUGGEST |
27 #else
28 // "URL from clipboard" can only be used on iOS.
29 AutocompleteProvider::TYPE_CLIPBOARD_URL |
30 #endif
31 AutocompleteProvider::TYPE_BOOKMARK |
32 AutocompleteProvider::TYPE_HISTORY_QUICK |
33 AutocompleteProvider::TYPE_HISTORY_URL |
34 AutocompleteProvider::TYPE_SEARCH;
36 AutocompleteClassifier::AutocompleteClassifier(
37 scoped_ptr<AutocompleteController> controller,
38 scoped_ptr<AutocompleteSchemeClassifier> scheme_classifier)
39 : controller_(controller.Pass()),
40 scheme_classifier_(scheme_classifier.Pass()),
41 inside_classify_(false) {
44 AutocompleteClassifier::~AutocompleteClassifier() {
45 // We should only reach here after Shutdown() has been called.
46 DCHECK(!controller_.get());
49 void AutocompleteClassifier::Classify(
50 const base::string16& text,
51 bool prefer_keyword,
52 bool allow_exact_keyword_match,
53 metrics::OmniboxEventProto::PageClassification page_classification,
54 AutocompleteMatch* match,
55 GURL* alternate_nav_url) {
56 DCHECK(!inside_classify_);
57 base::AutoReset<bool> reset(&inside_classify_, true);
58 controller_->Start(AutocompleteInput(
59 text, base::string16::npos, std::string(), GURL(), page_classification,
60 true, prefer_keyword, allow_exact_keyword_match, false, false,
61 *scheme_classifier_));
62 DCHECK(controller_->done());
63 const AutocompleteResult& result = controller_->result();
64 if (result.empty()) {
65 if (alternate_nav_url)
66 *alternate_nav_url = GURL();
67 return;
70 DCHECK(result.default_match() != result.end());
71 *match = *result.default_match();
72 if (alternate_nav_url)
73 *alternate_nav_url = result.alternate_nav_url();
76 void AutocompleteClassifier::Shutdown() {
77 controller_.reset();