toolkit/components/places/ColorAnalyzer.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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
michael@0 5 "use strict";
michael@0 6
michael@0 7 const Ci = Components.interfaces;
michael@0 8 const Cc = Components.classes;
michael@0 9 const Cu = Components.utils;
michael@0 10
michael@0 11 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 12
michael@0 13 const XHTML_NS = "http://www.w3.org/1999/xhtml";
michael@0 14 const MAXIMUM_PIXELS = Math.pow(144, 2);
michael@0 15
michael@0 16 function ColorAnalyzer() {
michael@0 17 // a queue of callbacks for each job we give to the worker
michael@0 18 this.callbacks = [];
michael@0 19
michael@0 20 this.hiddenWindowDoc = Cc["@mozilla.org/appshell/appShellService;1"].
michael@0 21 getService(Ci.nsIAppShellService).
michael@0 22 hiddenDOMWindow.document;
michael@0 23
michael@0 24 this.worker = new ChromeWorker("resource://gre/modules/ColorAnalyzer_worker.js");
michael@0 25 this.worker.onmessage = this.onWorkerMessage.bind(this);
michael@0 26 this.worker.onerror = this.onWorkerError.bind(this);
michael@0 27 }
michael@0 28
michael@0 29 ColorAnalyzer.prototype = {
michael@0 30 findRepresentativeColor: function ColorAnalyzer_frc(imageURI, callback) {
michael@0 31 function cleanup() {
michael@0 32 image.removeEventListener("load", loadListener);
michael@0 33 image.removeEventListener("error", errorListener);
michael@0 34 }
michael@0 35 let image = this.hiddenWindowDoc.createElementNS(XHTML_NS, "img");
michael@0 36 let loadListener = this.onImageLoad.bind(this, image, callback, cleanup);
michael@0 37 let errorListener = this.onImageError.bind(this, image, callback, cleanup);
michael@0 38 image.addEventListener("load", loadListener);
michael@0 39 image.addEventListener("error", errorListener);
michael@0 40 image.src = imageURI.spec;
michael@0 41 },
michael@0 42
michael@0 43 onImageLoad: function ColorAnalyzer_onImageLoad(image, callback, cleanup) {
michael@0 44 if (image.naturalWidth * image.naturalHeight > MAXIMUM_PIXELS) {
michael@0 45 // this will probably take too long to process - fail
michael@0 46 callback.onComplete(false);
michael@0 47 } else {
michael@0 48 let canvas = this.hiddenWindowDoc.createElementNS(XHTML_NS, "canvas");
michael@0 49 canvas.width = image.naturalWidth;
michael@0 50 canvas.height = image.naturalHeight;
michael@0 51 let ctx = canvas.getContext("2d");
michael@0 52 ctx.drawImage(image, 0, 0);
michael@0 53 this.startJob(ctx.getImageData(0, 0, canvas.width, canvas.height),
michael@0 54 callback);
michael@0 55 }
michael@0 56 cleanup();
michael@0 57 },
michael@0 58
michael@0 59 onImageError: function ColorAnalyzer_onImageError(image, callback, cleanup) {
michael@0 60 Cu.reportError("ColorAnalyzer: image at " + image.src + " didn't load");
michael@0 61 callback.onComplete(false);
michael@0 62 cleanup();
michael@0 63 },
michael@0 64
michael@0 65 startJob: function ColorAnalyzer_startJob(imageData, callback) {
michael@0 66 this.callbacks.push(callback);
michael@0 67 this.worker.postMessage({ imageData: imageData, maxColors: 1 });
michael@0 68 },
michael@0 69
michael@0 70 onWorkerMessage: function ColorAnalyzer_onWorkerMessage(event) {
michael@0 71 // colors can be empty on failure
michael@0 72 if (event.data.colors.length < 1) {
michael@0 73 this.callbacks.shift().onComplete(false);
michael@0 74 } else {
michael@0 75 this.callbacks.shift().onComplete(true, event.data.colors[0]);
michael@0 76 }
michael@0 77 },
michael@0 78
michael@0 79 onWorkerError: function ColorAnalyzer_onWorkerError(error) {
michael@0 80 // this shouldn't happen, but just in case
michael@0 81 error.preventDefault();
michael@0 82 Cu.reportError("ColorAnalyzer worker: " + error.message);
michael@0 83 this.callbacks.shift().onComplete(false);
michael@0 84 },
michael@0 85
michael@0 86 classID: Components.ID("{d056186c-28a0-494e-aacc-9e433772b143}"),
michael@0 87 QueryInterface: XPCOMUtils.generateQI([Ci.mozIColorAnalyzer])
michael@0 88 };
michael@0 89
michael@0 90 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ColorAnalyzer]);

mercurial