Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/mediasession/MediaSession.h" | 5 #include "modules/mediasession/MediaSession.h" |
| 6 | 6 |
| 7 #include "bindings/modules/v8/MediaSessionActionCallback.h" | |
| 7 #include "core/dom/Document.h" | 8 #include "core/dom/Document.h" |
| 8 #include "core/dom/DocumentUserGestureToken.h" | 9 #include "core/dom/DocumentUserGestureToken.h" |
| 9 #include "core/dom/ExecutionContext.h" | 10 #include "core/dom/ExecutionContext.h" |
| 10 #include "core/events/Event.h" | |
| 11 #include "core/frame/LocalFrame.h" | 11 #include "core/frame/LocalFrame.h" |
| 12 #include "modules/EventTargetModules.h" | |
| 13 #include "modules/mediasession/MediaMetadata.h" | 12 #include "modules/mediasession/MediaMetadata.h" |
| 14 #include "modules/mediasession/MediaMetadataSanitizer.h" | 13 #include "modules/mediasession/MediaMetadataSanitizer.h" |
| 15 #include "platform/UserGestureIndicator.h" | 14 #include "platform/UserGestureIndicator.h" |
| 16 #include "public/platform/InterfaceProvider.h" | 15 #include "public/platform/InterfaceProvider.h" |
| 17 #include "wtf/Optional.h" | 16 #include "wtf/Optional.h" |
| 18 #include <memory> | 17 #include <memory> |
| 19 | 18 |
| 20 namespace blink { | 19 namespace blink { |
| 21 | 20 |
| 22 namespace { | 21 namespace { |
| 23 | 22 |
| 24 using ::blink::mojom::blink::MediaSessionAction; | 23 using ::blink::mojom::blink::MediaSessionAction; |
| 25 | 24 |
| 26 const AtomicString& mojomActionToEventName(MediaSessionAction action) { | 25 const AtomicString& mojomActionToEventName(MediaSessionAction action) { |
| 26 DEFINE_STATIC_LOCAL(const AtomicString, playActionName, ("play")); | |
| 27 DEFINE_STATIC_LOCAL(const AtomicString, pauseActionName, ("pause")); | |
| 28 DEFINE_STATIC_LOCAL(const AtomicString, previousTrackActionName, | |
| 29 ("previoustrack")); | |
| 30 DEFINE_STATIC_LOCAL(const AtomicString, nextTrackActionName, ("nexttrack")); | |
| 31 DEFINE_STATIC_LOCAL(const AtomicString, seekBackwardActionName, | |
| 32 ("seekbackward")); | |
| 33 DEFINE_STATIC_LOCAL(const AtomicString, seekForwardActionName, | |
| 34 ("seekforward")); | |
| 35 | |
| 27 switch (action) { | 36 switch (action) { |
| 28 case MediaSessionAction::PLAY: | 37 case MediaSessionAction::PLAY: |
| 29 return EventTypeNames::play; | 38 return playActionName; |
| 30 case MediaSessionAction::PAUSE: | 39 case MediaSessionAction::PAUSE: |
| 31 return EventTypeNames::pause; | 40 return pauseActionName; |
| 32 case MediaSessionAction::PREVIOUS_TRACK: | 41 case MediaSessionAction::PREVIOUS_TRACK: |
| 33 return EventTypeNames::previoustrack; | 42 return previousTrackActionName; |
| 34 case MediaSessionAction::NEXT_TRACK: | 43 case MediaSessionAction::NEXT_TRACK: |
| 35 return EventTypeNames::nexttrack; | 44 return nextTrackActionName; |
| 36 case MediaSessionAction::SEEK_BACKWARD: | 45 case MediaSessionAction::SEEK_BACKWARD: |
| 37 return EventTypeNames::seekbackward; | 46 return seekBackwardActionName; |
| 38 case MediaSessionAction::SEEK_FORWARD: | 47 case MediaSessionAction::SEEK_FORWARD: |
| 39 return EventTypeNames::seekforward; | 48 return seekForwardActionName; |
| 40 default: | 49 default: |
| 41 NOTREACHED(); | 50 NOTREACHED(); |
| 42 } | 51 } |
| 43 return WTF::emptyAtom; | 52 return WTF::emptyAtom; |
| 44 } | 53 } |
| 45 | 54 |
| 46 WTF::Optional<MediaSessionAction> eventNameToMojomAction( | 55 WTF::Optional<MediaSessionAction> eventNameToMojomAction( |
| 47 const AtomicString& eventName) { | 56 const String& eventName) { |
|
mlamouri (slow - plz ping)
2016/12/20 13:25:57
s/eventName/actionName/?
Zhiqiang Zhang (Slow)
2016/12/20 13:53:06
Done.
| |
| 48 if (EventTypeNames::play == eventName) | 57 if ("play" == eventName) |
| 49 return MediaSessionAction::PLAY; | 58 return MediaSessionAction::PLAY; |
| 50 if (EventTypeNames::pause == eventName) | 59 if ("pause" == eventName) |
| 51 return MediaSessionAction::PAUSE; | 60 return MediaSessionAction::PAUSE; |
| 52 if (EventTypeNames::previoustrack == eventName) | 61 if ("previoustrack" == eventName) |
| 53 return MediaSessionAction::PREVIOUS_TRACK; | 62 return MediaSessionAction::PREVIOUS_TRACK; |
| 54 if (EventTypeNames::nexttrack == eventName) | 63 if ("nexttrack" == eventName) |
| 55 return MediaSessionAction::NEXT_TRACK; | 64 return MediaSessionAction::NEXT_TRACK; |
| 56 if (EventTypeNames::seekbackward == eventName) | 65 if ("seekbackward" == eventName) |
| 57 return MediaSessionAction::SEEK_BACKWARD; | 66 return MediaSessionAction::SEEK_BACKWARD; |
| 58 if (EventTypeNames::seekforward == eventName) | 67 if ("seekforward" == eventName) |
| 59 return MediaSessionAction::SEEK_FORWARD; | 68 return MediaSessionAction::SEEK_FORWARD; |
| 60 | 69 |
| 61 NOTREACHED(); | 70 NOTREACHED(); |
| 62 return WTF::nullopt; | 71 return WTF::nullopt; |
| 63 } | 72 } |
| 64 | 73 |
| 65 const AtomicString& mediaSessionPlaybackStateToString( | 74 const AtomicString& mediaSessionPlaybackStateToString( |
| 66 mojom::blink::MediaSessionPlaybackState state) { | 75 mojom::blink::MediaSessionPlaybackState state) { |
| 67 DEFINE_STATIC_LOCAL(const AtomicString, noneValue, ("none")); | 76 DEFINE_STATIC_LOCAL(const AtomicString, noneValue, ("none")); |
| 68 DEFINE_STATIC_LOCAL(const AtomicString, pausedValue, ("paused")); | 77 DEFINE_STATIC_LOCAL(const AtomicString, pausedValue, ("paused")); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 133 | 142 |
| 134 void MediaSession::onMetadataChanged() { | 143 void MediaSession::onMetadataChanged() { |
| 135 mojom::blink::MediaSessionService* service = getService(); | 144 mojom::blink::MediaSessionService* service = getService(); |
| 136 if (!service) | 145 if (!service) |
| 137 return; | 146 return; |
| 138 | 147 |
| 139 service->SetMetadata(MediaMetadataSanitizer::sanitizeAndConvertToMojo( | 148 service->SetMetadata(MediaMetadataSanitizer::sanitizeAndConvertToMojo( |
| 140 m_metadata, getExecutionContext())); | 149 m_metadata, getExecutionContext())); |
| 141 } | 150 } |
| 142 | 151 |
| 143 const WTF::AtomicString& MediaSession::interfaceName() const { | 152 void MediaSession::setActionCallback(const String& action, |
| 144 return EventTargetNames::MediaSession; | 153 MediaSessionActionCallback* callback) { |
| 145 } | 154 m_actionCallbacks.set( |
| 155 action, TraceWrapperMember<MediaSessionActionCallback>(this, callback)); | |
| 146 | 156 |
| 147 ExecutionContext* MediaSession::getExecutionContext() const { | 157 mojom::blink::MediaSessionService* service = getService(); |
| 148 return ContextLifecycleObserver::getExecutionContext(); | 158 if (!service) |
| 159 return; | |
| 160 auto mojomAction = eventNameToMojomAction(action); | |
| 161 DCHECK(mojomAction.has_value()); | |
| 162 if (callback) | |
| 163 service->EnableAction(mojomAction.value()); | |
| 164 else | |
| 165 service->DisableAction(mojomAction.value()); | |
|
mlamouri (slow - plz ping)
2016/12/20 13:25:57
Would it make sense to have timer in order to bund
Zhiqiang Zhang (Slow)
2016/12/20 13:53:06
We are still passing individual actions through mo
| |
| 149 } | 166 } |
| 150 | 167 |
| 151 mojom::blink::MediaSessionService* MediaSession::getService() { | 168 mojom::blink::MediaSessionService* MediaSession::getService() { |
| 152 if (m_service) | 169 if (m_service) |
| 153 return m_service.get(); | 170 return m_service.get(); |
| 154 if (!getExecutionContext()) | 171 if (!getExecutionContext()) |
| 155 return nullptr; | 172 return nullptr; |
| 156 | 173 |
| 157 DCHECK(getExecutionContext()->isDocument()) | 174 DCHECK(getExecutionContext()->isDocument()) |
| 158 << "MediaSession::getService() is only available from a frame"; | 175 << "MediaSession::getService() is only available from a frame"; |
| 159 Document* document = toDocument(getExecutionContext()); | 176 Document* document = toDocument(getExecutionContext()); |
| 160 if (!document->frame()) | 177 if (!document->frame()) |
| 161 return nullptr; | 178 return nullptr; |
| 162 | 179 |
| 163 InterfaceProvider* interfaceProvider = document->frame()->interfaceProvider(); | 180 InterfaceProvider* interfaceProvider = document->frame()->interfaceProvider(); |
| 164 if (!interfaceProvider) | 181 if (!interfaceProvider) |
| 165 return nullptr; | 182 return nullptr; |
| 166 | 183 |
| 167 interfaceProvider->getInterface(mojo::GetProxy(&m_service)); | 184 interfaceProvider->getInterface(mojo::GetProxy(&m_service)); |
| 168 if (m_service.get()) | 185 if (m_service.get()) |
| 169 m_service->SetClient(m_clientBinding.CreateInterfacePtrAndBind()); | 186 m_service->SetClient(m_clientBinding.CreateInterfacePtrAndBind()); |
| 170 | 187 |
| 171 return m_service.get(); | 188 return m_service.get(); |
| 172 } | 189 } |
| 173 | 190 |
| 174 bool MediaSession::addEventListenerInternal( | |
| 175 const AtomicString& eventType, | |
| 176 EventListener* listener, | |
| 177 const AddEventListenerOptionsResolved& options) { | |
| 178 if (mojom::blink::MediaSessionService* service = getService()) { | |
| 179 auto mojomAction = eventNameToMojomAction(eventType); | |
| 180 DCHECK(mojomAction.has_value()); | |
| 181 service->EnableAction(mojomAction.value()); | |
| 182 } | |
| 183 return EventTarget::addEventListenerInternal(eventType, listener, options); | |
| 184 } | |
| 185 | |
| 186 bool MediaSession::removeEventListenerInternal( | |
| 187 const AtomicString& eventType, | |
| 188 const EventListener* listener, | |
| 189 const EventListenerOptions& options) { | |
| 190 if (mojom::blink::MediaSessionService* service = getService()) { | |
| 191 auto mojomAction = eventNameToMojomAction(eventType); | |
| 192 DCHECK(mojomAction.has_value()); | |
| 193 service->DisableAction(mojomAction.value()); | |
| 194 } | |
| 195 return EventTarget::removeEventListenerInternal(eventType, listener, options); | |
| 196 } | |
| 197 | |
| 198 void MediaSession::DidReceiveAction( | 191 void MediaSession::DidReceiveAction( |
| 199 blink::mojom::blink::MediaSessionAction action) { | 192 blink::mojom::blink::MediaSessionAction action) { |
| 200 DCHECK(getExecutionContext()->isDocument()); | 193 DCHECK(getExecutionContext()->isDocument()); |
| 201 Document* document = toDocument(getExecutionContext()); | 194 Document* document = toDocument(getExecutionContext()); |
| 202 UserGestureIndicator gestureIndicator( | 195 UserGestureIndicator gestureIndicator( |
| 203 DocumentUserGestureToken::create(document)); | 196 DocumentUserGestureToken::create(document)); |
| 204 dispatchEvent(Event::create(mojomActionToEventName(action))); | 197 |
| 198 auto iter = m_actionCallbacks.find(mojomActionToEventName(action)); | |
| 199 if (iter == m_actionCallbacks.end()) | |
| 200 return; | |
| 201 | |
| 202 iter->value->call(this); | |
| 203 } | |
| 204 | |
| 205 void MediaSession::setV8ReferencesForCallbacks( | |
| 206 v8::Isolate* isolate, | |
| 207 const v8::Persistent<v8::Object>& wrapper) { | |
| 208 for (auto callback : m_actionCallbacks.values()) | |
| 209 callback->setWrapperReference(isolate, wrapper); | |
| 205 } | 210 } |
| 206 | 211 |
| 207 DEFINE_TRACE(MediaSession) { | 212 DEFINE_TRACE(MediaSession) { |
| 208 visitor->trace(m_metadata); | 213 visitor->trace(m_metadata); |
| 209 EventTargetWithInlineData::trace(visitor); | 214 visitor->trace(m_actionCallbacks); |
| 210 ContextLifecycleObserver::trace(visitor); | 215 ContextLifecycleObserver::trace(visitor); |
| 211 } | 216 } |
| 212 | 217 |
| 218 DEFINE_TRACE_WRAPPERS(MediaSession) { | |
| 219 for (auto callback : m_actionCallbacks.values()) { | |
| 220 visitor->traceWrappers(callback); | |
| 221 } | |
|
mlamouri (slow - plz ping)
2016/12/20 13:25:57
style: you can drop the { }
Zhiqiang Zhang (Slow)
2016/12/20 13:53:06
Done.
| |
| 222 } | |
| 223 | |
| 213 } // namespace blink | 224 } // namespace blink |
| OLD | NEW |