Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add 'screen' to systemPreferences.getMediaAccessStatus() #20764

Merged
merged 2 commits into from Nov 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions chromium_src/BUILD.gn
Expand Up @@ -32,6 +32,8 @@ static_library("chrome") {
"//chrome/browser/icon_loader_win.cc",
"//chrome/browser/icon_manager.cc",
"//chrome/browser/icon_manager.h",
"//chrome/browser/media/webrtc/system_media_capture_permissions_mac.h",
"//chrome/browser/media/webrtc/system_media_capture_permissions_mac.mm",
"//chrome/browser/net/chrome_mojo_proxy_resolver_factory.cc",
"//chrome/browser/net/chrome_mojo_proxy_resolver_factory.h",
"//chrome/browser/net/proxy_config_monitor.cc",
Expand Down
4 changes: 4 additions & 0 deletions docs/api/desktop-capturer.md
Expand Up @@ -91,7 +91,11 @@ The `desktopCapturer` module has the following methods:

Returns `Promise<DesktopCapturerSource[]>` - Resolves with an array of [`DesktopCapturerSource`](structures/desktop-capturer-source.md) objects, each `DesktopCapturerSource` represents a screen or an individual window that can be captured.

**Note** Capturing the screen contents requires user consent on macOS 10.15 Catalina or higher,
which can detected by [`systemPreferences.getMediaAccessStatus`].

[`navigator.mediaDevices.getUserMedia`]: https://developer.mozilla.org/en/docs/Web/API/MediaDevices/getUserMedia
[`systemPreferences.getMediaAccessStatus`]: system-preferences.md#systempreferencesgetmediaaccessstatusmediatype-macos

## Caveats

Expand Down
6 changes: 4 additions & 2 deletions docs/api/system-preferences.md
Expand Up @@ -434,11 +434,13 @@ Returns `Boolean` - `true` if the current process is a trusted accessibility cli

### `systemPreferences.getMediaAccessStatus(mediaType)` _macOS_

* `mediaType` String - `microphone` or `camera`.
* `mediaType` String - `microphone`, `camera` or `screen`.
MarshallOfSound marked this conversation as resolved.
Show resolved Hide resolved

Returns `String` - Can be `not-determined`, `granted`, `denied`, `restricted` or `unknown`.

This user consent was not required until macOS 10.14 Mojave, so this method will always return `granted` if your system is running 10.13 High Sierra or lower.
This user consent was not required on macOS 10.13 High Sierra or lower so this method will always return `granted`.
macOS 10.14 Mojave or higher requires consent for `microphone` and `camera` access.
macOS 10.15 Catalina or higher requires consent for `screen` access.

### `systemPreferences.askForMediaAccess(mediaType)` _macOS_

Expand Down
45 changes: 27 additions & 18 deletions shell/browser/api/atom_api_system_preferences_mac.mm
Expand Up @@ -21,6 +21,7 @@
#include "base/strings/sys_string_conversions.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "base/values.h"
#include "chrome/browser/media/webrtc/system_media_capture_permissions_mac.h"
#include "net/base/mac/url_conversions.h"
#include "shell/browser/mac/atom_application.h"
#include "shell/browser/mac/dict_util.h"
Expand Down Expand Up @@ -103,6 +104,23 @@ AVMediaType ParseMediaType(const std::string& media_type) {
}
}

std::string ConvertSystemPermission(
system_media_permissions::SystemPermission value) {
using SystemPermission = system_media_permissions::SystemPermission;
switch (value) {
case SystemPermission::kNotDetermined:
return "not-determined";
case SystemPermission::kRestricted:
return "restricted";
case SystemPermission::kDenied:
return "denied";
case SystemPermission::kAllowed:
return "granted";
default:
return "unknown";
}
}

} // namespace

void SystemPreferences::PostNotification(const std::string& name,
Expand Down Expand Up @@ -574,24 +592,15 @@ AVMediaType ParseMediaType(const std::string& media_type) {
std::string SystemPreferences::GetMediaAccessStatus(
const std::string& media_type,
gin_helper::Arguments* args) {
if (auto type = ParseMediaType(media_type)) {
if (@available(macOS 10.14, *)) {
switch ([AVCaptureDevice authorizationStatusForMediaType:type]) {
case AVAuthorizationStatusNotDetermined:
return "not-determined";
case AVAuthorizationStatusRestricted:
return "restricted";
case AVAuthorizationStatusDenied:
return "denied";
case AVAuthorizationStatusAuthorized:
return "granted";
default:
return "unknown";
}
} else {
// access always allowed pre-10.14 Mojave
return "granted";
}
if (media_type == "camera") {
return ConvertSystemPermission(
system_media_permissions::CheckSystemVideoCapturePermission());
} else if (media_type == "microphone") {
return ConvertSystemPermission(
system_media_permissions::CheckSystemAudioCapturePermission());
} else if (media_type == "screen") {
return ConvertSystemPermission(
system_media_permissions::CheckSystemScreenCapturePermission());
} else {
args->ThrowError("Invalid media type");
return std::string();
Expand Down
5 changes: 5 additions & 0 deletions spec-main/api-system-preferences-spec.ts
Expand Up @@ -246,6 +246,11 @@ describe('systemPreferences module', () => {
const microphoneStatus = systemPreferences.getMediaAccessStatus('microphone')
expect(statuses).to.include(microphoneStatus)
})

it('returns an access status for a screen access request', () => {
const screenStatus = systemPreferences.getMediaAccessStatus('screen')
expect(statuses).to.include(screenStatus)
})
})

describe('systemPreferences.getAnimationSettings()', () => {
Expand Down