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