2021-01-26 19:32:36 +01:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
// See LICENSE.txt for license information.
|
2020-10-26 20:45:40 +01:00
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
import WebKit
|
|
|
|
|
|
|
|
class ViewController:
|
|
|
|
NSViewController,
|
|
|
|
WKUIDelegate,
|
2021-04-28 00:09:26 +02:00
|
|
|
WKNavigationDelegate,
|
|
|
|
WKScriptMessageHandler {
|
2022-01-07 23:30:18 +01:00
|
|
|
@IBOutlet var webView: CustomWKWebView!
|
2021-05-24 18:59:30 +02:00
|
|
|
private var didLoad = false
|
2021-03-11 04:03:21 +01:00
|
|
|
private var refreshWebViewOnLoad = true
|
2022-02-15 04:57:44 +01:00
|
|
|
private let downloadHandler = DownloadHandler()
|
2021-01-25 22:54:27 +01:00
|
|
|
|
2020-10-26 20:45:40 +01:00
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
|
|
|
NSLog("viewDidLoad")
|
|
|
|
|
|
|
|
webView.navigationDelegate = self
|
|
|
|
webView.uiDelegate = self
|
2021-02-09 21:34:45 +01:00
|
|
|
webView.isHidden = true
|
2021-04-28 00:09:26 +02:00
|
|
|
webView.configuration.userContentController.add(self, name: "nativeApp")
|
2020-10-26 20:45:40 +01:00
|
|
|
|
|
|
|
clearWebViewCache()
|
|
|
|
|
2021-03-09 03:38:50 +01:00
|
|
|
// Load the home page if the server was started, otherwise wait until it has
|
|
|
|
let appDelegate = NSApplication.shared.delegate as! AppDelegate
|
|
|
|
if (appDelegate.isServerStarted) {
|
2021-04-28 00:09:26 +02:00
|
|
|
updateSessionTokenAndUserSettings()
|
|
|
|
loadHomepage()
|
2021-03-09 03:38:50 +01:00
|
|
|
}
|
|
|
|
|
2020-10-26 20:45:40 +01:00
|
|
|
// Do any additional setup after loading the view.
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(onServerStarted), name: AppDelegate.serverStartedNotification, object: nil)
|
|
|
|
}
|
|
|
|
|
2021-04-21 19:33:28 +02:00
|
|
|
override func viewDidAppear() {
|
|
|
|
super.viewDidAppear()
|
|
|
|
self.view.window?.makeFirstResponder(self.webView)
|
|
|
|
}
|
|
|
|
|
2020-10-26 20:45:40 +01:00
|
|
|
override var representedObject: Any? {
|
|
|
|
didSet {
|
|
|
|
// Update the view, if already loaded.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func clearWebViewCache() {
|
|
|
|
let websiteDataTypes = NSSet(array: [WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeMemoryCache])
|
|
|
|
let date = Date(timeIntervalSince1970: 0)
|
|
|
|
WKWebsiteDataStore.default().removeData(ofTypes: websiteDataTypes as! Set<String>, modifiedSince: date, completionHandler:{ })
|
|
|
|
}
|
|
|
|
|
2021-04-01 17:15:06 +02:00
|
|
|
@IBAction func showDiagnosticsInfo(_ sender: NSObject) {
|
|
|
|
let appDelegate = NSApplication.shared.delegate as! AppDelegate
|
|
|
|
|
|
|
|
let alert: NSAlert = NSAlert()
|
|
|
|
alert.messageText = "Diagnostics info"
|
|
|
|
alert.informativeText = "Port: \(appDelegate.serverPort)"
|
|
|
|
alert.alertStyle = .informational
|
|
|
|
alert.addButton(withTitle: "OK")
|
|
|
|
alert.runModal()
|
|
|
|
}
|
|
|
|
|
2020-10-26 20:45:40 +01:00
|
|
|
@objc func onServerStarted() {
|
|
|
|
NSLog("onServerStarted")
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
2021-04-28 00:09:26 +02:00
|
|
|
self.updateSessionTokenAndUserSettings()
|
2020-10-26 20:45:40 +01:00
|
|
|
self.loadHomepage()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-28 00:09:26 +02:00
|
|
|
private func updateSessionTokenAndUserSettings() {
|
2021-02-09 21:27:34 +01:00
|
|
|
let appDelegate = NSApplication.shared.delegate as! AppDelegate
|
2021-04-28 00:09:26 +02:00
|
|
|
let sessionTokenScript = WKUserScript(
|
2021-04-16 13:42:37 +02:00
|
|
|
source: "localStorage.setItem('focalboardSessionId', '\(appDelegate.sessionToken)');",
|
2021-02-09 21:27:34 +01:00
|
|
|
injectionTime: .atDocumentStart,
|
|
|
|
forMainFrameOnly: true
|
|
|
|
)
|
2021-04-28 00:09:26 +02:00
|
|
|
let blob = UserDefaults.standard.string(forKey: "localStorage") ?? ""
|
|
|
|
let userSettingsScript = WKUserScript(
|
|
|
|
source: "const NativeApp = { settingsBlob: \"\(blob)\" };",
|
|
|
|
injectionTime: .atDocumentStart,
|
|
|
|
forMainFrameOnly: true
|
|
|
|
)
|
2021-02-09 21:27:34 +01:00
|
|
|
webView.configuration.userContentController.removeAllUserScripts()
|
2021-04-28 00:09:26 +02:00
|
|
|
webView.configuration.userContentController.addUserScript(sessionTokenScript)
|
|
|
|
webView.configuration.userContentController.addUserScript(userSettingsScript)
|
2021-02-09 21:27:34 +01:00
|
|
|
}
|
|
|
|
|
2020-10-26 20:45:40 +01:00
|
|
|
private func loadHomepage() {
|
2021-05-24 18:59:30 +02:00
|
|
|
NSLog("loadHomepage")
|
2020-10-26 20:45:40 +01:00
|
|
|
let appDelegate = NSApplication.shared.delegate as! AppDelegate
|
|
|
|
let port = appDelegate.serverPort
|
|
|
|
let url = URL(string: "http://localhost:\(port)/")!
|
|
|
|
let request = URLRequest(url: url)
|
2021-03-11 04:03:21 +01:00
|
|
|
refreshWebViewOnLoad = true
|
2020-10-26 20:45:40 +01:00
|
|
|
webView.load(request)
|
|
|
|
}
|
|
|
|
|
|
|
|
func webView(_ webView: WKWebView, runOpenPanelWith parameters: WKOpenPanelParameters, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping ([URL]?) -> Void) {
|
|
|
|
NSLog("webView runOpenPanel")
|
|
|
|
let openPanel = NSOpenPanel()
|
|
|
|
openPanel.canChooseFiles = true
|
|
|
|
openPanel.begin { (result) in
|
|
|
|
if result == NSApplication.ModalResponse.OK {
|
|
|
|
if let url = openPanel.url {
|
|
|
|
completionHandler([url])
|
|
|
|
}
|
|
|
|
} else if result == NSApplication.ModalResponse.cancel {
|
|
|
|
completionHandler(nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-15 04:57:44 +01:00
|
|
|
// Handle downloads
|
|
|
|
|
|
|
|
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, preferences: WKWebpagePreferences, decisionHandler: @escaping (WKNavigationActionPolicy, WKWebpagePreferences) -> Void) {
|
|
|
|
if navigationAction.shouldPerformDownload {
|
|
|
|
decisionHandler(.download, preferences)
|
|
|
|
} else {
|
|
|
|
decisionHandler(.allow, preferences)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
|
|
|
|
if navigationResponse.canShowMIMEType {
|
|
|
|
decisionHandler(.allow)
|
|
|
|
} else {
|
|
|
|
decisionHandler(.download)
|
2020-10-26 20:45:40 +01:00
|
|
|
}
|
2022-02-15 04:57:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func webView(_ webView: WKWebView, navigationAction: WKNavigationAction, didBecome download: WKDownload) {
|
|
|
|
download.delegate = downloadHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func webView(_ webView: WKWebView, navigationResponse: WKNavigationResponse, didBecome download: WKDownload) {
|
|
|
|
download.delegate = downloadHandler
|
2020-10-26 20:45:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
|
|
|
|
NSLog("webView didFinish navigation: \(webView.url?.absoluteString ?? "")")
|
|
|
|
// Disable right-click menu
|
|
|
|
webView.evaluateJavaScript("document.body.setAttribute('oncontextmenu', 'event.preventDefault();');", completionHandler: nil)
|
2021-02-09 21:34:45 +01:00
|
|
|
webView.isHidden = false
|
2021-05-24 18:59:30 +02:00
|
|
|
didLoad = true
|
2021-03-11 04:03:21 +01:00
|
|
|
|
|
|
|
// HACKHACK: Fix WebView initial rendering artifacts
|
|
|
|
if (refreshWebViewOnLoad) {
|
|
|
|
refreshWebViewOnLoad = false
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: {
|
|
|
|
self.refreshWebView()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HACKHACK: Fix WebView initial rendering artifacts
|
|
|
|
private func refreshWebView() {
|
|
|
|
let frame = self.webView.frame
|
|
|
|
var frame2 = frame
|
|
|
|
frame2.size.height += 1
|
|
|
|
self.webView.frame = frame2
|
|
|
|
self.webView.frame = frame
|
2020-10-26 20:45:40 +01:00
|
|
|
}
|
|
|
|
|
2021-05-24 18:59:30 +02:00
|
|
|
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
|
|
|
|
NSLog("webView didFailProvisionalNavigation, error: \(error.localizedDescription)")
|
|
|
|
if (!didLoad) {
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
|
|
|
self.updateSessionTokenAndUserSettings()
|
|
|
|
self.loadHomepage()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 21:34:45 +01:00
|
|
|
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
|
|
|
|
webView.isHidden = false
|
|
|
|
}
|
|
|
|
|
2021-01-25 22:49:27 +01:00
|
|
|
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
|
|
|
|
if let frame = navigationAction.targetFrame,
|
|
|
|
frame.isMainFrame {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// for _blank target or non-mainFrame target, open in default browser
|
|
|
|
if let url = navigationAction.request.url {
|
|
|
|
NSWorkspace.shared.open(url)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-26 20:45:40 +01:00
|
|
|
@IBAction func navigateToHome(_ sender: NSObject) {
|
|
|
|
loadHomepage()
|
|
|
|
}
|
2021-04-28 00:09:26 +02:00
|
|
|
|
|
|
|
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
|
2021-08-15 12:51:19 +02:00
|
|
|
guard
|
|
|
|
let body = message.body as? [AnyHashable: Any],
|
|
|
|
let type = body["type"] as? String,
|
|
|
|
let blob = body["settingsBlob"] as? String
|
|
|
|
else {
|
2021-04-28 00:09:26 +02:00
|
|
|
NSLog("Received unexpected script message \(message.body)")
|
|
|
|
return
|
|
|
|
}
|
2021-08-15 12:51:19 +02:00
|
|
|
NSLog("Received script message \(type)")
|
|
|
|
switch type {
|
|
|
|
case "didImportUserSettings":
|
|
|
|
NSLog("Imported user settings keys \(body["keys"] ?? "?")")
|
|
|
|
case "didNotImportUserSettings":
|
|
|
|
break
|
|
|
|
case "didChangeUserSettings":
|
|
|
|
UserDefaults.standard.set(blob, forKey: "localStorage")
|
|
|
|
NSLog("Persisted user settings after change for key \(body["key"] ?? "?")")
|
|
|
|
default:
|
|
|
|
NSLog("Received script message of unknown type \(type)")
|
|
|
|
}
|
|
|
|
if let settings = Data(base64Encoded: blob).flatMap({ try? JSONSerialization.jsonObject(with: $0, options: []) }) {
|
|
|
|
NSLog("Current user settings: \(settings)")
|
|
|
|
}
|
2021-04-28 00:09:26 +02:00
|
|
|
}
|
2020-10-26 20:45:40 +01:00
|
|
|
}
|
|
|
|
|