perf: prefer GURL string_view getters (#43443)
* chore: avoid double-call to url.scheme() in WebContentsZoomController::SetZoomMode() * perf: use gurl.scheme_piece() in GetAppInfoHelperForProtocol() * perf: use gurl.scheme_piece() in Browser::GetApplicationNameForProtocol() * refactor: add std::less<> to HandlersMap This lets us search it using string_view keys * refactor: ProtocolRegistry::FindRegistered() now takes a std::string_view * perf: use gurl.scheme_piece() in InspectableWebContents::LoadNetworkResource() * refactor: ProtocolRegistry::FindIntercepted() now takes a std::string_view * perf: use gurl.scheme_piece() in SimpleURLLoaderWrapper::GetURLLoaderFactoryForURL() * perf: use gurl.scheme_piece() in ProxyingURLLoaderFactory::CreateLoaderAndStart() * perf: use gurl.host_piece() in ElectronWebUIControllerFactory::GetWebUIType() * perf: use gurl.host_piece() in ElectronWebUIControllerFactory::CreateWebUIControllerForURL()
This commit is contained in:
parent
7f34b0e6f5
commit
5a1eeea102
10 changed files with 26 additions and 19 deletions
|
@ -10,6 +10,7 @@
|
||||||
#include "base/command_line.h"
|
#include "base/command_line.h"
|
||||||
#include "base/environment.h"
|
#include "base/environment.h"
|
||||||
#include "base/process/launch.h"
|
#include "base/process/launch.h"
|
||||||
|
#include "base/strings/strcat.h"
|
||||||
#include "electron/electron_version.h"
|
#include "electron/electron_version.h"
|
||||||
#include "shell/browser/javascript_environment.h"
|
#include "shell/browser/javascript_environment.h"
|
||||||
#include "shell/browser/native_window.h"
|
#include "shell/browser/native_window.h"
|
||||||
|
@ -124,7 +125,7 @@ bool Browser::RemoveAsDefaultProtocolClient(const std::string& protocol,
|
||||||
std::u16string Browser::GetApplicationNameForProtocol(const GURL& url) {
|
std::u16string Browser::GetApplicationNameForProtocol(const GURL& url) {
|
||||||
const std::vector<std::string> argv = {
|
const std::vector<std::string> argv = {
|
||||||
"xdg-mime", "query", "default",
|
"xdg-mime", "query", "default",
|
||||||
std::string("x-scheme-handler/") + url.scheme()};
|
base::StrCat({"x-scheme-handler/", url.scheme_piece()})};
|
||||||
|
|
||||||
return base::ASCIIToUTF16(GetXdgAppOutput(argv).value_or(std::string()));
|
return base::ASCIIToUTF16(GetXdgAppOutput(argv).value_or(std::string()));
|
||||||
}
|
}
|
||||||
|
|
|
@ -95,7 +95,7 @@ bool IsValidCustomProtocol(const std::wstring& scheme) {
|
||||||
// (https://docs.microsoft.com/en-us/windows/win32/api/shlwapi/ne-shlwapi-assocstr)
|
// (https://docs.microsoft.com/en-us/windows/win32/api/shlwapi/ne-shlwapi-assocstr)
|
||||||
// and returns the application name, icon and path that handles the protocol.
|
// and returns the application name, icon and path that handles the protocol.
|
||||||
std::wstring GetAppInfoHelperForProtocol(ASSOCSTR assoc_str, const GURL& url) {
|
std::wstring GetAppInfoHelperForProtocol(ASSOCSTR assoc_str, const GURL& url) {
|
||||||
const std::wstring url_scheme = base::ASCIIToWide(url.scheme());
|
const std::wstring url_scheme = base::ASCIIToWide(url.scheme_piece());
|
||||||
if (!IsValidCustomProtocol(url_scheme))
|
if (!IsValidCustomProtocol(url_scheme))
|
||||||
return std::wstring();
|
return std::wstring();
|
||||||
|
|
||||||
|
|
|
@ -25,8 +25,9 @@ ElectronWebUIControllerFactory::~ElectronWebUIControllerFactory() = default;
|
||||||
content::WebUI::TypeID ElectronWebUIControllerFactory::GetWebUIType(
|
content::WebUI::TypeID ElectronWebUIControllerFactory::GetWebUIType(
|
||||||
content::BrowserContext* browser_context,
|
content::BrowserContext* browser_context,
|
||||||
const GURL& url) {
|
const GURL& url) {
|
||||||
if (url.host() == chrome::kChromeUIDevToolsHost ||
|
if (const std::string_view host = url.host_piece();
|
||||||
url.host() == chrome::kChromeUIAccessibilityHost) {
|
host == chrome::kChromeUIDevToolsHost ||
|
||||||
|
host == chrome::kChromeUIAccessibilityHost) {
|
||||||
return const_cast<ElectronWebUIControllerFactory*>(this);
|
return const_cast<ElectronWebUIControllerFactory*>(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,13 +44,16 @@ std::unique_ptr<content::WebUIController>
|
||||||
ElectronWebUIControllerFactory::CreateWebUIControllerForURL(
|
ElectronWebUIControllerFactory::CreateWebUIControllerForURL(
|
||||||
content::WebUI* web_ui,
|
content::WebUI* web_ui,
|
||||||
const GURL& url) {
|
const GURL& url) {
|
||||||
if (url.host() == chrome::kChromeUIDevToolsHost) {
|
const std::string_view host = url.host_piece();
|
||||||
|
|
||||||
|
if (host == chrome::kChromeUIDevToolsHost) {
|
||||||
auto* browser_context = web_ui->GetWebContents()->GetBrowserContext();
|
auto* browser_context = web_ui->GetWebContents()->GetBrowserContext();
|
||||||
return std::make_unique<DevToolsUI>(browser_context, web_ui);
|
return std::make_unique<DevToolsUI>(browser_context, web_ui);
|
||||||
} else if (url.host() == chrome::kChromeUIAccessibilityHost) {
|
|
||||||
return std::make_unique<ElectronAccessibilityUI>(web_ui);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (host == chrome::kChromeUIAccessibilityHost)
|
||||||
|
return std::make_unique<ElectronAccessibilityUI>(web_ui);
|
||||||
|
|
||||||
return std::unique_ptr<content::WebUIController>();
|
return std::unique_ptr<content::WebUIController>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -53,8 +53,8 @@ using ProtocolHandler =
|
||||||
StartLoadingCallback)>;
|
StartLoadingCallback)>;
|
||||||
|
|
||||||
// scheme => (type, handler).
|
// scheme => (type, handler).
|
||||||
using HandlersMap =
|
using HandlersMap = std::
|
||||||
std::map<std::string, std::pair<ProtocolType, ProtocolHandler>>;
|
map<std::string, std::pair<ProtocolType, ProtocolHandler>, std::less<>>;
|
||||||
|
|
||||||
// Implementation of URLLoaderFactory.
|
// Implementation of URLLoaderFactory.
|
||||||
class ElectronURLLoaderFactory : public network::SelfDeletingURLLoaderFactory {
|
class ElectronURLLoaderFactory : public network::SelfDeletingURLLoaderFactory {
|
||||||
|
|
|
@ -806,7 +806,7 @@ void ProxyingURLLoaderFactory::CreateLoaderAndStart(
|
||||||
bool bypass_custom_protocol_handlers =
|
bool bypass_custom_protocol_handlers =
|
||||||
options & kBypassCustomProtocolHandlers;
|
options & kBypassCustomProtocolHandlers;
|
||||||
if (!bypass_custom_protocol_handlers) {
|
if (!bypass_custom_protocol_handlers) {
|
||||||
auto it = intercepted_handlers_->find(request.url.scheme());
|
auto it = intercepted_handlers_->find(request.url.scheme_piece());
|
||||||
if (it != intercepted_handlers_->end()) {
|
if (it != intercepted_handlers_->end()) {
|
||||||
mojo::PendingRemote<network::mojom::URLLoaderFactory> loader_remote;
|
mojo::PendingRemote<network::mojom::URLLoaderFactory> loader_remote;
|
||||||
this->Clone(loader_remote.InitWithNewPipeAndPassReceiver());
|
this->Clone(loader_remote.InitWithNewPipeAndPassReceiver());
|
||||||
|
|
|
@ -74,7 +74,7 @@ bool ProtocolRegistry::UnregisterProtocol(const std::string& scheme) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const HandlersMap::mapped_type* ProtocolRegistry::FindRegistered(
|
const HandlersMap::mapped_type* ProtocolRegistry::FindRegistered(
|
||||||
const std::string& scheme) const {
|
const std::string_view scheme) const {
|
||||||
const auto& map = handlers_;
|
const auto& map = handlers_;
|
||||||
const auto iter = map.find(scheme);
|
const auto iter = map.find(scheme);
|
||||||
return iter != std::end(map) ? &iter->second : nullptr;
|
return iter != std::end(map) ? &iter->second : nullptr;
|
||||||
|
@ -91,7 +91,7 @@ bool ProtocolRegistry::UninterceptProtocol(const std::string& scheme) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const HandlersMap::mapped_type* ProtocolRegistry::FindIntercepted(
|
const HandlersMap::mapped_type* ProtocolRegistry::FindIntercepted(
|
||||||
const std::string& scheme) const {
|
const std::string_view scheme) const {
|
||||||
const auto& map = intercept_handlers_;
|
const auto& map = intercept_handlers_;
|
||||||
const auto iter = map.find(scheme);
|
const auto iter = map.find(scheme);
|
||||||
return iter != std::end(map) ? &iter->second : nullptr;
|
return iter != std::end(map) ? &iter->second : nullptr;
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
#define ELECTRON_SHELL_BROWSER_PROTOCOL_REGISTRY_H_
|
#define ELECTRON_SHELL_BROWSER_PROTOCOL_REGISTRY_H_
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
#include "content/public/browser/content_browser_client.h"
|
#include "content/public/browser/content_browser_client.h"
|
||||||
#include "shell/browser/net/electron_url_loader_factory.h"
|
#include "shell/browser/net/electron_url_loader_factory.h"
|
||||||
|
@ -40,7 +41,7 @@ class ProtocolRegistry {
|
||||||
bool UnregisterProtocol(const std::string& scheme);
|
bool UnregisterProtocol(const std::string& scheme);
|
||||||
|
|
||||||
[[nodiscard]] const HandlersMap::mapped_type* FindRegistered(
|
[[nodiscard]] const HandlersMap::mapped_type* FindRegistered(
|
||||||
const std::string& scheme) const;
|
std::string_view scheme) const;
|
||||||
|
|
||||||
bool InterceptProtocol(ProtocolType type,
|
bool InterceptProtocol(ProtocolType type,
|
||||||
const std::string& scheme,
|
const std::string& scheme,
|
||||||
|
@ -48,7 +49,7 @@ class ProtocolRegistry {
|
||||||
bool UninterceptProtocol(const std::string& scheme);
|
bool UninterceptProtocol(const std::string& scheme);
|
||||||
|
|
||||||
[[nodiscard]] const HandlersMap::mapped_type* FindIntercepted(
|
[[nodiscard]] const HandlersMap::mapped_type* FindIntercepted(
|
||||||
const std::string& scheme) const;
|
std::string_view scheme) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class ElectronBrowserContext;
|
friend class ElectronBrowserContext;
|
||||||
|
|
|
@ -684,7 +684,7 @@ void InspectableWebContents::LoadNetworkResource(DispatchCallback callback,
|
||||||
std::make_unique<network::WrapperPendingSharedURLLoaderFactory>(
|
std::make_unique<network::WrapperPendingSharedURLLoaderFactory>(
|
||||||
std::move(pending_remote)));
|
std::move(pending_remote)));
|
||||||
} else if (const auto* const protocol_handler =
|
} else if (const auto* const protocol_handler =
|
||||||
protocol_registry->FindRegistered(gurl.scheme())) {
|
protocol_registry->FindRegistered(gurl.scheme_piece())) {
|
||||||
url_loader_factory = network::SharedURLLoaderFactory::Create(
|
url_loader_factory = network::SharedURLLoaderFactory::Create(
|
||||||
std::make_unique<network::WrapperPendingSharedURLLoaderFactory>(
|
std::make_unique<network::WrapperPendingSharedURLLoaderFactory>(
|
||||||
ElectronURLLoaderFactory::Create(protocol_handler->first,
|
ElectronURLLoaderFactory::Create(protocol_handler->first,
|
||||||
|
|
|
@ -186,15 +186,16 @@ void WebContentsZoomController::SetZoomMode(ZoomMode new_mode) {
|
||||||
|
|
||||||
if (entry) {
|
if (entry) {
|
||||||
GURL url = content::HostZoomMap::GetURLFromEntry(entry);
|
GURL url = content::HostZoomMap::GetURLFromEntry(entry);
|
||||||
std::string host = net::GetHostOrSpecFromURL(url);
|
const std::string host = net::GetHostOrSpecFromURL(url);
|
||||||
|
const std::string scheme = url.scheme();
|
||||||
|
|
||||||
if (zoom_map->HasZoomLevel(url.scheme(), host)) {
|
if (zoom_map->HasZoomLevel(scheme, host)) {
|
||||||
// If there are other tabs with the same origin, then set this tab's
|
// If there are other tabs with the same origin, then set this tab's
|
||||||
// zoom level to match theirs. The temporary zoom level will be
|
// zoom level to match theirs. The temporary zoom level will be
|
||||||
// cleared below, but this call will make sure this tab re-draws at
|
// cleared below, but this call will make sure this tab re-draws at
|
||||||
// the correct zoom level.
|
// the correct zoom level.
|
||||||
double origin_zoom_level =
|
double origin_zoom_level =
|
||||||
zoom_map->GetZoomLevelForHostAndScheme(url.scheme(), host);
|
zoom_map->GetZoomLevelForHostAndScheme(scheme, host);
|
||||||
event_data_->new_zoom_level = origin_zoom_level;
|
event_data_->new_zoom_level = origin_zoom_level;
|
||||||
zoom_map->SetTemporaryZoomLevel(rfh_id, origin_zoom_level);
|
zoom_map->SetTemporaryZoomLevel(rfh_id, origin_zoom_level);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -492,7 +492,7 @@ SimpleURLLoaderWrapper::GetURLLoaderFactoryForURL(const GURL& url) {
|
||||||
// correctly intercept file:// scheme URLs.
|
// correctly intercept file:// scheme URLs.
|
||||||
if (const bool bypass = request_options_ & kBypassCustomProtocolHandlers;
|
if (const bool bypass = request_options_ & kBypassCustomProtocolHandlers;
|
||||||
!bypass) {
|
!bypass) {
|
||||||
const auto scheme = url.scheme();
|
const std::string_view scheme = url.scheme_piece();
|
||||||
const auto* const protocol_registry =
|
const auto* const protocol_registry =
|
||||||
ProtocolRegistry::FromBrowserContext(browser_context_);
|
ProtocolRegistry::FromBrowserContext(browser_context_);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue