2019-01-15 04:34:50 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2019 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
import {Protocol} from 'devtools-protocol';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
2023-02-09 17:04:06 +00:00
|
|
|
import {JSHandle} from '../api/JSHandle.js';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
import {CDPSession} from './Connection.js';
|
2023-02-09 17:04:06 +00:00
|
|
|
import type {CDPElementHandle} from './ElementHandle.js';
|
2023-09-01 12:12:29 +00:00
|
|
|
import {IsolatedWorld} from './IsolatedWorld.js';
|
|
|
|
import {releaseObject, valueFromRemoteObject} from './util.js';
|
2022-04-27 21:00:07 +00:00
|
|
|
|
2021-04-06 08:58:01 +00:00
|
|
|
/**
|
2023-02-09 17:04:06 +00:00
|
|
|
* @internal
|
2021-04-06 08:58:01 +00:00
|
|
|
*/
|
2023-02-20 12:00:29 +00:00
|
|
|
export class CDPJSHandle<T = unknown> extends JSHandle<T> {
|
2022-06-13 09:16:25 +00:00
|
|
|
#disposed = false;
|
2023-09-01 12:12:29 +00:00
|
|
|
readonly #remoteObject: Protocol.Runtime.RemoteObject;
|
|
|
|
readonly #world: IsolatedWorld;
|
2022-06-13 09:16:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
constructor(
|
2023-09-01 12:12:29 +00:00
|
|
|
world: IsolatedWorld,
|
2020-05-07 10:54:55 +00:00
|
|
|
remoteObject: Protocol.Runtime.RemoteObject
|
|
|
|
) {
|
2023-02-09 17:04:06 +00:00
|
|
|
super();
|
2023-09-01 12:12:29 +00:00
|
|
|
this.#world = world;
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#remoteObject = remoteObject;
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
|
2023-09-01 12:12:29 +00:00
|
|
|
override get disposed(): boolean {
|
|
|
|
return this.#disposed;
|
2019-09-04 22:19:34 +00:00
|
|
|
}
|
|
|
|
|
2023-09-01 12:12:29 +00:00
|
|
|
override get realm(): IsolatedWorld {
|
|
|
|
return this.#world;
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
|
2023-09-01 12:12:29 +00:00
|
|
|
get client(): CDPSession {
|
|
|
|
return this.realm.environment.client;
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
|
2023-02-09 17:04:06 +00:00
|
|
|
override async jsonValue(): Promise<T> {
|
2022-08-11 09:45:35 +00:00
|
|
|
if (!this.#remoteObject.objectId) {
|
|
|
|
return valueFromRemoteObject(this.#remoteObject);
|
|
|
|
}
|
|
|
|
const value = await this.evaluate(object => {
|
|
|
|
return object;
|
|
|
|
});
|
|
|
|
if (value === undefined) {
|
|
|
|
throw new Error('Could not serialize referenced object');
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
2022-08-11 09:45:35 +00:00
|
|
|
return value;
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 14:49:35 +00:00
|
|
|
/**
|
2023-03-30 11:54:00 +00:00
|
|
|
* Either `null` or the handle itself if the handle is an
|
2022-08-11 09:45:35 +00:00
|
|
|
* instance of {@link ElementHandle}.
|
2020-06-25 14:49:35 +00:00
|
|
|
*/
|
2023-02-09 17:04:06 +00:00
|
|
|
override asElement(): CDPElementHandle<Node> | null {
|
2019-01-15 04:34:50 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-02-09 17:04:06 +00:00
|
|
|
override async dispose(): Promise<void> {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (this.#disposed) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#disposed = true;
|
2022-08-25 15:00:35 +00:00
|
|
|
await releaseObject(this.client, this.#remoteObject);
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
|
2023-02-09 17:04:06 +00:00
|
|
|
override toString(): string {
|
2022-08-11 09:45:35 +00:00
|
|
|
if (!this.#remoteObject.objectId) {
|
|
|
|
return 'JSHandle:' + valueFromRemoteObject(this.#remoteObject);
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
2022-08-11 09:45:35 +00:00
|
|
|
const type = this.#remoteObject.subtype || this.#remoteObject.type;
|
|
|
|
return 'JSHandle@' + type;
|
2022-07-07 13:04:28 +00:00
|
|
|
}
|
|
|
|
|
2023-02-15 10:29:18 +00:00
|
|
|
override get id(): string | undefined {
|
|
|
|
return this.#remoteObject.objectId;
|
|
|
|
}
|
|
|
|
|
2023-02-09 17:04:06 +00:00
|
|
|
override remoteObject(): Protocol.Runtime.RemoteObject {
|
2022-07-07 13:04:28 +00:00
|
|
|
return this.#remoteObject;
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
}
|