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:
Charles Kerr 2024-08-23 17:15:45 -05:00 committed by GitHub
parent 7f34b0e6f5
commit 5a1eeea102
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 26 additions and 19 deletions

View file

@ -10,6 +10,7 @@
#include "base/command_line.h"
#include "base/environment.h"
#include "base/process/launch.h"
#include "base/strings/strcat.h"
#include "electron/electron_version.h"
#include "shell/browser/javascript_environment.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) {
const std::vector<std::string> argv = {
"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()));
}

View file

@ -95,7 +95,7 @@ bool IsValidCustomProtocol(const std::wstring& scheme) {
// (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.
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))
return std::wstring();

View file

@ -25,8 +25,9 @@ ElectronWebUIControllerFactory::~ElectronWebUIControllerFactory() = default;
content::WebUI::TypeID ElectronWebUIControllerFactory::GetWebUIType(
content::BrowserContext* browser_context,
const GURL& url) {
if (url.host() == chrome::kChromeUIDevToolsHost ||
url.host() == chrome::kChromeUIAccessibilityHost) {
if (const std::string_view host = url.host_piece();
host == chrome::kChromeUIDevToolsHost ||
host == chrome::kChromeUIAccessibilityHost) {
return const_cast<ElectronWebUIControllerFactory*>(this);
}
@ -43,13 +44,16 @@ std::unique_ptr<content::WebUIController>
ElectronWebUIControllerFactory::CreateWebUIControllerForURL(
content::WebUI* web_ui,
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();
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>();
}

View file

@ -53,8 +53,8 @@ using ProtocolHandler =
StartLoadingCallback)>;
// scheme => (type, handler).
using HandlersMap =
std::map<std::string, std::pair<ProtocolType, ProtocolHandler>>;
using HandlersMap = std::
map<std::string, std::pair<ProtocolType, ProtocolHandler>, std::less<>>;
// Implementation of URLLoaderFactory.
class ElectronURLLoaderFactory : public network::SelfDeletingURLLoaderFactory {

View file

@ -806,7 +806,7 @@ void ProxyingURLLoaderFactory::CreateLoaderAndStart(
bool bypass_custom_protocol_handlers =
options & kBypassCustomProtocolHandlers;
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()) {
mojo::PendingRemote<network::mojom::URLLoaderFactory> loader_remote;
this->Clone(loader_remote.InitWithNewPipeAndPassReceiver());

View file

@ -74,7 +74,7 @@ bool ProtocolRegistry::UnregisterProtocol(const std::string& scheme) {
}
const HandlersMap::mapped_type* ProtocolRegistry::FindRegistered(
const std::string& scheme) const {
const std::string_view scheme) const {
const auto& map = handlers_;
const auto iter = map.find(scheme);
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 std::string& scheme) const {
const std::string_view scheme) const {
const auto& map = intercept_handlers_;
const auto iter = map.find(scheme);
return iter != std::end(map) ? &iter->second : nullptr;

View file

@ -6,6 +6,7 @@
#define ELECTRON_SHELL_BROWSER_PROTOCOL_REGISTRY_H_
#include <string>
#include <string_view>
#include "content/public/browser/content_browser_client.h"
#include "shell/browser/net/electron_url_loader_factory.h"
@ -40,7 +41,7 @@ class ProtocolRegistry {
bool UnregisterProtocol(const std::string& scheme);
[[nodiscard]] const HandlersMap::mapped_type* FindRegistered(
const std::string& scheme) const;
std::string_view scheme) const;
bool InterceptProtocol(ProtocolType type,
const std::string& scheme,
@ -48,7 +49,7 @@ class ProtocolRegistry {
bool UninterceptProtocol(const std::string& scheme);
[[nodiscard]] const HandlersMap::mapped_type* FindIntercepted(
const std::string& scheme) const;
std::string_view scheme) const;
private:
friend class ElectronBrowserContext;

View file

@ -684,7 +684,7 @@ void InspectableWebContents::LoadNetworkResource(DispatchCallback callback,
std::make_unique<network::WrapperPendingSharedURLLoaderFactory>(
std::move(pending_remote)));
} else if (const auto* const protocol_handler =
protocol_registry->FindRegistered(gurl.scheme())) {
protocol_registry->FindRegistered(gurl.scheme_piece())) {
url_loader_factory = network::SharedURLLoaderFactory::Create(
std::make_unique<network::WrapperPendingSharedURLLoaderFactory>(
ElectronURLLoaderFactory::Create(protocol_handler->first,

View file

@ -186,15 +186,16 @@ void WebContentsZoomController::SetZoomMode(ZoomMode new_mode) {
if (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
// zoom level to match theirs. The temporary zoom level will be
// cleared below, but this call will make sure this tab re-draws at
// the correct 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;
zoom_map->SetTemporaryZoomLevel(rfh_id, origin_zoom_level);
} else {

View file

@ -492,7 +492,7 @@ SimpleURLLoaderWrapper::GetURLLoaderFactoryForURL(const GURL& url) {
// correctly intercept file:// scheme URLs.
if (const bool bypass = request_options_ & kBypassCustomProtocolHandlers;
!bypass) {
const auto scheme = url.scheme();
const std::string_view scheme = url.scheme_piece();
const auto* const protocol_registry =
ProtocolRegistry::FromBrowserContext(browser_context_);