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

@@ -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