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