Skip to content

Commit adf5292

Browse files
committed
Merge remote-tracking branch 'sdk/master'
2 parents 7443f62 + 48475b0 commit adf5292

File tree

4 files changed

+155
-6
lines changed

4 files changed

+155
-6
lines changed

sdk/cothreads.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,17 @@ const CoThreadBase = {
5656
let f = this._func;
5757
let ctx = this._thisCtx;
5858
let callf = this._callf;
59+
const isStar = !('send' in g);
5960
try {
6061
for (let i = 0; i < y; ++i) {
61-
if (!callf(ctx, g.next(), this._idx++, f)) {
62+
let next = g.next();
63+
if (isStar) {
64+
if (next.done) {
65+
throw "complete";
66+
}
67+
next = next.value;
68+
}
69+
if (!callf(ctx, next, this._idx++, f)) {
6270
throw 'complete';
6371
}
6472
}
@@ -212,7 +220,7 @@ exports.CoThreadListWalker = function CoThreadListWalker(func, arrayOrGenerator,
212220
for (let i of arrayOrGenerator) {
213221
yield i;
214222
}
215-
});
223+
})();
216224
}
217225
else {
218226
this._generator = arrayOrGenerator;

sdk/preferences.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
* - Implement enumerate/getChildren
1010
*/
1111

12+
try {
13+
Instances.register("SupportsString", "@mozilla.org/supports-string;1", "nsISupportsString");
14+
}
15+
catch (ex) {
16+
// ignore
17+
}
18+
1219
const {
1320
PREF_INVALID: INVALID,
1421
PREF_STRING: STR,
@@ -17,7 +24,7 @@ const {
1724
} = Ci.nsIPrefBranch;
1825

1926
function createProxy(branch) {
20-
return Proxy.create({
27+
return new Proxy(branch, {
2128
get: function(receiver, name) {
2229
if (name in branch) {
2330
log(LOG_DEBUG, "prefproxy: returning plain " + name);
@@ -116,7 +123,7 @@ function Branch(branch) {
116123
{
117124
let str = new Instances.SupportsString();
118125
str.data = value;
119-
branch.setComplexValue(pref, str);
126+
branch.setComplexValue(pref, Ci.nsISupportsString, str);
120127
}
121128
break;
122129
}
@@ -139,7 +146,16 @@ function Branch(branch) {
139146

140147
(function setDefaultPrefs() {
141148
let branch = new Branch(Services.prefs.getDefaultBranch(""));
142-
let scope = {pref: (key, val) => branch.set(key, val)};
149+
let scope = {
150+
pref: (key, val) => {
151+
try {
152+
branch.set(key, val);
153+
}
154+
catch (ex) {
155+
Cu.reportError(ex);
156+
}
157+
}
158+
};
143159
try {
144160
Services.scriptloader.loadSubScript(BASE_PATH +
145161
"defaults/preferences/prefs.js", scope);

sdk/request.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
Cu.importGlobalProperties(["XMLHttpRequest", "Blob", "File"]);
7+
8+
exports.Blob = Blob;
9+
exports.File = File;
10+
exports.xhr = exports.XMLHttpRequest = XMLHttpRequest;
11+
12+
function Callbacks(underlay) {
13+
try {
14+
this.underlay = underlay.QueryInterface(Ci.nsIInterfaceRequestor);
15+
}
16+
catch (ex) {
17+
this.underlay = null;
18+
}
19+
}
20+
Callbacks.prototype = {
21+
badHost: null,
22+
getInterface: function(iid) {
23+
try {
24+
return this.QueryInterface(iid);
25+
}
26+
catch (ex) {
27+
if (this.underlay) {
28+
return this.underlay.getInterface(iid);
29+
}
30+
throw ex;
31+
}
32+
},
33+
QueryInterface: XPCOMUtils.generateQI([Ci.nsIBadCertListener2, Ci.nsIInterfaceRequestor]),
34+
notifyCertProblem: function(si, status, host) {
35+
this.badHost = host;
36+
return false;
37+
}
38+
};
39+
40+
exports.request = (method, url, data, options) => {
41+
options = options || {};
42+
return new Promise((resolve, reject) => {
43+
let req = new XMLHttpRequest();
44+
let cbs = null;
45+
try {
46+
url = url.spec || url;
47+
if (method == "POST" && !data) {
48+
let [u, ...pieces] = url.split("?");
49+
url = u;
50+
data = pieces.length && pieces.join("?") || null;
51+
}
52+
req.timeout = options.timeout || 10000;
53+
if (options.mime) {
54+
req.overrideMimeType(options.mime);
55+
}
56+
if (options.type) {
57+
req.responseType = options.type;
58+
}
59+
else {
60+
req.responseType = "json";
61+
}
62+
if (options.setup) {
63+
options.setup(req);
64+
}
65+
req.addEventListener("loadend", function end() {
66+
req.removeEventListener("loadend", end, false);
67+
try {
68+
if (req.status < 100 || req.status >= 400) {
69+
if (cbs && cbs.badHost) {
70+
throw new Error(`${cbs.badHost} has an invalid TLS Certificate`);
71+
}
72+
throw new Error("Bad response");
73+
}
74+
log(LOG_DEBUG, `request to ${url} resolved`);
75+
resolve(req);
76+
}
77+
catch (ex) {
78+
log(LOG_DEBUG, `request to ${url} rejected ${ex}`);
79+
reject(ex);
80+
}
81+
}, false);
82+
log(LOG_DEBUG, `Making request to ${url} w/ data ${data}`);
83+
req.open(method, url);
84+
if (req.channel) {
85+
try {
86+
cbs = new Callbacks(req.channel.notificationCallbacks);
87+
req.channel.notificationCallbacks = cbs;
88+
}
89+
catch (ex) {
90+
log(LOG_DEBUG, "Failed to set callbacks", ex);
91+
}
92+
if (req.channel instanceof Ci.nsIHttpChannel) {
93+
if (options.cookies) {
94+
for (let n of Object.keys(options.cookies)) {
95+
req.channel.setRequestHeader("Cookie", `${encodeURIComponent(n)}=${encodeURIComponent(options.cookies[n])}`, true);
96+
}
97+
}
98+
if (options.headers) {
99+
for (let n of Object.keys(options.headers)) {
100+
let h = options.headers[n];
101+
let merge = true;
102+
if (Array.isArray(h)) {
103+
merge = !!h[1];
104+
h = h[0];
105+
}
106+
req.channel.setRequestHeader(n, h, merge);
107+
}
108+
}
109+
}
110+
}
111+
req.send(data || null);
112+
}
113+
catch (ex) {
114+
reject(ex);
115+
}
116+
});
117+
};
118+
119+
/* vim: set et ts=2 sw=2 : */

sdk/strings.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ function Bundle(uri) {
1010
if (!bundleFlush) {
1111
bundleFlush = unload(() => Services.strings.flushBundles());
1212
}
13+
log(LOG_DEBUG, `loading bundle ${uri}`);
1314
return Services.strings.createBundle(uri);
1415
});
1516
this._dict = Object.create(null);
@@ -30,6 +31,11 @@ Bundle.prototype = {
3031
}
3132
};
3233

33-
Object.defineProperty(exports, "getBundle", {value: uri => new Bundle(uri)});
34+
Object.defineProperty(exports, "getBundle", {value: uri => {
35+
let bundle = new Bundle(uri);
36+
let rv = bundle.getString.bind(bundle);
37+
rv.getString = rv;
38+
return rv;
39+
}});
3440

3541
/* vim: set et ts=2 sw=2 : */

0 commit comments

Comments
 (0)