| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | * |
| 7 | * @flow |
| 8 | */ |
| 9 | |
| 10 | import {PermissionNotGrantedError} from 'react-devtools-shared/src/errors/PermissionNotGrantedError'; |
| 11 | |
| 12 | type SupportedPermission = 'clipboardWrite'; |
| 13 | type Permissions = Array<SupportedPermission>; |
| 14 | type PermissionsOptions = {permissions: Permissions}; |
| 15 | |
| 16 | // browser.permissions is not available for DevTools pages in Firefox |
| 17 | // https://bugzilla.mozilla.org/show_bug.cgi?id=1796933 |
| 18 | // We are going to assume that requested permissions are not optional. |
| 19 | export function withPermissionsCheck<T: (...$ReadOnlyArray<empty>) => mixed>( |
| 20 | options: PermissionsOptions, |
| 21 | callback: T, |
| 22 | ): T | (() => Promise<ReturnType<T>>) { |
| 23 | if (!__IS_CHROME__ && !__IS_EDGE__) { |
| 24 | return callback; |
| 25 | } else { |
| 26 | return async () => { |
| 27 | const granted = await chrome.permissions.request(options); |
| 28 | if (granted) { |
| 29 | return callback(); |
| 30 | } |
| 31 | |
| 32 | return Promise.reject(new PermissionNotGrantedError()); |
| 33 | }; |
| 34 | } |
| 35 | } |