Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[webview_flutter]Add the loadRequest method #4169

Closed
wants to merge 87 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
9eb8dd3
Add postUrl method to platform interface
ydag Jul 20, 2021
957ba7b
Add postUrl method to method channel
ydag Jul 20, 2021
b361ed7
Add postUrl method to app-facing
ydag Jul 20, 2021
5ba03b8
Add postUrl call to OnMethodCall on Android
ydag Jul 20, 2021
a99ce24
Add postUrl method on Android
ydag Jul 20, 2021
730018c
Add mockito dependency
ydag Jul 20, 2021
aafea35
Add a constructor for testing
ydag Jul 20, 2021
2c60a45
Add unit tests on Android
ydag Jul 20, 2021
d7d54f3
Add postUrl method call on iOS
ydag Jul 20, 2021
58121df
Add onPostUrl method
ydag Jul 20, 2021
83557f8
Add postRequest method
ydag Jul 20, 2021
8a6a524
Add postUrl method
ydag Jul 20, 2021
b941672
Add new constructor for testing
ydag Jul 20, 2021
6233114
Add mocking for testing on iOS
ydag Jul 20, 2021
1bc51fc
Add unit tests on iOS
ydag Jul 20, 2021
3c0b775
Update version and CHANGELOG
ydag Jul 20, 2021
377a0f8
Fix format
ydag Jul 20, 2021
68463e6
Merge branch 'master' into webview_flutter/add_post_url
ydag Jul 21, 2021
c405740
Merge branch 'master' into webview_flutter/add_post_url
ydag Jul 23, 2021
6fa4255
Add DisplayListenerProxy to constructor
ydag Jul 26, 2021
677f8b5
Move the unit test to FlutterWebViewTest class
ydag Jul 26, 2021
8663b07
Fix the format
ydag Jul 26, 2021
3a7d9c7
Merge branch 'master' into webview_flutter/add_post_url
ydag Jul 26, 2021
56c2aba
Change names of unit tests
ydag Jul 26, 2021
5aac75a
Make postUrl private
ydag Jul 26, 2021
a8ded90
Call public method instead private
ydag Jul 26, 2021
b2d364d
Remove constructor
ydag Jul 26, 2021
a260ca3
Remove initWithWebView constructor
ydag Jul 26, 2021
4936dda
Add createFLTWKWebViewWithFrame method
ydag Jul 26, 2021
37c7930
Create MockFLTWebViewController to mock FLTWKWebView
ydag Jul 26, 2021
bb6c67c
Refactor unit tests with the new implementation
ydag Jul 26, 2021
96c4922
Add method comments
ydag Jul 27, 2021
7253d1a
Refactor postRequest method
ydag Jul 27, 2021
2332582
Add buildNSURLRequest and remove postUrl, postRequest
ydag Jul 28, 2021
495a6d7
Refactor unit tests with the new implementation
ydag Jul 28, 2021
a6b5dc2
Add method comments
ydag Jul 28, 2021
0b41767
Remove comments
ydag Jul 28, 2021
a7567f3
Add WebViewRequest and WebViewLoadMethod
ydag Aug 9, 2021
22bd781
Refactor loadUrl method with request object
ydag Aug 9, 2021
af45469
Refactor loadUrl method in the app-facing
ydag Aug 9, 2021
a496344
Refactor loadUrl method
ydag Aug 9, 2021
d04e74a
Refactor tests after changes in loadUrl method.
ydag Aug 9, 2021
916ae97
Refactor integration test after changes in loadUrl method.
ydag Aug 9, 2021
8bfaa6a
Refactor buildNSURLRequest
ydag Aug 9, 2021
215a4e1
Remove loadRequest and loadUrl methods
ydag Aug 9, 2021
9264a6a
Refactor onLoadUrl to unify all HTTP load with URL methods
ydag Aug 9, 2021
5dbe50a
Refactor unit tests
ydag Aug 9, 2021
ea8b782
Add WebViewRequest
ydag Aug 9, 2021
b581476
Add CustomHttpPostRequest
ydag Aug 9, 2021
3d5c954
Add to instantiate CustomHttpPostRequest
ydag Aug 9, 2021
f43c386
Add createCustomHttpPostRequest method
ydag Aug 9, 2021
7a74436
Refactor loadUrl to unify all HTTP load with URL methods
ydag Aug 9, 2021
83da7ee
Refactor unit tests
ydag Aug 9, 2021
7ae1821
Add unit tests
ydag Aug 9, 2021
88a6db7
Merge branch 'master' into webview_flutter/add_post_url
ydag Aug 9, 2021
4fc237c
Update the CHANGELOG and version
ydag Aug 9, 2021
00814be
Fix the comment
ydag Aug 9, 2021
91f152b
Add licence comment
ydag Aug 9, 2021
562fec9
Add licence on android
ydag Aug 9, 2021
9c85d79
Remove unused import
ydag Aug 9, 2021
28f03cd
Add Uri to get URLs and set headers empty by default
ydag Aug 19, 2021
7f4b50b
Add loadRequest
ydag Aug 19, 2021
92448f6
Add loadRequest and refactor comment
ydag Aug 19, 2021
2f7b146
Add _validateUri method
ydag Aug 19, 2021
e71a7de
Add loadRequest and deprecate loadUrl
ydag Aug 19, 2021
fbbf1da
Revert changes on tests
ydag Aug 19, 2021
ec8f786
Fix the comment
ydag Aug 19, 2021
5711c5c
Refactor WebViewRequest
ydag Aug 19, 2021
3c0cbf3
Add loadRequest and deprecate loadUrl
ydag Aug 19, 2021
9db2760
Refactor unit tests on Android
ydag Aug 19, 2021
8b030be
Add onLoadRequest
ydag Aug 20, 2021
f5e5d21
Refactor according to changes on request
ydag Aug 20, 2021
3608d40
Change the method to onLoadRequest
ydag Aug 20, 2021
7862d30
Refactor test after changes on buildNSURLRequest
ydag Aug 20, 2021
b70b227
Fix unit tests
ydag Aug 27, 2021
a37d770
Revert changes for deprecated method
ydag Aug 27, 2021
433218c
Update version and CHANGELOG
ydag Aug 27, 2021
b702e1b
Merge branch 'master' into webview_flutter/add_post_url
ydag Aug 27, 2021
64be755
Fix the import
ydag Aug 27, 2021
17a30f0
Refactor headers to make it empty collections instead nullable
ydag Aug 27, 2021
58fdf9e
Add ignore for deprecated method
ydag Aug 27, 2021
9c78996
Fix parameter
ydag Aug 31, 2021
139b377
Java: Added additional tests and small refactors
BeMacized Aug 31, 2021
374bd47
Merge remote-tracking branch 'origin/webview_flutter/add_post_url' in…
BeMacized Aug 31, 2021
c095dc3
Fix tests
BeMacized Aug 31, 2021
817abe0
Add additional tests
BeMacized Aug 31, 2021
27692b3
Add license to test classes
ydag Sep 2, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/webview_flutter/webview_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## NEXT
## 2.1.0

