1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
8 * Struct for holding fullscreen request.
11 #ifndef mozilla_FullscreenRequest_h
12 #define mozilla_FullscreenRequest_h
14 #include "mozilla/LinkedList.h"
15 #include "mozilla/PendingFullscreenEvent.h"
16 #include "mozilla/UniquePtr.h"
17 #include "mozilla/dom/Element.h"
18 #include "mozilla/dom/Promise.h"
19 #include "mozilla/dom/Document.h"
20 #include "nsIScriptError.h"
21 #include "nsRefreshDriver.h"
25 class FullscreenChange
: public LinkedListElement
<FullscreenChange
> {
27 FullscreenChange(const FullscreenChange
&) = delete;
34 ChangeType
Type() const { return mType
; }
35 dom::Document
* Document() const { return mDocument
; }
36 dom::Promise
* GetPromise() const { return mPromise
; }
38 void MayResolvePromise() const {
40 MOZ_ASSERT(mPromise
->State() == Promise::PromiseState::Pending
);
41 mPromise
->MaybeResolveWithUndefined();
45 void MayRejectPromise(const nsACString
& aMessage
) {
47 MOZ_ASSERT(mPromise
->State() == Promise::PromiseState::Pending
);
48 mPromise
->MaybeRejectWithTypeError(aMessage
);
52 void MayRejectPromise(const char (&aMessage
)[N
]) {
53 MayRejectPromise(nsLiteralCString(aMessage
));
57 typedef dom::Promise Promise
;
59 FullscreenChange(ChangeType aType
, dom::Document
* aDocument
,
60 already_AddRefed
<Promise
> aPromise
)
61 : mType(aType
), mDocument(aDocument
), mPromise(aPromise
) {
62 MOZ_ASSERT(aDocument
);
66 MOZ_ASSERT_IF(mPromise
,
67 mPromise
->State() != Promise::PromiseState::Pending
);
72 nsCOMPtr
<dom::Document
> mDocument
;
73 RefPtr
<Promise
> mPromise
;
76 class FullscreenRequest
: public FullscreenChange
{
78 static const ChangeType kType
= eEnter
;
80 static UniquePtr
<FullscreenRequest
> Create(dom::Element
* aElement
,
81 dom::CallerType aCallerType
,
83 RefPtr
<Promise
> promise
= Promise::Create(aElement
->GetOwnerGlobal(), aRv
);
85 new FullscreenRequest(aElement
, promise
.forget(), aCallerType
, true));
88 static UniquePtr
<FullscreenRequest
> CreateForRemote(dom::Element
* aElement
) {
89 return WrapUnique(new FullscreenRequest(aElement
, nullptr,
90 dom::CallerType::NonSystem
, false));
93 MOZ_COUNTED_DTOR(FullscreenRequest
)
95 dom::Element
* Element() const { return mElement
; }
97 // Reject the fullscreen request with the given reason.
98 // It will dispatch the fullscreenerror event.
99 void Reject(const char* aReason
) {
100 if (nsPresContext
* presContext
= Document()->GetPresContext()) {
101 auto pendingEvent
= MakeUnique
<PendingFullscreenEvent
>(
102 FullscreenEventType::Error
, Document(), mElement
);
103 presContext
->RefreshDriver()->ScheduleFullscreenEvent(
104 std::move(pendingEvent
));
106 MayRejectPromise("Fullscreen request denied");
107 nsContentUtils::ReportToConsole(nsIScriptError::warningFlag
, "DOM"_ns
,
108 Document(), nsContentUtils::eDOM_PROPERTIES
,
113 RefPtr
<dom::Element
> mElement
;
116 // This value should be true if the fullscreen request is
117 // originated from system code.
118 const dom::CallerType mCallerType
;
119 // This value denotes whether we should trigger a NewOrigin event if
120 // requesting fullscreen in its document causes the origin which is
121 // fullscreen to change. We may want *not* to trigger that event if
122 // we're calling RequestFullscreen() as part of a continuation of a
123 // request in a subdocument in different process, whereupon the caller
124 // need to send some notification itself with the real origin.
125 const bool mShouldNotifyNewOrigin
;
128 FullscreenRequest(dom::Element
* aElement
,
129 already_AddRefed
<dom::Promise
> aPromise
,
130 dom::CallerType aCallerType
, bool aShouldNotifyNewOrigin
)
131 : FullscreenChange(kType
, aElement
->OwnerDoc(), std::move(aPromise
)),
133 mCallerType(aCallerType
),
134 mShouldNotifyNewOrigin(aShouldNotifyNewOrigin
) {
135 MOZ_COUNT_CTOR(FullscreenRequest
);
139 class FullscreenExit
: public FullscreenChange
{
141 static const ChangeType kType
= eExit
;
143 static UniquePtr
<FullscreenExit
> Create(dom::Document
* aDoc
,
145 RefPtr
<Promise
> promise
= Promise::Create(aDoc
->GetOwnerGlobal(), aRv
);
146 return WrapUnique(new FullscreenExit(aDoc
, promise
.forget()));
149 static UniquePtr
<FullscreenExit
> CreateForRemote(dom::Document
* aDoc
) {
150 return WrapUnique(new FullscreenExit(aDoc
, nullptr));
153 MOZ_COUNTED_DTOR(FullscreenExit
)
156 FullscreenExit(dom::Document
* aDoc
, already_AddRefed
<Promise
> aPromise
)
157 : FullscreenChange(kType
, aDoc
, std::move(aPromise
)) {
158 MOZ_COUNT_CTOR(FullscreenExit
);
162 } // namespace mozilla
164 #endif // mozilla_FullscreenRequest_h