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"
12#include "net/base/net_util.h"
13#include "url/url_util.h"
14
15ChromeAutocompleteSchemeClassifier::ChromeAutocompleteSchemeClassifier(
16    Profile* profile)
17    : profile_(profile) {
18}
19
20ChromeAutocompleteSchemeClassifier::~ChromeAutocompleteSchemeClassifier() {
21}
22
23metrics::OmniboxInputType::Type
24ChromeAutocompleteSchemeClassifier::GetInputTypeForScheme(
25    const std::string& scheme) const {
26  if (base::IsStringASCII(scheme) &&
27      (ProfileIOData::IsHandledProtocol(scheme) ||
28       LowerCaseEqualsASCII(scheme, content::kViewSourceScheme) ||
29       LowerCaseEqualsASCII(scheme, url::kJavaScriptScheme) ||
30       LowerCaseEqualsASCII(scheme, url::kDataScheme))) {
31    return metrics::OmniboxInputType::URL;
32  }
33
34  // Also check for schemes registered via registerProtocolHandler(), which
35  // can be handled by web pages/apps.
36  ProtocolHandlerRegistry* registry = profile_ ?
37      ProtocolHandlerRegistryFactory::GetForBrowserContext(profile_) : NULL;
38  if (registry && registry->IsHandledProtocol(scheme))
39    return metrics::OmniboxInputType::URL;
40
41  // Not an internal protocol; check if it's an external protocol, i.e. one
42  // that's registered on the user's OS and will shell out to another program.
43  //
44  // We need to do this after the checks above because some internally
45  // handlable schemes (e.g. "javascript") may be treated as "blocked" by the
46  // external protocol handler because we don't want pages to open them, but
47  // users still can.
48  const ExternalProtocolHandler::BlockState block_state =
49      ExternalProtocolHandler::GetBlockState(scheme);
50  switch (block_state) {
51    case ExternalProtocolHandler::DONT_BLOCK:
52      return metrics::OmniboxInputType::URL;
53
54    case ExternalProtocolHandler::BLOCK:
55      // If we don't want the user to open the URL, don't let it be navigated
56      // to at all.
57      return metrics::OmniboxInputType::QUERY;
58
59    default:
60      return metrics::OmniboxInputType::INVALID;
61  }
62}
63