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

Commit 2c16458

Browse files
authored
[webview_flutter] Add platform interface method loadRequest. (#4450)
1 parent 16e410b commit 2c16458

File tree

8 files changed

+182
-1
lines changed

8 files changed

+182
-1
lines changed

packages/webview_flutter/webview_flutter_platform_interface/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.3.0
2+
3+
* Added `loadRequest` method to platform interface.
4+
15
## 1.2.0
26

37
* Added `runJavascript` and `runJavascriptReturningResult` interface methods to supersede `evaluateJavascript`.

packages/webview_flutter/webview_flutter_platform_interface/lib/src/method_channel/webview_method_channel.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ class MethodChannelWebViewPlatform implements WebViewPlatformController {
9191
});
9292
}
9393

94+
@override
95+
Future<void> loadRequest(WebViewRequest request) async {
96+
assert(request != null);
97+
return _channel.invokeMethod<void>('loadRequest', <String, dynamic>{
98+
'request': request.toJson(),
99+
});
100+
}
101+
94102
@override
95103
Future<String?> currentUrl() => _channel.invokeMethod<String>('currentUrl');
96104

packages/webview_flutter/webview_flutter_platform_interface/lib/src/platform_interface/webview_platform_controller.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,25 @@ abstract class WebViewPlatformController {
3939
"WebView loadUrl is not implemented on the current platform");
4040
}
4141

42+
/// Makes a specific HTTP request ands loads the response in the webview.
43+
///
44+
/// [WebViewRequest.method] must be one of the supported HTTP methods
45+
/// in [WebViewRequestMethod].
46+
///
47+
/// If [WebViewRequest.headers] is not empty, its key-value pairs will be
48+
/// added as the headers for the request.
49+
///
50+
/// If [WebViewRequest.body] is not null, it will be added as the body
51+
/// for the request.
52+
///
53+
/// Throws an ArgumentError if [WebViewRequest.uri] has empty scheme.
54+
Future<void> loadRequest(
55+
WebViewRequest request,
56+
) {
57+
throw UnimplementedError(
58+
"WebView loadRequest is not implemented on the current platform");
59+
}
60+
4261
/// Updates the webview settings.
4362
///
4463
/// Any non null field in `settings` will be set as the new setting value.

packages/webview_flutter/webview_flutter_platform_interface/lib/src/types/types.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ export 'javascript_mode.dart';
1010
export 'web_resource_error.dart';
1111
export 'web_resource_error_type.dart';
1212
export 'web_settings.dart';
13+
export 'webview_request.dart';
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
import 'dart:typed_data';
6+
7+
/// Defines the supported HTTP methods for loading a page in [WebView].
8+
enum WebViewRequestMethod {
9+
/// HTTP GET method.
10+
get,
11+
12+
/// HTTP POST method.
13+
post,
14+
}
15+
16+
/// Extension methods on the [WebViewRequestMethod] enum.
17+
extension WebViewRequestMethodExtensions on WebViewRequestMethod {
18+
/// Converts [WebViewRequestMethod] to [String] format.
19+
String serialize() {
20+
switch (this) {
21+
case WebViewRequestMethod.get:
22+
return 'get';
23+
case WebViewRequestMethod.post:
24+
return 'post';
25+
}
26+
}
27+
}
28+
29+
/// Defines the parameters that can be used to load a page in the [WebView].
30+
class WebViewRequest {
31+
/// Creates the [WebViewRequest].
32+
WebViewRequest({
33+
required this.uri,
34+
required this.method,
35+
this.headers = const {},
36+
this.body,
37+
});
38+
39+
/// URI for the request.
40+
final Uri uri;
41+
42+
/// HTTP method used to make the request.
43+
final WebViewRequestMethod method;
44+
45+
/// Headers for the request.
46+
final Map<String, String> headers;
47+
48+
/// HTTP body for the request.
49+
final Uint8List? body;
50+
51+
/// Serializes the [WebViewRequest] to JSON.
52+
Map<String, dynamic> toJson() => {
53+
'uri': this.uri.toString(),
54+
'method': this.method.serialize(),
55+
'headers': this.headers,
56+
'body': this.body,
57+
};
58+
}

