Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 "core/html/MediaCustomControlsFullscreenDetector.h" | |
| 6 | |
| 7 #include "core/dom/DocumentFullscreen.h" | |
| 8 #include "core/dom/TaskRunnerHelper.h" | |
| 9 #include "core/events/Event.h" | |
| 10 #include "core/html/HTMLVideoElement.h" | |
| 11 #include "core/layout/IntersectionGeometry.h" | |
| 12 | |
| 13 namespace blink { | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 constexpr double kCheckFullscreenIntervalSeconds = 1.0f; | |
| 18 constexpr float kMostlyFillViewportThresholdOfOccupationProportion = 0.85f; | |
| 19 constexpr float kMostlyFillViewportThresholdOfVisibleProportion = 0.75f; | |
| 20 | |
| 21 } // anonymous namespace | |
| 22 | |
| 23 MediaCustomControlsFullscreenDetector::MediaCustomControlsFullscreenDetector( | |
| 24 HTMLVideoElement& video) | |
| 25 : EventListener(CPPEventListenerType), | |
| 26 m_videoElement(video), | |
| 27 m_checkViewportIntersectionTimer( | |
| 28 TaskRunnerHelper::get(TaskType::Unthrottled, &video.document()), | |
| 29 this, | |
| 30 &MediaCustomControlsFullscreenDetector:: | |
| 31 onCheckViewportIntersectionTimerFired) { | |
| 32 videoElement().document().addEventListener( | |
| 33 EventTypeNames::webkitfullscreenchange, this, true); | |
| 34 videoElement().document().addEventListener(EventTypeNames::fullscreenchange, | |
| 35 this, true); | |
| 36 } | |
| 37 | |
| 38 bool MediaCustomControlsFullscreenDetector::operator==( | |
| 39 const EventListener& other) const { | |
| 40 return this == &other; | |
| 41 } | |
| 42 | |
| 43 void MediaCustomControlsFullscreenDetector::didMoveToNewDocument( | |
| 44 Document& oldDocument) { | |
| 45 oldDocument.removeEventListener(EventTypeNames::webkitfullscreenchange, this, | |
| 46 true); | |
| 47 oldDocument.removeEventListener(EventTypeNames::fullscreenchange, this, true); | |
|
mlamouri (slow - plz ping)
2017/02/21 21:58:28
We should call addEventListener on the new documen
Zhiqiang Zhang (Slow)
2017/02/22 12:29:18
Done.
| |
| 48 } | |
| 49 | |
| 50 bool MediaCustomControlsFullscreenDetector::computeIsDominantVideo( | |
| 51 bool isVideoOrParentFullscreen, | |
| 52 const IntRect& targetRect, | |
| 53 const IntRect& rootRect, | |
| 54 const IntRect& intersectionRect) { | |
| 55 if (!isVideoOrParentFullscreen) | |
| 56 return false; | |
|
mlamouri (slow - plz ping)
2017/02/21 21:58:28
Can this happen? We should be fullscreen, right?
Zhiqiang Zhang (Slow)
2017/02/22 12:29:18
Done, removed this parameter as we should be in fu
| |
| 57 | |
| 58 const float xOccupationProportion = | |
| 59 1.0f * intersectionRect.width() / rootRect.width(); | |
| 60 const float yOccupationProportion = | |
| 61 1.0f * intersectionRect.height() / rootRect.height(); | |
| 62 | |
| 63 // If the viewport is mostly occupied by the video, return true. | |
| 64 if (std::min(xOccupationProportion, yOccupationProportion) >= | |
| 65 kMostlyFillViewportThresholdOfOccupationProportion) { | |
| 66 return true; | |
| 67 } | |
| 68 | |
| 69 // If neither of the dimensions of the viewport is mostly occupied by the | |
| 70 // video, return false. | |
| 71 if (std::max(xOccupationProportion, yOccupationProportion) < | |
| 72 kMostlyFillViewportThresholdOfOccupationProportion) { | |
| 73 return false; | |
| 74 } | |
| 75 | |
| 76 // If the video is mostly visible in the indominant dimension, return true. | |
| 77 // Otherwise return false. | |
| 78 if (xOccupationProportion > yOccupationProportion) { | |
| 79 return targetRect.height() * | |
| 80 kMostlyFillViewportThresholdOfVisibleProportion < | |
| 81 intersectionRect.height(); | |
| 82 } | |
| 83 return targetRect.width() * kMostlyFillViewportThresholdOfVisibleProportion < | |
| 84 intersectionRect.width(); | |
| 85 } | |
| 86 | |
| 87 void MediaCustomControlsFullscreenDetector::handleEvent( | |
| 88 ExecutionContext* context, | |
| 89 Event* event) { | |
| 90 DCHECK(event->type() == EventTypeNames::fullscreenchange || | |
| 91 event->type() == EventTypeNames::webkitfullscreenchange); | |
| 92 if (!isVideoOrParentFullscreen()) { | |
| 93 if (videoElement().webMediaPlayer()) { | |
| 94 videoElement().webMediaPlayer()->becameDominantVisibleContentInFullscreen( | |
| 95 false); | |
|
mlamouri (slow - plz ping)
2017/02/21 21:58:28
We should probably cancel the timer too so we guar
Zhiqiang Zhang (Slow)
2017/02/22 12:29:18
Done.
| |
| 96 } | |
| 97 return; | |
| 98 } | |
| 99 | |
| 100 m_checkViewportIntersectionTimer.startOneShot(kCheckFullscreenIntervalSeconds, | |
| 101 BLINK_FROM_HERE); | |
| 102 } | |
| 103 | |
| 104 void MediaCustomControlsFullscreenDetector:: | |
| 105 onCheckViewportIntersectionTimerFired(TimerBase*) { | |
| 106 IntersectionGeometry geometry(nullptr, videoElement(), Vector<Length>(), | |
| 107 true); | |
| 108 geometry.computeGeometry(); | |
| 109 | |
| 110 bool isDominant = computeIsDominantVideo( | |
| 111 isVideoOrParentFullscreen(), geometry.targetIntRect(), | |
| 112 geometry.rootIntRect(), geometry.intersectionIntRect()); | |
| 113 | |
| 114 if (videoElement().webMediaPlayer()) { | |
| 115 videoElement().webMediaPlayer()->becameDominantVisibleContentInFullscreen( | |
| 116 isDominant); | |
|
mlamouri (slow - plz ping)
2017/02/21 21:58:28
What happens if the video goes fullscreen before p
Zhiqiang Zhang (Slow)
2017/02/22 12:29:18
Now I also listen to loadedmetadata. When any even
| |
| 117 } | |
| 118 } | |
| 119 | |
| 120 bool MediaCustomControlsFullscreenDetector::isVideoOrParentFullscreen() { | |
| 121 return DocumentFullscreen::fullscreenElement(videoElement().document()); | |
| 122 } | |
| 123 | |
| 124 DEFINE_TRACE(MediaCustomControlsFullscreenDetector) { | |
| 125 EventListener::trace(visitor); | |
| 126 visitor->trace(m_videoElement); | |
| 127 } | |
| 128 | |
| 129 } // namespace blink | |
| OLD | NEW |