Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(321)

Side by Side Diff: third_party/WebKit/Source/core/html/shadow/MediaControlsOrientationLockDelegate.cpp

Issue 2556573003: Media: lock orientation when <video> goes fullscreen. (Closed)
Patch Set: with unit tests Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "MediaControlsOrientationLockDelegate.h"
6
7 #include "core/events/Event.h"
8 #include "core/frame/FrameHost.h"
9 #include "core/frame/ScreenOrientationController.h"
10 #include "core/html/HTMLVideoElement.h"
11 #include "core/page/ChromeClient.h"
12 #include "public/platform/WebScreenInfo.h"
13 #include "public/platform/modules/screen_orientation/WebLockOrientationCallback. h"
14
15 namespace blink {
16
17 namespace {
18
19 // WebLockOrientationCallback implementation that will not react to a success
20 // nor a failure.
21 class DummyScreenOrientationCallback : public WebLockOrientationCallback {
22 public:
23 void onSuccess() override {}
24 void onError(WebLockOrientationError) override {}
25 };
26
27 } // anonymous namespace
28
29 MediaControlsOrientationLockDelegate::MediaControlsOrientationLockDelegate(
30 MediaControls* mediaControls)
31 : EventListener(CPPEventListenerType), m_mediaControls(mediaControls) {
32 DCHECK(m_mediaControls->mediaElement().isHTMLVideoElement());
33
34 document().addEventListener(EventTypeNames::fullscreenchange, this, false);
35 document().addEventListener(EventTypeNames::webkitfullscreenchange, this,
foolip 2016/12/12 23:16:05 Because webkitfullscreenchange targets and element
mlamouri (slow - plz ping) 2016/12/13 10:25:17 Is your point that HTMLMediaElement is always noti
foolip 2016/12/13 10:35:39 It would be nice to not have a separate mechanism,
mlamouri (slow - plz ping) 2016/12/13 21:15:16 Listening to `fullscreenchange` on document and `w
36 false);
37
38 videoElement().addEventListener(EventTypeNames::loadedmetadata, this, false);
Zhiqiang Zhang (Slow) 2016/12/13 11:53:28 nit: Wouldn't this listen to loadedmetadata too of
mlamouri (slow - plz ping) 2016/12/13 21:15:16 loadedmetadata is generally called once. The overh
39 }
40
41 bool MediaControlsOrientationLockDelegate::operator==(
42 const EventListener& other) const {
43 return this == &other;
44 }
45
46 void MediaControlsOrientationLockDelegate::maybeLockOrientation() {
47 DCHECK(m_state != State::MaybeLockedFullscreen);
48
49 if (videoElement().getReadyState() == HTMLMediaElement::kHaveNothing) {
50 m_state = State::PendingMetadata;
51 return;
52 }
53
54 m_state = State::MaybeLockedFullscreen;
55
56 auto controller = ScreenOrientationController::from(*document().frame());
foolip 2016/12/12 23:16:05 Is it impossible for document().frame() to be null
mlamouri (slow - plz ping) 2016/12/13 21:15:16 Fixed.
57 if (controller->maybeHasActiveLock())
58 return;
59
60 controller->lock(computeOrientationLock(),
61 WTF::wrapUnique(new DummyScreenOrientationCallback));
62 m_shouldUnlockOrientation = true;
63 }
64
65 void MediaControlsOrientationLockDelegate::maybeUnlockOrientation() {
66 DCHECK(m_state != State::PendingFullscreen);
67
68 m_state = State::PendingFullscreen;
69
70 if (!m_shouldUnlockOrientation)
71 return;
72
73 ScreenOrientationController::from(*document().frame())->unlock();
74 m_shouldUnlockOrientation = false;
75 }
76
77 HTMLVideoElement& MediaControlsOrientationLockDelegate::videoElement() const {
78 return toHTMLVideoElement(m_mediaControls->mediaElement());
79 }
80
81 Document& MediaControlsOrientationLockDelegate::document() const {
82 return videoElement().document();
83 }
84
85 void MediaControlsOrientationLockDelegate::handleEvent(
86 ExecutionContext* executionContext,
87 Event* event) {
88 if (event->type() == EventTypeNames::fullscreenchange ||
89 event->type() == EventTypeNames::webkitfullscreenchange) {
90 if (videoElement().isFullscreen()) {
91 if (m_state == State::PendingFullscreen)
92 maybeLockOrientation();
93 } else {
94 if (m_state != State::PendingFullscreen)
95 maybeUnlockOrientation();
96 }
97
98 return;
99 }
100
101 if (event->type() == EventTypeNames::loadedmetadata) {
102 if (m_state == State::PendingMetadata)
103 maybeLockOrientation();
104
105 return;
106 }
107
108 NOTREACHED();
109 }
110
111 WebScreenOrientationLockType
112 MediaControlsOrientationLockDelegate::computeOrientationLock() const {
DaleCurtis 2016/12/12 23:25:13 Doesn't this need to know the current rotation? Th
mlamouri (slow - plz ping) 2016/12/13 10:25:17 I was planning to handle this as a follow-up. I ha
113 const unsigned width = videoElement().videoWidth();
foolip 2016/12/12 23:16:05 DCHECK something about readyState?
mlamouri (slow - plz ping) 2016/12/13 21:15:16 Done.
114 const unsigned height = videoElement().videoHeight();
115
116 if (width > height)
117 return WebScreenOrientationLockLandscape;
118
119 if (height > width)
120 return WebScreenOrientationLockPortrait;
121
122 // For square videos, try to lock to the current screen orientation for
123 // consistency. Use WebScreenOrientationLockLandscape as a fallback value.
124 // TODO(mlamouri): we could improve this by having direct access to
125 // `window.screen.orientation.type`.
126 Frame* frame = document().frame();
127 if (!frame || !frame->host())
128 return WebScreenOrientationLockLandscape;
129
130 switch (frame->host()->chromeClient().screenInfo().orientationType) {
foolip 2016/12/12 23:16:05 If you use frame->chromeClient() you can skip the
mlamouri (slow - plz ping) 2016/12/13 21:15:16 Done.
131 case WebScreenOrientationPortraitPrimary:
132 case WebScreenOrientationPortraitSecondary:
133 return WebScreenOrientationLockPortrait;
134 case WebScreenOrientationLandscapePrimary:
135 case WebScreenOrientationLandscapeSecondary:
136 return WebScreenOrientationLockLandscape;
137 case WebScreenOrientationUndefined:
138 return WebScreenOrientationLockLandscape;
139 }
140 }
141
142 DEFINE_TRACE(MediaControlsOrientationLockDelegate) {
143 EventListener::trace(visitor);
144 visitor->trace(m_mediaControls);
145 }
146
147 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698