/**
* SDK 内部事件,用于将容器层观测点上报给业务方。
*
* 协议回调走 `onSignResult`,容器观测走 `onSdkEvent`;事件用 `type` 判别字段(string literal 联合)便于 switch 穷尽匹配。
*
* **使用范例**:
* ```ts
* const handler = (event: SdkEvent): void => {
* switch (event.type) {
* case 'PageStarted':
* console.log('开始加载', event.url);
* break;
* case 'SchemeIntercepted':
* console.log('拦截 scheme', event.scheme, event.url);
* break;
* // ...
* }
* };
* ```
*/
// ────────── 页面生命周期 ──────────
export class PageStartedEvent {
public readonly type: 'PageStarted' = 'PageStarted';
public url: string;
constructor(url: string) {
this.url = url;
}
}
export class PageFinishedEvent {
public readonly type: 'PageFinished' = 'PageFinished';
public url: string;
constructor(url: string) {
this.url = url;
}
}
// ────────── 权限 ──────────
export class PermissionGrantedEvent {
public readonly type: 'PermissionGranted' = 'PermissionGranted';
public permissions: string[];
constructor(permissions: string[]) {
this.permissions = permissions;
}
}
export class PermissionDeniedEvent {
public readonly type: 'PermissionDenied' = 'PermissionDenied';
public permissions: string[];
constructor(permissions: string[]) {
this.permissions = permissions;
}
}
// ────────── Scheme 拦截 ──────────
export class SchemeInterceptedEvent {
public readonly type: 'SchemeIntercepted' = 'SchemeIntercepted';
public scheme: string;
public url: string;
constructor(scheme: string, url: string) {
this.scheme = scheme;
this.url = url;
}
}
export class SchemeNotMatchedEvent {
public readonly type: 'SchemeNotMatched' = 'SchemeNotMatched';
public url: string;
constructor(url: string) {
this.url = url;
}
}
// ────────── 网络与渲染错误 ──────────
export class LoadErrorEvent {
public readonly type: 'LoadError' = 'LoadError';
public url: string;
public errorCode: number;
public description: string;
constructor(url: string, errorCode: number, description: string) {
this.url = url;
this.errorCode = errorCode;
this.description = description;
}
}
export class HttpErrorEvent {
public readonly type: 'HttpError' = 'HttpError';
public url: string;
public statusCode: number;
public reasonPhrase: string;
constructor(url: string, statusCode: number, reasonPhrase: string) {
this.url = url;
this.statusCode = statusCode;
this.reasonPhrase = reasonPhrase;
}
}
export class SslErrorEvent {
public readonly type: 'SslError' = 'SslError';
public url: string;
public primaryError: number;
constructor(url: string, primaryError: number) {
this.url = url;
this.primaryError = primaryError;
}
}
// ────────── 下载 ──────────
export class DownloadStartEvent {
public readonly type: 'DownloadStart' = 'DownloadStart';
public url: string;
public mimeType: string;
public contentDisposition: string;
constructor(url: string, mimeType: string, contentDisposition: string) {
this.url = url;
this.mimeType = mimeType;
this.contentDisposition = contentDisposition;
}
}
export class DownloadFailedEvent {
public readonly type: 'DownloadFailed' = 'DownloadFailed';
public url: string;
public reason: string;
constructor(url: string, reason: string) {
this.url = url;
this.reason = reason;
}
}
export class DownloadCompletedEvent {
public readonly type: 'DownloadCompleted' = 'DownloadCompleted';
public url: string;
public filePath: string;
constructor(url: string, filePath: string) {
this.url = url;
this.filePath = filePath;
}
}
// ────────── 渲染进程崩溃 ──────────
export class RenderProcessGoneEvent {
public readonly type: 'RenderProcessGone' = 'RenderProcessGone';
/** `true` = 进程崩溃(OOM 等);`false` = 被系统回收(低内存) */
public didCrash: boolean;
public url: string;
constructor(didCrash: boolean, url: string) {
this.didCrash = didCrash;
this.url = url;
}
}
// ────────── UA 注入失败兜底 ──────────
export class UserAgentInjectFailedEvent {
public readonly type: 'UserAgentInjectFailed' = 'UserAgentInjectFailed';
public reason: string;
constructor(reason: string) {
this.reason = reason;
}
}
/**
* SDK 事件联合类型。
*
* 客户实现 `onSdkEvent: (event: SdkEvent) => void`,通过 `event.type` 判别具体子类型。
*/
export type SdkEvent =
| PageStartedEvent
| PageFinishedEvent
| PermissionGrantedEvent
| PermissionDeniedEvent
| SchemeInterceptedEvent
| SchemeNotMatchedEvent
| LoadErrorEvent
| HttpErrorEvent
| SslErrorEvent
| DownloadStartEvent
| DownloadCompletedEvent
| DownloadFailedEvent
| RenderProcessGoneEvent
| UserAgentInjectFailedEvent;
|