b498ae8e5ba87f2b2413aef8c46ba1fe4765de77
[WebKit-https.git] / Source / WebKit2 / UIProcess / ios / WebVideoFullscreenManagerProxy.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
26 #ifndef WebVideoFullscreenManagerProxy_h
27 #define WebVideoFullscreenManagerProxy_h
28
29 #if PLATFORM(IOS)
30
31 #include "MessageReceiver.h"
32 #include <WebCore/GraphicsLayer.h>
33 #include <WebCore/WebVideoFullscreenInterfaceAVKit.h>
34 #include <WebCore/WebVideoFullscreenModel.h>
35 #include <wtf/PassRefPtr.h>
36 #include <wtf/RefCounted.h>
37 #include <wtf/RefPtr.h>
38
39 OBJC_CLASS UIView;
40
41 namespace WebKit {
42
43 class WebPageProxy;
44 class WebVideoFullscreenManagerProxy;
45
46 class WebVideoFullscreenModelContext final: public RefCounted<WebVideoFullscreenModelContext>, public WebCore::WebVideoFullscreenModel, public WebCore::WebVideoFullscreenChangeObserver  {
47 public:
48     static Ref<WebVideoFullscreenModelContext> create(WebVideoFullscreenManagerProxy& manager, uint64_t contextId)
49     {
50         return adoptRef(*new WebVideoFullscreenModelContext(manager, contextId));
51     }
52     virtual ~WebVideoFullscreenModelContext() { }
53
54     void invalidate() { m_manager = nullptr; }
55
56     PlatformLayer* layerHost() const { return m_layerHost.get(); }
57     void setLayerHost(RetainPtr<PlatformLayer>&& layerHost) { m_layerHost = WTF::move(layerHost); }
58
59     void setInitialVideoLayerFrame(WebCore::FloatRect frame) { m_videoLayerFrame = frame; }
60
61 private:
62     WebVideoFullscreenModelContext(WebVideoFullscreenManagerProxy& manager, uint64_t contextId)
63         : m_manager(&manager)
64         , m_contextId(contextId)
65     {
66     }
67
68     // WebVideoFullscreenModel
69     virtual void play() override;
70     virtual void pause() override;
71     virtual void togglePlayState() override;
72     virtual void beginScrubbing() override;
73     virtual void endScrubbing() override;
74     virtual void seekToTime(double) override;
75     virtual void fastSeek(double time) override;
76     virtual void beginScanningForward() override;
77     virtual void beginScanningBackward() override;
78     virtual void endScanning() override;
79     virtual void requestExitFullscreen() override;
80     virtual void setVideoLayerFrame(WebCore::FloatRect) override;
81     virtual WebCore::FloatRect videoLayerFrame() const override;
82     virtual void setVideoLayerGravity(VideoGravity) override;
83     virtual VideoGravity videoLayerGravity() const override;
84     virtual void selectAudioMediaOption(uint64_t) override;
85     virtual void selectLegibleMediaOption(uint64_t) override;
86     virtual void fullscreenModeChanged(WebCore::HTMLMediaElement::VideoFullscreenMode) override;
87
88     // WebVideoFullscreenChangeObserver
89     virtual void didSetupFullscreen() override;
90     virtual void didEnterFullscreen() override;
91     virtual void didExitFullscreen() override;
92     virtual void didCleanupFullscreen() override;
93     virtual void fullscreenMayReturnToInline() override;
94
95     WebVideoFullscreenManagerProxy* m_manager;
96     uint64_t m_contextId;
97     RetainPtr<PlatformLayer> m_layerHost;
98     WebCore::FloatRect m_videoLayerFrame;
99     VideoGravity m_videoLayerGravity { VideoGravityResize };
100 };
101
102 class WebVideoFullscreenManagerProxy : public RefCounted<WebVideoFullscreenManagerProxy>, private IPC::MessageReceiver {
103 public:
104     static PassRefPtr<WebVideoFullscreenManagerProxy> create(WebPageProxy&);
105     virtual ~WebVideoFullscreenManagerProxy();
106
107     void invalidate();
108
109     void requestHideAndExitFullscreen();
110     bool hasMode(WebCore::HTMLMediaElement::VideoFullscreenMode) const;
111     bool mayAutomaticallyShowVideoOptimized() const;
112
113 private:
114     friend class WebVideoFullscreenModelContext;
115
116     explicit WebVideoFullscreenManagerProxy(WebPageProxy&);
117     virtual void didReceiveMessage(IPC::Connection&, IPC::MessageDecoder&) override;
118
119     typedef std::tuple<RefPtr<WebVideoFullscreenModelContext>, RefPtr<WebCore::WebVideoFullscreenInterfaceAVKit>> ModelInterfaceTuple;
120     ModelInterfaceTuple createModelAndInterface(uint64_t contextId);
121     ModelInterfaceTuple& ensureModelAndInterface(uint64_t contextId);
122     WebVideoFullscreenModelContext& ensureModel(uint64_t contextId);
123     WebCore::WebVideoFullscreenInterfaceAVKit& ensureInterface(uint64_t contextId);
124
125     // Messages from WebVideoFullscreenManager
126     void setupFullscreenWithID(uint64_t contextId, uint32_t videoLayerID, const WebCore::IntRect& initialRect, float hostingScaleFactor, WebCore::HTMLMediaElement::VideoFullscreenMode, bool allowOptimizedFullscreen);
127     void resetMediaState(uint64_t contextId);
128     void setCurrentTime(uint64_t contextId, double currentTime, double hostTime);
129     void setBufferedTime(uint64_t contextId, double bufferedTime);
130     void setVideoDimensions(uint64_t contextId, bool hasVideo, unsigned width, unsigned height);
131     void setSeekableRangesVector(uint64_t contextId, Vector<std::pair<double, double>> ranges);
132     void setCanPlayFastReverse(uint64_t contextId, bool value);
133     void setAudioMediaSelectionOptions(uint64_t contextId, Vector<String> options, uint64_t selectedIndex);
134     void setLegibleMediaSelectionOptions(uint64_t contextId, Vector<String> options, uint64_t selectedIndex);
135     void setExternalPlaybackProperties(uint64_t contextId, bool enabled, uint32_t targetType, String localizedDeviceName);
136     void setDuration(uint64_t contextId, double duration);
137     void setRate(uint64_t contextId, bool isPlaying, double rate);
138     void enterFullscreen(uint64_t contextId);
139     void exitFullscreen(uint64_t contextId, WebCore::IntRect finalRect);
140     void cleanupFullscreen(uint64_t contextId);
141     void preparedToReturnToInline(uint64_t contextId, bool visible, WebCore::IntRect inlineRect);
142
143     // Messages to WebVideoFullscreenManager
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 setVideoLayerFrame(uint64_t contextId, WebCore::FloatRect);
160     void setVideoLayerGravity(uint64_t contextId, WebCore::WebVideoFullscreenModel::VideoGravity);
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);
165
166     WebPageProxy* m_page;
167     HashMap<uint64_t, ModelInterfaceTuple> m_contextMap;
168
169 };
170     
171 } // namespace WebKit
172
173 #endif // PLATFORM(IOS)
174
175 #endif // WebVideoFullscreenManagerProxy_h