@exodus/react-native-webview 11.26.1-exodus.2 → 11.26.1-exodus.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,51 @@
1
+ package com.reactnativecommunity.webview;
2
+
3
+ import android.webkit.WebView;
4
+
5
+ import com.facebook.react.bridge.Promise;
6
+ import com.facebook.react.bridge.ReactApplicationContext;
7
+ import com.facebook.react.bridge.ReactContextBaseJavaModule;
8
+ import com.facebook.react.uimanager.ThemedReactContext;
9
+ import com.facebook.react.bridge.ReactMethod;
10
+ import com.facebook.react.bridge.UiThreadUtil;
11
+
12
+ public class RNCWebViewUtils extends ReactContextBaseJavaModule {
13
+ public static final String NAME = "RNCWebViewUtils";
14
+
15
+ ReactApplicationContext mCallerContext;
16
+
17
+ public RNCWebViewUtils(ReactApplicationContext reactContext) {
18
+ super(reactContext);
19
+ mCallerContext = reactContext;
20
+ }
21
+
22
+ @Override
23
+ public String getName() {
24
+ return NAME;
25
+ }
26
+
27
+ @ReactMethod
28
+ public void getWebViewDefaultUserAgent(final Promise promise) {
29
+ UiThreadUtil.runOnUiThread(new Runnable() {
30
+ @Override
31
+ public void run() {
32
+ try {
33
+ WebView webView = new WebView(mCallerContext);
34
+
35
+ // in case we ever use our own UA we need to unset it first the get the original
36
+ String currentUA = webView.getSettings().getUserAgentString();
37
+ webView.getSettings().setUserAgentString(null);
38
+ String webViewUA = webView.getSettings().getUserAgentString();
39
+
40
+ // Revert to overriden UA string
41
+ webView.getSettings().setUserAgentString(currentUA);
42
+
43
+ promise.resolve(webViewUA);
44
+ }
45
+ catch (Exception e) {
46
+ promise.reject(NAME, e.getMessage());
47
+ }
48
+ }
49
+ });
50
+ }
51
+ }
@@ -5,12 +5,12 @@ import com.facebook.react.uimanager.events.Event
5
5
  import com.facebook.react.uimanager.events.RCTEventEmitter
6
6
 
7
7
  /**
8
- * Event emitted when a http error is received from the server.
8
+ * Event emitted when the WebView opens a new Window (i.e: target=_blank)
9
9
  */
10
- class TopHttpErrorEvent(viewId: Int, private val mEventData: WritableMap) :
11
- Event<TopHttpErrorEvent>(viewId) {
10
+ class TopOpenWindowEvent(viewId: Int, private val mEventData: WritableMap) :
11
+ Event<TopOpenWindowEvent>(viewId) {
12
12
  companion object {
13
- const val EVENT_NAME = "topHttpError"
13
+ const val EVENT_NAME = "topOpenWindow"
14
14
  }
15
15
 
16
16
  override fun getEventName(): String = EVENT_NAME
@@ -22,4 +22,4 @@ class TopHttpErrorEvent(viewId: Int, private val mEventData: WritableMap) :
22
22
  override fun dispatch(rctEventEmitter: RCTEventEmitter) =
23
23
  rctEventEmitter.receiveEvent(viewTag, eventName, mEventData)
24
24
 
25
- }
25
+ }
@@ -42,15 +42,13 @@ shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *_Nonnull)request
42
42
  @property (nonatomic, assign) BOOL messagingEnabled;
43
43
  @property (nonatomic, copy) NSString * _Nullable injectedJavaScript;
44
44
  @property (nonatomic, copy) NSString * _Nullable injectedJavaScriptBeforeContentLoaded;
45
- @property (nonatomic, assign) BOOL injectedJavaScriptForMainFrameOnly;
46
- @property (nonatomic, assign) BOOL injectedJavaScriptBeforeContentLoadedForMainFrameOnly;
47
45
  @property (nonatomic, assign) BOOL scrollEnabled;
48
46
  @property (nonatomic, assign) BOOL sharedCookiesEnabled;
49
47
  @property (nonatomic, assign) BOOL autoManageStatusBarEnabled;
50
48
  @property (nonatomic, assign) BOOL pagingEnabled;
51
49
  @property (nonatomic, assign) CGFloat decelerationRate;
52
50
  @property (nonatomic, assign) BOOL allowsInlineMediaPlayback;
53
- @property (nonatomic, assign) BOOL allowsAirPlayForMediaPlayback;
51
+ @property (nonatomic, assign) BOOL webviewDebuggingEnabled;
54
52
  @property (nonatomic, assign) BOOL bounces;
55
53
  @property (nonatomic, assign) BOOL mediaPlaybackRequiresUserAction;
56
54
  #if WEBKIT_IOS_10_APIS_AVAILABLE
@@ -64,17 +62,12 @@ shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *_Nonnull)request
64
62
  @property (nonatomic, assign) BOOL incognito;
65
63
  @property (nonatomic, assign) BOOL useSharedProcessPool;
66
64
  @property (nonatomic, copy) NSString * _Nullable userAgent;
67
- @property (nonatomic, copy) NSString * _Nullable applicationNameForUserAgent;
68
65
  @property (nonatomic, assign) BOOL cacheEnabled;
69
66
  @property (nonatomic, assign) BOOL javaScriptEnabled;
70
- @property (nonatomic, assign) BOOL allowFileAccessFromFileURLs;
71
- @property (nonatomic, assign) BOOL allowUniversalAccessFromFileURLs;
72
67
  @property (nonatomic, assign) BOOL allowsLinkPreview;
