blob: 4e50bbfd9fe325f0d8d7edcc491d3326edce153e [file] [log] [blame]
[email protected]810ffba2012-06-12 01:07:481// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]69c579e2010-04-23 20:01:002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
blundell2102f7c2015-07-09 10:00:535#include "components/omnibox/browser/autocomplete_classifier.h"
[email protected]69c579e2010-04-23 20:01:006
dcheng51ace48a2015-12-26 22:45:177#include <utility>
8
[email protected]cc447362011-04-06 03:57:489#include "base/auto_reset.h"
sdefresne0c08d432015-08-08 11:09:4510#include "build/build_config.h"
[email protected]332d17d22014-06-20 16:56:0311#include "components/metrics/proto/omnibox_event.pb.h"
blundell2102f7c2015-07-09 10:00:5312#include "components/omnibox/browser/autocomplete_controller.h"
13#include "components/omnibox/browser/autocomplete_input.h"
14#include "components/omnibox/browser/autocomplete_match.h"
15#include "components/omnibox/browser/autocomplete_provider.h"
[email protected]761fa4702013-07-02 15:25:1516#include "url/gurl.h"
[email protected]69c579e2010-04-23 20:01:0017
[email protected]35f1f4f02012-09-11 13:17:0018// static
19const int AutocompleteClassifier::kDefaultOmniboxProviders =
sdefresne0c08d432015-08-08 11:09:4520#if !defined(OS_ANDROID) && !defined(OS_IOS)
21 // Custom search engines cannot be used on mobile..
22 AutocompleteProvider::TYPE_KEYWORD |
23#endif
24#if !defined(OS_IOS)
25 // "Builtin", "Shortcuts" and "Zero Suggest" are not supported on iOS.
[email protected]35f1f4f02012-09-11 13:17:0026 AutocompleteProvider::TYPE_BUILTIN |
sdefresne0c08d432015-08-08 11:09:4527 AutocompleteProvider::TYPE_SHORTCUTS |
28 AutocompleteProvider::TYPE_ZERO_SUGGEST |
sdefresne70948d62015-08-11 10:46:3529#else
30 // "URL from clipboard" can only be used on iOS.
31 AutocompleteProvider::TYPE_CLIPBOARD_URL |
mattreynolds5afc01692016-08-19 22:09:5632 // Physical Web omnibox results are only implemented on iOS.
33 AutocompleteProvider::TYPE_PHYSICAL_WEB |
sdefresne0c08d432015-08-08 11:09:4534#endif
35 AutocompleteProvider::TYPE_BOOKMARK |
[email protected]35f1f4f02012-09-11 13:17:0036 AutocompleteProvider::TYPE_HISTORY_QUICK |
37 AutocompleteProvider::TYPE_HISTORY_URL |
sdefresne0c08d432015-08-08 11:09:4538 AutocompleteProvider::TYPE_SEARCH;
[email protected]35f1f4f02012-09-11 13:17:0039
[email protected]a817ed392014-06-27 05:03:0040AutocompleteClassifier::AutocompleteClassifier(
dcheng259570c2016-04-22 00:45:5741 std::unique_ptr<AutocompleteController> controller,
42 std::unique_ptr<AutocompleteSchemeClassifier> scheme_classifier)
dcheng51ace48a2015-12-26 22:45:1743 : controller_(std::move(controller)),
44 scheme_classifier_(std::move(scheme_classifier)),
45 inside_classify_(false) {}
[email protected]69c579e2010-04-23 20:01:0046
47AutocompleteClassifier::~AutocompleteClassifier() {
[email protected]810ffba2012-06-12 01:07:4848 // We should only reach here after Shutdown() has been called.
49 DCHECK(!controller_.get());
[email protected]69c579e2010-04-23 20:01:0050}
51
blundellbe4da712016-02-01 17:18:4752void AutocompleteClassifier::Shutdown() {
53 controller_.reset();
54}
55
[email protected]51abb7b2014-02-09 23:00:0856void AutocompleteClassifier::Classify(
57 const base::string16& text,
58 bool prefer_keyword,
59 bool allow_exact_keyword_match,
[email protected]332d17d22014-06-20 16:56:0360 metrics::OmniboxEventProto::PageClassification page_classification,
[email protected]51abb7b2014-02-09 23:00:0861 AutocompleteMatch* match,
62 GURL* alternate_nav_url) {
[email protected]cc447362011-04-06 03:57:4863 DCHECK(!inside_classify_);
[email protected]997ec9f2012-11-21 04:44:1464 base::AutoReset<bool> reset(&inside_classify_, true);
[email protected]00790562012-12-14 09:57:1665 controller_->Start(AutocompleteInput(
pkastingd932779e2014-10-07 22:38:3566 text, base::string16::npos, std::string(), GURL(), page_classification,
jifcf322cda2015-06-17 11:01:1867 true, prefer_keyword, allow_exact_keyword_match, false, false,
pkastingd932779e2014-10-07 22:38:3568 *scheme_classifier_));
[email protected]69c579e2010-04-23 20:01:0069 DCHECK(controller_->done());
70 const AutocompleteResult& result = controller_->result();
71 if (result.empty()) {
72 if (alternate_nav_url)
73 *alternate_nav_url = GURL();
74 return;
75 }
76
77 DCHECK(result.default_match() != result.end());
78 *match = *result.default_match();
79 if (alternate_nav_url)
80 *alternate_nav_url = result.alternate_nav_url();
81}