| 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 "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
| 8 #include "core/dom/DocumentUserGestureToken.h" | 8 #include "core/dom/DocumentUserGestureToken.h" |
| 9 #include "core/dom/ExecutionContext.h" | 9 #include "core/dom/ExecutionContext.h" |
| 10 #include "core/events/Event.h" | 10 #include "core/events/Event.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 using ::blink::mojom::blink::MediaSessionAction; | 24 using ::blink::mojom::blink::MediaSessionAction; |
| 25 | 25 |
| 26 const AtomicString& mojomActionToEventName(MediaSessionAction action) { | 26 const AtomicString& mojomActionToEventName(MediaSessionAction action) { |
| 27 switch (action) { | 27 switch (action) { |
| 28 case MediaSessionAction::PLAY: | 28 case MediaSessionAction::PLAY: |
| 29 return EventTypeNames::play; | 29 return EventTypeNames::play; |
| 30 case MediaSessionAction::PAUSE: | 30 case MediaSessionAction::PAUSE: |
| 31 return EventTypeNames::pause; | 31 return EventTypeNames::pause; |
| 32 case MediaSessionAction::PLAY_PAUSE: | |
| 33 return EventTypeNames::playpause; | |
| 34 case MediaSessionAction::PREVIOUS_TRACK: | 32 case MediaSessionAction::PREVIOUS_TRACK: |
| 35 return EventTypeNames::previoustrack; | 33 return EventTypeNames::previoustrack; |
| 36 case MediaSessionAction::NEXT_TRACK: | 34 case MediaSessionAction::NEXT_TRACK: |
| 37 return EventTypeNames::nexttrack; | 35 return EventTypeNames::nexttrack; |
| 38 case MediaSessionAction::SEEK_BACKWARD: | 36 case MediaSessionAction::SEEK_BACKWARD: |
| 39 return EventTypeNames::seekbackward; | 37 return EventTypeNames::seekbackward; |
| 40 case MediaSessionAction::SEEK_FORWARD: | 38 case MediaSessionAction::SEEK_FORWARD: |
| 41 return EventTypeNames::seekforward; | 39 return EventTypeNames::seekforward; |
| 42 default: | 40 default: |
| 43 NOTREACHED(); | 41 NOTREACHED(); |
| 44 } | 42 } |
| 45 return WTF::emptyAtom; | 43 return WTF::emptyAtom; |
| 46 } | 44 } |
| 47 | 45 |
| 48 WTF::Optional<MediaSessionAction> eventNameToMojomAction( | 46 WTF::Optional<MediaSessionAction> eventNameToMojomAction( |
| 49 const AtomicString& eventName) { | 47 const AtomicString& eventName) { |
| 50 if (EventTypeNames::play == eventName) | 48 if (EventTypeNames::play == eventName) |
| 51 return MediaSessionAction::PLAY; | 49 return MediaSessionAction::PLAY; |
| 52 if (EventTypeNames::pause == eventName) | 50 if (EventTypeNames::pause == eventName) |
| 53 return MediaSessionAction::PAUSE; | 51 return MediaSessionAction::PAUSE; |
| 54 if (EventTypeNames::playpause == eventName) | |
| 55 return MediaSessionAction::PLAY_PAUSE; | |
| 56 if (EventTypeNames::previoustrack == eventName) | 52 if (EventTypeNames::previoustrack == eventName) |
| 57 return MediaSessionAction::PREVIOUS_TRACK; | 53 return MediaSessionAction::PREVIOUS_TRACK; |
| 58 if (EventTypeNames::nexttrack == eventName) | 54 if (EventTypeNames::nexttrack == eventName) |
| 59 return MediaSessionAction::NEXT_TRACK; | 55 return MediaSessionAction::NEXT_TRACK; |
| 60 if (EventTypeNames::seekbackward == eventName) | 56 if (EventTypeNames::seekbackward == eventName) |
| 61 return MediaSessionAction::SEEK_BACKWARD; | 57 return MediaSessionAction::SEEK_BACKWARD; |
| 62 if (EventTypeNames::seekforward == eventName) | 58 if (EventTypeNames::seekforward == eventName) |
| 63 return MediaSessionAction::SEEK_FORWARD; | 59 return MediaSessionAction::SEEK_FORWARD; |
| 64 | 60 |
| 65 NOTREACHED(); | 61 NOTREACHED(); |
| 66 return WTF::nullopt; | 62 return WTF::nullopt; |
| 67 } | 63 } |
| 68 | 64 |
| 65 const AtomicString& mediaSessionPlaybackStateToString( |
| 66 mojom::blink::MediaSessionPlaybackState state) { |
| 67 DEFINE_STATIC_LOCAL(const AtomicString, noneValue, ("none")); |
| 68 DEFINE_STATIC_LOCAL(const AtomicString, pausedValue, ("paused")); |
| 69 DEFINE_STATIC_LOCAL(const AtomicString, playingValue, ("playing")); |
| 70 |
| 71 switch (state) { |
| 72 case mojom::blink::MediaSessionPlaybackState::NONE: |
| 73 return noneValue; |
| 74 case mojom::blink::MediaSessionPlaybackState::PAUSED: |
| 75 return pausedValue; |
| 76 case mojom::blink::MediaSessionPlaybackState::PLAYING: |
| 77 return playingValue; |
| 78 } |
| 79 NOTREACHED(); |
| 80 return WTF::emptyAtom; |
| 81 } |
| 82 |
| 83 mojom::blink::MediaSessionPlaybackState stringToMediaSessionPlaybackState( |
| 84 const String& stateName) { |
| 85 if (stateName == "none") |
| 86 return mojom::blink::MediaSessionPlaybackState::NONE; |
| 87 if (stateName == "paused") |
| 88 return mojom::blink::MediaSessionPlaybackState::PAUSED; |
| 89 DCHECK_EQ(stateName, "playing"); |
| 90 return mojom::blink::MediaSessionPlaybackState::PLAYING; |
| 91 } |
| 92 |
| 69 } // anonymous namespace | 93 } // anonymous namespace |
| 70 | 94 |
| 71 MediaSession::MediaSession(ExecutionContext* executionContext) | 95 MediaSession::MediaSession(ExecutionContext* executionContext) |
| 72 : ContextLifecycleObserver(executionContext), m_clientBinding(this) {} | 96 : ContextLifecycleObserver(executionContext), |
| 97 m_playbackState(mojom::blink::MediaSessionPlaybackState::NONE), |
| 98 m_clientBinding(this) {} |
| 73 | 99 |
| 74 MediaSession* MediaSession::create(ExecutionContext* executionContext) { | 100 MediaSession* MediaSession::create(ExecutionContext* executionContext) { |
| 75 return new MediaSession(executionContext); | 101 return new MediaSession(executionContext); |
| 76 } | 102 } |
| 77 | 103 |
| 78 void MediaSession::dispose() { | 104 void MediaSession::dispose() { |
| 79 m_clientBinding.Close(); | 105 m_clientBinding.Close(); |
| 80 } | 106 } |
| 81 | 107 |
| 108 void MediaSession::setPlaybackState(const String& playbackState) { |
| 109 m_playbackState = stringToMediaSessionPlaybackState(playbackState); |
| 110 mojom::blink::MediaSessionService* service = getService(); |
| 111 if (service) |
| 112 service->SetPlaybackState(m_playbackState); |
| 113 } |
| 114 |
| 115 String MediaSession::playbackState() { |
| 116 return mediaSessionPlaybackStateToString(m_playbackState); |
| 117 } |
| 118 |
| 82 void MediaSession::setMetadata(MediaMetadata* metadata) { | 119 void MediaSession::setMetadata(MediaMetadata* metadata) { |
| 83 if (metadata) | 120 if (metadata) |
| 84 metadata->setSession(this); | 121 metadata->setSession(this); |
| 85 | 122 |
| 86 if (m_metadata) | 123 if (m_metadata) |
| 87 m_metadata->setSession(nullptr); | 124 m_metadata->setSession(nullptr); |
| 88 | 125 |
| 89 m_metadata = metadata; | 126 m_metadata = metadata; |
| 90 onMetadataChanged(); | 127 onMetadataChanged(); |
| 91 } | 128 } |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 dispatchEvent(Event::create(mojomActionToEventName(action))); | 204 dispatchEvent(Event::create(mojomActionToEventName(action))); |
| 168 } | 205 } |
| 169 | 206 |
| 170 DEFINE_TRACE(MediaSession) { | 207 DEFINE_TRACE(MediaSession) { |
| 171 visitor->trace(m_metadata); | 208 visitor->trace(m_metadata); |
| 172 EventTargetWithInlineData::trace(visitor); | 209 EventTargetWithInlineData::trace(visitor); |
| 173 ContextLifecycleObserver::trace(visitor); | 210 ContextLifecycleObserver::trace(visitor); |
| 174 } | 211 } |
| 175 | 212 |
| 176 } // namespace blink | 213 } // namespace blink |
| OLD | NEW |