123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408 |
- import UIKit
- import WebKit
- import DcCore
- class WebxdcViewController: WebViewViewController {
-
- enum WebxdcHandler: String {
- case log = "log"
- case setUpdateListener = "setUpdateListener"
- case sendStatusUpdate = "sendStatusUpdateHandler"
- }
- let INTERNALSCHEMA = "webxdc"
-
- var messageId: Int
- var webxdcUpdateObserver: NSObjectProtocol?
- var webxdcName: String = ""
- var sourceCodeUrl: String?
- private var allowInternet: Bool = false
- private var shortcutManager: ShortcutManager?
- private lazy var moreButton: UIBarButtonItem = {
- let image: UIImage?
- if #available(iOS 13.0, *) {
- image = UIImage(systemName: "ellipsis.circle")
- } else {
- image = UIImage(named: "ic_more")
- }
- return UIBarButtonItem(image: image,
- style: .plain,
- target: self,
- action: #selector(moreButtonPressed))
- }()
-
- // Block just everything, except of webxdc urls
- let blockRules = """
- [
- {
- "trigger": {
- "url-filter": ".*"
- },
- "action": {
- "type": "block"
- }
- },
- {
- "trigger": {
- "url-filter": "webxdc://*"
- },
- "action": {
- "type": "ignore-previous-rules"
- }
- }
- ]
- """
-
- lazy var webxdcbridge: String = {
- let addr = dcContext.addr?
- .addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
- let displayname = (dcContext.displayname ?? dcContext.addr)?
- .addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
-
- let script = """
- window.webxdc = (() => {
- var log = (s)=>webkit.messageHandlers.log.postMessage(s);
-
- var update_listener = () => {};
- let should_run_again = false;
- let running = false;
- let lastSerial = 0;
- window.__webxdcUpdate = async () => {
- if (running) {
- should_run_again = true
- return
- }
- should_run_again = false
- running = true;
- try {
- const updates = await fetch("webxdc-update.json?"+lastSerial).then((response) => response.json())
- updates.forEach((update) => {
- update_listener(update);
- if (lastSerial < update["max_serial"]){
- lastSerial = update["max_serial"]
- }
- });
- } catch (e) {
- log("json error: "+ e.message)
- } finally {
- running = false;
- if (should_run_again) {
- await window.__webxdcUpdate()
- }
- }
- }
- return {
- selfAddr: decodeURI("\((addr ?? "unknown"))"),
-
- selfName: decodeURI("\((displayname ?? "unknown"))"),
-
- setUpdateListener: (cb, serial) => {
- update_listener = cb
- return window.__webxdcUpdate()
- },
- getAllUpdates: () => {
- console.error("deprecated 2022-02-20 all updates are returned through the callback set by setUpdateListener");
- return Promise.resolve([]);
- },
-
- sendUpdate: (payload, descr) => {
- // only one parameter is allowed, we we create a new parameter object here
- var parameter = {
- payload: payload,
- descr: descr
- };
- webkit.messageHandlers.sendStatusUpdateHandler.postMessage(parameter);
- },
- };
- })();
- """
- return script
- }()
-
- override var configuration: WKWebViewConfiguration {
- let config = WKWebViewConfiguration()
- let preferences = WKPreferences()
- let contentController = WKUserContentController()
-
- contentController.add(self, name: WebxdcHandler.sendStatusUpdate.rawValue)
- contentController.add(self, name: WebxdcHandler.setUpdateListener.rawValue)
- contentController.add(self, name: WebxdcHandler.log.rawValue)
-
- let scriptSource = """
- window.RTCPeerConnection = ()=>{};
- RTCPeerConnection = ()=>{};
- try {
- window.webkitRTCPeerConnection = ()=>{};
- webkitRTCPeerConnection = ()=>{};
- } catch (e){}
- """
- let script = WKUserScript(source: scriptSource, injectionTime: .atDocumentStart, forMainFrameOnly: false)
- contentController.addUserScript(script)
- config.userContentController = contentController
- config.setURLSchemeHandler(self, forURLScheme: INTERNALSCHEMA)
-
- config.mediaTypesRequiringUserActionForPlayback = []
- config.allowsInlineMediaPlayback = true
- if #available(iOS 13.0, *) {
- preferences.isFraudulentWebsiteWarningEnabled = true
- }
-
- if #available(iOS 14.0, *) {
- config.defaultWebpagePreferences.allowsContentJavaScript = true
- } else {
- preferences.javaScriptEnabled = true
- }
- preferences.javaScriptCanOpenWindowsAutomatically = false
- config.preferences = preferences
- return config
- }
-
-
- init(dcContext: DcContext, messageId: Int) {
- self.messageId = messageId
- self.shortcutManager = ShortcutManager(dcContext: dcContext, messageId: messageId)
- super.init(dcContext: dcContext)
- }
-
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
-
- override func viewDidLoad() {
- super.viewDidLoad()
- let msg = dcContext.getMessage(id: messageId)
- let dict = msg.getWebxdcInfoDict()
- let document = dict["document"] as? String ?? ""
- webxdcName = dict["name"] as? String ?? "ErrName" // name should not be empty
- let chatName = dcContext.getChat(chatId: msg.chatId).name
- self.allowInternet = dict["internet_access"] as? Bool ?? false
- self.title = document.isEmpty ? "\(webxdcName) – \(chatName)" : "\(document) – \(chatName)"
- navigationItem.rightBarButtonItem = moreButton
- if let sourceCode = dict["source_code_url"] as? String,
- !sourceCode.isEmpty {
- sourceCodeUrl = sourceCode
- }
- }
-
- override func willMove(toParent parent: UIViewController?) {
- super.willMove(toParent: parent)
- let willBeRemoved = parent == nil
- navigationController?.interactivePopGestureRecognizer?.isEnabled = willBeRemoved
- if willBeRemoved {
- let nc = NotificationCenter.default
- if let webxdcUpdateObserver = webxdcUpdateObserver {
- nc.removeObserver(webxdcUpdateObserver)
- }
- shortcutManager = nil
- } else {
- addObserver()
- }
- }
-
- private func addObserver() {
- let nc = NotificationCenter.default
- webxdcUpdateObserver = nc.addObserver(
- forName: dcNotificationWebxdcUpdate,
- object: nil,
- queue: OperationQueue.main
- ) { [weak self] notification in
- guard let self = self else { return }
- guard let ui = notification.userInfo,
- let messageId = ui["message_id"] as? Int else {
- logger.error("failed to handle dcNotificationWebxdcUpdate")
- return
- }
- if messageId == self.messageId {
- self.updateWebxdc()
- }
- }
- }
-
- override func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
- if let url = navigationAction.request.url {
- if url.scheme == "mailto" {
- openChatFor(url: url)
- decisionHandler(.cancel)
- return
- } else if url.scheme != INTERNALSCHEMA {
- logger.debug("cancel loading: \(url)")
- decisionHandler(.cancel)
- return
- }
- }
- logger.debug("loading: \(String(describing: navigationAction.request.url))")
- decisionHandler(.allow)
- }
-
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- if allowInternet {
- loadHtml()
- } else {
- loadRestrictedHtml()
- }
- }
- override func viewDidDisappear(_ animated: Bool) {
- super.viewDidDisappear(animated)
- if #available(iOS 15.0, *) {
- webView.setAllMediaPlaybackSuspended(true)
- }
- }
- private func loadRestrictedHtml() {
- WKContentRuleListStore.default().compileContentRuleList(
- forIdentifier: "WebxdcContentBlockingRules",
- encodedContentRuleList: blockRules) { (contentRuleList, error) in
-
- guard let contentRuleList = contentRuleList, error == nil else {
- return
- }
-
- let configuration = self.webView.configuration
- configuration.userContentController.add(contentRuleList)
- self.loadHtml()
- }
- }
-
- private func loadHtml() {
- DispatchQueue.global(qos: .userInitiated).async { [weak self] in
- guard let self = self else { return }
- let url = URL(string: "\(self.INTERNALSCHEMA)://acc\(self.dcContext.id)-msg\(self.messageId).localhost/index.html")
- let urlRequest = URLRequest(url: url!)
- DispatchQueue.main.async {
- self.webView.load(urlRequest)
- }
- }
- }
- private func updateWebxdc() {
- webView.evaluateJavaScript("window.__webxdcUpdate()", completionHandler: nil)
- }
- @objc private func moreButtonPressed() {
- let alert = UIAlertController(title: webxdcName + " – " + String.localized("webxdc_app"),
- message: nil,
- preferredStyle: .safeActionSheet)
- let addToHomescreenAction = UIAlertAction(title: String.localized("add_to_home_screen"), style: .default, handler: addToHomeScreen(_:))
- alert.addAction(addToHomescreenAction)
- if sourceCodeUrl != nil {
- let sourceCodeAction = UIAlertAction(title: String.localized("source_code"), style: .default, handler: openUrl(_:))
- alert.addAction(sourceCodeAction)
- }
- let cancelAction = UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil)
- alert.addAction(cancelAction)
- self.present(alert, animated: true, completion: nil)
- }
- private func addToHomeScreen(_ action: UIAlertAction) {
- shortcutManager?.showShortcutLandingPage()
- }
- private func openUrl(_ action: UIAlertAction) {
- if let sourceCodeUrl = sourceCodeUrl,
- let url = URL(string: sourceCodeUrl) {
- UIApplication.shared.open(url)
- }
- }
- }
- extension WebxdcViewController: WKScriptMessageHandler {
- func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
- let handler = WebxdcHandler(rawValue: message.name)
- switch handler {
- case .log:
- guard let msg = message.body as? String else {
- logger.error("could not convert param \(message.body) to string")
- return
- }
- logger.debug("webxdc log msg: "+msg)
-
- case .sendStatusUpdate:
- guard let dict = message.body as? [String: AnyObject],
- let payloadDict = dict["payload"] as? [String: AnyObject],
- let payloadJson = try? JSONSerialization.data(withJSONObject: payloadDict, options: []),
- let payloadString = String(data: payloadJson, encoding: .utf8),
- let description = dict["descr"] as? String else {
- logger.error("Failed to parse status update parameters \(message.body)")
- return
- }
- _ = dcContext.sendWebxdcStatusUpdate(msgId: messageId, payload: payloadString, description: description)
- default:
- logger.debug("another method was called")
- }
- }
- }
- extension WebxdcViewController: WKURLSchemeHandler {
- func webView(_ webView: WKWebView, start urlSchemeTask: WKURLSchemeTask) {
- if let url = urlSchemeTask.request.url, let scheme = url.scheme, scheme == INTERNALSCHEMA {
- if url.path == "/webxdc-update.json" || url.path == "webxdc-update.json" {
- let lastKnownSerial = Int(url.query ?? "0") ?? 0
- let data = Data(
- dcContext.getWebxdcStatusUpdates(msgId: messageId, lastKnownSerial: lastKnownSerial).utf8)
- let response = URLResponse(url: url, mimeType: "application/json", expectedContentLength: data.count, textEncodingName: "utf-8")
-
- urlSchemeTask.didReceive(response)
- urlSchemeTask.didReceive(data)
- urlSchemeTask.didFinish()
- return
- }
- let file = url.path
- let dcMsg = dcContext.getMessage(id: messageId)
- var data: Data
- if url.lastPathComponent == "webxdc.js" {
- data = Data(webxdcbridge.utf8)
- } else {
- data = dcMsg.getWebxdcBlob(filename: file)
- }
- let mimeType = DcUtils.getMimeTypeForPath(path: file)
- let statusCode = (data.isEmpty ? 404 : 200)
- var headerFields = [
- "Content-Type": mimeType,
- "Content-Length": "\(data.count)",
- ]
- if !self.allowInternet {
- headerFields["Content-Security-Policy"] = """
- default-src 'self';
- style-src 'self' 'unsafe-inline' blob: ;
- font-src 'self' data: blob: ;
- script-src 'self' 'unsafe-inline' 'unsafe-eval' blob: ;
- connect-src 'self' data: blob: ;
- img-src 'self' data: blob: ;
- webrtc 'block' ;
- """
- }
- guard let response = HTTPURLResponse(
- url: url,
- statusCode: statusCode,
- httpVersion: "HTTP/1.1",
- headerFields: headerFields
- ) else {
- return
- }
- urlSchemeTask.didReceive(response)
- urlSchemeTask.didReceive(data)
- urlSchemeTask.didFinish()
- } else {
- logger.debug("not loading \(String(describing: urlSchemeTask.request.url))")
- }
- }
-
- func webView(_ webView: WKWebView, stop urlSchemeTask: WKURLSchemeTask) {
- }
- }
|