73
68
  @property (nonatomic, assign) BOOL showsHorizontalScrollIndicator;
74
69
  @property (nonatomic, assign) BOOL showsVerticalScrollIndicator;
75
70
  @property (nonatomic, assign) BOOL directionalLockEnabled;
76
- @property (nonatomic, assign) BOOL ignoreSilentHardwareSwitch;
77
- @property (nonatomic, copy) NSString * _Nullable allowingReadAccessToURL;
78
71
  @property (nonatomic, copy) NSDictionary * _Nullable basicAuthCredential;
79
72
  @property (nonatomic, assign) BOOL pullToRefreshEnabled;
80
73
  @property (nonatomic, assign) BOOL enableApplePay;
@@ -54,13 +54,11 @@ UIScrollViewDelegate,
54
54
  #endif // !TARGET_OS_OSX
55
55
  RCTAutoInsetsProtocol>
56
56
 
57
- @property (nonatomic, copy) RCTDirectEventBlock onFileDownload;
58
57
  @property (nonatomic, copy) RCTDirectEventBlock onLoadingStart;
59
58
  @property (nonatomic, copy) RCTDirectEventBlock onLoadingFinish;
60
59
  @property (nonatomic, copy) RCTDirectEventBlock onLoadingError;
61
60
  @property (nonatomic, copy) RCTDirectEventBlock onLoadingProgress;
62
61
  @property (nonatomic, copy) RCTDirectEventBlock onShouldStartLoadWithRequest;
63
- @property (nonatomic, copy) RCTDirectEventBlock onHttpError;
64
62
  @property (nonatomic, copy) RCTDirectEventBlock onMessage;
65
63
  @property (nonatomic, copy) RCTDirectEventBlock onScroll;
66
64
  @property (nonatomic, copy) RCTDirectEventBlock onContentProcessDidTerminate;
@@ -122,9 +120,7 @@ RCTAutoInsetsProtocol>
122
120
  _savedStatusBarHidden = RCTSharedApplication().statusBarHidden;
123
121
  #endif // !TARGET_OS_OSX
124
122
  _injectedJavaScript = nil;
125
- _injectedJavaScriptForMainFrameOnly = YES;
126
123
  _injectedJavaScriptBeforeContentLoaded = nil;
127
- _injectedJavaScriptBeforeContentLoadedForMainFrameOnly = YES;
128
124
 
129
125
  #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
130
126
  _savedContentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
@@ -139,15 +135,6 @@ RCTAutoInsetsProtocol>
139
135
  }
140
136
 
141
137
  #if !TARGET_OS_OSX
142
- [[NSNotificationCenter defaultCenter]addObserver:self
143
- selector:@selector(appDidBecomeActive)
144
- name:UIApplicationDidBecomeActiveNotification
145
- object:nil];
146
-
147
- [[NSNotificationCenter defaultCenter]addObserver:self
148
- selector:@selector(appWillResignActive)
149
- name:UIApplicationWillResignActiveNotification
150
- object:nil];
151
138
  if (@available(iOS 12.0, *)) {
152
139
  // Workaround for a keyboard dismissal bug present in iOS 12
153
140
  // https://openradar.appspot.com/radar?id=5018321736957952
@@ -306,30 +293,23 @@ RCTAutoInsetsProtocol>
306
293
  {
307
294
  WKWebViewConfiguration *wkWebViewConfig = [WKWebViewConfiguration new];
308
295
  WKPreferences *prefs = [[WKPreferences alloc]init];
309
- BOOL _prefsUsed = YES;
310
296
  if (!_javaScriptEnabled) {
311
297
  prefs.javaScriptEnabled = NO;
312
- _prefsUsed = YES;
313
- }
314
- if (_allowUniversalAccessFromFileURLs) {
315
- [wkWebViewConfig setValue:@TRUE forKey:@"allowUniversalAccessFromFileURLs"];
316
- }
317
- if (_allowFileAccessFromFileURLs) {
318
- [prefs setValue:@TRUE forKey:@"allowFileAccessFromFileURLs"];
319
- _prefsUsed = YES;
320
298
  }
299
+
300
+ // NOTE: defaults, recheck?
301
+ // [wkWebViewConfig setValue:@FALSE forKey:@"allowUniversalAccessFromFileURLs"];
302
+ // [prefs setValue:@FALSE forKey:@"allowFileAccessFromFileURLs"];
303
+
321
304
  [prefs setValue:@FALSE forKey:@"javaScriptCanOpenWindowsAutomatically"];
322
305
  #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 140500 /* iOS 14.5 */
323
306
  if (@available(iOS 14.5, *)) {
324
307
  if (!_textInteractionEnabled) {
325
308
  [prefs setValue:@FALSE forKey:@"textInteractionEnabled"];
326
- _prefsUsed = YES;
327
309
  }
328
310
  }
329
311
  #endif
330
- if (_prefsUsed) {
331
- wkWebViewConfig.preferences = prefs;
332
- }
312
+ wkWebViewConfig.preferences = prefs;
333
313
  if (_incognito) {
334
314
  wkWebViewConfig.websiteDataStore = [WKWebsiteDataStore nonPersistentDataStore];
335
315
  } else if (_cacheEnabled) {
@@ -364,7 +344,7 @@ RCTAutoInsetsProtocol>
364
344
  [self resetupScripts:wkWebViewConfig];
365
345
 
366
346
  if(@available(ios 9.0, *)) {
367
- wkWebViewConfig.allowsAirPlayForMediaPlayback = _allowsAirPlayForMediaPlayback;
347
+ wkWebViewConfig.allowsAirPlayForMediaPlayback = NO;
368
348
  }
369
349
 
370
350
  #if !TARGET_OS_OSX
@@ -379,10 +359,6 @@ RCTAutoInsetsProtocol>
379
359
  #endif
380
360
  #endif // !TARGET_OS_OSX
381
361
 
382
- if (_applicationNameForUserAgent) {
383
- wkWebViewConfig.applicationNameForUserAgent = [NSString stringWithFormat:@"%@ %@", wkWebViewConfig.applicationNameForUserAgent, _applicationNameForUserAgent];
384
- }
385
-
386
362
  return wkWebViewConfig;
387
363
  }
388
364
 
@@ -430,6 +406,13 @@ RCTAutoInsetsProtocol>
430
406
  }
431
407
  #endif
432
408
 
409
+ #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 130300 || \
410
+ __IPHONE_OS_VERSION_MAX_ALLOWED >= 160400 || \
411
+ __TV_OS_VERSION_MAX_ALLOWED >= 160400
412
+ if (@available(macOS 13.3, iOS 16.4, tvOS 16.4, *))
413
+ _webView.inspectable = _webviewDebuggingEnabled;
414
+ #endif
415
+
433
416
  [self addSubview:_webView];
434
417
  [self setHideKeyboardAccessoryView: _savedHideKeyboardAccessoryView];
435
418
  [self setKeyboardDisplayRequiresUserAction: _savedKeyboardDisplayRequiresUserAction];
@@ -455,6 +438,16 @@ RCTAutoInsetsProtocol>
455
438
  _webView.allowsBackForwardNavigationGestures = _allowsBackForwardNavigationGestures;
456
439
  }
457
440
 
441
+ #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 130300 || \
442
+ __IPHONE_OS_VERSION_MAX_ALLOWED >= 160400 || \
443
+ __TV_OS_VERSION_MAX_ALLOWED >= 160400
444
+ - (void)setWebviewDebuggingEnabled:(BOOL)webviewDebuggingEnabled {
445
+ _webviewDebuggingEnabled = webviewDebuggingEnabled;
446
+ if (@available(macOS 13.3, iOS 16.4, tvOS 16.4, *))
447
+ _webView.inspectable = _webviewDebuggingEnabled;
448
+ }
449
+ #endif
450
+
458
451
  - (void)removeFromSuperview
459
452
  {
460
453
  if (_webView) {
@@ -466,10 +459,6 @@ RCTAutoInsetsProtocol>
466
459
  _webView.scrollView.delegate = nil;
467
460
  #endif // !TARGET_OS_OSX
468
461
  _webView = nil;
469
- if (_onContentProcessDidTerminate) {
470
- NSMutableDictionary<NSString *, id> *event = [self baseEvent];
471
- _onContentProcessDidTerminate(event);
472
- }
473
462
  }
474
463
 
475
464
  [super removeFromSuperview];
@@ -624,17 +613,6 @@ RCTAutoInsetsProtocol>
624
613
  }
625
614
  }
626
615
 
627
- - (void)setAllowingReadAccessToURL:(NSString *)allowingReadAccessToURL
628
- {
629
- if (![_allowingReadAccessToURL isEqualToString:allowingReadAccessToURL]) {
630
- _allowingReadAccessToURL = [allowingReadAccessToURL copy];
631
-
632
- if (_webView != nil) {
633
- [self visitSource];
634
- }
635
- }
636
- }
637
-
638
616
  #if !TARGET_OS_OSX
639
617
  - (void)setContentInset:(UIEdgeInsets)contentInset
640
618
  {
@@ -682,8 +660,10 @@ RCTAutoInsetsProtocol>
682
660
  [_webView loadRequest:request];
683
661
  }
684
662
  else {
685
- NSURL* readAccessUrl = _allowingReadAccessToURL ? [RCTConvert NSURL:_allowingReadAccessToURL] : request.URL;
686
- [_webView loadFileURL:request.URL allowingReadAccessToURL:readAccessUrl];
663
+ // WARNING: UNREACHABLE, non-host loads (file urls)
664
+ // Clear the webview
665
+ [_webView loadHTMLString:@"" baseURL:nil];
666
+ return;
687
667
  }
688
668
  }];
689
669
  }
