Bug 1378820 - remove usage of sdk/clipboard in devtools clipboard helper;r=gl
authorJulian Descottes <jdescottes@mozilla.com>
Mon, 31 Jul 2017 14:02:37 +0200
changeset 420712 0e724df2d505714cb63f3bd32e3b29f95f9c546e
parent 420711 992636ea8646c921c5cb59c86b7287a6cd746143
child 420713 5f2af8ad61397dfaa61ede3cc1334d85f9be057d
push id7566
push usermtabara@mozilla.com
push dateWed, 02 Aug 2017 08:25:16 +0000
treeherdermozilla-beta@86913f512c3c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgl
bugs1378820
milestone56.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1378820 - remove usage of sdk/clipboard in devtools clipboard helper;r=gl Remove previous helper APIs: - currentFlavors() - getData() The only consumer for both APIs was in devtools/cient/inspector/inspector. The logic was: - call currentFlavors() - if clipboard contains: - "text/unicode" or - "text/html" but not "image/png" - then call getData() But actually calling getData() without any argument means that unless "image/png" flavor is available, we will always use the "text/unicode" flavor. (see current code for sdk helper: http://searchfox.org/mozilla-central/rev/09c065976fd4f18d4ad764d7cb4bbc684bf56714/addon-sdk/source/lib/sdk/clipboard.js#204-209) If the text/unicode flavor was available at the same time as image/png, the code would have returned the image data, which is probably not what we expect in the context of the inspector. I think we should explicitly request for text/unicode. So this patch introduces a new getText() api in the devtools clipboard helper, which is a simplified version of the sdk get() helper. We could always extend it to also support text/html. MozReview-Commit-ID: E3JVj9boVKe
devtools/client/inspector/inspector.js
devtools/shared/platform/chrome/clipboard.js
devtools/shared/platform/content/clipboard.js
--- a/devtools/client/inspector/inspector.js
+++ b/devtools/client/inspector/inspector.js
@@ -1044,23 +1044,19 @@ Inspector.prototype = {
     return this._panelDestroyer;
   },
 
   /**
    * Returns the clipboard content if it is appropriate for pasting
    * into the current node's outer HTML, otherwise returns null.
    */
   _getClipboardContentForPaste: function () {
-    let flavors = clipboardHelper.getCurrentFlavors();
-    if (flavors.indexOf("text") != -1 ||
-        (flavors.indexOf("html") != -1 && flavors.indexOf("image") == -1)) {
-      let content = clipboardHelper.getData();
-      if (content && content.trim().length > 0) {
-        return content;
-      }
+    let content = clipboardHelper.getText();
+    if (content && content.trim().length > 0) {
+      return content;
     }
     return null;
   },
 
   _onContextMenu: function (e) {
     e.preventDefault();
     this._openMenu({
       screenX: e.screenX,
--- a/devtools/shared/platform/chrome/clipboard.js
+++ b/devtools/shared/platform/chrome/clipboard.js
@@ -4,25 +4,58 @@
 
 // Helpers for clipboard handling.
 
 "use strict";
 
 const {Cc, Ci} = require("chrome");
 const clipboardHelper = Cc["@mozilla.org/widget/clipboardhelper;1"]
       .getService(Ci.nsIClipboardHelper);
-var clipboard = require("sdk/clipboard");
+const clipboardService = Cc["@mozilla.org/widget/clipboard;1"]
+      .getService(Ci.nsIClipboard);
 
 function copyString(string) {
   clipboardHelper.copyString(string);
 }
 
-function getCurrentFlavors() {
-  return clipboard.currentFlavors;
-}
+/**
+ * Retrieve the current clipboard data matching the flavor "text/unicode".
+ *
+ * @return {String} Clipboard text content, null if no text clipboard data is available.
+ */
+function getText() {
+  let flavor = "text/unicode";
+
+  let xferable = Cc["@mozilla.org/widget/transferable;1"]
+                 .createInstance(Ci.nsITransferable);
+
+  if (!xferable) {
+    throw new Error("Couldn't get the clipboard data due to an internal error " +
+                    "(couldn't create a Transferable object).");
+  }
+
+  xferable.init(null);
+  xferable.addDataFlavor(flavor);
 
-function getData() {
-  return clipboard.get();
+  // Get the data into our transferable.
+  clipboardService.getData(
+    xferable,
+    clipboardService.kGlobalClipboard
+  );
+
+  let data = {};
+  try {
+    xferable.getTransferData(flavor, data, {});
+  } catch (e) {
+    // Clipboard doesn't contain data in flavor, return null.
+    return null;
+  }
+
+  // There's no data available, return.
+  if (!data.value) {
+    return null;
+  }
+
+  return data.value.QueryInterface(Ci.nsISupportsString).data;
 }
 
 exports.copyString = copyString;
-exports.getCurrentFlavors = getCurrentFlavors;
-exports.getData = getData;
+exports.getText = getText;
--- a/devtools/shared/platform/content/clipboard.js
+++ b/devtools/shared/platform/content/clipboard.js
@@ -14,21 +14,15 @@ function copyString(string) {
     e.preventDefault();
   };
 
   document.addEventListener("copy", doCopy);
   document.execCommand("copy", false, null);
   document.removeEventListener("copy", doCopy);
 }
 
-function getCurrentFlavors() {
-  // See bug 1295692.
-  return [];
-}
-
-function getData() {
+function getText() {
   // See bug 1295692.
   return null;
 }
 
 exports.copyString = copyString;
-exports.getCurrentFlavors = getCurrentFlavors;
-exports.getData = getData;
+exports.getText = getText;