summaryrefslogtreecommitdiffstats
path: root/dom/ipc/tests/browser_child_clipboard_restricted.js
blob: cafed732d12470187ad0f22cc6137510b3fe0781 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
add_task(async function () {
  // Create a new content tab to make sure the paste is cross-process.
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "data:text/html,<br>"
  );
  let browser = tab.linkedBrowser;

  await SpecialPowers.spawn(browser, [], async function (arg) {
    const trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(
      Ci.nsITransferable
    );
    trans.init(null);

    const string = Cc["@mozilla.org/supports-string;1"].createInstance(
      Ci.nsISupportsString
    );
    string.data = "blablabla";

    trans.addDataFlavor("text/unknown");
    trans.setTransferData("text/unknown", string);

    trans.addDataFlavor("text/plain");
    trans.setTransferData("text/plain", string);

    // Write to clipboard.
    Services.clipboard.setData(trans, null, Ci.nsIClipboard.kGlobalClipboard);
  });

  // Wait for known.
  for (var i = 0; i < 20; i++) {
    if (
      Services.clipboard.hasDataMatchingFlavors(
        ["text/plain"],
        Services.clipboard.kGlobalClipboard
      )
    ) {
      break;
    }
  }

  function readClipboard(flavor) {
    const trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(
      Ci.nsITransferable
    );
    trans.init(null);
    trans.addDataFlavor(flavor);
    Services.clipboard.getData(trans, Services.clipboard.kGlobalClipboard);

    let data = {};
    trans.getTransferData(flavor, data);
    return data.value.QueryInterface(Ci.nsISupportsString).data;
  }

  ok(
    Services.clipboard.hasDataMatchingFlavors(
      ["text/plain"],
      Services.clipboard.kGlobalClipboard
    ),
    "clipboard should have text/plain"
  );

  is(
    readClipboard("text/plain"),
    "blablabla",
    "matching string for text/plain"
  );

  ok(
    !Services.clipboard.hasDataMatchingFlavors(
      ["text/unknown"],
      Services.clipboard.kGlobalClipboard
    ),
    "clipboard should not have text/unknown"
  );

  let error = undefined;
  try {
    readClipboard("text/unknown");
  } catch (e) {
    error = e;
  }
  is(typeof error, "object", "reading text/unknown should fail");

  BrowserTestUtils.removeTab(tab);
});