1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-09-19 02:51:14 +02:00

process safari tab on app messages

This commit is contained in:
Kyle Spearrin 2019-08-20 13:06:38 -04:00
parent 630d825ed5
commit 80314e766f
8 changed files with 59 additions and 24 deletions

View File

@ -9,7 +9,7 @@ document.addEventListener('DOMContentLoaded', () => {
} }
safari.self.addEventListener('message', (msgEvent: any) => { safari.self.addEventListener('message', (msgEvent: any) => {
init2fa(msgEvent.message); init2fa(JSON.parse(msgEvent.message.msg));
}, false); }, false);
function init2fa(msg: any) { function init2fa(msg: any) {

View File

@ -59,7 +59,10 @@ export class SafariApp {
if ((message.id == null || message.id === '') && message.command === 'app_message') { if ((message.id == null || message.id === '') && message.command === 'app_message') {
try { try {
const msg = JSON.parse(message.data); const msg = JSON.parse(message.data);
SafariApp.sendMessageToListeners(msg, 'app_message', null); SafariApp.sendMessageToListeners(msg, {
id: 'app_message',
tab: message.senderTab,
}, null);
} catch { } } catch { }
} else if (message.id != null && (window as any).bitwardenSafariAppRequests.has(message.id)) { } else if (message.id != null && (window as any).bitwardenSafariAppRequests.has(message.id)) {
const p = (window as any).bitwardenSafariAppRequests.get(message.id); const p = (window as any).bitwardenSafariAppRequests.get(message.id);

View File

@ -17,7 +17,7 @@ document.addEventListener('DOMContentLoaded', (event) => {
bitwardenFrameId: (window as any).__bitwardenFrameId, bitwardenFrameId: (window as any).__bitwardenFrameId,
}); });
safari.self.addEventListener('message', (msgEvent: any) => { safari.self.addEventListener('message', (msgEvent: any) => {
const msg = msgEvent.message; const msg = JSON.parse(msgEvent.message.msg);
if (msg.bitwardenFrameId != null && (window as any).__bitwardenFrameId !== msg.bitwardenFrameId) { if (msg.bitwardenFrameId != null && (window as any).__bitwardenFrameId !== msg.bitwardenFrameId) {
return; return;
} }

View File

@ -38,7 +38,7 @@ document.addEventListener('DOMContentLoaded', (event) => {
bitwardenFrameId: (window as any).__bitwardenFrameId, bitwardenFrameId: (window as any).__bitwardenFrameId,
}); });
safari.self.addEventListener('message', (msgEvent: any) => { safari.self.addEventListener('message', (msgEvent: any) => {
const msg = msgEvent.message; const msg = JSON.parse(msgEvent.message.msg);
if (msg.bitwardenFrameId != null && (window as any).__bitwardenFrameId !== msg.bitwardenFrameId) { if (msg.bitwardenFrameId != null && (window as any).__bitwardenFrameId !== msg.bitwardenFrameId) {
return; return;
} }

View File

@ -7,7 +7,7 @@ document.addEventListener('DOMContentLoaded', () => {
} }
safari.self.addEventListener('message', (msgEvent: any) => { safari.self.addEventListener('message', (msgEvent: any) => {
doDownload(msgEvent.message); doDownload(JSON.parse(msgEvent.message.msg));
}, false); }, false);
function doDownload(msg: any) { function doDownload(msg: any) {

View File

@ -9,7 +9,7 @@ document.addEventListener('DOMContentLoaded', () => {
responseCommand: responseCommand responseCommand: responseCommand
}); });
safari.self.addEventListener('message', (msgEvent) => { safari.self.addEventListener('message', (msgEvent) => {
const msg = msgEvent.message; const msg = JSON.parse(msgEvent.message.msg);
if (msg.command === responseCommand && msg.data) { if (msg.command === responseCommand && msg.data) {
i18n = msg.data.i18n; i18n = msg.data.i18n;
load(); load();

View File

@ -17,10 +17,12 @@ class SafariExtensionHandler: SFSafariExtensionHandler {
override func messageReceived(withName messageName: String, from page: SFSafariPage, userInfo: [String: Any]?) { override func messageReceived(withName messageName: String, from page: SFSafariPage, userInfo: [String: Any]?) {
// This method will be called when a content script provided by your extension calls safari.extension.dispatchMessage("message"). // This method will be called when a content script provided by your extension calls safari.extension.dispatchMessage("message").
if messageName == "bitwarden" { if messageName == "bitwarden" {
page.getPropertiesWithCompletionHandler { _ in page.getPropertiesWithCompletionHandler { properties in
// NSLog("The extension received a message (\(messageName)) from a script injected into (\(String(describing: properties?.url))) with userInfo (\(userInfo ?? [:]))") // NSLog("The extension received a message (\(messageName)) from a script injected into (\(String(describing: properties?.url))) with userInfo (\(userInfo ?? [:]))")
DispatchQueue.main.async { DispatchQueue.main.async {
SafariExtensionViewController.shared.sendMessage(msg: userInfo) makeSenderTabObject(page: page, props: properties, complete: { senderTab in
self.sendMessage(msg: userInfo, sender: senderTab)
})
} }
} }
} }
@ -42,7 +44,51 @@ class SafariExtensionHandler: SFSafariExtensionHandler {
override func popoverWillShow(in _: SFSafariWindow) { override func popoverWillShow(in _: SFSafariWindow) {
DispatchQueue.main.async { DispatchQueue.main.async {
SafariExtensionViewController.shared.sendMessage(msg: ["command": "reloadPopup"]) self.sendMessage(msg: ["command": "reloadPopup"], sender: nil)
} }
} }
func sendMessage(msg: [String: Any]?, sender: Tab? = nil) {
if SafariExtensionViewController.shared.webView == nil {
return
}
let newMsg = AppMessage()
newMsg.command = "app_message"
newMsg.senderTab = sender
do {
let jsonData = try JSONSerialization.data(withJSONObject: msg as Any, options: [])
newMsg.data = String(data: jsonData, encoding: .utf8)
} catch let error {
print("error converting to json: \(error)")
}
SafariExtensionViewController.shared.replyMessage(message: newMsg)
}
}
func makeSenderTabObject(page: SFSafariPage, props: SFSafariPageProperties?, complete: @escaping (Tab) -> Void) {
let t = Tab()
t.title = props?.title
t.url = props?.url?.absoluteString
page.getContainingTab { tab in
tab.getContainingWindow(completionHandler: { win in
win?.getActiveTab(completionHandler: { activeTab in
t.active = activeTab != nil && tab == activeTab
SFSafariApplication.getAllWindows(completionHandler: { allWins in
t.windowId = allWins.firstIndex(of: win!) ?? -100
let winGroup = DispatchGroup()
for allWin in allWins {
winGroup.enter()
allWin.getAllTabs { allWinTabs in
t.index = allWinTabs.firstIndex(of: tab) ?? -1
winGroup.leave()
}
}
winGroup.notify(queue: .main) {
t.id = "\(t.windowId)_\(t.index)"
complete(t)
}
})
})
})
}
} }

View File

@ -169,21 +169,6 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe
let json = (jsonSerialize(obj: message) ?? "null") let json = (jsonSerialize(obj: message) ?? "null")
webView.evaluateJavaScript("window.bitwardenSafariAppMessageReceiver(\(json));", completionHandler: nil) webView.evaluateJavaScript("window.bitwardenSafariAppMessageReceiver(\(json));", completionHandler: nil)
} }
func sendMessage(msg: [String: Any]?) {
if webView == nil {
return
}
let newMsg = AppMessage()
newMsg.command = "app_message"
do {
let jsonData = try JSONSerialization.data(withJSONObject: msg as Any, options: [])
newMsg.data = String(data: jsonData, encoding: .utf8)
} catch let error {
print("error converting to json: \(error)")
}
replyMessage(message: newMsg)
}
} }
func processWindowsForTabs(wins: [SFSafariWindow], options: TabQueryOptions?, complete: @escaping ([Tab]) -> Void) { func processWindowsForTabs(wins: [SFSafariWindow], options: TabQueryOptions?, complete: @escaping ([Tab]) -> Void) {
@ -289,6 +274,7 @@ class AppMessage: Decodable, Encodable {
var data: String? var data: String?
var responseData: String? var responseData: String?
var responseError: Bool? var responseError: Bool?
var senderTab: Tab?
} }
class StorageData: Decodable, Encodable { class StorageData: Decodable, Encodable {