| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/remoteplayback/RemotePlayback.h" | 5 #include "modules/remoteplayback/RemotePlayback.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ScriptPromiseResolver.h" | 7 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 8 #include "core/dom/Document.h" | 8 #include "core/dom/Document.h" |
| 9 #include "core/events/Event.h" | 9 #include "core/events/Event.h" |
| 10 #include "core/html/HTMLMediaElement.h" | 10 #include "core/html/HTMLMediaElement.h" |
| 11 #include "modules/EventTargetModules.h" | 11 #include "modules/EventTargetModules.h" |
| 12 #include "modules/remoteplayback/RemotePlaybackAvailability.h" | 12 #include "modules/remoteplayback/RemotePlaybackAvailability.h" |
| 13 #include "platform/UserGestureIndicator.h" |
| 13 | 14 |
| 14 namespace blink { | 15 namespace blink { |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 const AtomicString& remotePlaybackStateToString(WebRemotePlaybackState state) | 19 const AtomicString& remotePlaybackStateToString(WebRemotePlaybackState state) |
| 19 { | 20 { |
| 20 DEFINE_STATIC_LOCAL(const AtomicString, connectedValue, ("connected", Atomic
String::ConstructFromLiteral)); | 21 DEFINE_STATIC_LOCAL(const AtomicString, connectedValue, ("connected", Atomic
String::ConstructFromLiteral)); |
| 21 DEFINE_STATIC_LOCAL(const AtomicString, disconnectedValue, ("disconnected",
AtomicString::ConstructFromLiteral)); | 22 DEFINE_STATIC_LOCAL(const AtomicString, disconnectedValue, ("disconnected",
AtomicString::ConstructFromLiteral)); |
| 22 | 23 |
| 23 switch (state) { | 24 switch (state) { |
| 24 case WebRemotePlaybackState::Connected: | 25 case WebRemotePlaybackState::Connected: |
| 25 return connectedValue; | 26 return connectedValue; |
| 26 case WebRemotePlaybackState::Disconnected: | 27 case WebRemotePlaybackState::Disconnected: |
| 27 return disconnectedValue; | 28 return disconnectedValue; |
| 28 } | 29 } |
| 29 | 30 |
| 30 ASSERT_NOT_REACHED(); | 31 ASSERT_NOT_REACHED(); |
| 31 return disconnectedValue; | 32 return disconnectedValue; |
| 32 } | 33 } |
| 33 | 34 |
| 34 } // anonymous namespace | 35 } // anonymous namespace |
| 35 | 36 |
| 36 // static | 37 // static |
| 37 RemotePlayback* RemotePlayback::create(HTMLMediaElement& element) | 38 RemotePlayback* RemotePlayback::create(HTMLMediaElement& element) |
| 38 { | 39 { |
| 39 ASSERT(element.document().frame()); | 40 ASSERT(element.document().frame()); |
| 40 | 41 |
| 41 RemotePlayback* remotePlayback = new RemotePlayback( | 42 RemotePlayback* remotePlayback = new RemotePlayback(element); |
| 42 element.document().frame(), | |
| 43 element.isPlayingRemotely() ? WebRemotePlaybackState::Connected : WebRem
otePlaybackState::Disconnected, | |
| 44 element.hasRemoteRoutes()); | |
| 45 element.setRemotePlaybackClient(remotePlayback); | 43 element.setRemotePlaybackClient(remotePlayback); |
| 46 | 44 |
| 47 return remotePlayback; | 45 return remotePlayback; |
| 48 } | 46 } |
| 49 | 47 |
| 50 RemotePlayback::RemotePlayback(LocalFrame* frame, WebRemotePlaybackState state,
bool availability) | 48 RemotePlayback::RemotePlayback(HTMLMediaElement& element) |
| 51 : DOMWindowProperty(frame) | 49 : DOMWindowProperty(element.document().frame()) |
| 52 , m_state(state) | 50 , m_state(element.isPlayingRemotely() ? WebRemotePlaybackState::Connected :
WebRemotePlaybackState::Disconnected) |
| 53 , m_availability(availability) | 51 , m_availability(element.hasRemoteRoutes()) |
| 52 , m_mediaElement(&element) |
| 54 { | 53 { |
| 55 } | 54 } |
| 56 | 55 |
| 57 const AtomicString& RemotePlayback::interfaceName() const | 56 const AtomicString& RemotePlayback::interfaceName() const |
| 58 { | 57 { |
| 59 return EventTargetNames::RemotePlayback; | 58 return EventTargetNames::RemotePlayback; |
| 60 } | 59 } |
| 61 | 60 |
| 62 ExecutionContext* RemotePlayback::getExecutionContext() const | 61 ExecutionContext* RemotePlayback::getExecutionContext() const |
| 63 { | 62 { |
| 64 if (!m_frame) | 63 if (!m_frame) |
| 65 return 0; | 64 return 0; |
| 66 return m_frame->document(); | 65 return m_frame->document(); |
| 67 } | 66 } |
| 68 | 67 |
| 69 ScriptPromise RemotePlayback::getAvailability(ScriptState* scriptState) | 68 ScriptPromise RemotePlayback::getAvailability(ScriptState* scriptState) |
| 70 { | 69 { |
| 71 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 70 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 72 ScriptPromise promise = resolver->promise(); | 71 ScriptPromise promise = resolver->promise(); |
| 73 | 72 |
| 74 RemotePlaybackAvailability* availability = RemotePlaybackAvailability::take(
resolver, m_availability); | 73 RemotePlaybackAvailability* availability = RemotePlaybackAvailability::take(
resolver, m_availability); |
| 75 m_availabilityObjects.append(availability); | 74 m_availabilityObjects.append(availability); |
| 76 resolver->resolve(availability); | 75 resolver->resolve(availability); |
| 77 return promise; | 76 return promise; |
| 78 } | 77 } |
| 79 | 78 |
| 79 ScriptPromise RemotePlayback::connect(ScriptState* scriptState) |
| 80 { |
| 81 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 82 ScriptPromise promise = resolver->promise(); |
| 83 |
| 84 // TODO(avayvod): should we have a flag or reuse the one for the Presentatio
nRequest::start(). |
| 85 if (!UserGestureIndicator::processingUserGesture()) { |
| 86 resolver->reject(DOMException::create(InvalidAccessError, "RemotePlaybac
k::connect() requires user gesture.")); |
| 87 return promise; |
| 88 } |
| 89 |
| 90 if (!m_mediaElement) { |
| 91 resolver->reject(DOMException::create(InvalidStateError, "The RemotePlay
back is no longer associated with a media element.")); |
| 92 return promise; |
| 93 } |
| 94 |
| 95 m_connectPromiseResolvers.append(resolver); |
| 96 if (m_state == WebRemotePlaybackState::Disconnected) |
| 97 m_mediaElement->requestRemotePlayback(); |
| 98 else |
| 99 m_mediaElement->requestRemotePlaybackControl(); |
| 100 |
| 101 return promise; |
| 102 } |
| 103 |
| 80 String RemotePlayback::state() const | 104 String RemotePlayback::state() const |
| 81 { | 105 { |
| 82 return remotePlaybackStateToString(m_state); | 106 return remotePlaybackStateToString(m_state); |
| 83 } | 107 } |
| 84 | 108 |
| 85 void RemotePlayback::stateChanged(WebRemotePlaybackState state) | 109 void RemotePlayback::stateChanged(WebRemotePlaybackState state) |
| 86 { | 110 { |
| 87 if (m_state == state) | 111 if (m_state == state) |
| 88 return; | 112 return; |
| 89 | 113 |
| 90 m_state = state; | 114 m_state = state; |
| 91 dispatchEvent(Event::create(EventTypeNames::statechange)); | 115 dispatchEvent(Event::create(EventTypeNames::statechange)); |
| 92 } | 116 } |
| 93 | 117 |
| 94 void RemotePlayback::availabilityChanged(bool available) | 118 void RemotePlayback::availabilityChanged(bool available) |
| 95 { | 119 { |
| 96 if (m_availability == available) | 120 if (m_availability == available) |
| 97 return; | 121 return; |
| 98 | 122 |
| 99 m_availability = available; | 123 m_availability = available; |
| 100 for (auto& availabilityObject : m_availabilityObjects) | 124 for (auto& availabilityObject : m_availabilityObjects) |
| 101 availabilityObject->availabilityChanged(available); | 125 availabilityObject->availabilityChanged(available); |
| 102 } | 126 } |
| 103 | 127 |
| 128 void RemotePlayback::connectionResultReceived(bool success) |
| 129 { |
| 130 for (auto& resolver : m_connectPromiseResolvers) |
| 131 resolver->resolve(success); |
| 132 m_connectPromiseResolvers.clear(); |
| 133 } |
| 134 |
| 104 DEFINE_TRACE(RemotePlayback) | 135 DEFINE_TRACE(RemotePlayback) |
| 105 { | 136 { |
| 106 visitor->trace(m_availabilityObjects); | 137 visitor->trace(m_availabilityObjects); |
| 138 visitor->trace(m_mediaElement); |
| 139 visitor->trace(m_connectPromiseResolvers); |
| 107 RefCountedGarbageCollectedEventTargetWithInlineData<RemotePlayback>::trace(v
isitor); | 140 RefCountedGarbageCollectedEventTargetWithInlineData<RemotePlayback>::trace(v
isitor); |
| 108 DOMWindowProperty::trace(visitor); | 141 DOMWindowProperty::trace(visitor); |
| 109 } | 142 } |
| 110 | 143 |
| 111 } // namespace blink | 144 } // namespace blink |
| OLD | NEW |