fix: cache webview top frame url in onPageStarted#38
Conversation
📝 WalkthroughWalkthroughThe changes add URL state tracking to RNCWebView by introducing a new mLastCommittedUrl field with getter and setter methods, and update RNCWebViewClient to capture and store the URL when page loading begins. Changes
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@android/src/main/java/com/reactnativecommunity/webview/RNCWebView.java`:
- Line 82: mLastCommittedUrl is shared between WebView callbacks and
`@JavascriptInterface` bridge code and needs cross-thread visibility; make
mLastCommittedUrl volatile (and apply the same volatile protection to the other
shared mutable fields noted at lines 260-266 and 497-502) so updates on the UI
thread are immediately visible to background/bridge threads, and ensure any
compound read-modify-write sequences use a synchronized block or explicit lock
to preserve atomicity where used (search for mLastCommittedUrl and the other
listed fields to locate usages).
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 1bc5962b-0962-49b6-98a8-c4500e6a721c
📒 Files selected for processing (2)
android/src/main/java/com/reactnativecommunity/webview/RNCWebView.javaandroid/src/main/java/com/reactnativecommunity/webview/RNCWebViewClient.java
| protected boolean nestedScrollEnabled = false; | ||
| protected ProgressChangedFilter progressChangedFilter; | ||
| protected boolean mActive = true; | ||
| protected @Nullable String mLastCommittedUrl; |
There was a problem hiding this comment.
Protect mLastCommittedUrl with cross-thread visibility guarantees.
mLastCommittedUrl is shared mutable state across WebView callback flow and @JavascriptInterface bridge flow. As currently implemented, stale reads are possible; this can still produce incorrect topFrameUrl under timing races.
Proposed fix
- protected `@Nullable` String mLastCommittedUrl;
+ protected volatile `@Nullable` String mLastCommittedUrl;According to Android WebView documentation, are methods annotated with `@JavascriptInterface` invoked on the UI thread or on a background thread, and what synchronization/visibility guarantees are required when sharing fields with UI-thread callbacks?
Also applies to: 260-266, 497-502
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@android/src/main/java/com/reactnativecommunity/webview/RNCWebView.java` at
line 82, mLastCommittedUrl is shared between WebView callbacks and
`@JavascriptInterface` bridge code and needs cross-thread visibility; make
mLastCommittedUrl volatile (and apply the same volatile protection to the other
shared mutable fields noted at lines 260-266 and 497-502) so updates on the UI
thread are immediately visible to background/bridge threads, and ensure any
compound read-modify-write sequences use a synchronized block or explicit lock
to preserve atomicity where used (search for mLastCommittedUrl and the other
listed fields to locate usages).
This will narrow down the possibility of getting the wrong url when calling webview.getUrl.
Summary by CodeRabbit