| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "core/html/MediaCustomControlsFullscreenDetector.h" | 5 #include "core/html/MediaCustomControlsFullscreenDetector.h" |
| 6 | 6 |
| 7 #include "core/dom/Fullscreen.h" | 7 #include "core/dom/Fullscreen.h" |
| 8 #include "core/dom/TaskRunnerHelper.h" | 8 #include "core/dom/TaskRunnerHelper.h" |
| 9 #include "core/events/Event.h" | 9 #include "core/events/Event.h" |
| 10 #include "core/html/HTMLVideoElement.h" | 10 #include "core/html/HTMLVideoElement.h" |
| 11 #include "core/layout/IntersectionGeometry.h" | 11 #include "core/layout/IntersectionGeometry.h" |
| 12 | 12 |
| 13 namespace blink { | 13 namespace blink { |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 constexpr double kCheckFullscreenIntervalSeconds = 1.0f; | 17 constexpr double kCheckFullscreenIntervalSeconds = 1.0f; |
| 18 constexpr float kMostlyFillViewportThresholdOfOccupationProportion = 0.85f; | 18 constexpr float kMostlyFillViewportThresholdOfOccupationProportion = 0.85f; |
| 19 constexpr float kMostlyFillViewportThresholdOfVisibleProportion = 0.75f; | 19 constexpr float kMostlyFillViewportThresholdOfVisibleProportion = 0.75f; |
| 20 | 20 |
| 21 } // anonymous namespace | 21 } // anonymous namespace |
| 22 | 22 |
| 23 MediaCustomControlsFullscreenDetector::MediaCustomControlsFullscreenDetector( | 23 MediaCustomControlsFullscreenDetector::MediaCustomControlsFullscreenDetector( |
| 24 HTMLVideoElement& video) | 24 HTMLVideoElement& video) |
| 25 : EventListener(CPPEventListenerType), | 25 : EventListener(CPPEventListenerType), |
| 26 ContextLifecycleObserver(nullptr), |
| 26 m_videoElement(video), | 27 m_videoElement(video), |
| 27 m_checkViewportIntersectionTimer( | 28 m_checkViewportIntersectionTimer( |
| 28 TaskRunnerHelper::get(TaskType::Unthrottled, &video.document()), | 29 TaskRunnerHelper::get(TaskType::Unthrottled, &video.document()), |
| 29 this, | 30 this, |
| 30 &MediaCustomControlsFullscreenDetector:: | 31 &MediaCustomControlsFullscreenDetector:: |
| 31 onCheckViewportIntersectionTimerFired) { | 32 onCheckViewportIntersectionTimerFired) { |
| 32 videoElement().addEventListener(EventTypeNames::DOMNodeInsertedIntoDocument, | 33 videoElement().addEventListener(EventTypeNames::DOMNodeInsertedIntoDocument, |
| 33 this, false); | 34 this, false); |
| 34 videoElement().addEventListener(EventTypeNames::DOMNodeRemovedFromDocument, | 35 videoElement().addEventListener(EventTypeNames::DOMNodeRemovedFromDocument, |
| 35 this, false); | 36 this, false); |
| 36 | 37 |
| 37 videoElement().addEventListener(EventTypeNames::loadedmetadata, this, true); | 38 videoElement().addEventListener(EventTypeNames::loadedmetadata, this, true); |
| 38 } | 39 } |
| 39 | 40 |
| 40 bool MediaCustomControlsFullscreenDetector::operator==( | 41 bool MediaCustomControlsFullscreenDetector::operator==( |
| 41 const EventListener& other) const { | 42 const EventListener& other) const { |
| 42 return this == &other; | 43 return this == &other; |
| 43 } | 44 } |
| 44 | 45 |
| 45 void MediaCustomControlsFullscreenDetector::attach() { | 46 void MediaCustomControlsFullscreenDetector::attach() { |
| 47 setContext(&videoElement().document()); |
| 46 videoElement().document().addEventListener( | 48 videoElement().document().addEventListener( |
| 47 EventTypeNames::webkitfullscreenchange, this, true); | 49 EventTypeNames::webkitfullscreenchange, this, true); |
| 48 videoElement().document().addEventListener(EventTypeNames::fullscreenchange, | 50 videoElement().document().addEventListener(EventTypeNames::fullscreenchange, |
| 49 this, true); | 51 this, true); |
| 50 } | 52 } |
| 51 | 53 |
| 52 void MediaCustomControlsFullscreenDetector::detach() { | 54 void MediaCustomControlsFullscreenDetector::detach() { |
| 55 setContext(nullptr); |
| 53 videoElement().document().removeEventListener( | 56 videoElement().document().removeEventListener( |
| 54 EventTypeNames::webkitfullscreenchange, this, true); | 57 EventTypeNames::webkitfullscreenchange, this, true); |
| 55 videoElement().document().removeEventListener( | 58 videoElement().document().removeEventListener( |
| 56 EventTypeNames::fullscreenchange, this, true); | 59 EventTypeNames::fullscreenchange, this, true); |
| 60 m_checkViewportIntersectionTimer.stop(); |
| 57 } | 61 } |
| 58 | 62 |
| 59 bool MediaCustomControlsFullscreenDetector::computeIsDominantVideoForTests( | 63 bool MediaCustomControlsFullscreenDetector::computeIsDominantVideoForTests( |
| 60 const IntRect& targetRect, | 64 const IntRect& targetRect, |
| 61 const IntRect& rootRect, | 65 const IntRect& rootRect, |
| 62 const IntRect& intersectionRect) { | 66 const IntRect& intersectionRect) { |
| 63 if (targetRect.isEmpty() || rootRect.isEmpty()) | 67 if (targetRect.isEmpty() || rootRect.isEmpty()) |
| 64 return false; | 68 return false; |
| 65 | 69 |
| 66 const float xOccupationProportion = | 70 const float xOccupationProportion = |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 if (videoElement().webMediaPlayer()) | 123 if (videoElement().webMediaPlayer()) |
| 120 videoElement().webMediaPlayer()->setIsEffectivelyFullscreen(false); | 124 videoElement().webMediaPlayer()->setIsEffectivelyFullscreen(false); |
| 121 | 125 |
| 122 return; | 126 return; |
| 123 } | 127 } |
| 124 | 128 |
| 125 m_checkViewportIntersectionTimer.startOneShot(kCheckFullscreenIntervalSeconds, | 129 m_checkViewportIntersectionTimer.startOneShot(kCheckFullscreenIntervalSeconds, |
| 126 BLINK_FROM_HERE); | 130 BLINK_FROM_HERE); |
| 127 } | 131 } |
| 128 | 132 |
| 133 void MediaCustomControlsFullscreenDetector::contextDestroyed( |
| 134 ExecutionContext*) { |
| 135 if (videoElement().webMediaPlayer()) |
| 136 videoElement().webMediaPlayer()->setIsEffectivelyFullscreen(false); |
| 137 |
| 138 detach(); |
| 139 } |
| 140 |
| 129 void MediaCustomControlsFullscreenDetector:: | 141 void MediaCustomControlsFullscreenDetector:: |
| 130 onCheckViewportIntersectionTimerFired(TimerBase*) { | 142 onCheckViewportIntersectionTimerFired(TimerBase*) { |
| 131 DCHECK(isVideoOrParentFullscreen()); | 143 DCHECK(isVideoOrParentFullscreen()); |
| 132 IntersectionGeometry geometry(nullptr, videoElement(), Vector<Length>(), | 144 IntersectionGeometry geometry(nullptr, videoElement(), Vector<Length>(), |
| 133 true); | 145 true); |
| 134 geometry.computeGeometry(); | 146 geometry.computeGeometry(); |
| 135 | 147 |
| 136 bool isDominant = computeIsDominantVideoForTests( | 148 bool isDominant = computeIsDominantVideoForTests( |
| 137 geometry.targetIntRect(), geometry.rootIntRect(), | 149 geometry.targetIntRect(), geometry.rootIntRect(), |
| 138 geometry.intersectionIntRect()); | 150 geometry.intersectionIntRect()); |
| 139 | 151 |
| 140 if (videoElement().webMediaPlayer()) | 152 if (videoElement().webMediaPlayer()) |
| 141 videoElement().webMediaPlayer()->setIsEffectivelyFullscreen(isDominant); | 153 videoElement().webMediaPlayer()->setIsEffectivelyFullscreen(isDominant); |
| 142 } | 154 } |
| 143 | 155 |
| 144 bool MediaCustomControlsFullscreenDetector::isVideoOrParentFullscreen() { | 156 bool MediaCustomControlsFullscreenDetector::isVideoOrParentFullscreen() { |
| 145 Element* fullscreenElement = | 157 Element* fullscreenElement = |
| 146 Fullscreen::currentFullScreenElementFrom(videoElement().document()); | 158 Fullscreen::currentFullScreenElementFrom(videoElement().document()); |
| 147 if (!fullscreenElement) | 159 if (!fullscreenElement) |
| 148 return false; | 160 return false; |
| 149 | 161 |
| 150 return fullscreenElement->contains(&videoElement()); | 162 return fullscreenElement->contains(&videoElement()); |
| 151 } | 163 } |
| 152 | 164 |
| 153 DEFINE_TRACE(MediaCustomControlsFullscreenDetector) { | 165 DEFINE_TRACE(MediaCustomControlsFullscreenDetector) { |
| 154 EventListener::trace(visitor); | 166 EventListener::trace(visitor); |
| 167 ContextLifecycleObserver::trace(visitor); |
| 155 visitor->trace(m_videoElement); | 168 visitor->trace(m_videoElement); |
| 156 } | 169 } |
| 157 | 170 |
| 158 } // namespace blink | 171 } // namespace blink |
| OLD | NEW |