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:
parent
05684b3b03
commit
da9bd3a8db
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,7 +9,6 @@
|
|||||||
import Cocoa
|
import Cocoa
|
||||||
|
|
||||||
class ViewController: NSViewController {
|
class ViewController: NSViewController {
|
||||||
|
|
||||||
override func viewDidLoad() {
|
override func viewDidLoad() {
|
||||||
super.viewDidLoad()
|
super.viewDidLoad()
|
||||||
|
|
||||||
@ -18,10 +17,7 @@ class ViewController: NSViewController {
|
|||||||
|
|
||||||
override var representedObject: Any? {
|
override var representedObject: Any? {
|
||||||
didSet {
|
didSet {
|
||||||
// Update the view, if already loaded.
|
// Update the view, if already loaded.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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)
|
||||||
@ -21,25 +20,24 @@ 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, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
override func popoverViewController() -> SFSafariExtensionViewController {
|
override func popoverViewController() -> SFSafariExtensionViewController {
|
||||||
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"])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -10,15 +10,14 @@ 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 = {
|
||||||
let shared = SafariExtensionViewController()
|
let shared = SafariExtensionViewController()
|
||||||
shared.preferredContentSize = NSSize(width:375, height:600)
|
shared.preferredContentSize = NSSize(width: 375, height: 600)
|
||||||
return shared
|
return shared
|
||||||
}()
|
}()
|
||||||
|
|
||||||
func initWebView() {
|
func initWebView() {
|
||||||
let parentHeight = SafariExtensionViewController.shared.preferredContentSize.height
|
let parentHeight = SafariExtensionViewController.shared.preferredContentSize.height
|
||||||
let parentWidth = SafariExtensionViewController.shared.preferredContentSize.width
|
let parentWidth = SafariExtensionViewController.shared.preferredContentSize.width
|
||||||
@ -32,107 +31,107 @@ 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
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
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")
|
||||||
do {
|
do {
|
||||||
let json = try String(contentsOf: messagesUrl, encoding: .utf8)
|
let json = try String(contentsOf: messagesUrl, encoding: .utf8)
|
||||||
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])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -144,18 +143,18 @@ 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,21 +196,21 @@ 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()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tabGroup.notify(queue: .main){
|
tabGroup.notify(queue: .main) {
|
||||||
windowIndex = windowIndex + 1
|
windowIndex = windowIndex + 1
|
||||||
winGroup.leave()
|
winGroup.leave()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
winGroup.notify(queue: .main){
|
winGroup.notify(queue: .main) {
|
||||||
complete(newTabs)
|
complete(newTabs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -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 {
|
||||||
@ -258,30 +257,31 @@ func jsonDeserialize<T: Decodable>(json: String?) -> T? {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class AppMessage : Decodable, Encodable {
|
class AppMessage: Decodable, Encodable {
|
||||||
init() {
|
init() {
|
||||||
id = ""
|
id = ""
|
||||||
command = ""
|
command = ""
|
||||||
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?
|
||||||
var responseData: String?
|
var responseData: String?
|
||||||
}
|
}
|
||||||
|
|
||||||
class StorageData : Decodable, Encodable {
|
class StorageData: Decodable, Encodable {
|
||||||
var key: String
|
var key: String
|
||||||
var obj: String?
|
var obj: String?
|
||||||
}
|
}
|
||||||
|
|
||||||
class TabQueryOptions : Decodable, Encodable {
|
class TabQueryOptions: Decodable, Encodable {
|
||||||
var currentWindow: Bool?
|
var currentWindow: Bool?
|
||||||
var active: Bool?
|
var active: Bool?
|
||||||
}
|
}
|
||||||
|
|
||||||
class Tab : Decodable, Encodable {
|
class Tab: Decodable, Encodable {
|
||||||
init() {
|
init() {
|
||||||
id = ""
|
id = ""
|
||||||
index = -1
|
index = -1
|
||||||
@ -290,7 +290,7 @@ class Tab : Decodable, Encodable {
|
|||||||
active = false
|
active = false
|
||||||
url = ""
|
url = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
var id: String
|
var id: String
|
||||||
var index: Int
|
var index: Int
|
||||||
var windowId: Int
|
var windowId: Int
|
||||||
|
Loading…
Reference in New Issue
Block a user