Bug 1234020: Part 2a - [webext] Return promises from the background page APIs. r=rpl

--HG--
extra : commitid : 6QaVA3XIUzC
extra : rebase_source : a769506ee88b7ac0e4b972c26750409adec5239c
extra : histedit_source : 1768cb7a91aa363ee2ea0d5094ad8e5a18eb9605
This commit is contained in:
Kris Maglione 2016-02-02 19:14:34 -08:00
Родитель c387030feb
Коммит f0a1600d9a
4 изменённых файлов: 28 добавлений и 7 удалений

Просмотреть файл

@ -20,6 +20,7 @@ function genericChecker() {
"tab": 0,
"popup": 0,
};
let background;
for (let i = 0; i < views.length; i++) {
let view = views[i];
browser.test.assertTrue(view.kind in counts, "view type is valid");
@ -27,9 +28,17 @@ function genericChecker() {
if (view.kind == "background") {
browser.test.assertTrue(view === browser.extension.getBackgroundPage(),
"background page is correct");
background = view;
}
}
if (background) {
browser.runtime.getBackgroundPage().then(view => {
browser.test.assertEq(background, view, "runtime.getBackgroundPage() is correct");
browser.test.sendMessage("counts", counts);
});
} else {
browser.test.sendMessage("counts", counts);
}
} else if (msg == kind + "-open-tab") {
browser.tabs.create({windowId: args[0], url: browser.runtime.getURL("tab.html")});
} else if (msg == kind + "-close-tab") {

Просмотреть файл

@ -212,30 +212,41 @@ class BaseContext {
* callback. May resolve to a `SpreadArgs` instance, in which case
* each element will be used as a separate argument.
*
* Unless the promise object belongs to the cloneScope global, its
* resolution value is cloned into cloneScope prior to calling the
* `callback` function or resolving the wrapped promise.
*
* @param {function} [callback] The callback function to wrap
*
* @returns {Promise|undefined} If callback is null, a promise object
* belonging to the target scope. Otherwise, undefined.
*/
wrapPromise(promise, callback = null) {
// Note: `promise instanceof this.cloneScope.Promise` returns true
// here even for promises that do not belong to the content scope.
let runSafe = runSafeSync.bind(null, this);
if (promise.constructor === this.cloneScope.Promise) {
runSafe = runSafeSyncWithoutClone;
}
if (callback) {
promise.then(
args => {
if (args instanceof SpreadArgs) {
runSafeSync(this, callback, ...args);
runSafe(callback, ...args);
} else {
runSafeSync(this, callback, args);
runSafe(callback, args);
}
},
error => {
this.withLastError(error, () => {
runSafeSync(this, callback);
runSafeSyncWithoutClone(callback);
});
});
} else {
return new this.cloneScope.Promise((resolve, reject) => {
promise.then(
value => { runSafeSync(this, resolve, value); },
value => { runSafe(resolve, value); },
value => {
if (!(value instanceof this.cloneScope.Error)) {
value = new this.cloneScope.Error(value.message);

Просмотреть файл

@ -133,8 +133,8 @@ extensions.registerSchemaAPI("extension", null, (extension, context) => {
},
runtime: {
getBackgroundPage: function(callback) {
runSafe(context, callback, backgroundPagesMap.get(extension).contentWindow);
getBackgroundPage() {
return context.cloneScope.Promise.resolve(backgroundPagesMap.get(extension).contentWindow);
},
},
};

Просмотреть файл

@ -114,6 +114,7 @@
"name": "getBackgroundPage",
"type": "function",
"description": "Retrieves the JavaScript 'window' object for the background page running inside the current extension/app. If the background page is an event page, the system will ensure it is loaded before calling the callback. If there is no background page, an error is set.",
"async": "callback",
"parameters": [
{
"type": "function",