Ver código fonte

fix(Android): Ensure each mounted WebView binds their personal onMessage handler (#1301)

* Ensure each mounted WebView binds their personal onMessage handler

* Changed unique ref generation to uuid

Uses `uuid` npm package.
Dashes are removed from the ref for sanity.
ivari 4 anos atrás
pai
commit
04f9fb23ba
Nenhuma conta vinculada ao e-mail do autor do commit

+ 12
- 1
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java Ver arquivo

@@ -398,6 +398,11 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
398 398
   public void setMessagingEnabled(WebView view, boolean enabled) {
399 399
     ((RNCWebView) view).setMessagingEnabled(enabled);
400 400
   }
401
+
402
+  @ReactProp(name = "messagingModuleName")
403
+  public void setMessagingModuleName(WebView view, String moduleName) {
404
+    ((RNCWebView) view).setMessagingModuleName(moduleName);
405
+  }
401 406
    
402 407
   @ReactProp(name = "incognito")
403 408
   public void setIncognito(WebView view, boolean enabled) {
@@ -975,6 +980,8 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
975 980
     String injectedJS;
976 981
     protected boolean messagingEnabled = false;
977 982
     protected @Nullable
983
+    String messagingModuleName;
984
+    protected @Nullable
978 985
     RNCWebViewClient mRNCWebViewClient;
979 986
     protected @Nullable
980 987
     CatalystInstance mCatalystInstance;
@@ -1076,6 +1083,10 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
1076 1083
       }
1077 1084
     }
1078 1085
 
1086
+    public void setMessagingModuleName(String moduleName) {
1087
+      messagingModuleName = moduleName;
1088
+    }
1089
+
1079 1090
     protected void evaluateJavascriptWithFallback(String script) {
1080 1091
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
1081 1092
         evaluateJavascript(script, null);
@@ -1139,7 +1150,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
1139 1150
       WritableNativeArray params = new WritableNativeArray();
1140 1151
       params.pushMap(event);
1141 1152
 
1142
-      mCatalystInstance.callFunction("WebViewMessageHandler", "onMessage", params);
1153
+      mCatalystInstance.callFunction(messagingModuleName, "onMessage", params);
1143 1154
     }
1144 1155
 
