fixes to safariapp messaging

This commit is contained in:
Kyle Spearrin 2019-08-15 14:48:02 -04:00
parent 87abab1eb8
commit fcc777cded
3 changed files with 10 additions and 13 deletions

View File

@ -6,8 +6,7 @@ export class SafariApp {
(window as any).bitwardenSafariAppMessageReceiver = (message: any) => { (window as any).bitwardenSafariAppMessageReceiver = (message: any) => {
// tslint:disable-next-line // tslint:disable-next-line
console.log(message); console.log(message);
message.data = null; SafariApp.receiveMessageFromApp(message);
SafariApp.receiveMessageFromApp(message == null ? null : JSON.parse(message));
}; };
} }
} }

View File

@ -25,12 +25,13 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe
let webViewConfig = WKWebViewConfiguration() let webViewConfig = WKWebViewConfiguration()
let bundleURL = Bundle.main.resourceURL!.absoluteURL let bundleURL = Bundle.main.resourceURL!.absoluteURL
let html = bundleURL.appendingPathComponent("app/popup/index.html") let html = bundleURL.appendingPathComponent("app/popup/index.html")
webViewConfig.preferences.setValue(true, forKey: "allowFileAccessFromFileURLs")
webViewConfig.preferences.setValue(true, forKey: "developerExtrasEnabled") webViewConfig.preferences.setValue(true, forKey: "developerExtrasEnabled")
webViewConfig.userContentController.add(self, name: "bitwardenApp") webViewConfig.userContentController.add(self, name: "bitwardenApp")
webView = WKWebView(frame: CGRect(x: 0, y: 0, width: parentWidth, height: parentHeight), configuration: webViewConfig) webView = WKWebView(frame: CGRect(x: 0, y: 0, width: parentWidth, height: parentHeight), configuration: webViewConfig)
webView.navigationDelegate = self webView.navigationDelegate = self
webView.allowsLinkPreview = false webView.allowsLinkPreview = false
webView.loadFileURL(html, allowingReadAccessTo:bundleURL) webView.loadFileURL(html, allowingReadAccessTo: bundleURL)
webView.alphaValue = 0.0; webView.alphaValue = 0.0;
self.view.addSubview(webView) self.view.addSubview(webView)
} }
@ -57,11 +58,11 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe
if(m == nil) { if(m == nil) {
print("m is nil") print("m is nil")
} else { } else {
let command = m?.command ?? "\"null\"" let command = m?.command ?? "null"
print(command) print(command)
if(command == "storage_get") { if(command == "storage_get") {
let obj = UserDefaults.standard.string(forKey: m!.data!) ?? "null" let obj = UserDefaults.standard.string(forKey: m!.data!)
m!.data = obj m!.responseData = obj
replyMessage(message: m!) replyMessage(message: m!)
} else if(command == "storage_save") { } else if(command == "storage_save") {
let data : StorageData? = jsonDeserialize(json: m!.data) let data : StorageData? = jsonDeserialize(json: m!.data)
@ -70,21 +71,17 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe
} else { } else {
UserDefaults.standard.set(data?.obj, forKey: data!.key) UserDefaults.standard.set(data?.obj, forKey: data!.key)
} }
m!.data = nil
replyMessage(message: m!) replyMessage(message: m!)
} else if(command == "storage_remove") { } else if(command == "storage_remove") {
UserDefaults.standard.removeObject(forKey: m!.data!) UserDefaults.standard.removeObject(forKey: m!.data!)
m!.data = nil
replyMessage(message: m!) replyMessage(message: m!)
} else if(command == "getLocaleStrings") { } else if(command == "getLocaleStrings") {
let language = m!.data let language = m!.data
m!.data = nil
let bundleURL = Bundle.main.resourceURL!.absoluteURL let bundleURL = Bundle.main.resourceURL!.absoluteURL
let messagesUrl = bundleURL.appendingPathComponent("app/_locales/en/messages.json") let messagesUrl = bundleURL.appendingPathComponent("app/_locales/en/messages.json")
do { do {
let json = try String(contentsOf: messagesUrl, encoding: .utf8) let json = try String(contentsOf: messagesUrl, encoding: .utf8)
webView.evaluateJavaScript("window.bitwardenLocaleStrings = \(json);", completionHandler: nil) webView.evaluateJavaScript("window.bitwardenLocaleStrings = \(json);", completionHandler: nil)
m!.data = nil
} catch { } } catch { }
replyMessage(message: m!) replyMessage(message: m!)
} }
@ -93,8 +90,8 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe
} }
func replyMessage(message: AppMessage) { func replyMessage(message: AppMessage) {
let json = (jsonSerialize(obj: message) ?? "\"null\"").replacingOccurrences(of: "`", with: "\\`") let json = (jsonSerialize(obj: message) ?? "null")
webView.evaluateJavaScript("window.bitwardenSafariAppMessageReceiver(`\(json)`);", completionHandler: nil) webView.evaluateJavaScript("window.bitwardenSafariAppMessageReceiver(\(json));", completionHandler: nil)
} }
} }
@ -126,6 +123,7 @@ class AppMessage : Decodable, Encodable {
var id: String var id: String
var command: String var command: String
var data: String? var data: String?
var responseData: String?
} }
class StorageData : Decodable, Encodable { class StorageData : Decodable, Encodable {

View File

@ -19,7 +19,7 @@ export default class BrowserStorageService implements StorageService {
async get<T>(key: string): Promise<T> { async get<T>(key: string): Promise<T> {
if (this.isSafari) { if (this.isSafari) {
const obj = await SafariApp.sendMessageToApp('storage_get', key); const obj = await SafariApp.sendMessageToApp('storage_get', key);
return JSON.parse(obj) as T; return obj == null ? null : JSON.parse(obj) as T;
} else { } else {
return new Promise((resolve) => { return new Promise((resolve) => {
this.chromeStorageApi.get(key, (obj: any) => { this.chromeStorageApi.get(key, (obj: any) => {