Chromium Code Reviews| 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/MediaControlTimelineElement.h" | |
| 6 | |
| 7 #include "core/HTMLNames.h" | |
| 8 #include "core/InputTypeNames.h" | |
| 9 #include "core/events/Event.h" | |
| 10 #include "core/events/KeyboardEvent.h" | |
| 11 #include "core/events/MouseEvent.h" | |
| 12 #include "core/events/PointerEvent.h" | |
| 13 #include "core/html/HTMLMediaElement.h" | |
| 14 #include "core/html/TimeRanges.h" | |
| 15 #include "core/html/shadow/ShadowElementNames.h" | |
| 16 #include "core/layout/LayoutBoxModelObject.h" | |
| 17 #include "core/page/ChromeClient.h" | |
| 18 #include "modules/media_controls/MediaControlsImpl.h" | |
| 19 #include "modules/media_controls/elements/MediaControlElementsHelper.h" | |
| 20 #include "public/platform/Platform.h" | |
| 21 #include "public/platform/WebScreenInfo.h" | |
| 22 | |
| 23 namespace blink { | |
| 24 | |
| 25 MediaControlTimelineElement::MediaControlTimelineElement( | |
| 26 MediaControlsImpl& media_controls) | |
| 27 : MediaControlInputElement(media_controls, kMediaSlider) { | |
| 28 EnsureUserAgentShadowRoot(); | |
| 29 setType(InputTypeNames::range); | |
| 30 setAttribute(HTMLNames::stepAttr, "any"); | |
| 31 SetShadowPseudoId(AtomicString("-webkit-media-controls-timeline")); | |
| 32 } | |
| 33 | |
| 34 bool MediaControlTimelineElement::WillRespondToMouseClickEvents() { | |
| 35 return isConnected() && GetDocument().IsActive(); | |
| 36 } | |
| 37 | |
| 38 void MediaControlTimelineElement::SetPosition(double current_time) { | |
| 39 setValue(String::Number(current_time)); | |
| 40 | |
| 41 if (LayoutObject* layout_object = this->GetLayoutObject()) | |
| 42 layout_object->SetShouldDoFullPaintInvalidation(); | |
| 43 } | |
| 44 | |
| 45 void MediaControlTimelineElement::SetDuration(double duration) { | |
| 46 SetFloatingPointAttribute(HTMLNames::maxAttr, | |
| 47 std::isfinite(duration) ? duration : 0); | |
| 48 | |
| 49 if (LayoutObject* layout_object = this->GetLayoutObject()) | |
| 50 layout_object->SetShouldDoFullPaintInvalidation(); | |
| 51 } | |
| 52 | |
| 53 void MediaControlTimelineElement::OnPlaying() { | |
| 54 Frame* frame = GetDocument().GetFrame(); | |
| 55 if (!frame) | |
| 56 return; | |
| 57 metrics_.RecordPlaying( | |
| 58 frame->GetChromeClient().GetScreenInfo().orientation_type, | |
| 59 MediaElement().IsFullscreen(), TimelineWidth()); | |
| 60 } | |
| 61 | |
| 62 void MediaControlTimelineElement::DefaultEventHandler(Event* event) { | |
|
Zhiqiang Zhang (Slow)
2017/04/20 10:56:34
I know this method is old code, but it seems too l
mlamouri (slow - plz ping)
2017/04/20 14:17:20
I agree that this could be improved. Hopefully, we
| |
| 63 if (event->IsMouseEvent() && | |
| 64 ToMouseEvent(event)->button() != | |
| 65 static_cast<short>(WebPointerProperties::Button::kLeft)) | |
| 66 return; | |
| 67 | |
| 68 if (!isConnected() || !GetDocument().IsActive()) | |
| 69 return; | |
| 70 | |
| 71 // TODO(crbug.com/706504): These should listen for pointerdown/up. | |
| 72 if (event->type() == EventTypeNames::mousedown) | |
| 73 static_cast<MediaControlsImpl&>(GetMediaControls()).BeginScrubbing(); | |
| 74 if (event->type() == EventTypeNames::mouseup) | |
| 75 static_cast<MediaControlsImpl&>(GetMediaControls()).EndScrubbing(); | |
| 76 | |
| 77 // Only respond to main button of primary pointer(s). | |
| 78 if (event->IsPointerEvent() && ToPointerEvent(event)->isPrimary() && | |
| 79 ToPointerEvent(event)->button() == | |
| 80 static_cast<short>(WebPointerProperties::Button::kLeft)) { | |
| 81 if (event->type() == EventTypeNames::pointerdown) { | |
| 82 Platform::Current()->RecordAction( | |
| 83 UserMetricsAction("Media.Controls.ScrubbingBegin")); | |
| 84 static_cast<MediaControlsImpl&>(GetMediaControls()).BeginScrubbing(); | |
| 85 Element* thumb = UserAgentShadowRoot()->GetElementById( | |
| 86 ShadowElementNames::SliderThumb()); | |
| 87 bool started_from_thumb = thumb && thumb == event->target()->ToNode(); | |
| 88 metrics_.StartGesture(started_from_thumb); | |
| 89 } | |
| 90 if (event->type() == EventTypeNames::pointerup) { | |
| 91 Platform::Current()->RecordAction( | |
| 92 UserMetricsAction("Media.Controls.ScrubbingEnd")); | |
| 93 static_cast<MediaControlsImpl&>(GetMediaControls()).EndScrubbing(); | |
| 94 metrics_.RecordEndGesture(TimelineWidth(), MediaElement().duration()); | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 if (event->type() == EventTypeNames::keydown) { | |
| 99 metrics_.StartKey(); | |
| 100 } | |
| 101 if (event->type() == EventTypeNames::keyup && event->IsKeyboardEvent()) { | |
| 102 metrics_.RecordEndKey(TimelineWidth(), ToKeyboardEvent(event)->keyCode()); | |
| 103 } | |
| 104 | |
| 105 MediaControlInputElement::DefaultEventHandler(event); | |
| 106 | |
| 107 if (event->type() != EventTypeNames::input) | |
| 108 return; | |
| 109 | |
| 110 double time = value().ToDouble(); | |
| 111 | |
| 112 double duration = MediaElement().duration(); | |
| 113 // Workaround for floating point error - it's possible for this element's max | |
| 114 // attribute to be rounded to a value slightly higher than the duration. If | |
| 115 // this happens and scrubber is dragged near the max, seek to duration. | |
| 116 if (time > duration) | |
| 117 time = duration; | |
| 118 | |
| 119 metrics_.OnInput(MediaElement().currentTime(), time); | |
| 120 | |
| 121 // FIXME: This will need to take the timeline offset into consideration | |
| 122 // once that concept is supported, see https://crbug.com/312699 | |
| 123 if (MediaElement().seekable()->Contain(time)) | |
| 124 MediaElement().setCurrentTime(time); | |
| 125 | |
| 126 // Provide immediate feedback (without waiting for media to seek) to make it | |
| 127 // easier for user to seek to a precise time. | |
| 128 static_cast<MediaControlsImpl&>(GetMediaControls()) | |
| 129 .UpdateCurrentTimeDisplay(); | |
| 130 } | |
| 131 | |
| 132 bool MediaControlTimelineElement::KeepEventInNode(Event* event) { | |
| 133 return MediaControlElementsHelper::IsUserInteractionEventForSlider( | |
| 134 event, GetLayoutObject()); | |
| 135 } | |
| 136 | |
| 137 int MediaControlTimelineElement::TimelineWidth() { | |
| 138 if (LayoutBoxModelObject* box = GetLayoutBoxModelObject()) | |
| 139 return box->OffsetWidth().Round(); | |
| 140 return 0; | |
| 141 } | |
| 142 | |
| 143 } // namespace blink | |
| OLD | NEW |