1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 ChromeUtils.defineESModuleGetters(lazy, {
8 generateUUID: "chrome://remote/content/shared/UUID.sys.mjs",
9 UnsupportedError: "chrome://remote/content/cdp/Error.sys.mjs",
24 await IOUtils.remove(this.#path);
28 // To keep compatibility with Chrome clip invalid offsets
29 this.#offset = Math.max(0, Math.min(seekTo, await this.length()));
32 async readBytes(count) {
33 const bytes = await IOUtils.read(this.#path, {
37 this.#offset += bytes.length;
42 const length = await this.length();
43 return length - this.#offset;
47 if (this.#length === null) {
48 const info = await IOUtils.stat(this.#path);
49 this.#length = info.size;
60 export class StreamRegistry {
63 this.streams = new Map();
65 // Register an async shutdown blocker to ensure all open IO streams are
66 // closed, and remaining temporary files removed. Needs to happen before
67 // IOUtils has been shutdown.
68 IOUtils.profileBeforeChange.addBlocker(
69 "Remote Agent: Clean-up of open streams",
71 await this.destructor();
77 for (const stream of this.streams.values()) {
78 await stream.destroy();
85 * Add a new stream to the registry.
87 * @param {Stream} stream
91 * Stream handle (uuid)
94 if (!(stream instanceof Stream)) {
95 // Bug 1602731 - Implement support for blob
96 throw new lazy.UnsupportedError(`Unknown stream type for ${stream}`);
99 const handle = lazy.generateUUID();
101 this.streams.set(handle, stream);
106 * Get a stream from the registry.
108 * @param {string} handle
109 * Handle of the stream to retrieve.
112 * The requested stream.
115 const stream = this.streams.get(handle);
118 throw new TypeError(`Invalid stream handle`);
125 * Remove a stream from the registry.
127 * @param {string} handle
128 * Handle of the stream to remove.
131 * true if successfully removed
133 async remove(handle) {
134 const stream = this.get(handle);
135 await stream.destroy();
137 return this.streams.delete(handle);