6ac36b974754233452bc0bb6bab0c0daf7d40039
[WebKit-https.git] / Source / WebKit2 / WebProcess / ios / WebVideoFullscreenManager.h
1 /*
2  * Copyright (C) 2014 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #ifndef WebVideoFullscreenManager_h
26 #define WebVideoFullscreenManager_h
27
28 #if PLATFORM(IOS)
29
30 #include "MessageReceiver.h"
31 #include <WebCore/EventListener.h>
32 #include <WebCore/HTMLMediaElement.h>
33 #include <WebCore/PlatformCALayer.h>
34 #include <WebCore/WebVideoFullscreenInterface.h>
35 #include <WebCore/WebVideoFullscreenModelVideoElement.h>
36 #include <wtf/HashMap.h>
37 #include <wtf/RefCounted.h>
38 #include <wtf/RefPtr.h>
39
40 namespace IPC {
41 class Attachment;
42 class Connection;
43 class MessageDecoder;
44 class MessageReceiver;
45 }
46
47 namespace WebCore {
48 class Node;
49 }
50
51 namespace WebKit {
52
53 class LayerHostingContext;
54 class WebPage;
55 class WebVideoFullscreenManager;
56
57 class WebVideoFullscreenInterfaceContext : public RefCounted<WebVideoFullscreenInterfaceContext>, public WebCore::WebVideoFullscreenInterface {
58 public:
59     static Ref<WebVideoFullscreenInterfaceContext> create(WebVideoFullscreenManager& manager, uint64_t contextId)
60     {
61         return adoptRef(*new WebVideoFullscreenInterfaceContext(manager, contextId));
62     }
63     virtual ~WebVideoFullscreenInterfaceContext();
64
65     void invalidate() { m_manager = nullptr; }
66
67     LayerHostingContext* layerHostingContext() { return m_layerHostingContext.get(); }
68     void setLayerHostingContext(std::unique_ptr<LayerHostingContext>&&);
69
70     bool isAnimating() const { return m_isAnimating; }
71     void setIsAnimating(bool flag) { m_isAnimating = flag; }
72
73     bool targetIsFullscreen() const { return m_targetIsFullscreen; }
74     void setTargetIsFullscreen(bool flag) { m_targetIsFullscreen = flag; }
75
76     WebCore::HTMLMediaElement::VideoFullscreenMode fullscreenMode() const { return m_fullscreenMode; }
77     void setFullscreenMode(WebCore::HTMLMediaElement::VideoFullscreenMode mode) { m_fullscreenMode = mode; }
78
79     bool isFullscreen() const { return m_isFullscreen; }
80     void setIsFullscreen(bool flag) { m_isFullscreen = flag; }
81
82 private:
83     // WebVideoFullscreenInterface
84     virtual void resetMediaState() override;
85     virtual void setDuration(double) override;
86     virtual void setCurrentTime(double currentTime, double anchorTime) override;
87     virtual void setBufferedTime(double) override;
88     virtual void setRate(bool isPlaying, float playbackRate) override;
89     virtual void setVideoDimensions(bool hasVideo, float width, float height) override;
90     virtual void setSeekableRanges(const WebCore::TimeRanges&) override;
91     virtual void setCanPlayFastReverse(bool value) override;
92     virtual void setAudioMediaSelectionOptions(const Vector<WTF::String>& options, uint64_t selectedIndex) override;
93     virtual void setLegibleMediaSelectionOptions(const Vector<WTF::String>& options, uint64_t selectedIndex) override;
94     virtual void setExternalPlayback(bool enabled, ExternalPlaybackTargetType, WTF::String localizedDeviceName) override;
95
96     WebVideoFullscreenInterfaceContext(WebVideoFullscreenManager&, uint64_t contextId);
97
98     WebVideoFullscreenManager* m_manager;
99     uint64_t m_contextId;
100     std::unique_ptr<LayerHostingContext> m_layerHostingContext;
101     bool m_isAnimating { false };
102     bool m_targetIsFullscreen { false };
103     WebCore::HTMLMediaElement::VideoFullscreenMode m_fullscreenMode { WebCore::HTMLMediaElement::VideoFullscreenModeNone };
104     bool m_isFullscreen { false };
105 };
106
107 class WebVideoFullscreenManager : public RefCounted<WebVideoFullscreenManager>, private IPC::MessageReceiver {
108 public:
109     static PassRefPtr<WebVideoFullscreenManager> create(PassRefPtr<WebPage>);
110     virtual ~WebVideoFullscreenManager();
111     
112     void didReceiveMessage(IPC::Connection&, IPC::MessageDecoder&) override;
113
114     // Interface to ChromeClient
115     bool supportsVideoFullscreen() const;
116     void enterVideoFullscreenForVideoElement(WebCore::HTMLVideoElement&, WebCore::HTMLMediaElement::VideoFullscreenMode);
117     void exitVideoFullscreenForVideoElement(WebCore::HTMLVideoElement&);
118     
119 protected:
120     friend class WebVideoFullscreenInterfaceContext;
121
122     explicit WebVideoFullscreenManager(PassRefPtr<WebPage>);
123
124     typedef std::tuple<RefPtr<WebCore::WebVideoFullscreenModelVideoElement>, RefPtr<WebVideoFullscreenInterfaceContext>> ModelInterfaceTuple;
125     ModelInterfaceTuple createModelAndInterface(uint64_t contextId);
126     ModelInterfaceTuple& ensureModelAndInterface(uint64_t contextId);
127     WebCore::WebVideoFullscreenModelVideoElement& ensureModel(uint64_t contextId);
128     WebVideoFullscreenInterfaceContext& ensureInterface(uint64_t contextId);
129
130     // Interface to WebVideoFullscreenInterfaceContext
131     void resetMediaState(uint64_t contextId);
132     void setDuration(uint64_t contextId, double);
133     void setCurrentTime(uint64_t contextId, double currentTime, double anchorTime);
134     void setBufferedTime(uint64_t contextId, double bufferedTime);
135     void setRate(uint64_t contextId, bool isPlaying, float playbackRate);
136     void setVideoDimensions(uint64_t contextId, bool hasVideo, float width, float height);
137     void setSeekableRanges(uint64_t contextId, const WebCore::TimeRanges&);
138     void setCanPlayFastReverse(uint64_t contextId, bool value);
139     void setAudioMediaSelectionOptions(uint64_t contextId, const Vector<String>& options, uint64_t selectedIndex);
140     void setLegibleMediaSelectionOptions(uint64_t contextId, const Vector<String>& options, uint64_t selectedIndex);
141     void setExternalPlayback(uint64_t contextId, bool enabled, WebCore::WebVideoFullscreenInterface::ExternalPlaybackTargetType, String localizedDeviceName);
142
143     // Messages from WebVideoFullscreenManagerProxy
144     void play(uint64_t contextId);
145     void pause(uint64_t contextId);
146     void togglePlayState(uint64_t contextId);
147     void beginScrubbing(uint64_t contextId);
148     void endScrubbing(uint64_t contextId);
149     void seekToTime(uint64_t contextId, double time);
150     void fastSeek(uint64_t contextId, double time);
151     void beginScanningForward(uint64_t contextId);
152     void beginScanningBackward(uint64_t contextId);
153     void endScanning(uint64_t contextId);
154     void requestExitFullscreen(uint64_t contextId);
155     void didSetupFullscreen(uint64_t contextId);
156     void didExitFullscreen(uint64_t contextId);
157     void didEnterFullscreen(uint64_t contextId);
158     void didCleanupFullscreen(uint64_t contextId);
159     void setVideoLayerFrameFenced(uint64_t contextId, WebCore::FloatRect bounds, IPC::Attachment fencePort);
160     void setVideoLayerGravityEnum(uint64_t contextId, unsigned gravity);
161     void selectAudioMediaOption(uint64_t contextId, uint64_t index);
162     void selectLegibleMediaOption(uint64_t contextId, uint64_t index);
163     void fullscreenModeChanged(uint64_t contextId, WebCore::HTMLMediaElement::VideoFullscreenMode);
164     void fullscreenMayReturnToInline(uint64_t contextId, bool isPageVisible);
165     
166     WebPage* m_page;
167     HashMap<WebCore::HTMLVideoElement*, uint64_t> m_videoElements;
168     HashMap<uint64_t, ModelInterfaceTuple> m_contextMap;
169 };
170     
171 } // namespace WebKit
172
173 #endif // PLATFORM(IOS)
174
175 #endif // WebVideoFullscreenManager_h