summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/ColorAnalyzer.js
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /toolkit/components/places/ColorAnalyzer.js
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'toolkit/components/places/ColorAnalyzer.js')
-rw-r--r--toolkit/components/places/ColorAnalyzer.js90
1 files changed, 90 insertions, 0 deletions
diff --git a/toolkit/components/places/ColorAnalyzer.js b/toolkit/components/places/ColorAnalyzer.js
new file mode 100644
index 000000000..861ce7107
--- /dev/null
+++ b/toolkit/components/places/ColorAnalyzer.js
@@ -0,0 +1,90 @@
+/* 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/. */
+
+"use strict";
+
+const Ci = Components.interfaces;
+const Cc = Components.classes;
+const Cu = Components.utils;
+
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+
+const XHTML_NS = "http://www.w3.org/1999/xhtml";
+const MAXIMUM_PIXELS = Math.pow(144, 2);
+
+function ColorAnalyzer() {
+ // a queue of callbacks for each job we give to the worker
+ this.callbacks = [];
+
+ this.hiddenWindowDoc = Cc["@mozilla.org/appshell/appShellService;1"].
+ getService(Ci.nsIAppShellService).
+ hiddenDOMWindow.document;
+
+ this.worker = new ChromeWorker("resource://gre/modules/ColorAnalyzer_worker.js");
+ this.worker.onmessage = this.onWorkerMessage.bind(this);
+ this.worker.onerror = this.onWorkerError.bind(this);
+}
+
+ColorAnalyzer.prototype = {
+ findRepresentativeColor: function ColorAnalyzer_frc(imageURI, callback) {
+ function cleanup() {
+ image.removeEventListener("load", loadListener);
+ image.removeEventListener("error", errorListener);
+ }
+ let image = this.hiddenWindowDoc.createElementNS(XHTML_NS, "img");
+ let loadListener = this.onImageLoad.bind(this, image, callback, cleanup);
+ let errorListener = this.onImageError.bind(this, image, callback, cleanup);
+ image.addEventListener("load", loadListener);
+ image.addEventListener("error", errorListener);
+ image.src = imageURI.spec;
+ },
+
+ onImageLoad: function ColorAnalyzer_onImageLoad(image, callback, cleanup) {
+ if (image.naturalWidth * image.naturalHeight > MAXIMUM_PIXELS) {
+ // this will probably take too long to process - fail
+ callback.onComplete(false);
+ } else {
+ let canvas = this.hiddenWindowDoc.createElementNS(XHTML_NS, "canvas");
+ canvas.width = image.naturalWidth;
+ canvas.height = image.naturalHeight;
+ let ctx = canvas.getContext("2d");
+ ctx.drawImage(image, 0, 0);
+ this.startJob(ctx.getImageData(0, 0, canvas.width, canvas.height),
+ callback);
+ }
+ cleanup();
+ },
+
+ onImageError: function ColorAnalyzer_onImageError(image, callback, cleanup) {
+ Cu.reportError("ColorAnalyzer: image at " + image.src + " didn't load");
+ callback.onComplete(false);
+ cleanup();
+ },
+
+ startJob: function ColorAnalyzer_startJob(imageData, callback) {
+ this.callbacks.push(callback);
+ this.worker.postMessage({ imageData: imageData, maxColors: 1 });
+ },
+
+ onWorkerMessage: function ColorAnalyzer_onWorkerMessage(event) {
+ // colors can be empty on failure
+ if (event.data.colors.length < 1) {
+ this.callbacks.shift().onComplete(false);
+ } else {
+ this.callbacks.shift().onComplete(true, event.data.colors[0]);
+ }
+ },
+
+ onWorkerError: function ColorAnalyzer_onWorkerError(error) {
+ // this shouldn't happen, but just in case
+ error.preventDefault();
+ Cu.reportError("ColorAnalyzer worker: " + error.message);
+ this.callbacks.shift().onComplete(false);
+ },
+
+ classID: Components.ID("{d056186c-28a0-494e-aacc-9e433772b143}"),
+ QueryInterface: XPCOMUtils.generateQI([Ci.mozIColorAnalyzer])
+};
+
+this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ColorAnalyzer]);