1 /* -*- Mode: C++; tab-width: 40; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsICanvasRenderingContextInternal.h"
8 #include "mozilla/dom/CanvasUtils.h"
9 #include "mozilla/dom/Document.h"
10 #include "mozilla/dom/Event.h"
11 #include "mozilla/dom/WorkerCommon.h"
12 #include "mozilla/dom/WorkerPrivate.h"
13 #include "mozilla/ErrorResult.h"
14 #include "mozilla/PresShell.h"
15 #include "nsContentUtils.h"
16 #include "nsPIDOMWindow.h"
17 #include "nsRefreshDriver.h"
19 nsICanvasRenderingContextInternal::nsICanvasRenderingContextInternal() =
22 nsICanvasRenderingContextInternal::~nsICanvasRenderingContextInternal() =
25 mozilla::PresShell
* nsICanvasRenderingContextInternal::GetPresShell() {
27 return mCanvasElement
->OwnerDoc()->GetPresShell();
32 nsIGlobalObject
* nsICanvasRenderingContextInternal::GetParentObject() const {
34 return mCanvasElement
->OwnerDoc()->GetScopeObject();
36 if (mOffscreenCanvas
) {
37 return mOffscreenCanvas
->GetParentObject();
42 nsIPrincipal
* nsICanvasRenderingContextInternal::PrincipalOrNull() const {
44 return mCanvasElement
->NodePrincipal();
46 if (mOffscreenCanvas
) {
47 nsIGlobalObject
* global
= mOffscreenCanvas
->GetParentObject();
49 return global
->PrincipalOrNull();
55 nsICookieJarSettings
* nsICanvasRenderingContextInternal::GetCookieJarSettings()
58 return mCanvasElement
->OwnerDoc()->CookieJarSettings();
61 // If there is an offscreen canvas, attempt to retrieve its owner window
62 // and return the cookieJarSettings for the window's document, if available.
63 if (mOffscreenCanvas
) {
64 nsCOMPtr
<nsPIDOMWindowInner
> win
=
65 do_QueryInterface(mOffscreenCanvas
->GetOwnerGlobal());
68 return win
->GetExtantDoc()->CookieJarSettings();
71 // If the owner window cannot be retrieved, check if there is a current
72 // worker and return its cookie jar settings if available.
73 mozilla::dom::WorkerPrivate
* worker
=
74 mozilla::dom::GetCurrentThreadWorkerPrivate();
77 return worker
->CookieJarSettings();
84 void nsICanvasRenderingContextInternal::RemovePostRefreshObserver() {
86 mRefreshDriver
->RemovePostRefreshObserver(this);
87 mRefreshDriver
= nullptr;
91 void nsICanvasRenderingContextInternal::AddPostRefreshObserverIfNecessary() {
92 if (!GetPresShell() || !GetPresShell()->GetPresContext() ||
93 !GetPresShell()->GetPresContext()->RefreshDriver()) {
96 mRefreshDriver
= GetPresShell()->GetPresContext()->RefreshDriver();
97 mRefreshDriver
->AddPostRefreshObserver(this);
100 void nsICanvasRenderingContextInternal::DoSecurityCheck(
101 nsIPrincipal
* aPrincipal
, bool aForceWriteOnly
, bool aCORSUsed
) {
102 if (mCanvasElement
) {
103 mozilla::CanvasUtils::DoDrawImageSecurityCheck(mCanvasElement
, aPrincipal
,
104 aForceWriteOnly
, aCORSUsed
);
105 } else if (mOffscreenCanvas
) {
106 mozilla::CanvasUtils::DoDrawImageSecurityCheck(mOffscreenCanvas
, aPrincipal
,
107 aForceWriteOnly
, aCORSUsed
);
111 bool nsICanvasRenderingContextInternal::ShouldResistFingerprinting(
112 mozilla::RFPTarget aTarget
) const {
113 if (mCanvasElement
) {
114 return mCanvasElement
->OwnerDoc()->ShouldResistFingerprinting(aTarget
);
116 if (mOffscreenCanvas
) {
117 return mOffscreenCanvas
->ShouldResistFingerprinting(aTarget
);
119 // Last resort, just check the global preference
120 return nsContentUtils::ShouldResistFingerprinting("Fallback", aTarget
);
123 bool nsICanvasRenderingContextInternal::DispatchEvent(
124 const nsAString
& eventName
, mozilla::CanBubble aCanBubble
,
125 mozilla::Cancelable aIsCancelable
) const {
126 bool useDefaultHandler
= true;
128 if (mCanvasElement
) {
129 nsContentUtils::DispatchTrustedEvent(mCanvasElement
->OwnerDoc(),
130 mCanvasElement
, eventName
, aCanBubble
,
131 aIsCancelable
, &useDefaultHandler
);
132 } else if (mOffscreenCanvas
) {
133 // OffscreenCanvas case
134 auto event
= mozilla::MakeRefPtr
<mozilla::dom::Event
>(mOffscreenCanvas
,
136 event
->InitEvent(eventName
, aCanBubble
, aIsCancelable
);
137 event
->SetTrusted(true);
138 useDefaultHandler
= mOffscreenCanvas
->DispatchEvent(
139 *event
, mozilla::dom::CallerType::System
, mozilla::IgnoreErrors());
141 return useDefaultHandler
;