简体   繁体   中英

How can we disable webRTC on Android webview?

I am developing VPN app and I need to put WebRTC support menu in that. Where user can enable and disable that option. But I didn't disable it in my app. I dont know how to disable it via code.

here is code please check and tell me how can resolve this.

mWebView.setWebChromeClient(new WebChromeClient(){
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void onPermissionRequest(final PermissionRequest request) {
            request.deny();
        }
    });

or

mWebView.setWebChromeClient(null);

Permissions

If you do not want to inject JS I think your solution isn't far off. Just a bit of state management can help this.

You need to have the AndroidManifiest and Android Permission checks at compile time and the first runtime a user uses your app because you want to allow a user to specifically allow/deny this functionality dynamically during the experience. Not constantly fiddle around with Android app settings / permissions. Going to and from settings would be rather tedious. I also presume this UX is what you are trying to avoid.

Thus you should do runtime checking on permissions within WebChromeClients onPermissionRequest (which is basically what you have) for when each time this is fired. You can use a state variable, or a white list of domains stored in the state to allow a user allows to limit this functionality and even lock it to specific domains. You can also check specifically for RESOURCE_VIDEO_CAPTURE, etc.

For an example you can see how I do domain locking in my GitHub example here: https://github.com/marcusbelcher/android-getUserMedia-test see line 106 and 116. You can use a variable/state logic to block specific requests here. This is the request per session when someone fires off getUserMedia.

This is the only place at runtime you can deny/allow. More info is here:

https://github.com/googlesamples/android-PermissionRequest

https://developer.android.com/reference/android/webkit/PermissionRequest

JS Overrides

If you can / want to inject JS you can inject and override getUserMedia inside the DOM via navigator.mediaDevices.getUserMedia = undefined . You could move the reference temporarily window.t = navigator.mediaDevices.getUserMedia; navigator.mediaDevices.getUserMedia = null; window.t = navigator.mediaDevices.getUserMedia; navigator.mediaDevices.getUserMedia = null; and reverse this when you would like.

More info is here:

Android webview, loading javascript file in assets folder

https://medium.com/appunite-edu-collection/webview-with-injected-js-script-13eb1e0257c9

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM