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

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

Issue 2428523002: Add media controls to MediaSession in Blink (Closed)
Patch Set: Created 4 years, 2 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/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/frame/LocalFrame.h" 10 #include "core/frame/LocalFrame.h"
11 #include "modules/EventTargetModules.h"
11 #include "modules/mediasession/MediaMetadata.h" 12 #include "modules/mediasession/MediaMetadata.h"
12 #include "modules/mediasession/MediaMetadataSanitizer.h" 13 #include "modules/mediasession/MediaMetadataSanitizer.h"
13 #include "public/platform/InterfaceProvider.h" 14 #include "public/platform/InterfaceProvider.h"
14 #include <memory> 15 #include <memory>
15 16
16 namespace blink { 17 namespace blink {
17 18
18 MediaSession::MediaSession() = default; 19 MediaSession::MediaSession(ScriptState* scriptState)
20 : m_scriptState(scriptState) {}
19 21
20 MediaSession* MediaSession::create() { 22 MediaSession* MediaSession::create(ScriptState* scriptState) {
21 return new MediaSession(); 23 return new MediaSession(scriptState);
22 } 24 }
23 25
24 void MediaSession::setMetadata(ScriptState* scriptState, 26 void MediaSession::setMetadata(MediaMetadata* metadata) {
25 MediaMetadata* metadata) { 27 if (mojom::blink::MediaSessionService* service =
26 if (getService(scriptState)) { 28 getService(m_scriptState.get())) {
27 getService(scriptState) 29 service->SetMetadata(
28 ->SetMetadata( 30 MediaMetadataSanitizer::sanitizeAndConvertToMojo(metadata));
29 MediaMetadataSanitizer::sanitizeAndConvertToMojo(metadata));
30 } 31 }
31 } 32 }
32 33
33 MediaMetadata* MediaSession::metadata(ScriptState*) const { 34 MediaMetadata* MediaSession::metadata() const {
34 return m_metadata; 35 return m_metadata;
35 } 36 }
36 37
38 const WTF::AtomicString& MediaSession::interfaceName() const {
39 return EventTargetNames::MediaSession;
40 }
41
42 ExecutionContext* MediaSession::getExecutionContext() const {
43 return m_scriptState->getExecutionContext();
44 }
45
37 mojom::blink::MediaSessionService* MediaSession::getService( 46 mojom::blink::MediaSessionService* MediaSession::getService(
38 ScriptState* scriptState) { 47 ScriptState* scriptState) {
39 if (!m_service) { 48 if (!m_service) {
40 InterfaceProvider* interfaceProvider = nullptr; 49 InterfaceProvider* interfaceProvider = nullptr;
41 DCHECK(scriptState->getExecutionContext()->isDocument()) 50 DCHECK(scriptState->getExecutionContext()->isDocument())
42 << "MediaSession::getService() is only available from a frame"; 51 << "MediaSession::getService() is only available from a frame";
43 Document* document = toDocument(scriptState->getExecutionContext()); 52 Document* document = toDocument(scriptState->getExecutionContext());
44 if (document->frame()) 53 if (document->frame())
45 interfaceProvider = document->frame()->interfaceProvider(); 54 interfaceProvider = document->frame()->interfaceProvider();
46 55
47 if (interfaceProvider) 56 if (interfaceProvider)
48 interfaceProvider->getInterface(mojo::GetProxy(&m_service)); 57 interfaceProvider->getInterface(mojo::GetProxy(&m_service));
49 } 58 }
50 return m_service.get(); 59 return m_service.get();
51 } 60 }
52 61
62 // TODO(zqzhang): register listener to MediaSessionService. See
63 // https://crbug.com/xxxxxx
whywhat 2016/10/17 15:32:34 The link gives me 404.
Zhiqiang Zhang (Slow) 2016/10/17 17:19:08 Fixed.
64 #define DEFINE_MEDIA_SESSION_ATTRIBUTE_EVENT_LISTENER(attribute) \
whywhat 2016/10/17 15:32:35 Ditto in the header.
Zhiqiang Zhang (Slow) 2016/10/17 17:19:08 Done.
65 EventListener* MediaSession::on##attribute() { \
66 return getAttributeEventListener(EventTypeNames::attribute); \
67 } \
68 void MediaSession::setOn##attribute(EventListener* listener) { \
69 setAttributeEventListener(EventTypeNames::attribute, listener); \
70 }
71
72 DEFINE_MEDIA_SESSION_ATTRIBUTE_EVENT_LISTENER(play);
73 DEFINE_MEDIA_SESSION_ATTRIBUTE_EVENT_LISTENER(pause);
74 DEFINE_MEDIA_SESSION_ATTRIBUTE_EVENT_LISTENER(playpause);
75 DEFINE_MEDIA_SESSION_ATTRIBUTE_EVENT_LISTENER(previoustrack);
76 DEFINE_MEDIA_SESSION_ATTRIBUTE_EVENT_LISTENER(nexttrack);
77 DEFINE_MEDIA_SESSION_ATTRIBUTE_EVENT_LISTENER(seekforward);
78 DEFINE_MEDIA_SESSION_ATTRIBUTE_EVENT_LISTENER(seekbackward);
79
53 DEFINE_TRACE(MediaSession) { 80 DEFINE_TRACE(MediaSession) {
54 visitor->trace(m_metadata); 81 visitor->trace(m_metadata);
82 EventTargetWithInlineData::trace(visitor);
55 } 83 }
56 84
57 } // namespace blink 85 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698