Delete web process bundle in favor of custom scheme handler
This commit is contained in:
@@ -36,8 +36,6 @@ NS_SWIFT_NAME(ProcessBundleBridge)
|
||||
@property (nonatomic, assign) BOOL allowAllScripts; // default is NO
|
||||
@property (nonatomic, assign) BOOL darkModeEnabled;
|
||||
|
||||
@property (nonatomic, readonly) BOOL webContentProcessConnected;
|
||||
|
||||
- (instancetype)initWithWebViewConfiguration:(nullable WKWebViewConfiguration *)webViewConfiguration NS_DESIGNATED_INITIALIZER;
|
||||
|
||||
- (void)policyDataSourceDidChange;
|
||||
|
||||
@@ -7,8 +7,9 @@
|
||||
|
||||
#import "SBRProcessBundleBridge.h"
|
||||
|
||||
#import "SBRWebProcessDelegate.h"
|
||||
#import "SBRWebProcessProxy.h"
|
||||
#import "SBRScriptPolicy.h"
|
||||
|
||||
#import <OSLog/OSLog.h>
|
||||
|
||||
#import <WebKit/_WKRemoteObjectInterface.h>
|
||||
#import <WebKit/_WKRemoteObjectRegistry.h>
|
||||
@@ -20,21 +21,51 @@
|
||||
#import <WebKit/WKWebViewConfigurationPrivate.h>
|
||||
#import <WebKit/WKUserContentControllerPrivate.h>
|
||||
|
||||
@interface SBRProcessBundleBridge () <SBRWebProcessDelegate>
|
||||
#define LOG_DEBUG(format, ...) os_log_debug(_log, format, ##__VA_ARGS__)
|
||||
#define LOG_ERROR(format, ...) os_log_error(_log, format, ##__VA_ARGS__)
|
||||
|
||||
@interface NSURLResponse (BridgeAdditions)
|
||||
@property (nonatomic, readonly) BOOL isJavascriptResponse;
|
||||
@end
|
||||
|
||||
@implementation NSURLResponse (BridgeAdditions)
|
||||
|
||||
- (BOOL)isJavascriptResponse
|
||||
{
|
||||
NSString *extension = [[self URL] pathExtension];
|
||||
if ([[extension lowercaseString] isEqualToString:@"js"]) {
|
||||
return YES;
|
||||
}
|
||||
|
||||
NSString *MIMEType = [self MIMEType];
|
||||
if ([[MIMEType lowercaseString] containsString:@"javascript"]) {
|
||||
return YES;
|
||||
}
|
||||
|
||||
return NO;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@interface SBRProcessBundleBridge () <WKURLSchemeHandler>
|
||||
|
||||
@end
|
||||
|
||||
@implementation SBRProcessBundleBridge {
|
||||
os_log_t _log;
|
||||
|
||||
WKWebView *_webView;
|
||||
WKWebViewConfiguration *_webViewConfiguration;
|
||||
WKProcessPool *_processPool;
|
||||
id<SBRWebProcessProxy> _webProcessProxy;
|
||||
|
||||
_WKUserStyleSheet *_darkModeStyleSheet;
|
||||
WKUserScript *_readabilityScript;
|
||||
|
||||
NSArray<WKUserScript *> *_userScripts;
|
||||
|
||||
dispatch_queue_t _dataTasksAccessQueue;
|
||||
NSMutableDictionary<NSURLRequest *, NSURLSessionDataTask *> *_dataTasks;
|
||||
|
||||
// These come from settings.
|
||||
_WKUserStyleSheet *_customizedUserStylesheet;
|
||||
WKUserScript *_customizedUserScript;
|
||||
@@ -42,29 +73,7 @@
|
||||
|
||||
- (void)tearDown
|
||||
{
|
||||
[[_webView _remoteObjectRegistry] unregisterExportedObject:self interface:[self _webProcessDelegateInterface]];
|
||||
}
|
||||
|
||||
- (_WKRemoteObjectInterface *)_webProcessDelegateInterface
|
||||
{
|
||||
static dispatch_once_t onceToken;
|
||||
static _WKRemoteObjectInterface *interface = nil;
|
||||
dispatch_once(&onceToken, ^{
|
||||
interface = [_WKRemoteObjectInterface remoteObjectInterfaceWithProtocol:@protocol(SBRWebProcessDelegate)];
|
||||
});
|
||||
|
||||
return interface;
|
||||
}
|
||||
|
||||
- (_WKRemoteObjectInterface *)_webProcessProxyInterface
|
||||
{
|
||||
static dispatch_once_t onceToken;
|
||||
static _WKRemoteObjectInterface *interface = nil;
|
||||
dispatch_once(&onceToken, ^{
|
||||
interface = [_WKRemoteObjectInterface remoteObjectInterfaceWithProtocol:@protocol(SBRWebProcessProxy)];
|
||||
});
|
||||
|
||||
return interface;
|
||||
// This was used to unregister the delegate with the web process.
|
||||
}
|
||||
|
||||
- (instancetype)initWithWebViewConfiguration:(WKWebViewConfiguration *)webViewConfiguration
|
||||
@@ -72,32 +81,22 @@
|
||||
self = [super init];
|
||||
if (self) {
|
||||
if (!webViewConfiguration) {
|
||||
_log = os_log_create("net.buzzert.attractor.webview", "bridge");
|
||||
|
||||
webViewConfiguration = [[WKWebViewConfiguration alloc] init];
|
||||
|
||||
// Inject bundle
|
||||
_WKProcessPoolConfiguration *poolConfiguration = [[_WKProcessPoolConfiguration alloc] init];
|
||||
NSURL *bundleURL = [[[NSBundle mainBundle] builtInPlugInsURL] URLByAppendingPathComponent:@"SBrowserProcessBundle.bundle"];
|
||||
|
||||
// Make sure it exists. Bail if otherwise.
|
||||
NSBundle *pluginBundle = [NSBundle bundleWithURL:bundleURL];
|
||||
NSAssert(pluginBundle != nil, @"Attix process bundle not found at %@", bundleURL.path);
|
||||
|
||||
[poolConfiguration setInjectedBundleURL:bundleURL];
|
||||
|
||||
// Set up process pool
|
||||
_processPool = [[WKProcessPool alloc] _initWithConfiguration:poolConfiguration];
|
||||
|
||||
// Initialize allowed origins now
|
||||
NSDictionary<NSString *, NSNumber *> *policies = [_policyDataSource scriptPolicyTypeByOrigin];
|
||||
NSArray<NSString *> *allowedOrigins = [[_policyDataSource allowedOriginsForScriptResources] allObjects];
|
||||
[_processPool _setObject:allowedOrigins forBundleParameter:SBRGetAllowedOriginsKey()];
|
||||
[_processPool _setObject:@(_allowAllScripts) forBundleParameter:SBRGetAllScriptsAllowedKey()];
|
||||
[_processPool _setObject:policies forBundleParameter:SBRGetPolicyTypeByOriginKey()];
|
||||
|
||||
_processPool = [[WKProcessPool alloc] init];
|
||||
webViewConfiguration.processPool = _processPool;
|
||||
|
||||
webViewConfiguration._waitsForPaintAfterViewDidMoveToWindow = NO;
|
||||
webViewConfiguration._applePayEnabled = YES;
|
||||
|
||||
_dataTasks = [NSMutableDictionary dictionary];
|
||||
_dataTasksAccessQueue = dispatch_queue_create("net.buzzert.attractor.dataTasksAccess", DISPATCH_QUEUE_SERIAL);
|
||||
|
||||
[webViewConfiguration setURLSchemeHandler:self forURLScheme:@"http"];
|
||||
[webViewConfiguration setURLSchemeHandler:self forURLScheme:@"https"];
|
||||
}
|
||||
|
||||
_webViewConfiguration = webViewConfiguration;
|
||||
@@ -117,12 +116,6 @@
|
||||
webView.findInteractionEnabled = YES;
|
||||
}
|
||||
|
||||
// Configure proxy interface (interface to remote web process)
|
||||
_webProcessProxy = [[webView _remoteObjectRegistry] remoteObjectProxyWithInterface:[self _webProcessProxyInterface]];
|
||||
|
||||
// Configure delegate interface (registering us as the web process delegate for the remote process)
|
||||
[[webView _remoteObjectRegistry] registerExportedObject:self interface:[self _webProcessDelegateInterface]];
|
||||
|
||||
_webView = webView;
|
||||
}
|
||||
|
||||
@@ -172,45 +165,113 @@
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark <SBRWebProcessDelegate>
|
||||
|
||||
- (void)webProcessDidConnect
|
||||
{
|
||||
NSLog(@"SBRProcessBundleBridge: did connect. Saying hello, syncing allowlist");
|
||||
_webContentProcessConnected = YES;
|
||||
|
||||
[_webProcessProxy hello];
|
||||
[self policyDataSourceDidChange];
|
||||
}
|
||||
#pragma mark Former <SBRWebProcessDelegate> methods
|
||||
|
||||
- (void)webProcessDidAllowScriptWithOrigin:(NSString *)origin
|
||||
{
|
||||
[[self delegate] webProcess:self didAllowScriptResourceFromOrigin:origin];
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[[self delegate] webProcess:self didAllowScriptResourceFromOrigin:origin];
|
||||
});
|
||||
}
|
||||
|
||||
- (void)webProcessDidBlockScriptWithOrigin:(NSString *)origin
|
||||
{
|
||||
[[self delegate] webProcess:self didBlockScriptResourceFromOrigin:origin];
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[[self delegate] webProcess:self didBlockScriptResourceFromOrigin:origin];
|
||||
});
|
||||
}
|
||||
|
||||
#pragma mark <WKURLSchemeHandler>
|
||||
|
||||
- (void)webView:(WKWebView *)webView startURLSchemeTask:(id<WKURLSchemeTask>)urlSchemeTask
|
||||
{
|
||||
NSString *hostOrigin = [[_webView URL] host];
|
||||
NSURLRequest *request = [urlSchemeTask request];
|
||||
|
||||
LOG_DEBUG("Start URL scheme task: request: %@", request);
|
||||
|
||||
__weak __auto_type welf = self;
|
||||
NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
|
||||
{
|
||||
if (!welf) return;
|
||||
__strong __auto_type sself = welf;
|
||||
|
||||
if (error != nil) {
|
||||
[urlSchemeTask didFailWithError:error];
|
||||
} else if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
|
||||
NSURL *requestURL = [request URL];
|
||||
NSString *resourceOrigin = [requestURL host];
|
||||
const __auto_type allowResource = ^{
|
||||
os_log_debug(sself->_log, "Allowing resource: %@", requestURL.lastPathComponent);
|
||||
[urlSchemeTask didReceiveResponse:response];
|
||||
[urlSchemeTask didReceiveData:data];
|
||||
[urlSchemeTask didFinish];
|
||||
|
||||
[self webProcessDidAllowScriptWithOrigin:resourceOrigin];
|
||||
};
|
||||
|
||||
const __auto_type denyResource = ^{
|
||||
os_log_debug(sself->_log, "Blocking resource: %@", requestURL.lastPathComponent);
|
||||
NSHTTPURLResponse *altResponse = [[NSHTTPURLResponse alloc] initWithURL:requestURL
|
||||
MIMEType:@"application/javascript"
|
||||
expectedContentLength:0 textEncodingName:@"utf8"];
|
||||
[urlSchemeTask didReceiveResponse:altResponse];
|
||||
[urlSchemeTask didReceiveData:[NSData data]];
|
||||
[urlSchemeTask didFinish];
|
||||
|
||||
[self webProcessDidBlockScriptWithOrigin:resourceOrigin];
|
||||
};
|
||||
|
||||
// Check MIME type for JavaScript responses.
|
||||
if ([response isJavascriptResponse] && ![sself allowAllScripts]) {
|
||||
dispatch_async(sself->_dataTasksAccessQueue, ^{
|
||||
NSDictionary<NSString *, NSNumber *> *policyTypes = [sself->_policyDataSource scriptPolicyTypeByOrigin];
|
||||
NSNumber *policyType = [policyTypes objectForKey:hostOrigin];
|
||||
|
||||
SBRScriptPolicy *policy = [[SBRScriptPolicy alloc] initWithSecurityOrigin:hostOrigin policyType:[policyType integerValue]];
|
||||
if ([policy allowsExternalJavaScriptResourceOrigin:resourceOrigin]) {
|
||||
allowResource();
|
||||
} else {
|
||||
denyResource();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
allowResource();
|
||||
}
|
||||
} else {
|
||||
[urlSchemeTask didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:0 userInfo:nil]];
|
||||
}
|
||||
|
||||
[sself->_dataTasks removeObjectForKey:request];
|
||||
}];
|
||||
|
||||
[_dataTasks setObject:dataTask forKey:request];
|
||||
[dataTask resume];
|
||||
}
|
||||
|
||||
- (void)webView:(WKWebView *)webView stopURLSchemeTask:(id<WKURLSchemeTask>)urlSchemeTask
|
||||
{
|
||||
NSURLRequest *request = [urlSchemeTask request];
|
||||
NSURLSessionDataTask *dataTask = [_dataTasks objectForKey:request];
|
||||
if (dataTask) {
|
||||
if ([dataTask state] != NSURLSessionTaskStateCanceling) {
|
||||
[dataTask cancel];
|
||||
}
|
||||
|
||||
[_dataTasks removeObjectForKey:request];
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark Actions
|
||||
|
||||
- (void)policyDataSourceDidChange
|
||||
{
|
||||
NSArray<NSString *> *allowedOrigins = [[_policyDataSource allowedOriginsForScriptResources] allObjects];
|
||||
[_processPool _setObject:allowedOrigins forBundleParameter:SBRGetAllowedOriginsKey()];
|
||||
[_webProcessProxy syncAllowedResourceOrigins:allowedOrigins];
|
||||
|
||||
NSDictionary<NSString *, NSNumber *> *policyTypes = [_policyDataSource scriptPolicyTypeByOrigin];
|
||||
[_processPool _setObject:policyTypes forBundleParameter:SBRGetPolicyTypeByOriginKey()];
|
||||
[_webProcessProxy syncPolicyTypes:policyTypes];
|
||||
// This was used when we had to signal the process bundle.
|
||||
}
|
||||
|
||||
- (void)setAllowAllScripts:(BOOL)allowAllScripts
|
||||
{
|
||||
_allowAllScripts = allowAllScripts;
|
||||
[_processPool _setObject:@(_allowAllScripts) forBundleParameter:SBRGetAllScriptsAllowedKey()];
|
||||
[_webProcessProxy setAllScriptsAllowed:allowAllScripts];
|
||||
}
|
||||
|
||||
- (void)setDarkModeEnabled:(BOOL)darkModeEnabled
|
||||
@@ -232,7 +293,6 @@
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
- (void)parseDocumentForReaderMode:(void (^)(NSString * _Nonnull))completionBlock
|
||||
{
|
||||
WKUserContentController *userContentController = [_webViewConfiguration userContentController];
|
||||
@@ -248,9 +308,10 @@
|
||||
"var article = new Readability(documentClone).parse();"
|
||||
"article.content";
|
||||
|
||||
os_log_t log = _log;
|
||||
[_webView evaluateJavaScript:script completionHandler:^(NSString *result, NSError * _Nullable error) {
|
||||
if (error != nil) {
|
||||
NSLog(@"Bridge: Readability error: %@", error.localizedDescription);
|
||||
os_log_error(log, "Bridge: Readability error: %@", error.localizedDescription);
|
||||
} else {
|
||||
completionBlock(result);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user