1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-07 09:31:31 +01:00

format swift code with swiftformat

This commit is contained in:
Kyle Spearrin 2019-08-19 09:22:19 -04:00
parent 05684b3b03
commit da9bd3a8db
4 changed files with 83 additions and 95 deletions

View File

@ -10,17 +10,11 @@ import Cocoa
@NSApplicationMain @NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate { class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_: Notification) {
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application // Insert code here to initialize your application
} }
func applicationWillTerminate(_ aNotification: Notification) { func applicationWillTerminate(_: Notification) {
// Insert code here to tear down your application // Insert code here to tear down your application
} }
} }

View File

@ -9,7 +9,6 @@
import Cocoa import Cocoa
class ViewController: NSViewController { class ViewController: NSViewController {
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
@ -21,7 +20,4 @@ class ViewController: NSViewController {
// Update the view, if already loaded. // Update the view, if already loaded.
} }
} }
} }

View File

@ -9,11 +9,10 @@
import SafariServices import SafariServices
class SafariExtensionHandler: SFSafariExtensionHandler { 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 { properties in page.getPropertiesWithCompletionHandler { _ 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) SafariExtensionViewController.shared.sendMessage(msg: userInfo)
@ -22,12 +21,12 @@ class SafariExtensionHandler: SFSafariExtensionHandler {
} }
} }
override func toolbarItemClicked(in window: SFSafariWindow) { override func toolbarItemClicked(in _: SFSafariWindow) {
// This method will be called when your toolbar item is clicked. // This method will be called when your toolbar item is clicked.
// NSLog("The extension's toolbar item was clicked") // NSLog("The extension's toolbar item was clicked")
} }
override func validateToolbarItem(in window: SFSafariWindow, validationHandler: @escaping ((Bool, String) -> Void)) { override func validateToolbarItem(in _: SFSafariWindow, validationHandler: @escaping ((Bool, String) -> Void)) {
// This is called when Safari's state changed in some way that would require the extension's toolbar item to be validated again. // This is called when Safari's state changed in some way that would require the extension's toolbar item to be validated again.
validationHandler(true, "") validationHandler(true, "")
} }
@ -36,10 +35,9 @@ class SafariExtensionHandler: SFSafariExtensionHandler {
return SafariExtensionViewController.shared return SafariExtensionViewController.shared
} }
override func popoverWillShow(in window: SFSafariWindow) { override func popoverWillShow(in _: SFSafariWindow) {
DispatchQueue.main.async { DispatchQueue.main.async {
SafariExtensionViewController.shared.sendMessage(msg: ["command": "reloadPopup"]) SafariExtensionViewController.shared.sendMessage(msg: ["command": "reloadPopup"])
} }
} }
} }

View File

