chrome_autocomplete_scheme_classifier.cc revision 116680a4aac90f2aa7413d9095a592090648e557
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 =
37      profile_ ? ProtocolHandlerRegistryFactory::GetForProfile(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  //
49  // Note that the protocol handler needs to be informed that omnibox input
50  // should always be considered "user gesture-triggered", lest it always
51  // return BLOCK.
52  const ExternalProtocolHandler::BlockState block_state =
53      ExternalProtocolHandler::GetBlockState(scheme, true);
54  switch (block_state) {
55    case ExternalProtocolHandler::DONT_BLOCK:
56      return metrics::OmniboxInputType::URL;
57
58    case ExternalProtocolHandler::BLOCK:
59      // If we don't want the user to open the URL, don't let it be navigated
60      // to at all.
61      return metrics::OmniboxInputType::QUERY;
62
63    default:
64      return metrics::OmniboxInputType::INVALID;
65  }
66}
67