SelectionActionDelegateParent.sys.mjs (2372B)
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 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 import { GeckoViewActorParent } from "resource://gre/modules/GeckoViewActorParent.sys.mjs"; 6 7 export class SelectionActionDelegateParent extends GeckoViewActorParent { 8 respondTo = null; 9 actionId = null; 10 11 get rootActor() { 12 return this.browsingContext.top.currentWindowGlobal.getActor( 13 "SelectionActionDelegate" 14 ); 15 } 16 17 receiveMessage(aMessage) { 18 const { data, name } = aMessage; 19 switch (name) { 20 case "ShowSelectionAction": { 21 this.rootActor.showSelectionAction(this, data); 22 break; 23 } 24 25 case "HideSelectionAction": { 26 this.rootActor.hideSelectionAction(this, data.reason); 27 break; 28 } 29 30 case "GeckoView:ShowMagnifier": { 31 return this.eventDispatcher.sendRequest({ 32 ...data, 33 type: "GeckoView:ShowMagnifier", 34 }); 35 } 36 37 case "GeckoView:HideMagnifier": { 38 return this.eventDispatcher.sendRequest({ 39 type: "GeckoView:HideMagnifier", 40 }); 41 } 42 43 default: { 44 super.receiveMessage(aMessage); 45 } 46 } 47 48 return undefined; 49 } 50 51 hideSelectionAction(aRespondTo, reason) { 52 // Mark previous actions as stale. Don't do this for "invisibleselection" 53 // or "scroll" because previous actions should still be valid even after 54 // these events occur. 55 if (reason !== "invisibleselection" && reason !== "scroll") { 56 this.actionId = null; 57 } 58 59 this.eventDispatcher?.sendRequest({ 60 type: "GeckoView:HideSelectionAction", 61 reason, 62 }); 63 } 64 65 showSelectionAction(aRespondTo, aData) { 66 this.actionId = Services.uuid.generateUUID().toString(); 67 this.respondTo = aRespondTo; 68 69 this.eventDispatcher?.sendRequest({ 70 type: "GeckoView:ShowSelectionAction", 71 actionId: this.actionId, 72 ...aData, 73 }); 74 } 75 76 executeSelectionAction(aData) { 77 if (this.actionId === null || aData.actionId != this.actionId) { 78 warn`Stale response ${aData.id} ${aData.actionId}`; 79 return; 80 } 81 this.respondTo.sendAsyncMessage("ExecuteSelectionAction", aData); 82 } 83 } 84 85 const { debug, warn } = SelectionActionDelegateParent.initLogging( 86 "SelectionActionDelegate" 87 );