cef/libcef/common/net/scheme_registration.cc

101 lines
3.3 KiB
C++
Raw Normal View History

// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
#include "libcef/common/net/scheme_registration.h"
#include "libcef/common/app_manager.h"
#include "libcef/common/net/scheme_info.h"
#include "libcef/features/runtime.h"
#include "base/containers/contains.h"
#include "content/public/common/url_constants.h"
#include "extensions/common/constants.h"
#include "url/url_constants.h"
Implement NetworkService request interception/handling (see issue #2622). Implementation notes: - Chromium change: CookieMonster::SetCookieableSchemes needs to be called immediately after the CookieMonster is created in NetworkContext:: ApplyContextParamsToBuilder. Add a Profile::GetCookieableSchemes method and NetworkContextParams.cookieable_schemes member (set from ProfileNetworkContextService::CreateNetworkContextParams) to support that. - Chromium change: Add a ContentBrowserClient::HandleExternalProtocol variant that exposes additional NetworkService request information. - GetResourceResponseFilter is not yet implemented. API changes: - Resource-related callbacks have been moved from CefRequestHandler to a new CefResourceRequestHandler interface which is returned via the GetResourceRequestHandler method. If the CefRequestHandler declines to handle a resource it can optionally be handled by the CefRequestContextHandler, if any, associated with the loading context. - The OnProtocolExecution callback has been moved from CefRequestHandler to CefResourceRequestHandler and will be called if a custom scheme request is unhandled. - Cookie send/save permission callbacks have been moved from CefRequestHandler and CefResourceHandler to CefResourceRequestHandler. - New methods added to CefResourceHandler that better match NetworkService execution sequence expectations. The old methods are now deprecated. - New methods added to CefRequest and CefResponse. Known behavior changes with the NetworkService implementation: - Modifying the |new_url| parameter in OnResourceRedirect will no longer result in the method being called an additional time (likely a bug in the old implementation). - Modifying the request URL in OnResourceResponse would previously cause a redirect. This behavior is now deprecated because the NetworkService does not support this functionality when using default network loaders. Temporary support has been added in combination with CefResourceHandler usage only. - Other changes to the request object in OnResourceResponse will now cause the request to be restarted. This means that OnBeforeResourceLoad, etc, will be called an additional time with the new request information. - CefResponse::GetMimeType will now be empty for non-200 responses. - Requests using custom schemes can now be handled via CefResourceRequestHandler with the same callback behavior as builtin schemes. - Redirects of custom scheme requests will now be followed as expected. - Default handling of builtin schemes can now be disabled by setting |disable_default_handling| to true in GetResourceRequestHandler. - Unhandled requests (custom scheme or builtin scheme with default handling disabled) will fail with an CefResponse::GetError value of ERR_UNKNOWN_URL_SCHEME. - The CefSchemeHandlerFactory::Create callback will now include cookie headers. To test: - Run `cefclient --enable-network-service`. All resources should load successfully (this tests the transparent proxy capability). - All tests pass with NetworkService disabled. - The following tests pass with NetworkService enabled: - CookieTest.* - FrameTest.* (excluding .*Nav) - NavigationTest.* (excluding .Redirect*) - RequestHandlerTest.* - RequestContextTest.Basic* - RequestContextTest.Popup* - RequestTest.* - ResourceManagerTest.* - ResourceRequestHandlerTest.* (excluding .Filter*) - SchemeHandlerTest.* - StreamResourceHandlerTest.*
2019-04-24 04:50:25 +02:00
#include "url/url_util.h"
namespace scheme {
void AddInternalSchemes(content::ContentClient::Schemes* schemes) {
2023-01-02 23:59:03 +01:00
if (!cef::IsAlloyRuntimeEnabled()) {
return;
2023-01-02 23:59:03 +01:00
}
// chrome: and chrome-devtools: schemes are registered in
// RenderThreadImpl::RegisterSchemes().
// Access restrictions for chrome-extension: and chrome-extension-resource:
// schemes will be applied in AlloyContentRendererClient::WillSendRequest().
static CefSchemeInfo internal_schemes[] = {
{
extensions::kExtensionScheme, true, /* is_standard */
false, /* is_local */
false, /* is_display_isolated */
true, /* is_secure */
true, /* is_cors_enabled */
true, /* is_csp_bypassing */
},
};
// The |is_display_isolated| value is excluded here because it's registered
// with Blink only.
for (size_t i = 0; i < sizeof(internal_schemes) / sizeof(internal_schemes[0]);
++i) {
2023-01-02 23:59:03 +01:00
if (internal_schemes[i].is_standard) {
schemes->standard_schemes.push_back(internal_schemes[i].scheme_name);
2023-01-02 23:59:03 +01:00
}
if (internal_schemes[i].is_local) {
schemes->local_schemes.push_back(internal_schemes[i].scheme_name);
2023-01-02 23:59:03 +01:00
}
if (internal_schemes[i].is_secure) {
schemes->secure_schemes.push_back(internal_schemes[i].scheme_name);
2023-01-02 23:59:03 +01:00
}
if (internal_schemes[i].is_cors_enabled) {
schemes->cors_enabled_schemes.push_back(internal_schemes[i].scheme_name);
2023-01-02 23:59:03 +01:00
}
if (internal_schemes[i].is_csp_bypassing) {
schemes->csp_bypassing_schemes.push_back(internal_schemes[i].scheme_name);
2023-01-02 23:59:03 +01:00
}
CefAppManager::Get()->AddCustomScheme(&internal_schemes[i]);
}
}
bool IsInternalHandledScheme(const std::string& scheme) {
static const char* schemes[] = {
url::kAboutScheme,
url::kBlobScheme,
content::kChromeDevToolsScheme,
content::kChromeUIScheme,
content::kChromeUIUntrustedScheme,
url::kDataScheme,
extensions::kExtensionScheme,
url::kFileScheme,
url::kFileSystemScheme,
url::kHttpScheme,
url::kHttpsScheme,
url::kJavaScriptScheme,
url::kWsScheme,
url::kWssScheme,
};
for (size_t i = 0; i < sizeof(schemes) / sizeof(schemes[0]); ++i) {
2023-01-02 23:59:03 +01:00
if (scheme == schemes[i]) {
return true;
2023-01-02 23:59:03 +01:00
}
}
return false;
}
Implement NetworkService request interception/handling (see issue #2622). Implementation notes: - Chromium change: CookieMonster::SetCookieableSchemes needs to be called immediately after the CookieMonster is created in NetworkContext:: ApplyContextParamsToBuilder. Add a Profile::GetCookieableSchemes method and NetworkContextParams.cookieable_schemes member (set from ProfileNetworkContextService::CreateNetworkContextParams) to support that. - Chromium change: Add a ContentBrowserClient::HandleExternalProtocol variant that exposes additional NetworkService request information. - GetResourceResponseFilter is not yet implemented. API changes: - Resource-related callbacks have been moved from CefRequestHandler to a new CefResourceRequestHandler interface which is returned via the GetResourceRequestHandler method. If the CefRequestHandler declines to handle a resource it can optionally be handled by the CefRequestContextHandler, if any, associated with the loading context. - The OnProtocolExecution callback has been moved from CefRequestHandler to CefResourceRequestHandler and will be called if a custom scheme request is unhandled. - Cookie send/save permission callbacks have been moved from CefRequestHandler and CefResourceHandler to CefResourceRequestHandler. - New methods added to CefResourceHandler that better match NetworkService execution sequence expectations. The old methods are now deprecated. - New methods added to CefRequest and CefResponse. Known behavior changes with the NetworkService implementation: - Modifying the |new_url| parameter in OnResourceRedirect will no longer result in the method being called an additional time (likely a bug in the old implementation). - Modifying the request URL in OnResourceResponse would previously cause a redirect. This behavior is now deprecated because the NetworkService does not support this functionality when using default network loaders. Temporary support has been added in combination with CefResourceHandler usage only. - Other changes to the request object in OnResourceResponse will now cause the request to be restarted. This means that OnBeforeResourceLoad, etc, will be called an additional time with the new request information. - CefResponse::GetMimeType will now be empty for non-200 responses. - Requests using custom schemes can now be handled via CefResourceRequestHandler with the same callback behavior as builtin schemes. - Redirects of custom scheme requests will now be followed as expected. - Default handling of builtin schemes can now be disabled by setting |disable_default_handling| to true in GetResourceRequestHandler. - Unhandled requests (custom scheme or builtin scheme with default handling disabled) will fail with an CefResponse::GetError value of ERR_UNKNOWN_URL_SCHEME. - The CefSchemeHandlerFactory::Create callback will now include cookie headers. To test: - Run `cefclient --enable-network-service`. All resources should load successfully (this tests the transparent proxy capability). - All tests pass with NetworkService disabled. - The following tests pass with NetworkService enabled: - CookieTest.* - FrameTest.* (excluding .*Nav) - NavigationTest.* (excluding .Redirect*) - RequestHandlerTest.* - RequestContextTest.Basic* - RequestContextTest.Popup* - RequestTest.* - ResourceManagerTest.* - ResourceRequestHandlerTest.* (excluding .Filter*) - SchemeHandlerTest.* - StreamResourceHandlerTest.*
2019-04-24 04:50:25 +02:00
bool IsStandardScheme(const std::string& scheme) {
url::Component scheme_comp(0, scheme.length());
return url::IsStandard(scheme.c_str(), scheme_comp);
}
// Should return the same value as SecurityOrigin::isLocal and
// SchemeRegistry::shouldTreatURLSchemeAsCorsEnabled.
bool IsCorsEnabledScheme(const std::string& scheme) {
return base::Contains(url::GetCorsEnabledSchemes(), scheme);
}
} // namespace scheme