Skip to content

Commit

Permalink
Typescript: Created definition files for chrome.* APIs
Browse files Browse the repository at this point in the history
- Created settings_private_mv2.d.ts which adds definitions to be used by
  manifest v2 extensions, which can't use the Promise-based extension
  APIs unlike manifest v3 extensions or other WebUI code.

- Created definitions for chrome.tts

This CL is in preparation for chrome accessibility extensions
switch to Typescript.

Bug: b/267512305
Change-Id: Ib780b3389a0fbfd11ea00f44eed25998aeee8ca7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4214715
Reviewed-by: Demetrios Papadopoulos <dpapad@chromium.org>
Commit-Queue: Jacob Francis <francisjp@google.com>
Reviewed-by: Akihiro Ota <akihiroota@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1100685}
  • Loading branch information
Jacob Francis authored and Chromium LUCI CQ committed Feb 2, 2023
1 parent da41cde commit 67d4c8b
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 0 deletions.
84 changes: 84 additions & 0 deletions tools/typescript/definitions/settings_private_mv2.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/** @fileoverview Definitions for chrome.settingsPrivate API in Manifest V2 */
// This file exists because MV3 supports promises and MV2 does not.
// TODO(b/260590502): Delete this after MV3 migration.
// TODO(crbug.com/1203307): Auto-generate this file.

import {ChromeEvent} from './chrome_event.js';

declare global {
export namespace chrome {
export namespace settingsPrivate {
export enum PrefType {
BOOLEAN = 'BOOLEAN',
NUMBER = 'NUMBER',
STRING = 'STRING',
URL = 'URL',
LIST = 'LIST',
DICTIONARY = 'DICTIONARY',
}

export enum ControlledBy {
DEVICE_POLICY = 'DEVICE_POLICY',
USER_POLICY = 'USER_POLICY',
OWNER = 'OWNER',
PRIMARY_USER = 'PRIMARY_USER',
EXTENSION = 'EXTENSION',
PARENT = 'PARENT',
CHILD_RESTRICTION = 'CHILD_RESTRICTION',
}

export enum Enforcement {
ENFORCED = 'ENFORCED',
RECOMMENDED = 'RECOMMENDED',
PARENT_SUPERVISED = 'PARENT_SUPERVISED',
}

// Callback Types
type GetAllPrefsCallback = (prefs: PrefObject[]) => void;
type OnPrefSetCallback = (success: boolean) => void;
type GetPrefCallback = (pref: PrefObject) => void;
// TODO(crbug/1373934) Update existing usages of PrefObject to be typed,
// removing the need to use any here.
export interface PrefObject<T = any> {
key: string;
type:
// clang-format off
T extends boolean ? PrefType.BOOLEAN :
T extends number ? PrefType.NUMBER :
T extends string ? PrefType.STRING | PrefType.URL :
T extends unknown[] ? PrefType.LIST :
T extends Record<string|number, unknown> ? PrefType.DICTIONARY :
never;
// clang-format on
value: T;
controlledBy?: ControlledBy;
controlledByName?: string;
enforcement?: Enforcement;
recommendedValue?: T;
userSelectableValues?: T[];
userControlDisabled?: boolean;
extensionId?: string;
extensionCanBeDisabled?: boolean;
}

export function getAllPrefs(callback: GetAllPrefsCallback): void;
export function getPref(name: string, callback: GetPrefCallback): void;

export function setPref(
name: string, value: any, pageId?: string,
callback?: OnPrefSetCallback): void;

export function getDefaultZoom(callback: (arg: number) => void): void;
export function setDefaultZoom(
zoom: number, callback?: (arg: boolean) => void): void;

type PrefsCallback = (prefs: PrefObject[]) => void;

export const onPrefsChanged: ChromeEvent<PrefsCallback>;
}
}
}
72 changes: 72 additions & 0 deletions tools/typescript/definitions/tts.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import {ChromeEvent} from './chrome_event.js';

// TODO(crbug.com/1203307): Auto-generate this file.

declare global {
namespace chrome {
export namespace tts {

export enum EventType {
START = 'start',
END = 'end',
WORD = 'word',
SENTENCE = 'sentence',
MARKER = 'marker',
INTERRUPTED = 'interrupted',
CANCELLED = 'cancelled',
ERROR = 'error',
PAUSE = 'pause',
RESUME = 'resume',
}

export class TtsOptions {
enqueue?: boolean;
voiceName?: string;
extensionId?: string;
lang?: string;
rate: number;
pitch?: number;
volume?: number;
requiredEventTypes?: string[];
desiredEventTypes?: string[];
onEvent(event: TtsEvent): void;
}

export interface TtsEvent {
type: EventType;
charIndex?: number;
errorMessage?: string;
srcId?: number;
isFinalEvent?: boolean;
length?: number;
}

export interface TtsVoice {
voiceName?: string;
lang?: string;
remote?: boolean;
extensionId?: string;
eventTypes?: EventType[];
}

export function speak(
utterance: string, options: TtsOptions, callback?: () => void): void;

export function stop(): void;

export function pause(): void;

export function resume(): void;

export function isSpeaking(callback?: (param: boolean) => void): void;

export function getVoices(callback?: (param: TtsVoice[]) => void): void;

export const onEvent: ChromeEvent<(event: TtsEvent) => void>;
}
}
}

0 comments on commit 67d4c8b

Please sign in to comment.