Backed out 4 changesets (bug 1858627) for causing clipboard/paste failures. CLOSED...
[gecko.git] / dom / canvas / nsICanvasRenderingContextInternal.cpp
blobc463a00a1aef219c90804eb7b40be4bc8d70bca1
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() =
17 default;
19 nsICanvasRenderingContextInternal::~nsICanvasRenderingContextInternal() =
20 default;
22 mozilla::PresShell* nsICanvasRenderingContextInternal::GetPresShell() {
23 if (mCanvasElement) {
24 return mCanvasElement->OwnerDoc()->GetPresShell();
26 return nullptr;
29 nsIGlobalObject* nsICanvasRenderingContextInternal::GetParentObject() const {
30 if (mCanvasElement) {
31 return mCanvasElement->OwnerDoc()->GetScopeObject();
33 if (mOffscreenCanvas) {
34 return mOffscreenCanvas->GetParentObject();
36 return nullptr;
39 nsIPrincipal* nsICanvasRenderingContextInternal::PrincipalOrNull() const {
40 if (mCanvasElement) {
41 return mCanvasElement->NodePrincipal();
43 if (mOffscreenCanvas) {
44 nsIGlobalObject* global = mOffscreenCanvas->GetParentObject();
45 if (global) {
46 return global->PrincipalOrNull();
49 return nullptr;
52 nsICookieJarSettings* nsICanvasRenderingContextInternal::GetCookieJarSettings()
53 const {
54 if (mCanvasElement) {
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());
64 if (win) {
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();
73 if (worker) {
74 return worker->CookieJarSettings();
78 return nullptr;
81 void nsICanvasRenderingContextInternal::RemovePostRefreshObserver() {
82 if (mRefreshDriver) {
83 mRefreshDriver->RemovePostRefreshObserver(this);
84 mRefreshDriver = nullptr;
88 void nsICanvasRenderingContextInternal::AddPostRefreshObserverIfNecessary() {
89 if (!GetPresShell() || !GetPresShell()->GetPresContext() ||
90 !GetPresShell()->GetPresContext()->RefreshDriver()) {
91 return;
93 mRefreshDriver = GetPresShell()->GetPresContext()->RefreshDriver();
94 mRefreshDriver->AddPostRefreshObserver(this);
97 void nsICanvasRenderingContextInternal::DoSecurityCheck(
98 nsIPrincipal* aPrincipal, bool aForceWriteOnly, bool aCORSUsed) {
99 if (mCanvasElement) {
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);