heap-snapshot-file.js (2508B)
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/. */ 4 5 "use strict"; 6 7 const { Actor } = require("resource://devtools/shared/protocol.js"); 8 const { 9 heapSnapshotFileSpec, 10 } = require("resource://devtools/shared/specs/heap-snapshot-file.js"); 11 12 loader.lazyRequireGetter( 13 this, 14 "DevToolsUtils", 15 "resource://devtools/shared/DevToolsUtils.js" 16 ); 17 loader.lazyRequireGetter( 18 this, 19 "HeapSnapshotFileUtils", 20 "resource://devtools/shared/heapsnapshot/HeapSnapshotFileUtils.js" 21 ); 22 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); 32 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 } 44 45 /** 46 * @see MemoryFront.prototype.transferHeapSnapshot 47 * 48 * @param {string} snapshotId 49 * The ID returned by MemoryActor's saveHeapSnapshot method. 50 * @param {Function} startBulkSend 51 * Function provided by protocol.js Actor framework to initiate 52 * the bulk reply. This methods returns a promise resolving to 53 * a StreamCopier instance, whose `copyFrom` method allows 54 * to send the data back to the client via an input stream. 55 */ 56 async transferHeapSnapshot(snapshotId, startBulkSend) { 57 const snapshotFilePath = 58 HeapSnapshotFileUtils.getHeapSnapshotTempFilePath(snapshotId); 59 if (!snapshotFilePath) { 60 throw new Error(`No heap snapshot with id: ${snapshotId}`); 61 } 62 63 const streamPromise = DevToolsUtils.openFileStream(snapshotFilePath); 64 65 const { size } = await IOUtils.stat(snapshotFilePath); 66 const bulkPromise = startBulkSend(size); 67 68 const [bulk, stream] = await Promise.all([bulkPromise, streamPromise]); 69 70 try { 71 await bulk.copyFrom(stream); 72 } finally { 73 stream.close(); 74 } 75 } 76 };