io.js (3048B)
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 file, 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 "use strict"; 5 6 const { 7 immutableUpdate, 8 reportException, 9 assert, 10 } = require("resource://devtools/shared/DevToolsUtils.js"); 11 const { 12 snapshotState: states, 13 actions, 14 } = require("resource://devtools/client/memory/constants.js"); 15 const { 16 L10N, 17 openFilePicker, 18 createSnapshot, 19 } = require("resource://devtools/client/memory/utils.js"); 20 const { 21 selectSnapshot, 22 computeSnapshotData, 23 readSnapshot, 24 } = require("resource://devtools/client/memory/actions/snapshot.js"); 25 const VALID_EXPORT_STATES = [states.SAVED, states.READ]; 26 27 exports.pickFileAndExportSnapshot = function (snapshot) { 28 return async function ({ dispatch }) { 29 const outputFile = await openFilePicker({ 30 title: L10N.getFormatStr("snapshot.io.save.window"), 31 defaultName: PathUtils.filename(snapshot.path), 32 filters: [[L10N.getFormatStr("snapshot.io.filter"), "*.fxsnapshot"]], 33 mode: "save", 34 }); 35 36 if (!outputFile) { 37 return; 38 } 39 40 await dispatch(exportSnapshot(snapshot, outputFile.path)); 41 }; 42 }; 43 44 const exportSnapshot = (exports.exportSnapshot = function (snapshot, dest) { 45 return async function ({ dispatch }) { 46 dispatch({ type: actions.EXPORT_SNAPSHOT_START, snapshot }); 47 48 assert( 49 VALID_EXPORT_STATES.includes(snapshot.state), 50 `Snapshot is in invalid state for exporting: ${snapshot.state}` 51 ); 52 53 try { 54 await IOUtils.copy(snapshot.path, dest); 55 } catch (error) { 56 reportException("exportSnapshot", error); 57 dispatch({ type: actions.EXPORT_SNAPSHOT_ERROR, snapshot, error }); 58 } 59 60 dispatch({ type: actions.EXPORT_SNAPSHOT_END, snapshot }); 61 }; 62 }); 63 64 exports.pickFileAndImportSnapshotAndCensus = function (heapWorker) { 65 return async function ({ dispatch }) { 66 const input = await openFilePicker({ 67 title: L10N.getFormatStr("snapshot.io.import.window"), 68 filters: [[L10N.getFormatStr("snapshot.io.filter"), "*.fxsnapshot"]], 69 mode: "open", 70 }); 71 72 if (!input) { 73 return; 74 } 75 76 await dispatch(importSnapshotAndCensus(heapWorker, input.path)); 77 }; 78 }; 79 80 const importSnapshotAndCensus = function (heapWorker, path) { 81 return async function ({ dispatch, getState }) { 82 const snapshot = immutableUpdate(createSnapshot(getState()), { 83 path, 84 state: states.IMPORTING, 85 imported: true, 86 }); 87 const id = snapshot.id; 88 89 dispatch({ type: actions.IMPORT_SNAPSHOT_START, snapshot }); 90 dispatch(selectSnapshot(snapshot.id)); 91 92 try { 93 await dispatch(readSnapshot(heapWorker, id)); 94 await dispatch(computeSnapshotData(heapWorker, id)); 95 } catch (error) { 96 reportException("importSnapshot", error); 97 dispatch({ type: actions.IMPORT_SNAPSHOT_ERROR, error, id }); 98 } 99 100 dispatch({ type: actions.IMPORT_SNAPSHOT_END, id }); 101 }; 102 }; 103 exports.importSnapshotAndCensus = importSnapshotAndCensus;