| 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), | |
| 27 m_videoElement(video), | 26 m_videoElement(video), |
| 28 m_checkViewportIntersectionTimer( | 27 m_checkViewportIntersectionTimer( |
| 29 TaskRunnerHelper::get(TaskType::Unthrottled, &video.document()), | 28 TaskRunnerHelper::get(TaskType::Unthrottled, &video.document()), |
| 30 this, | 29 this, |
| 31 &MediaCustomControlsFullscreenDetector:: | 30 &MediaCustomControlsFullscreenDetector:: |
| 32 onCheckViewportIntersectionTimerFired) { | 31 onCheckViewportIntersectionTimerFired) { |
| 33 videoElement().addEventListener(EventTypeNames::DOMNodeInsertedIntoDocument, | 32 videoElement().addEventListener(EventTypeNames::DOMNodeInsertedIntoDocument, |
| 34 this, false); | 33 this, false); |
| 35 videoElement().addEventListener(EventTypeNames::DOMNodeRemovedFromDocument, | 34 videoElement().addEventListener(EventTypeNames::DOMNodeRemovedFromDocument, |
| 36 this, false); | 35 this, false); |
| 37 | 36 |
| 38 videoElement().addEventListener(EventTypeNames::loadedmetadata, this, true); | 37 videoElement().addEventListener(EventTypeNames::loadedmetadata, this, true); |
| 39 } | 38 } |
| 40 | 39 |
| 41 bool MediaCustomControlsFullscreenDetector::operator==( | 40 bool MediaCustomControlsFullscreenDetector::operator==( |
| 42 const EventListener& other) const { | 41 const EventListener& other) const { |
| 43 return this == &other; | 42 return this == &other; |
| 44 } | 43 } |
| 45 | 44 |
| 46 void MediaCustomControlsFullscreenDetector::attach() { | 45 void MediaCustomControlsFullscreenDetector::attach() { |
| 47 setContext(&videoElement().document()); | |
| 48 videoElement().document().addEventListener( | 46 videoElement().document().addEventListener( |
| 49 EventTypeNames::webkitfullscreenchange, this, true); | 47 EventTypeNames::webkitfullscreenchange, this, true); |
| 50 videoElement().document().addEventListener(EventTypeNames::fullscreenchange, | 48 videoElement().document().addEventListener(EventTypeNames::fullscreenchange, |
| 51 this, true); | 49 this, true); |
| 52 } | 50 } |
| 53 | 51 |
| 54 void MediaCustomControlsFullscreenDetector::detach() { | 52 void MediaCustomControlsFullscreenDetector::detach() { |
| 55 setContext(nullptr); | |
| 56 videoElement().document().removeEventListener( | 53 videoElement().document().removeEventListener( |
| 57 EventTypeNames::webkitfullscreenchange, this, true); | 54 EventTypeNames::webkitfullscreenchange, this, true); |
| 58 videoElement().document().removeEventListener( | 55 videoElement().document().removeEventListener( |
| 59 EventTypeNames::fullscreenchange, this, true); | 56 EventTypeNames::fullscreenchange, this, true); |
| 60 m_checkViewportIntersectionTimer.stop(); | 57 m_checkViewportIntersectionTimer.stop(); |
| 58 |
| 59 if (videoElement().webMediaPlayer()) |
| 60 videoElement().webMediaPlayer()->setIsEffectivelyFullscreen(false); |
| 61 } | 61 } |
| 62 | 62 |
| 63 bool MediaCustomControlsFullscreenDetector::computeIsDominantVideoForTests( | 63 bool MediaCustomControlsFullscreenDetector::computeIsDominantVideoForTests( |
| 64 const IntRect& targetRect, | 64 const IntRect& targetRect, |
| 65 const IntRect& rootRect, | 65 const IntRect& rootRect, |
| 66 const IntRect& intersectionRect) { | 66 const IntRect& intersectionRect) { |
| 67 if (targetRect.isEmpty() || rootRect.isEmpty()) | 67 if (targetRect.isEmpty() || rootRect.isEmpty()) |
| 68 return false; | 68 return false; |
| 69 | 69 |
| 70 const float xOccupationProportion = | 70 const float xOccupationProportion = |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 if (videoElement().webMediaPlayer()) | 123 if (videoElement().webMediaPlayer()) |
| 124 videoElement().webMediaPlayer()->setIsEffectivelyFullscreen(false); | 124 videoElement().webMediaPlayer()->setIsEffectivelyFullscreen(false); |
| 125 | 125 |
| 126 return; | 126 return; |
| 127 } | 127 } |
| 128 | 128 |
| 129 m_checkViewportIntersectionTimer.startOneShot(kCheckFullscreenIntervalSeconds, | 129 m_checkViewportIntersectionTimer.startOneShot(kCheckFullscreenIntervalSeconds, |
| 130 BLINK_FROM_HERE); | 130 BLINK_FROM_HERE); |
| 131 } | 131 } |
| 132 | 132 |
| 133 void MediaCustomControlsFullscreenDetector::contextDestroyed( | 133 void MediaCustomControlsFullscreenDetector::contextDestroyed() { |
| 134 ExecutionContext*) { | 134 // This method is called by HTMLVideoElement when it observes context destroy. |
| 135 if (videoElement().webMediaPlayer()) | 135 // The reason is that when HTMLMediaElement observes context destroy, it will |
| 136 videoElement().webMediaPlayer()->setIsEffectivelyFullscreen(false); | 136 // destroy webMediaPlayer() thus the final setIsEffectivelyFullscreen(false) |
| 137 | 137 // is not called. |
| 138 detach(); | 138 detach(); |
| 139 } | 139 } |
| 140 | 140 |
| 141 void MediaCustomControlsFullscreenDetector:: | 141 void MediaCustomControlsFullscreenDetector:: |
| 142 onCheckViewportIntersectionTimerFired(TimerBase*) { | 142 onCheckViewportIntersectionTimerFired(TimerBase*) { |
| 143 DCHECK(isVideoOrParentFullscreen()); | 143 DCHECK(isVideoOrParentFullscreen()); |
| 144 IntersectionGeometry geometry(nullptr, videoElement(), Vector<Length>(), | 144 IntersectionGeometry geometry(nullptr, videoElement(), Vector<Length>(), |
| 145 true); | 145 true); |
| 146 geometry.computeGeometry(); | 146 geometry.computeGeometry(); |
| 147 | 147 |
| 148 bool isDominant = computeIsDominantVideoForTests( | 148 bool isDominant = computeIsDominantVideoForTests( |
| 149 geometry.targetIntRect(), geometry.rootIntRect(), | 149 geometry.targetIntRect(), geometry.rootIntRect(), |
| 150 geometry.intersectionIntRect()); | 150 geometry.intersectionIntRect()); |
| 151 | 151 |
| 152 if (videoElement().webMediaPlayer()) | 152 if (videoElement().webMediaPlayer()) |
| 153 videoElement().webMediaPlayer()->setIsEffectivelyFullscreen(isDominant); | 153 videoElement().webMediaPlayer()->setIsEffectivelyFullscreen(isDominant); |
| 154 } | 154 } |
| 155 | 155 |
| 156 bool MediaCustomControlsFullscreenDetector::isVideoOrParentFullscreen() { | 156 bool MediaCustomControlsFullscreenDetector::isVideoOrParentFullscreen() { |
| 157 Element* fullscreenElement = | 157 Element* fullscreenElement = |
| 158 Fullscreen::currentFullScreenElementFrom(videoElement().document()); | 158 Fullscreen::currentFullScreenElementFrom(videoElement().document()); |
| 159 if (!fullscreenElement) | 159 if (!fullscreenElement) |
| 160 return false; | 160 return false; |
| 161 | 161 |
| 162 return fullscreenElement->contains(&videoElement()); | 162 return fullscreenElement->contains(&videoElement()); |
| 163 } | 163 } |
| 164 | 164 |
| 165 DEFINE_TRACE(MediaCustomControlsFullscreenDetector) { | 165 DEFINE_TRACE(MediaCustomControlsFullscreenDetector) { |
| 166 EventListener::trace(visitor); | 166 EventListener::trace(visitor); |
| 167 ContextLifecycleObserver::trace(visitor); | |
| 168 visitor->trace(m_videoElement); | 167 visitor->trace(m_videoElement); |
| 169 } | 168 } |
| 170 | 169 |
| 171 } // namespace blink | 170 } // namespace blink |
| OLD | NEW |