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.
Fixed a background thread issue where "displayTitle" is being called on WKWebView on a non-main thread causing the PrintPreview to have undefined-behaviour. Fixed opening Authenticated documents in Private-Browsing mode when it opens in a new tab. Fixed opening Authenticated documents and attempting to share, or print them. Fixes tabs not having the same data store as other tabs (IE: Log-In to any website on one tab should log you in on another tab). Fixed destroying of data-store to only happen when ALL tabs are destroyed. Changed Temporary Storage to pull out the document from the WebPage instead of making server calls outside the webview's session (IE: No need for cookie injecting, etc).. Instead, we get the webview to give us the document via Javascript injection which passes the document to iOS for display/saving, etc..
- Loading branch information
Showing
10 changed files
with
210 additions
and
20 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// 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 Data | ||
import BraveShared | ||
|
||
struct DownloadedResourceResponse: Decodable { | ||
let statusCode: Int | ||
let data: Data? | ||
|
||
static func from(message: WKScriptMessage) throws -> DownloadedResourceResponse? { | ||
let data = try JSONSerialization.data(withJSONObject: message.body, options: .prettyPrinted) | ||
return try JSONDecoder().decode(DownloadedResourceResponse.self, from: data) | ||
} | ||
|
||
init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
self.statusCode = try container.decode(Int.self, forKey: .statusCode) | ||
self.data = Data(base64Encoded: try container.decode(String.self, forKey: .base64Data)) | ||
} | ||
|
||
private enum CodingKeys: String, CodingKey { | ||
case statusCode | ||
case base64Data | ||
} | ||
} | ||
|
||
class ResourceDownloadManager: TabContentScript { | ||
fileprivate weak var tab: Tab? | ||
|
||
init(tab: Tab) { | ||
self.tab = tab | ||
} | ||
|
||
static func name() -> String { | ||
return "ResourceDownloadManager" | ||
} | ||
|
||
func scriptMessageHandlerName() -> String? { | ||
return "resourceDownloadManager" | ||
} | ||
|
||
func userContentController(_ userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) { | ||
|
||
do { | ||
let response = try DownloadedResourceResponse.from(message: message) | ||
tab?.temporaryDocument?.onDocumentDownloaded(document: response, error: nil) | ||
} catch let error { | ||
tab?.temporaryDocument?.onDocumentDownloaded(document: nil, error: error) | ||
} | ||
} | ||
|
||
static func downloadResource(for tab: Tab, url: URL) { | ||
let token = UserScriptManager.securityToken.uuidString.replacingOccurrences(of: "-", with: "", options: .literal) | ||
|
||
tab.webView?.evaluateJavaScript("D\(token).download(\"\(url)\");", completionHandler: { _, error in | ||
if let error = error { | ||
tab.temporaryDocument?.onDocumentDownloaded(document: nil, error: error) | ||
} | ||
}) | ||
} | ||
} |
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
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
44 changes: 44 additions & 0 deletions
44
Client/Frontend/UserContent/UserScripts/ResourceDownloader.js
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,44 @@ | ||
// 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/. | ||
|
||
"use strict"; | ||
|
||
var $<downloadManager> = (function() { | ||
function postMessage(msg) { | ||
if (msg) { | ||
webkit.messageHandlers.resourceDownloadManager.postMessage(msg); | ||
} | ||
} | ||
|
||
function downloadPageResource(link) { | ||
var xhr = new XMLHttpRequest(); | ||
xhr.responseType = "arraybuffer"; | ||
xhr.onreadystatechange = function() { | ||
if (this.readyState == XMLHttpRequest.DONE) { | ||
if (this.status == 200) { | ||
var byteArray = new Uint8Array(this.response); | ||
var binaryString = new Array(byteArray.length); | ||
|
||
for (var i = 0; i < byteArray.length; ++i) { | ||
binaryString[i] = String.fromCharCode(byteArray[i]); | ||
} | ||
|
||
var data = binaryString.join(''); | ||
var base64 = window.btoa(data); | ||
|
||
postMessage({ "statusCode": this.status, "base64Data": base64 }); | ||
} | ||
else { | ||
postMessage({ "statusCode": this.status, "base64Data": "" }); | ||
} | ||
} | ||
}; | ||
xhr.open("GET", link, true); | ||
xhr.send(null); | ||
}; | ||
|
||
return { | ||
download: downloadPageResource | ||
}; | ||
})() |