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/core/v8/ScriptState.h" | 7 #include "bindings/core/v8/ScriptState.h" |
8 #include "core/dom/Document.h" | 8 #include "core/dom/Document.h" |
9 #include "core/dom/ExecutionContext.h" | 9 #include "core/dom/ExecutionContext.h" |
| 10 #include "core/events/Event.h" |
10 #include "core/frame/LocalFrame.h" | 11 #include "core/frame/LocalFrame.h" |
11 #include "modules/EventTargetModules.h" | 12 #include "modules/EventTargetModules.h" |
12 #include "modules/mediasession/MediaMetadata.h" | 13 #include "modules/mediasession/MediaMetadata.h" |
13 #include "modules/mediasession/MediaMetadataSanitizer.h" | 14 #include "modules/mediasession/MediaMetadataSanitizer.h" |
14 #include "public/platform/InterfaceProvider.h" | 15 #include "public/platform/InterfaceProvider.h" |
| 16 #include "wtf/Optional.h" |
15 #include <memory> | 17 #include <memory> |
16 | 18 |
17 namespace blink { | 19 namespace blink { |
18 | 20 |
| 21 namespace { |
| 22 |
| 23 using ::blink::mojom::blink::MediaSessionAction; |
| 24 |
| 25 const AtomicString& mojomActionToEventName(MediaSessionAction action) { |
| 26 switch (action) { |
| 27 case MediaSessionAction::PLAY: |
| 28 return EventTypeNames::play; |
| 29 case MediaSessionAction::PAUSE: |
| 30 return EventTypeNames::pause; |
| 31 case MediaSessionAction::PLAY_PAUSE: |
| 32 return EventTypeNames::playpause; |
| 33 case MediaSessionAction::PREVIOUS_TRACK: |
| 34 return EventTypeNames::previoustrack; |
| 35 case MediaSessionAction::NEXT_TRACK: |
| 36 return EventTypeNames::nexttrack; |
| 37 case MediaSessionAction::SEEK_FORWARD: |
| 38 return EventTypeNames::seekforward; |
| 39 case MediaSessionAction::SEEK_BACKWARD: |
| 40 return EventTypeNames::seekbackward; |
| 41 default: |
| 42 NOTREACHED(); |
| 43 } |
| 44 return WTF::emptyAtom; |
| 45 } |
| 46 |
| 47 WTF::Optional<MediaSessionAction> eventNameToMojomAction( |
| 48 const AtomicString& eventName) { |
| 49 if (EventTypeNames::play == eventName) |
| 50 return MediaSessionAction::PLAY; |
| 51 if (EventTypeNames::pause == eventName) |
| 52 return MediaSessionAction::PAUSE; |
| 53 if (EventTypeNames::playpause == eventName) |
| 54 return MediaSessionAction::PLAY_PAUSE; |
| 55 if (EventTypeNames::previoustrack == eventName) |
| 56 return MediaSessionAction::PREVIOUS_TRACK; |
| 57 if (EventTypeNames::nexttrack == eventName) |
| 58 return MediaSessionAction::NEXT_TRACK; |
| 59 if (EventTypeNames::seekforward == eventName) |
| 60 return MediaSessionAction::SEEK_FORWARD; |
| 61 if (EventTypeNames::seekbackward == eventName) |
| 62 return MediaSessionAction::SEEK_BACKWARD; |
| 63 |
| 64 NOTREACHED(); |
| 65 return WTF::nullopt; |
| 66 } |
| 67 |
| 68 } // anonymous namespace |
| 69 |
19 MediaSession::MediaSession(ScriptState* scriptState) | 70 MediaSession::MediaSession(ScriptState* scriptState) |
20 : m_scriptState(scriptState) {} | 71 : m_scriptState(scriptState), m_clientBinding(this) {} |
21 | 72 |
22 MediaSession* MediaSession::create(ScriptState* scriptState) { | 73 MediaSession* MediaSession::create(ScriptState* scriptState) { |
23 return new MediaSession(scriptState); | 74 return new MediaSession(scriptState); |
24 } | 75 } |
25 | 76 |
| 77 void MediaSession::dispose() { |
| 78 m_clientBinding.Close(); |
| 79 } |
| 80 |
26 void MediaSession::setMetadata(MediaMetadata* metadata) { | 81 void MediaSession::setMetadata(MediaMetadata* metadata) { |
27 if (mojom::blink::MediaSessionService* service = | 82 if (mojom::blink::MediaSessionService* service = |
28 getService(m_scriptState.get())) { | 83 getService(m_scriptState.get())) { |
29 service->SetMetadata( | 84 service->SetMetadata( |
30 MediaMetadataSanitizer::sanitizeAndConvertToMojo(metadata)); | 85 MediaMetadataSanitizer::sanitizeAndConvertToMojo(metadata)); |
31 } | 86 } |
32 } | 87 } |
33 | 88 |
34 MediaMetadata* MediaSession::metadata() const { | 89 MediaMetadata* MediaSession::metadata() const { |
35 return m_metadata; | 90 return m_metadata; |
36 } | 91 } |
37 | 92 |
38 const WTF::AtomicString& MediaSession::interfaceName() const { | 93 const WTF::AtomicString& MediaSession::interfaceName() const { |
39 return EventTargetNames::MediaSession; | 94 return EventTargetNames::MediaSession; |
40 } | 95 } |
41 | 96 |
42 ExecutionContext* MediaSession::getExecutionContext() const { | 97 ExecutionContext* MediaSession::getExecutionContext() const { |
43 return m_scriptState->getExecutionContext(); | 98 return m_scriptState->getExecutionContext(); |
44 } | 99 } |
45 | 100 |
46 mojom::blink::MediaSessionService* MediaSession::getService( | 101 mojom::blink::MediaSessionService* MediaSession::getService( |
47 ScriptState* scriptState) { | 102 ScriptState* scriptState) { |
48 if (!m_service) { | 103 if (m_service) |
49 InterfaceProvider* interfaceProvider = nullptr; | 104 return m_service.get(); |
50 DCHECK(scriptState->getExecutionContext()->isDocument()) | |
51 << "MediaSession::getService() is only available from a frame"; | |
52 Document* document = toDocument(scriptState->getExecutionContext()); | |
53 if (document->frame()) | |
54 interfaceProvider = document->frame()->interfaceProvider(); | |
55 | 105 |
56 if (interfaceProvider) | 106 DCHECK(scriptState->getExecutionContext()->isDocument()) |
57 interfaceProvider->getInterface(mojo::GetProxy(&m_service)); | 107 << "MediaSession::getService() is only available from a frame"; |
58 } | 108 Document* document = toDocument(scriptState->getExecutionContext()); |
| 109 if (!document->frame()) |
| 110 return nullptr; |
| 111 |
| 112 InterfaceProvider* interfaceProvider = document->frame()->interfaceProvider(); |
| 113 if (!interfaceProvider) |
| 114 return nullptr; |
| 115 |
| 116 interfaceProvider->getInterface(mojo::GetProxy(&m_service)); |
| 117 if (m_service.get()) |
| 118 m_service->SetClient(m_clientBinding.CreateInterfacePtrAndBind()); |
| 119 |
59 return m_service.get(); | 120 return m_service.get(); |
60 } | 121 } |
61 | 122 |
62 bool MediaSession::addEventListenerInternal( | 123 bool MediaSession::addEventListenerInternal( |
63 const AtomicString& eventType, | 124 const AtomicString& eventType, |
64 EventListener* listener, | 125 EventListener* listener, |
65 const AddEventListenerOptionsResolved& options) { | 126 const AddEventListenerOptionsResolved& options) { |
66 // TODO(zqzhang): Notify MediaSessionService the handler has been set. See | 127 if (mojom::blink::MediaSessionService* service = |
67 // https://crbug.com/656563 | 128 getService(m_scriptState.get())) { |
| 129 auto mojomAction = eventNameToMojomAction(eventType); |
| 130 DCHECK(mojomAction.has_value()); |
| 131 service->EnableAction(mojomAction.value()); |
| 132 } |
68 return EventTarget::addEventListenerInternal(eventType, listener, options); | 133 return EventTarget::addEventListenerInternal(eventType, listener, options); |
69 } | 134 } |
70 | 135 |
71 bool MediaSession::removeEventListenerInternal( | 136 bool MediaSession::removeEventListenerInternal( |
72 const AtomicString& eventType, | 137 const AtomicString& eventType, |
73 const EventListener* listener, | 138 const EventListener* listener, |
74 const EventListenerOptions& options) { | 139 const EventListenerOptions& options) { |
75 // TODO(zqzhang): Notify MediaSessionService the handler has been unset. See | 140 if (mojom::blink::MediaSessionService* service = |
76 // https://crbug.com/656563 | 141 getService(m_scriptState.get())) { |
| 142 auto mojomAction = eventNameToMojomAction(eventType); |
| 143 DCHECK(mojomAction.has_value()); |
| 144 service->DisableAction(mojomAction.value()); |
| 145 } |
77 return EventTarget::removeEventListenerInternal(eventType, listener, options); | 146 return EventTarget::removeEventListenerInternal(eventType, listener, options); |
78 } | 147 } |
79 | 148 |
| 149 void MediaSession::DidReceiveAction( |
| 150 blink::mojom::blink::MediaSessionAction action) { |
| 151 dispatchEvent(Event::create(mojomActionToEventName(action))); |
| 152 } |
| 153 |
80 DEFINE_TRACE(MediaSession) { | 154 DEFINE_TRACE(MediaSession) { |
81 visitor->trace(m_metadata); | 155 visitor->trace(m_metadata); |
82 EventTargetWithInlineData::trace(visitor); | 156 EventTargetWithInlineData::trace(visitor); |
83 } | 157 } |
84 | 158 |
85 } // namespace blink | 159 } // namespace blink |
OLD | NEW |