GeckoViewClipboardPermission.sys.mjs (2907B)
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 const lazy = {}; 6 ChromeUtils.defineESModuleGetters(lazy, { 7 PromptUtils: "resource://gre/modules/PromptUtils.sys.mjs", 8 }); 9 10 import { GeckoViewUtils } from "resource://gre/modules/GeckoViewUtils.sys.mjs"; 11 12 const { debug } = GeckoViewUtils.initLogging("GeckoViewClipboardPermission"); 13 14 export var GeckoViewClipboardPermission = { 15 confirmUserPaste(aWindowContext) { 16 return new Promise((resolve, reject) => { 17 if (!aWindowContext) { 18 reject( 19 Components.Exception("Null window context.", Cr.NS_ERROR_INVALID_ARG) 20 ); 21 return; 22 } 23 24 const { document } = aWindowContext.browsingContext.topChromeWindow; 25 if (!document) { 26 reject( 27 Components.Exception( 28 "Unable to get chrome document.", 29 Cr.NS_ERROR_FAILURE 30 ) 31 ); 32 return; 33 } 34 35 if (this._pendingRequest) { 36 reject( 37 Components.Exception( 38 "There is an ongoing request.", 39 Cr.NS_ERROR_FAILURE 40 ) 41 ); 42 return; 43 } 44 45 this._pendingRequest = { resolve, reject }; 46 47 const mouseXInCSSPixels = {}; 48 const mouseYInCSSPixels = {}; 49 const windowUtils = document.ownerGlobal.windowUtils; 50 windowUtils.getLastOverWindowPointerLocationInCSSPixels( 51 mouseXInCSSPixels, 52 mouseYInCSSPixels 53 ); 54 const screenRect = windowUtils.toScreenRect( 55 mouseXInCSSPixels.value, 56 mouseYInCSSPixels.value, 57 0, 58 0 59 ); 60 61 debug`confirmUserPaste (${screenRect.x}, ${screenRect.y})`; 62 63 document.addEventListener("pointerdown", this); 64 document.ownerGlobal.WindowEventDispatcher.sendRequestForResult({ 65 type: "GeckoView:ClipboardPermissionRequest", 66 screenPoint: { 67 x: screenRect.x, 68 y: screenRect.y, 69 }, 70 }).then( 71 allowOrDeny => { 72 const propBag = lazy.PromptUtils.objectToPropBag({ ok: allowOrDeny }); 73 this._pendingRequest.resolve(propBag); 74 this._pendingRequest = null; 75 document.removeEventListener("pointerdown", this); 76 }, 77 error => { 78 debug`Permission error: ${error}`; 79 this._pendingRequest.reject(); 80 this._pendingRequest = null; 81 document.removeEventListener("pointerdown", this); 82 } 83 ); 84 }); 85 }, 86 87 // EventListener interface. 88 handleEvent(aEvent) { 89 debug`handleEvent: ${aEvent.type}`; 90 switch (aEvent.type) { 91 case "pointerdown": { 92 aEvent.target.ownerGlobal.WindowEventDispatcher.sendRequestForResult({ 93 type: "GeckoView:DismissClipboardPermissionRequest", 94 }); 95 break; 96 } 97 } 98 }, 99 };