浏览代码

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 年前
父节点
当前提交
04f9fb23ba
没有帐户链接到提交者的电子邮件

+ 12
- 1
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java 查看文件

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

+ 3
- 1
package.json 查看文件

35
   "dependencies": {
35
   "dependencies": {
36
     "escape-string-regexp": "2.0.0",
36
     "escape-string-regexp": "2.0.0",
37
     "invariant": "2.2.4",
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
   "devDependencies": {
41
   "devDependencies": {
41
     "@babel/core": "7.4.5",
42
     "@babel/core": "7.4.5",
48
     "@types/jest": "24.0.18",
49
     "@types/jest": "24.0.18",
49
     "@types/react": "16.8.8",
50
     "@types/react": "16.8.8",
50
     "@types/react-native": "0.60.11",
51
     "@types/react-native": "0.60.11",
52
+    "@types/uuid": "^7.0.2",
51
     "@typescript-eslint/eslint-plugin": "2.1.0",
53
     "@typescript-eslint/eslint-plugin": "2.1.0",
52
     "@typescript-eslint/parser": "2.1.0",
54
     "@typescript-eslint/parser": "2.1.0",
53
     "babel-eslint": "10.0.3",
55
     "babel-eslint": "10.0.3",

+ 7
- 1
src/WebView.android.tsx 查看文件

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

+ 1
- 0
src/WebViewTypes.ts 查看文件

272
   saveFormDataDisabled?: boolean;
272
   saveFormDataDisabled?: boolean;
273
   textZoom?: number;
273
   textZoom?: number;
274
   thirdPartyCookiesEnabled?: boolean;
274
   thirdPartyCookiesEnabled?: boolean;
275
+  messagingModuleName?: string;
275
   readonly urlPrefixesForDefaultIntent?: string[];
276
   readonly urlPrefixesForDefaultIntent?: string[];
276
 }
277
 }
277
 
278
 

+ 10
- 0
yarn.lock 查看文件

1502
   resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e"
1502
   resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e"
1503
   integrity sha512-l42BggppR6zLmpfU6fq9HEa2oGPEI8yrSPL3GITjfRInppYFahObbIQOQK3UGxEnyQpltZLaPe75046NOZQikw==
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
 "@types/yargs-parser@*":
1510
 "@types/yargs-parser@*":
1506
   version "13.0.0"
1511
   version "13.0.0"
1507
   resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-13.0.0.tgz#453743c5bbf9f1bed61d959baab5b06be029b2d0"
1512
   resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-13.0.0.tgz#453743c5bbf9f1bed61d959baab5b06be029b2d0"
10154
   resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.3.tgz#4568f0216e78760ee1dbf3a4d2cf53e224112866"
10159
   resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.3.tgz#4568f0216e78760ee1dbf3a4d2cf53e224112866"
10155
   integrity sha512-pW0No1RGHgzlpHJO1nsVrHKpOEIxkGg1xB+v0ZmdNH5OAeAwzAVrCnI2/6Mtx+Uys6iaylxa+D3g4j63IKKjSQ==
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
 v8-compile-cache@^2.0.3:
10167
 v8-compile-cache@^2.0.3:
10158
   version "2.1.0"
10168
   version "2.1.0"
10159
   resolved "https://registry.yarnpkg.com/v8-compile-cache/-/v8-compile-cache-2.1.0.tgz#e14de37b31a6d194f5690d67efc4e7f6fc6ab30e"
10169
   resolved "https://registry.yarnpkg.com/v8-compile-cache/-/v8-compile-cache-2.1.0.tgz#e14de37b31a6d194f5690d67efc4e7f6fc6ab30e"