Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DBP: Fix memory leak on WebViewHandler #2483

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protocol WebViewHandler: NSObject {
func load(url: URL) async throws
func takeSnaphost(path: String, fileName: String) async throws
func saveHTML(path: String, fileName: String) async throws
func waitForWebViewLoad(timeoutInSeconds: Int) async throws
func waitForWebViewLoad() async throws
func finish() async
func execute(action: Action, data: CCFRequestData) async
func evaluateJavaScript(_ javaScript: String) async throws
Expand All @@ -38,7 +38,7 @@ final class DataBrokerProtectionWebViewHandler: NSObject, WebViewHandler {
private var activeContinuation: CheckedContinuation<Void, Error>?

private let isFakeBroker: Bool
private let webViewConfiguration: WKWebViewConfiguration
private var webViewConfiguration: WKWebViewConfiguration?
private var userContentController: DataBrokerUserContentController?

private var webView: WebView?
Expand All @@ -59,7 +59,11 @@ final class DataBrokerProtectionWebViewHandler: NSObject, WebViewHandler {
}

func initializeWebView(showWebView: Bool) async {
webView = WebView(frame: CGRect(origin: .zero, size: CGSize(width: 1024, height: 1024)), configuration: webViewConfiguration)
guard let configuration = self.webViewConfiguration else {
return
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add an assert here?

}

webView = WebView(frame: CGRect(origin: .zero, size: CGSize(width: 1024, height: 1024)), configuration: configuration)
webView?.navigationDelegate = self

if showWebView {
Expand All @@ -78,40 +82,30 @@ final class DataBrokerProtectionWebViewHandler: NSObject, WebViewHandler {
func load(url: URL) async throws {
webView?.load(url)
os_log("Loading URL: %@", log: .action, String(describing: url.absoluteString))
try await waitForWebViewLoad(timeoutInSeconds: 120)
try await waitForWebViewLoad()
}

func finish() {
os_log("WebViewHandler finished", log: .action)

webView?.stopLoading()
userContentController?.cleanUpBeforeClosing()
WKWebsiteDataStore.default().removeData(ofTypes: [WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeMemoryCache], modifiedSince: Date(timeIntervalSince1970: 0)) {
os_log("WKWebView data store deleted correctly", log: .action)
}

webViewConfiguration = nil
userContentController = nil
webView?.navigationDelegate = nil
webView = nil
}

deinit {
print("WebViewHandler Deinit")
os_log("WebViewHandler Deinit", log: .action)
}

func waitForWebViewLoad(timeoutInSeconds: Int = 0) async throws {
func waitForWebViewLoad() async throws {
try await withCheckedThrowingContinuation { continuation in
self.activeContinuation = continuation

if timeoutInSeconds > 0 {
Task {
try await Task.sleep(nanoseconds: UInt64(timeoutInSeconds) * NSEC_PER_SEC)
if self.activeContinuation != nil {
self.activeContinuation?.resume()
self.activeContinuation = nil
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ final class DebugScanOperation: DataBrokerOperation {
} else {
os_log("Releasing the web view", log: .action)
await webViewHandler?.finish() // If we executed all steps we release the web view
continuation = nil
webViewHandler = nil
}
}

Expand All @@ -178,4 +180,8 @@ final class DebugScanOperation: DataBrokerOperation {
await completeWith(error: error)
}
}

deinit {
os_log("DebugScanOperation Deinit", log: .action)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@ extension DataBrokerOperation {
switch actionType {
case .click:
stageCalculator?.fireOptOutFillForm()
try? await webViewHandler?.waitForWebViewLoad(timeoutInSeconds: 30)
try? await webViewHandler?.waitForWebViewLoad()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Bunn This is a collateral change of removing the timeout. We were doing similar for clicks, what I’m doing there now is waiting 10 seconds before tapping.

We will probably need the same retry function that is now on C-S-S with clicks, but because clicks are done in opt-out, I think it makes sense to do it after launch.

// We wait 10 seconds before tapping
try? await Task.sleep(nanoseconds: UInt64(10) * 1_000_000_000)
await executeNextStep()
case .fillForm:
stageCalculator?.fireOptOutFillForm()
Expand Down
Loading