@@ -872,7 +852,7 @@ RCTAutoInsetsProtocol>
872
852
  {
873
853
  NSDictionary *eventInitDict = @{@"data": message};
874
854
  NSString *source = [NSString
875
- stringWithFormat:@"window.dispatchEvent(new MessageEvent('message', %@));",
855
+ stringWithFormat:@"document.dispatchEvent(new MessageEvent('message', %@));",
876
856
  RCTJSONStringify(eventInitDict, NULL)
877
857
  ];
878
858
  [self injectJavaScript: source];
@@ -1147,10 +1127,6 @@ RCTAutoInsetsProtocol>
1147
1127
  - (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView
1148
1128
  {
1149
1129
  RCTLogWarn(@"Webview Process Terminated");
1150
- if (_onContentProcessDidTerminate) {
1151
- NSMutableDictionary<NSString *, id> *event = [self baseEvent];
1152
- _onContentProcessDidTerminate(event);
1153
- }
1154
1130
  }
1155
1131
 
1156
1132
  /**
@@ -1162,36 +1138,19 @@ RCTAutoInsetsProtocol>
1162
1138
  decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
1163
1139
  {
1164
1140
  WKNavigationResponsePolicy policy = WKNavigationResponsePolicyAllow;
1165
- if (_onHttpError && navigationResponse.forMainFrame) {
1141
+ if (navigationResponse.forMainFrame) {
1166
1142
  if ([navigationResponse.response isKindOfClass:[NSHTTPURLResponse class]]) {
1167
1143
  NSHTTPURLResponse *response = (NSHTTPURLResponse *)navigationResponse.response;
1168
1144
  NSInteger statusCode = response.statusCode;
1169
1145
 
1170
- if (statusCode >= 400) {
1171
- NSMutableDictionary<NSString *, id> *httpErrorEvent = [self baseEvent];
1172
- [httpErrorEvent addEntriesFromDictionary: @{
1173
- @"url": response.URL.absoluteString,
1174
- @"statusCode": @(statusCode)
1175
- }];
1176
-
1177
- _onHttpError(httpErrorEvent);
1178
- }
1179
-
1180
1146
  NSString *disposition = nil;
1181
1147
  if (@available(iOS 13, *)) {
1182
1148
  disposition = [response valueForHTTPHeaderField:@"Content-Disposition"];
1183
1149
  }
1184
1150
  BOOL isAttachment = disposition != nil && [disposition hasPrefix:@"attachment"];
1185
1151
  if (isAttachment || !navigationResponse.canShowMIMEType) {
1186
- if (_onFileDownload) {
1187
- policy = WKNavigationResponsePolicyCancel;
1188
-
1189
- NSMutableDictionary<NSString *, id> *downloadEvent = [self baseEvent];
1190
- [downloadEvent addEntriesFromDictionary: @{
1191
- @"downloadUrl": (response.URL).absoluteString,
1192
- }];
1193
- _onFileDownload(downloadEvent);
1194
- }
1152
+ policy = WKNavigationResponsePolicyCancel;
1153
+ // File downloads are cancelled
1195
1154
  }
1196
1155
  }
1197
1156
  }
@@ -1252,37 +1211,6 @@ RCTAutoInsetsProtocol>
1252
1211
  }];
1253
1212
  }
1254
1213
 
1255
- -(void)forceIgnoreSilentHardwareSwitch:(BOOL)initialSetup
1256
- {
1257
- NSString *mp3Str = @"data:audio/mp3;base64,//tAxAAAAAAAAAAAAAAAAAAAAAAASW5mbwAAAA8AAAAFAAAESAAzMzMzMzMzMzMzMzMzMzMzMzMzZmZmZmZmZmZmZmZmZmZmZmZmZmaZmZmZmZmZmZmZmZmZmZmZmZmZmczMzMzMzMzMzMzMzMzMzMzMzMzM//////////////////////////8AAAA5TEFNRTMuMTAwAZYAAAAAAAAAABQ4JAMGQgAAOAAABEhNIZS0AAAAAAD/+0DEAAPH3Yz0AAR8CPqyIEABp6AxjG/4x/XiInE4lfQDFwIIRE+uBgZoW4RL0OLMDFn6E5v+/u5ehf76bu7/6bu5+gAiIQGAABQIUJ0QolFghEn/9PhZQpcUTpXMjo0OGzRCZXyKxoIQzB2KhCtGobpT9TRVj/3Pmfp+f8X7Pu1B04sTnc3s0XhOlXoGVCMNo9X//9/r6a10TZEY5DsxqvO7mO5qFvpFCmKIjhpSItGsUYcRO//7QsQRgEiljQIAgLFJAbIhNBCa+JmorCbOi5q9nVd2dKnusTMQg4MFUlD6DQ4OFijwGAijRMfLbHG4nLVTjydyPlJTj8pfPflf9/5GD950A5e+jsrmNZSjSirjs1R7hnkia8vr//l/7Nb+crvr9Ok5ZJOylUKRxf/P9Zn0j2P4pJYXyKkeuy5wUYtdmOu6uobEtFqhIJViLEKIjGxchGev/L3Y0O3bwrIOszTBAZ7Ih28EUaSOZf/7QsQfg8fpjQIADN0JHbGgQBAZ8T//y//t/7d/2+f5m7MdCeo/9tdkMtGLbt1tqnabRroO1Qfvh20yEbei8nfDXP7btW7f9/uO9tbe5IvHQbLlxpf3DkAk0ojYcv///5/u3/7PTfGjPEPUvt5D6f+/3Lea4lz4tc4TnM/mFPrmalWbboeNiNyeyr+vufttZuvrVrt/WYv3T74JFo8qEDiJqJrmDTs///v99xDku2xG02jjunrICP/7QsQtA8kpkQAAgNMA/7FgQAGnobgfghgqA+uXwWQ3XFmGimSbe2X3ksY//KzK1a2k6cnNWOPJnPWUsYbKqkh8RJzrVf///P///////4vyhLKHLrCb5nIrYIUss4cthigL1lQ1wwNAc6C1pf1TIKRSkt+a//z+yLVcwlXKSqeSuCVQFLng2h4AFAFgTkH+Z/8jTX/zr//zsJV/5f//5UX/0ZNCNCCaf5lTCTRkaEdhNP//n/KUjf/7QsQ5AEhdiwAAjN7I6jGddBCO+WGTQ1mXrYatSAgaykxBTUUzLjEwMKqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqg==";
1258
- NSString *scr;
1259
- if (initialSetup) {
1260
- scr = [NSString stringWithFormat:@"var s=new Audio('%@');s.id='wkwebviewAudio';s.controls=false;s.loop=true;s.play();document.body.appendChild(s);true", mp3Str];
1261
- } else {
1262
- scr = [NSString stringWithFormat:@"var s=document.getElementById('wkwebviewAudio');s.src=null;s.parentNode.removeChild(s);s=null;s=new Audio('%@');s.id='wkwebviewAudio';s.controls=false;s.loop=true;s.play();document.body.appendChild(s);true", mp3Str];
1263
- }
1264
- [self evaluateJS: scr thenCall: nil];
1265
- }
1266
-
1267
- -(void)disableIgnoreSilentSwitch
1268
- {
1269
- [self evaluateJS: @"document.getElementById('wkwebviewAudio').src=null;true" thenCall: nil];
1270
- }
1271
-
1272
- -(void)appDidBecomeActive
1273
- {
1274
- if (_ignoreSilentHardwareSwitch) {
1275
- [self forceIgnoreSilentHardwareSwitch:false];
1276
- }
1277
- }
1278
-
1279
- -(void)appWillResignActive
1280
- {
1281
- if (_ignoreSilentHardwareSwitch) {
1282
- [self disableIgnoreSilentSwitch];
1283
- }
1284
- }
1285
-
1286
1214
  /**
1287
1215
  * Called when the navigation is complete.
1288
1216
  * @see https://fburl.com/rtys6jlb
@@ -1290,10 +1218,6 @@ RCTAutoInsetsProtocol>
1290
1218
  - (void)webView:(WKWebView *)webView
1291
1219
  didFinishNavigation:(WKNavigation *)navigation
1292
1220
  {
1293
- if (_ignoreSilentHardwareSwitch) {
1294
- [self forceIgnoreSilentHardwareSwitch:true];
1295
- }
1296
-
1297
1221
  if (_onLoadingFinish) {
1298
1222
  _onLoadingFinish([self baseEvent]);
1299
1223
  }
@@ -1389,7 +1313,7 @@ didFinishNavigation:(WKNavigation *)navigation
1389
1313
 
1390
1314
  self.atEndScript = source == nil ? nil : [[WKUserScript alloc] initWithSource:source
1391
1315
  injectionTime:WKUserScriptInjectionTimeAtDocumentEnd
1392
- forMainFrameOnly:_injectedJavaScriptForMainFrameOnly];
1316
+ forMainFrameOnly:YES];
1393
1317
 
1394
1318
  if(_webView != nil){
1395
1319
  [self resetupScripts:_webView.configuration];
@@ -1401,23 +1325,13 @@ didFinishNavigation:(WKNavigation *)navigation
1401
1325
 
1402
1326
  self.atStartScript = source == nil ? nil : [[WKUserScript alloc] initWithSource:source
1403
1327
  injectionTime:WKUserScriptInjectionTimeAtDocumentStart
1404
- forMainFrameOnly:_injectedJavaScriptBeforeContentLoadedForMainFrameOnly];
1328
+ forMainFrameOnly:YES];
1405
1329
 
1406
1330
  if(_webView != nil){
1407
1331
  [self resetupScripts:_webView.configuration];
1408
1332
  }
1409
1333
  }
1410
1334
 
1411
- - (void)setInjectedJavaScriptForMainFrameOnly:(BOOL)mainFrameOnly {
1412
- _injectedJavaScriptForMainFrameOnly = mainFrameOnly;
1413
- [self setInjectedJavaScript:_injectedJavaScript];
1414
- }
1415
-
1416
- - (void)setInjectedJavaScriptBeforeContentLoadedForMainFrameOnly:(BOOL)mainFrameOnly {
1417
- _injectedJavaScriptBeforeContentLoadedForMainFrameOnly = mainFrameOnly;
1418
- [self setInjectedJavaScriptBeforeContentLoaded:_injectedJavaScriptBeforeContentLoaded];
1419
- }
1420
-
1421
1335
  - (void)setMessagingEnabled:(BOOL)messagingEnabled {
1422
1336
  _messagingEnabled = messagingEnabled;
1423
1337
 
@@ -1434,9 +1348,6 @@ didFinishNavigation:(WKNavigation *)navigation
1434
1348
  "};", MessageHandlerName, MessageHandlerName
1435
1349
  ]
1436
1350
  injectionTime:WKUserScriptInjectionTimeAtDocumentStart
1437
- /* TODO: For a separate (minor) PR: use logic like this (as react-native-wkwebview does) so that messaging can be used in all frames if desired.
1438
- * I am keeping it as YES for consistency with previous behaviour. */
1439
- // forMainFrameOnly:_messagingEnabledForMainFrameOnly
1440
1351
  forMainFrameOnly:YES
1441
1352
  ] :
1442
1353
  nil;
@@ -54,23 +54,16 @@ RCT_EXPORT_MODULE()
54
54
  }
