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/WorkerCommon.h"
11 #include "mozilla/dom/WorkerPrivate.h"
12 #include "mozilla/PresShell.h"
13 #include "nsPIDOMWindow.h"
14 #include "nsRefreshDriver.h"
16 nsICanvasRenderingContextInternal::nsICanvasRenderingContextInternal() =
19 nsICanvasRenderingContextInternal::~nsICanvasRenderingContextInternal() =
22 mozilla::PresShell
* nsICanvasRenderingContextInternal::GetPresShell() {
24 return mCanvasElement
->OwnerDoc()->GetPresShell();
29 nsIGlobalObject
* nsICanvasRenderingContextInternal::GetParentObject() const {
31 return mCanvasElement
->OwnerDoc()->GetScopeObject();
33 if (mOffscreenCanvas
) {
34 return mOffscreenCanvas
->GetParentObject();
39 nsIPrincipal
* nsICanvasRenderingContextInternal::PrincipalOrNull() const {
41 return mCanvasElement
->NodePrincipal();
43 if (mOffscreenCanvas
) {
44 nsIGlobalObject
* global
= mOffscreenCanvas
->GetParentObject();
46 return global
->PrincipalOrNull();
52 nsICookieJarSettings
* nsICanvasRenderingContextInternal::GetCookieJarSettings()
55 return mCanvasElement
->OwnerDoc()->CookieJarSettings();
58 // If there is an offscreen canvas, attempt to retrieve its owner window
59 // and return the cookieJarSettings for the window's document, if available.
60 if (mOffscreenCanvas
) {
61 nsCOMPtr
<nsPIDOMWindowInner
> win
=
62 do_QueryInterface(mOffscreenCanvas
->GetOwnerGlobal());
65 return win
->GetExtantDoc()->CookieJarSettings();
68 // If the owner window cannot be retrieved, check if there is a current
69 // worker and return its cookie jar settings if available.
70 mozilla::dom::WorkerPrivate
* worker
=
71 mozilla::dom::GetCurrentThreadWorkerPrivate();
74 return worker
->CookieJarSettings();
81 void nsICanvasRenderingContextInternal::RemovePostRefreshObserver() {
83 mRefreshDriver
->RemovePostRefreshObserver(this);
84 mRefreshDriver
= nullptr;
88 void nsICanvasRenderingContextInternal::AddPostRefreshObserverIfNecessary() {
89 if (!GetPresShell() || !GetPresShell()->GetPresContext() ||
90 !GetPresShell()->GetPresContext()->RefreshDriver()) {
93 mRefreshDriver
= GetPresShell()->GetPresContext()->RefreshDriver();
94 mRefreshDriver
->AddPostRefreshObserver(this);
97 void nsICanvasRenderingContextInternal::DoSecurityCheck(
98 nsIPrincipal
* aPrincipal
, bool aForceWriteOnly
, bool aCORSUsed
) {
100 mozilla::CanvasUtils::DoDrawImageSecurityCheck(mCanvasElement
, aPrincipal
,
101 aForceWriteOnly
, aCORSUsed
);
102 } else if (mOffscreenCanvas
) {
103 mozilla::CanvasUtils::DoDrawImageSecurityCheck(mOffscreenCanvas
, aPrincipal
,
104 aForceWriteOnly
, aCORSUsed
);
108 bool nsICanvasRenderingContextInternal::ShouldResistFingerprinting(
109 mozilla::RFPTarget aTarget
) const {
110 if (mCanvasElement
) {
111 return mCanvasElement
->OwnerDoc()->ShouldResistFingerprinting(aTarget
);
113 if (mOffscreenCanvas
) {
114 return mOffscreenCanvas
->ShouldResistFingerprinting(aTarget
);
116 // Last resort, just check the global preference
117 return nsContentUtils::ShouldResistFingerprinting("Fallback", aTarget
);