@ -10,7 +10,6 @@ import SafariServices
import WebKit import WebKit
class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMessageHandler, WKNavigationDelegate { class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMessageHandler, WKNavigationDelegate {
var webView: WKWebView! var webView: WKWebView!
static let shared: SafariExtensionViewController = { static let shared: SafariExtensionViewController = {
@ -32,11 +31,11 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe
webView.navigationDelegate = self webView.navigationDelegate = self
webView.allowsLinkPreview = false webView.allowsLinkPreview = false
webView.loadFileURL(html, allowingReadAccessTo: bundleURL) webView.loadFileURL(html, allowingReadAccessTo: bundleURL)
webView.alphaValue = 0.0; webView.alphaValue = 0.0
self.view.addSubview(webView) view.addSubview(webView)
} }
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { func webView(_ webView: WKWebView, didFinish _: WKNavigation!) {
NSAnimationContext.runAnimationGroup({ _ in NSAnimationContext.runAnimationGroup({ _ in
NSAnimationContext.current.duration = 0.35 NSAnimationContext.current.duration = 0.35
webView.animator().alphaValue = 1.0 webView.animator().alphaValue = 1.0
@ -45,37 +44,37 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
let backgroundColor = NSColor.init(red: (39/255.0), green: (42/255.0), blue: (46/255.0), alpha: 1.0) let backgroundColor = NSColor(red: (39 / 255.0), green: (42 / 255.0), blue: (46 / 255.0), alpha: 1.0)
view.setValue(backgroundColor, forKey: "backgroundColor") view.setValue(backgroundColor, forKey: "backgroundColor")
initWebView() initWebView()
} }
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { func userContentController(_: WKUserContentController, didReceive message: WKScriptMessage) {
if message.name == "bitwardenApp" { if message.name == "bitwardenApp" {
let messageBody = message.body as! String; let messageBody = message.body as! String
print(messageBody) print(messageBody)
let m: AppMessage? = jsonDeserialize(json: messageBody) let m: AppMessage? = jsonDeserialize(json: messageBody)
if(m == nil) { if m == nil {
print("m is nil") print("m is nil")
} else { } else {
let command = m?.command ?? "null" let command = m?.command ?? "null"
print(command) print(command)
if(command == "storage_get") { if command == "storage_get" {
let obj = UserDefaults.standard.string(forKey: m!.data!) let obj = UserDefaults.standard.string(forKey: m!.data!)
m!.responseData = obj m!.responseData = obj
replyMessage(message: m!) 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?.obj == nil {
UserDefaults.standard.removeObject(forKey: data!.key) UserDefaults.standard.removeObject(forKey: data!.key)
} else { } else {
UserDefaults.standard.set(data?.obj, forKey: data!.key) 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!) UserDefaults.standard.removeObject(forKey: m!.data!)
replyMessage(message: m!) replyMessage(message: m!)
} else if(command == "getLocaleStrings") { } else if command == "getLocaleStrings" {
let language = m!.data let language = m!.data
let bundleURL = Bundle.main.resourceURL!.absoluteURL let bundleURL = Bundle.main.resourceURL!.absoluteURL
let messagesUrl = bundleURL.appendingPathComponent("app/_locales/en/messages.json") let messagesUrl = bundleURL.appendingPathComponent("app/_locales/en/messages.json")
@ -84,55 +83,55 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe
webView.evaluateJavaScript("window.bitwardenLocaleStrings = \(json);", completionHandler: nil) webView.evaluateJavaScript("window.bitwardenLocaleStrings = \(json);", completionHandler: nil)
} catch {} } catch {}
replyMessage(message: m!) replyMessage(message: m!)
} else if(command == "tabs_query") { } else if command == "tabs_query" {
let options: TabQueryOptions? = jsonDeserialize(json: m!.data) let options: TabQueryOptions? = jsonDeserialize(json: m!.data)
if(options?.currentWindow ?? false) { if options?.currentWindow ?? false {
SFSafariApplication.getActiveWindow { (win) in SFSafariApplication.getActiveWindow { win in
processWindowsForTabs(wins: [win!], options: options processWindowsForTabs(wins: [win!], options: options
, complete: { (tabs) in , complete: { tabs in
m!.responseData = jsonSerialize(obj: tabs) m!.responseData = jsonSerialize(obj: tabs)
self.replyMessage(message: m!) self.replyMessage(message: m!)
}) })
} }
} else { } else {
SFSafariApplication.getAllWindows { (wins) in SFSafariApplication.getAllWindows { wins in
processWindowsForTabs(wins: wins, options: options processWindowsForTabs(wins: wins, options: options
, complete: { (tabs) in , complete: { tabs in
m!.responseData = jsonSerialize(obj: tabs) m!.responseData = jsonSerialize(obj: tabs)
self.replyMessage(message: m!) self.replyMessage(message: m!)
}) })
} }
} }
} else if(command == "tabs_message") { } else if command == "tabs_message" {
let tabMsg: TabMessage? = jsonDeserialize(json: m!.data) let tabMsg: TabMessage? = jsonDeserialize(json: m!.data)
SFSafariApplication.getAllWindows { (wins) in SFSafariApplication.getAllWindows { wins in
var theWin: SFSafariWindow? var theWin: SFSafariWindow?
var winIndex = 0 var winIndex = 0
for win in wins { for win in wins {
if(tabMsg?.tab.windowId == winIndex) { if tabMsg?.tab.windowId == winIndex {
theWin = win theWin = win
break break
} }
winIndex = winIndex + 1 winIndex = winIndex + 1
} }
if(theWin == nil) { if theWin == nil {
// TODO: error // TODO: error
} else { } else {
var theTab: SFSafariTab? var theTab: SFSafariTab?
theWin!.getAllTabs { (tabs) in theWin!.getAllTabs { tabs in
var tabIndex = 0 var tabIndex = 0
for tab in tabs { for tab in tabs {
if(tabMsg?.tab.index == tabIndex) { if tabMsg?.tab.index == tabIndex {
theTab = tab theTab = tab
break break
} }
tabIndex = tabIndex + 1 tabIndex = tabIndex + 1
} }
if(theTab == nil) { if theTab == nil {
// TODO: error // TODO: error
} else { } else {
theTab!.getActivePage { (activePage) in theTab!.getActivePage { activePage in
if(activePage != nil) { if activePage != nil {
activePage?.dispatchMessageToScript(withName: "bitwarden", userInfo: ["msg": tabMsg!.obj]) activePage?.dispatchMessageToScript(withName: "bitwarden", userInfo: ["msg": tabMsg!.obj])
} }
} }
@ -146,16 +145,16 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe
} }
func replyMessage(message: AppMessage) { func replyMessage(message: AppMessage) {
if(webView == nil) { if webView == nil {
return; return
} }
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]?) { func sendMessage(msg: [String: Any]?) {
if(webView == nil) { if webView == nil {
return; return
} }
let newMsg = AppMessage() let newMsg = AppMessage()
newMsg.command = "app_message" newMsg.command = "app_message"
@ -170,7 +169,7 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe
} }
func processWindowsForTabs(wins: [SFSafariWindow], options: TabQueryOptions?, complete: @escaping ([Tab]) -> Void) { func processWindowsForTabs(wins: [SFSafariWindow], options: TabQueryOptions?, complete: @escaping ([Tab]) -> Void) {
if(wins.count == 0) { if wins.count == 0 {
complete([]) complete([])
return return
} }
@ -179,15 +178,15 @@ func processWindowsForTabs(wins: [SFSafariWindow], options: TabQueryOptions?, co
var windowIndex = 0 var windowIndex = 0
for win in wins { for win in wins {
winGroup.enter() winGroup.enter()
win.getActiveTab { (activeTab) in win.getActiveTab { activeTab in
win.getAllTabs { (allTabs) in win.getAllTabs { allTabs in
let tabGroup = DispatchGroup() let tabGroup = DispatchGroup()
var tabIndex = 0 var tabIndex = 0
for tab in allTabs { for tab in allTabs {
tabGroup.enter() tabGroup.enter()
if(options?.active ?? false) { if options?.active ?? false {
if(activeTab != nil && activeTab == tab) { if activeTab != nil && activeTab == tab {
makeTabObject(tab: tab, activeTab: activeTab, windowIndex: windowIndex, tabIndex: tabIndex, complete: { (t) in makeTabObject(tab: tab, activeTab: activeTab, windowIndex: windowIndex, tabIndex: tabIndex, complete: { t in
newTabs.append(t) newTabs.append(t)
tabIndex = tabIndex + 1 tabIndex = tabIndex + 1
tabGroup.leave() tabGroup.leave()
@ -197,7 +196,7 @@ func processWindowsForTabs(wins: [SFSafariWindow], options: TabQueryOptions?, co
tabGroup.leave() tabGroup.leave()
} }
} else { } else {
makeTabObject(tab: tab, activeTab: activeTab, windowIndex: windowIndex, tabIndex: tabIndex, complete: { (t) in makeTabObject(tab: tab, activeTab: activeTab, windowIndex: windowIndex, tabIndex: tabIndex, complete: { t in
newTabs.append(t) newTabs.append(t)
tabIndex = tabIndex + 1 tabIndex = tabIndex + 1
tabGroup.leave() tabGroup.leave()
@ -222,11 +221,11 @@ func makeTabObject(tab: SFSafariTab, activeTab: SFSafariTab?, windowIndex: Int,
t.windowId = windowIndex t.windowId = windowIndex
t.index = tabIndex t.index = tabIndex
t.id = "\(windowIndex)_\(tabIndex)" t.id = "\(windowIndex)_\(tabIndex)"
tab.getActivePage { (page) in tab.getActivePage { page in
if(page == nil) { if page == nil {
complete(t) complete(t)
} else { } else {
page!.getPropertiesWithCompletionHandler({ (props) in page!.getPropertiesWithCompletionHandler({ props in
t.title = props?.title t.title = props?.title
t.url = props?.url?.absoluteString t.url = props?.url?.absoluteString
complete(t) complete(t)
@ -246,8 +245,8 @@ func jsonSerialize<T: Encodable>(obj: T?) -> String? {
} }
func jsonDeserialize<T: Decodable>(json: String?) -> T? { func jsonDeserialize<T: Decodable>(json: String?) -> T? {
if(json == nil) { if json == nil {
return nil; return nil
} }
let decoder = JSONDecoder() let decoder = JSONDecoder()
do { do {
@ -265,6 +264,7 @@ class AppMessage : Decodable, Encodable {
data = nil data = nil
responseData = nil responseData = nil
} }
var id: String var id: String
var command: String var command: String
var data: String? var data: String?