Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add media access apis for mojave
- Loading branch information
1 parent
bf3edf8
commit ad39da6
Showing
7 changed files
with
233 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) 2018 GitHub, Inc. | ||
// Use of this source code is governed by the MIT license that can be | ||
// found in the LICENSE file. | ||
|
||
#import <Cocoa/Cocoa.h> | ||
|
||
#ifndef ATOM_BROWSER_UI_COCOA_ATOM_ACCESS_CONTROLLER_H_ | ||
#define ATOM_BROWSER_UI_COCOA_ATOM_ACCESS_CONTROLLER_H_ | ||
|
||
typedef NS_ENUM(NSInteger, AccessState) { | ||
AccessStateUnknown, | ||
AccessStateGranted, | ||
AccessStateDenied | ||
}; | ||
|
||
@interface AtomAccessController : NSObject { | ||
AccessState microphoneAccessState_; | ||
AccessState cameraAccessState_; | ||
} | ||
|
||
+ (instancetype)sharedController; | ||
|
||
- (void)askForMediaAccess:(BOOL)askAgain | ||
completion:(void (^)(BOOL))allAccessGranted; | ||
|
||
- (void)askForMicrophoneAccess; | ||
- (void)askForCameraAccess; | ||
|
||
- (BOOL)hasMicrophoneAccess; | ||
- (BOOL)hasCameraAccess; | ||
- (BOOL)hasFullMediaAccess; | ||
|
||
@end | ||
|
||
#endif // ATOM_BROWSER_UI_COCOA_ATOM_ACCESS_CONTROLLER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
#include "atom/browser/ui/cocoa/atom_access_controller.h" | ||
|
||
#import <AVFoundation/AVFoundation.h> | ||
#import <Cocoa/Cocoa.h> | ||
|
||
@implementation AtomAccessController | ||
|
||
+ (instancetype)sharedController { | ||
static dispatch_once_t once; | ||
static AtomAccessController* sharedController; | ||
dispatch_once(&once, ^{ | ||
sharedController = [[self alloc] init]; | ||
}); | ||
return sharedController; | ||
} | ||
|
||
- (instancetype)init { | ||
if ((self = [super init])) { | ||
if (@available(macOS 10.14, *)) { | ||
switch ( | ||
[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]) { | ||
case AVAuthorizationStatusAuthorized: | ||
case AVAuthorizationStatusRestricted: | ||
cameraAccessState_ = AccessStateDenied; | ||
break; | ||
case AVAuthorizationStatusDenied: | ||
cameraAccessState_ = AccessStateDenied; | ||
break; | ||
case AVAuthorizationStatusNotDetermined: | ||
cameraAccessState_ = AccessStateDenied; | ||
} | ||
switch ( | ||
[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio]) { | ||
case AVAuthorizationStatusAuthorized: | ||
case AVAuthorizationStatusRestricted: | ||
microphoneAccessState_ = AccessStateDenied; | ||
break; | ||
case AVAuthorizationStatusDenied: | ||
microphoneAccessState_ = AccessStateDenied; | ||
break; | ||
case AVAuthorizationStatusNotDetermined: | ||
microphoneAccessState_ = AccessStateDenied; | ||
} | ||
[[[NSWorkspace sharedWorkspace] notificationCenter] | ||
addObserver:self | ||
selector:@selector(applicationLaunched:) | ||
name:NSWorkspaceDidLaunchApplicationNotification | ||
object:nil]; | ||
} else { | ||
cameraAccessState_ = AccessStateGranted; | ||
microphoneAccessState_ = AccessStateDenied; | ||
} | ||
} | ||
return self; | ||
} | ||
|
||
- (void)askForMicrophoneAccess { | ||
if (microphoneAccessState_ == AccessStateDenied) { | ||
NSAlert* alert = [[NSAlert alloc] init]; | ||
alert.alertStyle = NSAlertStyleWarning; | ||
alert.messageText = @"This app needs access to the microphone."; | ||
[alert addButtonWithTitle:@"Change Preferences"]; | ||
[alert addButtonWithTitle:@"Cancel"]; | ||
NSInteger modalResponse = [alert runModal]; | ||
if (modalResponse == NSAlertFirstButtonReturn) { | ||
[[NSWorkspace sharedWorkspace] | ||
openURL:[NSURL | ||
URLWithString:@"x-apple.systempreferences:com.apple." | ||
@"preference.security?Privacy_Microphone"]]; | ||
} | ||
} | ||
} | ||
|
||
- (void)askForCameraAccess { | ||
if (cameraAccessState_ == AccessStateDenied) { | ||
NSAlert* alert = [[NSAlert alloc] init]; | ||
alert.alertStyle = NSAlertStyleWarning; | ||
alert.messageText = @"This app needs access to the camera."; | ||
[alert addButtonWithTitle:@"Change Preferences"]; | ||
[alert addButtonWithTitle:@"Cancel"]; | ||
NSInteger modalResponse = [alert runModal]; | ||
if (modalResponse == NSAlertFirstButtonReturn) { | ||
[[NSWorkspace sharedWorkspace] | ||
openURL:[NSURL URLWithString:@"x-apple.systempreferences:com.apple." | ||
@"preference.security?Privacy_Camera"]]; | ||
} | ||
} | ||
} | ||
|
||
- (void)askForMediaAccess:(BOOL)askAgain | ||
completion:(void (^)(BOOL))allAccessGranted { | ||
if (@available(macOS 10.14, *)) { | ||
[AVCaptureDevice | ||
requestAccessForMediaType:AVMediaTypeAudio | ||
completionHandler:^(BOOL granted) { | ||
microphoneAccessState_ = | ||
(granted) ? AccessStateGranted : AccessStateDenied; | ||
[AVCaptureDevice | ||
requestAccessForMediaType:AVMediaTypeVideo | ||
completionHandler:^(BOOL granted) { | ||
cameraAccessState_ = (granted) | ||
? AccessStateGranted | ||
: AccessStateDenied; | ||
if (askAgain) { | ||
dispatch_async(dispatch_get_main_queue(), ^{ | ||
[self askForMicrophoneAccess]; | ||
[self askForCameraAccess]; | ||
}); | ||
} | ||
dispatch_async(dispatch_get_main_queue(), ^{ | ||
allAccessGranted(self.hasFullMediaAccess); | ||
}); | ||
}]; | ||
}]; | ||
} else { | ||
allAccessGranted(self.hasFullMediaAccess); | ||
} | ||
} | ||
|
||
- (BOOL)mediaConsentStatusKnown { | ||
return ((microphoneAccessState_ != AccessStateUnknown) && | ||
(cameraAccessState_ != AccessStateUnknown)); | ||
} | ||
|
||
- (BOOL)hasCameraAccess { | ||
if (@available(macOS 10.14, *)) { | ||
return (cameraAccessState_ == AccessStateGranted); | ||
} | ||
return YES; | ||
} | ||
|
||
- (BOOL)hasMicrophoneAccess { | ||
if (@available(macOS 10.14, *)) { | ||
return (microphoneAccessState_ == AccessStateGranted); | ||
} | ||
return YES; | ||
} | ||
|
||
- (BOOL)hasFullMediaAccess { | ||
return (self.hasCameraAccess && self.hasMicrophoneAccess); | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters