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" |
15 #include <memory> | 16 #include <memory> |
16 | 17 |
17 namespace blink { | 18 namespace blink { |
18 | 19 |
20 namespace { | |
21 | |
22 using ::blink::mojom::blink::MediaSessionAction; | |
23 | |
24 const AtomicString& mojomActionToEventName(MediaSessionAction action) { | |
25 DEFINE_STATIC_LOCAL(AtomicString, emptyString, ("")); | |
26 | |
27 switch (action) { | |
28 case MediaSessionAction::PLAY: | |
29 return EventTypeNames::play; | |
30 case MediaSessionAction::PAUSE: | |
31 return EventTypeNames::pause; | |
32 case MediaSessionAction::PLAY_PAUSE: | |
33 return EventTypeNames::playpause; | |
34 case MediaSessionAction::PREVIOUS_TRACK: | |
35 return EventTypeNames::previoustrack; | |
36 case MediaSessionAction::NEXT_TRACK: | |
37 return EventTypeNames::nexttrack; | |
38 case MediaSessionAction::SEEK_FORWARD: | |
39 return EventTypeNames::seekforward; | |
40 case MediaSessionAction::SEEK_BACKWARD: | |
41 return EventTypeNames::seekbackward; | |
42 default: | |
43 NOTREACHED(); | |
44 } | |
45 return emptyString; | |
haraken
2016/10/19 12:58:05
Return WTF::emptyString() ?
Zhiqiang Zhang (Slow)
2016/10/21 14:52:45
Actually WTF::emptyAtom :)
| |
46 } | |
47 | |
48 MediaSessionAction eventNameToMojomAction(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 MediaSessionAction::PLAY; | |
whywhat
2016/10/19 21:59:00
I believe you need to return something different a
Zhiqiang Zhang (Slow)
2016/10/21 14:52:45
Done.
| |
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 service->EnableAction(eventNameToMojomAction(eventType)); | |
130 } | |
68 return EventTarget::addEventListenerInternal(eventType, listener, options); | 131 return EventTarget::addEventListenerInternal(eventType, listener, options); |
69 } | 132 } |
70 | 133 |
71 bool MediaSession::removeEventListenerInternal( | 134 bool MediaSession::removeEventListenerInternal( |
72 const AtomicString& eventType, | 135 const AtomicString& eventType, |
73 const EventListener* listener, | 136 const EventListener* listener, |
74 const EventListenerOptions& options) { | 137 const EventListenerOptions& options) { |
75 // TODO(zqzhang): Notify MediaSessionService the handler has been unset. See | 138 if (mojom::blink::MediaSessionService* service = |
76 // https://crbug.com/656563 | 139 getService(m_scriptState.get())) { |
140 service->DisableAction(eventNameToMojomAction(eventType)); | |
141 } | |
77 return EventTarget::removeEventListenerInternal(eventType, listener, options); | 142 return EventTarget::removeEventListenerInternal(eventType, listener, options); |
78 } | 143 } |
79 | 144 |
145 void MediaSession::DidReceiveAction( | |
146 blink::mojom::blink::MediaSessionAction action) { | |
147 dispatchEvent(Event::create(mojomActionToEventName(action))); | |
148 } | |
149 | |
80 DEFINE_TRACE(MediaSession) { | 150 DEFINE_TRACE(MediaSession) { |
81 visitor->trace(m_metadata); | 151 visitor->trace(m_metadata); |
82 EventTargetWithInlineData::trace(visitor); | 152 EventTargetWithInlineData::trace(visitor); |
83 } | 153 } |
84 | 154 |
85 } // namespace blink | 155 } // namespace blink |
OLD | NEW |