diff --git a/browser/app/profile/extensions/testpilot@labs.mozilla.com/content/experiment-page.js b/browser/app/profile/extensions/testpilot@labs.mozilla.com/content/experiment-page.js index 338065fbf37..48d6175e290 100644 --- a/browser/app/profile/extensions/testpilot@labs.mozilla.com/content/experiment-page.js +++ b/browser/app/profile/extensions/testpilot@labs.mozilla.com/content/experiment-page.js @@ -123,7 +123,7 @@ var stringBundle; // and targets let io = Components.classes["@mozilla.org/network/io-service;1"]. getService(Components.interfaces.nsIIOService); - let source = io.newURI(canvas.toDataURL("image/png", ""), "UTF8", null); + let source = io.newURI(canvas.toDataURL("image/png"), "UTF8", null); let target = io.newFileURI(file); // prepare to save the canvas data diff --git a/browser/base/content/tabview/tabitems.js b/browser/base/content/tabview/tabitems.js index 24b467d63cc..50d17934008 100644 --- a/browser/base/content/tabview/tabitems.js +++ b/browser/base/content/tabview/tabitems.js @@ -1558,6 +1558,6 @@ TabCanvas.prototype = { // ---------- // Function: toImageData toImageData: function TabCanvas_toImageData() { - return this.canvas.toDataURL("image/png", ""); + return this.canvas.toDataURL("image/png"); } }; diff --git a/content/canvas/test/Makefile.in b/content/canvas/test/Makefile.in index fa508e2cdb3..3d30e9b3d96 100644 --- a/content/canvas/test/Makefile.in +++ b/content/canvas/test/Makefile.in @@ -80,6 +80,7 @@ _TEST_FILES_0 = \ test_2d.composite.uncovered.image.source-in.html \ test_2d.composite.uncovered.image.source-out.html \ test_toDataURL_lowercase_ascii.html \ + test_toDataURL_parameters.html \ test_mozGetAsFile.html \ test_canvas_strokeStyle_getter.html \ test_bug613794.html \ diff --git a/content/canvas/test/test_canvas.html b/content/canvas/test/test_canvas.html index 96fb75ea6cf..cd61158d5e5 100644 --- a/content/canvas/test/test_canvas.html +++ b/content/canvas/test/test_canvas.html @@ -19537,21 +19537,23 @@ var canvas = document.getElementById('c614'); var ctx = canvas.getContext('2d'); netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); +var _threw = false; try { var data = canvas.toDataURL('image/png', 'quality=100'); - ok(false, "Should have thrown an exception for invalid args to png encoder"); } catch (e) { - is(e.result, Components.results.NS_ERROR_INVALID_ARG, "Exception was wrong for png encoder"); + _threw = true; } +ok(!_threw, "Should not throw an exception for invalid args to png encoder"); +_threw = false; try { var data = canvas.toDataURL('image/jpeg', 'foobar=true'); - ok(false, "Should have thrown an exception for invalid args to jpeg encoder"); } catch (e) { - is(e.result, Components.results.NS_ERROR_INVALID_ARG, "Exception was wrong for jpeg encoder"); + _threw = true; } +ok(!_threw, "Should not throw an exception for invalid args to jpeg encoder"); } @@ -20761,7 +20763,7 @@ ok(/^data:image\/png[;,]/.test(data), "data =~ /^data:image\\/png[;,]/"); } catch (e) { _thrown_outer = true; } -todo(!_thrown_outer, 'should not throw exception'); +ok(!_thrown_outer, 'should not throw exception'); } @@ -20788,7 +20790,7 @@ ok(/^data:image\/png[;,]/.test(data), "data =~ /^data:image\\/png[;,]/"); } catch (e) { _thrown_outer = true; } -todo(!_thrown_outer, 'should not throw exception'); +ok(!_thrown_outer, 'should not throw exception'); } @@ -20816,7 +20818,7 @@ ok(/^data:image\/png[;,]/.test(data), "data =~ /^data:image\\/png[;,]/"); } catch (e) { _thrown_outer = true; } -todo(!_thrown_outer, 'should not throw exception'); +ok(!_thrown_outer, 'should not throw exception'); } diff --git a/content/canvas/test/test_toDataURL_parameters.html b/content/canvas/test/test_toDataURL_parameters.html new file mode 100644 index 00000000000..0a58ec3a441 --- /dev/null +++ b/content/canvas/test/test_toDataURL_parameters.html @@ -0,0 +1,55 @@ + +
+This test covers the JPEG quality parameter. If (when) the HTML5 spec changes the +allowed parameters for ToDataURL, new tests should go here. +
+ + diff --git a/content/html/content/public/nsHTMLCanvasElement.h b/content/html/content/public/nsHTMLCanvasElement.h index 630a789899d..bdfc075f123 100644 --- a/content/html/content/public/nsHTMLCanvasElement.h +++ b/content/html/content/public/nsHTMLCanvasElement.h @@ -181,7 +181,7 @@ protected: PRUint32& aSize, bool& aFellBackToPNG); nsresult ToDataURLImpl(const nsAString& aMimeType, - const nsAString& aEncoderOptions, + nsIVariant* aEncoderOptions, nsAString& aDataURL); nsresult MozGetAsFileImpl(const nsAString& aName, const nsAString& aType, diff --git a/content/html/content/src/nsHTMLCanvasElement.cpp b/content/html/content/src/nsHTMLCanvasElement.cpp index 873d8941161..381155a289e 100644 --- a/content/html/content/src/nsHTMLCanvasElement.cpp +++ b/content/html/content/src/nsHTMLCanvasElement.cpp @@ -47,6 +47,7 @@ #include "nsIXPConnect.h" #include "jsapi.h" #include "nsJSUtils.h" +#include "nsMathUtils.h" #include "nsFrameManager.h" #include "nsDisplayList.h" @@ -196,32 +197,17 @@ nsHTMLCanvasElement::ParseAttribute(PRInt32 aNamespaceID, // nsHTMLCanvasElement::toDataURL NS_IMETHODIMP -nsHTMLCanvasElement::ToDataURL(const nsAString& aType, const nsAString& aParams, +nsHTMLCanvasElement::ToDataURL(const nsAString& aType, nsIVariant* aParams, PRUint8 optional_argc, nsAString& aDataURL) { // do a trust check if this is a write-only canvas - // or if we're trying to use the 2-arg form - if ((mWriteOnly || optional_argc >= 2) && - !nsContentUtils::IsCallerTrustedForRead()) { + if (mWriteOnly && !nsContentUtils::IsCallerTrustedForRead()) { return NS_ERROR_DOM_SECURITY_ERR; } return ToDataURLImpl(aType, aParams, aDataURL); } - -// nsHTMLCanvasElement::toDataURLAs -// -// Native-callers only - -NS_IMETHODIMP -nsHTMLCanvasElement::ToDataURLAs(const nsAString& aMimeType, - const nsAString& aEncoderOptions, - nsAString& aDataURL) -{ - return ToDataURLImpl(aMimeType, aEncoderOptions, aDataURL); -} - nsresult nsHTMLCanvasElement::ExtractData(const nsAString& aType, const nsAString& aOptions, @@ -323,7 +309,7 @@ nsHTMLCanvasElement::ExtractData(const nsAString& aType, nsresult nsHTMLCanvasElement::ToDataURLImpl(const nsAString& aMimeType, - const nsAString& aEncoderOptions, + nsIVariant* aEncoderOptions, nsAString& aDataURL) { bool fallbackToPNG = false; @@ -337,11 +323,30 @@ nsHTMLCanvasElement::ToDataURLImpl(const nsAString& aMimeType, nsAutoString type; nsContentUtils::ASCIIToLower(aMimeType, type); + nsAutoString params; + + // Quality parameter is only valid for the image/jpeg MIME type + if (type.EqualsLiteral("image/jpeg")) { + PRUint16 vartype; + + if (aEncoderOptions && + NS_SUCCEEDED(aEncoderOptions->GetDataType(&vartype)) && + vartype <= nsIDataType::VTYPE_DOUBLE) { + + double quality; + // Quality must be between 0.0 and 1.0, inclusive + if (NS_SUCCEEDED(aEncoderOptions->GetAsDouble(&quality)) && + quality >= 0.0 && quality <= 1.0) { + params.AppendLiteral("quality="); + params.AppendInt(NS_lround(quality * 100.0)); + } + } + } + PRUint32 imgSize = 0; char* imgData; - nsresult rv = ExtractData(type, aEncoderOptions, imgData, - imgSize, fallbackToPNG); + nsresult rv = ExtractData(type, params, imgData, imgSize, fallbackToPNG); NS_ENSURE_SUCCESS(rv, rv); // base 64, result will be NULL terminated diff --git a/dom/interfaces/html/nsIDOMHTMLCanvasElement.idl b/dom/interfaces/html/nsIDOMHTMLCanvasElement.idl index cdc0d91a429..92058282255 100644 --- a/dom/interfaces/html/nsIDOMHTMLCanvasElement.idl +++ b/dom/interfaces/html/nsIDOMHTMLCanvasElement.idl @@ -53,8 +53,9 @@ */ interface nsIDOMFile; +interface nsIVariant; -[scriptable, uuid(2e98cd39-2269-493a-a3bb-abe85be2523c)] +[scriptable, uuid(010d8e6f-86ba-47ad-a04f-1a4d75f1caf8)] interface nsIDOMHTMLCanvasElement : nsIDOMHTMLElement { attribute unsigned long width; @@ -68,18 +69,10 @@ interface nsIDOMHTMLCanvasElement : nsIDOMHTMLElement // Valid calls are: // toDataURL(); -- defaults to image/png // toDataURL(type); -- uses given type - // toDataURL(type, params); -- only available to trusted callers + // toDataURL(type, params); -- uses given type, and any valid parameters [optional_argc] DOMString toDataURL([optional] in DOMString type, - [optional] in DOMString params); + [optional] in nsIVariant params); - // This version lets you specify different image types and pass parameters - // to the encoder. For example toDataURLAs("image/png", "transparency=none") - // gives you a PNG with the alpha channel discarded. See the encoder for - // the options string that it supports. Separate multiple options with - // semicolons. - [noscript] DOMString toDataURLAs(in DOMString mimeType, in DOMString encoderOptions); - - // Valid calls are // mozGetAsFile(name); -- defaults to image/png // mozGetAsFile(name, type); -- uses given type