Skip to content

Commit e8c0c6c

Browse files
authored
🤖 Merge PR DefinitelyTyped#73818 [chrome] update system.storage namespace by @erwanjugand
1 parent 3239762 commit e8c0c6c

File tree

2 files changed

+85
-41
lines changed

2 files changed

+85
-41
lines changed

‎types/chrome/index.d.ts‎

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10156,69 +10156,74 @@ declare namespace chrome {
1015610156
* Permissions: "system.storage"
1015710157
*/
1015810158
export namespace system.storage {
10159+
export enum EjectDeviceResultCode {
10160+
/** The ejection command is successful -- the application can prompt the user to remove the device. */
10161+
SUCCESS = "success",
10162+
/** The device is in use by another application. The ejection did not succeed; the user should not remove the device until the other application is done with the device. */
10163+
IN_USE = "in_use",
10164+
/** There is no such device known. */
10165+
NO_SUCH_DEVICE = "no_such_device",
10166+
/** The ejection command failed. */
10167+
FAILURE = "failure",
10168+
}
10169+
1015910170
export interface StorageUnitInfo {
1016010171
/** The transient ID that uniquely identifies the storage device. This ID will be persistent within the same run of a single application. It will not be a persistent identifier between different runs of an application, or between different applications. */
1016110172
id: string;
1016210173
/** The name of the storage unit. */
1016310174
name: string;
10164-
/**
10165-
* The media type of the storage unit.
10166-
* fixed: The storage has fixed media, e.g. hard disk or SSD.
10167-
* removable: The storage is removable, e.g. USB flash drive.
10168-
* unknown: The storage type is unknown.
10169-
*/
10170-
type: string;
10175+
/** The media type of the storage unit. */
10176+
type: `${StorageUnitType}`;
1017110177
/** The total amount of the storage space, in bytes. */
1017210178
capacity: number;
1017310179
}
1017410180

10175-
export interface StorageCapacityInfo {
10176-
/** A copied |id| of getAvailableCapacity function parameter |id|. */
10181+
export enum StorageUnitType {
10182+
/** The storage has fixed media, e.g. hard disk or SSD. */
10183+
FIXED = "fixed",
10184+
/** The storage is removable, e.g. USB flash drive. */
10185+
REMOVABLE = "removable",
10186+
/** The storage type is unknown. */
10187+
UNKNOWN = "unknown",
10188+
}
10189+
10190+
export interface StorageAvailableCapacityInfo {
10191+
/** A copied `id` of getAvailableCapacity function parameter `id`. */
1017710192
id: string;
1017810193
/** The available capacity of the storage device, in bytes. */
1017910194
availableCapacity: number;
1018010195
}
1018110196

10182-
export interface SystemStorageAttachedEvent extends chrome.events.Event<(info: StorageUnitInfo) => void> {}
10183-
10184-
export interface SystemStorageDetachedEvent extends chrome.events.Event<(id: string) => void> {}
10185-
10186-
/** Get the storage information from the system. The argument passed to the callback is an array of StorageUnitInfo objects. */
10187-
export function getInfo(callback: (info: StorageUnitInfo[]) => void): void;
1018810197
/**
1018910198
* Get the storage information from the system. The argument passed to the callback is an array of StorageUnitInfo objects.
10190-
* @return The `getInfo` method provides its result via callback or returned as a `Promise` (MV3 only).
10199+
*
10200+
* Can return its result via Promise in Manifest V3 or later since Chrome 91.
1019110201
*/
1019210202
export function getInfo(): Promise<StorageUnitInfo[]>;
10203+
export function getInfo(callback: (info: StorageUnitInfo[]) => void): void;
10204+
1019310205
/**
1019410206
* Ejects a removable storage device.
10195-
* @param callback
10196-
* Parameter result: success: The ejection command is successful -- the application can prompt the user to remove the device; in_use: The device is in use by another application. The ejection did not succeed; the user should not remove the device until the other application is done with the device; no_such_device: There is no such device known. failure: The ejection command failed.
10197-
*/
10198-
export function ejectDevice(id: string, callback: (result: string) => void): void;
10199-
/**
10200-
* Ejects a removable storage device.
10201-
* @param callback
10202-
* Parameter result: success: The ejection command is successful -- the application can prompt the user to remove the device; in_use: The device is in use by another application. The ejection did not succeed; the user should not remove the device until the other application is done with the device; no_such_device: There is no such device known. failure: The ejection command failed.
10203-
* @return The `ejectDevice` method provides its result via callback or returned as a `Promise` (MV3 only).
10204-
*/
10205-
export function ejectDevice(id: string): Promise<string>;
10206-
/**
10207-
* Get the available capacity of a specified |id| storage device. The |id| is the transient device ID from StorageUnitInfo.
10208-
* @since Dev channel only.
10207+
*
10208+
* Can return its result via Promise in Manifest V3 or later since Chrome 91.
1020910209
*/
10210-
export function getAvailableCapacity(id: string, callback: (info: StorageCapacityInfo) => void): void;
10210+
export function ejectDevice(id: string): Promise<`${EjectDeviceResultCode}`>;
10211+
export function ejectDevice(id: string, callback: (result: `${EjectDeviceResultCode}`) => void): void;
10212+
1021110213
/**
10212-
* Get the available capacity of a specified |id| storage device. The |id| is the transient device ID from StorageUnitInfo.
10214+
* Get the available capacity of a specified `id` storage device. The `id` is the transient device ID from StorageUnitInfo.
10215+
*
10216+
* Can return its result via Promise in Manifest V3.
1021310217
* @since Dev channel only.
10214-
* @return The `getAvailableCapacity` method provides its result via callback or returned as a `Promise` (MV3 only).
1021510218
*/
10216-
export function getAvailableCapacity(id: string): Promise<StorageCapacityInfo>;
10219+
export function getAvailableCapacity(id: string): Promise<StorageAvailableCapacityInfo>;
10220+
export function getAvailableCapacity(id: string, callback: (info: StorageAvailableCapacityInfo) => void): void;
1021710221

1021810222
/** Fired when a new removable storage is attached to the system. */
10219-
export var onAttached: SystemStorageAttachedEvent;
10223+
export const onAttached: events.Event<(info: StorageUnitInfo) => void>;
10224+
1022010225
/** Fired when a removable storage is detached from the system. */
10221-
export var onDetached: SystemStorageDetachedEvent;
10226+
export const onDetached: events.Event<(id: string) => void>;
1022210227
}
1022310228

1022410229
////////////////////

‎types/chrome/test/index.ts‎

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2863,11 +2863,50 @@ async function testSystemCpu() {
28632863
chrome.system.cpu.getInfo(() => {}).then(() => {});
28642864
}
28652865

2866-
// https://developer.chrome.com/docs/extensions/reference/system_storage
2867-
async function testSystemStorageForPromise() {
2868-
await chrome.system.storage.getInfo();
2869-
await chrome.system.storage.ejectDevice("id1");
2870-
await chrome.system.storage.getAvailableCapacity("id1");
2866+
// https://developer.chrome.com/docs/extensions/reference/api/system/storage
2867+
async function testSystemStorage() {
2868+
chrome.system.storage.EjectDeviceResultCode.FAILURE === "failure";
2869+
chrome.system.storage.EjectDeviceResultCode.IN_USE === "in_use";
2870+
chrome.system.storage.EjectDeviceResultCode.NO_SUCH_DEVICE === "no_such_device";
2871+
chrome.system.storage.EjectDeviceResultCode.SUCCESS === "success";
2872+
2873+
chrome.system.storage.StorageUnitType.FIXED === "fixed";
2874+
chrome.system.storage.StorageUnitType.REMOVABLE === "removable";
2875+
chrome.system.storage.StorageUnitType.UNKNOWN === "unknown";
2876+
2877+
const id = "id";
2878+
chrome.system.storage.ejectDevice(id); // $ExpectType Promise<"success" | "in_use" | "no_such_device" | "failure">
2879+
chrome.system.storage.ejectDevice(id, (result) => { // $ExpectType void
2880+
result; // $ExpectType "success" | "in_use" | "no_such_device" | "failure"
2881+
});
2882+
// @ts-expect-error
2883+
chrome.system.storage.ejectDevice(id, () => {}).then(() => {});
2884+
2885+
chrome.system.storage.getAvailableCapacity(id); // $ExpectType Promise<StorageAvailableCapacityInfo>
2886+
chrome.system.storage.getAvailableCapacity(id, (info) => { // $ExpectType void
2887+
info.availableCapacity; // $ExpectType number
2888+
info.id; // $ExpectType string
2889+
});
2890+
// @ts-expect-error
2891+
chrome.system.storage.getAvailableCapacity(id, () => {}).then(() => {});
2892+
2893+
chrome.system.storage.getInfo(); // $ExpectType Promise<StorageUnitInfo[]>
2894+
chrome.system.storage.getInfo((units) => { // $ExpectType void
2895+
units; // $ExpectType StorageUnitInfo[]
2896+
});
2897+
// @ts-expect-error
2898+
chrome.system.storage.getInfo(() => {}).then(() => {});
2899+
2900+
checkChromeEvent(chrome.system.storage.onAttached, (info) => {
2901+
info.capacity; // $ExpectType number
2902+
info.id; // $ExpectType string
2903+
info.name; // $ExpectType string
2904+
info.type; // $ExpectType "fixed" | "removable" | "unknown"
2905+
});
2906+
2907+
checkChromeEvent(chrome.system.storage.onDetached, (id) => {
2908+
id; // $ExpectType string
2909+
});
28712910
}
28722911

28732912
// https://developer.chrome.com/docs/extensions/reference/api/system/display

0 commit comments

Comments
 (0)