Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | "use strict"; |
michael@0 | 5 | |
michael@0 | 6 | require("sdk/clipboard"); |
michael@0 | 7 | |
michael@0 | 8 | const { Cc, Ci } = require("chrome"); |
michael@0 | 9 | |
michael@0 | 10 | const imageTools = Cc["@mozilla.org/image/tools;1"]. |
michael@0 | 11 | getService(Ci.imgITools); |
michael@0 | 12 | |
michael@0 | 13 | const io = Cc["@mozilla.org/network/io-service;1"]. |
michael@0 | 14 | getService(Ci.nsIIOService); |
michael@0 | 15 | |
michael@0 | 16 | const base64png = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYA" + |
michael@0 | 17 | "AABzenr0AAAASUlEQVRYhe3O0QkAIAwD0eyqe3Q993AQ3cBSUKpygfsNTy" + |
michael@0 | 18 | "N5ugbQpK0BAADgP0BRDWXWlwEAAAAAgPsA3rzDaAAAAHgPcGrpgAnzQ2FG" + |
michael@0 | 19 | "bWRR9AAAAABJRU5ErkJggg%3D%3D"; |
michael@0 | 20 | |
michael@0 | 21 | const base64jpeg = "data:image/jpeg;base64,%2F9j%2F4AAQSkZJRgABAQAAAQABAAD%2F" + |
michael@0 | 22 | "2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCg" + |
michael@0 | 23 | "sOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD%2F2wBDAQMDAwQDBAgEBAgQCw" + |
michael@0 | 24 | "kLEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQ" + |
michael@0 | 25 | "EBAQEBAQEBD%2FwAARCAAgACADAREAAhEBAxEB%2F8QAHwAAAQUBAQEBAQ" + |
michael@0 | 26 | "EAAAAAAAAAAAECAwQFBgcICQoL%2F8QAtRAAAgEDAwIEAwUFBAQAAAF9AQ" + |
michael@0 | 27 | "IDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRol" + |
michael@0 | 28 | "JicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eX" + |
michael@0 | 29 | "qDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJ" + |
michael@0 | 30 | "ytLT1NXW19jZ2uHi4%2BTl5ufo6erx8vP09fb3%2BPn6%2F8QAHwEAAwEB" + |
michael@0 | 31 | "AQEBAQEBAQAAAAAAAAECAwQFBgcICQoL%2F8QAtREAAgECBAQDBAcFBAQA" + |
michael@0 | 32 | "AQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNO" + |
michael@0 | 33 | "El8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0" + |
michael@0 | 34 | "dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6ws" + |
michael@0 | 35 | "PExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3%2BPn6%2F9oADAMB" + |
michael@0 | 36 | "AAIRAxEAPwD5Kr8kP9CwoA5f4m%2F8iRqX%2FbH%2FANHJXr5F%2FwAjCn" + |
michael@0 | 37 | "8%2F%2FSWfnnir%2FwAkji%2F%2B4f8A6dgeD1%2BiH8bn1BX5If6FmFqW" + |
michael@0 | 38 | "pXtveyQwzbUXGBtB7D2r9l4U4UyjMsoo4rFUeacua75pLaUktFJLZH5NxN" + |
michael@0 | 39 | "xNmmX5pVw2Gq8sI8tlyxe8U3q03uzD8S3dxqOi3NneSeZDJs3LgDOHBHI5" + |
michael@0 | 40 | "6gV%2BkcG%2BH%2FDmJzuhSq4e8XzfbqfyS%2FvH5rx1xTm2MyDEUa1W8X" + |
michael@0 | 41 | "yXXLFbTi%2BkThv7B0r%2FAJ9f%2FH2%2Fxr90%2FwCIVcI%2F9An%2FAJ" + |
michael@0 | 42 | "Uq%2FwDyZ%2FO%2F16v%2FADfgv8j0r%2FhZvgj%2FAKDf%2FktN%2FwDE" + |
michael@0 | 43 | "V%2Fnr%2FYWYf8%2B%2Fxj%2Fmf3R%2FxFXhH%2FoL%2FwDKdX%2F5Azrv" + |
michael@0 | 44 | "xLouo3D3lne%2BZDJja3luM4GDwRnqDX9LeH%2FBud4nhzD1aVC8Xz%2Fa" + |
michael@0 | 45 | "h%2Fz8l%2FePx%2FinjrIMZm1WtRxF4vls%2BSa2jFdYlDUdRsp7OSKKbc" + |
michael@0 | 46 | "7YwNpHce1fqfCvCub5bm9HFYqjywjzXfNF7xklopN7s%2BC4l4lyvMMrq4" + |
michael@0 | 47 | "fD1bzfLZcsltJPqktkYlfsZ%2BUnBV%2FnufVnXaD%2FAMgqD%2FgX%2Fo" + |
michael@0 | 48 | "Rr%2BxvCr%2FkkcJ%2F3E%2F8ATsz5%2FHfx5fL8kX6%2FQjkCgD%2F%2F" + |
michael@0 | 49 | "2Q%3D%3D"; |
michael@0 | 50 | |
michael@0 | 51 | const canvasHTML = "data:text/html," + encodeURIComponent( |
michael@0 | 52 | "<html>\ |
michael@0 | 53 | <body>\ |
michael@0 | 54 | <canvas width='32' height='32'></canvas>\ |
michael@0 | 55 | </body>\ |
michael@0 | 56 | </html>" |
michael@0 | 57 | ); |
michael@0 | 58 | |
michael@0 | 59 | function comparePixelImages(imageA, imageB, callback) { |
michael@0 | 60 | let tabs = require("sdk/tabs"); |
michael@0 | 61 | |
michael@0 | 62 | tabs.open({ |
michael@0 | 63 | url: canvasHTML, |
michael@0 | 64 | |
michael@0 | 65 | onReady: function onReady(tab) { |
michael@0 | 66 | let worker = tab.attach({ |
michael@0 | 67 | contentScript: "new " + function() { |
michael@0 | 68 | let canvas = document.querySelector("canvas"); |
michael@0 | 69 | let context = canvas.getContext("2d"); |
michael@0 | 70 | |
michael@0 | 71 | self.port.on("draw-image", function(imageURI) { |
michael@0 | 72 | let img = new Image(); |
michael@0 | 73 | |
michael@0 | 74 | img.onload = function() { |
michael@0 | 75 | context.drawImage(this, 0, 0); |
michael@0 | 76 | |
michael@0 | 77 | let pixels = Array.join(context.getImageData(0, 0, 32, 32).data); |
michael@0 | 78 | self.port.emit("image-pixels", pixels); |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | img.src = imageURI; |
michael@0 | 82 | }); |
michael@0 | 83 | } |
michael@0 | 84 | }); |
michael@0 | 85 | |
michael@0 | 86 | let compared = ""; |
michael@0 | 87 | |
michael@0 | 88 | worker.port.on("image-pixels", function (pixels) { |
michael@0 | 89 | if (!compared) { |
michael@0 | 90 | compared = pixels; |
michael@0 | 91 | this.emit("draw-image", imageB); |
michael@0 | 92 | } else { |
michael@0 | 93 | callback(compared === pixels); |
michael@0 | 94 | tab.close() |
michael@0 | 95 | } |
michael@0 | 96 | }); |
michael@0 | 97 | |
michael@0 | 98 | worker.port.emit("draw-image", imageA); |
michael@0 | 99 | } |
michael@0 | 100 | }); |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | |
michael@0 | 104 | // Test the typical use case, setting & getting with no flavors specified |
michael@0 | 105 | exports["test With No Flavor"] = function(assert) { |
michael@0 | 106 | var contents = "hello there"; |
michael@0 | 107 | var flavor = "text"; |
michael@0 | 108 | var fullFlavor = "text/unicode"; |
michael@0 | 109 | var clip = require("sdk/clipboard"); |
michael@0 | 110 | |
michael@0 | 111 | // Confirm we set the clipboard |
michael@0 | 112 | assert.ok(clip.set(contents)); |
michael@0 | 113 | |
michael@0 | 114 | // Confirm flavor is set |
michael@0 | 115 | assert.equal(clip.currentFlavors[0], flavor); |
michael@0 | 116 | |
michael@0 | 117 | // Confirm we set the clipboard |
michael@0 | 118 | assert.equal(clip.get(), contents); |
michael@0 | 119 | |
michael@0 | 120 | // Confirm we can get the clipboard using the flavor |
michael@0 | 121 | assert.equal(clip.get(flavor), contents); |
michael@0 | 122 | |
michael@0 | 123 | // Confirm we can still get the clipboard using the full flavor |
michael@0 | 124 | assert.equal(clip.get(fullFlavor), contents); |
michael@0 | 125 | }; |
michael@0 | 126 | |
michael@0 | 127 | // Test the slightly less common case where we specify the flavor |
michael@0 | 128 | exports["test With Flavor"] = function(assert) { |
michael@0 | 129 | var contents = "<b>hello there</b>"; |
michael@0 | 130 | var contentsText = "hello there"; |
michael@0 | 131 | var flavor = "html"; |
michael@0 | 132 | var fullFlavor = "text/html"; |
michael@0 | 133 | var unicodeFlavor = "text"; |
michael@0 | 134 | var unicodeFullFlavor = "text/unicode"; |
michael@0 | 135 | var clip = require("sdk/clipboard"); |
michael@0 | 136 | |
michael@0 | 137 | assert.ok(clip.set(contents, flavor)); |
michael@0 | 138 | |
michael@0 | 139 | assert.equal(clip.currentFlavors[0], unicodeFlavor); |
michael@0 | 140 | assert.equal(clip.currentFlavors[1], flavor); |
michael@0 | 141 | assert.equal(clip.get(), contentsText); |
michael@0 | 142 | assert.equal(clip.get(flavor), contents); |
michael@0 | 143 | assert.equal(clip.get(fullFlavor), contents); |
michael@0 | 144 | assert.equal(clip.get(unicodeFlavor), contentsText); |
michael@0 | 145 | assert.equal(clip.get(unicodeFullFlavor), contentsText); |
michael@0 | 146 | }; |
michael@0 | 147 | |
michael@0 | 148 | // Test that the typical case still works when we specify the flavor to set |
michael@0 | 149 | exports["test With Redundant Flavor"] = function(assert) { |
michael@0 | 150 | var contents = "<b>hello there</b>"; |
michael@0 | 151 | var flavor = "text"; |
michael@0 | 152 | var fullFlavor = "text/unicode"; |
michael@0 | 153 | var clip = require("sdk/clipboard"); |
michael@0 | 154 | |
michael@0 | 155 | assert.ok(clip.set(contents, flavor)); |
michael@0 | 156 | assert.equal(clip.currentFlavors[0], flavor); |
michael@0 | 157 | assert.equal(clip.get(), contents); |
michael@0 | 158 | assert.equal(clip.get(flavor), contents); |
michael@0 | 159 | assert.equal(clip.get(fullFlavor), contents); |
michael@0 | 160 | }; |
michael@0 | 161 | |
michael@0 | 162 | exports["test Not In Flavor"] = function(assert) { |
michael@0 | 163 | var contents = "hello there"; |
michael@0 | 164 | var flavor = "html"; |
michael@0 | 165 | var clip = require("sdk/clipboard"); |
michael@0 | 166 | |
michael@0 | 167 | assert.ok(clip.set(contents)); |
michael@0 | 168 | // If there's nothing on the clipboard with this flavor, should return null |
michael@0 | 169 | assert.equal(clip.get(flavor), null); |
michael@0 | 170 | }; |
michael@0 | 171 | |
michael@0 | 172 | exports["test Set Image"] = function(assert) { |
michael@0 | 173 | var clip = require("sdk/clipboard"); |
michael@0 | 174 | var flavor = "image"; |
michael@0 | 175 | var fullFlavor = "image/png"; |
michael@0 | 176 | |
michael@0 | 177 | assert.ok(clip.set(base64png, flavor), "clipboard set"); |
michael@0 | 178 | assert.equal(clip.currentFlavors[0], flavor, "flavor is set"); |
michael@0 | 179 | }; |
michael@0 | 180 | |
michael@0 | 181 | exports["test Get Image"] = function(assert, done) { |
michael@0 | 182 | var clip = require("sdk/clipboard"); |
michael@0 | 183 | |
michael@0 | 184 | clip.set(base64png, "image"); |
michael@0 | 185 | |
michael@0 | 186 | var contents = clip.get(); |
michael@0 | 187 | |
michael@0 | 188 | comparePixelImages(base64png, contents, function (areEquals) { |
michael@0 | 189 | assert.ok(areEquals, |
michael@0 | 190 | "Image gets from clipboard equals to image sets to the clipboard"); |
michael@0 | 191 | |
michael@0 | 192 | done(); |
michael@0 | 193 | }); |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | exports["test Set Image Type Not Supported"] = function(assert) { |
michael@0 | 197 | var clip = require("sdk/clipboard"); |
michael@0 | 198 | var flavor = "image"; |
michael@0 | 199 | |
michael@0 | 200 | assert.throws(function () { |
michael@0 | 201 | clip.set(base64jpeg, flavor); |
michael@0 | 202 | }, "Invalid flavor for image/jpeg"); |
michael@0 | 203 | |
michael@0 | 204 | }; |
michael@0 | 205 | |
michael@0 | 206 | // Notice that `imageTools.decodeImageData`, used by `clipboard.set` method for |
michael@0 | 207 | // images, write directly to the javascript console the error in case the image |
michael@0 | 208 | // is corrupt, even if the error is catched. |
michael@0 | 209 | // |
michael@0 | 210 | // See: http://mxr.mozilla.org/mozilla-central/source/image/src/Decoder.cpp#136 |
michael@0 | 211 | exports["test Set Image Type Wrong Data"] = function(assert) { |
michael@0 | 212 | var clip = require("sdk/clipboard"); |
michael@0 | 213 | var flavor = "image"; |
michael@0 | 214 | |
michael@0 | 215 | var wrongPNG = "data:image/png" + base64jpeg.substr(15); |
michael@0 | 216 | |
michael@0 | 217 | assert.throws(function () { |
michael@0 | 218 | clip.set(wrongPNG, flavor); |
michael@0 | 219 | }, "Unable to decode data given in a valid image."); |
michael@0 | 220 | }; |
michael@0 | 221 | |
michael@0 | 222 | require("test").run(exports) |