gecko-dev/dom/push/Push.jsm

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

289 строки
8.5 KiB
JavaScript
Исходник Обычный вид История

/* 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";
Bug 1514594: Part 3 - Change ChromeUtils.import API. *** Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8 This changes the behavior of ChromeUtils.import() to return an exports object, rather than a module global, in all cases except when `null` is passed as a second argument, and changes the default behavior not to pollute the global scope with the module's exports. Thus, the following code written for the old model: ChromeUtils.import("resource://gre/modules/Services.jsm"); is approximately the same as the following, in the new model: var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm"); Since the two behaviors are mutually incompatible, this patch will land with a scripted rewrite to update all existing callers to use the new model rather than the old. *** Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs This was done using the followng script: https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm *** Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8 Differential Revision: https://phabricator.services.mozilla.com/D16747 *** Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D16748 *** Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D16749 *** Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs *** Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D16750 --HG-- extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895 extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
2019-01-17 21:18:31 +03:00
const {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
const {DOMRequestIpcHelper} = ChromeUtils.import("resource://gre/modules/DOMRequestHelper.jsm");
XPCOMUtils.defineLazyGetter(this, "console", () => {
Bug 1514594: Part 3 - Change ChromeUtils.import API. *** Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8 This changes the behavior of ChromeUtils.import() to return an exports object, rather than a module global, in all cases except when `null` is passed as a second argument, and changes the default behavior not to pollute the global scope with the module's exports. Thus, the following code written for the old model: ChromeUtils.import("resource://gre/modules/Services.jsm"); is approximately the same as the following, in the new model: var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm"); Since the two behaviors are mutually incompatible, this patch will land with a scripted rewrite to update all existing callers to use the new model rather than the old. *** Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs This was done using the followng script: https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm *** Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8 Differential Revision: https://phabricator.services.mozilla.com/D16747 *** Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D16748 *** Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D16749 *** Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs *** Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D16750 --HG-- extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895 extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
2019-01-17 21:18:31 +03:00
let {ConsoleAPI} = ChromeUtils.import("resource://gre/modules/Console.jsm");
return new ConsoleAPI({
maxLogLevelPref: "dom.push.loglevel",
prefix: "Push",
});
});
XPCOMUtils.defineLazyServiceGetter(this, "PushService",
"@mozilla.org/push/Service;1", "nsIPushService");
const PUSH_CID = Components.ID("{cde1d019-fad8-4044-b141-65fb4fb7a245}");
/**
* The Push component runs in the child process and exposes the Push API
* to the web application. The PushService running in the parent process is the
* one actually performing all operations.
*/
function Push() {
console.debug("Push()");
}
Push.prototype = {
__proto__: DOMRequestIpcHelper.prototype,
contractID: "@mozilla.org/push/PushManager;1",
classID : PUSH_CID,
QueryInterface : ChromeUtils.generateQI([Ci.nsIDOMGlobalPropertyInitializer,
Ci.nsISupportsWeakReference,
Ci.nsIObserver]),
init: function(win) {
console.debug("init()");
this._window = win;
this.initDOMRequestHelper(win);
this._principal = win.document.nodePrincipal;
this._topLevelPrincipal = win.top.document.nodePrincipal;
},
__init: function(scope) {
this._scope = scope;
},
askPermission: function () {
console.debug("askPermission()");
return this.createPromise((resolve, reject) => {
let permissionDenied = () => {
reject(new this._window.DOMException(
"User denied permission to use the Push API.",
"NotAllowedError"
));
};
let permission = Ci.nsIPermissionManager.UNKNOWN_ACTION;
try {
permission = this._testPermission();
} catch (e) {
permissionDenied();
return;
}
if (permission == Ci.nsIPermissionManager.ALLOW_ACTION) {
resolve();
} else if (permission == Ci.nsIPermissionManager.DENY_ACTION) {
permissionDenied();
} else {
this._requestPermission(resolve, permissionDenied);
}
});
},
subscribe: function(options) {
console.debug("subscribe()", this._scope);
return this.askPermission().then(() =>
this.createPromise((resolve, reject) => {
let callback = new PushSubscriptionCallback(this, resolve, reject);
if (!options || options.applicationServerKey === null) {
PushService.subscribe(this._scope, this._principal, callback);
return;
}
let keyView = this._normalizeAppServerKey(options.applicationServerKey);
if (keyView.byteLength === 0) {
callback._rejectWithError(Cr.NS_ERROR_DOM_PUSH_INVALID_KEY_ERR);
return;
}
PushService.subscribeWithKey(this._scope, this._principal,
keyView.byteLength, keyView,
callback);
})
);
},
_normalizeAppServerKey: function(appServerKey) {
let key;
if (typeof appServerKey == "string") {
try {
key = Cu.cloneInto(ChromeUtils.base64URLDecode(appServerKey, {
padding: "reject",
}), this._window);
} catch (e) {
throw new this._window.DOMException(
"String contains an invalid character",
"InvalidCharacterError"
);
}
} else if (this._window.ArrayBuffer.isView(appServerKey)) {
key = appServerKey.buffer;
} else {
// `appServerKey` is an array buffer.
key = appServerKey;
}
return new this._window.Uint8Array(key);
},
getSubscription: function() {
console.debug("getSubscription()", this._scope);
return this.createPromise((resolve, reject) => {
let callback = new PushSubscriptionCallback(this, resolve, reject);
PushService.getSubscription(this._scope, this._principal, callback);
});
},
permissionState: function() {
console.debug("permissionState()", this._scope);
return this.createPromise((resolve, reject) => {
let permission = Ci.nsIPermissionManager.UNKNOWN_ACTION;
try {
permission = this._testPermission();
} catch(e) {
reject();
return;
}
let pushPermissionStatus = "prompt";
if (permission == Ci.nsIPermissionManager.ALLOW_ACTION) {
pushPermissionStatus = "granted";
} else if (permission == Ci.nsIPermissionManager.DENY_ACTION) {
pushPermissionStatus = "denied";
}
resolve(pushPermissionStatus);
});
},
_testPermission: function() {
let permission = Services.perms.testExactPermissionFromPrincipal(
this._principal, "desktop-notification");
if (permission == Ci.nsIPermissionManager.ALLOW_ACTION) {
return permission;
}
try {
if (Services.prefs.getBoolPref("dom.push.testing.ignorePermission")) {
permission = Ci.nsIPermissionManager.ALLOW_ACTION;
}
} catch (e) {}
return permission;
},
_requestPermission: function(allowCallback, cancelCallback) {
// Create an array with a single nsIContentPermissionType element.
let type = {
type: "desktop-notification",
options: [],
QueryInterface: ChromeUtils.generateQI([Ci.nsIContentPermissionType]),
};
let typeArray = Cc["@mozilla.org/array;1"].createInstance(Ci.nsIMutableArray);
typeArray.appendElement(type);
// create a nsIContentPermissionRequest
let request = {
types: typeArray,
principal: this._principal,
topLevelrincipal: this._topLevelPrincipal,
QueryInterface: ChromeUtils.generateQI([Ci.nsIContentPermissionRequest]),
allow: allowCallback,
cancel: cancelCallback,
window: this._window,
};
// Using askPermission from nsIDOMWindowUtils that takes care of the
// remoting if needed.
let windowUtils = this._window.windowUtils;
windowUtils.askPermission(request);
},
};
function PushSubscriptionCallback(pushManager, resolve, reject) {
this.pushManager = pushManager;
this.resolve = resolve;
this.reject = reject;
}
PushSubscriptionCallback.prototype = {
QueryInterface: ChromeUtils.generateQI([Ci.nsIPushSubscriptionCallback]),
onPushSubscription: function(ok, subscription) {
let {pushManager} = this;
if (!Components.isSuccessCode(ok)) {
this._rejectWithError(ok);
return;
}
if (!subscription) {
this.resolve(null);
return;
}
let p256dhKey = this._getKey(subscription, "p256dh");
let authSecret = this._getKey(subscription, "auth");
let options = {
endpoint: subscription.endpoint,
scope: pushManager._scope,
p256dhKey: p256dhKey,
authSecret: authSecret,
};
let appServerKey = this._getKey(subscription, "appServer");
if (appServerKey) {
// Avoid passing null keys to work around bug 1256449.
options.appServerKey = appServerKey;
}
let sub = new pushManager._window.PushSubscription(options);
this.resolve(sub);
},
_getKey: function(subscription, name) {
let outKeyLen = {};
let rawKey = Cu.cloneInto(subscription.getKey(name, outKeyLen),
this.pushManager._window);
if (!outKeyLen.value) {
return null;
}
let key = new this.pushManager._window.ArrayBuffer(outKeyLen.value);
let keyView = new this.pushManager._window.Uint8Array(key);
keyView.set(rawKey);
return key;
},
_rejectWithError: function(result) {
let error;
switch (result) {
case Cr.NS_ERROR_DOM_PUSH_INVALID_KEY_ERR:
error = new this.pushManager._window.DOMException(
"Invalid raw ECDSA P-256 public key.",
"InvalidAccessError"
);
break;
case Cr.NS_ERROR_DOM_PUSH_MISMATCHED_KEY_ERR:
error = new this.pushManager._window.DOMException(
"A subscription with a different application server key already exists.",
"InvalidStateError"
);
break;
default:
error = new this.pushManager._window.DOMException(
"Error retrieving push subscription.",
"AbortError"
);
}
this.reject(error);
},
};
var EXPORTED_SYMBOLS = ["Push"];