Bug 1936278 - Prevent search mode chiclet from being dismissed when clicking in page...
[gecko.git] / dom / canvas / nsICanvasRenderingContextInternal.cpp
blobca8e16136cab0d7a165938051ab45e7d2b34c48a
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() =
20 default;
22 nsICanvasRenderingContextInternal::~nsICanvasRenderingContextInternal() =
23 default;
25 mozilla::PresShell* nsICanvasRenderingContextInternal::GetPresShell() {
26 if (mCanvasElement) {
27 return mCanvasElement->OwnerDoc()->GetPresShell();
29 return nullptr;
32 nsIGlobalObject* nsICanvasRenderingContextInternal::GetParentObject() const {
33 if (mCanvasElement) {
34 return mCanvasElement->OwnerDoc()->GetScopeObject();
36 if (mOffscreenCanvas) {
37 return mOffscreenCanvas->GetParentObject();
39 return nullptr;
42 nsIPrincipal* nsICanvasRenderingContextInternal::PrincipalOrNull() const {
43 if (mCanvasElement) {
44 return mCanvasElement->NodePrincipal();
46 if (mOffscreenCanvas) {
47 nsIGlobalObject* global = mOffscreenCanvas->GetParentObject();
48 if (global) {
49 return global->PrincipalOrNull();
52 return nullptr;
55 nsICookieJarSettings* nsICanvasRenderingContextInternal::GetCookieJarSettings()
56 const {
57 if (mCanvasElement) {
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());
67 if (win) {
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();
76 if (worker) {
77 return worker->CookieJarSettings();
81 return nullptr;
84 void nsICanvasRenderingContextInternal::RemovePostRefreshObserver() {
85 if (mRefreshDriver) {
86 mRefreshDriver->RemovePostRefreshObserver(this);
87 mRefreshDriver = nullptr;
91 void nsICanvasRenderingContextInternal::AddPostRefreshObserverIfNecessary() {
92 if (!GetPresShell() || !GetPresShell()->GetPresContext() ||
93 !GetPresShell()->GetPresContext()->RefreshDriver()) {
94 return;
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,
135 nullptr, nullptr);
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;