perf: avoid protocol registry redundant lookup (#41991)

* perf: avoid redundant map lookup in SimpleURLLoaderWrapper::GetURLLoaderFactoryForURL()

* perf: avoid redundant map lookup in InspectableWebContents::LoadNetworkResource()

* refactor: remove unused ProtocolRegistry::IsProtocolRegistered()

refactor: remove unused ProtocolRegistry::IsProtocolIntercepted()

* refactor: remove unused ProtocolRegistry::handlers()

* refactor: rename ProtocolRegistry::FindIntercepted()

refactor: rename ProtocolRegistry::FindRegistered()

similar semantics to base::Value::Find*()

* chore: follow Google C++ brace style

chore: use same variable names as in main
This commit is contained in:
Charles Kerr 2024-05-09 08:53:09 -05:00 committed by GitHub
parent 865b0499bb
commit e2acdffe58
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 54 additions and 52 deletions

View file

@ -473,47 +473,43 @@ void SimpleURLLoaderWrapper::Cancel() {
}
scoped_refptr<network::SharedURLLoaderFactory>
SimpleURLLoaderWrapper::GetURLLoaderFactoryForURL(const GURL& url) {
if (electron::IsUtilityProcess()) {
if (electron::IsUtilityProcess())
return URLLoaderBundle::GetInstance()->GetSharedURLLoaderFactory();
}
CHECK(browser_context_);
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory;
auto* protocol_registry =
ProtocolRegistry::FromBrowserContext(browser_context_);
// Explicitly handle intercepted protocols here, even though
// ProxyingURLLoaderFactory would handle them later on, so that we can
// correctly intercept file:// scheme URLs.
bool bypass_custom_protocol_handlers =
request_options_ & kBypassCustomProtocolHandlers;
if (!bypass_custom_protocol_handlers &&
protocol_registry->IsProtocolIntercepted(url.scheme())) {
auto& protocol_handler =
protocol_registry->intercept_handlers().at(url.scheme());
mojo::PendingRemote<network::mojom::URLLoaderFactory> pending_remote =
ElectronURLLoaderFactory::Create(protocol_handler.first,
protocol_handler.second);
url_loader_factory = network::SharedURLLoaderFactory::Create(
std::make_unique<network::WrapperPendingSharedURLLoaderFactory>(
std::move(pending_remote)));
} else if (!bypass_custom_protocol_handlers &&
protocol_registry->IsProtocolRegistered(url.scheme())) {
auto& protocol_handler = protocol_registry->handlers().at(url.scheme());
mojo::PendingRemote<network::mojom::URLLoaderFactory> pending_remote =
ElectronURLLoaderFactory::Create(protocol_handler.first,
protocol_handler.second);
url_loader_factory = network::SharedURLLoaderFactory::Create(
std::make_unique<network::WrapperPendingSharedURLLoaderFactory>(
std::move(pending_remote)));
} else if (url.SchemeIsFile()) {
mojo::PendingRemote<network::mojom::URLLoaderFactory> pending_remote =
AsarURLLoaderFactory::Create();
url_loader_factory = network::SharedURLLoaderFactory::Create(
std::make_unique<network::WrapperPendingSharedURLLoaderFactory>(
std::move(pending_remote)));
} else {
url_loader_factory = browser_context_->GetURLLoaderFactory();
if (const bool bypass = request_options_ & kBypassCustomProtocolHandlers;
!bypass) {
const auto scheme = url.scheme();
const auto* const protocol_registry =
ProtocolRegistry::FromBrowserContext(browser_context_);
if (const auto* const protocol_handler =
protocol_registry->FindIntercepted(scheme)) {
return network::SharedURLLoaderFactory::Create(
std::make_unique<network::WrapperPendingSharedURLLoaderFactory>(
ElectronURLLoaderFactory::Create(protocol_handler->first,
protocol_handler->second)));
}
if (const auto* const protocol_handler =
protocol_registry->FindRegistered(scheme)) {
return network::SharedURLLoaderFactory::Create(
std::make_unique<network::WrapperPendingSharedURLLoaderFactory>(
ElectronURLLoaderFactory::Create(protocol_handler->first,
protocol_handler->second)));
}
}
return url_loader_factory;
if (url.SchemeIsFile()) {
return network::SharedURLLoaderFactory::Create(
std::make_unique<network::WrapperPendingSharedURLLoaderFactory>(
AsarURLLoaderFactory::Create()));
}
return browser_context_->GetURLLoaderFactory();
}
// static