tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_child_clipboard_restricted.js (2435B)


      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 add_task(async function () {
      5  // Create a new content tab to make sure the paste is cross-process.
      6  let tab = await BrowserTestUtils.openNewForegroundTab(
      7    gBrowser,
      8    "data:text/html,<br>"
      9  );
     10  let browser = tab.linkedBrowser;
     11 
     12  await SpecialPowers.spawn(browser, [], async function () {
     13    const trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(
     14      Ci.nsITransferable
     15    );
     16    trans.init(null);
     17 
     18    const string = Cc["@mozilla.org/supports-string;1"].createInstance(
     19      Ci.nsISupportsString
     20    );
     21    string.data = "blablabla";
     22 
     23    trans.addDataFlavor("text/unknown");
     24    trans.setTransferData("text/unknown", string);
     25 
     26    trans.addDataFlavor("text/plain");
     27    trans.setTransferData("text/plain", string);
     28 
     29    // Write to clipboard.
     30    Services.clipboard.setData(trans, null, Ci.nsIClipboard.kGlobalClipboard);
     31  });
     32 
     33  // Wait for known.
     34  for (var i = 0; i < 20; i++) {
     35    if (
     36      Services.clipboard.hasDataMatchingFlavors(
     37        ["text/plain"],
     38        Services.clipboard.kGlobalClipboard
     39      )
     40    ) {
     41      break;
     42    }
     43  }
     44 
     45  function readClipboard(flavor) {
     46    const trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(
     47      Ci.nsITransferable
     48    );
     49    trans.init(null);
     50    trans.addDataFlavor(flavor);
     51    Services.clipboard.getData(
     52      trans,
     53      Services.clipboard.kGlobalClipboard,
     54      SpecialPowers.wrap(window).browsingContext.currentWindowContext
     55    );
     56 
     57    let data = {};
     58    trans.getTransferData(flavor, data);
     59    return data.value.QueryInterface(Ci.nsISupportsString).data;
     60  }
     61 
     62  ok(
     63    Services.clipboard.hasDataMatchingFlavors(
     64      ["text/plain"],
     65      Services.clipboard.kGlobalClipboard
     66    ),
     67    "clipboard should have text/plain"
     68  );
     69 
     70  is(
     71    readClipboard("text/plain"),
     72    "blablabla",
     73    "matching string for text/plain"
     74  );
     75 
     76  ok(
     77    !Services.clipboard.hasDataMatchingFlavors(
     78      ["text/unknown"],
     79      Services.clipboard.kGlobalClipboard
     80    ),
     81    "clipboard should not have text/unknown"
     82  );
     83 
     84  let error = undefined;
     85  try {
     86    readClipboard("text/unknown");
     87  } catch (e) {
     88    error = e;
     89  }
     90  is(typeof error, "object", "reading text/unknown should fail");
     91 
     92  BrowserTestUtils.removeTab(tab);
     93 });