Proj reorg

This commit is contained in:
James Magahern
2020-07-22 19:32:45 -07:00
parent cddc684f54
commit 6efd8bbb78
3 changed files with 10 additions and 2 deletions

View File

@@ -0,0 +1,37 @@
//
// SBRProcessBundleBridge.h
// SBrowser
//
// Created by James Magahern on 7/22/20.
//
#import <Foundation/Foundation.h>
#import <WebKit/WebKit.h>
NS_ASSUME_NONNULL_BEGIN
@protocol SBRResourceOriginPolicyDataSource <NSObject>
/// Returns a list of origins (e.g., "buzzert.net") for which we are allowed to load script resources from
- (NSSet<NSString *> *)allowedOriginsForScriptResources;
@end
@class SBRProcessBundleBridge;
@protocol SBRProcessBundleBridgeDelegate <NSObject>
- (void)webProcess:(SBRProcessBundleBridge *)bridge didBlockScriptResourceFromOrigin:(NSString *)origin;
@end
@interface SBRProcessBundleBridge : NSObject
@property (nonatomic, readonly) WKWebView *webView;
@property (nonatomic, weak) id<SBRProcessBundleBridgeDelegate> delegate;
@property (nonatomic, weak) id<SBRResourceOriginPolicyDataSource> policyDataSource;
- (void)policyDataSourceDidChange;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,82 @@
//
// 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];
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
{
NSSet<NSString *> *allowedOrigins = [_policyDataSource allowedOriginsForScriptResources];
[_webProcessProxy syncAllowedResourceOrigins:allowedOrigins];
}
@end