2014-05-20 04:54:00 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* 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/. */
|
2005-04-18 06:33:23 +00:00
|
|
|
|
2013-01-04 05:16:14 +00:00
|
|
|
#include "mozilla/dom/HTMLCanvasElement.h"
|
2010-05-18 04:04:22 +00:00
|
|
|
|
2013-10-17 02:55:08 +00:00
|
|
|
#include "ImageEncoder.h"
|
2013-04-13 07:03:57 +00:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jsfriendapi.h"
|
2013-10-17 02:55:08 +00:00
|
|
|
#include "Layers.h"
|
2011-06-08 18:12:18 +00:00
|
|
|
#include "mozilla/Base64.h"
|
2012-05-14 19:50:20 +00:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2013-04-13 07:03:57 +00:00
|
|
|
#include "mozilla/dom/CanvasRenderingContext2D.h"
|
2013-04-13 07:05:51 +00:00
|
|
|
#include "mozilla/dom/HTMLCanvasElementBinding.h"
|
2013-07-26 18:25:54 +00:00
|
|
|
#include "mozilla/dom/UnionTypes.h"
|
2014-05-20 04:54:00 +00:00
|
|
|
#include "mozilla/dom/MouseEvent.h"
|
|
|
|
#include "mozilla/EventDispatcher.h"
|
2012-11-04 08:05:25 +00:00
|
|
|
#include "mozilla/gfx/Rect.h"
|
2014-05-20 04:54:00 +00:00
|
|
|
#include "mozilla/MouseEvents.h"
|
2012-11-04 08:05:25 +00:00
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "mozilla/Telemetry.h"
|
2013-04-13 07:03:57 +00:00
|
|
|
#include "nsAttrValueInlines.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsDisplayList.h"
|
2010-09-18 01:14:59 +00:00
|
|
|
#include "nsDOMFile.h"
|
2013-10-17 02:55:08 +00:00
|
|
|
#include "nsDOMJSUtils.h"
|
2006-05-03 00:48:27 +00:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2013-04-13 07:03:57 +00:00
|
|
|
#include "nsITimer.h"
|
|
|
|
#include "nsIWritablePropertyBag2.h"
|
2006-05-03 00:48:27 +00:00
|
|
|
#include "nsIXPConnect.h"
|
2010-12-03 08:24:17 +00:00
|
|
|
#include "nsJSUtils.h"
|
2014-05-20 04:54:00 +00:00
|
|
|
#include "nsLayoutUtils.h"
|
2011-05-26 07:28:26 +00:00
|
|
|
#include "nsMathUtils.h"
|
2013-04-13 07:03:57 +00:00
|
|
|
#include "nsNetUtil.h"
|
2012-02-03 14:23:18 +00:00
|
|
|
#include "nsStreamUtils.h"
|
2013-09-04 11:30:57 +00:00
|
|
|
#include "ActiveLayerTracker.h"
|
2014-07-25 21:15:35 +00:00
|
|
|
#include "WebGL2Context.h"
|
2013-07-17 13:58:09 +00:00
|
|
|
|
2010-05-18 04:04:22 +00:00
|
|
|
using namespace mozilla::layers;
|
2014-04-17 05:29:52 +00:00
|
|
|
using namespace mozilla::gfx;
|
2005-04-18 06:33:23 +00:00
|
|
|
|
2013-04-13 07:03:57 +00:00
|
|
|
NS_IMPL_NS_NEW_HTML_ELEMENT(Canvas)
|
2013-01-04 05:16:14 +00:00
|
|
|
|
2012-10-15 21:15:25 +00:00
|
|
|
namespace {
|
|
|
|
|
2012-11-14 00:35:36 +00:00
|
|
|
typedef mozilla::dom::HTMLImageElementOrHTMLCanvasElementOrHTMLVideoElement
|
|
|
|
HTMLImageOrCanvasOrVideoElement;
|
|
|
|
|
2012-10-15 21:15:25 +00:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2013-01-04 05:16:14 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-04-29 08:57:00 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(HTMLCanvasPrintState, mCanvas,
|
|
|
|
mContext, mCallback)
|
2013-09-11 22:35:51 +00:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(HTMLCanvasPrintState, AddRef)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(HTMLCanvasPrintState, Release)
|
|
|
|
|
|
|
|
HTMLCanvasPrintState::HTMLCanvasPrintState(HTMLCanvasElement* aCanvas,
|
|
|
|
nsICanvasRenderingContextInternal* aContext,
|
|
|
|
nsITimerCallback* aCallback)
|
|
|
|
: mIsDone(false), mPendingNotify(false), mCanvas(aCanvas),
|
|
|
|
mContext(aContext), mCallback(aCallback)
|
2012-08-31 17:45:45 +00:00
|
|
|
{
|
2013-09-11 22:35:51 +00:00
|
|
|
SetIsDOMBinding();
|
|
|
|
}
|
2012-08-31 17:45:45 +00:00
|
|
|
|
2013-09-11 22:35:51 +00:00
|
|
|
HTMLCanvasPrintState::~HTMLCanvasPrintState()
|
|
|
|
{
|
|
|
|
}
|
2012-08-31 17:45:45 +00:00
|
|
|
|
2013-09-11 22:35:51 +00:00
|
|
|
/* virtual */ JSObject*
|
2014-04-08 22:27:18 +00:00
|
|
|
HTMLCanvasPrintState::WrapObject(JSContext* aCx)
|
2013-09-11 22:35:51 +00:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 22:27:17 +00:00
|
|
|
return MozCanvasPrintStateBinding::Wrap(aCx, this);
|
2013-09-11 22:35:51 +00:00
|
|
|
}
|
2012-08-31 17:45:45 +00:00
|
|
|
|
2013-09-11 22:35:51 +00:00
|
|
|
nsISupports*
|
|
|
|
HTMLCanvasPrintState::Context() const
|
|
|
|
{
|
|
|
|
return mContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HTMLCanvasPrintState::Done()
|
|
|
|
{
|
|
|
|
if (!mPendingNotify && !mIsDone) {
|
|
|
|
// The canvas needs to be invalidated for printing reftests on linux to
|
|
|
|
// work.
|
|
|
|
if (mCanvas) {
|
|
|
|
mCanvas->InvalidateCanvas();
|
|
|
|
}
|
|
|
|
nsRefPtr<nsRunnableMethod<HTMLCanvasPrintState> > doneEvent =
|
|
|
|
NS_NewRunnableMethod(this, &HTMLCanvasPrintState::NotifyDone);
|
|
|
|
if (NS_SUCCEEDED(NS_DispatchToCurrentThread(doneEvent))) {
|
|
|
|
mPendingNotify = true;
|
2012-08-31 17:45:45 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-11 22:35:51 +00:00
|
|
|
}
|
2012-08-31 17:45:45 +00:00
|
|
|
|
2013-09-11 22:35:51 +00:00
|
|
|
void
|
|
|
|
HTMLCanvasPrintState::NotifyDone()
|
|
|
|
{
|
|
|
|
mIsDone = true;
|
|
|
|
mPendingNotify = false;
|
|
|
|
if (mCallback) {
|
|
|
|
mCallback->Notify(nullptr);
|
2012-08-31 17:45:45 +00:00
|
|
|
}
|
2013-09-11 22:35:51 +00:00
|
|
|
}
|
2012-08-31 17:45:45 +00:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2014-06-20 02:01:40 +00:00
|
|
|
HTMLCanvasElement::HTMLCanvasElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
|
2013-04-13 07:03:57 +00:00
|
|
|
: nsGenericHTMLElement(aNodeInfo),
|
2012-08-31 17:45:45 +00:00
|
|
|
mWriteOnly(false)
|
2005-04-18 06:33:23 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement::~HTMLCanvasElement()
|
2005-04-18 06:33:23 +00:00
|
|
|
{
|
2012-08-31 17:45:45 +00:00
|
|
|
ResetPrintCallback();
|
2005-04-18 06:33:23 +00:00
|
|
|
}
|
|
|
|
|
2014-04-25 16:49:00 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(HTMLCanvasElement, nsGenericHTMLElement,
|
|
|
|
mCurrentContext, mPrintCallback,
|
|
|
|
mPrintState, mOriginalCanvas)
|
2011-06-21 21:12:50 +00:00
|
|
|
|
2013-01-04 05:16:14 +00:00
|
|
|
NS_IMPL_ADDREF_INHERITED(HTMLCanvasElement, Element)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(HTMLCanvasElement, Element)
|
2010-01-12 13:08:43 +00:00
|
|
|
|
2013-01-04 05:16:14 +00:00
|
|
|
NS_INTERFACE_TABLE_HEAD_CYCLE_COLLECTION_INHERITED(HTMLCanvasElement)
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_INTERFACE_TABLE_INHERITED(HTMLCanvasElement, nsIDOMHTMLCanvasElement)
|
2013-08-07 20:23:08 +00:00
|
|
|
NS_INTERFACE_TABLE_TAIL_INHERITING(nsGenericHTMLElement)
|
2005-04-18 06:33:23 +00:00
|
|
|
|
2013-01-04 05:16:14 +00:00
|
|
|
NS_IMPL_ELEMENT_CLONE(HTMLCanvasElement)
|
2005-04-18 06:33:23 +00:00
|
|
|
|
2013-04-13 07:05:51 +00:00
|
|
|
/* virtual */ JSObject*
|
2014-04-08 22:27:17 +00:00
|
|
|
HTMLCanvasElement::WrapNode(JSContext* aCx)
|
2013-04-13 07:05:51 +00:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 22:27:17 +00:00
|
|
|
return HTMLCanvasElementBinding::Wrap(aCx, this);
|
2013-04-13 07:05:51 +00:00
|
|
|
}
|
|
|
|
|
2005-05-07 03:45:06 +00:00
|
|
|
nsIntSize
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement::GetWidthHeight()
|
2005-04-18 06:33:23 +00:00
|
|
|
{
|
2011-05-19 10:49:18 +00:00
|
|
|
nsIntSize size(DEFAULT_CANVAS_WIDTH, DEFAULT_CANVAS_HEIGHT);
|
2005-04-18 06:33:23 +00:00
|
|
|
const nsAttrValue* value;
|
|
|
|
|
2006-12-26 17:47:52 +00:00
|
|
|
if ((value = GetParsedAttr(nsGkAtoms::width)) &&
|
2005-05-07 03:45:06 +00:00
|
|
|
value->Type() == nsAttrValue::eInteger)
|
|
|
|
{
|
2005-04-18 06:33:23 +00:00
|
|
|
size.width = value->GetIntegerValue();
|
|
|
|
}
|
|
|
|
|
2006-12-26 17:47:52 +00:00
|
|
|
if ((value = GetParsedAttr(nsGkAtoms::height)) &&
|
2005-05-07 03:45:06 +00:00
|
|
|
value->Type() == nsAttrValue::eInteger)
|
|
|
|
{
|
2005-04-18 06:33:23 +00:00
|
|
|
size.height = value->GetIntegerValue();
|
|
|
|
}
|
|
|
|
|
2005-05-07 03:45:06 +00:00
|
|
|
return size;
|
2005-04-18 06:33:23 +00:00
|
|
|
}
|
|
|
|
|
2013-01-04 05:16:14 +00:00
|
|
|
NS_IMPL_UINT_ATTR_DEFAULT_VALUE(HTMLCanvasElement, Width, width, DEFAULT_CANVAS_WIDTH)
|
|
|
|
NS_IMPL_UINT_ATTR_DEFAULT_VALUE(HTMLCanvasElement, Height, height, DEFAULT_CANVAS_HEIGHT)
|
|
|
|
NS_IMPL_BOOL_ATTR(HTMLCanvasElement, MozOpaque, moz_opaque)
|
2005-04-18 06:33:23 +00:00
|
|
|
|
|
|
|
nsresult
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement::SetAttr(int32_t aNameSpaceID, nsIAtom* aName,
|
|
|
|
nsIAtom* aPrefix, const nsAString& aValue,
|
|
|
|
bool aNotify)
|
2005-04-18 06:33:23 +00:00
|
|
|
{
|
|
|
|
nsresult rv = nsGenericHTMLElement::SetAttr(aNameSpaceID, aName, aPrefix, aValue,
|
|
|
|
aNotify);
|
|
|
|
if (NS_SUCCEEDED(rv) && mCurrentContext &&
|
2013-09-30 21:02:40 +00:00
|
|
|
aNameSpaceID == kNameSpaceID_None &&
|
2008-06-04 00:00:32 +00:00
|
|
|
(aName == nsGkAtoms::width || aName == nsGkAtoms::height || aName == nsGkAtoms::moz_opaque))
|
2005-04-18 06:33:23 +00:00
|
|
|
{
|
2013-06-07 18:42:12 +00:00
|
|
|
rv = UpdateContext(nullptr, JS::NullHandleValue);
|
2005-04-18 06:33:23 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2013-09-30 21:02:40 +00:00
|
|
|
nsresult
|
|
|
|
HTMLCanvasElement::UnsetAttr(int32_t aNameSpaceID, nsIAtom* aName,
|
|
|
|
bool aNotify)
|
|
|
|
{
|
|
|
|
nsresult rv = nsGenericHTMLElement::UnsetAttr(aNameSpaceID, aName, aNotify);
|
|
|
|
if (NS_SUCCEEDED(rv) && mCurrentContext &&
|
|
|
|
aNameSpaceID == kNameSpaceID_None &&
|
|
|
|
(aName == nsGkAtoms::width || aName == nsGkAtoms::height || aName == nsGkAtoms::moz_opaque))
|
|
|
|
{
|
|
|
|
rv = UpdateContext(nullptr, JS::NullHandleValue);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-08-31 17:45:45 +00:00
|
|
|
void
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement::HandlePrintCallback(nsPresContext::nsPresContextType aType)
|
2012-08-31 17:45:45 +00:00
|
|
|
{
|
|
|
|
// Only call the print callback here if 1) we're in a print testing mode or
|
|
|
|
// print preview mode, 2) the canvas has a print callback and 3) the callback
|
|
|
|
// hasn't already been called. For real printing the callback is handled in
|
|
|
|
// nsSimplePageSequenceFrame::PrePrintNextPage.
|
|
|
|
if ((aType == nsPresContext::eContext_PageLayout ||
|
|
|
|
aType == nsPresContext::eContext_PrintPreview) &&
|
2013-09-11 22:35:51 +00:00
|
|
|
!mPrintState && GetMozPrintCallback()) {
|
2012-08-31 17:45:45 +00:00
|
|
|
DispatchPrintCallback(nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement::DispatchPrintCallback(nsITimerCallback* aCallback)
|
2012-08-31 17:45:45 +00:00
|
|
|
{
|
|
|
|
// For print reftests the context may not be initialized yet, so get a context
|
|
|
|
// so mCurrentContext is set.
|
|
|
|
if (!mCurrentContext) {
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsISupports> context;
|
2013-04-13 07:05:33 +00:00
|
|
|
rv = GetContext(NS_LITERAL_STRING("2d"), getter_AddRefs(context));
|
2012-08-31 17:45:45 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2013-01-04 05:16:14 +00:00
|
|
|
mPrintState = new HTMLCanvasPrintState(this, mCurrentContext, aCallback);
|
2012-08-31 17:45:45 +00:00
|
|
|
|
2013-01-04 05:16:14 +00:00
|
|
|
nsRefPtr<nsRunnableMethod<HTMLCanvasElement> > renderEvent =
|
|
|
|
NS_NewRunnableMethod(this, &HTMLCanvasElement::CallPrintCallback);
|
2012-08-31 17:45:45 +00:00
|
|
|
return NS_DispatchToCurrentThread(renderEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement::CallPrintCallback()
|
2012-08-31 17:45:45 +00:00
|
|
|
{
|
2013-09-11 22:35:51 +00:00
|
|
|
ErrorResult rv;
|
|
|
|
GetMozPrintCallback()->Call(*mPrintState, rv);
|
2012-08-31 17:45:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement::ResetPrintCallback()
|
2012-08-31 17:45:45 +00:00
|
|
|
{
|
|
|
|
if (mPrintState) {
|
|
|
|
mPrintState = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement::IsPrintCallbackDone()
|
2012-08-31 17:45:45 +00:00
|
|
|
{
|
|
|
|
if (mPrintState == nullptr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mPrintState->mIsDone;
|
|
|
|
}
|
|
|
|
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement*
|
|
|
|
HTMLCanvasElement::GetOriginalCanvas()
|
2012-08-31 17:45:45 +00:00
|
|
|
{
|
|
|
|
return mOriginalCanvas ? mOriginalCanvas.get() : this;
|
|
|
|
}
|
|
|
|
|
2009-12-11 04:02:13 +00:00
|
|
|
nsresult
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement::CopyInnerTo(Element* aDest)
|
2009-12-11 04:02:13 +00:00
|
|
|
{
|
|
|
|
nsresult rv = nsGenericHTMLElement::CopyInnerTo(aDest);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-10-18 10:53:36 +00:00
|
|
|
if (aDest->OwnerDoc()->IsStaticDocument()) {
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement* dest = static_cast<HTMLCanvasElement*>(aDest);
|
2013-04-13 07:05:33 +00:00
|
|
|
dest->mOriginalCanvas = this;
|
2012-08-31 17:45:45 +00:00
|
|
|
|
2009-12-11 04:02:13 +00:00
|
|
|
nsCOMPtr<nsISupports> cxt;
|
2013-04-13 07:05:33 +00:00
|
|
|
dest->GetContext(NS_LITERAL_STRING("2d"), getter_AddRefs(cxt));
|
2012-11-14 00:35:36 +00:00
|
|
|
nsRefPtr<CanvasRenderingContext2D> context2d =
|
|
|
|
static_cast<CanvasRenderingContext2D*>(cxt.get());
|
2013-04-13 07:05:33 +00:00
|
|
|
if (context2d && !mPrintCallback) {
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLImageOrCanvasOrVideoElement element;
|
|
|
|
element.SetAsHTMLCanvasElement() = this;
|
2012-11-14 00:35:36 +00:00
|
|
|
ErrorResult err;
|
|
|
|
context2d->DrawImage(element,
|
2012-11-21 21:55:37 +00:00
|
|
|
0.0, 0.0, err);
|
2012-11-14 00:35:36 +00:00
|
|
|
rv = err.ErrorCode();
|
2009-12-11 04:02:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-05-20 04:54:00 +00:00
|
|
|
nsresult HTMLCanvasElement::PreHandleEvent(EventChainPreVisitor& aVisitor)
|
|
|
|
{
|
2014-08-04 05:28:50 +00:00
|
|
|
if (aVisitor.mEvent->mClass == eMouseEventClass) {
|
2014-05-20 04:54:00 +00:00
|
|
|
WidgetMouseEventBase* evt = (WidgetMouseEventBase*)aVisitor.mEvent;
|
|
|
|
if (mCurrentContext) {
|
|
|
|
nsIFrame *frame = GetPrimaryFrame();
|
|
|
|
if (!frame)
|
|
|
|
return NS_OK;
|
|
|
|
nsPoint ptInRoot = nsLayoutUtils::GetEventCoordinatesRelativeTo(evt, frame);
|
|
|
|
nsRect paddingRect = frame->GetContentRectRelativeToSelf();
|
|
|
|
Point hitpoint;
|
|
|
|
hitpoint.x = (ptInRoot.x - paddingRect.x) / AppUnitsPerCSSPixel();
|
|
|
|
hitpoint.y = (ptInRoot.y - paddingRect.y) / AppUnitsPerCSSPixel();
|
|
|
|
|
|
|
|
evt->region = mCurrentContext->GetHitRegion(hitpoint);
|
|
|
|
aVisitor.mCanHandle = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nsGenericHTMLElement::PreHandleEvent(aVisitor);
|
|
|
|
}
|
|
|
|
|
2005-04-18 06:33:23 +00:00
|
|
|
nsChangeHint
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement::GetAttributeChangeHint(const nsIAtom* aAttribute,
|
|
|
|
int32_t aModType) const
|
2005-04-18 06:33:23 +00:00
|
|
|
{
|
|
|
|
nsChangeHint retval =
|
|
|
|
nsGenericHTMLElement::GetAttributeChangeHint(aAttribute, aModType);
|
2006-12-26 17:47:52 +00:00
|
|
|
if (aAttribute == nsGkAtoms::width ||
|
|
|
|
aAttribute == nsGkAtoms::height)
|
2005-04-18 06:33:23 +00:00
|
|
|
{
|
2005-05-07 03:45:06 +00:00
|
|
|
NS_UpdateHint(retval, NS_STYLE_HINT_REFLOW);
|
2008-06-04 00:00:32 +00:00
|
|
|
} else if (aAttribute == nsGkAtoms::moz_opaque)
|
|
|
|
{
|
2012-02-07 11:31:24 +00:00
|
|
|
NS_UpdateHint(retval, NS_STYLE_HINT_VISUAL);
|
2005-04-18 06:33:23 +00:00
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement::ParseAttribute(int32_t aNamespaceID,
|
|
|
|
nsIAtom* aAttribute,
|
|
|
|
const nsAString& aValue,
|
|
|
|
nsAttrValue& aResult)
|
2005-04-18 06:33:23 +00:00
|
|
|
{
|
2010-04-23 02:53:09 +00:00
|
|
|
if (aNamespaceID == kNameSpaceID_None &&
|
|
|
|
(aAttribute == nsGkAtoms::width || aAttribute == nsGkAtoms::height)) {
|
2011-03-25 11:40:55 +00:00
|
|
|
return aResult.ParseNonNegativeIntValue(aValue);
|
2005-04-18 06:33:23 +00:00
|
|
|
}
|
|
|
|
|
2005-11-29 16:37:15 +00:00
|
|
|
return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
|
|
|
|
aResult);
|
2005-04-18 06:33:23 +00:00
|
|
|
}
|
|
|
|
|
2005-08-31 22:16:51 +00:00
|
|
|
|
2013-01-04 05:16:14 +00:00
|
|
|
// HTMLCanvasElement::toDataURL
|
2005-08-31 22:16:51 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-09 17:39:36 +00:00
|
|
|
HTMLCanvasElement::ToDataURL(const nsAString& aType, JS::Handle<JS::Value> aParams,
|
2013-04-13 07:05:22 +00:00
|
|
|
JSContext* aCx, nsAString& aDataURL)
|
2005-08-31 22:16:51 +00:00
|
|
|
{
|
2006-10-12 14:38:35 +00:00
|
|
|
// do a trust check if this is a write-only canvas
|
2012-10-25 23:10:53 +00:00
|
|
|
if (mWriteOnly && !nsContentUtils::IsCallerChrome()) {
|
2006-10-12 14:38:35 +00:00
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
2006-05-03 00:48:27 +00:00
|
|
|
}
|
|
|
|
|
2013-04-13 07:05:22 +00:00
|
|
|
return ToDataURLImpl(aCx, aType, aParams, aDataURL);
|
2005-08-31 22:16:51 +00:00
|
|
|
}
|
|
|
|
|
2013-01-04 05:16:14 +00:00
|
|
|
// HTMLCanvasElement::mozFetchAsStream
|
2012-02-01 00:13:44 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement::MozFetchAsStream(nsIInputStreamCallback *aCallback,
|
|
|
|
const nsAString& aType)
|
2012-02-01 00:13:44 +00:00
|
|
|
{
|
|
|
|
if (!nsContentUtils::IsCallerChrome())
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIInputStream> inputData;
|
|
|
|
|
2013-10-17 02:55:08 +00:00
|
|
|
nsAutoString type(aType);
|
|
|
|
rv = ExtractData(type, EmptyString(), getter_AddRefs(inputData));
|
2012-02-01 00:13:44 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAsyncInputStream> asyncData = do_QueryInterface(inputData, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-02-03 14:23:18 +00:00
|
|
|
nsCOMPtr<nsIThread> mainThread;
|
|
|
|
rv = NS_GetMainThread(getter_AddRefs(mainThread));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-04-04 07:05:18 +00:00
|
|
|
nsCOMPtr<nsIInputStreamCallback> asyncCallback =
|
|
|
|
NS_NewInputStreamReadyEvent(aCallback, mainThread);
|
2012-02-03 14:23:18 +00:00
|
|
|
|
|
|
|
return asyncCallback->OnInputStreamReady(asyncData);
|
2012-02-01 00:13:44 +00:00
|
|
|
}
|
|
|
|
|
2013-09-11 22:35:51 +00:00
|
|
|
void
|
|
|
|
HTMLCanvasElement::SetMozPrintCallback(PrintCallback* aCallback)
|
2012-08-31 17:45:45 +00:00
|
|
|
{
|
|
|
|
mPrintCallback = aCallback;
|
|
|
|
}
|
|
|
|
|
2013-09-11 22:35:51 +00:00
|
|
|
PrintCallback*
|
2013-04-13 07:05:40 +00:00
|
|
|
HTMLCanvasElement::GetMozPrintCallback() const
|
2012-08-31 17:45:45 +00:00
|
|
|
{
|
|
|
|
if (mOriginalCanvas) {
|
2013-04-13 07:05:40 +00:00
|
|
|
return mOriginalCanvas->GetMozPrintCallback();
|
2012-08-31 17:45:45 +00:00
|
|
|
}
|
2013-04-13 07:05:40 +00:00
|
|
|
return mPrintCallback;
|
|
|
|
}
|
|
|
|
|
2006-05-03 00:48:27 +00:00
|
|
|
nsresult
|
2013-10-17 02:55:08 +00:00
|
|
|
HTMLCanvasElement::ExtractData(nsAString& aType,
|
2013-01-04 05:16:14 +00:00
|
|
|
const nsAString& aOptions,
|
2013-10-17 02:55:08 +00:00
|
|
|
nsIInputStream** aStream)
|
2005-08-31 22:16:51 +00:00
|
|
|
{
|
2013-10-17 02:55:08 +00:00
|
|
|
return ImageEncoder::ExtractData(aType,
|
|
|
|
aOptions,
|
|
|
|
GetSize(),
|
|
|
|
mCurrentContext,
|
|
|
|
aStream);
|
2010-09-18 01:14:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-07-24 11:17:00 +00:00
|
|
|
HTMLCanvasElement::ParseParams(JSContext* aCx,
|
|
|
|
const nsAString& aType,
|
|
|
|
const JS::Value& aEncoderOptions,
|
|
|
|
nsAString& aParams,
|
|
|
|
bool* usingCustomParseOptions)
|
2010-09-18 01:14:59 +00:00
|
|
|
{
|
2011-05-26 07:28:26 +00:00
|
|
|
// Quality parameter is only valid for the image/jpeg MIME type
|
2013-07-24 11:17:00 +00:00
|
|
|
if (aType.EqualsLiteral("image/jpeg")) {
|
2013-04-13 07:05:22 +00:00
|
|
|
if (aEncoderOptions.isNumber()) {
|
|
|
|
double quality = aEncoderOptions.toNumber();
|
2011-05-26 07:28:26 +00:00
|
|
|
// Quality must be between 0.0 and 1.0, inclusive
|
2013-04-13 07:05:22 +00:00
|
|
|
if (quality >= 0.0 && quality <= 1.0) {
|
2013-07-24 11:17:00 +00:00
|
|
|
aParams.AppendLiteral("quality=");
|
|
|
|
aParams.AppendInt(NS_lround(quality * 100.0));
|
2011-05-26 07:28:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-24 11:17:00 +00:00
|
|
|
// If we haven't parsed the aParams check for proprietary options.
|
2011-08-25 20:09:05 +00:00
|
|
|
// The proprietary option -moz-parse-options will take a image lib encoder
|
|
|
|
// parse options string as is and pass it to the encoder.
|
2013-07-24 11:17:00 +00:00
|
|
|
*usingCustomParseOptions = false;
|
|
|
|
if (aParams.Length() == 0 && aEncoderOptions.isString()) {
|
2011-08-25 20:09:05 +00:00
|
|
|
NS_NAMED_LITERAL_STRING(mozParseOptions, "-moz-parse-options:");
|
2014-07-05 15:30:54 +00:00
|
|
|
nsAutoJSString paramString;
|
2013-04-13 07:05:22 +00:00
|
|
|
if (!paramString.init(aCx, aEncoderOptions.toString())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
if (StringBeginsWith(paramString, mozParseOptions)) {
|
2013-04-13 07:03:57 +00:00
|
|
|
nsDependentSubstring parseOptions = Substring(paramString,
|
|
|
|
mozParseOptions.Length(),
|
|
|
|
paramString.Length() -
|
2011-08-25 20:09:05 +00:00
|
|
|
mozParseOptions.Length());
|
2013-07-24 11:17:00 +00:00
|
|
|
aParams.Append(parseOptions);
|
|
|
|
*usingCustomParseOptions = true;
|
2011-08-25 20:09:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-24 11:17:00 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
HTMLCanvasElement::ToDataURLImpl(JSContext* aCx,
|
|
|
|
const nsAString& aMimeType,
|
|
|
|
const JS::Value& aEncoderOptions,
|
|
|
|
nsAString& aDataURL)
|
|
|
|
{
|
|
|
|
nsIntSize size = GetWidthHeight();
|
|
|
|
if (size.height == 0 || size.width == 0) {
|
|
|
|
aDataURL = NS_LITERAL_STRING("data:,");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString type;
|
2014-08-13 11:53:32 +00:00
|
|
|
nsContentUtils::ASCIIToLower(aMimeType, type);
|
2013-07-24 11:17:00 +00:00
|
|
|
|
|
|
|
nsAutoString params;
|
|
|
|
bool usingCustomParseOptions;
|
2014-08-13 11:53:32 +00:00
|
|
|
nsresult rv =
|
|
|
|
ParseParams(aCx, type, aEncoderOptions, params, &usingCustomParseOptions);
|
2013-07-24 11:17:00 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-06-08 18:12:18 +00:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2013-10-17 02:55:08 +00:00
|
|
|
rv = ExtractData(type, params, getter_AddRefs(stream));
|
2011-08-25 20:09:05 +00:00
|
|
|
|
2013-04-13 07:03:57 +00:00
|
|
|
// If there are unrecognized custom parse options, we should fall back to
|
2011-08-25 20:09:05 +00:00
|
|
|
// the default values for the encoder without any options at all.
|
|
|
|
if (rv == NS_ERROR_INVALID_ARG && usingCustomParseOptions) {
|
2013-10-17 02:55:08 +00:00
|
|
|
rv = ExtractData(type, EmptyString(), getter_AddRefs(stream));
|
2011-08-25 20:09:05 +00:00
|
|
|
}
|
|
|
|
|
2010-09-18 01:14:59 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2005-08-31 22:16:51 +00:00
|
|
|
// build data URL string
|
2013-10-17 02:55:08 +00:00
|
|
|
aDataURL = NS_LITERAL_STRING("data:") + type + NS_LITERAL_STRING(";base64,");
|
2005-08-31 22:16:51 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint64_t count;
|
2011-06-08 18:12:18 +00:00
|
|
|
rv = stream->Available(&count);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-09-28 06:57:33 +00:00
|
|
|
NS_ENSURE_TRUE(count <= UINT32_MAX, NS_ERROR_FILE_TOO_BIG);
|
2005-08-31 22:16:51 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
return Base64EncodeInputStream(stream, aDataURL, (uint32_t)count, aDataURL.Length());
|
2005-08-31 22:16:51 +00:00
|
|
|
}
|
|
|
|
|
2013-09-11 22:35:51 +00:00
|
|
|
void
|
|
|
|
HTMLCanvasElement::ToBlob(JSContext* aCx,
|
|
|
|
FileCallback& aCallback,
|
2013-07-24 11:17:00 +00:00
|
|
|
const nsAString& aType,
|
2014-02-19 15:13:38 +00:00
|
|
|
JS::Handle<JS::Value> aParams,
|
2013-09-11 22:35:51 +00:00
|
|
|
ErrorResult& aRv)
|
2012-10-15 21:15:25 +00:00
|
|
|
{
|
|
|
|
// do a trust check if this is a write-only canvas
|
2012-10-25 23:10:53 +00:00
|
|
|
if (mWriteOnly && !nsContentUtils::IsCallerChrome()) {
|
2013-09-11 22:35:51 +00:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
2012-10-19 06:59:38 +00:00
|
|
|
}
|
|
|
|
|
2012-10-15 21:15:25 +00:00
|
|
|
nsAutoString type;
|
2014-08-13 11:53:32 +00:00
|
|
|
nsContentUtils::ASCIIToLower(aType, type);
|
2012-10-15 21:15:25 +00:00
|
|
|
|
2013-07-24 11:17:00 +00:00
|
|
|
nsAutoString params;
|
|
|
|
bool usingCustomParseOptions;
|
2014-02-19 15:13:38 +00:00
|
|
|
aRv = ParseParams(aCx, type, aParams, params, &usingCustomParseOptions);
|
2013-09-11 22:35:51 +00:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
2013-07-24 11:17:00 +00:00
|
|
|
}
|
|
|
|
|
2013-09-30 21:02:40 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (mCurrentContext) {
|
2013-10-09 21:22:11 +00:00
|
|
|
// We disallow canvases of width or height zero, and set them to 1, so
|
|
|
|
// we will have a discrepancy with the sizes of the canvas and the context.
|
|
|
|
// That discrepancy is OK, the rest are not.
|
2013-09-30 21:02:40 +00:00
|
|
|
nsIntSize elementSize = GetWidthHeight();
|
2013-10-09 21:22:11 +00:00
|
|
|
MOZ_ASSERT(elementSize.width == mCurrentContext->GetWidth() ||
|
|
|
|
(elementSize.width == 0 && mCurrentContext->GetWidth() == 1));
|
|
|
|
MOZ_ASSERT(elementSize.height == mCurrentContext->GetHeight() ||
|
|
|
|
(elementSize.height == 0 && mCurrentContext->GetHeight() == 1));
|
2013-09-30 21:02:40 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-10-17 02:55:08 +00:00
|
|
|
uint8_t* imageBuffer = nullptr;
|
|
|
|
int32_t format = 0;
|
|
|
|
if (mCurrentContext) {
|
|
|
|
mCurrentContext->GetImageBuffer(&imageBuffer, &format);
|
|
|
|
}
|
|
|
|
|
2014-07-01 12:32:47 +00:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = OwnerDoc()->GetScopeObject();
|
|
|
|
MOZ_ASSERT(global);
|
2013-10-17 02:55:08 +00:00
|
|
|
aRv = ImageEncoder::ExtractDataAsync(type,
|
|
|
|
params,
|
|
|
|
usingCustomParseOptions,
|
|
|
|
imageBuffer,
|
|
|
|
format,
|
|
|
|
GetSize(),
|
|
|
|
mCurrentContext,
|
2014-07-01 12:32:47 +00:00
|
|
|
global,
|
2013-10-17 02:55:08 +00:00
|
|
|
aCallback);
|
2012-10-15 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2013-04-13 07:05:40 +00:00
|
|
|
already_AddRefed<nsIDOMFile>
|
|
|
|
HTMLCanvasElement::MozGetAsFile(const nsAString& aName,
|
|
|
|
const nsAString& aType,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMFile> file;
|
|
|
|
aRv = MozGetAsFile(aName, aType, getter_AddRefs(file));
|
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
2010-09-18 01:14:59 +00:00
|
|
|
NS_IMETHODIMP
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement::MozGetAsFile(const nsAString& aName,
|
|
|
|
const nsAString& aType,
|
|
|
|
nsIDOMFile** aResult)
|
2010-09-18 01:14:59 +00:00
|
|
|
{
|
2013-08-12 17:26:31 +00:00
|
|
|
OwnerDoc()->WarnOnceAbout(nsIDocument::eMozGetAsFile);
|
|
|
|
|
2010-09-18 01:14:59 +00:00
|
|
|
// do a trust check if this is a write-only canvas
|
|
|
|
if ((mWriteOnly) &&
|
2012-10-25 23:10:53 +00:00
|
|
|
!nsContentUtils::IsCallerChrome()) {
|
2010-09-18 01:14:59 +00:00
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MozGetAsFileImpl(aName, aType, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement::MozGetAsFileImpl(const nsAString& aName,
|
|
|
|
const nsAString& aType,
|
|
|
|
nsIDOMFile** aResult)
|
2010-09-18 01:14:59 +00:00
|
|
|
{
|
2011-06-08 18:12:18 +00:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2013-10-08 19:00:17 +00:00
|
|
|
nsAutoString type(aType);
|
2013-10-17 02:55:08 +00:00
|
|
|
nsresult rv = ExtractData(type, EmptyString(), getter_AddRefs(stream));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-10-08 19:00:17 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint64_t imgSize;
|
2011-06-08 18:12:18 +00:00
|
|
|
rv = stream->Available(&imgSize);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-09-28 06:57:33 +00:00
|
|
|
NS_ENSURE_TRUE(imgSize <= UINT32_MAX, NS_ERROR_FILE_TOO_BIG);
|
2011-06-08 18:12:18 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
void* imgData = nullptr;
|
2012-08-22 15:56:38 +00:00
|
|
|
rv = NS_ReadInputStreamToBuffer(stream, &imgData, (uint32_t)imgSize);
|
2011-06-08 18:12:18 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-11-08 01:49:21 +00:00
|
|
|
JSContext* cx = nsContentUtils::GetCurrentJSContext();
|
|
|
|
if (cx) {
|
|
|
|
JS_updateMallocCounter(cx, imgSize);
|
|
|
|
}
|
|
|
|
|
2010-09-18 01:14:59 +00:00
|
|
|
// The DOMFile takes ownership of the buffer
|
2014-06-26 16:47:44 +00:00
|
|
|
nsRefPtr<DOMFile> file =
|
|
|
|
DOMFile::CreateMemoryFile(imgData, (uint32_t)imgSize, aName, type,
|
|
|
|
PR_Now());
|
2010-09-18 01:14:59 +00:00
|
|
|
|
2011-11-16 07:50:19 +00:00
|
|
|
file.forget(aResult);
|
|
|
|
return NS_OK;
|
2010-09-18 01:14:59 +00:00
|
|
|
}
|
|
|
|
|
2010-03-24 10:47:18 +00:00
|
|
|
nsresult
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement::GetContextHelper(const nsAString& aContextId,
|
|
|
|
nsICanvasRenderingContextInternal **aContext)
|
2010-03-24 10:47:18 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aContext);
|
|
|
|
|
2012-11-14 00:35:36 +00:00
|
|
|
if (aContextId.EqualsLiteral("2d")) {
|
|
|
|
Telemetry::Accumulate(Telemetry::CANVAS_2D_USED, 1);
|
|
|
|
nsRefPtr<CanvasRenderingContext2D> ctx =
|
|
|
|
new CanvasRenderingContext2D();
|
|
|
|
|
|
|
|
ctx->SetCanvasElement(this);
|
|
|
|
ctx.forget(aContext);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-08-12 16:04:36 +00:00
|
|
|
|
2013-07-19 16:53:54 +00:00
|
|
|
if (WebGL2Context::IsSupported() &&
|
|
|
|
aContextId.EqualsLiteral("experimental-webgl2"))
|
2013-07-17 13:58:09 +00:00
|
|
|
{
|
|
|
|
Telemetry::Accumulate(Telemetry::CANVAS_WEBGL_USED, 1);
|
|
|
|
nsRefPtr<WebGL2Context> ctx = WebGL2Context::Create();
|
|
|
|
|
|
|
|
if (ctx == nullptr) {
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->SetCanvasElement(this);
|
|
|
|
ctx.forget(aContext);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-11-14 00:35:36 +00:00
|
|
|
|
2011-07-08 10:42:21 +00:00
|
|
|
NS_ConvertUTF16toUTF8 ctxId(aContextId);
|
2010-03-24 10:47:18 +00:00
|
|
|
|
|
|
|
// check that ctxId is clamped to A-Za-z0-9_-
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < ctxId.Length(); i++) {
|
2010-03-24 10:47:18 +00:00
|
|
|
if ((ctxId[i] < 'A' || ctxId[i] > 'Z') &&
|
|
|
|
(ctxId[i] < 'a' || ctxId[i] > 'z') &&
|
|
|
|
(ctxId[i] < '0' || ctxId[i] > '9') &&
|
|
|
|
(ctxId[i] != '-') &&
|
|
|
|
(ctxId[i] != '_'))
|
|
|
|
{
|
|
|
|
// XXX ERRMSG we need to report an error to developers here! (bug 329026)
|
2010-07-12 14:51:17 +00:00
|
|
|
return NS_OK;
|
2010-03-24 10:47:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString ctxString("@mozilla.org/content/canvas-rendering-context;1?id=");
|
|
|
|
ctxString.Append(ctxId);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsICanvasRenderingContextInternal> ctx =
|
2011-06-09 20:17:58 +00:00
|
|
|
do_CreateInstance(ctxString.get(), &rv);
|
2010-03-24 10:47:18 +00:00
|
|
|
if (rv == NS_ERROR_OUT_OF_MEMORY) {
|
2012-07-30 14:20:58 +00:00
|
|
|
*aContext = nullptr;
|
2010-03-24 10:47:18 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
2012-07-30 14:20:58 +00:00
|
|
|
*aContext = nullptr;
|
2010-03-24 10:47:18 +00:00
|
|
|
// XXX ERRMSG we need to report an error to developers here! (bug 329026)
|
2010-07-12 14:51:17 +00:00
|
|
|
return NS_OK;
|
2010-03-24 10:47:18 +00:00
|
|
|
}
|
|
|
|
|
2012-06-06 07:42:47 +00:00
|
|
|
ctx->SetCanvasElement(this);
|
2011-11-16 07:50:19 +00:00
|
|
|
ctx.forget(aContext);
|
2012-06-06 07:42:47 +00:00
|
|
|
return NS_OK;
|
2010-03-24 10:47:18 +00:00
|
|
|
}
|
|
|
|
|
2013-04-13 07:05:33 +00:00
|
|
|
nsresult
|
|
|
|
HTMLCanvasElement::GetContext(const nsAString& aContextId,
|
|
|
|
nsISupports** aContext)
|
|
|
|
{
|
2013-06-07 18:42:12 +00:00
|
|
|
ErrorResult rv;
|
2014-03-15 19:00:15 +00:00
|
|
|
*aContext =
|
|
|
|
GetContext(nullptr, aContextId, JS::NullHandleValue, rv).take();
|
2013-06-07 18:42:12 +00:00
|
|
|
return rv.ErrorCode();
|
2013-04-13 07:05:33 +00:00
|
|
|
}
|
|
|
|
|
2013-09-06 18:48:18 +00:00
|
|
|
static bool
|
|
|
|
IsContextIdWebGL(const nsAString& str)
|
|
|
|
{
|
|
|
|
return str.EqualsLiteral("webgl") ||
|
2014-01-27 20:59:24 +00:00
|
|
|
str.EqualsLiteral("experimental-webgl");
|
2013-09-06 18:48:18 +00:00
|
|
|
}
|
|
|
|
|
2013-06-07 18:42:12 +00:00
|
|
|
already_AddRefed<nsISupports>
|
|
|
|
HTMLCanvasElement::GetContext(JSContext* aCx,
|
|
|
|
const nsAString& aContextId,
|
|
|
|
JS::Handle<JS::Value> aContextOptions,
|
|
|
|
ErrorResult& rv)
|
2005-04-18 06:33:23 +00:00
|
|
|
{
|
2012-04-25 22:04:36 +00:00
|
|
|
if (mCurrentContextId.IsEmpty()) {
|
|
|
|
rv = GetContextHelper(aContextId, getter_AddRefs(mCurrentContext));
|
2013-06-07 18:42:12 +00:00
|
|
|
if (rv.Failed() || !mCurrentContext) {
|
|
|
|
return nullptr;
|
2010-07-15 20:39:31 +00:00
|
|
|
}
|
2005-04-18 06:33:23 +00:00
|
|
|
|
2010-06-15 21:38:05 +00:00
|
|
|
// Ensure that the context participates in CC. Note that returning a
|
|
|
|
// CC participant from QI doesn't addref.
|
2012-07-30 14:20:58 +00:00
|
|
|
nsXPCOMCycleCollectionParticipant *cp = nullptr;
|
2010-06-15 21:38:05 +00:00
|
|
|
CallQueryInterface(mCurrentContext, &cp);
|
|
|
|
if (!cp) {
|
2012-07-30 14:20:58 +00:00
|
|
|
mCurrentContext = nullptr;
|
2013-06-07 18:42:12 +00:00
|
|
|
rv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
2010-06-15 21:38:05 +00:00
|
|
|
}
|
|
|
|
|
2013-06-07 18:42:12 +00:00
|
|
|
rv = UpdateContext(aCx, aContextOptions);
|
|
|
|
if (rv.Failed()) {
|
2013-08-29 14:14:22 +00:00
|
|
|
rv = NS_OK; // See bug 645792
|
2013-06-07 18:42:12 +00:00
|
|
|
return nullptr;
|
2007-06-25 16:24:19 +00:00
|
|
|
}
|
2010-03-24 10:47:18 +00:00
|
|
|
mCurrentContextId.Assign(aContextId);
|
2011-07-08 10:42:21 +00:00
|
|
|
}
|
2012-04-25 22:04:36 +00:00
|
|
|
|
2011-07-08 10:42:21 +00:00
|
|
|
if (!mCurrentContextId.Equals(aContextId)) {
|
2013-09-06 18:48:18 +00:00
|
|
|
if (IsContextIdWebGL(aContextId) &&
|
|
|
|
IsContextIdWebGL(mCurrentContextId))
|
|
|
|
{
|
|
|
|
// Warn when we get a request for a webgl context with an id that differs
|
|
|
|
// from the id it was created with.
|
|
|
|
nsCString creationId = NS_LossyConvertUTF16toASCII(mCurrentContextId);
|
|
|
|
nsCString requestId = NS_LossyConvertUTF16toASCII(aContextId);
|
|
|
|
JS_ReportWarning(aCx, "WebGL: Retrieving a WebGL context from a canvas "
|
|
|
|
"via a request id ('%s') different from the id used "
|
|
|
|
"to create the context ('%s') is not allowed.",
|
|
|
|
requestId.get(),
|
|
|
|
creationId.get());
|
|
|
|
}
|
|
|
|
|
2010-03-24 10:47:18 +00:00
|
|
|
//XXX eventually allow for more than one active context on a given canvas
|
2013-06-07 18:42:12 +00:00
|
|
|
return nullptr;
|
2010-03-24 10:47:18 +00:00
|
|
|
}
|
|
|
|
|
2013-06-07 18:42:12 +00:00
|
|
|
nsCOMPtr<nsICanvasRenderingContextInternal> context = mCurrentContext;
|
|
|
|
return context.forget();
|
2010-03-24 10:47:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement::MozGetIPCContext(const nsAString& aContextId,
|
|
|
|
nsISupports **aContext)
|
2010-03-24 10:47:18 +00:00
|
|
|
{
|
2012-10-25 23:10:53 +00:00
|
|
|
if(!nsContentUtils::IsCallerChrome()) {
|
2010-03-24 10:47:18 +00:00
|
|
|
// XXX ERRMSG we need to report an error to developers here! (bug 329026)
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We only support 2d shmem contexts for now.
|
2014-05-22 03:48:51 +00:00
|
|
|
if (!aContextId.EqualsLiteral("2d"))
|
2010-03-24 10:47:18 +00:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
if (mCurrentContextId.IsEmpty()) {
|
2012-04-25 22:04:36 +00:00
|
|
|
nsresult rv = GetContextHelper(aContextId, getter_AddRefs(mCurrentContext));
|
2010-07-15 20:39:31 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!mCurrentContext) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-03-24 10:47:18 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mCurrentContext->SetIsIPC(true);
|
2010-03-24 10:47:18 +00:00
|
|
|
|
2013-06-07 18:42:12 +00:00
|
|
|
rv = UpdateContext(nullptr, JS::NullHandleValue);
|
2011-08-01 07:20:05 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2005-04-18 06:33:23 +00:00
|
|
|
|
|
|
|
mCurrentContextId.Assign(aContextId);
|
|
|
|
} else if (!mCurrentContextId.Equals(aContextId)) {
|
2005-05-07 03:45:06 +00:00
|
|
|
//XXX eventually allow for more than one active context on a given canvas
|
2005-04-18 06:33:23 +00:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ADDREF (*aContext = mCurrentContext);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-06-07 18:42:12 +00:00
|
|
|
HTMLCanvasElement::UpdateContext(JSContext* aCx, JS::Handle<JS::Value> aNewContextOptions)
|
2005-04-18 06:33:23 +00:00
|
|
|
{
|
2010-11-17 04:33:03 +00:00
|
|
|
if (!mCurrentContext)
|
|
|
|
return NS_OK;
|
|
|
|
|
2011-05-20 19:53:53 +00:00
|
|
|
nsIntSize sz = GetWidthHeight();
|
2010-11-17 04:33:03 +00:00
|
|
|
|
2014-03-16 19:34:14 +00:00
|
|
|
nsresult rv = mCurrentContext->SetIsOpaque(HasAttr(kNameSpaceID_None, nsGkAtoms::moz_opaque));
|
2011-05-20 19:53:53 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-07-30 14:20:58 +00:00
|
|
|
mCurrentContext = nullptr;
|
2011-11-16 07:50:19 +00:00
|
|
|
mCurrentContextId.Truncate();
|
2010-11-17 04:33:03 +00:00
|
|
|
return rv;
|
2011-05-20 19:53:53 +00:00
|
|
|
}
|
2010-11-17 04:33:03 +00:00
|
|
|
|
2013-06-07 18:42:12 +00:00
|
|
|
rv = mCurrentContext->SetContextOptions(aCx, aNewContextOptions);
|
2011-05-20 19:53:53 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-07-30 14:20:58 +00:00
|
|
|
mCurrentContext = nullptr;
|
2011-11-16 07:50:19 +00:00
|
|
|
mCurrentContextId.Truncate();
|
2010-11-17 04:33:03 +00:00
|
|
|
return rv;
|
2011-05-20 19:53:53 +00:00
|
|
|
}
|
2010-11-17 04:33:03 +00:00
|
|
|
|
|
|
|
rv = mCurrentContext->SetDimensions(sz.width, sz.height);
|
2011-05-20 19:53:53 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-07-30 14:20:58 +00:00
|
|
|
mCurrentContext = nullptr;
|
2011-11-16 07:50:19 +00:00
|
|
|
mCurrentContextId.Truncate();
|
2010-11-17 04:33:03 +00:00
|
|
|
return rv;
|
2011-05-20 19:53:53 +00:00
|
|
|
}
|
2005-04-18 06:33:23 +00:00
|
|
|
|
2006-04-18 21:23:15 +00:00
|
|
|
return rv;
|
2005-04-18 06:33:23 +00:00
|
|
|
}
|
|
|
|
|
2010-05-18 04:04:22 +00:00
|
|
|
nsIntSize
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement::GetSize()
|
2005-04-18 06:33:23 +00:00
|
|
|
{
|
2010-05-18 04:04:22 +00:00
|
|
|
return GetWidthHeight();
|
2005-05-12 21:34:56 +00:00
|
|
|
}
|
2006-03-02 00:44:01 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement::IsWriteOnly()
|
2006-03-02 00:44:01 +00:00
|
|
|
{
|
|
|
|
return mWriteOnly;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement::SetWriteOnly()
|
2006-03-02 00:44:01 +00:00
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
mWriteOnly = true;
|
2006-03-02 00:44:01 +00:00
|
|
|
}
|
2007-09-25 20:46:08 +00:00
|
|
|
|
2010-05-18 04:04:22 +00:00
|
|
|
void
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement::InvalidateCanvasContent(const gfx::Rect* damageRect)
|
2007-09-25 20:46:08 +00:00
|
|
|
{
|
2009-07-09 21:09:43 +00:00
|
|
|
// We don't need to flush anything here; if there's no frame or if
|
|
|
|
// we plan to reframe we don't need to invalidate it anyway.
|
2009-12-24 21:20:05 +00:00
|
|
|
nsIFrame *frame = GetPrimaryFrame();
|
2010-05-18 04:04:22 +00:00
|
|
|
if (!frame)
|
|
|
|
return;
|
2007-09-25 20:46:08 +00:00
|
|
|
|
2013-09-04 11:30:57 +00:00
|
|
|
ActiveLayerTracker::NotifyContentChange(frame);
|
2011-03-27 23:59:47 +00:00
|
|
|
|
2012-10-12 01:31:39 +00:00
|
|
|
Layer* layer = nullptr;
|
2010-05-18 04:04:22 +00:00
|
|
|
if (damageRect) {
|
2009-04-08 20:27:20 +00:00
|
|
|
nsIntSize size = GetWidthHeight();
|
2011-05-19 10:49:18 +00:00
|
|
|
if (size.width != 0 && size.height != 0) {
|
|
|
|
|
2012-11-04 08:05:25 +00:00
|
|
|
gfx::Rect realRect(*damageRect);
|
2011-05-19 10:49:18 +00:00
|
|
|
realRect.RoundOut();
|
|
|
|
|
2012-11-04 08:05:25 +00:00
|
|
|
// then make it a nsIntRect
|
2012-08-29 05:47:18 +00:00
|
|
|
nsIntRect invalRect(realRect.X(), realRect.Y(),
|
|
|
|
realRect.Width(), realRect.Height());
|
2012-07-04 00:24:55 +00:00
|
|
|
|
2012-08-29 05:47:18 +00:00
|
|
|
layer = frame->InvalidateLayer(nsDisplayItem::TYPE_CANVAS, &invalRect);
|
2011-05-19 10:49:18 +00:00
|
|
|
}
|
2011-03-29 18:31:53 +00:00
|
|
|
} else {
|
2012-08-29 05:47:18 +00:00
|
|
|
layer = frame->InvalidateLayer(nsDisplayItem::TYPE_CANVAS);
|
2011-03-27 23:59:47 +00:00
|
|
|
}
|
|
|
|
if (layer) {
|
|
|
|
static_cast<CanvasLayer*>(layer)->Updated();
|
2007-09-25 20:46:08 +00:00
|
|
|
}
|
2012-06-29 21:35:06 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Treat canvas invalidations as animation activity for JS. Frequently
|
|
|
|
* invalidating a canvas will feed into heuristics and cause JIT code to be
|
|
|
|
* kept around longer, for smoother animations.
|
|
|
|
*/
|
2013-06-21 14:25:20 +00:00
|
|
|
nsCOMPtr<nsIGlobalObject> global =
|
|
|
|
do_QueryInterface(OwnerDoc()->GetInnerWindow());
|
|
|
|
|
|
|
|
if (global) {
|
|
|
|
if (JSObject *obj = global->GetGlobalJSObject()) {
|
2012-06-29 21:35:06 +00:00
|
|
|
js::NotifyAnimationActivity(obj);
|
|
|
|
}
|
|
|
|
}
|
2007-09-25 20:46:08 +00:00
|
|
|
}
|
2008-04-30 17:04:46 +00:00
|
|
|
|
2011-03-27 23:59:47 +00:00
|
|
|
void
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement::InvalidateCanvas()
|
2011-03-27 23:59:47 +00:00
|
|
|
{
|
|
|
|
// We don't need to flush anything here; if there's no frame or if
|
|
|
|
// we plan to reframe we don't need to invalidate it anyway.
|
|
|
|
nsIFrame *frame = GetPrimaryFrame();
|
|
|
|
if (!frame)
|
|
|
|
return;
|
|
|
|
|
2012-08-29 05:39:31 +00:00
|
|
|
frame->InvalidateFrame();
|
2011-03-27 23:59:47 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement::CountContexts()
|
2008-04-30 17:04:46 +00:00
|
|
|
{
|
|
|
|
if (mCurrentContext)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsICanvasRenderingContextInternal *
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement::GetContextAtIndex(int32_t index)
|
2008-04-30 17:04:46 +00:00
|
|
|
{
|
|
|
|
if (mCurrentContext && index == 0)
|
2011-11-16 07:50:19 +00:00
|
|
|
return mCurrentContext;
|
2008-04-30 17:04:46 +00:00
|
|
|
|
2013-04-03 01:14:24 +00:00
|
|
|
return nullptr;
|
2008-04-30 17:04:46 +00:00
|
|
|
}
|
2009-03-05 13:43:45 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement::GetIsOpaque()
|
2009-03-05 13:43:45 +00:00
|
|
|
{
|
2014-03-16 19:34:14 +00:00
|
|
|
if (mCurrentContext) {
|
|
|
|
return mCurrentContext->GetIsOpaque();
|
|
|
|
}
|
|
|
|
|
2009-03-05 13:43:45 +00:00
|
|
|
return HasAttr(kNameSpaceID_None, nsGkAtoms::moz_opaque);
|
|
|
|
}
|
2010-05-18 04:04:22 +00:00
|
|
|
|
|
|
|
already_AddRefed<CanvasLayer>
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement::GetCanvasLayer(nsDisplayListBuilder* aBuilder,
|
|
|
|
CanvasLayer *aOldLayer,
|
|
|
|
LayerManager *aManager)
|
2010-05-18 04:04:22 +00:00
|
|
|
{
|
|
|
|
if (!mCurrentContext)
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2010-05-18 04:04:22 +00:00
|
|
|
|
2011-03-27 23:59:47 +00:00
|
|
|
return mCurrentContext->GetCanvasLayer(aBuilder, aOldLayer, aManager);
|
2010-05-18 04:04:22 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement::ShouldForceInactiveLayer(LayerManager *aManager)
|
2011-07-08 10:42:21 +00:00
|
|
|
{
|
|
|
|
return !mCurrentContext || mCurrentContext->ShouldForceInactiveLayer(aManager);
|
|
|
|
}
|
|
|
|
|
2010-05-18 04:04:22 +00:00
|
|
|
void
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement::MarkContextClean()
|
2010-05-18 04:04:22 +00:00
|
|
|
{
|
|
|
|
if (!mCurrentContext)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mCurrentContext->MarkContextClean();
|
|
|
|
}
|
|
|
|
|
2014-04-17 05:29:52 +00:00
|
|
|
TemporaryRef<SourceSurface>
|
|
|
|
HTMLCanvasElement::GetSurfaceSnapshot(bool* aPremultAlpha)
|
|
|
|
{
|
|
|
|
if (!mCurrentContext)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return mCurrentContext->GetSurfaceSnapshot(aPremultAlpha);
|
|
|
|
}
|
|
|
|
|
2013-01-04 05:16:14 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|