Skip to content

Commit aa37e8d

Browse files
committed
Resolved PR comments
1 parent 32b6d05 commit aa37e8d

File tree

13 files changed

+107
-81
lines changed

13 files changed

+107
-81
lines changed

packages/webview_flutter/webview_flutter/CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
## 4.14.0
22

3-
* Deprecates `WebViewController.loadFile(String)` in favor of `WebViewController.loadFileWithParams(LoadFileParams)`.
43
* Introduces `AndroidLoadFileParams` and `WebKitLoadFileParams` to support platform-specific parameters when loading local HTML files on Android and iOS/macOS.
54

65
## 4.13.0

packages/webview_flutter/webview_flutter/lib/src/webview_controller.dart

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -131,25 +131,10 @@ class WebViewController {
131131
/// `/Users/username/Documents/www/index.html`.
132132
///
133133
/// Throws a `PlatformException` if the [absoluteFilePath] does not exist.
134-
@Deprecated('Use loadFileWithParams(LocalFileParams params) instead. '
135-
'This method will be removed in a future release.')
136134
Future<void> loadFile(String absoluteFilePath) {
137135
return platform.loadFile(absoluteFilePath);
138136
}
139137

140-
/// Loads a local HTML file using the provided [params].
141-
///
142-
/// The [params] object should contain the absolute path to the
143-
/// file as it is stored on the device. For example:
144-
/// `/Users/username/Documents/www/index.html`.
145-
/// In addition, it may include platform-specific fields,
146-
/// such as headers (on Android) or resource paths (on iOS/macOS).
147-
///
148-
/// Throws a `PlatformException` if the [absoluteFilePath] does not exist.
149-
Future<void> loadFileWithParams(LoadFileParams params) {
150-
return platform.loadFileWithParams(params);
151-
}
152-
153138
/// Loads the Flutter asset specified in the pubspec.yaml file.
154139
///
155140
/// Throws a `PlatformException` if [key] is not part of the specified assets

packages/webview_flutter/webview_flutter/test/webview_controller_test.dart

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,6 @@ void main() {
2727
verify(mockPlatformWebViewController.loadFile('file/path'));
2828
});
2929

30-
test('loadFileWithParams', () async {
31-
final MockPlatformWebViewController mockPlatformWebViewController =
32-
MockPlatformWebViewController();
33-
34-
final WebViewController webViewController = WebViewController.fromPlatform(
35-
mockPlatformWebViewController,
36-
);
37-
38-
await webViewController.loadFileWithParams(
39-
const LoadFileParams(absoluteFilePath: 'file/path'),
40-
);
41-
verify(mockPlatformWebViewController.loadFileWithParams(
42-
const LoadFileParams(absoluteFilePath: 'file/path'),
43-
));
44-
});
45-
4630
test('loadFlutterAsset', () async {
4731
final MockPlatformWebViewController mockPlatformWebViewController =
4832
MockPlatformWebViewController();

packages/webview_flutter/webview_flutter/test/webview_controller_test.mocks.dart

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,6 @@ class MockPlatformWebViewController extends _i1.Mock
7878
returnValueForMissingStub: _i5.Future<void>.value(),
7979
) as _i5.Future<void>);
8080

