feat: enable code cache for custom protocols (#40544)
This commit is contained in:
parent
85bc005cd6
commit
9aa73abe78
15 changed files with 526 additions and 6 deletions
|
@ -32,6 +32,9 @@ std::vector<std::string> g_standard_schemes;
|
|||
// List of registered custom streaming schemes.
|
||||
std::vector<std::string> g_streaming_schemes;
|
||||
|
||||
// Schemes that support V8 code cache.
|
||||
std::vector<std::string> g_code_cache_schemes;
|
||||
|
||||
struct SchemeOptions {
|
||||
bool standard = false;
|
||||
bool secure = false;
|
||||
|
@ -40,6 +43,7 @@ struct SchemeOptions {
|
|||
bool supportFetchAPI = false;
|
||||
bool corsEnabled = false;
|
||||
bool stream = false;
|
||||
bool codeCache = false;
|
||||
};
|
||||
|
||||
struct CustomScheme {
|
||||
|
@ -71,6 +75,7 @@ struct Converter<CustomScheme> {
|
|||
opt.Get("supportFetchAPI", &(out->options.supportFetchAPI));
|
||||
opt.Get("corsEnabled", &(out->options.corsEnabled));
|
||||
opt.Get("stream", &(out->options.stream));
|
||||
opt.Get("codeCache", &(out->options.codeCache));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -82,10 +87,14 @@ namespace electron::api {
|
|||
|
||||
gin::WrapperInfo Protocol::kWrapperInfo = {gin::kEmbedderNativeGin};
|
||||
|
||||
std::vector<std::string> GetStandardSchemes() {
|
||||
const std::vector<std::string>& GetStandardSchemes() {
|
||||
return g_standard_schemes;
|
||||
}
|
||||
|
||||
const std::vector<std::string>& GetCodeCacheSchemes() {
|
||||
return g_code_cache_schemes;
|
||||
}
|
||||
|
||||
void AddServiceWorkerScheme(const std::string& scheme) {
|
||||
// There is no API to add service worker scheme, but there is an API to
|
||||
// return const reference to the schemes vector.
|
||||
|
@ -104,6 +113,15 @@ void RegisterSchemesAsPrivileged(gin_helper::ErrorThrower thrower,
|
|||
return;
|
||||
}
|
||||
|
||||
for (const auto& custom_scheme : custom_schemes) {
|
||||
if (custom_scheme.options.codeCache && !custom_scheme.options.standard) {
|
||||
thrower.ThrowError(
|
||||
"Code cache can only be enabled when the custom scheme is registered "
|
||||
"as standard scheme.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::string> secure_schemes, cspbypassing_schemes, fetch_schemes,
|
||||
service_worker_schemes, cors_schemes;
|
||||
for (const auto& custom_scheme : custom_schemes) {
|
||||
|
@ -137,10 +155,16 @@ void RegisterSchemesAsPrivileged(gin_helper::ErrorThrower thrower,
|
|||
if (custom_scheme.options.stream) {
|
||||
g_streaming_schemes.push_back(custom_scheme.scheme);
|
||||
}
|
||||
if (custom_scheme.options.codeCache) {
|
||||
g_code_cache_schemes.push_back(custom_scheme.scheme);
|
||||
url::AddCodeCacheScheme(custom_scheme.scheme.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
const auto AppendSchemesToCmdLine = [](const char* switch_name,
|
||||
std::vector<std::string> schemes) {
|
||||
if (schemes.empty())
|
||||
return;
|
||||
// Add the schemes to command line switches, so child processes can also
|
||||
// register them.
|
||||
base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
|
||||
|
@ -158,6 +182,8 @@ void RegisterSchemesAsPrivileged(gin_helper::ErrorThrower thrower,
|
|||
g_standard_schemes);
|
||||
AppendSchemesToCmdLine(electron::switches::kStreamingSchemes,
|
||||
g_streaming_schemes);
|
||||
AppendSchemesToCmdLine(electron::switches::kCodeCacheSchemes,
|
||||
g_code_cache_schemes);
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
|
|
@ -22,7 +22,8 @@ class ProtocolRegistry;
|
|||
|
||||
namespace api {
|
||||
|
||||
std::vector<std::string> GetStandardSchemes();
|
||||
const std::vector<std::string>& GetStandardSchemes();
|
||||
const std::vector<std::string>& GetCodeCacheSchemes();
|
||||
|
||||
void AddServiceWorkerScheme(const std::string& scheme);
|
||||
|
||||
|
|
|
@ -526,7 +526,8 @@ void ElectronBrowserClient::AppendExtraCommandLineSwitches(
|
|||
switches::kStandardSchemes, switches::kEnableSandbox,
|
||||
switches::kSecureSchemes, switches::kBypassCSPSchemes,
|
||||
switches::kCORSSchemes, switches::kFetchSchemes,
|
||||
switches::kServiceWorkerSchemes, switches::kStreamingSchemes};
|
||||
switches::kServiceWorkerSchemes, switches::kStreamingSchemes,
|
||||
switches::kCodeCacheSchemes};
|
||||
command_line->CopySwitchesFrom(*base::CommandLine::ForCurrentProcess(),
|
||||
kCommonSwitchNames);
|
||||
if (process_type == ::switches::kUtilityProcess ||
|
||||
|
@ -694,7 +695,7 @@ ElectronBrowserClient::CreateWindowForVideoPictureInPicture(
|
|||
|
||||
void ElectronBrowserClient::GetAdditionalAllowedSchemesForFileSystem(
|
||||
std::vector<std::string>* additional_schemes) {
|
||||
auto schemes_list = api::GetStandardSchemes();
|
||||
const auto& schemes_list = api::GetStandardSchemes();
|
||||
if (!schemes_list.empty())
|
||||
additional_schemes->insert(additional_schemes->end(), schemes_list.begin(),
|
||||
schemes_list.end());
|
||||
|
|
|
@ -227,6 +227,9 @@ const char kCORSSchemes[] = "cors-schemes";
|
|||
// Register schemes as streaming responses.
|
||||
const char kStreamingSchemes[] = "streaming-schemes";
|
||||
|
||||
// Register schemes as supporting V8 code cache.
|
||||
const char kCodeCacheSchemes[] = "code-cache-schemes";
|
||||
|
||||
// The browser process app model ID
|
||||
const char kAppUserModelId[] = "app-user-model-id";
|
||||
|
||||
|
|
|
@ -113,6 +113,7 @@ extern const char kBypassCSPSchemes[];
|
|||
extern const char kFetchSchemes[];
|
||||
extern const char kCORSSchemes[];
|
||||
extern const char kStreamingSchemes[];
|
||||
extern const char kCodeCacheSchemes[];
|
||||
extern const char kAppUserModelId[];
|
||||
extern const char kAppPath[];
|
||||
|
||||
|
|
|
@ -276,6 +276,13 @@ void RendererClientBase::RenderThreadStarted() {
|
|||
blink::SchemeRegistry::RegisterURLSchemeAsBypassingContentSecurityPolicy(
|
||||
WTF::String::FromUTF8(scheme.data(), scheme.length()));
|
||||
|
||||
std::vector<std::string> code_cache_schemes_list =
|
||||
ParseSchemesCLISwitch(command_line, switches::kCodeCacheSchemes);
|
||||
for (const auto& scheme : code_cache_schemes_list) {
|
||||
blink::WebSecurityPolicy::RegisterURLSchemeAsCodeCacheWithHashing(
|
||||
blink::WebString::FromASCII(scheme));
|
||||
}
|
||||
|
||||
// Allow file scheme to handle service worker by default.
|
||||
// FIXME(zcbenz): Can this be moved elsewhere?
|
||||
if (electron::fuses::IsGrantFileProtocolExtraPrivilegesEnabled()) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue