-
Notifications
You must be signed in to change notification settings - Fork 3
feat: support async onShouldStartLoad requests #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
1e6070c
adfe5ff
8a11f80
4b0427b
20b4976
33f9051
b6cad2e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| #import <React/RCTConvert.h> | ||
| #import <React/RCTAutoInsetsProtocol.h> | ||
| #import "RNCWKProcessPoolManager.h" | ||
| #import "RNCWebViewDecisionManager.h" | ||
| #if !TARGET_OS_OSX | ||
| #import <UIKit/UIKit.h> | ||
| #else | ||
|
|
@@ -1106,7 +1107,32 @@ - (void) webView:(WKWebView *)webView | |
| NSURLRequest *request = navigationAction.request; | ||
| BOOL isTopFrame = [request.URL isEqual:request.mainDocumentURL]; | ||
|
|
||
| void (^allowNavigation)(void) = ^{ | ||
| if (self->_onLoadingStart) { | ||
| // We have this check to filter out iframe requests and whatnot | ||
| if (isTopFrame) { | ||
| NSMutableDictionary<NSString *, id> *event = [self baseEvent]; | ||
| [event addEntriesFromDictionary: @{ | ||
| @"url": (request.URL).absoluteString, | ||
| @"navigationType": navigationTypes[@(navigationType)] | ||
| }]; | ||
| self->_onLoadingStart(event); | ||
| } | ||
| } | ||
| decisionHandler(WKNavigationActionPolicyAllow); | ||
| }; | ||
|
|
||
| if (_onShouldStartLoadWithRequest) { | ||
| int lockIdentifier = [[RNCWebViewDecisionManager getInstance] setDecisionHandler:^(BOOL shouldStart) { | ||
| dispatch_async(dispatch_get_main_queue(), ^{ | ||
| if (!shouldStart) { | ||
| decisionHandler(WKNavigationActionPolicyCancel); | ||
| return; | ||
| } | ||
| allowNavigation(); | ||
| }); | ||
| }]; | ||
|
Comment on lines
+1126
to
+1134
|
||
|
|
||
|
Comment on lines
+1126
to
+1135
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The allowing code is moved into the |
||
| NSMutableDictionary<NSString *, id> *event = [self baseEvent]; | ||
| if (request.mainDocumentURL) { | ||
| [event addEntriesFromDictionary: @{ | ||
|
|
@@ -1116,30 +1142,15 @@ - (void) webView:(WKWebView *)webView | |
| [event addEntriesFromDictionary: @{ | ||
| @"url": (request.URL).absoluteString, | ||
| @"navigationType": navigationTypes[@(navigationType)], | ||
| @"isTopFrame": @(isTopFrame) | ||
| @"isTopFrame": @(isTopFrame), | ||
| @"lockIdentifier": @(lockIdentifier) | ||
| }]; | ||
| if (![self.delegate webView:self | ||
| shouldStartLoadForRequest:event | ||
| withCallback:_onShouldStartLoadWithRequest]) { | ||
| decisionHandler(WKNavigationActionPolicyCancel); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| if (_onLoadingStart) { | ||
| // We have this check to filter out iframe requests and whatnot | ||
| if (isTopFrame) { | ||
| NSMutableDictionary<NSString *, id> *event = [self baseEvent]; | ||
| [event addEntriesFromDictionary: @{ | ||
| @"url": (request.URL).absoluteString, | ||
| @"navigationType": navigationTypes[@(navigationType)] | ||
| }]; | ||
| _onLoadingStart(event); | ||
| } | ||
| _onShouldStartLoadWithRequest(event); | ||
| return; | ||
| } | ||
|
|
||
| // Allow all navigation by default | ||
| decisionHandler(WKNavigationActionPolicyAllow); | ||
| allowNavigation(); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,18 @@ | ||||||||||||||||||||||||
| #import <Foundation/Foundation.h> | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| typedef void (^DecisionBlock)(BOOL); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| @interface RNCWebViewDecisionManager : NSObject { | ||||||||||||||||||||||||
| int nextLockIdentifier; | ||||||||||||||||||||||||
| NSMutableDictionary *decisionHandlers; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| @property (nonatomic) int nextLockIdentifier; | ||||||||||||||||||||||||
| @property (nonatomic, retain) NSMutableDictionary *decisionHandlers; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
Comment on lines
+5
to
+12
|
||||||||||||||||||||||||
| @interface RNCWebViewDecisionManager : NSObject { | |
| int nextLockIdentifier; | |
| NSMutableDictionary *decisionHandlers; | |
| } | |
| @property (nonatomic) int nextLockIdentifier; | |
| @property (nonatomic, retain) NSMutableDictionary *decisionHandlers; | |
| @interface RNCWebViewDecisionManager : NSObject | |
| @property (nonatomic) int nextLockIdentifier; | |
| @property (nonatomic, retain) NSMutableDictionary *decisionHandlers; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| #import "RNCWebViewDecisionManager.h" | ||
| #import <React/RCTLog.h> | ||
|
|
||
| /** | ||
| * Thread-safe singleton that manages navigation decision handlers for WKWebView. | ||
| * | ||
| * This class bridges async navigation decisions between: | ||
| * - WKWebView delegate (main thread) - stores decision handlers | ||
| * - React Native bridge (background thread) - resolves decisions from JS | ||
| * | ||
| * All public methods use @synchronized for thread safety since they access | ||
| * shared state (nextLockIdentifier and decisionHandlers) from different threads. | ||
| */ | ||
| @implementation RNCWebViewDecisionManager | ||
|
|
||
| @synthesize nextLockIdentifier; | ||
| @synthesize decisionHandlers; | ||
|
|
||
| + (id)getInstance { | ||
| static RNCWebViewDecisionManager *lockManager = nil; | ||
| static dispatch_once_t onceToken; | ||
| dispatch_once(&onceToken, ^{ | ||
| lockManager = [[self alloc] init]; | ||
| }); | ||
| return lockManager; | ||
| } | ||
|
|
||
| /** | ||
| * Stores a decision handler and returns a unique identifier. | ||
| * Called from the main thread (WKNavigationDelegate). | ||
| * @synchronized ensures atomic increment + insertion. | ||
| */ | ||
| - (int)setDecisionHandler:(DecisionBlock)decisionHandler { | ||
| @synchronized (self) { | ||
| int lockIdentifier = self.nextLockIdentifier++; | ||
|
||
| [self.decisionHandlers setObject:decisionHandler forKey:@(lockIdentifier)]; | ||
| return lockIdentifier; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Resolves a pending navigation decision. | ||
| * Called from the RN bridge thread (background) when JS responds. | ||
| * | ||
| * The handler is invoked OUTSIDE the @synchronized block to prevent deadlocks, | ||
| * since the handler dispatches to the main queue and could potentially | ||
| * trigger another navigation that re-enters this class. | ||
| */ | ||
| - (void) setResult:(BOOL)shouldStart | ||
| forLockIdentifier:(int)lockIdentifier { | ||
| DecisionBlock handler; | ||
| @synchronized (self) { | ||
| handler = [self.decisionHandlers objectForKey:@(lockIdentifier)]; | ||
| if (handler == nil) { | ||
| RCTLogWarn(@"Lock not found"); | ||
ChALkeR marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return; | ||
| } | ||
| [self.decisionHandlers removeObjectForKey:@(lockIdentifier)]; | ||
| } | ||
| handler(shouldStart); | ||
|
Comment on lines
+49
to
+60
|
||
| } | ||
|
|
||
| - (id)init { | ||
| if (self = [super init]) { | ||
| self.nextLockIdentifier = 1; | ||
| self.decisionHandlers = [[NSMutableDictionary alloc] init]; | ||
| } | ||
| return self; | ||
| } | ||
|
|
||
| - (void)dealloc {} | ||
|
|
||
ChALkeR marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @end | ||
Uh oh!
There was an error while loading. Please reload this page.