Bug 902030 - DOMRequestIpcHelper stores/retrieves Promises too. r=fabrice

--HG--
extra : rebase_source : 4610af506cc1e15f8f2efdc682e3d7989626cebf
This commit is contained in:
Nikhil Marathe 2013-08-09 18:11:11 -07:00
parent e82bab972a
commit b1c04087a0
3 changed files with 160 additions and 2 deletions

View File

@ -3,8 +3,8 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Helper object for APIs that deal with DOMRequests and need to release them
* when the window goes out of scope.
* Helper object for APIs that deal with DOMRequests and Promises and need to
* release them when the window goes out of scope.
*/
const Cu = Components.utils;
const Cc = Components.classes;
@ -134,16 +134,34 @@ DOMRequestIpcHelper.prototype = {
return id;
},
getPromiseResolverId: function(aPromiseResolver) {
// Delegates to getRequest() since the lookup table is agnostic about
// storage.
return this.getRequestId(aPromiseResolver);
},
getRequest: function(aId) {
if (this._requests[aId])
return this._requests[aId];
},
getPromiseResolver: function(aId) {
// Delegates to getRequest() since the lookup table is agnostic about
// storage.
return this.getRequest(aId);
},
removeRequest: function(aId) {
if (this._requests[aId])
delete this._requests[aId];
},
removePromiseResolver: function(aId) {
// Delegates to getRequest() since the lookup table is agnostic about
// storage.
this.removeRequest(aId);
},
takeRequest: function(aId) {
if (!this._requests[aId])
return null;
@ -152,6 +170,12 @@ DOMRequestIpcHelper.prototype = {
return request;
},
takePromiseResolver: function(aId) {
// Delegates to getRequest() since the lookup table is agnostic about
// storage.
return this.takeRequest(aId);
},
_getRandomId: function() {
return Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator).generateUUID().toString();
},
@ -176,5 +200,14 @@ DOMRequestIpcHelper.prototype = {
createRequest: function() {
return Services.DOMRequest.createRequest(this._window);
},
/**
* createPromise() creates a new Promise, with `aPromiseInit` as the
* PromiseInit callback. The promise constructor is obtained from the
* reference to window owned by this DOMRequestIPCHelper.
*/
createPromise: function(aPromiseInit) {
return new this._window.Promise(aPromiseInit);
}
}

View File

@ -36,6 +36,7 @@ MOCHITEST_FILES = \
MOCHITEST_CHROME_FILES = \
test_bug715041.xul \
test_bug715041_removal.xul \
test_domrequesthelper.xul \
$(NULL)
include $(topsrcdir)/config/rules.mk

View File

@ -0,0 +1,124 @@
<?xml version="1.0"?>
<!--
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
<window title="DOMRequestHelper Test"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
onload="start();">
<title>DOMRequestHelper Test</title>
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
<script type="application/javascript">
<![CDATA[
Components.utils.import("resource://gre/modules/DOMRequestHelper.jsm");
function DummyHelperSubclass() {
this.initDOMRequestHelper(window, []);
}
DummyHelperSubclass.prototype = {
__proto__: DOMRequestIpcHelper.prototype
};
var dummy = new DummyHelperSubclass();
function createPromise() {
ok(Promise, "Promise object should exist");
var promise = dummy.createPromise(function(r) {
ok(r, "received PromiseResolver");
r.resolve(true);
});
ok(promise instanceof Promise, "returned a Promise");
promise.then(runTest);
}
function getResolver() {
var id;
var resolver;
var promise = dummy.createPromise(function(r) {
id = dummy.getPromiseResolverId(r);
resolver = r;
ok(typeof id === "string", "id should be string");
r.resolve(true);
}).then(function(unused) {
var r = dummy.getPromiseResolver(id);
ok(resolver === r, "get should succeed");
runTest();
});
}
function removeResolver() {
var id;
var promise = dummy.createPromise(function(r) {
id = dummy.getPromiseResolverId(r);
ok(typeof id === "string", "id should be string");
var resolver = dummy.getPromiseResolver(id);
ok(resolver === r, "resolver get should succeed");
r.resolve(true);
}).then(function(unused) {
dummy.removePromiseResolver(id);
var resolver = dummy.getPromiseResolver(id);
ok(resolver === undefined, "removeResolver: get should fail");
runTest();
});
}
function takeResolver() {
var id;
var resolver;
var promise = dummy.createPromise(function(r) {
id = dummy.getPromiseResolverId(r);
resolver = r;
ok(typeof id === "string", "id should be string");
var gotR = dummy.getPromiseResolver(id);
ok(gotR === r, "resolver get should succeed");
r.resolve(true);
}).then(function(unused) {
var r = dummy.takePromiseResolver(id);
ok(resolver === r, "take should succeed");
r = dummy.getPromiseResolver(id);
ok(r === undefined, "takeResolver: get should fail");
runTest();
});
}
var tests = [ createPromise,
getResolver,
removeResolver,
takeResolver,
];
function runTest() {
if (!tests.length) {
SimpleTest.finish();
return;
}
var test = tests.shift();
test();
}
function start() {
SimpleTest.waitForExplicitFinish();
runTest();
}
]]>
</script>
<body xmlns="http://www.w3.org/1999/xhtml">
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test"></pre>
</body>
</window>