1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4  *           (C) 2001 Dirk Mueller (mueller@kde.org)
5  *           (C) 2006 Alexey Proskuryakov (ap@webkit.org)
6  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012 Apple Inc. All
7  * rights reserved.
8  * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved.
9  * (http://www.torchmobile.com/)
10  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
11  * Copyright (C) 2013 Google Inc. All rights reserved.
12  *
13  * This library is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Library General Public
15  * License as published by the Free Software Foundation; either
16  * version 2 of the License, or (at your option) any later version.
17  *
18  * This library is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * Library General Public License for more details.
22  *
23  * You should have received a copy of the GNU Library General Public License
24  * along with this library; see the file COPYING.LIB.  If not, write to
25  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26  * Boston, MA 02110-1301, USA.
27  *
28  */
29 
30 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_FULLSCREEN_FULLSCREEN_H_
31 #define THIRD_PARTY_BLINK_RENDERER_CORE_FULLSCREEN_FULLSCREEN_H_
32 
33 #include "base/memory/scoped_refptr.h"
34 #include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
35 #include "third_party/blink/renderer/core/core_export.h"
36 #include "third_party/blink/renderer/core/dom/document.h"
37 #include "third_party/blink/renderer/core/dom/element.h"
38 #include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h"
39 #include "third_party/blink/renderer/core/fullscreen/fullscreen_request_type.h"
40 #include "third_party/blink/renderer/platform/geometry/layout_rect.h"
41 #include "third_party/blink/renderer/platform/supplementable.h"
42 #include "third_party/blink/renderer/platform/wtf/deque.h"
43 #include "third_party/blink/renderer/platform/wtf/vector.h"
44 
45 namespace blink {
46 
47 class LocalDOMWindow;
48 class FullscreenOptions;
49 class ScriptPromiseResolver;
50 
51 // The Fullscreen class implements most of the Fullscreen API Standard,
52 // https://fullscreen.spec.whatwg.org/, especially its algorithms. It is a
53 // Document supplement as each document has some fullscreen state, and to
54 // actually enter and exit fullscreen it (indirectly) uses FullscreenController.
55 class CORE_EXPORT Fullscreen final : public GarbageCollected<Fullscreen>,
56                                      public Supplement<LocalDOMWindow>,
57                                      public ExecutionContextLifecycleObserver {
58  public:
59   static const char kSupplementName[];
60 
61   explicit Fullscreen(LocalDOMWindow&);
62   virtual ~Fullscreen();
63 
64   static Element* FullscreenElementFrom(Document&);
65   static Element* FullscreenElementForBindingFrom(TreeScope&);
66   static bool IsFullscreenElement(const Element&);
67   static bool IsInFullscreenElementStack(const Element&);
68 
69   static void RequestFullscreen(Element&);
70   static ScriptPromise RequestFullscreen(
71       Element&,
72       const FullscreenOptions*,
73       FullscreenRequestType,
74       ScriptState* state = nullptr,
75       ExceptionState* exception_state = nullptr);
76 
77   static void FullyExitFullscreen(Document&, bool ua_originated = false);
78   static ScriptPromise ExitFullscreen(Document&,
79                                       ScriptState* state = nullptr,
80                                       ExceptionState* exception_state = nullptr,
81                                       bool ua_originated = false);
82 
83   static bool FullscreenEnabled(Document&);
84 
85   // Called by FullscreenController to notify that we've entered or exited
86   // fullscreen. All frames are notified, so there may be no pending request.
87   static void DidResolveEnterFullscreenRequest(Document&, bool granted);
88   static void DidExitFullscreen(Document&);
89 
90   static void DidUpdateSize(Element&);
91 
92   static void ElementRemoved(Element&);
93 
94   // ExecutionContextLifecycleObserver:
95   void ContextDestroyed() override;
96 
97   void Trace(Visitor*) const override;
98 
99  private:
100   static Fullscreen& From(LocalDOMWindow&);
101 
102   static void ContinueRequestFullscreen(Document&,
103                                         Element&,
104                                         FullscreenRequestType,
105                                         ScriptPromiseResolver* resolver,
106                                         bool error);
107 
108   static void ContinueExitFullscreen(Document*,
109                                      ScriptPromiseResolver* resolver,
110                                      bool resize);
111 
112   void FullscreenElementChanged(Element* old_element,
113                                 Element* new_element,
114                                 FullscreenRequestType new_request_type);
115 
116   // Stores the pending request, promise and the type for executing
117   // the asynchronous portion of the request.
118   class PendingRequest : public GarbageCollected<PendingRequest> {
119    public:
120     PendingRequest(Element* element,
121                    FullscreenRequestType type,
122                    ScriptPromiseResolver* resolver);
123     virtual ~PendingRequest();
124     virtual void Trace(Visitor* visitor) const;
125 
element()126     Element* element() { return element_; }
type()127     FullscreenRequestType type() { return type_; }
resolver()128     ScriptPromiseResolver* resolver() { return resolver_; }
129 
130    private:
131     Member<Element> element_;
132     FullscreenRequestType type_;
133     Member<ScriptPromiseResolver> resolver_;
134 
135     DISALLOW_COPY_AND_ASSIGN(PendingRequest);
136   };
137   using PendingRequests = HeapVector<Member<PendingRequest>>;
138   PendingRequests pending_requests_;
139 
140   using PendingExit = ScriptPromiseResolver;
141   using PendingExits = HeapVector<Member<PendingExit>>;
142   PendingExits pending_exits_;
143 };
144 
IsFullscreenElement(const Element & element)145 inline bool Fullscreen::IsFullscreenElement(const Element& element) {
146   return FullscreenElementFrom(element.GetDocument()) == &element;
147 }
148 
149 }  // namespace blink
150 
151 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_FULLSCREEN_FULLSCREEN_H_
152