Skip to content

Commit

Permalink
Fix brave/brave-ios#8661: Always allow WebKit to load popup URLs befo…
Browse files Browse the repository at this point in the history
…re switching tabs (brave/brave-ios#8683)

There was a race condition on older devices that where WebKit would not load the popup URL before Brave attempted to select the tab. Selecting the tab would then attempt to restore it using the tab's current URL which would be `about:blank` since the web view had not made any requests yet.
  • Loading branch information
kylehickinson authored Jan 22, 2024
1 parent 140d84c commit 43c9775
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,16 @@ extension BrowserViewController: WKUIDelegate {
newTab.url = URL(string: "about:blank")

toolbarVisibilityViewModel.toolbarState = .expanded

// Wait until WebKit starts the request before selecting the new tab, otherwise the tab manager may
// restore it as if it was a dead tab.
var observation: NSKeyValueObservation?
observation = newTab.webView?.observe(\.url, changeHandler: { [weak self] webView, _ in
_ = observation // Silence write but not read warning
observation = nil
guard let self = self, let tab = self.tabManager[webView] else { return }
self.tabManager.selectTab(tab)
})

return newTab.webView
}
Expand Down
8 changes: 0 additions & 8 deletions Sources/Brave/Frontend/Browser/TabManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -411,14 +411,6 @@ class TabManager: NSObject {
@MainActor func addPopupForParentTab(_ parentTab: Tab, configuration: WKWebViewConfiguration) -> Tab {
let popup = Tab(configuration: configuration, id: UUID(), type: parentTab.type, tabGeneratorAPI: tabGeneratorAPI)
configureTab(popup, request: nil, afterTab: parentTab, flushToDisk: true, zombie: false, isPopup: true)

// Wait momentarily before selecting the new tab, otherwise the parent tab
// may be unable to set `window.location` on the popup immediately after
// calling `window.open("")`.
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) {
self.selectTab(popup)
}

return popup
}

Expand Down

0 comments on commit 43c9775

Please sign in to comment.