[email protected] | 810ffba | 2012-06-12 01:07:48 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 69c579e | 2010-04-23 20:01:00 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
blundell | 2102f7c | 2015-07-09 10:00:53 | [diff] [blame] | 5 | #include "components/omnibox/browser/autocomplete_classifier.h" |
[email protected] | 69c579e | 2010-04-23 20:01:00 | [diff] [blame] | 6 | |
dcheng | 51ace48a | 2015-12-26 22:45:17 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
[email protected] | cc44736 | 2011-04-06 03:57:48 | [diff] [blame] | 9 | #include "base/auto_reset.h" |
sdefresne | 0c08d43 | 2015-08-08 11:09:45 | [diff] [blame] | 10 | #include "build/build_config.h" |
[email protected] | 332d17d2 | 2014-06-20 16:56:03 | [diff] [blame] | 11 | #include "components/metrics/proto/omnibox_event.pb.h" |
blundell | 2102f7c | 2015-07-09 10:00:53 | [diff] [blame] | 12 | #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] | 761fa470 | 2013-07-02 15:25:15 | [diff] [blame] | 16 | #include "url/gurl.h" |
[email protected] | 69c579e | 2010-04-23 20:01:00 | [diff] [blame] | 17 | |
[email protected] | 35f1f4f0 | 2012-09-11 13:17:00 | [diff] [blame] | 18 | // static |
| 19 | const int AutocompleteClassifier::kDefaultOmniboxProviders = |
sdefresne | 0c08d43 | 2015-08-08 11:09:45 | [diff] [blame] | 20 | #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] | 35f1f4f0 | 2012-09-11 13:17:00 | [diff] [blame] | 26 | AutocompleteProvider::TYPE_BUILTIN | |
sdefresne | 0c08d43 | 2015-08-08 11:09:45 | [diff] [blame] | 27 | AutocompleteProvider::TYPE_SHORTCUTS | |
| 28 | AutocompleteProvider::TYPE_ZERO_SUGGEST | |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 29 | #else |
| 30 | // "URL from clipboard" can only be used on iOS. |
| 31 | AutocompleteProvider::TYPE_CLIPBOARD_URL | |
mattreynolds | 5afc0169 | 2016-08-19 22:09:56 | [diff] [blame] | 32 | // Physical Web omnibox results are only implemented on iOS. |
| 33 | AutocompleteProvider::TYPE_PHYSICAL_WEB | |
sdefresne | 0c08d43 | 2015-08-08 11:09:45 | [diff] [blame] | 34 | #endif |
| 35 | AutocompleteProvider::TYPE_BOOKMARK | |
[email protected] | 35f1f4f0 | 2012-09-11 13:17:00 | [diff] [blame] | 36 | AutocompleteProvider::TYPE_HISTORY_QUICK | |
| 37 | AutocompleteProvider::TYPE_HISTORY_URL | |
sdefresne | 0c08d43 | 2015-08-08 11:09:45 | [diff] [blame] | 38 | AutocompleteProvider::TYPE_SEARCH; |
[email protected] | 35f1f4f0 | 2012-09-11 13:17:00 | [diff] [blame] | 39 | |
[email protected] | a817ed39 | 2014-06-27 05:03:00 | [diff] [blame] | 40 | AutocompleteClassifier::AutocompleteClassifier( |
dcheng | 259570c | 2016-04-22 00:45:57 | [diff] [blame] | 41 | std::unique_ptr<AutocompleteController> controller, |
| 42 | std::unique_ptr<AutocompleteSchemeClassifier> scheme_classifier) |
dcheng | 51ace48a | 2015-12-26 22:45:17 | [diff] [blame] | 43 | : controller_(std::move(controller)), |
| 44 | scheme_classifier_(std::move(scheme_classifier)), |
| 45 | inside_classify_(false) {} |
[email protected] | 69c579e | 2010-04-23 20:01:00 | [diff] [blame] | 46 | |
| 47 | AutocompleteClassifier::~AutocompleteClassifier() { |
[email protected] | 810ffba | 2012-06-12 01:07:48 | [diff] [blame] | 48 | // We should only reach here after Shutdown() has been called. |
| 49 | DCHECK(!controller_.get()); |
[email protected] | 69c579e | 2010-04-23 20:01:00 | [diff] [blame] | 50 | } |
| 51 | |
blundell | be4da71 | 2016-02-01 17:18:47 | [diff] [blame] | 52 | void AutocompleteClassifier::Shutdown() { |
| 53 | controller_.reset(); |
| 54 | } |
| 55 | |
[email protected] | 51abb7b | 2014-02-09 23:00:08 | [diff] [blame] | 56 | void AutocompleteClassifier::Classify( |
| 57 | const base::string16& text, |
| 58 | bool prefer_keyword, |
| 59 | bool allow_exact_keyword_match, |
[email protected] | 332d17d2 | 2014-06-20 16:56:03 | [diff] [blame] | 60 | metrics::OmniboxEventProto::PageClassification page_classification, |
[email protected] | 51abb7b | 2014-02-09 23:00:08 | [diff] [blame] | 61 | AutocompleteMatch* match, |
| 62 | GURL* alternate_nav_url) { |
[email protected] | cc44736 | 2011-04-06 03:57:48 | [diff] [blame] | 63 | DCHECK(!inside_classify_); |
[email protected] | 997ec9f | 2012-11-21 04:44:14 | [diff] [blame] | 64 | base::AutoReset<bool> reset(&inside_classify_, true); |
[email protected] | 0079056 | 2012-12-14 09:57:16 | [diff] [blame] | 65 | controller_->Start(AutocompleteInput( |
pkasting | d932779e | 2014-10-07 22:38:35 | [diff] [blame] | 66 | text, base::string16::npos, std::string(), GURL(), page_classification, |
jif | cf322cda | 2015-06-17 11:01:18 | [diff] [blame] | 67 | true, prefer_keyword, allow_exact_keyword_match, false, false, |
pkasting | d932779e | 2014-10-07 22:38:35 | [diff] [blame] | 68 | *scheme_classifier_)); |
[email protected] | 69c579e | 2010-04-23 20:01:00 | [diff] [blame] | 69 | 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 | } |