packages/webview_flutter/webview_flutter_platform_interface/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ repository: https://github.com/flutter/plugins/tree/master/packages/webview_flut
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview_flutter%22
55
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
66
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
7-
version: 1.2.0
7+
version: 1.3.0
88

99
environment:
1010
sdk: ">=2.12.0 <3.0.0"

packages/webview_flutter/webview_flutter_platform_interface/test/src/method_channel/webview_method_channel_test.dart

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'dart:typed_data';
6+
57
import 'package:flutter/services.dart';
68
import 'package:flutter_test/flutter_test.dart';
79
import 'package:mockito/mockito.dart';
@@ -101,6 +103,56 @@ void main() {
101103
);
102104
});
103105

106+
test('loadRequest', () async {
107+
await webViewPlatform.loadRequest(WebViewRequest(
108+
uri: Uri.parse('https://test.url'),
109+
method: WebViewRequestMethod.get,
110+
));
111+
112+
expect(
113+
log,
114+
<Matcher>[
115+
isMethodCall(
116+
'loadRequest',
117+
arguments: <String, dynamic>{
118+
'request': {
119+
'uri': 'https://test.url',
120+
'method': 'get',
121+
'headers': {},
122+
'body': null,
123+
}
124+
},
125+
),
126+
],
127+
);
128+
});
129+
130+
test('loadRequest with optional parameters', () async {
131+
await webViewPlatform.loadRequest(WebViewRequest(
132+
uri: Uri.parse('https://test.url'),
133+
method: WebViewRequestMethod.get,
134+
headers: {'foo': 'bar'},
135+
body: Uint8List.fromList('hello world'.codeUnits),
136+
));
137+
138+
expect(
139+
log,
140+
<Matcher>[
141+
isMethodCall(
142+
'loadRequest',
143+
arguments: <String, dynamic>{
144+
'request': {
145+
'uri': 'https://test.url',
146+
'method': 'get',
147+
'headers': {'foo': 'bar'},
148+
'body': 'hello world'.codeUnits,
149+
}
150+
},
151+
),
152+
],
153+
);
154+
});
155+
104156
test('currentUrl', () async {
105157
final String? currentUrl = await webViewPlatform.currentUrl();
106158

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
import 'dart:typed_data';
6+
import 'package:flutter_test/flutter_test.dart';
7+
import 'package:webview_flutter_platform_interface/src/types/types.dart';
8+
9+
void main() {
10+
test('WebViewRequestMethod should serialize correctly', () {
11+
expect(WebViewRequestMethod.get.serialize(), 'get');
12+
expect(WebViewRequestMethod.post.serialize(), 'post');
13+
});
14+
15+
test('WebViewRequest should serialize correctly', () {
16+
WebViewRequest request;
17+
Map<String, dynamic> serializedRequest;
18+
// Test serialization without headers or a body
19+
request = WebViewRequest(
20+
uri: Uri.parse('https://flutter.dev'),
21+
method: WebViewRequestMethod.get,
22+
);
23+
serializedRequest = request.toJson();
24+
expect(serializedRequest['uri'], 'https://flutter.dev');
25+
expect(serializedRequest['method'], 'get');
26+
expect(serializedRequest['headers'], {});
27+
expect(serializedRequest['body'], null);
28+
// Test serialization of headers and body
29+
request = WebViewRequest(
30+
uri: Uri.parse('https://flutter.dev'),
31+
method: WebViewRequestMethod.get,
32+
headers: {'foo': 'bar'},
33+
body: Uint8List.fromList('Example Body'.codeUnits),
34+
);
35+
serializedRequest = request.toJson();
36+
expect(serializedRequest['headers'], {'foo': 'bar'});
37+
expect(serializedRequest['body'], 'Example Body'.codeUnits);
38+
});
39+
}

0 commit comments

Comments
 (0)