Skip to content

Commit

Permalink
feat(Android): Handle RESOURCE_PROTECTED_MEDIA_ID permission (#2732)
Browse files Browse the repository at this point in the history
  • Loading branch information
enzo-macri committed Nov 26, 2022
1 parent 70e84de commit 2711f3a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {

protected RNCWebChromeClient mWebChromeClient = null;
protected boolean mAllowsFullscreenVideo = false;
protected boolean mAllowsProtectedMedia = false;
protected @Nullable String mUserAgent = null;
protected @Nullable String mUserAgentWithApplicationName = null;
protected @Nullable String mDownloadingMessage = null;
Expand Down Expand Up @@ -670,6 +671,20 @@ public void setMinimumFontSize(WebView view, int fontSize) {
view.getSettings().setMinimumFontSize(fontSize);
}

@ReactProp(name = "allowsProtectedMedia")
public void setAllowsProtectedMedia(WebView view, boolean enabled) {
// This variable is used to keep consistency
// in case a new WebChromeClient is created
// (eg. when mAllowsFullScreenVideo changes)
mAllowsProtectedMedia = enabled;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
WebChromeClient client = view.getWebChromeClient();
if (client != null && client instanceof RNCWebChromeClient) {
((RNCWebChromeClient) client).setAllowsProtectedMedia(enabled);
}
}
}

@Override
protected void addEventEmitters(ThemedReactContext reactContext, WebView view) {
// Do not register default touch emitter and let WebView implementation handle touches
Expand Down Expand Up @@ -875,8 +890,6 @@ public void onHideCustomView() {
mReactContext.removeLifecycleEventListener(this);
}
};

webView.setWebChromeClient(mWebChromeClient);
} else {
if (mWebChromeClient != null) {
mWebChromeClient.onHideCustomView();
Expand All @@ -888,9 +901,9 @@ public Bitmap getDefaultVideoPoster() {
return Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
}
};

webView.setWebChromeClient(mWebChromeClient);
}
mWebChromeClient.setAllowsProtectedMedia(mAllowsProtectedMedia);
webView.setWebChromeClient(mWebChromeClient);
}

protected static class RNCWebViewClient extends WebViewClient {
Expand Down Expand Up @@ -1227,6 +1240,9 @@ protected static class RNCWebChromeClient extends WebChromeClient implements Lif

protected RNCWebView.ProgressChangedFilter progressChangedFilter = null;

// True if protected media should be allowed, false otherwise
protected boolean mAllowsProtectedMedia = false;

public RNCWebChromeClient(ReactContext reactContext, WebView webView) {
this.mReactContext = reactContext;
this.mWebView = webView;
Expand Down Expand Up @@ -1288,9 +1304,20 @@ public void onPermissionRequest(final PermissionRequest request) {
} else if (requestedResource.equals(PermissionRequest.RESOURCE_VIDEO_CAPTURE)) {
androidPermission = Manifest.permission.CAMERA;
} else if(requestedResource.equals(PermissionRequest.RESOURCE_PROTECTED_MEDIA_ID)) {
androidPermission = PermissionRequest.RESOURCE_PROTECTED_MEDIA_ID;
if (mAllowsProtectedMedia) {
grantedPermissions.add(requestedResource);
} else {
/**
* Legacy handling (Kept in case it was working under some conditions (given Android version or something))
*
* Try to ask user to grant permission using Activity.requestPermissions
*
* Find more details here: https://github.com/react-native-webview/react-native-webview/pull/2732
*/
androidPermission = PermissionRequest.RESOURCE_PROTECTED_MEDIA_ID;
}
}
// TODO: RESOURCE_MIDI_SYSEX, RESOURCE_PROTECTED_MEDIA_ID.
// TODO: RESOURCE_MIDI_SYSEX.

if (androidPermission != null) {
if (ContextCompat.checkSelfPermission(mReactContext, androidPermission) == PackageManager.PERMISSION_GRANTED) {
Expand Down Expand Up @@ -1480,6 +1507,15 @@ protected ViewGroup getRootView() {
public void setProgressChangedFilter(RNCWebView.ProgressChangedFilter filter) {
progressChangedFilter = filter;
}

/**
* Set whether or not protected media should be allowed
* /!\ Setting this to false won't revoke permission already granted to the current webpage.
* In order to do so, you'd need to reload the page /!\
*/
public void setAllowsProtectedMedia(boolean enabled) {
mAllowsProtectedMedia = enabled;
}
}

/**
Expand Down
10 changes: 10 additions & 0 deletions docs/Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ This document lays out the current public properties and methods for the React N
- [`minimumFontSize`](Reference.md#minimumFontSize)
- [`downloadingMessage`](Reference.md#downloadingMessage)
- [`lackPermissionToDownloadMessage`](Reference.md#lackPermissionToDownloadMessage)
- [`allowsProtectedMedia`](Reference.md#allowsProtectedMedia)

## Methods Index

Expand Down Expand Up @@ -1592,6 +1593,15 @@ This is the message that is shown in the Toast when the webview is unable to dow
| ------ | -------- | -------- |
| string | No | Android |

### `allowsProtectedMedia`

Whether or not the Webview can play media protected by DRM. Default is false.
/!\ Setting this to false won't revoke the permission already granted to the current webpage. In order to do so, you'd have to reload the page as well. /!\

| Type | Required | Platform |
| ------ | -------- | -------- |
| boolean | No | Android |

## Methods

### `goForward()`[](#methods-index)<!-- Link generated with jump2header -->
Expand Down
8 changes: 8 additions & 0 deletions src/WebViewTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ export interface AndroidNativeWebViewProps extends CommonNativeWebViewProps {
minimumFontSize?: number;
downloadingMessage?: string;
lackPermissionToDownloadMessage?: string;
allowsProtectedMedia?: boolean;
}

export declare type ContentInsetAdjustmentBehavior =
Expand Down Expand Up @@ -1119,6 +1120,13 @@ export interface AndroidWebViewProps extends WebViewSharedProps {
* @platform android
*/
lackPermissionToDownloadMessage?: string;

/**
* Boolean value to control whether webview can play media protected by DRM.
* Default is false.
* @platform android
*/
allowsProtectedMedia?: boolean;
}

export interface WebViewSharedProps extends ViewProps {
Expand Down

0 comments on commit 2711f3a

Please sign in to comment.