94 lines
3.2 KiB
Objective-C
94 lines
3.2 KiB
Objective-C
//
|
|
// SBRProcessBundleBridge.m
|
|
// SBrowser
|
|
//
|
|
// Created by James Magahern on 7/22/20.
|
|
//
|
|
|
|
#import "SBRProcessBundleBridge.h"
|
|
|
|
#import "SBRWebProcessDelegate.h"
|
|
#import "SBRWebProcessProxy.h"
|
|
|
|
#import <WebKit/_WKRemoteObjectInterface.h>
|
|
#import <WebKit/_WKRemoteObjectRegistry.h>
|
|
#import <WebKit/_WKProcessPoolConfiguration.h>
|
|
|
|
#import <WebKit/WKProcessPoolPrivate.h>
|
|
#import <WebKit/WKWebViewPrivate.h>
|
|
#import <WebKit/WKWebViewConfigurationPrivate.h>
|
|
|
|
@interface SBRProcessBundleBridge () <SBRWebProcessDelegate>
|
|
|
|
@end
|
|
|
|
@implementation SBRProcessBundleBridge {
|
|
WKWebView *_webView;
|
|
id<SBRWebProcessProxy> _webProcessProxy;
|
|
}
|
|
|
|
- (WKWebView *)webView
|
|
{
|
|
if (!_webView) {
|
|
// Inject bundle
|
|
_WKProcessPoolConfiguration *poolConfiguration = [[_WKProcessPoolConfiguration alloc] init];
|
|
NSURL *bundleURL = [[[NSBundle mainBundle] builtInPlugInsURL] URLByAppendingPathComponent:@"SBrowserProcessBundle.bundle"];
|
|
[poolConfiguration setInjectedBundleURL:bundleURL];
|
|
|
|
// Set up process pool
|
|
WKProcessPool *processPool = [[WKProcessPool alloc] _initWithConfiguration:poolConfiguration];
|
|
|
|
// Initialize allowed origins now
|
|
NSArray<NSString *> *allowedOrigins = [[_policyDataSource allowedOriginsForScriptResources] allObjects];
|
|
[processPool _setObject:allowedOrigins forBundleParameter:SBRGetAllowedOriginsKey()];
|
|
[processPool _setObject:@(_allowAllScripts) forBundleParameter:SBRGetAllScriptsAllowedKey()];
|
|
|
|
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
|
|
configuration.processPool = processPool;
|
|
|
|
// Instantiate web view
|
|
WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
|
|
|
|
// Configure proxy interface (interface to remote web process)
|
|
_WKRemoteObjectInterface *proxyInterface = [_WKRemoteObjectInterface remoteObjectInterfaceWithProtocol:@protocol(SBRWebProcessProxy)];
|
|
_webProcessProxy = [[webView _remoteObjectRegistry] remoteObjectProxyWithInterface:proxyInterface];
|
|
|
|
// Configure delegate interface (registering us as the web process delegate for the remote process)
|
|
_WKRemoteObjectInterface *delegateInterface = [_WKRemoteObjectInterface remoteObjectInterfaceWithProtocol:@protocol(SBRWebProcessDelegate)];
|
|
[[webView _remoteObjectRegistry] registerExportedObject:self interface:delegateInterface];
|
|
|
|
_webView = webView;
|
|
}
|
|
|
|
return _webView;
|
|
}
|
|
|
|
#pragma mark <SBRWebProcessDelegate>
|
|
|
|
- (void)webProcessDidConnect
|
|
{
|
|
NSLog(@"SBRProcessBundleBridge: did connect. Saying hello, syncing allowlist");
|
|
[_webProcessProxy hello];
|
|
[self policyDataSourceDidChange];
|
|
}
|
|
|
|
- (void)webProcessDidLoadScriptWithOrigin:(NSString *)origin
|
|
{
|
|
[_delegate webProcess:self didBlockScriptResourceFromOrigin:origin];
|
|
}
|
|
|
|
#pragma mark Actions
|
|
|
|
- (void)policyDataSourceDidChange
|
|
{
|
|
NSArray<NSString *> *allowedOrigins = [[_policyDataSource allowedOriginsForScriptResources] allObjects];
|
|
[_webProcessProxy syncAllowedResourceOrigins:allowedOrigins];
|
|
}
|
|
|
|
- (void)setAllowAllScripts:(BOOL)allowAllScripts
|
|
{
|
|
[_webProcessProxy setAllScriptsAllowed:allowAllScripts];
|
|
}
|
|
|
|
@end
|