Commit b0224c74 authored by Lorenzo Pichilli's avatar Lorenzo Pichilli

Added onPermissionRequest event (available only on Android)

parent 992bf464
This diff is collapsed.
## 2.0.1
- Added `onPermissionRequest` event. This event is fired when the webview is requesting permission to access the specified resources and the permission currently isn't granted or denied (available only on Android).
## 2.0.0
- Merge "Avoid null pointer exception after webview is disposed" [#116](https://github.com/pichillilorenzo/flutter_inappbrowser/pull/116) (thanks to [robsonfingo](https://github.com/robsonfingo))
......
......@@ -447,6 +447,7 @@ Instead, on the `onLoadStop` WebView event, you can use `callHandler` directly:
* `onAjaxProgress`: Event fired as an `XMLHttpRequest` progress.
* `shouldInterceptFetchRequest`: Event fired when a request is sent to a server through [Fetch API](https://developer.mozilla.org/it/docs/Web/API/Fetch_API).
* `onNavigationStateChange`: Event fired when the navigation state of the InAppWebView changes.
* `onPermissionRequest`: Event fired when the webview is requesting permission to access the specified resources and the permission currently isn't granted or denied (available only on Android).
### `InAppBrowser` class
......
......@@ -16,6 +16,7 @@ import android.webkit.ConsoleMessage;
import android.webkit.GeolocationPermissions;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.PermissionRequest;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
......@@ -31,7 +32,10 @@ import com.pichillilorenzo.flutter_inappbrowser.InAppBrowserActivity;
import com.pichillilorenzo.flutter_inappbrowser.InAppBrowserFlutterPlugin;
import com.pichillilorenzo.flutter_inappbrowser.R;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.flutter.plugin.common.MethodChannel;
......@@ -573,6 +577,53 @@ public class InAppWebViewChromeClient extends WebChromeClient implements PluginR
return true;
}
@Override
public void onPermissionRequest(final PermissionRequest request) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Map<String, Object> obj = new HashMap<>();
if (inAppBrowserActivity != null)
obj.put("uuid", inAppBrowserActivity.uuid);
obj.put("origin", request.getOrigin().toString());
obj.put("resources", Arrays.asList(request.getResources()));
getChannel().invokeMethod("onPermissionRequest", obj, new MethodChannel.Result() {
@Override
public void success(Object response) {
if (response != null) {
Map<String, Object> responseMap = (Map<String, Object>) response;
Integer action = (Integer) responseMap.get("action");
List<String> resourceList = (List<String>) responseMap.get("resources");
if (resourceList == null)
resourceList = new ArrayList<String>();
String[] resources = new String[resourceList.size()];
resources = resourceList.toArray(resources);
if (action != null) {
switch (action) {
case 1:
request.grant(resources);
return;
case 0:
default:
request.deny();
return;
}
}
}
request.deny();
}
@Override
public void error(String s, String s1, Object o) {
Log.e(LOG_TAG, s + ", " + s1);
}
@Override
public void notImplemented() {
request.deny();
}
});
}
}
private MethodChannel getChannel() {
return (inAppBrowserActivity != null) ? InAppBrowserFlutterPlugin.inAppBrowser.channel : flutterWebView.channel;
}
......
......@@ -15,6 +15,12 @@
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.VIDEO_CAPTURE" />
<uses-permission android:name="android.permission.AUDIO_CAPTURE" />
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method.
In most cases you can leave this as-is, but you if you want to provide
......
......@@ -214,6 +214,15 @@ class InAppWebView extends StatefulWidget {
///[url] represents the new url.
final void Function(InAppWebViewController controller, String url) onNavigationStateChange;
///Event fired when the webview is requesting permission to access the specified resources and the permission currently isn't granted or denied.
///
///[origin] represents the origin of the web page which is trying to access the restricted resources.
///
///[resources] represents the array of resources the web content wants to access.
///
///**NOTE**: available only on Android 23+.
final Future<PermissionRequestResponse> Function(InAppWebViewController controller, String origin, List<String> resources) onPermissionRequest;
///Initial url that will be loaded.
final String initialUrl;
///Initial asset file that will be loaded. See [InAppWebView.loadFile()] for explanation.
......@@ -267,6 +276,7 @@ class InAppWebView extends StatefulWidget {
this.onAjaxProgress,
this.shouldInterceptFetchRequest,
this.onNavigationStateChange,
this.onPermissionRequest,
this.gestureRecognizers,
}) : super(key: key);
......@@ -581,6 +591,14 @@ class InAppWebViewController {
else if (_inAppBrowser != null)
_inAppBrowser.onNavigationStateChange(url);
break;
case "onPermissionRequest":
String origin = call.arguments["origin"];
List<String> resources = call.arguments["resources"].cast<String>();
if (_widget != null && _widget.onPermissionRequest != null)
return (await _widget.onPermissionRequest(this, origin, resources))?.toMap();
/*else if (_inAppBrowser != null)
return (await _inAppBrowser.onPermissionRequest(origin, resources))?.toMap();*/
break;
case "onCallJsHandler":
String handlerName = call.arguments["handlerName"];
// decode args to json
......
......@@ -145,9 +145,9 @@ class CustomSchemeResponse {
Map<String, dynamic> toJson() {
return {
'content-type': this.contentType,
'content-encoding': this.contentEnconding,
'data': this.data
'content-type': contentType,
'content-encoding': contentEnconding,
'data': data
};
}
}
......@@ -1587,3 +1587,37 @@ class Cookie {
Cookie({@required this.name, @required this.value});
}
///PermissionRequestResponseAction class used by [PermissionRequestResponse] class.
class PermissionRequestResponseAction {
final int _value;
const PermissionRequestResponseAction._internal(this._value);
int toValue() => _value;
///Denies the request.
static const DENY = const PermissionRequestResponseAction._internal(0);
///Grants origin the permission to access the given resources.
static const GRANT = const PermissionRequestResponseAction._internal(1);
bool operator ==(value) => value == _value;
@override
int get hashCode => _value.hashCode;
}
///PermissionRequestResponse class represents the response used by the [onPermissionRequest] event.
class PermissionRequestResponse {
///Resources granted to be accessed by origin.
List<String> resources;
///Indicate the [PermissionRequestResponseAction] to take in response of a permission request.
PermissionRequestResponseAction action;
PermissionRequestResponse({this.resources = const [], this.action = PermissionRequestResponseAction.DENY});
Map<String, dynamic> toMap() {
return {
"resources": resources,
"action": action?.toValue()
};
}
}
\ No newline at end of file
name: flutter_inappbrowser
description: A Flutter plugin that allows you to add an inline webview or open an in-app browser window.
version: 2.0.0
version: 2.0.1
author: Lorenzo Pichilli <pichillilorenzo@gmail.com>
homepage: https://github.com/pichillilorenzo/flutter_inappbrowser
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment