-
Notifications
You must be signed in to change notification settings - Fork 8
/
simple-wrapper.js
106 lines (90 loc) · 2.88 KB
/
simple-wrapper.js
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
104
105
106
/* See license.txt for terms of usage */
function ccdump_wrapper(options)
{
this.options = options;
this.requireConfig = {
baseUrl: options.ccdump_path + "/content",
};
}
ccdump_wrapper.prototype = {
options: null,
requireConfig: null,
analyzer: null,
graphSerializer: null,
_loaded: false,
_ran: false,
load_ccdump: function load_ccdump(callback)
{
if (this._loaded)
{
callback && callback(this);
return;
}
let loaderJs = this.options.ccdump_path + "/content/loader.js";
let global = this.options.global;
Services.scriptloader.loadSubScript(loaderJs, global, "utf8");
global.require(this.requireConfig,
["app/analyzer", "app/graphSerializer"],
function _onRequireCcdump(mAnalyzer, mGraphSerializer)
{
this.analyzer = new mAnalyzer();
this.graphSerializer = mGraphSerializer;
this._loaded = true;
callback && callback(this);
}.bind(this));
},
run: function ccdump_run(callback)
{
let listener = {
onProgress: function() { },
onFinished: function()
{
this._ran = true;
callback && callback(this);
}.bind(this),
};
this.load_ccdump(this.analyzer.run.bind(this.analyzer, listener));
},
save: function ccdump_save(filePath, callback)
{
function doSave()
{
let file = Cc["@mozilla.org/file/local;1"].
createInstance(Ci.nsILocalFile);
file.initWithPath(filePath);
let fs = Cc["@mozilla.org/network/file-output-stream;1"].
createInstance(Ci.nsIFileOutputStream);
let modeFlags = 0x02 | 0x08 | 0x20;
fs.init(file, modeFlags, 420 /* 0644 */, fs.DEFER_OPEN);
let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
createInstance(Ci.nsIScriptableUnicodeConverter);
converter.charset = "UTF-8";
let input = converter.convertToInputStream(this.toJSON());
NetUtil.asyncCopy(input, fs, function _onAsyncCopy(aStatus)
{
if (!Components.isSuccessCode(aStatus)) {
Cu.reportError("failed to save ccdump file " + filePath);
}
callback && callback(aStatus);
});
}
if (this._ran)
{
doSave.call(this);
}
else
{
this.run(doSave.bind(this));
}
},
toJSON: function ccdump_toJSON()
{
return this.graphSerializer.toJSON(this.analyzer);
},
clear: function ccdump_clear()
{
this.analyzer.clear();
this._ran = false;
},
};
// vim:set tw=80 sw=4 sts=4 et: