forked from react-native-webview/react-native-webview
-
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
Open
kbulgakov-exo
wants to merge
7
commits into
master
Choose a base branch
from
kb/feat/support-async-should-load-calls
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+126
−66
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1e6070c
feat: implement WebViewDecisionManager
kbulgakov-exo adfe5ff
refactor: `WebView` manager should rely on `WebViewDecisionManager`
kbulgakov-exo 8a11f80
refactor: make reusable `allowNavigation` block
kbulgakov-exo 4b0427b
feat: support async navigation decision handling
kbulgakov-exo 20b4976
chore: remove unnecessary pragma mark
kbulgakov-exo 33f9051
refactor: use consistent int type for lockIdentifier
kbulgakov-exo b6cad2e
fix: thread safe decision manager
kewdex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
+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(); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
|
|
||
| + (id) getInstance; | ||
|
|
||
| - (int)setDecisionHandler:(DecisionBlock)handler; | ||
| - (void) setResult:(BOOL)shouldStart | ||
| forLockIdentifier:(int)lockIdentifier; | ||
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | ||
| return; | ||
| } | ||
| [self.decisionHandlers removeObjectForKey:@(lockIdentifier)]; | ||
| } | ||
| handler(shouldStart); | ||
| } | ||
|
|
||
| - (id)init { | ||
| if (self = [super init]) { | ||
| self.nextLockIdentifier = 1; | ||
| self.decisionHandlers = [[NSMutableDictionary alloc] init]; | ||
| } | ||
| return self; | ||
| } | ||
|
|
||
| - (void)dealloc {} | ||
|
|
||
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.