This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[webview_flutter]Add the loadRequest
method
#4169
Closed
Closed
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 957ba7b
Add postUrl method to method channel
ydag b361ed7
Add postUrl method to app-facing
ydag 5ba03b8
Add postUrl call to OnMethodCall on Android
ydag a99ce24
Add postUrl method on Android
ydag 730018c
Add mockito dependency
ydag aafea35
Add a constructor for testing
ydag 2c60a45
Add unit tests on Android
ydag d7d54f3
Add postUrl method call on iOS
ydag 58121df
Add onPostUrl method
ydag 83557f8
Add postRequest method
ydag 8a6a524
Add postUrl method
ydag b941672
Add new constructor for testing
ydag 6233114
Add mocking for testing on iOS
ydag 1bc51fc
Add unit tests on iOS
ydag 3c0b775
Update version and CHANGELOG
ydag 377a0f8
Fix format
ydag 68463e6
Merge branch 'master' into webview_flutter/add_post_url
ydag c405740
Merge branch 'master' into webview_flutter/add_post_url
ydag 6fa4255
Add DisplayListenerProxy to constructor
ydag 677f8b5
Move the unit test to FlutterWebViewTest class
ydag 8663b07
Fix the format
ydag 3a7d9c7
Merge branch 'master' into webview_flutter/add_post_url
ydag 56c2aba
Change names of unit tests
ydag 5aac75a
Make postUrl private
ydag a8ded90
Call public method instead private
ydag b2d364d
Remove constructor
ydag a260ca3
Remove initWithWebView constructor
ydag 4936dda
Add createFLTWKWebViewWithFrame method
ydag 37c7930
Create MockFLTWebViewController to mock FLTWKWebView
ydag bb6c67c
Refactor unit tests with the new implementation
ydag 96c4922
Add method comments
ydag 7253d1a
Refactor postRequest method
ydag 2332582
Add buildNSURLRequest and remove postUrl, postRequest
ydag 495a6d7
Refactor unit tests with the new implementation
ydag a6b5dc2
Add method comments
ydag 0b41767
Remove comments
ydag a7567f3
Add WebViewRequest and WebViewLoadMethod
ydag 22bd781
Refactor loadUrl method with request object
ydag af45469
Refactor loadUrl method in the app-facing
ydag a496344
Refactor loadUrl method
ydag d04e74a
Refactor tests after changes in loadUrl method.
ydag 916ae97
Refactor integration test after changes in loadUrl method.
ydag 8bfaa6a
Refactor buildNSURLRequest
ydag 215a4e1
Remove loadRequest and loadUrl methods
ydag 9264a6a
Refactor onLoadUrl to unify all HTTP load with URL methods
ydag 5dbe50a
Refactor unit tests
ydag ea8b782
Add WebViewRequest
ydag b581476
Add CustomHttpPostRequest
ydag 3d5c954
Add to instantiate CustomHttpPostRequest
ydag f43c386
Add createCustomHttpPostRequest method
ydag 7a74436
Refactor loadUrl to unify all HTTP load with URL methods
ydag 83da7ee
Refactor unit tests
ydag 7ae1821
Add unit tests
ydag 88a6db7
Merge branch 'master' into webview_flutter/add_post_url
ydag 4fc237c
Update the CHANGELOG and version
ydag 00814be
Fix the comment
ydag 91f152b
Add licence comment
ydag 562fec9
Add licence on android
ydag 9c85d79
Remove unused import
ydag 28f03cd
Add Uri to get URLs and set headers empty by default
ydag 7f4b50b
Add loadRequest
ydag 92448f6
Add loadRequest and refactor comment
ydag 2f7b146
Add _validateUri method
ydag e71a7de
Add loadRequest and deprecate loadUrl
ydag fbbf1da
Revert changes on tests
ydag ec8f786
Fix the comment
ydag 5711c5c
Refactor WebViewRequest
ydag 3c0cbf3
Add loadRequest and deprecate loadUrl
ydag 9db2760
Refactor unit tests on Android
ydag 8b030be
Add onLoadRequest
ydag f5e5d21
Refactor according to changes on request
ydag 3608d40
Change the method to onLoadRequest
ydag 7862d30
Refactor test after changes on buildNSURLRequest
ydag b70b227
Fix unit tests
ydag a37d770
Revert changes for deprecated method
ydag 433218c
Update version and CHANGELOG
ydag b702e1b
Merge branch 'master' into webview_flutter/add_post_url
ydag 64be755
Fix the import
ydag 17a30f0
Refactor headers to make it empty collections instead nullable
ydag 58fdf9e
Add ignore for deprecated method
ydag 9c78996
Fix parameter
ydag 139b377
Java: Added additional tests and small refactors
BeMacized 374bd47
Merge remote-tracking branch 'origin/webview_flutter/add_post_url' in…
BeMacized c095dc3
Fix tests
BeMacized 817abe0
Add additional tests
BeMacized 27692b3
Add license to test classes
ydag File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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 { | ||
|
||
|
@@ -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 { | ||
|
@@ -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); | ||
|
@@ -102,6 +110,12 @@ public void onProgressChanged(WebView view, int progress) { | |
|
||
platformThreadHandler = new Handler(context.getMainLooper()); | ||
|
||
executorService = Executors.newFixedThreadPool(4); | ||
|
||
mainThreadHandler = HandlerCompat.createAsync(Looper.getMainLooper()); | ||
|
||
httpRequestManager = HttpRequestManagerFactory.create(executorService, mainThreadHandler); | ||
|
||
this.methodChannel = methodChannel; | ||
this.methodChannel.setMethodCallHandler(this); | ||
|
||
|
@@ -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; | ||
|
@@ -282,6 +299,7 @@ public void onMethodCall(MethodCall methodCall, Result result) { | |
} | ||
} | ||
|
||
@Deprecated | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
} | ||
|
@@ -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); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.