55
55
 
56
56
  RCT_EXPORT_VIEW_PROPERTY(source, NSDictionary)
57
- RCT_EXPORT_VIEW_PROPERTY(onFileDownload, RCTDirectEventBlock)
58
57
  RCT_EXPORT_VIEW_PROPERTY(onLoadingStart, RCTDirectEventBlock)
59
58
  RCT_EXPORT_VIEW_PROPERTY(onLoadingFinish, RCTDirectEventBlock)
60
59
  RCT_EXPORT_VIEW_PROPERTY(onLoadingError, RCTDirectEventBlock)
61
60
  RCT_EXPORT_VIEW_PROPERTY(onLoadingProgress, RCTDirectEventBlock)
62
- RCT_EXPORT_VIEW_PROPERTY(onHttpError, RCTDirectEventBlock)
63
61
  RCT_EXPORT_VIEW_PROPERTY(onShouldStartLoadWithRequest, RCTDirectEventBlock)
64
- RCT_EXPORT_VIEW_PROPERTY(onContentProcessDidTerminate, RCTDirectEventBlock)
65
62
  RCT_EXPORT_VIEW_PROPERTY(injectedJavaScript, NSString)
66
63
  RCT_EXPORT_VIEW_PROPERTY(injectedJavaScriptBeforeContentLoaded, NSString)
