[email protected] | a817ed39 | 2014-06-27 05:03:00 | [diff] [blame] | 1 | // Copyright 2014 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/chrome_autocomplete_scheme_classifier.h" |
| 6 | |
| 7 | #include "base/strings/string_util.h" |
| 8 | #include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h" |
| 9 | #include "chrome/browser/external_protocol/external_protocol_handler.h" |
| 10 | #include "chrome/browser/profiles/profile_io_data.h" |
| 11 | #include "content/public/common/url_constants.h" |
[email protected] | a817ed39 | 2014-06-27 05:03:00 | [diff] [blame] | 12 | #include "url/url_util.h" |
| 13 | |
| 14 | ChromeAutocompleteSchemeClassifier::ChromeAutocompleteSchemeClassifier( |
| 15 | Profile* profile) |
| 16 | : profile_(profile) { |
| 17 | } |
| 18 | |
| 19 | ChromeAutocompleteSchemeClassifier::~ChromeAutocompleteSchemeClassifier() { |
| 20 | } |
| 21 | |
Steven Holte | 3696c941 | 2017-08-24 18:38:03 | [diff] [blame] | 22 | metrics::OmniboxInputType |
[email protected] | a817ed39 | 2014-06-27 05:03:00 | [diff] [blame] | 23 | ChromeAutocompleteSchemeClassifier::GetInputTypeForScheme( |
| 24 | const std::string& scheme) const { |
David Bienvenu | 6fcc593 | 2018-04-10 19:11:55 | [diff] [blame] | 25 | if (scheme.empty()) { |
| 26 | return metrics::OmniboxInputType::INVALID; |
| 27 | } |
[email protected] | a817ed39 | 2014-06-27 05:03:00 | [diff] [blame] | 28 | if (base::IsStringASCII(scheme) && |
| 29 | (ProfileIOData::IsHandledProtocol(scheme) || |
brettw | bc17d2c8 | 2015-06-09 22:39:08 | [diff] [blame] | 30 | base::LowerCaseEqualsASCII(scheme, content::kViewSourceScheme) || |
| 31 | base::LowerCaseEqualsASCII(scheme, url::kJavaScriptScheme) || |
| 32 | base::LowerCaseEqualsASCII(scheme, url::kDataScheme))) { |
[email protected] | a817ed39 | 2014-06-27 05:03:00 | [diff] [blame] | 33 | return metrics::OmniboxInputType::URL; |
| 34 | } |
| 35 | |
| 36 | // Also check for schemes registered via registerProtocolHandler(), which |
| 37 | // can be handled by web pages/apps. |
[email protected] | a23abe47 | 2014-07-29 23:46:31 | [diff] [blame] | 38 | ProtocolHandlerRegistry* registry = profile_ ? |
| 39 | ProtocolHandlerRegistryFactory::GetForBrowserContext(profile_) : NULL; |
[email protected] | a817ed39 | 2014-06-27 05:03:00 | [diff] [blame] | 40 | if (registry && registry->IsHandledProtocol(scheme)) |
| 41 | return metrics::OmniboxInputType::URL; |
| 42 | |
| 43 | // Not an internal protocol; check if it's an external protocol, i.e. one |
| 44 | // that's registered on the user's OS and will shell out to another program. |
| 45 | // |
| 46 | // We need to do this after the checks above because some internally |
| 47 | // handlable schemes (e.g. "javascript") may be treated as "blocked" by the |
| 48 | // external protocol handler because we don't want pages to open them, but |
| 49 | // users still can. |
[email protected] | a817ed39 | 2014-06-27 05:03:00 | [diff] [blame] | 50 | const ExternalProtocolHandler::BlockState block_state = |
ramyasharma | 2c618e17 | 2017-02-06 05:41:34 | [diff] [blame] | 51 | ExternalProtocolHandler::GetBlockState(scheme, profile_); |
[email protected] | a817ed39 | 2014-06-27 05:03:00 | [diff] [blame] | 52 | switch (block_state) { |
| 53 | case ExternalProtocolHandler::DONT_BLOCK: |
| 54 | return metrics::OmniboxInputType::URL; |
| 55 | |
| 56 | case ExternalProtocolHandler::BLOCK: |
| 57 | // If we don't want the user to open the URL, don't let it be navigated |
| 58 | // to at all. |
| 59 | return metrics::OmniboxInputType::QUERY; |
| 60 | |
David Bienvenu | 6fcc593 | 2018-04-10 19:11:55 | [diff] [blame] | 61 | case ExternalProtocolHandler::UNKNOWN: { |
David Bienvenu | bedfe9f | 2018-05-03 18:50:25 | [diff] [blame] | 62 | #if defined(OS_LINUX) |
| 63 | // Linux impl of GetApplicationNameForProtocol doesn't distinguish |
| 64 | // between URL schemes with handers and those without. This will |
| 65 | // make the default behaviour be search on Linux. |
| 66 | return metrics::OmniboxInputType::INVALID; |
| 67 | #endif // defined(OS_LINUX) |
David Bienvenu | 6fcc593 | 2018-04-10 19:11:55 | [diff] [blame] | 68 | // If block state is unknown, check if there is an application registered |
| 69 | // for the url scheme. |
| 70 | GURL url(scheme + "://"); |
| 71 | base::string16 application_name = |
| 72 | shell_integration::GetApplicationNameForProtocol(url); |
| 73 | return application_name.empty() ? metrics::OmniboxInputType::INVALID |
| 74 | : metrics::OmniboxInputType::URL; |
| 75 | } |
[email protected] | a817ed39 | 2014-06-27 05:03:00 | [diff] [blame] | 76 | } |
David Bienvenu | 6fcc593 | 2018-04-10 19:11:55 | [diff] [blame] | 77 | NOTREACHED(); |
| 78 | return metrics::OmniboxInputType::INVALID; |
[email protected] | a817ed39 | 2014-06-27 05:03:00 | [diff] [blame] | 79 | } |