diff --git a/MacPass.xcodeproj/project.pbxproj b/MacPass.xcodeproj/project.pbxproj index 05acab70..481f8823 100644 --- a/MacPass.xcodeproj/project.pbxproj +++ b/MacPass.xcodeproj/project.pbxproj @@ -134,6 +134,7 @@ 4C4F72D118DF704400E8D378 /* DDHotKeyTextField.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C4F72CF18DF704400E8D378 /* DDHotKeyTextField.m */; }; 4C4FCE15177CFE6B00BBF7AE /* MPCustomFieldTableCellView.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C4FCE14177CFE6B00BBF7AE /* MPCustomFieldTableCellView.m */; }; 4C50CC041F6C18830095629D /* MPCollectionViewItem.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C50CC031F6C18830095629D /* MPCollectionViewItem.m */; }; + 4C521981273D192C00C719D3 /* MPOpenURLHandler.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C521980273D192C00C719D3 /* MPOpenURLHandler.m */; }; 4C52A88E1788628B00868229 /* 06_BlockDeviceTemplate.pdf in Resources */ = {isa = PBXBuildFile; fileRef = 4C52A88B1788628B00868229 /* 06_BlockDeviceTemplate.pdf */; }; 4C52A88F1788628B00868229 /* 13_KeysTemplate.pdf in Resources */ = {isa = PBXBuildFile; fileRef = 4C52A88C1788628B00868229 /* 13_KeysTemplate.pdf */; }; 4C52A8901788628B00868229 /* 18_DisplayTemplate.pdf in Resources */ = {isa = PBXBuildFile; fileRef = 4C52A88D1788628B00868229 /* 18_DisplayTemplate.pdf */; }; @@ -582,6 +583,8 @@ 4C4FCE14177CFE6B00BBF7AE /* MPCustomFieldTableCellView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPCustomFieldTableCellView.m; sourceTree = ""; }; 4C50CC021F6C18830095629D /* MPCollectionViewItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPCollectionViewItem.h; sourceTree = ""; }; 4C50CC031F6C18830095629D /* MPCollectionViewItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPCollectionViewItem.m; sourceTree = ""; }; + 4C52197F273D192C00C719D3 /* MPOpenURLHandler.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MPOpenURLHandler.h; sourceTree = ""; }; + 4C521980273D192C00C719D3 /* MPOpenURLHandler.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MPOpenURLHandler.m; sourceTree = ""; }; 4C52A88B1788628B00868229 /* 06_BlockDeviceTemplate.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = 06_BlockDeviceTemplate.pdf; sourceTree = ""; }; 4C52A88C1788628B00868229 /* 13_KeysTemplate.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = 13_KeysTemplate.pdf; sourceTree = ""; }; 4C52A88D1788628B00868229 /* 18_DisplayTemplate.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = 18_DisplayTemplate.pdf; sourceTree = ""; }; @@ -1314,6 +1317,8 @@ 4C4B7EF717A4B335000234C7 /* MPUniqueCharactersFormatter.m */, 4C3C4EAD18D7039300153127 /* MPValueTransformerHelper.h */, 4C3C4EAE18D7039300153127 /* MPValueTransformerHelper.m */, + 4C52197F273D192C00C719D3 /* MPOpenURLHandler.h */, + 4C521980273D192C00C719D3 /* MPOpenURLHandler.m */, ); name = Helper; sourceTree = ""; @@ -2308,6 +2313,7 @@ 4C0728BD17B5B7F7005A7DD9 /* MPPasswordEditWindowController.m in Sources */, 4C0F647B17B6BC9C00D9522A /* MPSavePanelAccessoryViewController.m in Sources */, 4C0949591FD6B89B004F2971 /* NSUserNotification+MPAdditions.m in Sources */, + 4C521981273D192C00C719D3 /* MPOpenURLHandler.m in Sources */, 4C2057EE23CDF6F900C731EC /* MPPathCell.m in Sources */, 4C3B42871F935316007B04FD /* MPDayCountFormatter.m in Sources */, 4C7D9D5424ABE6FD00907034 /* MPTabViewController.m in Sources */, diff --git a/MacPass/EntryView.xib b/MacPass/EntryView.xib index 93081a00..c1844c5d 100644 --- a/MacPass/EntryView.xib +++ b/MacPass/EntryView.xib @@ -19,13 +19,13 @@ - + - + - + diff --git a/MacPass/MPEntryViewController.m b/MacPass/MPEntryViewController.m index d42460d5..a1176596 100644 --- a/MacPass/MPEntryViewController.m +++ b/MacPass/MPEntryViewController.m @@ -49,6 +49,8 @@ #import "HNHUi/HNHUi.h" +#import "MPOpenURLHandler.h" + #import "MPNotifications.h" #define STATUS_BAR_ANIMATION_TIME 0.15 @@ -752,27 +754,7 @@ NSString *const _MPTableMonoSpacedStringCellView = @"MonospacedStringCell"; KPKEntry *selectedEntry = nodes.count == 1 ? [nodes.firstObject asEntry] : nil; NSString *expandedURL = [selectedEntry.url kpk_finalValueForEntry:selectedEntry]; if(expandedURL.length > 0) { - NSURL *webURL = [NSURL URLWithString:expandedURL]; - NSString *scheme = webURL.scheme; - if(!scheme) { - webURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", expandedURL]]; - } - - NSString *browserBundleID = [NSUserDefaults.standardUserDefaults stringForKey:kMPSettingsKeyBrowserBundleId]; - NSURL *browserApplicationURL = browserBundleID ? [NSWorkspace.sharedWorkspace URLForApplicationWithBundleIdentifier:browserBundleID] : nil; - BOOL openedURL = NO; - - if(browserApplicationURL) { - NSRunningApplication *urlOpeningApplication = [NSWorkspace.sharedWorkspace openURLs:@[webURL] withApplicationAtURL:browserApplicationURL options:NSWorkspaceLaunchDefault configuration:@{} error:nil]; - openedURL = nil != urlOpeningApplication; - } - - if(!openedURL) { - openedURL = [NSWorkspace.sharedWorkspace openURL:webURL]; - } - if(!openedURL) { - NSLog(@"Unable to open URL %@", webURL); - } + [MPOpenURLHandler.sharedHandler openURL:expandedURL]; } } @@ -873,22 +855,4 @@ NSString *const _MPTableMonoSpacedStringCellView = @"MonospacedStringCell"; } } -- (NSArray*)_launchArgumentsForBrowserBundleID:(NSString *)bundleId { - static NSDictionary *privateBrowsingArgs; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - privateBrowsingArgs = @{ @"com.google.Chrome" : @[@"--incognito"] }; - }); - - BOOL usePrivateBrowsing = [NSUserDefaults.standardUserDefaults boolForKey:kMPSettingsKeyUsePrivateBrowsingWhenOpeningURLs]; - NSMutableArray *args = [[NSMutableArray alloc] init]; - if(usePrivateBrowsing) { - NSArray* privateArgs = privateBrowsingArgs[bundleId]; - if(privateBrowsingArgs) { - [args addObjectsFromArray:privateArgs]; - } - } - return [args copy]; -} - @end diff --git a/MacPass/MPOpenURLHandler.h b/MacPass/MPOpenURLHandler.h new file mode 100644 index 00000000..2e901a76 --- /dev/null +++ b/MacPass/MPOpenURLHandler.h @@ -0,0 +1,23 @@ +// +// MPOpenURLHandler.h +// MacPass +// +// Created by Michael Starke on 11.11.21. +// Copyright © 2021 HicknHack Software GmbH. All rights reserved. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface MPOpenURLHandler : NSObject + +@property (class, strong, readonly) MPOpenURLHandler *sharedHandler; +- (instancetype)init NS_UNAVAILABLE; + +- (void)openURL:(NSString *)url; +- (BOOL)supportsPrivateBrowsingForBundleId:(NSString *)bundleId; + +@end + +NS_ASSUME_NONNULL_END diff --git a/MacPass/MPOpenURLHandler.m b/MacPass/MPOpenURLHandler.m new file mode 100644 index 00000000..9163bade --- /dev/null +++ b/MacPass/MPOpenURLHandler.m @@ -0,0 +1,87 @@ +// +// MPOpenURLHandler.m +// MacPass +// +// Created by Michael Starke on 11.11.21. +// Copyright © 2021 HicknHack Software GmbH. All rights reserved. +// + +#import "MPOpenURLHandler.h" + +#import "MPSettingsHelper.h" + +@implementation MPOpenURLHandler + +static MPOpenURLHandler *_defaultInstance; + ++ (MPOpenURLHandler *)sharedHandler { + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + _defaultInstance = [[MPOpenURLHandler alloc] _init]; + }); + return _defaultInstance; +} + +- (instancetype)init { + return _defaultInstance; +} + +- (NSArray*)privateBrowsingArgsForBundleId:(NSString *)bundleId { + + static NSDictionary *privateBrowsingArgs; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + privateBrowsingArgs = @{ @"com.google.Chrome" : @[@"--incognito"] }; + }); + return privateBrowsingArgs[bundleId]; +} + + +- (instancetype)_init { + NSAssert(_defaultInstance == nil, @"Multiple instances of MPLockDaemon not allowed!"); + self = [super init]; + return self; +} + +- (void)openURL:(NSString *)url { + NSURL *webURL = [NSURL URLWithString:url]; + NSString *scheme = webURL.scheme; + if(!scheme) { + webURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", url]]; + } + + NSString *browserBundleID = [NSUserDefaults.standardUserDefaults stringForKey:kMPSettingsKeyBrowserBundleId]; + NSURL *browserApplicationURL = browserBundleID ? [NSWorkspace.sharedWorkspace URLForApplicationWithBundleIdentifier:browserBundleID] : nil; + BOOL openedURL = NO; + + if(browserApplicationURL) { + NSRunningApplication *urlOpeningApplication = [NSWorkspace.sharedWorkspace openURLs:@[webURL] withApplicationAtURL:browserApplicationURL options:NSWorkspaceLaunchDefault configuration:@{} error:nil]; + openedURL = nil != urlOpeningApplication; + } + + if(!openedURL) { + openedURL = [NSWorkspace.sharedWorkspace openURL:webURL]; + } + if(!openedURL) { + NSLog(@"Unable to open URL %@", webURL); + } +} + +- (BOOL)supportsPrivateBrowsingForBundleId:(NSString *)bundleId { + return (nil != [self privateBrowsingArgsForBundleId:bundleId]); +} + +- (NSArray*)_launchArgumentsForBrowserBundleID:(NSString *)bundleId { + BOOL usePrivateBrowsing = [NSUserDefaults.standardUserDefaults boolForKey:kMPSettingsKeyUsePrivateBrowsingWhenOpeningURLs]; + NSMutableArray *args = [[NSMutableArray alloc] init]; + if(usePrivateBrowsing) { + NSArray* privateArgs = [self privateBrowsingArgsForBundleId:bundleId]; + if(privateArgs) { + [args addObjectsFromArray:privateArgs]; + } + } + return [args copy]; +} + + +@end