67
- RCT_EXPORT_VIEW_PROPERTY(injectedJavaScriptForMainFrameOnly, BOOL)
68
- RCT_EXPORT_VIEW_PROPERTY(injectedJavaScriptBeforeContentLoadedForMainFrameOnly, BOOL)
69
64
  RCT_EXPORT_VIEW_PROPERTY(javaScriptEnabled, BOOL)
70
- RCT_EXPORT_VIEW_PROPERTY(allowFileAccessFromFileURLs, BOOL)
71
- RCT_EXPORT_VIEW_PROPERTY(allowUniversalAccessFromFileURLs, BOOL)
72
65
  RCT_EXPORT_VIEW_PROPERTY(allowsInlineMediaPlayback, BOOL)
73
- RCT_EXPORT_VIEW_PROPERTY(allowsAirPlayForMediaPlayback, BOOL)
66
+ RCT_EXPORT_VIEW_PROPERTY(webviewDebuggingEnabled, BOOL)
74
67
  RCT_EXPORT_VIEW_PROPERTY(mediaPlaybackRequiresUserAction, BOOL)
75
68
  #if WEBKIT_IOS_10_APIS_AVAILABLE
76
69
  RCT_EXPORT_VIEW_PROPERTY(dataDetectorTypes, WKDataDetectorTypes)
@@ -82,10 +75,8 @@ RCT_EXPORT_VIEW_PROPERTY(hideKeyboardAccessoryView, BOOL)
82
75
  RCT_EXPORT_VIEW_PROPERTY(allowsBackForwardNavigationGestures, BOOL)
83
76
  RCT_EXPORT_VIEW_PROPERTY(incognito, BOOL)
84
77
  RCT_EXPORT_VIEW_PROPERTY(pagingEnabled, BOOL)
85
- RCT_EXPORT_VIEW_PROPERTY(applicationNameForUserAgent, NSString)
86
78
  RCT_EXPORT_VIEW_PROPERTY(cacheEnabled, BOOL)
87
79
  RCT_EXPORT_VIEW_PROPERTY(allowsLinkPreview, BOOL)
88
- RCT_EXPORT_VIEW_PROPERTY(allowingReadAccessToURL, NSString)
89
80
  RCT_EXPORT_VIEW_PROPERTY(basicAuthCredential, NSDictionary)
90
81
 
91
82
  #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
package/index.js CHANGED
@@ -1,4 +1,7 @@
1
- import WebView from './lib/WebView';
1
+ import WebView, {
2
+ RNCWebViewUtils,
3
+ getWebViewDefaultUserAgent,
4
+ } from './lib/WebView';
2
5
 
3
- export { WebView };
6
+ export { WebView, RNCWebViewUtils, getWebViewDefaultUserAgent };
4
7
  export default WebView;
@@ -1,17 +1,24 @@
1
- import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef } from 'react';
2
- import { Image, View, NativeModules, } from 'react-native';
1
+ import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react';
2
+ import { Text, View, NativeModules, } from 'react-native';
3
3
  import BatchedBridge from 'react-native/Libraries/BatchedBridge/BatchedBridge';
4
4
  // @ts-expect-error react-native doesn't have this type
5
5
  import codegenNativeCommandsUntyped from 'react-native/Libraries/Utilities/codegenNativeCommands';
6
6
  import invariant from 'invariant';
7
7
  import RNCWebView from "./WebViewNativeComponent.android";
8
- import { defaultOriginWhitelist, defaultRenderError, defaultRenderLoading, useWebWiewLogic, } from './WebViewShared';
8
+ import { defaultOriginWhitelist, defaultRenderError, defaultRenderLoading, useWebWiewLogic, versionPasses, } from './WebViewShared';
9
9
  import styles from './WebView.styles';
10
+ const { getWebViewDefaultUserAgent } = NativeModules.RNCWebViewUtils;
11
+ let userAgentPromise;
12
+ async function getUserAgent() {
13
+ if (!userAgentPromise)
14
+ userAgentPromise = getWebViewDefaultUserAgent();
15
+ const userAgent = await userAgentPromise;
16
+ return userAgent || 'unknown';
17
+ }
10
18
  const codegenNativeCommands = codegenNativeCommandsUntyped;
11
19
  const Commands = codegenNativeCommands({
12
20
  supportedCommands: ['goBack', 'goForward', 'reload', 'stopLoading', /* 'injectJavaScript', */ 'requestFocus', 'postMessage', 'clearFormData', 'clearCache', 'clearHistory', 'loadUrl'],
13
21
  });
14
- const { resolveAssetSource } = Image;
15
22
  /**
16
23
  * A simple counter to uniquely identify WebView instances. Do not use this for anything else.
17
24
  */
@@ -19,17 +26,13 @@ let uniqueRef = 0;
19
26
  /**
20
27
  * Harcoded default for security.
21
28
  */
22
- const allowFileAccessFromFileURLs = false;
23
- const allowUniversalAccessFromFileURLs = false;
24
- const injectedJavaScriptForMainFrameOnly = true;
25
- const injectedJavaScriptBeforeContentLoadedForMainFrameOnly = true;
26
29
  const mediaPlaybackRequiresUserAction = true;
