From 0bfd65b345e5d46443fcb42c6033e8f248d9dab7 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 16 Aug 2019 15:07:07 -0400 Subject: [PATCH] safariapp to cs messaging --- src/browser/safariApp.ts | 2 +- .../SafariExtensionViewController.swift | 54 +++++++++++++++++-- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/browser/safariApp.ts b/src/browser/safariApp.ts index 6b4f07a7ca..ac7721936a 100644 --- a/src/browser/safariApp.ts +++ b/src/browser/safariApp.ts @@ -42,7 +42,7 @@ export class SafariApp { if (message == null) { return; } - if (message.id == null && message.command === 'cs_message') { + if ((message.id == null || message.id === '') && message.command === 'cs_message') { try { const msg = JSON.parse(message.data); SafariApp.sendMessageToListeners(msg, 'cs_message', null); diff --git a/src/safari/app/desktop/safari/SafariExtensionViewController.swift b/src/safari/app/desktop/safari/SafariExtensionViewController.swift index b408ae29e9..9c2e9ebfbb 100644 --- a/src/safari/app/desktop/safari/SafariExtensionViewController.swift +++ b/src/safari/app/desktop/safari/SafariExtensionViewController.swift @@ -103,7 +103,43 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe }) } } - // SFSafariApplication. + } else if(command == "tabs_message") { + let tabMsg: TabMessage? = jsonDeserialize(json: m!.data) + SFSafariApplication.getAllWindows { (wins) in + var theWin: SFSafariWindow? + var winIndex = 0 + for win in wins { + if(tabMsg?.tab.windowId == winIndex) { + theWin = win + break + } + winIndex = winIndex + 1 + } + if(theWin == nil) { + // TODO: error + } else { + var theTab: SFSafariTab? + theWin!.getAllTabs { (tabs) in + var tabIndex = 0 + for tab in tabs { + if(tabMsg?.tab.index == tabIndex) { + theTab = tab + break + } + tabIndex = tabIndex + 1 + } + if(theTab == nil) { + // TODO: error + } else { + theTab!.getActivePage { (activePage) in + if(activePage != nil) { + activePage?.dispatchMessageToScript(withName: "bitwarden", userInfo: ["msg": tabMsg!.obj]) + } + } + } + } + } + } } } } @@ -153,25 +189,27 @@ func processWindowsForTabs(wins: [SFSafariWindow], options: TabQueryOptions?, co if(activeTab != nil && activeTab == tab) { makeTabObject(tab: tab, activeTab: activeTab, windowIndex: windowIndex, tabIndex: tabIndex, complete: { (t) in newTabs.append(t) + tabIndex = tabIndex + 1 tabGroup.leave() }) } else { + tabIndex = tabIndex + 1 tabGroup.leave() } } else { makeTabObject(tab: tab, activeTab: activeTab, windowIndex: windowIndex, tabIndex: tabIndex, complete: { (t) in newTabs.append(t) + tabIndex = tabIndex + 1 tabGroup.leave() }) } - tabIndex = tabIndex + 1 } tabGroup.notify(queue: .main){ + windowIndex = windowIndex + 1 winGroup.leave() } } } - windowIndex = windowIndex + 1 } winGroup.notify(queue: .main){ complete(newTabs) @@ -260,3 +298,13 @@ class Tab : Decodable, Encodable { var active: Bool var url: String? } + +class TabMessage: Decodable, Encodable { + var tab: Tab + var obj: String + var options: TabMessageOptions? +} + +class TabMessageOptions: Decodable, Encodable { + var frameId: Int? +}