1145 1156
     protected void onScrollChanged(int x, int y, int oldX, int oldY) {

+ 3
- 1
package.json Ver arquivo

@@ -35,7 +35,8 @@
35 35
   "dependencies": {
36 36
     "escape-string-regexp": "2.0.0",
37 37
     "invariant": "2.2.4",
38
-    "rnpm-plugin-windows": "^0.5.1-0"
38
+    "rnpm-plugin-windows": "^0.5.1-0",
39
+    "uuid": "^7.0.3"
39 40
   },
40 41
   "devDependencies": {
41 42
     "@babel/core": "7.4.5",
@@ -48,6 +49,7 @@
48 49
     "@types/jest": "24.0.18",
49 50
     "@types/react": "16.8.8",
50 51
     "@types/react-native": "0.60.11",
52
+    "@types/uuid": "^7.0.2",
51 53
     "@typescript-eslint/eslint-plugin": "2.1.0",
52 54
     "@typescript-eslint/parser": "2.1.0",
53 55
     "babel-eslint": "10.0.3",

+ 7
- 1
src/WebView.android.tsx Ver arquivo

@@ -1,4 +1,5 @@
1 1
 import React from 'react';
2
+import { v4 as uuid } from 'uuid';
2 3
 
3 4
 import {
4 5
   Image,
@@ -70,12 +71,16 @@ class WebView extends React.Component<AndroidWebViewProps, State> {
70 71
     lastErrorEvent: null,
71 72
   };
72 73
 
74
+  uniqueRef = uuid().replace(/-/g, '');
75
+
73 76
   webViewRef = React.createRef<NativeWebViewAndroid>();
74 77
 
75 78
   componentDidMount = () => {
76
-    BatchedBridge.registerCallableModule('WebViewMessageHandler', this);
79
+    BatchedBridge.registerCallableModule(this.getMessagingModuleName(), this);
77 80
   }
78 81
 
82
+  getMessagingModuleName = () => `WebViewMessageHandler${this.uniqueRef}`;
83
+
79 84
   getCommands = () => UIManager.getViewManagerConfig('RNCWebView').Commands;
80 85
 
81 86
   goForward = () => {
@@ -333,6 +338,7 @@ class WebView extends React.Component<AndroidWebViewProps, State> {
333 338
         key="webViewKey"
334 339
         {...otherProps}
335 340
         messagingEnabled={typeof onMessage === 'function'}
341
+        messagingModuleName={this.getMessagingModuleName()}
336 342
         onLoadingError={this.onLoadingError}
337 343
         onLoadingFinish={this.onLoadingFinish}
338 344
         onLoadingProgress={this.onLoadingProgress}

+ 1
- 0
src/WebViewTypes.ts Ver arquivo

@@ -272,6 +272,7 @@ export interface AndroidNativeWebViewProps extends CommonNativeWebViewProps {
272 272
   saveFormDataDisabled?: boolean;
273 273
   textZoom?: number;
274 274
   thirdPartyCookiesEnabled?: boolean;
275
+  messagingModuleName?: string;
275 276
   readonly urlPrefixesForDefaultIntent?: string[];
276 277
 }
277 278
 

+ 10
- 0
yarn.lock Ver arquivo

@@ -1502,6 +1502,11 @@
1502 1502
   resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e"
1503 1503
   integrity sha512-l42BggppR6zLmpfU6fq9HEa2oGPEI8yrSPL3GITjfRInppYFahObbIQOQK3UGxEnyQpltZLaPe75046NOZQikw==
1504 1504
 
1505
+"@types/uuid@^7.0.2":
1506
+  version "7.0.2"
1507
+  resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-7.0.2.tgz#d680a9c596ef84abf5c4c07a32ffd66d582526f8"
1508
+  integrity sha512-8Ly3zIPTnT0/8RCU6Kg/G3uTICf9sRwYOpUzSIM3503tLIKcnJPRuinHhXngJUy2MntrEf6dlpOHXJju90Qh5w==
1509
+
1505 1510
 "@types/yargs-parser@*":
1506 1511
   version "13.0.0"
1507 1512
   resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-13.0.0.tgz#453743c5bbf9f1bed61d959baab5b06be029b2d0"
@@ -10154,6 +10159,11 @@ uuid@^3.3.2, uuid@^3.3.3:
10154 10159
   resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.3.tgz#4568f0216e78760ee1dbf3a4d2cf53e224112866"
10155 10160
   integrity sha512-pW0No1RGHgzlpHJO1nsVrHKpOEIxkGg1xB+v0ZmdNH5OAeAwzAVrCnI2/6Mtx+Uys6iaylxa+D3g4j63IKKjSQ==
10156 10161
 
10162
+uuid@^7.0.3:
10163
+  version "7.0.3"
10164
+  resolved "https://registry.yarnpkg.com/uuid/-/uuid-7.0.3.tgz#c5c9f2c8cf25dc0a372c4df1441c41f5bd0c680b"
10165
+  integrity sha512-DPSke0pXhTZgoF/d+WSt2QaKMCFSfx7QegxEWT+JOuHF5aWrKEn0G+ztjuJg/gG8/ItK+rbPCD/yNv8yyih6Cg==
10166
+
10157 10167
 v8-compile-cache@^2.0.3:
10158 10168
   version "2.1.0"
10159 10169
   resolved "https://registry.yarnpkg.com/v8-compile-cache/-/v8-compile-cache-2.1.0.tgz#e14de37b31a6d194f5690d67efc4e7f6fc6ab30e"