From 68593e9df237d9b044b59e1225b16ce7f2d26fdf Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 14 Aug 2019 12:53:43 -0400 Subject: [PATCH] storage service communicating with swift app --- .../SafariExtensionViewController.swift | 42 +++++++++++++++---- src/services/browserStorage.service.ts | 4 +- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/safari/app/desktop/safari/SafariExtensionViewController.swift b/src/safari/app/desktop/safari/SafariExtensionViewController.swift index 1c7946fbd2..cdad3d45e8 100644 --- a/src/safari/app/desktop/safari/SafariExtensionViewController.swift +++ b/src/safari/app/desktop/safari/SafariExtensionViewController.swift @@ -59,33 +59,52 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe } else { let command = m?.command ?? "\"null\"" print(command) - replyMessage(message: m!) + if(command == "storage_get") { + let obj = UserDefaults.standard.string(forKey: m!.data!) ?? "null" + m!.data = obj + replyMessage(message: m!) + } else if(command == "storage_save") { + let data : StorageData? = jsonDeserialize(json: m!.data) + if(data?.obj == nil) { + UserDefaults.standard.removeObject(forKey: data!.key) + } else { + UserDefaults.standard.set(data?.obj, forKey: data!.key) + } + m!.data = nil + replyMessage(message: m!) + } else if(command == "storage_remove") { + UserDefaults.standard.removeObject(forKey: m!.data!) + m!.data = nil + replyMessage(message: m!) + } } } } func replyMessage(message: AppMessage) { let json = jsonSerialize(obj: message) ?? "\"null\"" - webView.evaluateJavaScript("bitwardenSafariAppMessageReceiver('\(json)');", completionHandler: nil) + webView.evaluateJavaScript("window.bitwardenSafariAppMessageReceiver('\(json)');", completionHandler: nil) } } -func jsonSerialize(obj: T) -> String? { +func jsonSerialize(obj: T?) -> String? { let encoder = JSONEncoder() - encoder.outputFormatting = .prettyPrinted do { let data = try encoder.encode(obj) - return String(data: data, encoding: .utf8)! + return String(data: data, encoding: .utf8) ?? "null" } catch _ { - return nil + return "null" } } -func jsonDeserialize(json: String) -> T? { +func jsonDeserialize(json: String?) -> T? { + if(json == nil) { + return nil; + } let decoder = JSONDecoder() do { - let obj = try decoder.decode(T.self, from: json.data(using: .utf8)!) + let obj = try decoder.decode(T.self, from: json!.data(using: .utf8)!) return obj } catch _ { return nil @@ -95,5 +114,10 @@ func jsonDeserialize(json: String) -> T? { class AppMessage : Decodable, Encodable { var id: String var command: String - var data: String + var data: String? +} + +class StorageData : Decodable, Encodable { + var key: String + var obj: String? } diff --git a/src/services/browserStorage.service.ts b/src/services/browserStorage.service.ts index c09d3c5cf3..03ce1feb12 100644 --- a/src/services/browserStorage.service.ts +++ b/src/services/browserStorage.service.ts @@ -36,10 +36,10 @@ export default class BrowserStorageService implements StorageService { async save(key: string, obj: any): Promise { const keyedObj = { [key]: obj }; if (this.isSafari) { - await SafariApp.sendMessageToApp('storage_save', { + await SafariApp.sendMessageToApp('storage_save', JSON.stringify({ key: key, obj: JSON.stringify(obj), - }); + })); } else { return new Promise((resolve) => { this.chromeStorageApi.set(keyedObj, () => {