summaryrefslogtreecommitdiffstats
path: root/devtools/client/memory/actions/io.js
blob: bedd5d8bfc90b00537ae54550cecbcf82be264d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

const {
  immutableUpdate,
  reportException,
  assert,
} = require("resource://devtools/shared/DevToolsUtils.js");
const {
  snapshotState: states,
  actions,
} = require("resource://devtools/client/memory/constants.js");
const {
  L10N,
  openFilePicker,
  createSnapshot,
} = require("resource://devtools/client/memory/utils.js");
const {
  selectSnapshot,
  computeSnapshotData,
  readSnapshot,
} = require("resource://devtools/client/memory/actions/snapshot.js");
const VALID_EXPORT_STATES = [states.SAVED, states.READ];

exports.pickFileAndExportSnapshot = function (snapshot) {
  return async function ({ dispatch }) {
    const outputFile = await openFilePicker({
      title: L10N.getFormatStr("snapshot.io.save.window"),
      defaultName: PathUtils.filename(snapshot.path),
      filters: [[L10N.getFormatStr("snapshot.io.filter"), "*.fxsnapshot"]],
      mode: "save",
    });

    if (!outputFile) {
      return;
    }

    await dispatch(exportSnapshot(snapshot, outputFile.path));
  };
};

const exportSnapshot = (exports.exportSnapshot = function (snapshot, dest) {
  return async function ({ dispatch }) {
    dispatch({ type: actions.EXPORT_SNAPSHOT_START, snapshot });

    assert(
      VALID_EXPORT_STATES.includes(snapshot.state),
      `Snapshot is in invalid state for exporting: ${snapshot.state}`
    );

    try {
      await IOUtils.copy(snapshot.path, dest);
    } catch (error) {
      reportException("exportSnapshot", error);
      dispatch({ type: actions.EXPORT_SNAPSHOT_ERROR, snapshot, error });
    }

    dispatch({ type: actions.EXPORT_SNAPSHOT_END, snapshot });
  };
});

exports.pickFileAndImportSnapshotAndCensus = function (heapWorker) {
  return async function ({ dispatch }) {
    const input = await openFilePicker({
      title: L10N.getFormatStr("snapshot.io.import.window"),
      filters: [[L10N.getFormatStr("snapshot.io.filter"), "*.fxsnapshot"]],
      mode: "open",
    });

    if (!input) {
      return;
    }

    await dispatch(importSnapshotAndCensus(heapWorker, input.path));
  };
};

const importSnapshotAndCensus = function (heapWorker, path) {
  return async function ({ dispatch, getState }) {
    const snapshot = immutableUpdate(createSnapshot(getState()), {
      path,
      state: states.IMPORTING,
      imported: true,
    });
    const id = snapshot.id;

    dispatch({ type: actions.IMPORT_SNAPSHOT_START, snapshot });
    dispatch(selectSnapshot(snapshot.id));

    try {
      await dispatch(readSnapshot(heapWorker, id));
      await dispatch(computeSnapshotData(heapWorker, id));
    } catch (error) {
      reportException("importSnapshot", error);
      dispatch({ type: actions.IMPORT_SNAPSHOT_ERROR, error, id });
    }

    dispatch({ type: actions.IMPORT_SNAPSHOT_END, id });
  };
};
exports.importSnapshotAndCensus = importSnapshotAndCensus;