This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
4 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...ave/Frontend/Browser/BrowserViewController/BrowserViewController+WKDownloadDelegate.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright 2021 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
import Foundation | ||
import WebKit | ||
import PassKit | ||
import Shared | ||
|
||
extension BrowserViewController: WKDownloadDelegate { | ||
|
||
struct PendingDownload: Hashable, Equatable { | ||
let fileURL: URL | ||
let response: URLResponse | ||
let suggestedFileName: String | ||
} | ||
|
||
public func download(_ download: WKDownload, decideDestinationUsing response: URLResponse, suggestedFilename: String) async -> URL? { | ||
let temporaryDir = NSTemporaryDirectory() | ||
let fileName = temporaryDir + "/" + suggestedFilename | ||
let url = URL(fileURLWithPath: fileName) | ||
pendingDownloads[download] = PendingDownload(fileURL: url, response: response, suggestedFileName: suggestedFilename) | ||
return url | ||
} | ||
|
||
public func download(_ download: WKDownload, decidedPolicyForHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest) async -> WKDownload.RedirectPolicy { | ||
return .allow | ||
} | ||
|
||
public func download(_ download: WKDownload, respondTo challenge: URLAuthenticationChallenge) async -> (URLSession.AuthChallengeDisposition, URLCredential?) { | ||
return (.performDefaultHandling, nil) | ||
} | ||
|
||
@MainActor | ||
public func downloadDidFinish(_ download: WKDownload) { | ||
guard let downloadInfo = pendingDownloads[download] else { | ||
return | ||
} | ||
|
||
pendingDownloads.removeValue(forKey: download) | ||
|
||
let response = URLResponse(url: downloadInfo.fileURL, | ||
mimeType: downloadInfo.response.mimeType, | ||
expectedContentLength: Int(downloadInfo.response.expectedContentLength), | ||
textEncodingName: downloadInfo.response.textEncodingName) | ||
|
||
if let passbookHelper = OpenPassBookHelper(request: nil, response: response, canShowInWebView: false, forceDownload: false, browserViewController: self) { | ||
passbookHelper.open() | ||
} | ||
|
||
Task { | ||
try FileManager.default.removeItem(at: downloadInfo.fileURL) | ||
} | ||
} | ||
|
||
@MainActor | ||
public func download(_ download: WKDownload, didFailWithError error: Error, resumeData: Data?) { | ||
// display an error | ||
let alertController = UIAlertController( | ||
title: Strings.unableToAddPassErrorTitle, | ||
message: Strings.unableToAddPassErrorMessage, | ||
preferredStyle: .alert) | ||
alertController.addAction( | ||
UIAlertAction(title: Strings.unableToAddPassErrorDismiss, style: .cancel) { (action) in | ||
// Do nothing. | ||
} | ||
) | ||
present(alertController, animated: true, completion: nil) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters