This repository has been archived by the owner on Nov 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathpermissions.request.ts
399 lines (348 loc) · 14.3 KB
/
permissions.request.ts
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// Copyright 2016 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
export type PermissionName =
"geolocation" |
"notifications" |
"push" |
"midi" |
"camera" |
"microphone" |
"speaker" |
"device-info" |
"background-sync" |
"bluetooth" |
"persistent-storage";
// Descriptors:
export interface PermissionDescriptor {
name: PermissionName;
}
export interface GeolocationPermissionDescriptor extends PermissionDescriptor {
name: "geolocation";
enableHighAccuracy?: boolean;
timeout?: number;
maximumAge?: number;
}
interface NotificationsPermissionDescriptor extends PermissionDescriptor {
name: "notifications";
}
export interface PushPermissionDescriptor extends PermissionDescriptor {
name: "push";
serviceWorker: ServiceWorkerRegistration;
userVisibleOnly?: boolean;
applicationServerKey?: ArrayBuffer | ArrayBufferView;
}
export interface MidiPermissionDescriptor extends PermissionDescriptor {
name: "midi";
sysex: boolean;
software: boolean;
}
type MediaTrackConstraints = any;
export interface CameraPermissionDescriptor extends PermissionDescriptor {
name: "camera";
constraints?: MediaTrackConstraints;
peerIdentity?: string;
}
export interface MicrophonePermissionDescriptor extends PermissionDescriptor {
name: "microphone";
constraints?: MediaTrackConstraints;
peerIdentity?: string;
}
interface SpeakerPermissionDescriptor extends PermissionDescriptor {
name: "speaker";
}
interface DeviceInfoPermissionDescriptor extends PermissionDescriptor {
name: "device-info";
}
export interface BackgroundSyncPermissionDescriptor extends PermissionDescriptor {
name: "background-sync";
serviceWorker: ServiceWorkerRegistration;
tag: string;
}
export interface BluetoothPermissionDescriptor extends PermissionDescriptor {
name: "bluetooth";
deviceId?: string;
// These match RequestDeviceOptions.
filters?: Array<any>;
optionalServices?: Array<any>;
}
interface PersistentStoragePermissionDescriptor extends PermissionDescriptor {
name: "persistent-storage";
}
export type PermissionState = "granted" | "denied" | "prompt";
// Query and Request results:
export interface PermissionStatus {
state: PermissionState;
}
export interface GeolocationPermissionResult extends PermissionStatus {
// For stream=false, from Position:
coords?: Coordinates;
timestamp?: number;
}
interface NotificationsPermissionResult extends PermissionStatus {
}
export interface PushPermissionResult extends PermissionStatus {
subscription: PushSubscription;
}
type MIDIAccess = any;
export interface MidiPermissionResult extends PermissionStatus {
access: MIDIAccess;
}
type MediaStream = any;
export interface CameraPermissionResult extends PermissionStatus {
stream?: MediaStream;
}
export interface MicrophonePermissionResult extends PermissionStatus {
stream?: MediaStream;
}
interface SpeakerPermissionResult extends PermissionStatus {
}
interface DeviceInfoPermissionResult extends PermissionStatus {
}
interface BackgroundSyncPermissionResult extends PermissionStatus {
}
type BluetoothDevice = any;
export interface BluetoothPermissionResult extends PermissionStatus {
devices: Array<BluetoothDevice>;
}
interface PersistentStoragePermissionResult extends PermissionStatus {
}
export interface Permissions {
query(permissionDesc: PermissionDescriptor): Promise<PermissionStatus>;
request(permissionDesc: GeolocationPermissionDescriptor): Promise<PermissionStatus>;
request(permissionDesc: NotificationsPermissionDescriptor): Promise<PermissionStatus>;
request(permissionDesc: PushPermissionDescriptor): Promise<PermissionStatus>;
request(permissionDesc: MidiPermissionDescriptor): Promise<PermissionStatus>;
request(permissionDesc: CameraPermissionDescriptor): Promise<PermissionStatus>;
request(permissionDesc: MicrophonePermissionDescriptor): Promise<PermissionStatus>;
request(permissionDesc: SpeakerPermissionDescriptor): Promise<PermissionStatus>;
request(permissionDesc: DeviceInfoPermissionDescriptor): Promise<PermissionStatus>;
request(permissionDesc: BackgroundSyncPermissionDescriptor): Promise<PermissionStatus>;
request(permissionDesc: BluetoothPermissionDescriptor): Promise<BluetoothPermissionResult>;
request(permissionDesc: PersistentStoragePermissionDescriptor): Promise<PermissionStatus>;
}
declare global {
interface Navigator {
permissions: Permissions;
// Incomplete members from other specs:
bluetooth: any;
mediaDevices: {
getUserMedia(opts: any,
success: (stream: MediaStream)=>any,
failure: (err: DOMException)=>any): void;
}
requestMIDIAccess(options?: any): Promise<MIDIAccess>;
storage: { persist(): Promise<boolean>; };
}
interface PushManager {
subscribe(options?: any): Promise<PushSubscription>;
}
interface ServiceWorkerRegistration {
sync: any;
}
var ServiceWorkerRegistration: {
prototype: ServiceWorkerRegistration;
new(): ServiceWorkerRegistration;
}
}
declare var Notification: {
requestPermission(): Promise<"default"|"denied"|"granted">;
}
function tryQuery(permissionDesc: PermissionDescriptor, dflt: PermissionState
): Promise<PermissionStatus> {
if (navigator.permissions && navigator.permissions.query) {
return navigator.permissions.query(permissionDesc);
} else {
return Promise.resolve({state: dflt});
}
}
function requestGeolocation(permissionDesc: GeolocationPermissionDescriptor): Promise<GeolocationPermissionResult> {
return new Promise((resolve, reject) => {
let {enableHighAccuracy, timeout, maximumAge} =
permissionDesc;
navigator.geolocation.getCurrentPosition(
({coords, timestamp}) => {
resolve(tryQuery(permissionDesc, "granted")
.then(({state}) => {state, coords, timestamp}));
}, ({code, message}) => {
resolve(tryQuery(permissionDesc, "prompt"));
},
{enableHighAccuracy, timeout, maximumAge})
});
}
function requestNotifications(permissionDesc: NotificationsPermissionDescriptor): Promise<NotificationsPermissionResult> {
return Notification.requestPermission().then(state => {
switch(state) {
case "default": return {state: "prompt"};
case "granted": return {state: "granted"};
case "denied": return {state: "denied"};
}
throw new TypeError(state);
});
}
function requestPush(permissionDesc: PushPermissionDescriptor): Promise<PushPermissionResult> {
return new Promise((resolve, reject) => {
let {serviceWorker, userVisibleOnly, applicationServerKey} =
<PushPermissionDescriptor>permissionDesc;
if (!(serviceWorker instanceof ServiceWorkerRegistration)) {
throw new TypeError();
}
resolve(
serviceWorker.pushManager.subscribe(
{userVisibleOnly, applicationServerKey})
.then(subscription => ({state: "granted", subscription}),
err => {
if (err.name === "NotAllowedError") {
return {state: "denied"};
}
// Re-throw SecurityError, InvalidStateError,
// and AbortError.
throw err;
})
);
});
}
function requestMidi(permissionDesc: MidiPermissionDescriptor): Promise<MidiPermissionResult> {
let {sysex, software} = permissionDesc;
return navigator.requestMIDIAccess({sysex, software})
.then(access => ({state: "granted", access}),
err => {
if (err.name === "SecurityError") {
return {state: "denied"};
}
throw err;
})
}
function requestCamera(permissionDesc: CameraPermissionDescriptor): Promise<CameraPermissionResult> {
return new Promise((resolve, reject) => {
let {constraints, peerIdentity} = permissionDesc;
navigator.mediaDevices.getUserMedia(
{video: constraints ? constraints: true,
peerIdentity},
stream => resolve({state: "granted", stream}),
err => {
if (err.name === "PermissionDeniedError") {
resolve({state: "denied"});
}
reject(err);
})
});
}
function requestMicrophone(permissionDesc: MicrophonePermissionDescriptor): Promise<MicrophonePermissionResult> {
return new Promise((resolve, reject) => {
let {constraints, peerIdentity} = permissionDesc;
navigator.mediaDevices.getUserMedia(
{audio: constraints ? constraints: true,
peerIdentity},
stream => resolve({state: "granted", stream}),
err => {
if (err.name === "PermissionDeniedError") {
resolve({state: "denied"});
}
reject(err);
})
});
}
function requestSpeaker(permissionDesc: SpeakerPermissionDescriptor): Promise<SpeakerPermissionResult> {
return Promise.reject(new TypeError("speaker can't be requested (yet?)"));
}
function requestDeviceInfo(permissionDesc: DeviceInfoPermissionDescriptor): Promise<DeviceInfoPermissionResult> {
return Promise.reject(new TypeError("device-info can't be requested (yet?)"));
}
function requestBackgroundSync(permissionDesc: BackgroundSyncPermissionDescriptor): Promise<BackgroundSyncPermissionResult> {
try {
let {serviceWorker, tag} = permissionDesc;
return serviceWorker.sync.register(tag).then(
() => ({state: "granted"}),
(err: DOMException) => {
if (err.name === "NotAllowedError") {
return {state: "denied"};
}
throw err;
});
} catch(e) {
return Promise.reject(e);
}
}
function requestBluetooth(permissionDesc: BluetoothPermissionDescriptor): Promise<BluetoothPermissionResult> {
if (navigator.bluetooth && navigator.bluetooth.requestDevice) {
return (navigator.bluetooth.requestDevice(permissionDesc) as Promise<BluetoothDevice>)
.then<BluetoothPermissionResult>(
device => ({state: "prompt", devices: [device]}),
(err: DOMException) => {
if (err.name === "NotFoundError") {
return {state: "prompt", devices: []};
}
throw err;
});
}
return Promise.reject(new TypeError("navigator.bluetooth not supported"));
}
function requestPersistentStorage(permissionDesc: PersistentStoragePermissionDescriptor): Promise<PersistentStoragePermissionResult> {
return navigator.storage.persist().then(persisted => {
if (persisted) {
return {state: "granted"};
} else {
return {state: "denied"};
}
});
}
export function request(permissionDesc: GeolocationPermissionDescriptor): Promise<PermissionStatus>;
export function request(permissionDesc: NotificationsPermissionDescriptor): Promise<PermissionStatus>;
export function request(permissionDesc: PushPermissionDescriptor): Promise<PermissionStatus>;
export function request(permissionDesc: MidiPermissionDescriptor): Promise<PermissionStatus>;
export function request(permissionDesc: CameraPermissionDescriptor): Promise<PermissionStatus>;
export function request(permissionDesc: MicrophonePermissionDescriptor): Promise<PermissionStatus>;
export function request(permissionDesc: SpeakerPermissionDescriptor): Promise<PermissionStatus>;
export function request(permissionDesc: DeviceInfoPermissionDescriptor): Promise<PermissionStatus>;
export function request(permissionDesc: BackgroundSyncPermissionDescriptor): Promise<PermissionStatus>;
export function request(permissionDesc: BluetoothPermissionDescriptor): Promise<BluetoothPermissionResult>;
export function request(permissionDesc: PersistentStoragePermissionDescriptor): Promise<PermissionStatus>;
export function request(permissionDesc: PermissionDescriptor): Promise<PermissionStatus> {
switch (permissionDesc.name) {
case "geolocation":
return requestGeolocation(<GeolocationPermissionDescriptor>permissionDesc);
case "notifications":
return requestNotifications(<NotificationsPermissionDescriptor>permissionDesc);
case "push":
return requestPush(<PushPermissionDescriptor>permissionDesc);
case "midi":
return requestMidi(<MidiPermissionDescriptor>permissionDesc);
case "camera":
return requestCamera(<CameraPermissionDescriptor>permissionDesc);
case "microphone":
return requestMicrophone(<MicrophonePermissionDescriptor>permissionDesc);
case "speaker":
return requestSpeaker(<SpeakerPermissionDescriptor>permissionDesc);
case "device-info":
return requestDeviceInfo(<DeviceInfoPermissionDescriptor>permissionDesc);
case "background-sync":
return requestBackgroundSync(<BackgroundSyncPermissionDescriptor>permissionDesc);
case "bluetooth":
return requestBluetooth(<BluetoothPermissionDescriptor>permissionDesc);
case "persistent-storage":
return requestPersistentStorage(<PersistentStoragePermissionDescriptor>permissionDesc);
}
// Issue: Permissions doesn't specify what to do for an unrecognized
// PermissionName.
return Promise.reject(new TypeError(
"Unknown PermissionName: " + permissionDesc.name));
};
export function polyfill() {
if (!self.navigator.permissions) {
self.navigator.permissions = <Permissions>{};
}
if (!self.navigator.permissions.request) {
self.navigator.permissions.request = request;
}
}