27
30
  // Android only
28
- const allowsFullscreenVideo = false;
29
- const allowFileAccess = false;
30
31
  const setSupportMultipleWindows = true;
31
32
  const mixedContentMode = 'never';
32
- const WebViewComponent = forwardRef(({ overScrollMode = 'always', javaScriptEnabled = true, thirdPartyCookiesEnabled = true, scalesPageToFit = true, saveFormDataDisabled = false, cacheEnabled = true, androidHardwareAccelerationDisabled = false, androidLayerType = "none", originWhitelist = defaultOriginWhitelist, setBuiltInZoomControls = true, setDisplayZoomControls = false, nestedScrollEnabled = false, startInLoadingState, onNavigationStateChange, onLoadStart, onError, onLoad, onLoadEnd, onLoadProgress, onHttpError: onHttpErrorProp, onRenderProcessGone: onRenderProcessGoneProp, onMessage: onMessageProp, renderLoading, renderError, style, containerStyle, source, onShouldStartLoadWithRequest: onShouldStartLoadWithRequestProp, ...otherProps }, ref) => {
33
+ const hardMinimumChromeVersion = '100.0'; // TODO: determinime a good lower bound
34
+ const WebViewComponent = forwardRef(({ overScrollMode = 'always', javaScriptEnabled = true, thirdPartyCookiesEnabled = true, scalesPageToFit = true, saveFormDataDisabled = false, cacheEnabled = true, androidHardwareAccelerationDisabled = false, androidLayerType = "none", originWhitelist = defaultOriginWhitelist, setBuiltInZoomControls = true, setDisplayZoomControls = false, nestedScrollEnabled = false, startInLoadingState, onLoadStart, onError, onLoad, onLoadEnd, onMessage: onMessageProp, onOpenWindow: onOpenWindowProp, renderLoading, renderError, style, containerStyle, source, onShouldStartLoadWithRequest: onShouldStartLoadWithRequestProp, validateMeta, validateData, minimumChromeVersion, unsupportedVersionComponent: UnsupportedVersionComponent, ...otherProps }, ref) => {
35
+ var _a;
33
36
  const messagingModuleName = useRef(`WebViewMessageHandler${uniqueRef += 1}`).current;
34
37
  const webViewRef = useRef(null);
35
38
  const onShouldStartLoadWithRequestCallback = useCallback((shouldStart, url, lockIdentifier) => {
@@ -40,35 +43,36 @@ const WebViewComponent = forwardRef(({ overScrollMode = 'always', javaScriptEnab
40
43
  Commands.loadUrl(webViewRef.current, url);
41
44
  }
42
45
  }, []);
43
- const { onLoadingStart, onShouldStartLoadWithRequest, onMessage, viewState, setViewState, lastErrorEvent, onHttpError, onLoadingError, onLoadingFinish, onLoadingProgress, onRenderProcessGone } = useWebWiewLogic({
44
- onNavigationStateChange,
46
+ const { onLoadingStart, onShouldStartLoadWithRequest, onMessage, viewState, setViewState, lastErrorEvent, onLoadingError, onLoadingFinish, onLoadingProgress, onOpenWindow, passesWhitelist } = useWebWiewLogic({
45
47
  onLoad,
46
48
  onError,
47
- onHttpErrorProp,
48
49
  onLoadEnd,
49
- onLoadProgress,
50
50
  onLoadStart,
51
- onRenderProcessGoneProp,
52
51
  onMessageProp,
52
+ onOpenWindowProp,
53
53
  startInLoadingState,
54
54
  originWhitelist,
55
55
  onShouldStartLoadWithRequestProp,
56
56
  onShouldStartLoadWithRequestCallback,
57
+ validateMeta,
58
+ validateData,
57
59
  });
58
60
  useImperativeHandle(ref, () => ({
59
- goForward: () => Commands.goForward(webViewRef.current),
60
- goBack: () => Commands.goBack(webViewRef.current),
61
+ goForward: () => webViewRef.current && Commands.goForward(webViewRef.current),
62
+ goBack: () => webViewRef.current && Commands.goBack(webViewRef.current),
61
63
  reload: () => {
62
64
  setViewState('LOADING');
63
- Commands.reload(webViewRef.current);
65
+ if (webViewRef.current) {
66
+ Commands.reload(webViewRef.current);
67
+ }
64
68
  },
65
- stopLoading: () => Commands.stopLoading(webViewRef.current),
66
- postMessage: (data) => Commands.postMessage(webViewRef.current, data),
69
+ stopLoading: () => webViewRef.current && Commands.stopLoading(webViewRef.current),
70
+ postMessage: (data) => webViewRef.current && Commands.postMessage(webViewRef.current, data),
67
71
  // injectJavaScript: (data: string) => Commands.injectJavaScript(webViewRef.current, data),
68
- requestFocus: () => Commands.requestFocus(webViewRef.current),
69
- clearFormData: () => Commands.clearFormData(webViewRef.current),
70
- clearCache: (includeDiskFiles) => Commands.clearCache(webViewRef.current, includeDiskFiles),
71
- clearHistory: () => Commands.clearHistory(webViewRef.current),
72
+ requestFocus: () => webViewRef.current && Commands.requestFocus(webViewRef.current),
73
+ clearFormData: () => webViewRef.current && Commands.clearFormData(webViewRef.current),
74
+ clearCache: (includeDiskFiles) => webViewRef.current && Commands.clearCache(webViewRef.current, includeDiskFiles),
75
+ clearHistory: () => webViewRef.current && Commands.clearHistory(webViewRef.current),
72
76
  }), [setViewState, webViewRef]);
73
77
  const directEventCallbacks = useMemo(() => ({
74
78
  onShouldStartLoadWithRequest,
@@ -77,6 +81,23 @@ const WebViewComponent = forwardRef(({ overScrollMode = 'always', javaScriptEnab
77
81
  useEffect(() => {
78
82
  BatchedBridge.registerCallableModule(messagingModuleName, directEventCallbacks);
79
83
  }, [messagingModuleName, directEventCallbacks]);
84
+ const [userAgent, setUserAgent] = useState();
85
+ useEffect(() => {
86
+ getUserAgent().then(setUserAgent);
87
+ }, []);
88
+ if (!userAgent)
89
+ return null; // stop the rendering until userAgent is known
90
+ const version = (_a = userAgent.match(/chrome\/((?:[0-9]+\.)+[0-9]+)/i)) === null || _a === void 0 ? void 0 : _a[1];
91
+ if (!(versionPasses(version, minimumChromeVersion) && versionPasses(version, hardMinimumChromeVersion))) {
92
+ if (UnsupportedVersionComponent) {
93
+ return <UnsupportedVersionComponent />;
94
+ }
95
+ return (<View style={{ alignSelf: 'flex-start' }}>
96
+ <Text style={{ color: 'red' }}>
97
+ Chrome version is outdated and insecure. Update it to continue.
98
+ </Text>
99
+ </View>);
100
+ }
80
101
  let otherView = null;
81
102
  if (viewState === 'LOADING') {
82
103
  otherView = (renderLoading || defaultRenderLoading)();
@@ -98,10 +119,14 @@ const WebViewComponent = forwardRef(({ overScrollMode = 'always', javaScriptEnab
98
119
  console.warn('WebView: `source.body` is not supported when using GET.');
99
120
  }
100
121
  }
122
+ if (typeof source === "object" && 'uri' in source && !passesWhitelist(source.uri)) {
123
+ // eslint-disable-next-line
124
+ source = { uri: "about:blank" };
125
+ }
101
126
  const NativeWebView = RNCWebView;
102
- const webView = <NativeWebView key="webViewKey" {...otherProps} messagingEnabled={typeof onMessageProp === 'function'} messagingModuleName={messagingModuleName} onLoadingError={onLoadingError} onLoadingFinish={onLoadingFinish} onLoadingProgress={onLoadingProgress} onLoadingStart={onLoadingStart} onHttpError={onHttpError} onRenderProcessGone={onRenderProcessGone} onMessage={onMessage} onShouldStartLoadWithRequest={onShouldStartLoadWithRequest} injectedJavaScriptForMainFrameOnly={injectedJavaScriptForMainFrameOnly} injectedJavaScriptBeforeContentLoadedForMainFrameOnly={injectedJavaScriptBeforeContentLoadedForMainFrameOnly} ref={webViewRef}
127
+ const webView = <NativeWebView key="webViewKey" {...otherProps} messagingEnabled={typeof onMessageProp === 'function'} messagingModuleName={messagingModuleName} onLoadingError={onLoadingError} onLoadingFinish={onLoadingFinish} onLoadingProgress={onLoadingProgress} onLoadingStart={onLoadingStart} onMessage={onMessage} onOpenWindow={onOpenWindow} onShouldStartLoadWithRequest={onShouldStartLoadWithRequest} ref={webViewRef}
103
128
  // TODO: find a better way to type this.
104
- source={resolveAssetSource(source)} style={webViewStyles} overScrollMode={overScrollMode} javaScriptEnabled={javaScriptEnabled} thirdPartyCookiesEnabled={thirdPartyCookiesEnabled} scalesPageToFit={scalesPageToFit} allowsFullscreenVideo={allowsFullscreenVideo} allowFileAccess={allowFileAccess} allowFileAccessFromFileURLs={allowFileAccessFromFileURLs} allowUniversalAccessFromFileURLs={allowUniversalAccessFromFileURLs} saveFormDataDisabled={saveFormDataDisabled} cacheEnabled={cacheEnabled} androidHardwareAccelerationDisabled={androidHardwareAccelerationDisabled} androidLayerType={androidLayerType} setSupportMultipleWindows={setSupportMultipleWindows} setBuiltInZoomControls={setBuiltInZoomControls} setDisplayZoomControls={setDisplayZoomControls} mixedContentMode={mixedContentMode} nestedScrollEnabled={nestedScrollEnabled} mediaPlaybackRequiresUserAction={mediaPlaybackRequiresUserAction}/>;
129
+ source={source} style={webViewStyles} overScrollMode={overScrollMode} javaScriptEnabled={javaScriptEnabled} thirdPartyCookiesEnabled={thirdPartyCookiesEnabled} scalesPageToFit={scalesPageToFit} saveFormDataDisabled={saveFormDataDisabled} cacheEnabled={cacheEnabled} androidHardwareAccelerationDisabled={androidHardwareAccelerationDisabled} androidLayerType={androidLayerType} setSupportMultipleWindows={setSupportMultipleWindows} setBuiltInZoomControls={setBuiltInZoomControls} setDisplayZoomControls={setDisplayZoomControls} mixedContentMode={mixedContentMode} nestedScrollEnabled={nestedScrollEnabled} mediaPlaybackRequiresUserAction={mediaPlaybackRequiresUserAction}/>;
105
130
  return (<View style={webViewContainerStyle}>
106
131
  {webView}
107
132
  {otherView}