aboutsummaryrefslogtreecommitdiffstats
path: root/src/electron/macOSPermissions.js
blob: 940b16c6ebbe12b5149da53c08b773011b35e174 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { app, systemPreferences, dialog } from 'electron';
import fs from 'fs';
import macosVersion from 'macos-version';
import path from 'path';
import { isMac } from '../environment';

let askForScreenCaptureAccess;
if (isMac) {
  // eslint-disable-next-line global-require
  askForScreenCaptureAccess = require('node-mac-permissions').askForScreenCaptureAccess;
}

const debug = require('debug')('Ferdi:macOSPermissions');

const permissionExists = macosVersion.isGreaterThanOrEqualTo('10.15');
const filePath = path.join(app.getPath('userData'), '.has-app-requested-screen-capture-permissions');

function hasPromptedForPermission() {
  if (!permissionExists) {
    return false;
  }

  if (filePath && fs.existsSync(filePath)) {
    return true;
  }

  return false;
}

function hasScreenCapturePermission() {
  if (!permissionExists) {
    return true;
  }

  const screenCaptureStatus = systemPreferences.getMediaAccessStatus('screen');
  return screenCaptureStatus === 'granted';
}

function createStatusFile() {
  try {
    fs.writeFileSync(filePath, '');
  } catch (error) {
    if (error.code === 'ENOENT') {
      fs.mkdirSync(path.dirname(filePath));
      fs.writeFileSync(filePath, '');
    }

    throw error;
  }
}

export default async function (mainWindow) {
  debug('Checking camera & microphone permissions');
  systemPreferences.askForMediaAccess('camera');
  systemPreferences.askForMediaAccess('microphone');

  if (!hasPromptedForPermission() && !hasScreenCapturePermission()) {
    debug('Checking screen capture permissions');

    const { response } = await dialog.showMessageBox(mainWindow, {
      type: 'info',
      message: 'Enable Screen Sharing',
      detail: 'To enable screen sharing for some services, Ferdi needs the permission to record your screen.',
      buttons: [
        'Allow screen sharing',
        'No',
        'Ask me later',
      ],
      defaultId: 0,
      cancelId: 2,
    });

    console.log('result', response);
    if (response === 0) {
      debug('Asking for access');
      askForScreenCaptureAccess();
      createStatusFile();
    } else if (response === 1) {
      debug('Don\'t ask again');
      createStatusFile();
    }
  }
}