Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(164)

Side by Side Diff: third_party/WebKit/Source/modules/mediasession/MediaSession.cpp

Issue 2589893002: [Blink>MediaSession] Use setActionCallback() instead of event listeners for media control actions (Closed)
Patch Set: fixed tests Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/MediaSessionActionHandler.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& mojomActionToActionName(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> actionNameToMojomAction(
47 const AtomicString& eventName) { 56 const String& actionName) {
48 if (EventTypeNames::play == eventName) 57 if ("play" == actionName)
49 return MediaSessionAction::PLAY; 58 return MediaSessionAction::PLAY;
50 if (EventTypeNames::pause == eventName) 59 if ("pause" == actionName)
51 return MediaSessionAction::PAUSE; 60 return MediaSessionAction::PAUSE;
52 if (EventTypeNames::previoustrack == eventName) 61 if ("previoustrack" == actionName)
53 return MediaSessionAction::PREVIOUS_TRACK; 62 return MediaSessionAction::PREVIOUS_TRACK;
54 if (EventTypeNames::nexttrack == eventName) 63 if ("nexttrack" == actionName)
55 return MediaSessionAction::NEXT_TRACK; 64 return MediaSessionAction::NEXT_TRACK;
56 if (EventTypeNames::seekbackward == eventName) 65 if ("seekbackward" == actionName)
57 return MediaSessionAction::SEEK_BACKWARD; 66 return MediaSessionAction::SEEK_BACKWARD;
58 if (EventTypeNames::seekforward == eventName) 67 if ("seekforward" == actionName)
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
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::setActionHandler(const String& action,
144 return EventTargetNames::MediaSession; 153 MediaSessionActionHandler* handler) {
154 if (handler) {
155 auto addResult = m_actionHandlers.set(
156 action, TraceWrapperMember<MediaSessionActionHandler>(this, handler));
157
158 if (!addResult.isNewEntry)
159 return;
160
161 notifyActionChange(action, ActionChangeType::ActionEnabled);
162 } else {
163 if (m_actionHandlers.find(action) == m_actionHandlers.end())
164 return;
165
166 m_actionHandlers.remove(action);
167
168 notifyActionChange(action, ActionChangeType::ActionDisabled);
169 }
145 } 170 }
146 171
147 ExecutionContext* MediaSession::getExecutionContext() const { 172 void MediaSession::notifyActionChange(const String& action,
148 return ContextClient::getExecutionContext(); 173 ActionChangeType type) {
174 mojom::blink::MediaSessionService* service = getService();
175 if (!service)
176 return;
177
178 auto mojomAction = actionNameToMojomAction(action);
179 DCHECK(mojomAction.has_value());
180
181 switch (type) {
182 case ActionChangeType::ActionEnabled:
183 service->EnableAction(mojomAction.value());
184 break;
185 case ActionChangeType::ActionDisabled:
186 service->DisableAction(mojomAction.value());
187 break;
188 }
149 } 189 }
150 190
151 mojom::blink::MediaSessionService* MediaSession::getService() { 191 mojom::blink::MediaSessionService* MediaSession::getService() {
152 if (m_service) 192 if (m_service)
153 return m_service.get(); 193 return m_service.get();
154 if (!getExecutionContext()) 194 if (!getExecutionContext())
155 return nullptr; 195 return nullptr;
156 196
157 DCHECK(getExecutionContext()->isDocument()) 197 DCHECK(getExecutionContext()->isDocument())
158 << "MediaSession::getService() is only available from a frame"; 198 << "MediaSession::getService() is only available from a frame";
159 Document* document = toDocument(getExecutionContext()); 199 Document* document = toDocument(getExecutionContext());
160 if (!document->frame()) 200 if (!document->frame())
161 return nullptr; 201 return nullptr;
162 202
163 InterfaceProvider* interfaceProvider = document->frame()->interfaceProvider(); 203 InterfaceProvider* interfaceProvider = document->frame()->interfaceProvider();
164 if (!interfaceProvider) 204 if (!interfaceProvider)
165 return nullptr; 205 return nullptr;
166 206
167 interfaceProvider->getInterface(mojo::MakeRequest(&m_service)); 207 interfaceProvider->getInterface(mojo::MakeRequest(&m_service));
168 if (m_service.get()) 208 if (m_service.get())
169 m_service->SetClient(m_clientBinding.CreateInterfacePtrAndBind()); 209 m_service->SetClient(m_clientBinding.CreateInterfacePtrAndBind());
170 210
171 return m_service.get(); 211 return m_service.get();
172 } 212 }
173 213
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( 214 void MediaSession::DidReceiveAction(
199 blink::mojom::blink::MediaSessionAction action) { 215 blink::mojom::blink::MediaSessionAction action) {
200 DCHECK(getExecutionContext()->isDocument()); 216 DCHECK(getExecutionContext()->isDocument());
201 Document* document = toDocument(getExecutionContext()); 217 Document* document = toDocument(getExecutionContext());
202 UserGestureIndicator gestureIndicator( 218 UserGestureIndicator gestureIndicator(
203 DocumentUserGestureToken::create(document)); 219 DocumentUserGestureToken::create(document));
204 dispatchEvent(Event::create(mojomActionToEventName(action))); 220
221 auto iter = m_actionHandlers.find(mojomActionToActionName(action));
222 if (iter == m_actionHandlers.end())
223 return;
224
225 iter->value->call(this);
226 }
227
228 void MediaSession::setV8ReferencesForHandlers(
229 v8::Isolate* isolate,
230 const v8::Persistent<v8::Object>& wrapper) {
231 for (auto handler : m_actionHandlers.values())
232 handler->setWrapperReference(isolate, wrapper);
205 } 233 }
206 234
207 DEFINE_TRACE(MediaSession) { 235 DEFINE_TRACE(MediaSession) {
208 visitor->trace(m_metadata); 236 visitor->trace(m_metadata);
209 EventTargetWithInlineData::trace(visitor); 237 visitor->trace(m_actionHandlers);
210 ContextClient::trace(visitor); 238 ContextClient::trace(visitor);
211 } 239 }
212 240
241 DEFINE_TRACE_WRAPPERS(MediaSession) {
242 for (auto handler : m_actionHandlers.values())
243 visitor->traceWrappers(handler);
244 }
245
213 } // namespace blink 246 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698