ChromePushSubscription.sys.mjs (2503B)
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 5 /** `ChromePushSubscription` instances are passed to all subscription callbacks. */ 6 export class ChromePushSubscription { 7 #props; 8 9 constructor(props) { 10 this.#props = props; 11 } 12 13 QueryInterface = ChromeUtils.generateQI(["nsIPushSubscription"]); 14 15 /** The URL for sending messages to this subscription. */ 16 get endpoint() { 17 return this.#props.endpoint; 18 } 19 20 /** The last time a message was sent to this subscription. */ 21 get lastPush() { 22 return this.#props.lastPush; 23 } 24 25 /** The total number of messages sent to this subscription. */ 26 get pushCount() { 27 return this.#props.pushCount; 28 } 29 30 /** 31 * The number of remaining background messages that can be sent to this 32 * subscription, or -1 of the subscription is exempt from the quota. 33 */ 34 get quota() { 35 return this.#props.quota; 36 } 37 38 /** 39 * Indicates whether this subscription was created with the system principal. 40 * System subscriptions are exempt from the background message quota and 41 * permission checks. 42 */ 43 get isSystemSubscription() { 44 return !!this.#props.systemRecord; 45 } 46 47 /** The private key used to decrypt incoming push messages, in JWK format */ 48 get p256dhPrivateKey() { 49 return this.#props.p256dhPrivateKey; 50 } 51 52 /** 53 * Indicates whether this subscription is subject to the background message 54 * quota. 55 */ 56 quotaApplies() { 57 return this.quota >= 0; 58 } 59 60 /** 61 * Indicates whether this subscription exceeded the background message quota, 62 * or the user revoked the notification permission. The caller must request a 63 * new subscription to continue receiving push messages. 64 */ 65 isExpired() { 66 return this.quota === 0; 67 } 68 69 /** 70 * Returns a key for encrypting messages sent to this subscription. JS 71 * callers receive the key buffer as a return value, while C++ callers 72 * receive the key size and buffer as out parameters. 73 */ 74 getKey(name) { 75 switch (name) { 76 case "p256dh": 77 return this.#getRawKey(this.#props.p256dhKey); 78 79 case "auth": 80 return this.#getRawKey(this.#props.authenticationSecret); 81 82 case "appServer": 83 return this.#getRawKey(this.#props.appServerKey); 84 } 85 return []; 86 } 87 88 #getRawKey(key) { 89 if (!key) { 90 return []; 91 } 92 return new Uint8Array(key); 93 } 94 }