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

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: rebased and more tests Created 3 years, 12 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 LOG(INFO) << action << reinterpret_cast<size_t>(handler);
mlamouri (slow - plz ping) 2016/12/23 12:13:58 You might want to remove this :)
Zhiqiang Zhang (Slow) 2017/01/05 09:29:15 Done.
155 if (handler) {
156 auto addResult = m_actionHandlers.set(
157 action, TraceWrapperMember<MediaSessionActionHandler>(this, handler));
158
159 if (!addResult.isNewEntry)
160 return;
161
162 notifyActionChange(action, ActionChangeType::ActionEnabled);
163 } else {
164 if (m_actionHandlers.find(action) == m_actionHandlers.end())
165 return;
166
167 m_actionHandlers.remove(action);
168
169 notifyActionChange(action, ActionChangeType::ActionDisabled);
170 }
145 } 171 }
146 172
147 ExecutionContext* MediaSession::getExecutionContext() const { 173 void MediaSession::notifyActionChange(const String& action,
148 return ContextLifecycleObserver::getExecutionContext(); 174 ActionChangeType type) {
175 mojom::blink::MediaSessionService* service = getService();
176 if (!service)
177 return;
178
179 auto mojomAction = actionNameToMojomAction(action);
180 DCHECK(mojomAction.has_value());
181
182 switch (type) {
183 case ActionChangeType::ActionEnabled:
184 service->EnableAction(mojomAction.value());
185 break;
186 case ActionChangeType::ActionDisabled:
187 service->DisableAction(mojomAction.value());
188 break;
189 }
149 } 190 }
150 191
151 mojom::blink::MediaSessionService* MediaSession::getService() { 192 mojom::blink::MediaSessionService* MediaSession::getService() {
152 if (m_service) 193 if (m_service)
153 return m_service.get(); 194 return m_service.get();
154 if (!getExecutionContext()) 195 if (!getExecutionContext())
155 return nullptr; 196 return nullptr;
156 197
157 DCHECK(getExecutionContext()->isDocument()) 198 DCHECK(getExecutionContext()->isDocument())
158 << "MediaSession::getService() is only available from a frame"; 199 << "MediaSession::getService() is only available from a frame";
159 Document* document = toDocument(getExecutionContext()); 200 Document* document = toDocument(getExecutionContext());
160 if (!document->frame()) 201 if (!document->frame())
161 return nullptr; 202 return nullptr;
162 203
163 InterfaceProvider* interfaceProvider = document->frame()->interfaceProvider(); 204 InterfaceProvider* interfaceProvider = document->frame()->interfaceProvider();
164 if (!interfaceProvider) 205 if (!interfaceProvider)
165 return nullptr; 206 return nullptr;
166 207
167 interfaceProvider->getInterface(mojo::MakeRequest(&m_service)); 208 interfaceProvider->getInterface(mojo::MakeRequest(&m_service));
168 if (m_service.get()) 209 if (m_service.get())
169 m_service->SetClient(m_clientBinding.CreateInterfacePtrAndBind()); 210 m_service->SetClient(m_clientBinding.CreateInterfacePtrAndBind());
170 211
171 return m_service.get(); 212 return m_service.get();
172 } 213 }
173 214
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( 215 void MediaSession::DidReceiveAction(
199 blink::mojom::blink::MediaSessionAction action) { 216 blink::mojom::blink::MediaSessionAction action) {
200 DCHECK(getExecutionContext()->isDocument()); 217 DCHECK(getExecutionContext()->isDocument());
201 Document* document = toDocument(getExecutionContext()); 218 Document* document = toDocument(getExecutionContext());
202 UserGestureIndicator gestureIndicator( 219 UserGestureIndicator gestureIndicator(
203 DocumentUserGestureToken::create(document)); 220 DocumentUserGestureToken::create(document));
204 dispatchEvent(Event::create(mojomActionToEventName(action))); 221
222 auto iter = m_actionHandlers.find(mojomActionToActionName(action));
223 if (iter == m_actionHandlers.end())
224 return;
225
226 iter->value->call(this);
227 }
228
229 void MediaSession::setV8ReferencesForHandlers(
230 v8::Isolate* isolate,
231 const v8::Persistent<v8::Object>& wrapper) {
232 for (auto handler : m_actionHandlers.values())
233 handler->setWrapperReference(isolate, wrapper);
205 } 234 }
206 235
207 DEFINE_TRACE(MediaSession) { 236 DEFINE_TRACE(MediaSession) {
208 visitor->trace(m_metadata); 237 visitor->trace(m_metadata);
209 EventTargetWithInlineData::trace(visitor); 238 visitor->trace(m_actionHandlers);
210 ContextLifecycleObserver::trace(visitor); 239 ContextLifecycleObserver::trace(visitor);
211 } 240 }
212 241
242 DEFINE_TRACE_WRAPPERS(MediaSession) {
243 for (auto handler : m_actionHandlers.values())
244 visitor->traceWrappers(handler);
245 }
246
213 } // namespace blink 247 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698