Skip to content

Commit d09abd5

Browse files
[webview_flutter_android] Fix bugs in Java Implementation of the Pigeon API (flutter#4459)
1 parent 76799fa commit d09abd5

19 files changed

Lines changed: 1467 additions & 575 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
package io.flutter.plugins.webviewflutter;
6+
7+
import android.webkit.DownloadListener;
8+
import io.flutter.plugin.common.BinaryMessenger;
9+
import io.flutter.plugins.webviewflutter.GeneratedAndroidWebView.DownloadListenerFlutterApi;
10+
11+
/**
12+
* Flutter Api implementation for {@link DownloadListener}.
13+
*
14+
* <p>Passes arguments of callbacks methods from a {@link DownloadListener} to Dart.
15+
*/
16+
public class DownloadListenerFlutterApiImpl extends DownloadListenerFlutterApi {
17+
private final InstanceManager instanceManager;
18+
19+
/**
20+
* Creates a Flutter api that sends messages to Dart.
21+
*
22+
* @param binaryMessenger handles sending messages to Dart
23+
* @param instanceManager maintains instances stored to communicate with Dart objects
24+
*/
25+
public DownloadListenerFlutterApiImpl(
26+
BinaryMessenger binaryMessenger, InstanceManager instanceManager) {
27+
super(binaryMessenger);
28+
this.instanceManager = instanceManager;
29+
}
30+
31+
/** Passes arguments from {@link DownloadListener#onDownloadStart} to Dart. */
32+
public void onDownloadStart(
33+
DownloadListener downloadListener,
34+
String url,
35+
String userAgent,
36+
String contentDisposition,
37+
String mimetype,
38+
long contentLength,
39+
Reply<Void> callback) {
40+
onDownloadStart(
41+
instanceManager.getInstanceId(downloadListener),
42+
url,
43+
userAgent,
44+
contentDisposition,
45+
mimetype,
46+
contentLength,
47+
callback);
48+
}
49+
50+
/**
51+
* Communicates to Dart that the reference to a {@link DownloadListener} was removed.
52+
*
53+
* @param downloadListener the instance whose reference will be removed
54+
* @param callback reply callback with return value from Dart
55+
*/
56+
public void dispose(DownloadListener downloadListener, Reply<Void> callback) {
57+
final Long instanceId = instanceManager.removeInstance(downloadListener);
58+
if (instanceId != null) {
59+
dispose(instanceId, callback);
60+
} else {
61+
callback.reply(null);
62+
}
63+
}
64+
}

packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/DownloadListenerHostApiImpl.java

Lines changed: 71 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,92 @@
55
package io.flutter.plugins.webviewflutter;
66

77
import android.webkit.DownloadListener;
8-
import io.flutter.plugins.webviewflutter.GeneratedAndroidWebView.DownloadListenerFlutterApi;
8+
import androidx.annotation.NonNull;
9+
import androidx.annotation.Nullable;
10+
import io.flutter.plugins.webviewflutter.GeneratedAndroidWebView.DownloadListenerHostApi;
911

10-
class DownloadListenerHostApiImpl implements GeneratedAndroidWebView.DownloadListenerHostApi {
12+
/**
13+
* Host api implementation for {@link DownloadListener}.
14+
*
15+
* <p>Handles creating {@link DownloadListener}s that intercommunicate with a paired Dart object.
16+
*/
17+
public class DownloadListenerHostApiImpl implements DownloadListenerHostApi {
1118
private final InstanceManager instanceManager;
1219
private final DownloadListenerCreator downloadListenerCreator;
13-
private final GeneratedAndroidWebView.DownloadListenerFlutterApi downloadListenerFlutterApi;
14-
15-
static class DownloadListenerCreator {
16-
DownloadListener createDownloadListener(
17-
Long instanceId, DownloadListenerFlutterApi downloadListenerFlutterApi) {
18-
return (url, userAgent, contentDisposition, mimetype, contentLength) ->
19-
downloadListenerFlutterApi.onDownloadStart(
20-
instanceId, url, userAgent, contentDisposition, mimetype, contentLength, reply -> {});
20+
private final DownloadListenerFlutterApiImpl flutterApi;
21+
22+
/**
23+
* Implementation of {@link DownloadListener} that passes arguments of callback methods to Dart.
24+
*
25+
* <p>No messages are sent to Dart after {@link DownloadListenerImpl#release} is called.
26+
*/
27+
public static class DownloadListenerImpl implements DownloadListener, Releasable {
28+
@Nullable private DownloadListenerFlutterApiImpl flutterApi;
29+
30+
/**
31+
* Creates a {@link DownloadListenerImpl} that passes arguments of callbacks methods to Dart.
32+
*
33+
* @param flutterApi handles sending messages to Dart
34+
*/
35+
public DownloadListenerImpl(@NonNull DownloadListenerFlutterApiImpl flutterApi) {
36+
this.flutterApi = flutterApi;
37+
}
38+
39+
@Override
40+
public void onDownloadStart(
41+
String url,
42+
String userAgent,
43+
String contentDisposition,
44+
String mimetype,
45+
long contentLength) {
46+
if (flutterApi != null) {
47+
flutterApi.onDownloadStart(
48+
this, url, userAgent, contentDisposition, mimetype, contentLength, reply -> {});
49+
}
50+
}
51+
52+
@Override
53+
public void release() {
54+
if (flutterApi != null) {
55+
flutterApi.dispose(this, reply -> {});
56+
}
57+
flutterApi = null;
2158
}
2259
}
2360

24-
DownloadListenerHostApiImpl(
61+
/** Handles creating {@link DownloadListenerImpl}s for a {@link DownloadListenerHostApiImpl}. */
62+
public static class DownloadListenerCreator {
63+
/**
64+
* Creates a {@link DownloadListenerImpl}.
65+
*
66+
* @param flutterApi handles sending messages to Dart
67+
* @return the created {@link DownloadListenerImpl}
68+
*/
69+
public DownloadListenerImpl createDownloadListener(DownloadListenerFlutterApiImpl flutterApi) {
70+
return new DownloadListenerImpl(flutterApi);
71+
}
72+
}
73+
74+
/**
75+
* Creates a host API that handles creating {@link DownloadListener}s.
76+
*
77+
* @param instanceManager maintains instances stored to communicate with Dart objects
78+
* @param downloadListenerCreator handles creating {@link DownloadListenerImpl}s
79+
* @param flutterApi handles sending messages to Dart
80+
*/
81+
public DownloadListenerHostApiImpl(
2582
InstanceManager instanceManager,
2683
DownloadListenerCreator downloadListenerCreator,
27-
DownloadListenerFlutterApi downloadListenerFlutterApi) {
84+
DownloadListenerFlutterApiImpl flutterApi) {
2885
this.instanceManager = instanceManager;
2986
this.downloadListenerCreator = downloadListenerCreator;
30-
this.downloadListenerFlutterApi = downloadListenerFlutterApi;
87+
this.flutterApi = flutterApi;
3188
}
3289

3390
@Override
3491
public void create(Long instanceId) {
3592
final DownloadListener downloadListener =
36-
downloadListenerCreator.createDownloadListener(instanceId, downloadListenerFlutterApi);
93+
downloadListenerCreator.createDownloadListener(flutterApi);
3794
instanceManager.addInstance(downloadListener, instanceId);
3895
}
39-
40-
@Override
41-
public void dispose(Long instanceId) {
42-
instanceManager.removeInstance(instanceId);
43-
}
4496
}

0 commit comments

Comments
 (0)