* Add the `loadRequest` method to unify all supported HTTP methods for loading a page with URL
that are defined in `WebViewLoadMethod`.
* Add the `WebViewRequest` class to define parameters that can be used to load a page in `WebView`.
* Updated Android lint settings.

## 2.0.12
Expand Down
5 changes: 5 additions & 0 deletions packages/webview_flutter/webview_flutter/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ android {
disable 'InvalidPackage'
disable 'GradleDependency'
}
compileOptions {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
}


dependencies {
implementation 'androidx.annotation:annotation:1.0.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.hardware.display.DisplayManager;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.webkit.WebChromeClient;
Expand All @@ -18,6 +19,7 @@
import android.webkit.WebViewClient;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.core.os.HandlerCompat;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
Expand All @@ -26,6 +28,9 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class FlutterWebView implements PlatformView, MethodCallHandler {

Expand All @@ -34,6 +39,9 @@ public class FlutterWebView implements PlatformView, MethodCallHandler {
private final MethodChannel methodChannel;
private final FlutterWebViewClient flutterWebViewClient;
private final Handler platformThreadHandler;
private final Handler mainThreadHandler;
private final ExecutorService executorService;
private final HttpRequestManager httpRequestManager;

// Verifies that a url opened by `Window.open` has a secure url.
private class FlutterWebChromeClient extends WebChromeClient {
Expand Down Expand Up @@ -87,9 +95,9 @@ public void onProgressChanged(WebView view, int progress) {
final Context context,
MethodChannel methodChannel,
Map<String, Object> params,
View containerView) {
View containerView,
DisplayListenerProxy displayListenerProxy) {

DisplayListenerProxy displayListenerProxy = new DisplayListenerProxy();
DisplayManager displayManager =
(DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
displayListenerProxy.onPreWebViewInitialization(displayManager);
Expand All @@ -102,6 +110,12 @@ public void onProgressChanged(WebView view, int progress) {

platformThreadHandler = new Handler(context.getMainLooper());

executorService = Executors.newFixedThreadPool(4);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment explaining how the number 4 was derived; it seems very arbitrary.


mainThreadHandler = HandlerCompat.createAsync(Looper.getMainLooper());

httpRequestManager = HttpRequestManagerFactory.create(executorService, mainThreadHandler);

this.methodChannel = methodChannel;
this.methodChannel.setMethodCallHandler(this);

Expand Down Expand Up @@ -229,6 +243,9 @@ public void onMethodCall(MethodCall methodCall, Result result) {
case "loadUrl":
loadUrl(methodCall, result);
break;
case "loadRequest":
loadRequest(methodCall, result);
break;
case "updateSettings":
updateSettings(methodCall, result);
break;
Expand Down Expand Up @@ -282,6 +299,7 @@ public void onMethodCall(MethodCall methodCall, Result result) {
}
}

@Deprecated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we deprecating a private method?

@SuppressWarnings("unchecked")
private void loadUrl(MethodCall methodCall, Result result) {
Map<String, Object> request = (Map<String, Object>) methodCall.arguments;
Expand All @@ -294,6 +312,65 @@ private void loadUrl(MethodCall methodCall, Result result) {
result.success(null);
}

private void loadRequest(MethodCall methodCall, final Result result) {
final WebViewRequest webViewRequest = buildWebViewRequest(methodCall);
if (webViewRequest == null) {
result.error("missing_args", "Missing arguments", null);
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: early return rather than else?

switch (webViewRequest.getMethod()) {
case GET:
webView.loadUrl(webViewRequest.getUrl(), webViewRequest.getHeaders());
break;
case POST:
if (webViewRequest.getHeaders().isEmpty()) {
webView.postUrl(webViewRequest.getUrl(), webViewRequest.getBody());
} else {
// Execute the request manually to be able to provide headers with the post request.
httpRequestManager.requestAsync(
webViewRequest,
new HttpRequestCallback() {
@Override
public void onComplete(String content) {
if (!webView.isAttachedToWindow()) {
result.error(
"webview_destroyed",
"Could not complete the post request because the webview is destroyed",
null);
} else {
webView.loadDataWithBaseURL(
webViewRequest.getUrl(), content, "text/html", "UTF-8", null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we certain that the request URL is always correct? E.g., what happens here in the case of a server-side redirect to a different origin?

}
}

@Override
public void onError(Exception error) {
result.error("request_failed", "HttpURLConnection has failed", null);
}
});
}
break;
default:
result.error("unsupported_method", "Unsupported HTTP method call", null);
}
result.success(null);
}
}

@SuppressWarnings("unchecked")
private WebViewRequest buildWebViewRequest(MethodCall methodCall) {
Map<String, Object> request = (Map<String, Object>) methodCall.arguments;
if (request == null) {
return null;
}

Map<String, Object> requestObject = (Map<String, Object>) request.get("request");
if (requestObject == null) {
return null;
}

return WebViewRequest.fromMap(requestObject);
}

private void canGoBack(Result result) {
result.success(webView.canGoBack());
}
Expand Down Expand Up @@ -482,4 +559,22 @@ public void dispose() {
}
webView.destroy();
}

/** Factory class for creating a {@link HttpRequestManager} */
static class HttpRequestManagerFactory {
/**
* Creates a {@link HttpRequestManager}.
*
* <p><strong>Important:</strong> This method is visible for testing purposes only and should
* never be called from outside this class.
*
* @param executor a {@link Executor} to run network request on background thread.
* @param resultHandler a {@link Handler} to communicate back with main thread.
* @return The new {@link HttpRequestManager} object.
*/
@VisibleForTesting
public static HttpRequestManager create(Executor executor, Handler resultHandler) {
return new HttpRequestManager(executor, resultHandler);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public final class FlutterWebViewFactory extends PlatformViewFactory {
public PlatformView create(Context context, int id, Object args) {
Map<String, Object> params = (Map<String, Object>) args;
MethodChannel methodChannel = new MethodChannel(messenger, "plugins.flutter.io/webview_" + id);
return new FlutterWebView(context, methodChannel, params, containerView);
DisplayListenerProxy displayListenerProxy = new DisplayListenerProxy();
return new FlutterWebView(context, methodChannel, params, containerView, displayListenerProxy);
}
}
Loading