Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #8060: Increase performance of closing tabs (#8061)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon-T authored and iccub committed Sep 22, 2023
1 parent 951cffc commit 825a7a3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ extension BrowserViewController: TabManagerDelegate {
let alert = UIAlertController(title: nil, message: Strings.closeAllTabsPrompt, preferredStyle: .actionSheet)
let cancelAction = UIAlertAction(title: Strings.CancelString, style: .cancel)
let closedTabsTitle = String(format: Strings.closeAllTabsTitle, tabManager.tabsForCurrentMode.count)
let closeAllAction = UIAlertAction(title: closedTabsTitle, style: .destructive) { _ in
let closeAllAction = UIAlertAction(title: closedTabsTitle, style: .destructive) { [unowned self] _ in
if !privateBrowsingManager.isPrivateBrowsing {
// Add the tab information to recently closed before removing
tabManager.addAllTabsToRecentlyClosed()
Expand Down
8 changes: 4 additions & 4 deletions Sources/Brave/Frontend/Browser/Helpers/ScreenshotHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ class ScreenshotHelper {
// This is for a bug in certain iOS 13 versions, snapshots cannot be taken correctly without this boolean being set
configuration.afterScreenUpdates = false

webView.takeSnapshot(with: configuration) { image, error in
webView.takeSnapshot(with: configuration) { [weak tab] image, error in
if let image = image {
tab.setScreenshot(image)
tab?.setScreenshot(image)
} else if let error = error {
Logger.module.error("\(error.localizedDescription)")
tab.setScreenshot(nil)
tab?.setScreenshot(nil)
} else {
Logger.module.error("Cannot snapshot Tab Screenshot - No error description")
tab.setScreenshot(nil)
tab?.setScreenshot(nil)
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/Brave/Frontend/Browser/Tab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ class Tab: NSObject {
var onPageReadyStateChanged: ((ReadyState.State) -> Void)?

// If this tab has been opened from another, its parent will point to the tab from which it was opened
var parent: Tab?
weak var parent: Tab?

fileprivate var contentScriptManager = TabContentScriptManager()
private var userScripts = Set<UserScriptManager.ScriptType>()
Expand Down Expand Up @@ -420,6 +420,7 @@ class Tab: NSObject {
}

// Remove the tab history from saved tabs
// To remove history from WebKit, we simply update the session data AFTER calling "clear" above
SessionTab.update(tabId: id, interactionState: webView.sessionData ?? Data(), title: title, url: webView.url ?? TabManager.ntpInteralURL)
}

Expand Down
26 changes: 12 additions & 14 deletions Sources/Brave/Frontend/Browser/TabManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,9 @@ class TabManager: NSObject {
_selectedIndex = -1
}

preserveScreenshots()
if let previousTab = previous {
preserveScreenshot(for: previousTab)
}

if let t = selectedTab, t.webView == nil {
selectedTab?.createWebview()
Expand Down Expand Up @@ -564,14 +566,14 @@ class TabManager: NSObject {
tab.webStateDebounceTimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false) { [weak self, weak tab] _ in
guard let self = self, let tab = tab else { return }
tab.webStateDebounceTimer?.invalidate()

if state == .complete || state == .loaded || state == .pushstate || state == .popstate || state == .replacestate {

if Preferences.Privacy.privateBrowsingOnly.value || (tab.isPrivate && !Preferences.Privacy.persistentPrivateBrowsing.value) {
return
}
self.preserveScreenshots()

self.preserveScreenshot(for: tab)
self.saveTab(tab)
}
}
Expand Down Expand Up @@ -907,16 +909,12 @@ class TabManager: NSObject {
}
}

private func preserveScreenshots() {
private func preserveScreenshot(for tab: Tab) {
assert(Thread.isMainThread)
if isRestoring { return }

Task { @MainActor in
for tab in allTabs {
guard let screenshot = tab.screenshot else { continue }
SessionTab.updateScreenshot(tabId: tab.id, screenshot: screenshot)
}
}

guard let screenshot = tab.screenshot else { return }
SessionTab.updateScreenshot(tabId: tab.id, screenshot: screenshot)
}

@MainActor fileprivate var restoreTabsInternal: Tab? {
Expand Down Expand Up @@ -1186,7 +1184,7 @@ extension TabManager: WKNavigationDelegate {
return
}

preserveScreenshots()
preserveScreenshot(for: tab)
saveTab(tab)
}
}
Expand Down

0 comments on commit 825a7a3

Please sign in to comment.