GeckoViewPushController.sys.mjs (1876B)
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 import { GeckoViewUtils } from "resource://gre/modules/GeckoViewUtils.sys.mjs"; 6 import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs"; 7 8 const lazy = {}; 9 10 XPCOMUtils.defineLazyServiceGetter( 11 lazy, 12 "PushNotifier", 13 "@mozilla.org/push/Notifier;1", 14 Ci.nsIPushNotifier 15 ); 16 17 const { debug, warn } = GeckoViewUtils.initLogging("GeckoViewPushController"); 18 19 function createScopeAndPrincipal(scopeAndAttrs) { 20 const principal = 21 Services.scriptSecurityManager.createContentPrincipalFromOrigin( 22 scopeAndAttrs 23 ); 24 const scope = principal.URI.spec; 25 26 return [scope, principal]; 27 } 28 29 export const GeckoViewPushController = { 30 onEvent(aEvent, aData) { 31 debug`onEvent ${aEvent} ${aData}`; 32 33 switch (aEvent) { 34 case "GeckoView:PushEvent": { 35 const { scope, data } = aData; 36 37 const [url, principal] = createScopeAndPrincipal(scope); 38 39 if ( 40 Services.perms.testPermissionFromPrincipal( 41 principal, 42 "desktop-notification" 43 ) != Services.perms.ALLOW_ACTION 44 ) { 45 return; 46 } 47 48 if (!data) { 49 lazy.PushNotifier.notifyPush(url, principal, ""); 50 return; 51 } 52 53 const payload = new Uint8Array( 54 ChromeUtils.base64URLDecode(data, { padding: "ignore" }) 55 ); 56 57 lazy.PushNotifier.notifyPushWithData(url, principal, "", payload); 58 break; 59 } 60 case "GeckoView:PushSubscriptionChanged": { 61 const { scope } = aData; 62 63 const [url, principal] = createScopeAndPrincipal(scope); 64 65 lazy.PushNotifier.notifySubscriptionChange(url, principal); 66 break; 67 } 68 } 69 }, 70 };