81-
@override
82-
_i5.Future<void> loadFileWithParams(_i2.LoadFileParams? params) =>
83-
(super.noSuchMethod(
84-
Invocation.method(#loadFileWithParams, [params]),
85-
returnValue: _i5.Future<void>.value(),
86-
returnValueForMissingStub: _i5.Future<void>.value(),
87-
) as _i5.Future<void>);
88-
8981
@override
9082
_i5.Future<void> loadFlutterAsset(String? key) => (super.noSuchMethod(
9183
Invocation.method(#loadFlutterAsset, [key]),

packages/webview_flutter/webview_flutter/test/webview_widget_test.mocks.dart

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,6 @@ class MockPlatformWebViewController extends _i1.Mock
9191
returnValueForMissingStub: _i7.Future<void>.value(),
9292
) as _i7.Future<void>);
9393

94-
@override
95-
_i7.Future<void> loadFileWithParams(_i2.LoadFileParams? params) =>
96-
(super.noSuchMethod(
97-
Invocation.method(#loadFileWithParams, [params]),
98-
returnValue: _i7.Future<void>.value(),
99-
returnValueForMissingStub: _i7.Future<void>.value(),
100-
) as _i7.Future<void>);
101-
10294
@override
10395
_i7.Future<void> loadFlutterAsset(String? key) => (super.noSuchMethod(
10496
Invocation.method(#loadFlutterAsset, [key]),

packages/webview_flutter/webview_flutter_android/lib/src/android_webview_controller.dart

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,9 @@ import 'weak_reference_utils.dart';
2323
@immutable
2424
class AndroidLoadFileParams extends LoadFileParams {
2525
/// Constructs a [AndroidLoadFileParams], the subclass of a [LoadFileParams].
26-
///
27-
/// Optionally, [headers] map may be included, which contains key-value pairs
28-
/// that will be passed as additional HTTP headers when loading the file.
2926
AndroidLoadFileParams({
3027
required String absoluteFilePath,
31-
this.headers = _defaultHeaders,
28+
this.headers = const <String, String>{},
3229
}) : super(
3330
absoluteFilePath: absoluteFilePath.startsWith('file://')
3431
? absoluteFilePath
@@ -38,21 +35,16 @@ class AndroidLoadFileParams extends LoadFileParams {
3835
/// Constructs a [AndroidLoadFileParams] using a [LoadFileParams].
3936
factory AndroidLoadFileParams.fromLoadFileParams(
4037
LoadFileParams params, {
41-
Map<String, String> headers = _defaultHeaders,
38+
Map<String, String> headers = const <String, String>{},
4239
}) {
4340
return AndroidLoadFileParams(
4441
absoluteFilePath: params.absoluteFilePath,
4542
headers: headers,
4643
);
4744
}
4845

49-
/// Default empty headers used when no custom headers are provided.
50-
///
51-
/// This constant ensures that the [headers] parameter is never `null`,
52-
/// simplifying internal logic and avoiding the need for null checks.
53-
static const Map<String, String> _defaultHeaders = <String, String>{};
54-
5546
/// Additional HTTP headers to be included when loading the local file.
47+
/// If not provided at initialization time, doesn't add any addition headers.
5648
///
5749
/// On Android, WebView supports adding headers when loading local or remote
5850
/// content. This can be useful for scenarios like authentication,
@@ -425,18 +417,32 @@ class AndroidWebViewController extends PlatformWebViewController {
425417
_webView.pigeon_instanceManager.getIdentifier(_webView)!;
426418

427419
@override
428-
Future<void> loadFileWithParams(LoadFileParams params) {
420+
Future<void> loadFile(
421+
String absoluteFilePath,
422+
) {
423+
return loadFileWithParams(
424+
AndroidLoadFileParams(
425+
absoluteFilePath: absoluteFilePath,
426+
),
427+
);
428+
}
429+
430+
@override
431+
Future<void> loadFileWithParams(
432+
LoadFileParams params,
433+
) async {
429434
switch (params) {
430435
case final AndroidLoadFileParams params:
431-
_webView.settings.setAllowFileAccess(true);
432-
return _webView.loadUrl(
436+
await _webView.settings.setAllowFileAccess(true);
437+
await _webView.loadUrl(
433438
params.absoluteFilePath,
434439
params.headers,
435440
);
436441

437442
default:
438-
return loadFileWithParams(
439-
AndroidLoadFileParams.fromLoadFileParams(params));
443+
await loadFileWithParams(
444+
AndroidLoadFileParams.fromLoadFileParams(params),
445+
);
440446
}
441447
}
442448

packages/webview_flutter/webview_flutter_android/test/android_webview_controller_test.dart

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,60 @@ void main() {
292292
verify(mockWebSettings.setUseWideViewPort(false)).called(1);
293293
});
294294

295+
group('loadFile', () {
296+
test('Without file prefix', () async {
297+
final MockWebView mockWebView = MockWebView();
298+
final MockWebSettings mockWebSettings = MockWebSettings();
299+
final AndroidWebViewController controller = createControllerWithMocks(
300+
mockWebView: mockWebView,
301+
mockSettings: mockWebSettings,
302+
);
303+
304+
await controller.loadFile('/path/to/file.html');
305+
306+
verify(mockWebSettings.setAllowFileAccess(true)).called(1);
307+
verify(mockWebView.loadUrl(
308+
'file:///path/to/file.html',
309+
<String, String>{},
310+
)).called(1);
311+
});
312+
313+
test('Without file prefix and characters to be escaped', () async {
314+
final MockWebView mockWebView = MockWebView();
315+
final MockWebSettings mockWebSettings = MockWebSettings();
316+
final AndroidWebViewController controller = createControllerWithMocks(
317+
mockWebView: mockWebView,
318+
mockSettings: mockWebSettings,
319+
);
320+
321+
await controller.loadFile('/path/to/?_<_>_.html');
322+
323+
verify(mockWebSettings.setAllowFileAccess(true)).called(1);
324+
verify(mockWebView.loadUrl(
325+
'file:///path/to/%3F_%3C_%3E_.html',
326+
<String, String>{},
327+
)).called(1);
328+
});
329+
330+
test('With file prefix', () async {
331+
final MockWebView mockWebView = MockWebView();
332+
final MockWebSettings mockWebSettings = MockWebSettings();
333+
final AndroidWebViewController controller = createControllerWithMocks(
334+
mockWebView: mockWebView,
335+
);
336+
337+
when(mockWebView.settings).thenReturn(mockWebSettings);
338+
339+
await controller.loadFile('file:///path/to/file.html');
340+
341+
verify(mockWebSettings.setAllowFileAccess(true)).called(1);
342+
verify(mockWebView.loadUrl(
343+
'file:///path/to/file.html',
344+
<String, String>{},
345+
)).called(1);
346+
});
347+
});
348+
295349
group('loadFileWithParams', () {
296350
group('Using LoadFileParams model', () {
297351
test('Without file prefix', () async {

packages/webview_flutter/webview_flutter_platform_interface/CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
## 2.14.0
22

3-
* Updates minimum supported SDK version to Flutter 3.27/Dart 3.6.
43
* Adds `PlatformWebViewController.loadFileWithParams(LoadFileParams)` to support loading local HTML files with platform-specific parameters.
54
* Deprecates `PlatformWebViewController.loadFile(String)` in favour of `PlatformWebViewController.loadFileWithParams(LoadFileParams)`.
65

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,11 @@ abstract class PlatformWebViewController extends PlatformInterface {
5454
/// `/Users/username/Documents/www/index.html`.
5555
///
5656
/// Throws an ArgumentError if the [absoluteFilePath] does not exist.
57-
@Deprecated('Use loadFileWithParams(LocalFileParams params) instead. '
58-
'This method will be removed in a future release.')
5957
Future<void> loadFile(
6058
String absoluteFilePath,
6159
) {
62-
return loadFileWithParams(
63-
LoadFileParams(absoluteFilePath: absoluteFilePath));
60+
throw UnimplementedError(
61+
'loadFile is not implemented on the current platform');
6462
}
6563

6664
/// Loads a local HTML file using the provided [params].

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,23 @@ import 'package:flutter/material.dart';
99
/// Platform-specific implementations can add additional fields by extending
1010
/// this class.
1111
///
12-
/// {@tool sample}
13-
/// This example demonstrates how to extend [LocalFileParams] to provide
12+
/// This example demonstrates how to extend [LoadFileParams] to provide
1413
/// additional platform-specific parameters.
1514
///
16-
/// When extending [LocalFileParams], additional parameters should always accept
15+
/// When extending [LoadFileParams], additional parameters should always accept
1716
/// `null` or have a default value to prevent breaking changes.
1817
///
1918
/// ```dart
2019
/// class WebKitLoadFileParams extends LoadFileParams {
2120
/// const WebKitLoadFileParams({
2221
/// required super.absoluteFilePath,
23-
/// required String readAccessPath,
22+
/// required this.readAccessPath,
2423
/// });
2524
///
2625
/// /// The directory to which the WebView is granted read access.
2726
/// final String readAccessPath;
2827
/// }
2928
/// ```
30-
/// {@end-tool}
3129
@immutable
3230
class LoadFileParams {
3331
/// Used by the platform implementation to load local HTML page.

packages/webview_flutter/webview_flutter_wkwebview/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
## 3.18.6
2727

2828
* Fixes `PlatformException` when calling `loadFlutterAsset` on macOS.
29-
* Updates native wrapper with methods handling `SecTust` and `SecCertificate`.
29+
* Updates native wrapper with methods handling `SecTust` and `SecCertificate`.
3030

3131
## 3.18.5
3232

packages/webview_flutter/webview_flutter_wkwebview/lib/src/webkit_webview_controller.dart

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ enum PlaybackMediaTypes {
4343
@immutable
4444
class WebKitLoadFileParams extends LoadFileParams {
4545
/// Constructs a [WebKitLoadFileParams], the subclass of a [LoadFileParams].
46-
///
47-
/// The optional [readAccessPath] defines the directory that the WebView is
48-
/// allowed to read from. If not provided, it defaults to the parent directory
49-
/// of [absoluteFilePath].
5046
WebKitLoadFileParams({
5147
required super.absoluteFilePath,
5248
String? readAccessPath,
@@ -65,6 +61,8 @@ class WebKitLoadFileParams extends LoadFileParams {
6561
}
6662

6763
/// The directory to which the WebView is granted read access.
64+
/// If not provided at initialization time, it defaults to
65+
/// the parent directory of [absoluteFilePath].
6866
///
6967
/// On iOS/macOS, this is required by WebKit to define the scope of readable
7068
/// files when loading a local HTML file. It must include the location of
@@ -438,7 +436,16 @@ class WebKitWebViewController extends PlatformWebViewController {
438436
}
439437

440438
@override
441-
Future<void> loadFileWithParams(LoadFileParams params) {
439+
Future<void> loadFile(String absoluteFilePath) {
440+
return loadFileWithParams(
441+
WebKitLoadFileParams(absoluteFilePath: absoluteFilePath),
442+
);
443+
}
444+
445+
@override
446+
Future<void> loadFileWithParams(
447+
LoadFileParams params,
448+
) {
442449
switch (params) {
443450
case final WebKitLoadFileParams params:
444451
return _webView.loadFileUrl(
@@ -448,7 +455,8 @@ class WebKitWebViewController extends PlatformWebViewController {
448455

449456
default:
450457
return loadFileWithParams(
451-
WebKitLoadFileParams.fromLoadFileParams(params));
458+
WebKitLoadFileParams.fromLoadFileParams(params),
459+
);
452460
}
453461
}
454462

packages/webview_flutter/webview_flutter_wkwebview/test/webkit_webview_controller_test.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,17 @@ void main() {
330330
});
331331
});
332332

333+
test('loadFile', () async {
334+
final MockUIViewWKWebView mockWebView = MockUIViewWKWebView();
335+
336+
final WebKitWebViewController controller = createControllerWithMocks(
337+
createMockWebView: (_, {dynamic observeValue}) => mockWebView,
338+
);
339+
340+
await controller.loadFile('/path/to/file.html');
341+
verify(mockWebView.loadFileUrl('/path/to/file.html', '/path/to'));
342+
});
343+
333344
group('loadFileWithParams', () {
334345
test('Using LoadFileParams model', () async {
335346
final MockUIViewWKWebView mockWebView = MockUIViewWKWebView();

0 commit comments

Comments
 (0)