Chromium Code Reviews| Index: third_party/WebKit/Source/modules/media_controls/MediaDownloadInProductHelpManager.cpp |
| diff --git a/third_party/WebKit/Source/modules/media_controls/MediaDownloadInProductHelpManager.cpp b/third_party/WebKit/Source/modules/media_controls/MediaDownloadInProductHelpManager.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9b80e4b371d66a1c1fb4b7028d0f4da718ff28b0 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/modules/media_controls/MediaDownloadInProductHelpManager.cpp |
| @@ -0,0 +1,117 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "modules/media_controls/MediaDownloadInProductHelpManager.h" |
| + |
| +#include "core/frame/LocalFrameClient.h" |
| +#include "modules/media_controls/MediaControlsImpl.h" |
| +#include "modules/media_controls/elements/MediaControlDownloadButtonElement.h" |
| +#include "services/service_manager/public/cpp/interface_provider.h" |
| + |
| +namespace blink { |
| + |
| +MediaDownloadInProductHelpManager::MediaDownloadInProductHelpManager( |
| + MediaControlsImpl& controls) |
| + : controls_(controls) {} |
| + |
| +MediaDownloadInProductHelpManager::~MediaDownloadInProductHelpManager() = |
| + default; |
| + |
| +void MediaDownloadInProductHelpManager::SetControlsState(bool can_show) { |
| + if (controls_can_show_ == can_show) |
| + return; |
| + |
| + controls_can_show_ = can_show; |
| + StateUpdated(); |
| +} |
| + |
| +void MediaDownloadInProductHelpManager::SetDownloadButtonState(bool can_show) { |
| + if (button_can_show_ == can_show) |
| + return; |
| + |
| + button_can_show_ = can_show; |
| + StateUpdated(); |
| +} |
| + |
| +void MediaDownloadInProductHelpManager::SetPlayState(bool is_playing) { |
| + if (is_playing_ == is_playing) |
| + return; |
| + |
| + is_playing_ = is_playing; |
| + StateUpdated(); |
| +} |
| + |
| +bool MediaDownloadInProductHelpManager::IsShowingInProductHelp() const { |
| + return !!media_in_product_help_; |
| +} |
| + |
| +void MediaDownloadInProductHelpManager:: |
| + MaybeDispatchDownloadInProductHelpTrigger() { |
| + // Only show in-product-help once for an element. |
| + if (media_download_in_product_trigger_observed_) |
| + return; |
| + |
| + auto* frame = controls_->GetDocument().GetFrame(); |
| + if (!frame) |
| + return; |
| + |
| + // If the button is not in the viewport, don't show the in-product-help. |
| + IntRect button_rect = |
| + controls_->DownloadButton().VisibleBoundsInVisualViewport(); |
| + if (button_rect.IsEmpty()) |
|
mlamouri (slow - plz ping)
2017/08/16 13:09:30
What if it is partially hidden?
Khushal
2017/08/16 19:01:22
That's a good point. I could look at button size a
|
| + return; |
| + |
| + media_download_in_product_trigger_observed_ = true; |
| + media_in_product_help_ = |
| + WTF::MakeUnique<mojom::blink::MediaDownloadInProductHelpPtr>(); |
| + frame->Client()->GetInterfaceProvider()->GetInterface( |
| + mojo::MakeRequest(media_in_product_help_.get())); |
| + media_in_product_help_->set_connection_error_handler(ConvertToBaseCallback( |
| + WTF::Bind(&MediaDownloadInProductHelpManager::DismissInProductHelp, |
| + WrapWeakPersistent(this)))); |
| + |
| + // MaybeShow should always make the controls visible since we early out if |
| + // CanShow is false for the controls. |
| + controls_->MaybeShow(); |
| + (*media_in_product_help_)->ShowInProductHelpWidget(button_rect); |
| +} |
| + |
| +void MediaDownloadInProductHelpManager::StateUpdated() { |
| + if (CanShowInProductHelp()) |
| + MaybeDispatchDownloadInProductHelpTrigger(); |
| + else |
| + DismissInProductHelp(); |
| +} |
| + |
| +bool MediaDownloadInProductHelpManager::CanShowInProductHelp() const { |
| + // If showing the controls is disabled, we can't make them visible to show the |
| + // in-product-help. |
| + if (!controls_can_show_) |
| + return false; |
| + |
| + // If the download option is not available for this video, no need to show |
| + // anything. |
| + if (!button_can_show_) |
| + return false; |
| + |
| + // Restrict showing in-product-help to when the video is playing. |
| + if (!is_playing_) |
| + return false; |
|
mlamouri (slow - plz ping)
2017/08/16 13:09:30
You might want to merge this into one `return cont
Khushal
2017/08/16 19:01:22
Done.
|
| + |
| + return true; |
| +} |
| + |
| +void MediaDownloadInProductHelpManager::DismissInProductHelp() { |
| + if (!media_in_product_help_) |
| + return; |
| + |
| + media_in_product_help_.reset(); |
| + controls_->DidDismissDownloadInProductHelp(); |
| +} |
| + |
| +DEFINE_TRACE(MediaDownloadInProductHelpManager) { |
| + visitor->Trace(controls_); |
| +} |
| + |
| +} // namespace blink |