Skip to content

Commit

Permalink
Speed up WebExtensionContext::getCurrentTab().
Browse files Browse the repository at this point in the history
https://webkit.org/b/270644
rdar://problem/124222506

Reviewed by Brian Weinstein.

Simplify the logic in getCurrentTab() by returning early if the page is
a know extension page, and IncludeExtensionViews::No. This prevents logging
an error "Tab for page XX was not found" and avoids looping through open tabs.

Also removed the deep nesting, and added gotos to skip to the end checks faster.

* Source/WebKit/UIProcess/Extensions/Cocoa/WebExtensionContextCocoa.mm:
(WebKit::WebExtensionContext::getCurrentTab const):

Canonical link: https://commits.webkit.org/275847@main
  • Loading branch information
xeenon committed Mar 8, 2024
1 parent 226f99e commit 5bf1b8f
Showing 1 changed file with 35 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1545,58 +1545,56 @@ static _WKWebExtensionContextError toAPI(WebExtensionContext::Error error)

RefPtr<WebExtensionTab> WebExtensionContext::getCurrentTab(WebPageProxyIdentifier webPageProxyIdentifier, IncludeExtensionViews includeExtensionViews, IgnoreExtensionAccess ignoreExtensionAccess) const
{
if (includeExtensionViews == IncludeExtensionViews::Yes && isBackgroundPage(webPageProxyIdentifier)) {
RefPtr<WebExtensionTab> result;

if (isBackgroundPage(webPageProxyIdentifier)) {
if (includeExtensionViews == IncludeExtensionViews::No)
return nullptr;

if (RefPtr window = frontmostWindow())
return window->activeTab();
return nullptr;
}
result = window->activeTab();

RefPtr<WebExtensionTab> result;
goto finish;
}

// Search actions for the page.
if (includeExtensionViews == IncludeExtensionViews::Yes) {
for (auto entry : m_popupPageActionMap) {
if (entry.key.identifier() != webPageProxyIdentifier)
continue;

RefPtr tab = entry.value->tab();
RefPtr window = tab ? tab->window() : entry.value->window();
for (auto entry : m_popupPageActionMap) {
if (entry.key.identifier() != webPageProxyIdentifier)
continue;

if (!tab && window)
tab = window->activeTab();
if (includeExtensionViews == IncludeExtensionViews::No)
return nullptr;

if (!tab)
continue;
RefPtr tab = entry.value->tab();
RefPtr window = tab ? tab->window() : entry.value->window();
if (!tab && window)
tab = window->activeTab();

result = tab;
break;
}
result = tab;
goto finish;
}

// Search open tabs for the page.
if (!result) {
for (auto entry : m_extensionPageTabMap) {
if (entry.key.identifier() == webPageProxyIdentifier) {
result = m_tabMap.get(entry.value);
break;
}
}
// Search extension tabs for the page.
for (auto entry : m_extensionPageTabMap) {
if (entry.key.identifier() != webPageProxyIdentifier)
continue;

result = m_tabMap.get(entry.value);
goto finish;
}

if (!result) {
for (Ref tab : openTabs()) {
for (WKWebView *webView in tab->webViews()) {
if (webView._page->identifier() == webPageProxyIdentifier) {
result = tab.ptr();
break;
}
}
// Search open tabs for the page.
for (Ref tab : openTabs()) {
for (WKWebView *webView in tab->webViews()) {
if (webView._page->identifier() != webPageProxyIdentifier)
continue;

if (result)
break;
result = tab.ptr();
goto finish;
}
}

finish:
if (!result) {
RELEASE_LOG_ERROR(Extensions, "Tab for page %{public}llu was not found", webPageProxyIdentifier.toUInt64());
return nullptr;
Expand Down

0 comments on commit 5bf1b8f

Please sign in to comment.