Extracted URL open functionality in preparation of launch arguments for incognito mode

This commit is contained in:
Michael Starke 2021-11-11 11:09:45 +01:00
parent 9d032dcbf4
commit 4f2888b759
5 changed files with 122 additions and 42 deletions

View File

@ -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 = "<group>"; };
4C50CC021F6C18830095629D /* MPCollectionViewItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPCollectionViewItem.h; sourceTree = "<group>"; };
4C50CC031F6C18830095629D /* MPCollectionViewItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPCollectionViewItem.m; sourceTree = "<group>"; };
4C52197F273D192C00C719D3 /* MPOpenURLHandler.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MPOpenURLHandler.h; sourceTree = "<group>"; };
4C521980273D192C00C719D3 /* MPOpenURLHandler.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MPOpenURLHandler.m; sourceTree = "<group>"; };
4C52A88B1788628B00868229 /* 06_BlockDeviceTemplate.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = 06_BlockDeviceTemplate.pdf; sourceTree = "<group>"; };
4C52A88C1788628B00868229 /* 13_KeysTemplate.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = 13_KeysTemplate.pdf; sourceTree = "<group>"; };
4C52A88D1788628B00868229 /* 18_DisplayTemplate.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = 18_DisplayTemplate.pdf; sourceTree = "<group>"; };
@ -1314,6 +1317,8 @@
4C4B7EF717A4B335000234C7 /* MPUniqueCharactersFormatter.m */,
4C3C4EAD18D7039300153127 /* MPValueTransformerHelper.h */,
4C3C4EAE18D7039300153127 /* MPValueTransformerHelper.m */,
4C52197F273D192C00C719D3 /* MPOpenURLHandler.h */,
4C521980273D192C00C719D3 /* MPOpenURLHandler.m */,
);
name = Helper;
sourceTree = "<group>";
@ -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 */,

View File

@ -19,13 +19,13 @@
<rect key="frame" x="0.0" y="0.0" width="691" height="331"/>
<subviews>
<scrollView focusRingType="none" borderType="none" autohidesScrollers="YES" horizontalLineScroll="19" horizontalPageScroll="10" verticalLineScroll="19" verticalPageScroll="10" usesPredominantAxisScrolling="NO" translatesAutoresizingMaskIntoConstraints="NO" id="54">
<rect key="frame" x="0.0" y="0.0" width="691" height="332"/>
<rect key="frame" x="0.0" y="0.0" width="691" height="308"/>
<clipView key="contentView" id="4tt-2K-SPF">
<rect key="frame" x="0.0" y="0.0" width="691" height="332"/>
<rect key="frame" x="0.0" y="0.0" width="691" height="308"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<tableView verticalHuggingPriority="750" allowsExpansionToolTips="YES" columnAutoresizingStyle="lastColumnOnly" alternatingRowBackgroundColors="YES" rowSizeStyle="automatic" headerView="676" viewBased="YES" id="55" customClass="MPTableView">
<rect key="frame" x="0.0" y="0.0" width="691" height="309"/>
<rect key="frame" x="0.0" y="0.0" width="691" height="285"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<size key="intercellSpacing" width="3" height="2"/>
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>

View File

@ -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<NSString *>*)_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<NSString *> *args = [[NSMutableArray alloc] init];
if(usePrivateBrowsing) {
NSArray<NSString *>* privateArgs = privateBrowsingArgs[bundleId];
if(privateBrowsingArgs) {
[args addObjectsFromArray:privateArgs];
}
}
return [args copy];
}
@end

View File

@ -0,0 +1,23 @@
//
// MPOpenURLHandler.h
// MacPass
//
// Created by Michael Starke on 11.11.21.
// Copyright © 2021 HicknHack Software GmbH. All rights reserved.
//
#import <Foundation/Foundation.h>
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

View File

@ -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<NSString *>*)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<NSString *>*)_launchArgumentsForBrowserBundleID:(NSString *)bundleId {
BOOL usePrivateBrowsing = [NSUserDefaults.standardUserDefaults boolForKey:kMPSettingsKeyUsePrivateBrowsingWhenOpeningURLs];
NSMutableArray<NSString *> *args = [[NSMutableArray alloc] init];
if(usePrivateBrowsing) {
NSArray<NSString *>* privateArgs = [self privateBrowsingArgsForBundleId:bundleId];
if(privateArgs) {
[args addObjectsFromArray:privateArgs];
}
}
return [args copy];
}
@end