GeckoViewPrompterChild.sys.mjs (2437B)
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 { GeckoViewActorChild } from "resource://gre/modules/GeckoViewActorChild.sys.mjs"; 6 7 export class GeckoViewPrompterChild extends GeckoViewActorChild { 8 constructor() { 9 super(); 10 this._prompts = new Map(); 11 } 12 13 dismissPrompt(prompt) { 14 this.sendAsyncMessage("GeckoView:Prompt:Dismiss", { 15 id: prompt.id, 16 }); 17 this.unregisterPrompt(prompt); 18 } 19 20 updatePrompt(message) { 21 this.sendAsyncMessage("GeckoView:Prompt:Update", { 22 prompt: message, 23 }); 24 } 25 26 unregisterPrompt(prompt) { 27 this._prompts.delete(prompt.id); 28 this.sendAsyncMessage("UnregisterPrompt", { 29 id: prompt.id, 30 }); 31 } 32 33 prompt(prompt, message) { 34 this._prompts.set(prompt.id, prompt); 35 this.sendAsyncMessage("RegisterPrompt", { 36 id: prompt.id, 37 promptType: prompt.getPromptType(), 38 }); 39 // We intentionally do not await here as we want to fire NotifyPromptShow 40 // immediately rather than waiting until the user accepts/dismisses the 41 // prompt. 42 const result = this.sendQuery("GeckoView:Prompt", { 43 prompt: message, 44 }); 45 this.sendAsyncMessage("NotifyPromptShow", { 46 id: prompt.id, 47 }); 48 return result; 49 } 50 51 /** 52 * Handles the message coming from GeckoViewPrompterParent. 53 * 54 * @param {string} message.name The subject of the message. 55 * @param {object} message.data The data of the message. 56 */ 57 async receiveMessage({ name, data }) { 58 const prompt = this._prompts.get(data.id); 59 if (!prompt) { 60 // Unknown prompt, probably for a different child actor. 61 return; 62 } 63 switch (name) { 64 case "GetPromptText": { 65 // eslint-disable-next-line consistent-return 66 return prompt.getPromptText(); 67 } 68 case "GetInputText": { 69 // eslint-disable-next-line consistent-return 70 return prompt.getInputText(); 71 } 72 case "SetInputText": { 73 prompt.setInputText(data.text); 74 break; 75 } 76 case "AcceptPrompt": { 77 prompt.accept(); 78 break; 79 } 80 case "DismissPrompt": { 81 prompt.dismiss(); 82 break; 83 } 84 default: { 85 break; 86 } 87 } 88 } 89 } 90 91 const { debug, warn } = GeckoViewPrompterChild.initLogging("Prompter");