-
Notifications
You must be signed in to change notification settings - Fork 54
/
Utils.jsm
92 lines (76 loc) · 2.33 KB
/
Utils.jsm
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
/* 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";
var EXPORTED_SYMBOLS = ["Utils"];
ChromeUtils.import("resource://gre/modules/Services.jsm", this);
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm", this);
ChromeUtils.defineModuleGetter(
this,
"NetUtil",
"resource://gre/modules/NetUtil.jsm"
);
XPCOMUtils.defineLazyServiceGetter(
this,
"eTLDService",
"@mozilla.org/network/effective-tld-service;1",
"nsIEffectiveTLDService"
);
var Utils = Object.freeze({
serializeInputStream(aStream) {
let data = {
content: NetUtil.readInputStreamToString(aStream, aStream.available()),
};
if (aStream instanceof Ci.nsIMIMEInputStream) {
data.headers = new Map();
aStream.visitHeaders((name, value) => {
data.headers.set(name, value);
});
}
return data;
},
/**
* Returns true if the |url| passed in is part of the given root |domain|.
* For example, if |url| is "www.mozilla.org", and we pass in |domain| as
* "mozilla.org", this will return true. It would return false the other way
* around.
*/
hasRootDomain(url, domain) {
let host;
try {
host = Services.io.newURI(url).host;
} catch (e) {
// The given URL probably doesn't have a host.
return false;
}
return eTLDService.hasRootDomain(host, domain);
},
shallowCopy(obj) {
let retval = {};
for (let key of Object.keys(obj)) {
retval[key] = obj[key];
}
return retval;
},
/**
* Restores frame tree |data|, starting at the given root |frame|. As the
* function recurses into descendant frames it will call cb(frame, data) for
* each frame it encounters, starting with the given root.
*/
restoreFrameTreeData(frame, data, cb) {
// Restore data for the root frame.
// The callback can abort by returning false.
if (cb(frame, data) === false) {
return;
}
if (!data.hasOwnProperty("children")) {
return;
}
// Recurse into child frames.
SessionStoreUtils.forEachNonDynamicChildFrame(frame, (subframe, index) => {
if (data.children[index]) {
this.restoreFrameTreeData(subframe, data.children[index], cb);
}
});
},
});