2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04: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 10:33:23 +04:00
|
|
|
|
2013-01-04 09:16:14 +04:00
|
|
|
#include "mozilla/dom/HTMLCanvasElement.h"
|
2010-05-18 08:04:22 +04:00
|
|
|
|
2013-10-17 06:55:08 +04:00
|
|
|
#include "ImageEncoder.h"
|
2013-04-13 11:03:57 +04:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jsfriendapi.h"
|
2013-10-17 06:55:08 +04:00
|
|
|
#include "Layers.h"
|
2015-09-17 07:36:57 +03:00
|
|
|
#include "MediaSegment.h"
|
2014-11-26 22:29:49 +03:00
|
|
|
#include "mozilla/Assertions.h"
|
2011-06-08 22:12:18 +04:00
|
|
|
#include "mozilla/Base64.h"
|
2012-05-14 23:50:20 +04:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2015-05-13 09:04:30 +03:00
|
|
|
#include "mozilla/dom/CanvasCaptureMediaStream.h"
|
2013-04-13 11:03:57 +04:00
|
|
|
#include "mozilla/dom/CanvasRenderingContext2D.h"
|
2014-10-08 20:15:23 +04:00
|
|
|
#include "mozilla/dom/File.h"
|
2013-04-13 11:05:51 +04:00
|
|
|
#include "mozilla/dom/HTMLCanvasElementBinding.h"
|
2014-05-20 08:54:00 +04:00
|
|
|
#include "mozilla/dom/MouseEvent.h"
|
|
|
|
#include "mozilla/EventDispatcher.h"
|
2012-11-04 12:05:25 +04:00
|
|
|
#include "mozilla/gfx/Rect.h"
|
2015-10-05 05:50:15 +03:00
|
|
|
#include "mozilla/layers/AsyncCanvasRenderer.h"
|
2014-05-20 08:54:00 +04:00
|
|
|
#include "mozilla/MouseEvents.h"
|
2012-11-04 12:05:25 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "mozilla/Telemetry.h"
|
2013-04-13 11:03:57 +04:00
|
|
|
#include "nsAttrValueInlines.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsDisplayList.h"
|
2013-10-17 06:55:08 +04:00
|
|
|
#include "nsDOMJSUtils.h"
|
2006-05-03 04:48:27 +04:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2013-04-13 11:03:57 +04:00
|
|
|
#include "nsITimer.h"
|
|
|
|
#include "nsIWritablePropertyBag2.h"
|
2006-05-03 04:48:27 +04:00
|
|
|
#include "nsIXPConnect.h"
|
2010-12-03 11:24:17 +03:00
|
|
|
#include "nsJSUtils.h"
|
2014-05-20 08:54:00 +04:00
|
|
|
#include "nsLayoutUtils.h"
|
2011-05-26 11:28:26 +04:00
|
|
|
#include "nsMathUtils.h"
|
2013-04-13 11:03:57 +04:00
|
|
|
#include "nsNetUtil.h"
|
2015-09-17 07:36:57 +03:00
|
|
|
#include "nsRefreshDriver.h"
|
2012-02-03 18:23:18 +04:00
|
|
|
#include "nsStreamUtils.h"
|
2013-09-04 15:30:57 +04:00
|
|
|
#include "ActiveLayerTracker.h"
|
2014-11-11 02:16:50 +03:00
|
|
|
#include "WebGL1Context.h"
|
2014-07-26 01:15:35 +04:00
|
|
|
#include "WebGL2Context.h"
|
2013-07-17 17:58:09 +04:00
|
|
|
|
2010-05-18 08:04:22 +04:00
|
|
|
using namespace mozilla::layers;
|
2014-04-17 09:29:52 +04:00
|
|
|
using namespace mozilla::gfx;
|
2005-04-18 10:33:23 +04:00
|
|
|
|
2013-04-13 11:03:57 +04:00
|
|
|
NS_IMPL_NS_NEW_HTML_ELEMENT(Canvas)
|
2013-01-04 09:16:14 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2015-09-17 07:36:57 +03:00
|
|
|
class RequestedFrameRefreshObserver : public nsARefreshObserver
|
|
|
|
{
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RequestedFrameRefreshObserver, override)
|
|
|
|
|
|
|
|
public:
|
|
|
|
RequestedFrameRefreshObserver(HTMLCanvasElement* const aOwningElement,
|
|
|
|
nsRefreshDriver* aRefreshDriver)
|
|
|
|
: mRegistered(false),
|
|
|
|
mOwningElement(aOwningElement),
|
|
|
|
mRefreshDriver(aRefreshDriver)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mOwningElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
static already_AddRefed<DataSourceSurface>
|
|
|
|
CopySurface(const RefPtr<SourceSurface>& aSurface)
|
|
|
|
{
|
|
|
|
RefPtr<DataSourceSurface> data = aSurface->GetDataSurface();
|
|
|
|
if (!data) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataSourceSurface::ScopedMap read(data, DataSourceSurface::READ);
|
|
|
|
if (!read.IsMapped()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<DataSourceSurface> copy =
|
|
|
|
Factory::CreateDataSourceSurfaceWithStride(data->GetSize(),
|
|
|
|
data->GetFormat(),
|
|
|
|
read.GetStride());
|
|
|
|
if (!copy) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataSourceSurface::ScopedMap write(copy, DataSourceSurface::WRITE);
|
|
|
|
if (!write.IsMapped()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(read.GetStride() == write.GetStride());
|
|
|
|
MOZ_ASSERT(data->GetSize() == copy->GetSize());
|
|
|
|
MOZ_ASSERT(data->GetFormat() == copy->GetFormat());
|
|
|
|
|
|
|
|
memcpy(write.GetData(), read.GetData(),
|
|
|
|
write.GetStride() * copy->GetSize().height);
|
|
|
|
|
|
|
|
return copy.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WillRefresh(TimeStamp aTime) override
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!mOwningElement) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mOwningElement->IsWriteOnly()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mOwningElement->IsContextCleanForFrameCapture()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mOwningElement->IsFrameCaptureRequested()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<SourceSurface> snapshot = mOwningElement->GetSurfaceSnapshot(nullptr);
|
|
|
|
if (!snapshot) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<DataSourceSurface> copy = CopySurface(snapshot);
|
|
|
|
|
|
|
|
mOwningElement->SetFrameCapture(copy.forget());
|
|
|
|
mOwningElement->MarkContextCleanForFrameCapture();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DetachFromRefreshDriver()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mOwningElement);
|
|
|
|
MOZ_ASSERT(mRefreshDriver);
|
|
|
|
|
|
|
|
Unregister();
|
|
|
|
mRefreshDriver = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Register()
|
|
|
|
{
|
|
|
|
if (mRegistered) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mRefreshDriver);
|
|
|
|
if (mRefreshDriver) {
|
|
|
|
mRefreshDriver->AddRefreshObserver(this, Flush_Display);
|
|
|
|
mRegistered = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Unregister()
|
|
|
|
{
|
|
|
|
if (!mRegistered) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mRefreshDriver);
|
|
|
|
if (mRefreshDriver) {
|
|
|
|
mRefreshDriver->RemoveRefreshObserver(this, Flush_Display);
|
|
|
|
mRegistered = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~RequestedFrameRefreshObserver()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mRefreshDriver);
|
|
|
|
MOZ_ASSERT(!mRegistered);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mRegistered;
|
|
|
|
HTMLCanvasElement* const mOwningElement;
|
|
|
|
RefPtr<nsRefreshDriver> mRefreshDriver;
|
|
|
|
};
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2014-04-29 12:57:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(HTMLCanvasPrintState, mCanvas,
|
|
|
|
mContext, mCallback)
|
2013-09-12 02:35:51 +04: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 21:45:45 +04:00
|
|
|
{
|
2013-09-12 02:35:51 +04:00
|
|
|
}
|
2012-08-31 21:45:45 +04:00
|
|
|
|
2013-09-12 02:35:51 +04:00
|
|
|
HTMLCanvasPrintState::~HTMLCanvasPrintState()
|
|
|
|
{
|
|
|
|
}
|
2012-08-31 21:45:45 +04:00
|
|
|
|
2013-09-12 02:35:51 +04:00
|
|
|
/* virtual */ JSObject*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
HTMLCanvasPrintState::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2013-09-12 02:35:51 +04:00
|
|
|
{
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
return MozCanvasPrintStateBinding::Wrap(aCx, this, aGivenProto);
|
2013-09-12 02:35:51 +04:00
|
|
|
}
|
2012-08-31 21:45:45 +04:00
|
|
|
|
2013-09-12 02:35:51 +04: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 21:45:45 +04:00
|
|
|
}
|
|
|
|
}
|
2013-09-12 02:35:51 +04:00
|
|
|
}
|
2012-08-31 21:45:45 +04:00
|
|
|
|
2013-09-12 02:35:51 +04:00
|
|
|
void
|
|
|
|
HTMLCanvasPrintState::NotifyDone()
|
|
|
|
{
|
|
|
|
mIsDone = true;
|
|
|
|
mPendingNotify = false;
|
|
|
|
if (mCallback) {
|
|
|
|
mCallback->Notify(nullptr);
|
2012-08-31 21:45:45 +04:00
|
|
|
}
|
2013-09-12 02:35:51 +04:00
|
|
|
}
|
2012-08-31 21:45:45 +04:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2014-06-20 06:01:40 +04:00
|
|
|
HTMLCanvasElement::HTMLCanvasElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
|
2013-04-13 11:03:57 +04:00
|
|
|
: nsGenericHTMLElement(aNodeInfo),
|
2012-08-31 21:45:45 +04:00
|
|
|
mWriteOnly(false)
|
2005-04-18 10:33:23 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::~HTMLCanvasElement()
|
2005-04-18 10:33:23 +04:00
|
|
|
{
|
2012-08-31 21:45:45 +04:00
|
|
|
ResetPrintCallback();
|
2015-09-17 07:36:57 +03:00
|
|
|
if (mRequestedFrameRefreshObserver) {
|
|
|
|
mRequestedFrameRefreshObserver->DetachFromRefreshDriver();
|
|
|
|
}
|
2015-10-05 05:50:15 +03:00
|
|
|
|
|
|
|
if (mAsyncCanvasRenderer) {
|
|
|
|
mAsyncCanvasRenderer->mHTMLCanvasElement = nullptr;
|
|
|
|
}
|
2005-04-18 10:33:23 +04:00
|
|
|
}
|
|
|
|
|
2014-04-25 20:49:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(HTMLCanvasElement, nsGenericHTMLElement,
|
|
|
|
mCurrentContext, mPrintCallback,
|
|
|
|
mPrintState, mOriginalCanvas)
|
2011-06-22 01:12:50 +04:00
|
|
|
|
2013-01-04 09:16:14 +04:00
|
|
|
NS_IMPL_ADDREF_INHERITED(HTMLCanvasElement, Element)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(HTMLCanvasElement, Element)
|
2010-01-12 16:08:43 +03:00
|
|
|
|
2013-01-04 09:16:14 +04:00
|
|
|
NS_INTERFACE_TABLE_HEAD_CYCLE_COLLECTION_INHERITED(HTMLCanvasElement)
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_INTERFACE_TABLE_INHERITED(HTMLCanvasElement, nsIDOMHTMLCanvasElement)
|
2013-08-08 00:23:08 +04:00
|
|
|
NS_INTERFACE_TABLE_TAIL_INHERITING(nsGenericHTMLElement)
|
2005-04-18 10:33:23 +04:00
|
|
|
|
2013-01-04 09:16:14 +04:00
|
|
|
NS_IMPL_ELEMENT_CLONE(HTMLCanvasElement)
|
2005-04-18 10:33:23 +04:00
|
|
|
|
2013-04-13 11:05:51 +04:00
|
|
|
/* virtual */ JSObject*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
HTMLCanvasElement::WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2013-04-13 11:05:51 +04:00
|
|
|
{
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
return HTMLCanvasElementBinding::Wrap(aCx, this, aGivenProto);
|
2013-04-13 11:05:51 +04:00
|
|
|
}
|
|
|
|
|
2005-05-07 07:45:06 +04:00
|
|
|
nsIntSize
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::GetWidthHeight()
|
2005-04-18 10:33:23 +04:00
|
|
|
{
|
2011-05-19 14:49:18 +04:00
|
|
|
nsIntSize size(DEFAULT_CANVAS_WIDTH, DEFAULT_CANVAS_HEIGHT);
|
2005-04-18 10:33:23 +04:00
|
|
|
const nsAttrValue* value;
|
|
|
|
|
2006-12-26 20:47:52 +03:00
|
|
|
if ((value = GetParsedAttr(nsGkAtoms::width)) &&
|
2005-05-07 07:45:06 +04:00
|
|
|
value->Type() == nsAttrValue::eInteger)
|
|
|
|
{
|
2005-04-18 10:33:23 +04:00
|
|
|
size.width = value->GetIntegerValue();
|
|
|
|
}
|
|
|
|
|
2006-12-26 20:47:52 +03:00
|
|
|
if ((value = GetParsedAttr(nsGkAtoms::height)) &&
|
2005-05-07 07:45:06 +04:00
|
|
|
value->Type() == nsAttrValue::eInteger)
|
|
|
|
{
|
2005-04-18 10:33:23 +04:00
|
|
|
size.height = value->GetIntegerValue();
|
|
|
|
}
|
|
|
|
|
2014-11-26 22:29:49 +03:00
|
|
|
MOZ_ASSERT(size.width >= 0 && size.height >= 0,
|
|
|
|
"we should've required <canvas> width/height attrs to be "
|
|
|
|
"unsigned (non-negative) values");
|
|
|
|
|
2005-05-07 07:45:06 +04:00
|
|
|
return size;
|
2005-04-18 10:33:23 +04:00
|
|
|
}
|
|
|
|
|
2013-01-04 09:16:14 +04: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 10:33:23 +04:00
|
|
|
|
|
|
|
nsresult
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::SetAttr(int32_t aNameSpaceID, nsIAtom* aName,
|
|
|
|
nsIAtom* aPrefix, const nsAString& aValue,
|
|
|
|
bool aNotify)
|
2005-04-18 10:33:23 +04:00
|
|
|
{
|
|
|
|
nsresult rv = nsGenericHTMLElement::SetAttr(aNameSpaceID, aName, aPrefix, aValue,
|
|
|
|
aNotify);
|
|
|
|
if (NS_SUCCEEDED(rv) && mCurrentContext &&
|
2013-10-01 01:02:40 +04:00
|
|
|
aNameSpaceID == kNameSpaceID_None &&
|
2008-06-04 04:00:32 +04:00
|
|
|
(aName == nsGkAtoms::width || aName == nsGkAtoms::height || aName == nsGkAtoms::moz_opaque))
|
2005-04-18 10:33:23 +04:00
|
|
|
{
|
2013-06-07 22:42:12 +04:00
|
|
|
rv = UpdateContext(nullptr, JS::NullHandleValue);
|
2005-04-18 10:33:23 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2013-10-01 01:02:40 +04: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 21:45:45 +04:00
|
|
|
void
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::HandlePrintCallback(nsPresContext::nsPresContextType aType)
|
2012-08-31 21:45:45 +04: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-12 02:35:51 +04:00
|
|
|
!mPrintState && GetMozPrintCallback()) {
|
2012-08-31 21:45:45 +04:00
|
|
|
DispatchPrintCallback(nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::DispatchPrintCallback(nsITimerCallback* aCallback)
|
2012-08-31 21:45:45 +04: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 11:05:33 +04:00
|
|
|
rv = GetContext(NS_LITERAL_STRING("2d"), getter_AddRefs(context));
|
2012-08-31 21:45:45 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2013-01-04 09:16:14 +04:00
|
|
|
mPrintState = new HTMLCanvasPrintState(this, mCurrentContext, aCallback);
|
2012-08-31 21:45:45 +04:00
|
|
|
|
2013-01-04 09:16:14 +04:00
|
|
|
nsRefPtr<nsRunnableMethod<HTMLCanvasElement> > renderEvent =
|
|
|
|
NS_NewRunnableMethod(this, &HTMLCanvasElement::CallPrintCallback);
|
2012-08-31 21:45:45 +04:00
|
|
|
return NS_DispatchToCurrentThread(renderEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::CallPrintCallback()
|
2012-08-31 21:45:45 +04:00
|
|
|
{
|
2013-09-12 02:35:51 +04:00
|
|
|
ErrorResult rv;
|
|
|
|
GetMozPrintCallback()->Call(*mPrintState, rv);
|
2012-08-31 21:45:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::ResetPrintCallback()
|
2012-08-31 21:45:45 +04:00
|
|
|
{
|
|
|
|
if (mPrintState) {
|
|
|
|
mPrintState = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::IsPrintCallbackDone()
|
2012-08-31 21:45:45 +04:00
|
|
|
{
|
|
|
|
if (mPrintState == nullptr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mPrintState->mIsDone;
|
|
|
|
}
|
|
|
|
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement*
|
|
|
|
HTMLCanvasElement::GetOriginalCanvas()
|
2012-08-31 21:45:45 +04:00
|
|
|
{
|
|
|
|
return mOriginalCanvas ? mOriginalCanvas.get() : this;
|
|
|
|
}
|
|
|
|
|
2009-12-11 07:02:13 +03:00
|
|
|
nsresult
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::CopyInnerTo(Element* aDest)
|
2009-12-11 07:02:13 +03:00
|
|
|
{
|
|
|
|
nsresult rv = nsGenericHTMLElement::CopyInnerTo(aDest);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-10-18 14:53:36 +04:00
|
|
|
if (aDest->OwnerDoc()->IsStaticDocument()) {
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement* dest = static_cast<HTMLCanvasElement*>(aDest);
|
2013-04-13 11:05:33 +04:00
|
|
|
dest->mOriginalCanvas = this;
|
2012-08-31 21:45:45 +04:00
|
|
|
|
2009-12-11 07:02:13 +03:00
|
|
|
nsCOMPtr<nsISupports> cxt;
|
2013-04-13 11:05:33 +04:00
|
|
|
dest->GetContext(NS_LITERAL_STRING("2d"), getter_AddRefs(cxt));
|
2012-11-14 04:35:36 +04:00
|
|
|
nsRefPtr<CanvasRenderingContext2D> context2d =
|
|
|
|
static_cast<CanvasRenderingContext2D*>(cxt.get());
|
2013-04-13 11:05:33 +04:00
|
|
|
if (context2d && !mPrintCallback) {
|
2015-07-30 21:49:00 +03:00
|
|
|
CanvasImageSource source;
|
|
|
|
source.SetAsHTMLCanvasElement() = this;
|
2012-11-14 04:35:36 +04:00
|
|
|
ErrorResult err;
|
2015-07-30 21:49:00 +03:00
|
|
|
context2d->DrawImage(source,
|
2012-11-22 01:55:37 +04:00
|
|
|
0.0, 0.0, err);
|
2015-04-27 16:18:52 +03:00
|
|
|
rv = err.StealNSResult();
|
2009-12-11 07:02:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-05-20 08:54:00 +04:00
|
|
|
nsresult HTMLCanvasElement::PreHandleEvent(EventChainPreVisitor& aVisitor)
|
|
|
|
{
|
2014-08-04 09:28:50 +04:00
|
|
|
if (aVisitor.mEvent->mClass == eMouseEventClass) {
|
2014-05-20 08:54:00 +04: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 10:33:23 +04:00
|
|
|
nsChangeHint
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::GetAttributeChangeHint(const nsIAtom* aAttribute,
|
|
|
|
int32_t aModType) const
|
2005-04-18 10:33:23 +04:00
|
|
|
{
|
|
|
|
nsChangeHint retval =
|
|
|
|
nsGenericHTMLElement::GetAttributeChangeHint(aAttribute, aModType);
|
2006-12-26 20:47:52 +03:00
|
|
|
if (aAttribute == nsGkAtoms::width ||
|
|
|
|
aAttribute == nsGkAtoms::height)
|
2005-04-18 10:33:23 +04:00
|
|
|
{
|
2005-05-07 07:45:06 +04:00
|
|
|
NS_UpdateHint(retval, NS_STYLE_HINT_REFLOW);
|
2008-06-04 04:00:32 +04:00
|
|
|
} else if (aAttribute == nsGkAtoms::moz_opaque)
|
|
|
|
{
|
2012-02-07 15:31:24 +04:00
|
|
|
NS_UpdateHint(retval, NS_STYLE_HINT_VISUAL);
|
2005-04-18 10:33:23 +04:00
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::ParseAttribute(int32_t aNamespaceID,
|
|
|
|
nsIAtom* aAttribute,
|
|
|
|
const nsAString& aValue,
|
|
|
|
nsAttrValue& aResult)
|
2005-04-18 10:33:23 +04:00
|
|
|
{
|
2010-04-23 06:53:09 +04:00
|
|
|
if (aNamespaceID == kNameSpaceID_None &&
|
|
|
|
(aAttribute == nsGkAtoms::width || aAttribute == nsGkAtoms::height)) {
|
2011-03-25 14:40:55 +03:00
|
|
|
return aResult.ParseNonNegativeIntValue(aValue);
|
2005-04-18 10:33:23 +04:00
|
|
|
}
|
|
|
|
|
2005-11-29 19:37:15 +03:00
|
|
|
return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
|
|
|
|
aResult);
|
2005-04-18 10:33:23 +04:00
|
|
|
}
|
|
|
|
|
2005-09-01 02:16:51 +04:00
|
|
|
|
2013-01-04 09:16:14 +04:00
|
|
|
// HTMLCanvasElement::toDataURL
|
2005-09-01 02:16:51 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-09 21:39:36 +04:00
|
|
|
HTMLCanvasElement::ToDataURL(const nsAString& aType, JS::Handle<JS::Value> aParams,
|
2013-04-13 11:05:22 +04:00
|
|
|
JSContext* aCx, nsAString& aDataURL)
|
2005-09-01 02:16:51 +04:00
|
|
|
{
|
2006-10-12 18:38:35 +04:00
|
|
|
// do a trust check if this is a write-only canvas
|
2012-10-26 03:10:53 +04:00
|
|
|
if (mWriteOnly && !nsContentUtils::IsCallerChrome()) {
|
2006-10-12 18:38:35 +04:00
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
2006-05-03 04:48:27 +04:00
|
|
|
}
|
|
|
|
|
2013-04-13 11:05:22 +04:00
|
|
|
return ToDataURLImpl(aCx, aType, aParams, aDataURL);
|
2005-09-01 02:16:51 +04:00
|
|
|
}
|
|
|
|
|
2013-09-12 02:35:51 +04:00
|
|
|
void
|
|
|
|
HTMLCanvasElement::SetMozPrintCallback(PrintCallback* aCallback)
|
2012-08-31 21:45:45 +04:00
|
|
|
{
|
|
|
|
mPrintCallback = aCallback;
|
|
|
|
}
|
|
|
|
|
2013-09-12 02:35:51 +04:00
|
|
|
PrintCallback*
|
2013-04-13 11:05:40 +04:00
|
|
|
HTMLCanvasElement::GetMozPrintCallback() const
|
2012-08-31 21:45:45 +04:00
|
|
|
{
|
|
|
|
if (mOriginalCanvas) {
|
2013-04-13 11:05:40 +04:00
|
|
|
return mOriginalCanvas->GetMozPrintCallback();
|
2012-08-31 21:45:45 +04:00
|
|
|
}
|
2013-04-13 11:05:40 +04:00
|
|
|
return mPrintCallback;
|
|
|
|
}
|
|
|
|
|
2015-05-13 09:04:30 +03:00
|
|
|
already_AddRefed<CanvasCaptureMediaStream>
|
|
|
|
HTMLCanvasElement::CaptureStream(const Optional<double>& aFrameRate,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2015-05-13 09:04:41 +03:00
|
|
|
if (IsWriteOnly()) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDOMWindow* window = OwnerDoc()->GetInnerWindow();
|
|
|
|
if (!window) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mCurrentContext) {
|
|
|
|
aRv.Throw(NS_ERROR_NOT_INITIALIZED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<CanvasCaptureMediaStream> stream =
|
|
|
|
CanvasCaptureMediaStream::CreateSourceStream(window, this);
|
|
|
|
if (!stream) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<nsIPrincipal> principal = NodePrincipal();
|
|
|
|
stream->CombineWithPrincipal(principal);
|
|
|
|
|
|
|
|
TrackID videoTrackId = 1;
|
|
|
|
nsresult rv = stream->Init(aFrameRate, videoTrackId);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-09-17 07:36:57 +03:00
|
|
|
|
2015-09-30 04:31:54 +03:00
|
|
|
stream->CreateOwnDOMTrack(videoTrackId, MediaSegment::VIDEO);
|
2015-09-17 07:36:57 +03:00
|
|
|
RegisterFrameCaptureListener(stream->FrameCaptureListener());
|
2015-05-13 09:04:41 +03:00
|
|
|
return stream.forget();
|
2015-05-13 09:04:30 +03:00
|
|
|
}
|
|
|
|
|
2006-05-03 04:48:27 +04:00
|
|
|
nsresult
|
2013-10-17 06:55:08 +04:00
|
|
|
HTMLCanvasElement::ExtractData(nsAString& aType,
|
2013-01-04 09:16:14 +04:00
|
|
|
const nsAString& aOptions,
|
2013-10-17 06:55:08 +04:00
|
|
|
nsIInputStream** aStream)
|
2005-09-01 02:16:51 +04:00
|
|
|
{
|
2013-10-17 06:55:08 +04:00
|
|
|
return ImageEncoder::ExtractData(aType,
|
|
|
|
aOptions,
|
|
|
|
GetSize(),
|
|
|
|
mCurrentContext,
|
|
|
|
aStream);
|
2010-09-18 05:14:59 +04:00
|
|
|
}
|
|
|
|
|
2015-09-29 18:57:36 +03:00
|
|
|
nsresult
|
|
|
|
HTMLCanvasElement::ParseParams(JSContext* aCx,
|
|
|
|
const nsAString& aType,
|
|
|
|
const JS::Value& aEncoderOptions,
|
|
|
|
nsAString& aParams,
|
|
|
|
bool* usingCustomParseOptions)
|
|
|
|
{
|
|
|
|
// Quality parameter is only valid for the image/jpeg MIME type
|
|
|
|
if (aType.EqualsLiteral("image/jpeg")) {
|
|
|
|
if (aEncoderOptions.isNumber()) {
|
|
|
|
double quality = aEncoderOptions.toNumber();
|
|
|
|
// Quality must be between 0.0 and 1.0, inclusive
|
|
|
|
if (quality >= 0.0 && quality <= 1.0) {
|
|
|
|
aParams.AppendLiteral("quality=");
|
|
|
|
aParams.AppendInt(NS_lround(quality * 100.0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we haven't parsed the aParams check for proprietary options.
|
|
|
|
// The proprietary option -moz-parse-options will take a image lib encoder
|
|
|
|
// parse options string as is and pass it to the encoder.
|
|
|
|
*usingCustomParseOptions = false;
|
|
|
|
if (aParams.Length() == 0 && aEncoderOptions.isString()) {
|
|
|
|
NS_NAMED_LITERAL_STRING(mozParseOptions, "-moz-parse-options:");
|
|
|
|
nsAutoJSString paramString;
|
|
|
|
if (!paramString.init(aCx, aEncoderOptions.toString())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
if (StringBeginsWith(paramString, mozParseOptions)) {
|
|
|
|
nsDependentSubstring parseOptions = Substring(paramString,
|
|
|
|
mozParseOptions.Length(),
|
|
|
|
paramString.Length() -
|
|
|
|
mozParseOptions.Length());
|
|
|
|
aParams.Append(parseOptions);
|
|
|
|
*usingCustomParseOptions = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-07-24 15:17:00 +04:00
|
|
|
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 15:53:32 +04:00
|
|
|
nsContentUtils::ASCIIToLower(aMimeType, type);
|
2013-07-24 15:17:00 +04:00
|
|
|
|
|
|
|
nsAutoString params;
|
|
|
|
bool usingCustomParseOptions;
|
2014-08-13 15:53:32 +04:00
|
|
|
nsresult rv =
|
|
|
|
ParseParams(aCx, type, aEncoderOptions, params, &usingCustomParseOptions);
|
2013-07-24 15:17:00 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-06-08 22:12:18 +04:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2013-10-17 06:55:08 +04:00
|
|
|
rv = ExtractData(type, params, getter_AddRefs(stream));
|
2011-08-26 00:09:05 +04:00
|
|
|
|
2013-04-13 11:03:57 +04:00
|
|
|
// If there are unrecognized custom parse options, we should fall back to
|
2011-08-26 00:09:05 +04:00
|
|
|
// the default values for the encoder without any options at all.
|
|
|
|
if (rv == NS_ERROR_INVALID_ARG && usingCustomParseOptions) {
|
2013-10-17 06:55:08 +04:00
|
|
|
rv = ExtractData(type, EmptyString(), getter_AddRefs(stream));
|
2011-08-26 00:09:05 +04:00
|
|
|
}
|
|
|
|
|
2010-09-18 05:14:59 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2005-09-01 02:16:51 +04:00
|
|
|
// build data URL string
|
2013-10-17 06:55:08 +04:00
|
|
|
aDataURL = NS_LITERAL_STRING("data:") + type + NS_LITERAL_STRING(";base64,");
|
2005-09-01 02:16:51 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint64_t count;
|
2011-06-08 22:12:18 +04:00
|
|
|
rv = stream->Available(&count);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-09-28 10:57:33 +04:00
|
|
|
NS_ENSURE_TRUE(count <= UINT32_MAX, NS_ERROR_FILE_TOO_BIG);
|
2005-09-01 02:16:51 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
return Base64EncodeInputStream(stream, aDataURL, (uint32_t)count, aDataURL.Length());
|
2005-09-01 02:16:51 +04:00
|
|
|
}
|
|
|
|
|
2013-09-12 02:35:51 +04:00
|
|
|
void
|
|
|
|
HTMLCanvasElement::ToBlob(JSContext* aCx,
|
|
|
|
FileCallback& aCallback,
|
2013-07-24 15:17:00 +04:00
|
|
|
const nsAString& aType,
|
2014-02-19 19:13:38 +04:00
|
|
|
JS::Handle<JS::Value> aParams,
|
2013-09-12 02:35:51 +04:00
|
|
|
ErrorResult& aRv)
|
2012-10-16 01:15:25 +04:00
|
|
|
{
|
|
|
|
// do a trust check if this is a write-only canvas
|
2012-10-26 03:10:53 +04:00
|
|
|
if (mWriteOnly && !nsContentUtils::IsCallerChrome()) {
|
2013-09-12 02:35:51 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
2012-10-19 10:59:38 +04:00
|
|
|
}
|
|
|
|
|
2015-09-29 18:57:36 +03:00
|
|
|
nsAutoString type;
|
|
|
|
nsContentUtils::ASCIIToLower(aType, type);
|
2013-07-24 15:17:00 +04:00
|
|
|
|
2015-09-29 18:57:36 +03:00
|
|
|
nsAutoString params;
|
|
|
|
bool usingCustomParseOptions;
|
|
|
|
aRv = ParseParams(aCx, type, aParams, params, &usingCustomParseOptions);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
2015-09-29 13:51:24 +03:00
|
|
|
|
2013-10-17 06:55:08 +04:00
|
|
|
if (mCurrentContext) {
|
2015-09-29 18:57:36 +03: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.
|
|
|
|
nsIntSize elementSize = GetWidthHeight();
|
2015-10-02 10:18:26 +03:00
|
|
|
if ((elementSize.width != mCurrentContext->GetWidth() &&
|
|
|
|
(elementSize.width != 0 || mCurrentContext->GetWidth() != 1)) ||
|
|
|
|
(elementSize.height != mCurrentContext->GetHeight() &&
|
|
|
|
(elementSize.height != 0 || mCurrentContext->GetHeight() != 1))) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
2015-09-29 18:57:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* imageBuffer = nullptr;
|
|
|
|
int32_t format = 0;
|
|
|
|
if (mCurrentContext) {
|
|
|
|
mCurrentContext->GetImageBuffer(&imageBuffer, &format);
|
2013-10-17 06:55:08 +04:00
|
|
|
}
|
|
|
|
|
2015-09-29 18:57:36 +03:00
|
|
|
// Encoder callback when encoding is complete.
|
|
|
|
class EncodeCallback : public EncodeCompleteCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
EncodeCallback(nsIGlobalObject* aGlobal, FileCallback* aCallback)
|
|
|
|
: mGlobal(aGlobal)
|
|
|
|
, mFileCallback(aCallback) {}
|
|
|
|
|
|
|
|
// This is called on main thread.
|
|
|
|
nsresult ReceiveBlob(already_AddRefed<Blob> aBlob)
|
|
|
|
{
|
|
|
|
nsRefPtr<Blob> blob = aBlob;
|
|
|
|
|
|
|
|
ErrorResult rv;
|
|
|
|
uint64_t size = blob->GetSize(rv);
|
|
|
|
if (rv.Failed()) {
|
|
|
|
rv.SuppressException();
|
|
|
|
} else {
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (jsapi.Init(mGlobal)) {
|
|
|
|
JS_updateMallocCounter(jsapi.cx(), size);
|
|
|
|
}
|
|
|
|
}
|
2014-09-04 04:08:00 +04:00
|
|
|
|
2015-09-29 18:57:36 +03:00
|
|
|
nsRefPtr<Blob> newBlob = Blob::Create(mGlobal, blob->Impl());
|
|
|
|
|
|
|
|
mFileCallback->Call(*newBlob, rv);
|
|
|
|
|
|
|
|
mGlobal = nullptr;
|
|
|
|
mFileCallback = nullptr;
|
|
|
|
|
|
|
|
return rv.StealNSResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> mGlobal;
|
|
|
|
nsRefPtr<FileCallback> mFileCallback;
|
|
|
|
};
|
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = OwnerDoc()->GetScopeObject();
|
|
|
|
MOZ_ASSERT(global);
|
|
|
|
nsRefPtr<EncodeCompleteCallback> callback = new EncodeCallback(global, &aCallback);
|
|
|
|
aRv = ImageEncoder::ExtractDataAsync(type,
|
|
|
|
params,
|
|
|
|
usingCustomParseOptions,
|
|
|
|
imageBuffer,
|
|
|
|
format,
|
|
|
|
GetSize(),
|
|
|
|
callback);
|
2012-10-16 01:15:25 +04:00
|
|
|
}
|
|
|
|
|
2014-10-08 20:15:23 +04:00
|
|
|
already_AddRefed<File>
|
2013-04-13 11:05:40 +04:00
|
|
|
HTMLCanvasElement::MozGetAsFile(const nsAString& aName,
|
|
|
|
const nsAString& aType,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2015-05-18 16:52:26 +03:00
|
|
|
nsCOMPtr<nsISupports> file;
|
2013-04-13 11:05:40 +04:00
|
|
|
aRv = MozGetAsFile(aName, aType, getter_AddRefs(file));
|
2015-05-18 16:52:26 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMBlob> blob = do_QueryInterface(file);
|
|
|
|
nsRefPtr<Blob> domBlob = static_cast<Blob*>(blob.get());
|
|
|
|
MOZ_ASSERT(domBlob->IsFile());
|
|
|
|
return domBlob->ToFile();
|
2013-04-13 11:05:40 +04:00
|
|
|
}
|
|
|
|
|
2010-09-18 05:14:59 +04:00
|
|
|
NS_IMETHODIMP
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::MozGetAsFile(const nsAString& aName,
|
|
|
|
const nsAString& aType,
|
2015-05-18 16:52:26 +03:00
|
|
|
nsISupports** aResult)
|
2010-09-18 05:14:59 +04:00
|
|
|
{
|
2013-08-12 21:26:31 +04:00
|
|
|
OwnerDoc()->WarnOnceAbout(nsIDocument::eMozGetAsFile);
|
|
|
|
|
2010-09-18 05:14:59 +04:00
|
|
|
// do a trust check if this is a write-only canvas
|
|
|
|
if ((mWriteOnly) &&
|
2012-10-26 03:10:53 +04:00
|
|
|
!nsContentUtils::IsCallerChrome()) {
|
2010-09-18 05:14:59 +04:00
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
|
|
|
|
2015-05-12 15:11:03 +03:00
|
|
|
return MozGetAsBlobImpl(aName, aType, aResult);
|
2010-09-18 05:14:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-05-12 15:11:03 +03:00
|
|
|
HTMLCanvasElement::MozGetAsBlobImpl(const nsAString& aName,
|
2013-01-04 09:16:14 +04:00
|
|
|
const nsAString& aType,
|
2015-05-18 16:52:26 +03:00
|
|
|
nsISupports** aResult)
|
2010-09-18 05:14:59 +04:00
|
|
|
{
|
2011-06-08 22:12:18 +04:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2013-10-08 23:00:17 +04:00
|
|
|
nsAutoString type(aType);
|
2013-10-17 06:55:08 +04:00
|
|
|
nsresult rv = ExtractData(type, EmptyString(), getter_AddRefs(stream));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-10-08 23:00:17 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint64_t imgSize;
|
2011-06-08 22:12:18 +04:00
|
|
|
rv = stream->Available(&imgSize);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-09-28 10:57:33 +04:00
|
|
|
NS_ENSURE_TRUE(imgSize <= UINT32_MAX, NS_ERROR_FILE_TOO_BIG);
|
2011-06-08 22:12:18 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
void* imgData = nullptr;
|
2012-08-22 19:56:38 +04:00
|
|
|
rv = NS_ReadInputStreamToBuffer(stream, &imgData, (uint32_t)imgSize);
|
2011-06-08 22:12:18 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-11-08 05:49:21 +04:00
|
|
|
JSContext* cx = nsContentUtils::GetCurrentJSContext();
|
|
|
|
if (cx) {
|
|
|
|
JS_updateMallocCounter(cx, imgSize);
|
|
|
|
}
|
|
|
|
|
2014-10-08 20:15:22 +04:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(OwnerDoc()->GetScopeObject());
|
|
|
|
|
2014-10-08 20:15:23 +04:00
|
|
|
// The File takes ownership of the buffer
|
2015-05-18 16:52:26 +03:00
|
|
|
nsCOMPtr<nsIDOMBlob> file =
|
2014-10-08 20:15:23 +04:00
|
|
|
File::CreateMemoryFile(win, imgData, (uint32_t)imgSize, aName, type,
|
|
|
|
PR_Now());
|
2010-09-18 05:14:59 +04:00
|
|
|
|
2011-11-16 11:50:19 +04:00
|
|
|
file.forget(aResult);
|
|
|
|
return NS_OK;
|
2010-09-18 05:14:59 +04:00
|
|
|
}
|
|
|
|
|
2015-09-29 18:57:36 +03:00
|
|
|
static bool
|
|
|
|
GetCanvasContextType(const nsAString& str, CanvasContextType* const out_type)
|
|
|
|
{
|
|
|
|
if (str.EqualsLiteral("2d")) {
|
|
|
|
*out_type = CanvasContextType::Canvas2D;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (str.EqualsLiteral("experimental-webgl")) {
|
|
|
|
*out_type = CanvasContextType::WebGL1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MOZ_WEBGL_CONFORMANT
|
|
|
|
if (str.EqualsLiteral("webgl")) {
|
|
|
|
/* WebGL 1.0, $2.1 "Context Creation":
|
|
|
|
* If the user agent supports both the webgl and experimental-webgl
|
|
|
|
* canvas context types, they shall be treated as aliases.
|
|
|
|
*/
|
|
|
|
*out_type = CanvasContextType::WebGL1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (WebGL2Context::IsSupported()) {
|
|
|
|
if (str.EqualsLiteral("webgl2")) {
|
|
|
|
*out_type = CanvasContextType::WebGL2;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static already_AddRefed<nsICanvasRenderingContextInternal>
|
|
|
|
CreateContextForCanvas(CanvasContextType contextType, HTMLCanvasElement* canvas)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(contextType != CanvasContextType::NoContext);
|
|
|
|
nsRefPtr<nsICanvasRenderingContextInternal> ret;
|
|
|
|
|
|
|
|
switch (contextType) {
|
|
|
|
case CanvasContextType::NoContext:
|
|
|
|
break;
|
|
|
|
case CanvasContextType::Canvas2D:
|
|
|
|
Telemetry::Accumulate(Telemetry::CANVAS_2D_USED, 1);
|
|
|
|
ret = new CanvasRenderingContext2D();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CanvasContextType::WebGL1:
|
|
|
|
Telemetry::Accumulate(Telemetry::CANVAS_WEBGL_USED, 1);
|
|
|
|
|
|
|
|
ret = WebGL1Context::Create();
|
|
|
|
if (!ret)
|
|
|
|
return nullptr;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CanvasContextType::WebGL2:
|
|
|
|
Telemetry::Accumulate(Telemetry::CANVAS_WEBGL_USED, 1);
|
|
|
|
|
|
|
|
ret = WebGL2Context::Create();
|
|
|
|
if (!ret)
|
|
|
|
return nullptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(ret);
|
|
|
|
|
|
|
|
ret->SetCanvasElement(canvas);
|
|
|
|
return ret.forget();
|
|
|
|
}
|
|
|
|
|
2013-04-13 11:05:33 +04:00
|
|
|
nsresult
|
|
|
|
HTMLCanvasElement::GetContext(const nsAString& aContextId,
|
|
|
|
nsISupports** aContext)
|
|
|
|
{
|
2013-06-07 22:42:12 +04:00
|
|
|
ErrorResult rv;
|
2014-11-11 02:16:50 +03:00
|
|
|
*aContext = GetContext(nullptr, aContextId, JS::NullHandleValue, rv).take();
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2013-04-13 11:05:33 +04:00
|
|
|
}
|
|
|
|
|
2013-06-07 22:42:12 +04:00
|
|
|
already_AddRefed<nsISupports>
|
|
|
|
HTMLCanvasElement::GetContext(JSContext* aCx,
|
|
|
|
const nsAString& aContextId,
|
|
|
|
JS::Handle<JS::Value> aContextOptions,
|
2015-09-29 18:57:36 +03:00
|
|
|
ErrorResult& rv)
|
2005-04-18 10:33:23 +04:00
|
|
|
{
|
2015-09-29 18:57:36 +03:00
|
|
|
CanvasContextType contextType;
|
|
|
|
if (!GetCanvasContextType(aContextId, &contextType))
|
2014-11-11 02:16:50 +03:00
|
|
|
return nullptr;
|
2015-09-29 18:57:36 +03:00
|
|
|
|
|
|
|
if (!mCurrentContext) {
|
|
|
|
// This canvas doesn't have a context yet.
|
|
|
|
|
|
|
|
nsRefPtr<nsICanvasRenderingContextInternal> context;
|
|
|
|
context = CreateContextForCanvas(contextType, this);
|
|
|
|
if (!context)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Ensure that the context participates in CC. Note that returning a
|
|
|
|
// CC participant from QI doesn't addref.
|
|
|
|
nsXPCOMCycleCollectionParticipant* cp = nullptr;
|
|
|
|
CallQueryInterface(context, &cp);
|
|
|
|
if (!cp) {
|
|
|
|
rv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCurrentContext = context.forget();
|
|
|
|
mCurrentContextType = contextType;
|
|
|
|
|
|
|
|
rv = UpdateContext(aCx, aContextOptions);
|
|
|
|
if (rv.Failed()) {
|
|
|
|
rv = NS_OK; // See bug 645792
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We already have a context of some type.
|
|
|
|
if (contextType != mCurrentContextType)
|
|
|
|
return nullptr;
|
2010-03-24 13:47:18 +03:00
|
|
|
}
|
|
|
|
|
2015-09-29 18:57:36 +03:00
|
|
|
nsCOMPtr<nsICanvasRenderingContextInternal> context = mCurrentContext;
|
|
|
|
return context.forget();
|
2010-03-24 13:47:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::MozGetIPCContext(const nsAString& aContextId,
|
|
|
|
nsISupports **aContext)
|
2010-03-24 13:47:18 +03:00
|
|
|
{
|
2012-10-26 03:10:53 +04:00
|
|
|
if(!nsContentUtils::IsCallerChrome()) {
|
2010-03-24 13:47:18 +03: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 07:48:51 +04:00
|
|
|
if (!aContextId.EqualsLiteral("2d"))
|
2010-03-24 13:47:18 +03:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
2014-11-11 02:16:50 +03:00
|
|
|
CanvasContextType contextType = CanvasContextType::Canvas2D;
|
|
|
|
|
|
|
|
if (!mCurrentContext) {
|
|
|
|
// This canvas doesn't have a context yet.
|
|
|
|
|
|
|
|
nsRefPtr<nsICanvasRenderingContextInternal> context;
|
2015-09-29 18:57:36 +03:00
|
|
|
context = CreateContextForCanvas(contextType, this);
|
2014-11-11 02:16:50 +03:00
|
|
|
if (!context) {
|
|
|
|
*aContext = nullptr;
|
2010-07-16 00:39:31 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-03-24 13:47:18 +03:00
|
|
|
|
2014-11-11 02:16:50 +03:00
|
|
|
mCurrentContext = context;
|
2011-10-17 18:59:28 +04:00
|
|
|
mCurrentContext->SetIsIPC(true);
|
2014-11-11 02:16:50 +03:00
|
|
|
mCurrentContextType = contextType;
|
2010-03-24 13:47:18 +03:00
|
|
|
|
2014-11-11 02:16:50 +03:00
|
|
|
nsresult rv = UpdateContext(nullptr, JS::NullHandleValue);
|
2011-08-01 11:20:05 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-11-11 02:16:50 +03:00
|
|
|
} else {
|
|
|
|
// We already have a context of some type.
|
|
|
|
if (contextType != mCurrentContextType)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2005-04-18 10:33:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_ADDREF (*aContext = mCurrentContext);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-29 18:57:36 +03:00
|
|
|
nsresult
|
|
|
|
HTMLCanvasElement::UpdateContext(JSContext* aCx, JS::Handle<JS::Value> aNewContextOptions)
|
|
|
|
{
|
|
|
|
if (!mCurrentContext)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsIntSize sz = GetWidthHeight();
|
|
|
|
|
|
|
|
nsCOMPtr<nsICanvasRenderingContextInternal> currentContext = mCurrentContext;
|
|
|
|
|
|
|
|
nsresult rv = currentContext->SetIsOpaque(HasAttr(kNameSpaceID_None, nsGkAtoms::moz_opaque));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mCurrentContext = nullptr;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = currentContext->SetContextOptions(aCx, aNewContextOptions);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mCurrentContext = nullptr;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = currentContext->SetDimensions(sz.width, sz.height);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mCurrentContext = nullptr;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2005-04-18 10:33:23 +04:00
|
|
|
|
2010-05-18 08:04:22 +04:00
|
|
|
nsIntSize
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::GetSize()
|
2005-04-18 10:33:23 +04:00
|
|
|
{
|
2010-05-18 08:04:22 +04:00
|
|
|
return GetWidthHeight();
|
2005-05-13 01:34:56 +04:00
|
|
|
}
|
2006-03-02 03:44:01 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::IsWriteOnly()
|
2006-03-02 03:44:01 +03:00
|
|
|
{
|
|
|
|
return mWriteOnly;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::SetWriteOnly()
|
2006-03-02 03:44:01 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
mWriteOnly = true;
|
2006-03-02 03:44:01 +03:00
|
|
|
}
|
2007-09-26 00:46:08 +04:00
|
|
|
|
2010-05-18 08:04:22 +04:00
|
|
|
void
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::InvalidateCanvasContent(const gfx::Rect* damageRect)
|
2007-09-26 00:46:08 +04:00
|
|
|
{
|
2009-07-10 01:09:43 +04: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-25 00:20:05 +03:00
|
|
|
nsIFrame *frame = GetPrimaryFrame();
|
2010-05-18 08:04:22 +04:00
|
|
|
if (!frame)
|
|
|
|
return;
|
2007-09-26 00:46:08 +04:00
|
|
|
|
2013-09-04 15:30:57 +04:00
|
|
|
ActiveLayerTracker::NotifyContentChange(frame);
|
2011-03-28 03:59:47 +04:00
|
|
|
|
2012-10-12 05:31:39 +04:00
|
|
|
Layer* layer = nullptr;
|
2010-05-18 08:04:22 +04:00
|
|
|
if (damageRect) {
|
2009-04-09 00:27:20 +04:00
|
|
|
nsIntSize size = GetWidthHeight();
|
2011-05-19 14:49:18 +04:00
|
|
|
if (size.width != 0 && size.height != 0) {
|
|
|
|
|
2012-11-04 12:05:25 +04:00
|
|
|
gfx::Rect realRect(*damageRect);
|
2011-05-19 14:49:18 +04:00
|
|
|
realRect.RoundOut();
|
|
|
|
|
2012-11-04 12:05:25 +04:00
|
|
|
// then make it a nsIntRect
|
2012-08-29 09:47:18 +04:00
|
|
|
nsIntRect invalRect(realRect.X(), realRect.Y(),
|
|
|
|
realRect.Width(), realRect.Height());
|
2012-07-04 04:24:55 +04:00
|
|
|
|
2012-08-29 09:47:18 +04:00
|
|
|
layer = frame->InvalidateLayer(nsDisplayItem::TYPE_CANVAS, &invalRect);
|
2011-05-19 14:49:18 +04:00
|
|
|
}
|
2011-03-29 22:31:53 +04:00
|
|
|
} else {
|
2012-08-29 09:47:18 +04:00
|
|
|
layer = frame->InvalidateLayer(nsDisplayItem::TYPE_CANVAS);
|
2011-03-28 03:59:47 +04:00
|
|
|
}
|
|
|
|
if (layer) {
|
|
|
|
static_cast<CanvasLayer*>(layer)->Updated();
|
2007-09-26 00:46:08 +04:00
|
|
|
}
|
2012-06-30 01:35:06 +04: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 18:25:20 +04:00
|
|
|
nsCOMPtr<nsIGlobalObject> global =
|
|
|
|
do_QueryInterface(OwnerDoc()->GetInnerWindow());
|
|
|
|
|
|
|
|
if (global) {
|
|
|
|
if (JSObject *obj = global->GetGlobalJSObject()) {
|
2012-06-30 01:35:06 +04:00
|
|
|
js::NotifyAnimationActivity(obj);
|
|
|
|
}
|
|
|
|
}
|
2007-09-26 00:46:08 +04:00
|
|
|
}
|
2008-04-30 21:04:46 +04:00
|
|
|
|
2011-03-28 03:59:47 +04:00
|
|
|
void
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::InvalidateCanvas()
|
2011-03-28 03:59:47 +04: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 09:39:31 +04:00
|
|
|
frame->InvalidateFrame();
|
2011-03-28 03:59:47 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::CountContexts()
|
2008-04-30 21:04:46 +04:00
|
|
|
{
|
|
|
|
if (mCurrentContext)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsICanvasRenderingContextInternal *
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::GetContextAtIndex(int32_t index)
|
2008-04-30 21:04:46 +04:00
|
|
|
{
|
|
|
|
if (mCurrentContext && index == 0)
|
2011-11-16 11:50:19 +04:00
|
|
|
return mCurrentContext;
|
2008-04-30 21:04:46 +04:00
|
|
|
|
2013-04-03 05:14:24 +04:00
|
|
|
return nullptr;
|
2008-04-30 21:04:46 +04:00
|
|
|
}
|
2009-03-05 16:43:45 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::GetIsOpaque()
|
2009-03-05 16:43:45 +03:00
|
|
|
{
|
2014-03-16 23:34:14 +04:00
|
|
|
if (mCurrentContext) {
|
|
|
|
return mCurrentContext->GetIsOpaque();
|
|
|
|
}
|
|
|
|
|
2009-03-05 16:43:45 +03:00
|
|
|
return HasAttr(kNameSpaceID_None, nsGkAtoms::moz_opaque);
|
|
|
|
}
|
2010-05-18 08:04:22 +04:00
|
|
|
|
|
|
|
already_AddRefed<CanvasLayer>
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::GetCanvasLayer(nsDisplayListBuilder* aBuilder,
|
|
|
|
CanvasLayer *aOldLayer,
|
|
|
|
LayerManager *aManager)
|
2010-05-18 08:04:22 +04:00
|
|
|
{
|
2015-09-29 18:57:36 +03:00
|
|
|
if (!mCurrentContext)
|
|
|
|
return nullptr;
|
2015-09-29 13:51:24 +03:00
|
|
|
|
2015-09-29 18:57:36 +03:00
|
|
|
return mCurrentContext->GetCanvasLayer(aBuilder, aOldLayer, aManager);
|
2010-05-18 08:04:22 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2015-09-29 18:57:36 +03:00
|
|
|
HTMLCanvasElement::ShouldForceInactiveLayer(LayerManager *aManager)
|
2011-07-08 14:42:21 +04:00
|
|
|
{
|
2015-09-29 18:57:36 +03:00
|
|
|
return !mCurrentContext || mCurrentContext->ShouldForceInactiveLayer(aManager);
|
2011-07-08 14:42:21 +04:00
|
|
|
}
|
|
|
|
|
2010-05-18 08:04:22 +04:00
|
|
|
void
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::MarkContextClean()
|
2010-05-18 08:04:22 +04:00
|
|
|
{
|
|
|
|
if (!mCurrentContext)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mCurrentContext->MarkContextClean();
|
|
|
|
}
|
|
|
|
|
2015-09-17 06:37:05 +03:00
|
|
|
void
|
|
|
|
HTMLCanvasElement::MarkContextCleanForFrameCapture()
|
|
|
|
{
|
|
|
|
if (!mCurrentContext)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mCurrentContext->MarkContextCleanForFrameCapture();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
HTMLCanvasElement::IsContextCleanForFrameCapture()
|
|
|
|
{
|
|
|
|
return mCurrentContext && mCurrentContext->IsContextCleanForFrameCapture();
|
|
|
|
}
|
|
|
|
|
2015-09-17 07:36:57 +03:00
|
|
|
void
|
|
|
|
HTMLCanvasElement::RegisterFrameCaptureListener(FrameCaptureListener* aListener)
|
|
|
|
{
|
|
|
|
WeakPtr<FrameCaptureListener> listener = aListener;
|
|
|
|
|
|
|
|
if (mRequestedFrameListeners.Contains(listener)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mRequestedFrameListeners.AppendElement(listener);
|
|
|
|
|
|
|
|
if (!mRequestedFrameRefreshObserver) {
|
|
|
|
nsIDocument* doc = OwnerDoc();
|
|
|
|
MOZ_RELEASE_ASSERT(doc);
|
|
|
|
|
|
|
|
nsIPresShell* shell = doc->GetShell();
|
|
|
|
MOZ_RELEASE_ASSERT(shell);
|
|
|
|
|
|
|
|
nsPresContext* context = shell->GetPresContext();
|
|
|
|
MOZ_RELEASE_ASSERT(context);
|
|
|
|
|
|
|
|
context = context->GetRootPresContext();
|
|
|
|
MOZ_RELEASE_ASSERT(context);
|
|
|
|
|
|
|
|
nsRefreshDriver* driver = context->RefreshDriver();
|
|
|
|
MOZ_RELEASE_ASSERT(driver);
|
|
|
|
|
|
|
|
mRequestedFrameRefreshObserver =
|
|
|
|
new RequestedFrameRefreshObserver(this, driver);
|
|
|
|
}
|
|
|
|
|
|
|
|
mRequestedFrameRefreshObserver->Register();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
HTMLCanvasElement::IsFrameCaptureRequested() const
|
|
|
|
{
|
|
|
|
for (WeakPtr<FrameCaptureListener> listener : mRequestedFrameListeners) {
|
|
|
|
if (!listener) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listener->FrameCaptureRequested()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HTMLCanvasElement::SetFrameCapture(already_AddRefed<SourceSurface> aSurface)
|
|
|
|
{
|
|
|
|
RefPtr<SourceSurface> surface = aSurface;
|
|
|
|
|
|
|
|
CairoImage::Data imageData;
|
|
|
|
imageData.mSize = surface->GetSize();
|
|
|
|
imageData.mSourceSurface = surface;
|
|
|
|
|
|
|
|
nsRefPtr<CairoImage> image = new CairoImage();
|
|
|
|
image->SetData(imageData);
|
|
|
|
|
|
|
|
// Loop backwards to allow removing elements in the loop.
|
|
|
|
for (int i = mRequestedFrameListeners.Length() - 1; i >= 0; --i) {
|
|
|
|
WeakPtr<FrameCaptureListener> listener = mRequestedFrameListeners[i];
|
|
|
|
if (!listener) {
|
|
|
|
// listener was destroyed. Remove it from the list.
|
|
|
|
mRequestedFrameListeners.RemoveElementAt(i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<Image> imageRefCopy = image.get();
|
|
|
|
listener->NewFrame(imageRefCopy.forget());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mRequestedFrameListeners.IsEmpty()) {
|
|
|
|
mRequestedFrameRefreshObserver->Unregister();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<SourceSurface>
|
2014-04-17 09:29:52 +04:00
|
|
|
HTMLCanvasElement::GetSurfaceSnapshot(bool* aPremultAlpha)
|
|
|
|
{
|
|
|
|
if (!mCurrentContext)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return mCurrentContext->GetSurfaceSnapshot(aPremultAlpha);
|
|
|
|
}
|
|
|
|
|
2015-10-05 05:50:15 +03:00
|
|
|
AsyncCanvasRenderer*
|
|
|
|
HTMLCanvasElement::GetAsyncCanvasRenderer()
|
|
|
|
{
|
|
|
|
if (!mAsyncCanvasRenderer) {
|
|
|
|
mAsyncCanvasRenderer = new AsyncCanvasRenderer();
|
|
|
|
mAsyncCanvasRenderer->mHTMLCanvasElement = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mAsyncCanvasRenderer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
HTMLCanvasElement::SetAttrFromAsyncCanvasRenderer(AsyncCanvasRenderer *aRenderer)
|
|
|
|
{
|
|
|
|
HTMLCanvasElement *element = aRenderer->mHTMLCanvasElement;
|
|
|
|
if (!element) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx::IntSize asyncCanvasSize = aRenderer->GetSize();
|
|
|
|
|
|
|
|
ErrorResult rv;
|
|
|
|
element->SetUnsignedIntAttr(nsGkAtoms::width, asyncCanvasSize.width, rv);
|
|
|
|
if (rv.Failed()) {
|
|
|
|
NS_WARNING("Failed to set width attribute to a canvas element asynchronously.");
|
|
|
|
}
|
|
|
|
|
|
|
|
element->SetUnsignedIntAttr(nsGkAtoms::height, asyncCanvasSize.height, rv);
|
|
|
|
if (rv.Failed()) {
|
|
|
|
NS_WARNING("Failed to set height attribute to a canvas element asynchronously.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-04 09:16:14 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|