Browse Source

fix(android): Do not hold a strong reference to native module in the package (#434)

Stanisław Chmiela 5 years ago
parent
commit
566a6292b4

+ 7
- 16
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java View File

@@ -105,7 +105,6 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
105 105
   // state and release page resources (including any running JavaScript).
106 106
   protected static final String BLANK_URL = "about:blank";
107 107
   protected WebViewConfig mWebViewConfig;
108
-  private RNCWebViewPackage aPackage;
109 108
 
110 109
   public RNCWebViewManager() {
111 110
     mWebViewConfig = new WebViewConfig() {
@@ -171,15 +170,15 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
171 170
       }
172 171
 
173 172
       protected void openFileChooser(ValueCallback<Uri> filePathCallback, String acceptType) {
174
-        getModule().startPhotoPickerIntent(filePathCallback, acceptType);
173
+        getModule(reactContext).startPhotoPickerIntent(filePathCallback, acceptType);
175 174
       }
176 175
 
177 176
       protected void openFileChooser(ValueCallback<Uri> filePathCallback) {
178
-        getModule().startPhotoPickerIntent(filePathCallback, "");
177
+        getModule(reactContext).startPhotoPickerIntent(filePathCallback, "");
179 178
       }
180 179
 
181 180
       protected void openFileChooser(ValueCallback<Uri> filePathCallback, String acceptType, String capture) {
182
-        getModule().startPhotoPickerIntent(filePathCallback, acceptType);
181
+        getModule(reactContext).startPhotoPickerIntent(filePathCallback, acceptType);
183 182
       }
184 183
 
185 184
       @TargetApi(Build.VERSION_CODES.LOLLIPOP)
@@ -188,7 +187,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
188 187
         String[] acceptTypes = fileChooserParams.getAcceptTypes();
189 188
         boolean allowMultiple = fileChooserParams.getMode() == WebChromeClient.FileChooserParams.MODE_OPEN_MULTIPLE;
190 189
         Intent intent = fileChooserParams.createIntent();
191
-        return getModule().startPhotoPickerIntent(filePathCallback, intent, acceptTypes, allowMultiple);
190
+        return getModule(reactContext).startPhotoPickerIntent(filePathCallback, intent, acceptTypes, allowMultiple);
192 191
       }
193 192
     });
194 193
     reactContext.addLifecycleEventListener(webView);
@@ -218,7 +217,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
218 217
 
219 218
     webView.setDownloadListener(new DownloadListener() {
220 219
       public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
221
-        RNCWebViewModule module = getModule();
220
+        RNCWebViewModule module = getModule(reactContext);
222 221
 
223 222
         DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
224 223
 
@@ -552,16 +551,8 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
552 551
     ((RNCWebView) webView).cleanupCallbacksAndDestroy();
553 552
   }
554 553
 
555
-  public RNCWebViewPackage getPackage() {
556
-    return this.aPackage;
557
-  }
558
-
559
-  public void setPackage(RNCWebViewPackage aPackage) {
560
-    this.aPackage = aPackage;
561
-  }
562
-
563
-  public RNCWebViewModule getModule() {
564
-    return this.aPackage.getModule();
554
+  public RNCWebViewModule getModule(ReactContext reactContext) {
555
+    return reactContext.getNativeModule(RNCWebViewModule.class);
565 556
   }
566 557
 
567 558
   protected static class RNCWebViewClient extends WebViewClient {

+ 0
- 11
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewModule.java View File

@@ -40,8 +40,6 @@ public class RNCWebViewModule extends ReactContextBaseJavaModule implements Acti
40 40
   private static final int PICKER_LEGACY = 3;
41 41
   private static final int FILE_DOWNLOAD_PERMISSION_REQUEST = 1;
42 42
   final String DEFAULT_MIME_TYPES = "*/*";
43
-  private final ReactApplicationContext reactContext;
44
-  private RNCWebViewPackage aPackage;
45 43
   private ValueCallback<Uri> filePathCallbackLegacy;
46 44
   private ValueCallback<Uri[]> filePathCallback;
47 45
   private Uri outputFileUri;
@@ -68,7 +66,6 @@ public class RNCWebViewModule extends ReactContextBaseJavaModule implements Acti
68 66
 
69 67
   public RNCWebViewModule(ReactApplicationContext reactContext) {
70 68
     super(reactContext);
71
-    this.reactContext = reactContext;
72 69
     reactContext.addActivityEventListener(this);
73 70
   }
74 71
 
@@ -234,14 +231,6 @@ public class RNCWebViewModule extends ReactContextBaseJavaModule implements Acti
234 231
     return result;
235 232
   }
236 233
 
237
-  public RNCWebViewPackage getPackage() {
238
-    return this.aPackage;
239
-  }
240
-
241
-  public void setPackage(RNCWebViewPackage aPackage) {
242
-    this.aPackage = aPackage;
243
-  }
244
-
245 234
   private Intent getPhotoIntent() {
246 235
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
247 236
     outputFileUri = getOutputUri(MediaStore.ACTION_IMAGE_CAPTURE);

+ 2
- 18
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewPackage.java View File

@@ -6,23 +6,13 @@ import com.facebook.react.bridge.NativeModule;
6 6
 import com.facebook.react.bridge.ReactApplicationContext;
7 7
 import com.facebook.react.uimanager.ViewManager;
8 8
 
9
-import java.util.ArrayList;
10
-import java.util.Arrays;
11 9
 import java.util.Collections;
12 10
 import java.util.List;
13 11
 
14 12
 public class RNCWebViewPackage implements ReactPackage {
15
-
16
-  private RNCWebViewManager manager;
17
-  private RNCWebViewModule module;
18
-
19 13
   @Override
20 14
   public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
21
-    List<NativeModule> modulesList = new ArrayList<>();
22
-    module = new RNCWebViewModule(reactContext);
23
-    module.setPackage(this);
24
-    modulesList.add(module);
25
-    return modulesList;
15
+    return Collections.singletonList(new RNCWebViewModule(reactContext));
26 16
   }
27 17
 
28 18
   // Deprecated from RN 0.47
@@ -32,12 +22,6 @@ public class RNCWebViewPackage implements ReactPackage {
32 22
 
33 23
   @Override
34 24
   public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
35
-    manager = new RNCWebViewManager();
36
-    manager.setPackage(this);
37
-    return Arrays.<ViewManager>asList(manager);
38
-  }
39
-
40
-  public RNCWebViewModule getModule() {
41
-    return module;
25
+    return Collections.singletonList(new RNCWebViewManager());
42 26
   }
43 27
 }