1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
6 const Cc = SpecialPowers.Cc;
7 const Ci = SpecialPowers.Ci;
8 const Cr = SpecialPowers.Cr;
9 const clipboard = SpecialPowers.Services.clipboard;
10 const clipboardTypes = [
11 clipboard.kGlobalClipboard,
12 clipboard.kSelectionClipboard,
13 clipboard.kFindClipboard,
14 clipboard.kSelectionCache,
17 function emptyClipboardData(aType) {
18 // XXX gtk doesn't support emptying clipboard data which is stored from
19 // other application (bug 1853884). As a workaround, we set dummy data
20 // to the clipboard first to ensure the subsequent emptyClipboard call
22 if (navigator.platform.includes("Linux")) {
23 writeStringToClipboard("foo", "text/plain", aType);
26 clipboard.emptyClipboard(aType);
29 function cleanupAllClipboard() {
30 clipboardTypes.forEach(function (type) {
31 if (clipboard.isClipboardTypeSupported(type)) {
32 info(`cleanup clipboard ${type}`);
33 emptyClipboardData(type);
38 function generateRandomString() {
39 return "random number: " + Math.random();
42 function generateNewTransferable(aFlavor, aStr) {
43 let trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(
47 trans.addDataFlavor(aFlavor);
49 let supportsStr = Cc["@mozilla.org/supports-string;1"].createInstance(
52 supportsStr.data = aStr;
53 trans.setTransferData(aFlavor, supportsStr);
58 function addStringToTransferable(aFlavor, aStr, aTrans) {
59 aTrans.addDataFlavor(aFlavor);
61 let supportsStr = Cc["@mozilla.org/supports-string;1"].createInstance(
64 supportsStr.data = aStr;
65 aTrans.setTransferData(aFlavor, supportsStr);
68 function updateStringToTransferable(aFlavor, aStr, aTrans) {
69 let supportsStr = Cc["@mozilla.org/supports-string;1"].createInstance(
72 supportsStr.data = aStr;
73 aTrans.setTransferData(aFlavor, supportsStr);
76 function writeStringToClipboard(
80 aClipboardOwner = null,
83 let trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(
87 trans.addDataFlavor(aFlavor);
89 let supportsStr = Cc["@mozilla.org/supports-string;1"].createInstance(
92 supportsStr.data = aStr;
93 trans.setTransferData(aFlavor, supportsStr);
96 let request = clipboard.asyncSetData(aClipboardType);
97 request.setData(trans, aClipboardOwner);
101 clipboard.setData(trans, aClipboardOwner, aClipboardType);
102 // XXX gtk doesn't support get empty text data from clipboard, bug 1852983.
103 if (aStr == "" && navigator.platform.includes("Linux")) {
105 getClipboardData(aFlavor, aClipboardType),
107 `Should get empty string on clipboard type ${aClipboardType}`
111 getClipboardData(aFlavor, aClipboardType),
112 // On Windows, widget adds extra data into HTML clipboard.
113 aFlavor == "text/html" && navigator.platform.includes("Win")
114 ? `<html><body>\n<!--StartFragment-->${aStr}<!--EndFragment-->\n</body>\n</html>`
116 "ensure clipboard data is set"
121 function writeRandomStringToClipboard(
124 aClipboardOwner = null,
127 let randomString = generateRandomString();
128 writeStringToClipboard(
138 function getClipboardData(aFlavor, aClipboardType) {
139 var trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(
143 trans.addDataFlavor(aFlavor);
147 SpecialPowers.wrap(window).browsingContext.currentWindowContext
151 var data = SpecialPowers.createBlankObject();
152 trans.getTransferData(aFlavor, data);
153 return data.value.QueryInterface(SpecialPowers.Ci.nsISupportsString).data;
155 // If the clipboard is empty getTransferData will throw.
160 function getClipboardDataSnapshotSync(aClipboardType) {
161 return clipboard.getDataSnapshotSync(
162 ["text/plain", "text/html", "image/png"],
167 function asyncGetClipboardData(
169 aFormats = ["text/plain", "text/html", "image/png"]
171 return new Promise((resolve, reject) => {
173 clipboard.asyncGetData(
177 SpecialPowers.Services.scriptSecurityManager.getSystemPrincipal(),
179 QueryInterface: SpecialPowers.ChromeUtils.generateQI([
180 "nsIAsyncClipboardGetCallback",
182 // nsIAsyncClipboardGetCallback
183 onSuccess: SpecialPowers.wrapCallback(function (
184 aAsyncGetClipboardData
186 resolve(aAsyncGetClipboardData);
188 onError: SpecialPowers.wrapCallback(function (aResult) {
194 ok(false, `asyncGetData should not throw`);
200 function asyncClipboardRequestGetData(aRequest, aFlavor, aThrows = false) {
201 return new Promise((resolve, reject) => {
202 var trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(
206 trans.addDataFlavor(aFlavor);
208 aRequest.getData(trans, aResult => {
209 if (aResult != Cr.NS_OK) {
215 var data = SpecialPowers.createBlankObject();
216 trans.getTransferData(aFlavor, data);
217 resolve(data.value.QueryInterface(Ci.nsISupportsString).data);
219 // XXX: should widget set empty string to transferable when there no
220 // data in system clipboard?
226 `nsIAsyncGetClipboardData.getData should ${
227 aThrows ? "throw" : "success"
233 `nsIAsyncGetClipboardData.getData should ${
234 aThrows ? "throw" : "success"