Chromium Code Reviews| 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 "modules/media_controls/elements/MediaControlCastButtonElement.h" | 5 #include "modules/media_controls/elements/MediaControlCastButtonElement.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ScriptState.h" | |
| 8 #include "bindings/core/v8/ToV8.h" | |
| 7 #include "core/InputTypeNames.h" | 9 #include "core/InputTypeNames.h" |
| 8 #include "core/dom/ClientRect.h" | 10 #include "core/dom/ClientRect.h" |
| 9 #include "core/events/Event.h" | 11 #include "core/events/Event.h" |
| 12 #include "core/frame/Settings.h" | |
| 10 #include "core/html/HTMLMediaElement.h" | 13 #include "core/html/HTMLMediaElement.h" |
| 14 #include "core/html/media/HTMLMediaElementControlsList.h" | |
| 11 #include "modules/media_controls/MediaControlsImpl.h" | 15 #include "modules/media_controls/MediaControlsImpl.h" |
| 12 #include "modules/media_controls/elements/MediaControlElementsHelper.h" | 16 #include "modules/media_controls/elements/MediaControlElementsHelper.h" |
| 17 #include "modules/remoteplayback/HTMLMediaElementRemotePlayback.h" | |
| 18 #include "modules/remoteplayback/RemotePlayback.h" | |
| 19 #include "platform/wtf/Functional.h" | |
| 13 #include "public/platform/Platform.h" | 20 #include "public/platform/Platform.h" |
| 14 | 21 |
| 15 namespace blink { | 22 namespace blink { |
| 16 | 23 |
| 17 namespace { | 24 MediaControlCastButtonElement::RemotePlaybackEventListener:: |
| 25 RemotePlaybackEventListener(MediaControlCastButtonElement* cast_button) | |
| 26 : EventListener(kCPPEventListenerType), cast_button_(cast_button) {} | |
| 18 | 27 |
| 19 Element* ElementFromCenter(Element& element) { | 28 bool MediaControlCastButtonElement::RemotePlaybackEventListener::operator==( |
| 20 ClientRect* client_rect = element.getBoundingClientRect(); | 29 const EventListener& other) const { |
| 21 int center_x = | 30 return this == &other; |
| 22 static_cast<int>((client_rect->left() + client_rect->right()) / 2); | |
| 23 int center_y = | |
| 24 static_cast<int>((client_rect->top() + client_rect->bottom()) / 2); | |
| 25 | |
| 26 return element.GetDocument().ElementFromPoint(center_x, center_y); | |
| 27 } | 31 } |
| 28 | 32 |
| 29 } // anonymous namespace | 33 void MediaControlCastButtonElement::RemotePlaybackEventListener::handleEvent( |
| 34 ExecutionContext* execution_context, | |
| 35 Event* event) { | |
| 36 DCHECK(event->type() == EventTypeNames::connect || | |
| 37 event->type() == EventTypeNames::connecting || | |
| 38 event->type() == EventTypeNames::disconnect); | |
| 39 cast_button_->UpdateDisplayType(); | |
| 40 } | |
| 41 | |
| 42 DEFINE_TRACE(MediaControlCastButtonElement::RemotePlaybackEventListener) { | |
| 43 EventListener::Trace(visitor); | |
| 44 visitor->Trace(cast_button_); | |
| 45 } | |
| 30 | 46 |
| 31 MediaControlCastButtonElement::MediaControlCastButtonElement( | 47 MediaControlCastButtonElement::MediaControlCastButtonElement( |
| 32 MediaControlsImpl& media_controls, | 48 MediaControlsImpl& media_controls, |
| 33 bool is_overlay_button) | 49 bool is_overlay_button) |
| 34 : MediaControlInputElement(media_controls, kMediaCastOnButton), | 50 : MediaControlInputElement(media_controls, kMediaCastOnButton), |
| 35 is_overlay_button_(is_overlay_button) { | 51 is_overlay_button_(is_overlay_button), |
| 52 remote_playback_(HTMLMediaElementRemotePlayback::remote( | |
| 53 media_controls.MediaElement())), | |
| 54 remote_playback_event_listener_(new RemotePlaybackEventListener(this)) { | |
| 36 EnsureUserAgentShadowRoot(); | 55 EnsureUserAgentShadowRoot(); |
| 37 SetShadowPseudoId(is_overlay_button | 56 SetShadowPseudoId(is_overlay_button |
| 38 ? "-internal-media-controls-overlay-cast-button" | 57 ? "-internal-media-controls-overlay-cast-button" |
| 39 : "-internal-media-controls-cast-button"); | 58 : "-internal-media-controls-cast-button"); |
| 40 setType(InputTypeNames::button); | 59 setType(InputTypeNames::button); |
| 41 | 60 |
| 42 if (is_overlay_button_) | 61 if (is_overlay_button_) |
| 43 RecordMetrics(CastOverlayMetrics::kCreated); | 62 RecordMetrics(CastOverlayMetrics::kCreated); |
| 44 SetIsPlayingRemotely(false); | |
| 45 } | |
| 46 | 63 |
| 47 void MediaControlCastButtonElement::TryShowOverlay() { | 64 remote_playback_->addEventListener(EventTypeNames::connect, |
| 48 DCHECK(is_overlay_button_); | 65 remote_playback_event_listener_); |
| 66 remote_playback_->addEventListener(EventTypeNames::connecting, | |
| 67 remote_playback_event_listener_); | |
| 68 remote_playback_->addEventListener(EventTypeNames::disconnect, | |
| 69 remote_playback_event_listener_); | |
| 70 UpdateDisplayType(); | |
| 49 | 71 |
| 50 SetIsWanted(true); | 72 // IsWanted() is true by default. For this button it must be false, so that |
| 51 if (ElementFromCenter(*this) != &MediaElement()) { | 73 // remote playback availability callback is set on the first time |
| 52 SetIsWanted(false); | 74 // SetIsWanted(true) is called. |
| 53 return; | 75 SetIsWanted(false); |
| 54 } | |
| 55 | |
| 56 DCHECK(IsWanted()); | |
| 57 if (!show_use_counted_) { | |
| 58 show_use_counted_ = true; | |
| 59 RecordMetrics(CastOverlayMetrics::kShown); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 void MediaControlCastButtonElement::SetIsPlayingRemotely( | |
| 64 bool is_playing_remotely) { | |
| 65 if (is_playing_remotely) { | |
| 66 if (is_overlay_button_) { | |
| 67 SetDisplayType(kMediaOverlayCastOnButton); | |
| 68 } else { | |
| 69 SetDisplayType(kMediaCastOnButton); | |
| 70 } | |
| 71 } else { | |
| 72 if (is_overlay_button_) { | |
| 73 SetDisplayType(kMediaOverlayCastOffButton); | |
| 74 } else { | |
| 75 SetDisplayType(kMediaCastOffButton); | |
| 76 } | |
| 77 } | |
| 78 UpdateOverflowString(); | |
| 79 } | 76 } |
| 80 | 77 |
| 81 bool MediaControlCastButtonElement::WillRespondToMouseClickEvents() { | 78 bool MediaControlCastButtonElement::WillRespondToMouseClickEvents() { |
| 82 return true; | 79 return true; |
| 83 } | 80 } |
| 84 | 81 |
| 85 WebLocalizedString::Name | 82 WebLocalizedString::Name |
| 86 MediaControlCastButtonElement::GetOverflowStringName() { | 83 MediaControlCastButtonElement::GetOverflowStringName() { |
| 87 if (MediaElement().IsPlayingRemotely()) | 84 if (IsPlayingRemotely()) |
| 88 return WebLocalizedString::kOverflowMenuStopCast; | 85 return WebLocalizedString::kOverflowMenuStopCast; |
| 89 return WebLocalizedString::kOverflowMenuCast; | 86 return WebLocalizedString::kOverflowMenuCast; |
| 90 } | 87 } |
| 91 | 88 |
| 92 bool MediaControlCastButtonElement::HasOverflowButton() { | 89 bool MediaControlCastButtonElement::HasOverflowButton() { |
| 93 return true; | 90 return true; |
| 94 } | 91 } |
| 95 | 92 |
| 96 void MediaControlCastButtonElement::DefaultEventHandler(Event* event) { | 93 void MediaControlCastButtonElement::DefaultEventHandler(Event* event) { |
| 97 if (event->type() == EventTypeNames::click) { | 94 if (event->type() == EventTypeNames::click) { |
| 98 if (is_overlay_button_) { | 95 if (is_overlay_button_) { |
| 99 Platform::Current()->RecordAction( | 96 Platform::Current()->RecordAction( |
| 100 UserMetricsAction("Media.Controls.CastOverlay")); | 97 UserMetricsAction("Media.Controls.CastOverlay")); |
| 98 | |
| 99 if (!click_use_counted_) { | |
| 100 click_use_counted_ = true; | |
| 101 RecordMetrics(CastOverlayMetrics::kClicked); | |
| 102 } | |
| 101 } else { | 103 } else { |
| 102 Platform::Current()->RecordAction( | 104 Platform::Current()->RecordAction( |
| 103 UserMetricsAction("Media.Controls.Cast")); | 105 UserMetricsAction("Media.Controls.Cast")); |
| 104 } | 106 } |
| 105 | 107 |
| 106 if (is_overlay_button_ && !click_use_counted_) { | 108 remote_playback_->PromptInternal(); |
| 107 click_use_counted_ = true; | |
| 108 RecordMetrics(CastOverlayMetrics::kClicked); | |
| 109 } | |
| 110 if (MediaElement().IsPlayingRemotely()) { | |
| 111 MediaElement().RequestRemotePlaybackControl(); | |
| 112 } else { | |
| 113 MediaElement().RequestRemotePlayback(); | |
| 114 } | |
| 115 } | 109 } |
| 116 MediaControlInputElement::DefaultEventHandler(event); | 110 MediaControlInputElement::DefaultEventHandler(event); |
| 117 } | 111 } |
| 118 | 112 |
| 113 void MediaControlCastButtonElement::UpdateDisplayType() { | |
| 114 if (IsPlayingRemotely()) { | |
| 115 if (is_overlay_button_) | |
| 116 SetDisplayType(kMediaOverlayCastOnButton); | |
| 117 else | |
| 118 SetDisplayType(kMediaCastOnButton); | |
| 119 } else { | |
| 120 if (is_overlay_button_) | |
| 121 SetDisplayType(kMediaOverlayCastOffButton); | |
|
mlamouri (slow - plz ping)
2017/04/19 16:35:36
A bit surprising: why do we have this? Worth filin
whywhat
2017/04/19 22:40:48
WDYM? That MediaCastOff and MediaOverlayCastOff ar
| |
| 122 else | |
| 123 SetDisplayType(kMediaCastOffButton); | |
| 124 } | |
| 125 UpdateOverflowString(); | |
| 126 } | |
| 127 | |
| 128 void MediaControlCastButtonElement::SetIsWanted(bool wanted) { | |
| 129 if (wanted == IsWanted()) | |
| 130 return; | |
| 131 | |
| 132 is_wanted_by_controls_ = wanted; | |
| 133 if (!wanted || !ShouldShow()) { | |
| 134 UpdateVisibility(false); | |
| 135 if (availability_callback_id_ != -1) { | |
| 136 remote_playback_->CancelWatchAvailabilityInternal( | |
| 137 availability_callback_id_); | |
| 138 availability_callback_id_ = -1; | |
| 139 } | |
| 140 return; | |
| 141 } | |
| 142 | |
| 143 if (availability_callback_id_ == -1) { | |
| 144 availability_callback_id_ = remote_playback_->WatchAvailabilityInternal( | |
| 145 new RemotePlayback::AvailabilityCallback(WTF::Bind( | |
| 146 &MediaControlCastButtonElement::OnRemotePlaybackAvailabilityChanged, | |
| 147 WrapPersistent(this)))); | |
| 148 } | |
| 149 UpdateVisibility(remote_playback_->RemotePlaybackAvailable()); | |
| 150 } | |
| 151 | |
| 152 void MediaControlCastButtonElement::UpdateVisibility(bool visible) { | |
| 153 MediaControlElement::SetIsWanted(visible); | |
| 154 if (IsWanted() && is_overlay_button_ && !show_use_counted_) { | |
| 155 show_use_counted_ = true; | |
| 156 RecordMetrics(CastOverlayMetrics::kShown); | |
| 157 } | |
| 158 } | |
| 159 | |
| 160 void MediaControlCastButtonElement::OnRemotePlaybackAvailabilityChanged() { | |
| 161 DCHECK(is_wanted_by_controls_ && ShouldShow()); | |
| 162 UpdateVisibility(remote_playback_->RemotePlaybackAvailable()); | |
| 163 } | |
| 164 | |
| 165 bool MediaControlCastButtonElement::ShouldShow() { | |
| 166 if (MediaElement().FastHasAttribute(HTMLNames::disableremoteplaybackAttr)) | |
| 167 return false; | |
| 168 | |
| 169 // Explicitly do not show cast button when the MediaControlsEnabled setting is | |
| 170 // false to make sure the overlay does not appear. | |
| 171 Document& document = MediaElement().GetDocument(); | |
| 172 if (document.GetSettings() && | |
| 173 !document.GetSettings()->GetMediaControlsEnabled()) { | |
| 174 return false; | |
| 175 } | |
| 176 | |
| 177 // The page disabled the button via the attribute. | |
| 178 if (MediaElement().ControlsListInternal()->ShouldHideRemotePlayback()) { | |
| 179 UseCounter::Count( | |
| 180 document, UseCounter::kHTMLMediaElementControlsListNoRemotePlayback); | |
| 181 return false; | |
| 182 } | |
| 183 | |
| 184 if (is_overlay_button_) { | |
| 185 // TODO(avayvod): Do we still need to hide the button for autoplay/paused | |
| 186 // videos? Check for IsAutoplayingMuted() here instead? | |
| 187 if (MediaElement().Autoplay() && MediaElement().paused()) | |
| 188 return false; | |
| 189 | |
| 190 // Don't show the overlay button if it would cover something on top of the | |
| 191 // media element. | |
| 192 // TODO(avayvod): DO NOT SUBMIT, should show the button to do the hit test? | |
| 193 if (UnderlyingElement() != &MediaElement()) | |
| 194 return false; | |
| 195 } | |
| 196 return true; | |
| 197 } | |
| 198 | |
| 119 bool MediaControlCastButtonElement::KeepEventInNode(Event* event) { | 199 bool MediaControlCastButtonElement::KeepEventInNode(Event* event) { |
| 120 return MediaControlElementsHelper::IsUserInteractionEvent(event); | 200 return MediaControlElementsHelper::IsUserInteractionEvent(event); |
| 121 } | 201 } |
| 122 | 202 |
| 123 void MediaControlCastButtonElement::RecordMetrics(CastOverlayMetrics metric) { | 203 void MediaControlCastButtonElement::RecordMetrics(CastOverlayMetrics metric) { |
| 124 DCHECK(is_overlay_button_); | 204 DCHECK(is_overlay_button_); |
| 125 DEFINE_STATIC_LOCAL( | 205 DEFINE_STATIC_LOCAL( |
| 126 EnumerationHistogram, overlay_histogram, | 206 EnumerationHistogram, overlay_histogram, |
| 127 ("Cast.Sender.Overlay", static_cast<int>(CastOverlayMetrics::kCount))); | 207 ("Cast.Sender.Overlay", static_cast<int>(CastOverlayMetrics::kCount))); |
| 128 overlay_histogram.Count(static_cast<int>(metric)); | 208 overlay_histogram.Count(static_cast<int>(metric)); |
| 129 } | 209 } |
| 130 | 210 |
| 211 bool MediaControlCastButtonElement::IsPlayingRemotely() const { | |
| 212 return remote_playback_->GetState() != WebRemotePlaybackState::kDisconnected; | |
| 213 } | |
| 214 | |
| 215 Element* MediaControlCastButtonElement::UnderlyingElement() { | |
| 216 DCHECK(is_overlay_button_); | |
| 217 MediaControlElement::SetIsWanted(true); | |
| 218 ClientRect* client_rect = getBoundingClientRect(); | |
| 219 MediaControlElement::SetIsWanted(false); | |
| 220 int center_x = | |
| 221 static_cast<int>((client_rect->left() + client_rect->right()) / 2); | |
| 222 int center_y = | |
| 223 static_cast<int>((client_rect->top() + client_rect->bottom()) / 2); | |
| 224 | |
| 225 return GetDocument().ElementFromPoint(center_x, center_y); | |
| 226 } | |
| 227 | |
| 228 DEFINE_TRACE(MediaControlCastButtonElement) { | |
| 229 MediaControlInputElement::Trace(visitor); | |
| 230 visitor->Trace(remote_playback_); | |
| 231 visitor->Trace(remote_playback_event_listener_); | |
| 232 } | |
| 233 | |
| 131 } // namespace blink | 234 } // namespace blink |
| OLD | NEW |