1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/common/url_schemes.h"
#include <string.h>
#include <iterator>
#include <utility>
#include "base/no_destructor.h"
#include "base/strings/string_util.h"
#include "build/build_config.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_features.h"
#include "content/public/common/url_constants.h"
#include "third_party/blink/public/common/scheme_registry.h"
#include "url/url_util.h"
namespace content {
namespace {
bool g_registered_url_schemes = false;
const char* const kDefaultSavableSchemes[] = {
url::kHttpScheme,
url::kHttpsScheme,
url::kFileScheme,
url::kFileSystemScheme,
kChromeDevToolsScheme,
kChromeUIScheme,
url::kDataScheme
};
// These lists are lazily initialized below and are leaked on shutdown to
// prevent any destructors from being called that will slow us down or cause
// problems.
std::vector<std::string>& GetMutableSavableSchemes() {
static base::NoDestructor<std::vector<std::string>> schemes;
return *schemes;
}
// This set contains serialized canonicalized origins as well as hostname
// patterns. The latter are canonicalized by component.
std::vector<std::string>& GetMutableServiceWorkerSchemes() {
static base::NoDestructor<std::vector<std::string>> schemes;
return *schemes;
}
} // namespace
void RegisterContentSchemes(bool should_lock_registry) {
// On Android and in tests, schemes may have been registered already.
if (g_registered_url_schemes)
return;
g_registered_url_schemes = true;
ContentClient::Schemes schemes;
GetContentClient()->AddAdditionalSchemes(&schemes);
url::AddStandardScheme(kChromeDevToolsScheme, url::SCHEME_WITH_HOST);
url::AddStandardScheme(kChromeUIScheme, url::SCHEME_WITH_HOST);
url::AddStandardScheme(kChromeUIUntrustedScheme, url::SCHEME_WITH_HOST);
url::AddStandardScheme(kChromeErrorScheme, url::SCHEME_WITH_HOST);
for (auto& scheme : schemes.standard_schemes)
url::AddStandardScheme(scheme.c_str(), url::SCHEME_WITH_HOST);
for (auto& scheme : schemes.referrer_schemes)
url::AddReferrerScheme(scheme.c_str(), url::SCHEME_WITH_HOST);
schemes.secure_schemes.push_back(kChromeDevToolsScheme);
schemes.secure_schemes.push_back(kChromeUIScheme);
schemes.secure_schemes.push_back(kChromeUIUntrustedScheme);
schemes.secure_schemes.push_back(kChromeErrorScheme);
for (auto& scheme : schemes.secure_schemes)
url::AddSecureScheme(scheme.c_str());
for (auto& scheme : schemes.local_schemes)
url::AddLocalScheme(scheme.c_str());
for (auto& scheme : schemes.extension_schemes)
blink::CommonSchemeRegistry::RegisterURLSchemeAsExtension(scheme.c_str());
schemes.no_access_schemes.push_back(kChromeErrorScheme);
for (auto& scheme : schemes.no_access_schemes)
url::AddNoAccessScheme(scheme.c_str());
schemes.cors_enabled_schemes.push_back(kChromeUIScheme);
schemes.cors_enabled_schemes.push_back(kChromeUIUntrustedScheme);
for (auto& scheme : schemes.cors_enabled_schemes)
url::AddCorsEnabledScheme(scheme.c_str());
// TODO(mkwst): Investigate whether chrome-error should be included in
// csp_bypassing_schemes.
for (auto& scheme : schemes.csp_bypassing_schemes)
url::AddCSPBypassingScheme(scheme.c_str());
for (auto& scheme : schemes.empty_document_schemes)
url::AddEmptyDocumentScheme(scheme.c_str());
#if BUILDFLAG(IS_ANDROID)
if (schemes.allow_non_standard_schemes_in_origins)
url::EnableNonStandardSchemesForAndroidWebView();
#endif
for (auto& [scheme, handler] : schemes.predefined_handler_schemes)
url::AddPredefinedHandlerScheme(scheme.c_str(), handler.c_str());
// This should only be registered if the
// kEnableServiceWorkerForChrome or
// kEnableServiceWorkerForChromeUntrusted feature is enabled but checking it
// here causes a crash when --no-sandbox is enabled. See crbug.com/1313812
// There are other render side checks and browser side checks that ensure
// service workers don't work for chrome[-untrusted]:// when the flag is not
// enabled.
schemes.service_worker_schemes.push_back(kChromeUIScheme);
schemes.service_worker_schemes.push_back(kChromeUIUntrustedScheme);
// Prevent future modification of the scheme lists. This is to prevent
// accidental creation of data races in the program. Add*Scheme aren't
// threadsafe so must be called when GURL isn't used on any other thread. This
// is really easy to mess up, so we say that all calls to Add*Scheme in Chrome
// must be inside this function.
if (should_lock_registry)
url::LockSchemeRegistries();
// Combine the default savable schemes with the additional ones given.
GetMutableSavableSchemes().assign(std::begin(kDefaultSavableSchemes),
std::end(kDefaultSavableSchemes));
GetMutableSavableSchemes().insert(GetMutableSavableSchemes().end(),
schemes.savable_schemes.begin(),
schemes.savable_schemes.end());
GetMutableServiceWorkerSchemes() = std::move(schemes.service_worker_schemes);
}
void ReRegisterContentSchemesForTests() {
url::ClearSchemesForTests();
g_registered_url_schemes = false;
RegisterContentSchemes();
}
const std::vector<std::string>& GetSavableSchemes() {
return GetMutableSavableSchemes();
}
const std::vector<std::string>& GetServiceWorkerSchemes() {
return GetMutableServiceWorkerSchemes();
}
} // namespace content
|