mirror of
https://github.com/bitwarden/browser.git
synced 2024-11-28 12:45:45 +01:00
Merge pull request #1227 from bitwarden/safari-crash-testfix
Attempt fix for safari app extension crash
This commit is contained in:
commit
5c3a66845b
@ -133,16 +133,6 @@ export class BrowserApi {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static getAssetUrl(path: string): Promise<string> {
|
|
||||||
if (BrowserApi.isChromeApi) {
|
|
||||||
return Promise.resolve(chrome.extension.getURL(path));
|
|
||||||
} else if (BrowserApi.isSafariApi) {
|
|
||||||
return SafariApp.sendMessageToApp('getAppPath');
|
|
||||||
} else {
|
|
||||||
return Promise.resolve(null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static messageListener(name: string, callback: (message: any, sender: any, response: any) => void) {
|
static messageListener(name: string, callback: (message: any, sender: any, response: any) => void) {
|
||||||
if (BrowserApi.isChromeApi) {
|
if (BrowserApi.isChromeApi) {
|
||||||
chrome.runtime.onMessage.addListener((msg: any, sender: any, response: any) => {
|
chrome.runtime.onMessage.addListener((msg: any, sender: any, response: any) => {
|
||||||
|
@ -60,28 +60,34 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
let messageBody = message.body as! String
|
let messageBody = message.body as! String
|
||||||
// print(messageBody)
|
// NSLog("MESSAGE: \(messageBody)")
|
||||||
let m: AppMessage? = jsonDeserialize(json: messageBody)
|
let m: AppMessage? = jsonDeserialize(json: messageBody)
|
||||||
if m == nil {
|
if m == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
let command = m!.command
|
let command = m!.command
|
||||||
// print(command)
|
NSLog("Command: \(command)")
|
||||||
if command == "storage_get" {
|
if command == "storage_get" {
|
||||||
let obj = UserDefaults.standard.string(forKey: m!.data!)
|
if m!.data != nil {
|
||||||
m!.responseData = obj
|
let obj = UserDefaults.standard.string(forKey: m!.data!)
|
||||||
replyMessage(message: m!)
|
m!.responseData = obj
|
||||||
|
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)
|
||||||
if data?.obj == nil {
|
if data?.key != nil {
|
||||||
UserDefaults.standard.removeObject(forKey: data!.key)
|
if data?.obj == nil {
|
||||||
} else {
|
UserDefaults.standard.removeObject(forKey: data!.key)
|
||||||
UserDefaults.standard.set(data?.obj, forKey: data!.key)
|
} else {
|
||||||
|
UserDefaults.standard.set(data?.obj, forKey: data!.key)
|
||||||
|
}
|
||||||
|
replyMessage(message: m!)
|
||||||
}
|
}
|
||||||
replyMessage(message: m!)
|
|
||||||
} else if command == "storage_remove" {
|
} else if command == "storage_remove" {
|
||||||
UserDefaults.standard.removeObject(forKey: m!.data!)
|
if m!.data != nil {
|
||||||
replyMessage(message: m!)
|
UserDefaults.standard.removeObject(forKey: m!.data!)
|
||||||
|
replyMessage(message: m!)
|
||||||
|
}
|
||||||
} else if command == "getLocaleStrings" {
|
} else if command == "getLocaleStrings" {
|
||||||
let language = m!.data ?? "en"
|
let language = m!.data ?? "en"
|
||||||
let bundleURL = Bundle.main.resourceURL!.absoluteURL
|
let bundleURL = Bundle.main.resourceURL!.absoluteURL
|
||||||
@ -165,7 +171,7 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe
|
|||||||
replyMessage(message: m!)
|
replyMessage(message: m!)
|
||||||
} else if command == "readFromClipboard" {
|
} else if command == "readFromClipboard" {
|
||||||
let pasteboard = NSPasteboard.general
|
let pasteboard = NSPasteboard.general
|
||||||
m?.responseData = pasteboard.pasteboardItems?.first?.string(forType: .string)
|
m!.responseData = pasteboard.pasteboardItems?.first?.string(forType: .string)
|
||||||
replyMessage(message: m!)
|
replyMessage(message: m!)
|
||||||
} else if command == "downloadFile" {
|
} else if command == "downloadFile" {
|
||||||
if m!.data != nil {
|
if m!.data != nil {
|
||||||
@ -173,7 +179,7 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe
|
|||||||
var data: Data?
|
var data: Data?
|
||||||
if dlMsg.blobOptions?.type == "text/plain" {
|
if dlMsg.blobOptions?.type == "text/plain" {
|
||||||
data = dlMsg.blobData?.data(using: .utf8)
|
data = dlMsg.blobData?.data(using: .utf8)
|
||||||
} else {
|
} else if dlMsg.blobData != nil {
|
||||||
data = Data(base64Encoded: dlMsg.blobData!)
|
data = Data(base64Encoded: dlMsg.blobData!)
|
||||||
}
|
}
|
||||||
if data != nil {
|
if data != nil {
|
||||||
@ -199,13 +205,6 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if command == "getAppPath" {
|
|
||||||
SFSafariExtension.getBaseURI(completionHandler: { uri in
|
|
||||||
if uri != nil {
|
|
||||||
m!.responseData = uri!.absoluteString
|
|
||||||
self.replyMessage(message: m!)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user