Skip to content

Commit 97c7c20

Browse files
authored
🤖 Merge PR DefinitelyTyped#73667 [chrome] update loginState namespace by @erwanjugand
1 parent 8475918 commit 97c7c20

File tree

2 files changed

+59
-12
lines changed

2 files changed

+59
-12
lines changed

types/chrome/index.d.ts

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6900,22 +6900,38 @@ declare namespace chrome {
69006900
* @since Chrome 78
69016901
*/
69026902
export namespace loginState {
6903-
export interface SessionStateChangedEvent extends chrome.events.Event<(sessionState: SessionState) => void> {}
6904-
6905-
/** Possible profile types. */
6906-
export type ProfileType = "SIGNIN_PROFILE" | "USER_PROFILE";
6903+
export enum ProfileType {
6904+
SIGNIN_PROFILE = "SIGNIN_PROFILE",
6905+
USER_PROFILE = "USER_PROFILE",
6906+
}
69076907

6908-
/** Possible session states. */
6909-
export type SessionState = "UNKNOWN" | "IN_OOBE_SCREEN" | "IN_LOGIN_SCREEN" | "IN_SESSION" | "IN_LOCK_SCREEN";
6908+
export enum SessionState {
6909+
UNKNOWN = "UNKNOWN",
6910+
IN_OOBE_SCREEN = "IN_OOBE_SCREEN",
6911+
IN_LOGIN_SCREEN = "IN_LOGIN_SCREEN",
6912+
IN_SESSION = "IN_SESSION",
6913+
IN_LOCK_SCREEN = "IN_LOCK_SCREEN",
6914+
IN_RMA_SCREEN = "IN_RMA_SCREEN",
6915+
}
69106916

6911-
/** Gets the type of the profile the extension is in. */
6912-
export function getProfileType(callback: (profileType: ProfileType) => void): void;
6917+
/**
6918+
* Gets the type of the profile the extension is in.
6919+
*
6920+
* Can return its result via Promise in Manifest V3 or later since Chrome 96.
6921+
*/
6922+
export function getProfileType(): Promise<`${ProfileType}`>;
6923+
export function getProfileType(callback: (result: `${ProfileType}`) => void): void;
69136924

6914-
/** Gets the current session state. */
6915-
export function getSessionState(callback: (sessionState: SessionState) => void): void;
6925+
/**
6926+
* Gets the current session state.
6927+
*
6928+
* Can return its result via Promise in Manifest V3 or later since Chrome 96.
6929+
*/
6930+
export function getSessionState(): Promise<`${SessionState}`>;
6931+
export function getSessionState(callback: (sessionState: `${SessionState}`) => void): void;
69166932

6917-
/** Dispatched when the session state changes. sessionState is the new session state.*/
6918-
export const onSessionStateChanged: SessionStateChangedEvent;
6933+
/** Dispatched when the session state changes. `sessionState` is the new session state.*/
6934+
export const onSessionStateChanged: events.Event<(sessionState: `${SessionState}`) => void>;
69196935
}
69206936

69216937
////////////////////

types/chrome/test/index.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6048,6 +6048,37 @@ function testInstanceID() {
60486048
checkChromeEvent(chrome.instanceID.onTokenRefresh, () => void 0);
60496049
}
60506050

6051+
// https://developer.chrome.com/docs/extensions/reference/api/loginState
6052+
function testLoginState() {
6053+
chrome.loginState.ProfileType.SIGNIN_PROFILE === "SIGNIN_PROFILE";
6054+
chrome.loginState.ProfileType.USER_PROFILE === "USER_PROFILE";
6055+
6056+
chrome.loginState.SessionState.IN_LOCK_SCREEN === "IN_LOCK_SCREEN";
6057+
chrome.loginState.SessionState.IN_LOGIN_SCREEN === "IN_LOGIN_SCREEN";
6058+
chrome.loginState.SessionState.IN_OOBE_SCREEN === "IN_OOBE_SCREEN";
6059+
chrome.loginState.SessionState.IN_RMA_SCREEN === "IN_RMA_SCREEN";
6060+
chrome.loginState.SessionState.IN_SESSION === "IN_SESSION";
6061+
chrome.loginState.SessionState.UNKNOWN === "UNKNOWN";
6062+
6063+
chrome.loginState.getProfileType(); // $ExpectType Promise<"SIGNIN_PROFILE" | "USER_PROFILE">
6064+
chrome.loginState.getProfileType((result) => { // $ExpectType void
6065+
result; // $ExpectType "SIGNIN_PROFILE" | "USER_PROFILE"
6066+
});
6067+
// @ts-expect-error
6068+
chrome.loginState.getProfileType(() => {}).then(() => {});
6069+
6070+
chrome.loginState.getSessionState(); // $ExpectType Promise<"IN_LOCK_SCREEN" | "IN_LOGIN_SCREEN" | "IN_OOBE_SCREEN" | "IN_RMA_SCREEN" | "IN_SESSION" | "UNKNOWN">
6071+
chrome.loginState.getSessionState((result) => { // $ExpectType void
6072+
result; // $ExpectType "IN_LOCK_SCREEN" | "IN_LOGIN_SCREEN" | "IN_OOBE_SCREEN" | "IN_RMA_SCREEN" | "IN_SESSION" | "UNKNOWN"
6073+
});
6074+
// @ts-expect-error
6075+
chrome.loginState.getSessionState(() => {}).then(() => {});
6076+
6077+
checkChromeEvent(chrome.loginState.onSessionStateChanged, (sessionState) => {
6078+
sessionState; // $ExpectType "IN_LOCK_SCREEN" | "IN_LOGIN_SCREEN" | "IN_OOBE_SCREEN" | "IN_RMA_SCREEN" | "IN_SESSION" | "UNKNOWN"
6079+
});
6080+
}
6081+
60516082
function testUserScripts() {
60526083
const worldProperties: chrome.userScripts.WorldProperties = {
60536084
csp: "script-src 'self'",

0 commit comments

Comments
 (0)