Skip to content

Commit

Permalink
feat: add 'screen' to systemPreferences.getMediaAccessStatus() (#20764)
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak committed Nov 13, 2019
1 parent 8d67f16 commit aed7d38
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 90 deletions.
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 - Can be `microphone`, `camera` or `screen`.

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
32 changes: 18 additions & 14 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 "native_mate/object_template_builder_deprecated.h"
#include "net/base/mac/url_conversions.h"
#include "shell/browser/mac/atom_application.h"
Expand Down Expand Up @@ -102,15 +103,17 @@ AVMediaType ParseMediaType(const std::string& media_type) {
}
}

std::string ConvertAuthorizationStatus(AVAuthorizationStatusMac status) {
switch (status) {
case AVAuthorizationStatusNotDeterminedMac:
std::string ConvertSystemPermission(
system_media_permissions::SystemPermission value) {
using SystemPermission = system_media_permissions::SystemPermission;
switch (value) {
case SystemPermission::kNotDetermined:
return "not-determined";
case AVAuthorizationStatusRestrictedMac:
case SystemPermission::kRestricted:
return "restricted";
case AVAuthorizationStatusDeniedMac:
case SystemPermission::kDenied:
return "denied";
case AVAuthorizationStatusAuthorizedMac:
case SystemPermission::kAllowed:
return "granted";
default:
return "unknown";
Expand Down Expand Up @@ -597,14 +600,15 @@ AVMediaType ParseMediaType(const std::string& media_type) {
std::string SystemPreferences::GetMediaAccessStatus(
const std::string& media_type,
mate::Arguments* args) {
if (auto type = ParseMediaType(media_type)) {
if (@available(macOS 10.14, *)) {
return ConvertAuthorizationStatus(
[AVCaptureDevice authorizationStatusForMediaType:type]);
} else {
// access always allowed pre-10.14 Mojave
return ConvertAuthorizationStatus(AVAuthorizationStatusAuthorizedMac);
}
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
74 changes: 0 additions & 74 deletions shell/browser/mac/atom_application.h
Expand Up @@ -9,80 +9,6 @@
#import <AVFoundation/AVFoundation.h>
#import <LocalAuthentication/LocalAuthentication.h>

// Forward Declare Appearance APIs
@interface NSApplication (MojaveSDK)
@property(copy, readonly)
NSAppearance* effectiveAppearance API_AVAILABLE(macosx(10.14));
@property(copy, readonly) NSAppearance* appearance API_AVAILABLE(macosx(10.14));
- (void)setAppearance:(NSAppearance*)appearance API_AVAILABLE(macosx(10.14));
@end

// forward declare Access APIs
typedef NSString* AVMediaType NS_EXTENSIBLE_STRING_ENUM;

AVF_EXPORT AVMediaType const AVMediaTypeVideo;
AVF_EXPORT AVMediaType const AVMediaTypeAudio;

typedef NS_ENUM(NSInteger, AVAuthorizationStatusMac) {
AVAuthorizationStatusNotDeterminedMac = 0,
AVAuthorizationStatusRestrictedMac = 1,
AVAuthorizationStatusDeniedMac = 2,
AVAuthorizationStatusAuthorizedMac = 3,
};

@interface AVCaptureDevice (MojaveSDK)
+ (void)requestAccessForMediaType:(AVMediaType)mediaType
completionHandler:(void (^)(BOOL granted))handler
API_AVAILABLE(macosx(10.14));
+ (AVAuthorizationStatusMac)authorizationStatusForMediaType:
(AVMediaType)mediaType API_AVAILABLE(macosx(10.14));
@end

@interface NSColor (MojaveSDK)
@property(class, strong, readonly)
NSColor* controlAccentColor API_AVAILABLE(macosx(10.14));

// macOS system colors
@property(class, strong, readonly)
NSColor* systemBlueColor API_AVAILABLE(macosx(10.10));
@property(class, strong, readonly)
NSColor* systemBrownColor API_AVAILABLE(macosx(10.10));
@property(class, strong, readonly)
NSColor* systemGrayColor API_AVAILABLE(macosx(10.10));
@property(class, strong, readonly)
NSColor* systemGreenColor API_AVAILABLE(macosx(10.10));
@property(class, strong, readonly)
NSColor* systemOrangeColor API_AVAILABLE(macosx(10.10));
@property(class, strong, readonly)
NSColor* systemPinkColor API_AVAILABLE(macosx(10.10));
@property(class, strong, readonly)
NSColor* systemPurpleColor API_AVAILABLE(macosx(10.10));
@property(class, strong, readonly)
NSColor* systemRedColor API_AVAILABLE(macosx(10.10));
@property(class, strong, readonly)
NSColor* systemYellowColor API_AVAILABLE(macosx(10.10));

// misc dynamic colors declarations
@property(class, strong, readonly)
NSColor* linkColor API_AVAILABLE(macosx(10.10));
@property(class, strong, readonly)
NSColor* placeholderTextColor API_AVAILABLE(macosx(10.10));
@property(class, strong, readonly)
NSColor* findHighlightColor API_AVAILABLE(macosx(10.13));
@property(class, strong, readonly)
NSColor* separatorColor API_AVAILABLE(macosx(10.14));
@property(class, strong, readonly)
NSColor* selectedContentBackgroundColor API_AVAILABLE(macosx(10.14));
@property(class, strong, readonly)
NSColor* unemphasizedSelectedContentBackgroundColor API_AVAILABLE(
macosx(10.14));
@property(class, strong, readonly)
NSColor* unemphasizedSelectedTextBackgroundColor API_AVAILABLE(macosx(10.14)
);
@property(class, strong, readonly)
NSColor* unemphasizedSelectedTextColor API_AVAILABLE(macosx(10.14));
@end

@interface AtomApplication : NSApplication <CrAppProtocol,
CrAppControlProtocol,
NSUserActivityDelegate> {
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

0 comments on commit aed7d38

Please sign in to comment.