Bug 720838 - [Page Thumbnails] Use canvas.mozFetchAsStream(); r=dietrich

This commit is contained in:
Tim Taubert 2012-02-09 09:48:30 +01:00
parent 8b64026974
commit 48c9eec3af
3 changed files with 42 additions and 56 deletions

View File

@ -78,8 +78,9 @@ let gBrowserThumbnails = {
_capture: function Thumbnails_capture(aBrowser) { _capture: function Thumbnails_capture(aBrowser) {
if (this._shouldCapture(aBrowser)) { if (this._shouldCapture(aBrowser)) {
let canvas = this._pageThumbs.capture(aBrowser.contentWindow); this._pageThumbs.capture(aBrowser.contentWindow, function (aInputStream) {
this._pageThumbs.store(aBrowser.currentURI.spec, canvas); this._pageThumbs.store(aBrowser.currentURI.spec, aInputStream);
}.bind(this));
} }
}, },

View File

@ -72,11 +72,13 @@ let PageThumbs = {
}, },
/** /**
* Creates a canvas containing a thumbnail depicting the given window. * Captures a thumbnail for the given window.
* @param aWindow The DOM window to capture a thumbnail from. * @param aWindow The DOM window to capture a thumbnail from.
* @return The newly created canvas containing the image data. * @param aCallback The function to be called when the thumbnail has been
* captured. The first argument will be the data stream
* containing the image data.
*/ */
capture: function PageThumbs_capture(aWindow) { capture: function PageThumbs_capture(aWindow, aCallback) {
let telemetryCaptureTime = new Date(); let telemetryCaptureTime = new Date();
let [sw, sh, scale] = this._determineCropSize(aWindow); let [sw, sh, scale] = this._determineCropSize(aWindow);
@ -94,21 +96,22 @@ let PageThumbs = {
// We couldn't draw to the canvas for some reason. // We couldn't draw to the canvas for some reason.
} }
Services.telemetry.getHistogramById("FX_THUMBNAILS_CAPTURE_TIME_MS") let telemetry = Services.telemetry;
telemetry.getHistogramById("FX_THUMBNAILS_CAPTURE_TIME_MS")
.add(new Date() - telemetryCaptureTime); .add(new Date() - telemetryCaptureTime);
return canvas; canvas.mozFetchAsStream(aCallback, this.contentType);
}, },
/** /**
* Stores the image data contained in the given canvas to the underlying * Stores the image data contained in the given canvas to the underlying
* storage. * storage.
* @param aKey The key to use for the storage. * @param aKey The key to use for the storage.
* @param aCanvas The canvas containing the thumbnail's image data. * @param aInputStream The input stream containing the image data.
* @param aCallback The function to be called when the canvas data has been * @param aCallback The function to be called when the canvas data has been
* stored (optional). * stored (optional).
*/ */
store: function PageThumbs_store(aKey, aCanvas, aCallback) { store: function PageThumbs_store(aKey, aInputStream, aCallback) {
let telemetryStoreTime = new Date(); let telemetryStoreTime = new Date();
function finish(aSuccessful) { function finish(aSuccessful) {
@ -121,8 +124,6 @@ let PageThumbs = {
aCallback(aSuccessful); aCallback(aSuccessful);
} }
let self = this;
// Get a writeable cache entry. // Get a writeable cache entry.
PageThumbsCache.getWriteEntry(aKey, function (aEntry) { PageThumbsCache.getWriteEntry(aKey, function (aEntry) {
if (!aEntry) { if (!aEntry) {
@ -130,12 +131,10 @@ let PageThumbs = {
return; return;
} }
// Extract image data from the canvas.
self._readImageData(aCanvas, function (aData) {
let outputStream = aEntry.openOutputStream(0); let outputStream = aEntry.openOutputStream(0);
// Write the image data to the cache entry. // Write the image data to the cache entry.
NetUtil.asyncCopy(aData, outputStream, function (aResult) { NetUtil.asyncCopy(aInputStream, outputStream, function (aResult) {
let success = Components.isSuccessCode(aResult); let success = Components.isSuccessCode(aResult);
if (success) if (success)
aEntry.markValid(); aEntry.markValid();
@ -144,22 +143,6 @@ let PageThumbs = {
finish(success); finish(success);
}); });
}); });
});
},
/**
* Reads the image data from a given canvas and passes it to the callback.
* @param aCanvas The canvas to read the image data from.
* @param aCallback The function that the image data is passed to.
*/
_readImageData: function PageThumbs_readImageData(aCanvas, aCallback) {
let dataUri = aCanvas.toDataURL(PageThumbs.contentType, "");
let uri = Services.io.newURI(dataUri, "UTF8", null);
NetUtil.asyncFetch(uri, function (aData, aResult) {
if (Components.isSuccessCode(aResult) && aData && aData.available())
aCallback(aData);
});
}, },
/** /**

View File

@ -96,11 +96,12 @@ function whenLoaded(aElement, aCallback) {
function captureAndCheckColor(aRed, aGreen, aBlue, aMessage) { function captureAndCheckColor(aRed, aGreen, aBlue, aMessage) {
let window = gBrowser.selectedTab.linkedBrowser.contentWindow; let window = gBrowser.selectedTab.linkedBrowser.contentWindow;
// Capture the screenshot.
PageThumbs.capture(window, function (aData) {
let key = Date.now(); let key = Date.now();
let data = PageThumbs.capture(window);
// Store the thumbnail in the cache. // Store the thumbnail in the cache.
PageThumbs.store(key, data, function () { PageThumbs.store(key, aData, function () {
let width = 100, height = 100; let width = 100, height = 100;
let thumb = PageThumbs.getThumbnailURL(key, width, height); let thumb = PageThumbs.getThumbnailURL(key, width, height);
@ -123,6 +124,7 @@ function captureAndCheckColor(aRed, aGreen, aBlue, aMessage) {
}); });
}); });
}); });
});
} }
/** /**