mirror of
https://github.com/bitwarden/browser.git
synced 2024-11-09 09:51:02 +01:00
storage service communicating with swift app
This commit is contained in:
parent
e36063ff7a
commit
68593e9df2
@ -59,33 +59,52 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe
|
|||||||
} else {
|
} else {
|
||||||
let command = m?.command ?? "\"null\""
|
let command = m?.command ?? "\"null\""
|
||||||
print(command)
|
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) {
|
func replyMessage(message: AppMessage) {
|
||||||
let json = jsonSerialize(obj: message) ?? "\"null\""
|
let json = jsonSerialize(obj: message) ?? "\"null\""
|
||||||
webView.evaluateJavaScript("bitwardenSafariAppMessageReceiver('\(json)');", completionHandler: nil)
|
webView.evaluateJavaScript("window.bitwardenSafariAppMessageReceiver('\(json)');", completionHandler: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func jsonSerialize<T: Encodable>(obj: T) -> String? {
|
func jsonSerialize<T: Encodable>(obj: T?) -> String? {
|
||||||
let encoder = JSONEncoder()
|
let encoder = JSONEncoder()
|
||||||
encoder.outputFormatting = .prettyPrinted
|
|
||||||
do {
|
do {
|
||||||
let data = try encoder.encode(obj)
|
let data = try encoder.encode(obj)
|
||||||
return String(data: data, encoding: .utf8)!
|
return String(data: data, encoding: .utf8) ?? "null"
|
||||||
} catch _ {
|
} catch _ {
|
||||||
return nil
|
return "null"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func jsonDeserialize<T: Decodable>(json: String) -> T? {
|
func jsonDeserialize<T: Decodable>(json: String?) -> T? {
|
||||||
|
if(json == nil) {
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
let decoder = JSONDecoder()
|
let decoder = JSONDecoder()
|
||||||
do {
|
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
|
return obj
|
||||||
} catch _ {
|
} catch _ {
|
||||||
return nil
|
return nil
|
||||||
@ -95,5 +114,10 @@ func jsonDeserialize<T: Decodable>(json: String) -> T? {
|
|||||||
class AppMessage : Decodable, Encodable {
|
class AppMessage : Decodable, Encodable {
|
||||||
var id: String
|
var id: String
|
||||||
var command: String
|
var command: String
|
||||||
var data: String
|
var data: String?
|
||||||
|
}
|
||||||
|
|
||||||
|
class StorageData : Decodable, Encodable {
|
||||||
|
var key: String
|
||||||
|
var obj: String?
|
||||||
}
|
}
|
||||||
|
@ -36,10 +36,10 @@ export default class BrowserStorageService implements StorageService {
|
|||||||
async save(key: string, obj: any): Promise<any> {
|
async save(key: string, obj: any): Promise<any> {
|
||||||
const keyedObj = { [key]: obj };
|
const keyedObj = { [key]: obj };
|
||||||
if (this.isSafari) {
|
if (this.isSafari) {
|
||||||
await SafariApp.sendMessageToApp('storage_save', {
|
await SafariApp.sendMessageToApp('storage_save', JSON.stringify({
|
||||||
key: key,
|
key: key,
|
||||||
obj: JSON.stringify(obj),
|
obj: JSON.stringify(obj),
|
||||||
});
|
}));
|
||||||
} else {
|
} else {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
this.chromeStorageApi.set(keyedObj, () => {
|
this.chromeStorageApi.set(keyedObj, () => {
|
||||||
|
Loading…
Reference in New Issue
Block a user