From 703474bd9391fb33c35754aa2a6481c3b9854930 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 5 Aug 2019 10:03:25 -0400 Subject: [PATCH] start converting some safari browser apis --- jslib | 2 +- src/browser/browserApi.ts | 70 ++++++++++--------- .../SafariExtensionViewController.swift | 4 +- .../app/desktop/safari/app/popup/index.html | 3 + src/services/browserMessaging.service.ts | 13 +--- 5 files changed, 43 insertions(+), 49 deletions(-) diff --git a/jslib b/jslib index ee91cfc2df..393f6c9c20 160000 --- a/jslib +++ b/jslib @@ -1 +1 @@ -Subproject commit ee91cfc2dfda974ab1ae8e0077e58998bbc6e832 +Subproject commit 393f6c9c20fb7eded5008b65242ea44e69bc349c diff --git a/src/browser/browserApi.ts b/src/browser/browserApi.ts index 1e8b2e937b..d7d645ec5f 100644 --- a/src/browser/browserApi.ts +++ b/src/browser/browserApi.ts @@ -1,7 +1,6 @@ export class BrowserApi { static isWebExtensionsApi: boolean = (typeof browser !== 'undefined'); - static isSafariApi: boolean = (typeof safari !== 'undefined') && - navigator.userAgent.indexOf(' Safari/') !== -1 && navigator.userAgent.indexOf('Chrome') === -1; + static isSafariApi: boolean = (window as any).safariAppExtension === true; static isChromeApi: boolean = !BrowserApi.isSafariApi && (typeof chrome !== 'undefined'); static isFirefoxOnAndroid: boolean = navigator.userAgent.indexOf('Firefox/') !== -1 && navigator.userAgent.indexOf('Android') !== -1; @@ -40,6 +39,8 @@ export class BrowserApi { }); } else if (BrowserApi.isSafariApi) { let wins: any[] = []; + // TODO + /* if (options.currentWindow) { if (safari.application.activeBrowserWindow) { wins.push(safari.application.activeBrowserWindow); @@ -64,6 +65,8 @@ export class BrowserApi { }); return Promise.resolve(returnedTabs); + */ + return Promise.resolve([]); } } @@ -103,6 +106,8 @@ export class BrowserApi { }); }); } else if (BrowserApi.isSafariApi) { + // TODO + /* let t = tab.safariTab; if (!t || !t.page) { const win = safari.application.activeBrowserWindow; @@ -128,6 +133,7 @@ export class BrowserApi { if (t.page) { t.page.dispatchMessage('bitwarden', obj); } + */ return Promise.resolve(); } @@ -137,7 +143,7 @@ export class BrowserApi { if (BrowserApi.isChromeApi) { return chrome.extension.getBackgroundPage(); } else if (BrowserApi.isSafariApi) { - return safari.extension.globalPage.contentWindow; + return window; } else { return null; } @@ -147,7 +153,8 @@ export class BrowserApi { if (BrowserApi.isChromeApi) { return chrome.runtime.getManifest().version; } else if (BrowserApi.isSafariApi) { - return safari.extension.displayVersion; + // TODO + return null; } else { return null; } @@ -157,8 +164,8 @@ export class BrowserApi { if (BrowserApi.isChromeApi) { return chrome.extension.getViews({ type: 'popup' }).length > 0; } else if (BrowserApi.isSafariApi) { - return safari.extension.popovers && safari.extension.popovers.length && - safari.extension.popovers[0].visible; + // TODO + return true; } else { return null; } @@ -169,14 +176,8 @@ export class BrowserApi { chrome.tabs.create({ url: url }); return null; } else if (BrowserApi.isSafariApi) { - if (extensionPage && url.indexOf('/') === 0) { - url = BrowserApi.getAssetUrl(url); - } - const tab = safari.application.activeBrowserWindow.openTab(); - if (tab) { - tab.url = url; - } - return tab; + // TODO + return; } else { return; } @@ -186,10 +187,8 @@ export class BrowserApi { if (BrowserApi.isChromeApi) { return chrome.extension.getURL(path); } else if (BrowserApi.isSafariApi) { - if (path.indexOf('/') === 0) { - path = path.substr(1); - } - return safari.extension.baseURI + path; + // TODO + return null; } else { return null; } @@ -201,13 +200,16 @@ export class BrowserApi { callback(msg, sender, response); }); } else if (BrowserApi.isSafariApi) { + // TODO + /* safari.application.addEventListener('message', async (msgEvent: any) => { callback(msgEvent.message, { tab: BrowserApi.makeTabObject(msgEvent.target), frameId: msgEvent.message != null && msgEvent.message.bitwardenFrameId != null ? msgEvent.message.bitwardenFrameId : null, - }, () => { /* No responses in Safari */ }); + }, () => { }); }, false); + */ } } @@ -220,24 +222,13 @@ export class BrowserApi { } else if (BrowserApi.isWebExtensionsApi || BrowserApi.isChromeApi) { win.close(); } else if (BrowserApi.isSafariApi && safari.extension.popovers && safari.extension.popovers.length > 0) { - safari.extension.popovers[0].hide(); + BrowserApi.sendSafariMessageToApp({ command: 'hideWindow' }); } } static downloadFile(win: Window, blobData: any, blobOptions: any, fileName: string) { if (BrowserApi.isSafariApi) { - const tab = BrowserApi.createNewTab(BrowserApi.getAssetUrl('downloader/index.html')); - const tabToSend = BrowserApi.makeTabObject(tab); - setTimeout(async () => { - BrowserApi.tabSendMessage(tabToSend, { - command: 'downloaderPageData', - data: { - blobData: blobData, - blobOptions: blobOptions, - fileName: fileName, - }, - }); - }, 1000); + // TODO } else { const blob = new Blob([blobData], blobOptions); if (navigator.msSaveOrOpenBlob) { @@ -262,6 +253,9 @@ export class BrowserApi { return {}; } + // TODO + return {}; + /* const winIndex = safari.application.browserWindows.indexOf(tab.browserWindow); const tabIndex = tab.browserWindow.tabs.indexOf(tab); return { @@ -273,11 +267,11 @@ export class BrowserApi { url: tab.url || 'about:blank', safariTab: tab, }; + */ } static gaFilter() { - return process.env.ENV !== 'production' || - (BrowserApi.isSafariApi && safari.application.activeBrowserWindow.activeTab.private); + return process.env.ENV !== 'production'; } static getUILanguage(win: Window) { @@ -291,8 +285,16 @@ export class BrowserApi { static reloadExtension(win: Window) { if (win != null) { return win.location.reload(true); + } else if (BrowserApi.isSafariApi) { + BrowserApi.sendSafariMessageToApp({ command: 'reloadWindow' }); } else if (!BrowserApi.isSafariApi) { return chrome.runtime.reload(); } } + + static sendSafariMessageToApp(message: any, response: (data: any) => {} = null) { + if (this.isSafariApi) { + (window as any).webkit.messageHandlers.bitwardenApp.postMessage(message); + } + } } diff --git a/src/safari/app/desktop/safari/SafariExtensionViewController.swift b/src/safari/app/desktop/safari/SafariExtensionViewController.swift index 0049ecc71a..9123b3832b 100644 --- a/src/safari/app/desktop/safari/SafariExtensionViewController.swift +++ b/src/safari/app/desktop/safari/SafariExtensionViewController.swift @@ -26,7 +26,7 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe let bundleURL = Bundle.main.resourceURL!.absoluteURL let html = bundleURL.appendingPathComponent("app/popup/index.html") webViewConfig.preferences.setValue(true, forKey: "developerExtrasEnabled") - webViewConfig.userContentController.add(self, name: "webViewOnLoad") + webViewConfig.userContentController.add(self, name: "bitwardenMessage") webView = WKWebView(frame: CGRect(x: 0, y: 0, width: parentWidth, height: parentHeight), configuration: webViewConfig) webView.navigationDelegate = self webView.allowsLinkPreview = false @@ -50,7 +50,7 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe } func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { - if message.name == "webViewOnLoad" { + if message.name == "bitwardenMessage" { } } diff --git a/src/safari/app/desktop/safari/app/popup/index.html b/src/safari/app/desktop/safari/app/popup/index.html index e1d16d45e5..a433ef40e8 100644 --- a/src/safari/app/desktop/safari/app/popup/index.html +++ b/src/safari/app/desktop/safari/app/popup/index.html @@ -11,6 +11,9 @@
+ diff --git a/src/services/browserMessaging.service.ts b/src/services/browserMessaging.service.ts index ec92bdc4f0..aeaa14db19 100644 --- a/src/services/browserMessaging.service.ts +++ b/src/services/browserMessaging.service.ts @@ -7,18 +7,7 @@ export default class BrowserMessagingService implements MessagingService { const message = Object.assign({}, { command: subscriber }, arg); if (BrowserApi.isSafariApi) { - const bgPage = BrowserApi.getBackgroundPage(); - bgPage.bitwardenMain.sendInternalRuntimeMessage(message); - - if (!safari.extension.popovers || !safari.extension.popovers.length) { - return; - } - - const popover = safari.extension.popovers[0]; - const popoverPage = popover.contentWindow; - if (popover.visible && popoverPage.bitwardenPopupMainMessageListener) { - popoverPage.bitwardenPopupMainMessageListener(message); - } + BrowserApi.sendSafariMessageToApp(message); } else { chrome.runtime.sendMessage(message); }