Bug 1913305 - Add test. r=mtigley
[gecko.git] / devtools / server / actors / heap-snapshot-file.js
blob942f72a98b5e2aad8afa1a6bddf311548541416f
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/. */
5 "use strict";
7 const { Actor } = require("resource://devtools/shared/protocol.js");
8 const {
9   heapSnapshotFileSpec,
10 } = require("resource://devtools/shared/specs/heap-snapshot-file.js");
12 loader.lazyRequireGetter(
13   this,
14   "DevToolsUtils",
15   "resource://devtools/shared/DevToolsUtils.js"
17 loader.lazyRequireGetter(
18   this,
19   "HeapSnapshotFileUtils",
20   "resource://devtools/shared/heapsnapshot/HeapSnapshotFileUtils.js"
23 /**
24  * The HeapSnapshotFileActor handles transferring heap snapshot files from the
25  * server to the client. This has to be a global actor in the parent process
26  * because child processes are sandboxed and do not have access to the file
27  * system.
28  */
29 exports.HeapSnapshotFileActor = class HeapSnapshotFileActor extends Actor {
30   constructor(conn) {
31     super(conn, heapSnapshotFileSpec);
33     if (
34       Services.appinfo.processType !== Services.appinfo.PROCESS_TYPE_DEFAULT
35     ) {
36       const err = new Error(
37         "Attempt to create a HeapSnapshotFileActor in a " +
38           "child process! The HeapSnapshotFileActor *MUST* " +
39           "be in the parent process!"
40       );
41       DevToolsUtils.reportException("HeapSnapshotFileActor's constructor", err);
42     }
43   }
45   /**
46    * @see MemoryFront.prototype.transferHeapSnapshot
47    */
48   async transferHeapSnapshot(snapshotId) {
49     const snapshotFilePath =
50       HeapSnapshotFileUtils.getHeapSnapshotTempFilePath(snapshotId);
51     if (!snapshotFilePath) {
52       throw new Error(`No heap snapshot with id: ${snapshotId}`);
53     }
55     const streamPromise = DevToolsUtils.openFileStream(snapshotFilePath);
57     const { size } = await IOUtils.stat(snapshotFilePath);
58     const bulkPromise = this.conn.startBulkSend({
59       actor: this.actorID,
60       type: "heap-snapshot",
61       length: size,
62     });
64     const [bulk, stream] = await Promise.all([bulkPromise, streamPromise]);
66     try {
67       await bulk.copyFrom(stream);
68     } finally {
69       stream.close();
70     }
71   }