Index: third_party/WebKit/Source/modules/media_controls/elements/MediaControlCastButtonElement.cpp |
diff --git a/third_party/WebKit/Source/modules/media_controls/elements/MediaControlCastButtonElement.cpp b/third_party/WebKit/Source/modules/media_controls/elements/MediaControlCastButtonElement.cpp |
index 448fc43425c2810f310f59b399cc401f15187731..b2409809c7584c9666cbb3a28bdc0aba719c3b32 100644 |
--- a/third_party/WebKit/Source/modules/media_controls/elements/MediaControlCastButtonElement.cpp |
+++ b/third_party/WebKit/Source/modules/media_controls/elements/MediaControlCastButtonElement.cpp |
@@ -4,31 +4,24 @@ |
#include "modules/media_controls/elements/MediaControlCastButtonElement.h" |
+#include "bindings/core/v8/ScriptState.h" |
+#include "bindings/core/v8/ToV8.h" |
#include "core/InputTypeNames.h" |
#include "core/dom/ClientRect.h" |
#include "core/events/Event.h" |
+#include "core/frame/Settings.h" |
#include "core/html/HTMLMediaElement.h" |
+#include "core/html/media/HTMLMediaElementControlsList.h" |
#include "modules/media_controls/MediaControlsImpl.h" |
#include "modules/media_controls/elements/MediaControlElementsHelper.h" |
+#include "modules/remoteplayback/HTMLMediaElementRemotePlayback.h" |
+#include "modules/remoteplayback/RemotePlayback.h" |
#include "platform/Histogram.h" |
+#include "platform/wtf/Functional.h" |
#include "public/platform/Platform.h" |
namespace blink { |
-namespace { |
- |
-Element* ElementFromCenter(Element& element) { |
- ClientRect* client_rect = element.getBoundingClientRect(); |
- int center_x = |
- static_cast<int>((client_rect->left() + client_rect->right()) / 2); |
- int center_y = |
- static_cast<int>((client_rect->top() + client_rect->bottom()) / 2); |
- |
- return element.GetDocument().ElementFromPoint(center_x, center_y); |
-} |
- |
-} // anonymous namespace |
- |
MediaControlCastButtonElement::MediaControlCastButtonElement( |
MediaControlsImpl& media_controls, |
bool is_overlay_button) |
@@ -42,41 +35,8 @@ MediaControlCastButtonElement::MediaControlCastButtonElement( |
if (is_overlay_button_) |
RecordMetrics(CastOverlayMetrics::kCreated); |
- SetIsPlayingRemotely(false); |
-} |
-void MediaControlCastButtonElement::TryShowOverlay() { |
- DCHECK(is_overlay_button_); |
- |
- SetIsWanted(true); |
- if (ElementFromCenter(*this) != &MediaElement()) { |
- SetIsWanted(false); |
- return; |
- } |
- |
- DCHECK(IsWanted()); |
- if (!show_use_counted_) { |
- show_use_counted_ = true; |
- RecordMetrics(CastOverlayMetrics::kShown); |
- } |
-} |
- |
-void MediaControlCastButtonElement::SetIsPlayingRemotely( |
- bool is_playing_remotely) { |
- if (is_playing_remotely) { |
- if (is_overlay_button_) { |
- SetDisplayType(kMediaOverlayCastOnButton); |
- } else { |
- SetDisplayType(kMediaCastOnButton); |
- } |
- } else { |
- if (is_overlay_button_) { |
- SetDisplayType(kMediaOverlayCastOffButton); |
- } else { |
- SetDisplayType(kMediaCastOffButton); |
- } |
- } |
- UpdateOverflowString(); |
+ UpdateDisplayType(); |
} |
bool MediaControlCastButtonElement::WillRespondToMouseClickEvents() { |
@@ -85,7 +45,7 @@ bool MediaControlCastButtonElement::WillRespondToMouseClickEvents() { |
WebLocalizedString::Name |
MediaControlCastButtonElement::GetOverflowStringName() { |
- if (MediaElement().IsPlayingRemotely()) |
+ if (IsPlayingRemotely()) |
return WebLocalizedString::kOverflowMenuStopCast; |
return WebLocalizedString::kOverflowMenuCast; |
} |
@@ -99,24 +59,89 @@ void MediaControlCastButtonElement::DefaultEventHandler(Event* event) { |
if (is_overlay_button_) { |
Platform::Current()->RecordAction( |
UserMetricsAction("Media.Controls.CastOverlay")); |
+ |
+ if (!click_use_counted_) { |
+ click_use_counted_ = true; |
+ RecordMetrics(CastOverlayMetrics::kClicked); |
+ } |
} else { |
Platform::Current()->RecordAction( |
UserMetricsAction("Media.Controls.Cast")); |
} |
- if (is_overlay_button_ && !click_use_counted_) { |
- click_use_counted_ = true; |
- RecordMetrics(CastOverlayMetrics::kClicked); |
- } |
- if (MediaElement().IsPlayingRemotely()) { |
- MediaElement().RequestRemotePlaybackControl(); |
- } else { |
- MediaElement().RequestRemotePlayback(); |
- } |
+ RemotePlayback* remote = |
+ HTMLMediaElementRemotePlayback::remote(MediaElement()); |
+ if (remote) |
+ remote->PromptInternal(); |
} |
MediaControlInputElement::DefaultEventHandler(event); |
} |
+void MediaControlCastButtonElement::UpdateDisplayType() { |
+ if (IsPlayingRemotely()) { |
+ if (is_overlay_button_) |
+ SetDisplayType(kMediaOverlayCastOnButton); |
+ else |
+ SetDisplayType(kMediaCastOnButton); |
+ } else { |
+ if (is_overlay_button_) |
+ SetDisplayType(kMediaOverlayCastOffButton); |
+ else |
+ SetDisplayType(kMediaCastOffButton); |
+ } |
+ UpdateOverflowString(); |
+} |
+ |
+void MediaControlCastButtonElement::SetIsWanted(bool wanted) { |
+ if (wanted == IsWanted()) |
+ return; |
+ |
+ MediaControlElement::SetIsWanted(wanted); |
+ if (IsWanted() && is_overlay_button_ && !show_use_counted_) { |
+ show_use_counted_ = true; |
+ RecordMetrics(CastOverlayMetrics::kShown); |
+ } |
+} |
+ |
+bool MediaControlCastButtonElement::ShouldShow() { |
+ if (MediaElement().FastHasAttribute(HTMLNames::disableremoteplaybackAttr)) |
+ return false; |
+ |
+ // Explicitly do not show cast button when the MediaControlsEnabled setting is |
+ // false to make sure the overlay does not appear. |
+ Document& document = MediaElement().GetDocument(); |
+ if (document.GetSettings() && |
+ !document.GetSettings()->GetMediaControlsEnabled()) { |
+ return false; |
+ } |
+ |
+ // The page disabled the button via the attribute. |
+ if (MediaElement().ControlsListInternal()->ShouldHideRemotePlayback()) { |
+ UseCounter::Count( |
+ document, UseCounter::kHTMLMediaElementControlsListNoRemotePlayback); |
+ return false; |
+ } |
+ |
+ RemotePlayback* remote = |
+ HTMLMediaElementRemotePlayback::remote(MediaElement()); |
+ if (!remote || !remote->RemotePlaybackAvailable()) |
+ return false; |
+ |
+ if (is_overlay_button_) { |
+ // TODO(avayvod): Do we still need to hide the button for autoplay/paused |
+ // videos? Check for IsAutoplayingMuted() here instead? |
+ if (MediaElement().Autoplay() && MediaElement().paused()) |
+ return false; |
+ |
+ // Don't show the overlay button if it would cover something on top of the |
+ // media element. |
+ if (UnderlyingElement() != &MediaElement()) |
+ return false; |
+ } |
+ |
+ return true; |
+} |
+ |
bool MediaControlCastButtonElement::KeepEventInNode(Event* event) { |
return MediaControlElementsHelper::IsUserInteractionEvent(event); |
} |
@@ -129,4 +154,23 @@ void MediaControlCastButtonElement::RecordMetrics(CastOverlayMetrics metric) { |
overlay_histogram.Count(static_cast<int>(metric)); |
} |
+bool MediaControlCastButtonElement::IsPlayingRemotely() const { |
+ RemotePlayback* remote = |
+ HTMLMediaElementRemotePlayback::remote(MediaElement()); |
+ return remote && remote->GetState() != WebRemotePlaybackState::kDisconnected; |
+} |
+ |
+Element* MediaControlCastButtonElement::UnderlyingElement() { |
+ DCHECK(is_overlay_button_); |
+ MediaControlElement::SetIsWanted(true); |
+ ClientRect* client_rect = getBoundingClientRect(); |
+ MediaControlElement::SetIsWanted(false); |
+ int center_x = |
+ static_cast<int>((client_rect->left() + client_rect->right()) / 2); |
+ int center_y = |
+ static_cast<int>((client_rect->top() + client_rect->bottom()) / 2); |
+ |
+ return GetDocument().ElementFromPoint(center_x, center_y); |
+} |
+ |
} // namespace blink |