| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "modules/media_controls/elements/MediaControlDownloadButtonElement.h" |
| 6 |
| 7 #include "core/InputTypeNames.h" |
| 8 #include "core/events/Event.h" |
| 9 #include "core/frame/Settings.h" |
| 10 #include "core/html/HTMLAnchorElement.h" |
| 11 #include "core/html/HTMLMediaElement.h" |
| 12 #include "core/html/media/HTMLMediaElementControlsList.h" |
| 13 #include "core/html/media/HTMLMediaSource.h" |
| 14 #include "core/page/Page.h" |
| 15 #include "modules/media_controls/MediaControlsImpl.h" |
| 16 #include "public/platform/Platform.h" |
| 17 |
| 18 namespace blink { |
| 19 |
| 20 MediaControlDownloadButtonElement::MediaControlDownloadButtonElement( |
| 21 MediaControlsImpl& media_controls) |
| 22 : MediaControlInputElement(media_controls, kMediaDownloadButton) { |
| 23 EnsureUserAgentShadowRoot(); |
| 24 setType(InputTypeNames::button); |
| 25 SetShadowPseudoId(AtomicString("-internal-media-controls-download-button")); |
| 26 SetIsWanted(false); |
| 27 } |
| 28 |
| 29 bool MediaControlDownloadButtonElement::ShouldDisplayDownloadButton() { |
| 30 const KURL& url = MediaElement().currentSrc(); |
| 31 |
| 32 // Check page settings to see if download is disabled. |
| 33 if (GetDocument().GetPage() && |
| 34 GetDocument().GetPage()->GetSettings().GetHideDownloadUI()) |
| 35 return false; |
| 36 |
| 37 // URLs that lead to nowhere are ignored. |
| 38 if (url.IsNull() || url.IsEmpty()) |
| 39 return false; |
| 40 |
| 41 // If we have no source, we can't download. |
| 42 if (MediaElement().getNetworkState() == HTMLMediaElement::kNetworkEmpty || |
| 43 MediaElement().getNetworkState() == HTMLMediaElement::kNetworkNoSource) { |
| 44 return false; |
| 45 } |
| 46 |
| 47 // Local files and blobs (including MSE) should not have a download button. |
| 48 if (url.IsLocalFile() || url.ProtocolIs("blob")) |
| 49 return false; |
| 50 |
| 51 // MediaStream can't be downloaded. |
| 52 if (HTMLMediaElement::IsMediaStreamURL(url.GetString())) |
| 53 return false; |
| 54 |
| 55 // MediaSource can't be downloaded. |
| 56 if (HTMLMediaSource::Lookup(url)) |
| 57 return false; |
| 58 |
| 59 // HLS stream shouldn't have a download button. |
| 60 if (HTMLMediaElement::IsHLSURL(url)) |
| 61 return false; |
| 62 |
| 63 // Infinite streams don't have a clear end at which to finish the download |
| 64 // (would require adding UI to prompt for the duration to download). |
| 65 if (MediaElement().duration() == std::numeric_limits<double>::infinity()) |
| 66 return false; |
| 67 |
| 68 // The attribute disables the download button. |
| 69 if (MediaElement().ControlsListInternal()->ShouldHideDownload()) { |
| 70 UseCounter::Count(MediaElement().GetDocument(), |
| 71 UseCounter::kHTMLMediaElementControlsListNoDownload); |
| 72 return false; |
| 73 } |
| 74 |
| 75 return true; |
| 76 } |
| 77 |
| 78 WebLocalizedString::Name |
| 79 MediaControlDownloadButtonElement::GetOverflowStringName() { |
| 80 return WebLocalizedString::kOverflowMenuDownload; |
| 81 } |
| 82 |
| 83 bool MediaControlDownloadButtonElement::HasOverflowButton() { |
| 84 return true; |
| 85 } |
| 86 |
| 87 void MediaControlDownloadButtonElement::SetIsWanted(bool wanted) { |
| 88 MediaControlElement::SetIsWanted(wanted); |
| 89 |
| 90 if (!IsWanted()) |
| 91 return; |
| 92 |
| 93 DCHECK(IsWanted()); |
| 94 if (!show_use_counted_) { |
| 95 show_use_counted_ = true; |
| 96 RecordMetrics(DownloadActionMetrics::kShown); |
| 97 } |
| 98 } |
| 99 |
| 100 DEFINE_TRACE(MediaControlDownloadButtonElement) { |
| 101 visitor->Trace(anchor_); |
| 102 MediaControlInputElement::Trace(visitor); |
| 103 } |
| 104 |
| 105 void MediaControlDownloadButtonElement::DefaultEventHandler(Event* event) { |
| 106 const KURL& url = MediaElement().currentSrc(); |
| 107 if (event->type() == EventTypeNames::click && |
| 108 !(url.IsNull() || url.IsEmpty())) { |
| 109 Platform::Current()->RecordAction( |
| 110 UserMetricsAction("Media.Controls.Download")); |
| 111 if (!click_use_counted_) { |
| 112 click_use_counted_ = true; |
| 113 RecordMetrics(DownloadActionMetrics::kClicked); |
| 114 } |
| 115 if (!anchor_) { |
| 116 HTMLAnchorElement* anchor = HTMLAnchorElement::Create(GetDocument()); |
| 117 anchor->setAttribute(HTMLNames::downloadAttr, ""); |
| 118 anchor_ = anchor; |
| 119 } |
| 120 anchor_->SetURL(url); |
| 121 anchor_->DispatchSimulatedClick(event); |
| 122 } |
| 123 MediaControlInputElement::DefaultEventHandler(event); |
| 124 } |
| 125 |
| 126 void MediaControlDownloadButtonElement::RecordMetrics( |
| 127 DownloadActionMetrics metric) { |
| 128 DEFINE_STATIC_LOCAL(EnumerationHistogram, download_action_histogram, |
| 129 ("Media.Controls.Download", |
| 130 static_cast<int>(DownloadActionMetrics::kCount))); |
| 131 download_action_histogram.Count(static_cast<int>(metric)); |
| 132 } |
| 133 |
| 134 } // namespace blink |
| OLD | NEW |