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

[webview_flutter] Add setAllowFileAccess #3802

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions packages/webview_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 2.1.0
* Export Android WebView configuration: `allowFileAccess`

## 2.0.4

* Fix a bug where `allowsInlineMediaPlayback` is not respected on iOS.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ public void onProgressChanged(WebView view, int progress) {
String url = (String) params.get("initialUrl");
webView.loadUrl(url);
}

if (params.containsKey("allowFileAccess") && params.get("allowFileAccess") != null) {
webView.getSettings().setAllowFileAccess((boolean) params.get("allowFileAccess"));
}
}

@Override
Expand Down
8 changes: 7 additions & 1 deletion packages/webview_flutter/lib/platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ class CreationParams {
this.userAgent,
this.autoMediaPlaybackPolicy =
AutoMediaPlaybackPolicy.require_user_action_for_all_media_types,
this.allowFileAccess,
}) : assert(autoMediaPlaybackPolicy != null);

/// The initialUrl to load in the webview.
Expand Down Expand Up @@ -488,9 +489,14 @@ class CreationParams {
/// Which restrictions apply on automatic media playback.
final AutoMediaPlaybackPolicy autoMediaPlaybackPolicy;

/// The value if it allows file access.
///
/// This setting only applies to Android
final bool? allowFileAccess;

@override
String toString() {
return '$runtimeType(initialUrl: $initialUrl, settings: $webSettings, javascriptChannelNames: $javascriptChannelNames, UserAgent: $userAgent)';
return '$runtimeType(initialUrl: $initialUrl, settings: $webSettings, javascriptChannelNames: $javascriptChannelNames, UserAgent: $userAgent, allowFileAccess: $allowFileAccess)';
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ class MethodChannelWebViewPlatform implements WebViewPlatformController {
'userAgent': creationParams.userAgent,
'autoMediaPlaybackPolicy': creationParams.autoMediaPlaybackPolicy.index,
'usesHybridComposition': usesHybridComposition,
'allowFileAccess': creationParams.allowFileAccess,
};
}
}
9 changes: 8 additions & 1 deletion packages/webview_flutter/lib/webview_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class JavascriptChannel {
JavascriptChannel({
required this.name,
required this.onMessageReceived,
}) : assert(name != null),
}) : assert(name != null),
assert(onMessageReceived != null),
assert(_validChannelNames.hasMatch(name));

Expand Down Expand Up @@ -231,6 +231,7 @@ class WebView extends StatefulWidget {
this.initialMediaPlaybackPolicy =
AutoMediaPlaybackPolicy.require_user_action_for_all_media_types,
this.allowsInlineMediaPlayback = false,
this.allowFileAccess,
}) : assert(javascriptMode != null),
assert(initialMediaPlaybackPolicy != null),
assert(allowsInlineMediaPlayback != null),
Expand Down Expand Up @@ -415,6 +416,11 @@ class WebView extends StatefulWidget {
/// The default policy is [AutoMediaPlaybackPolicy.require_user_action_for_all_media_types].
final AutoMediaPlaybackPolicy initialMediaPlaybackPolicy;

/// The value if it allows file access.
///
/// This setting only applies to Android
final bool? allowFileAccess;

@override
State<StatefulWidget> createState() => _WebViewState();
}
Expand Down Expand Up @@ -479,6 +485,7 @@ CreationParams _creationParamsfromWidget(WebView widget) {
javascriptChannelNames: _extractChannelNames(widget.javascriptChannels),
userAgent: widget.userAgent,
autoMediaPlaybackPolicy: widget.initialMediaPlaybackPolicy,
allowFileAccess: widget.allowFileAccess,
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/webview_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: webview_flutter
description: A Flutter plugin that provides a WebView widget on Android and iOS.
homepage: https://github.com/flutter/plugins/tree/master/packages/webview_flutter
version: 2.0.4
version: 2.1.0

environment:
sdk: ">=2.12.0-259.9.beta <3.0.0"
Expand Down
17 changes: 17 additions & 0 deletions packages/webview_flutter/test/webview_flutter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,19 @@ void main() {

expect(platformWebView.userAgent, 'UA');
});

testWidgets('Set allowFileAccess', (WidgetTester tester) async {
await tester.pumpWidget(const WebView(
initialUrl: 'https://youtube.com',
javascriptMode: JavascriptMode.unrestricted,
allowFileAccess: true,
));

final FakePlatformWebView platformWebView =
fakePlatformViewsController.lastCreatedView!;

expect(platformWebView.allowFileAccess, true);
});
}

class FakePlatformWebView {
Expand All @@ -935,6 +948,9 @@ class FakePlatformWebView {
javascriptChannelNames =
List<String>.from(params['javascriptChannelNames']);
}
if (params.containsKey('allowFileAccess')) {
allowFileAccess = params['allowFileAccess'];
}
javascriptMode = JavascriptMode.values[params['settings']['jsMode']];
hasNavigationDelegate =
params['settings']['hasNavigationDelegate'] ?? false;
Expand All @@ -959,6 +975,7 @@ class FakePlatformWebView {
bool? hasNavigationDelegate;
bool? debuggingEnabled;
String? userAgent;
bool? allowFileAccess;

Future<dynamic> onMethodCall(MethodCall call) {
switch (call.method) {
Expand Down