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

Unified Diff: third_party/WebKit/Source/modules/mediasession/MediaSession.cpp

Issue 2426653002: Adding mojo MediaSessionClient to support media controls (Closed)
Patch Set: addressing more comments 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/modules/mediasession/MediaSession.cpp
diff --git a/third_party/WebKit/Source/modules/mediasession/MediaSession.cpp b/third_party/WebKit/Source/modules/mediasession/MediaSession.cpp
index 0afbb0d8c1483b93121fc7f133fb10a5b3177c1b..209c161c20f754623fcbdb582a1cf7135c6aeb91 100644
--- a/third_party/WebKit/Source/modules/mediasession/MediaSession.cpp
+++ b/third_party/WebKit/Source/modules/mediasession/MediaSession.cpp
@@ -7,6 +7,7 @@
#include "bindings/core/v8/ScriptState.h"
#include "core/dom/Document.h"
#include "core/dom/ExecutionContext.h"
+#include "core/events/Event.h"
#include "core/frame/LocalFrame.h"
#include "modules/EventTargetModules.h"
#include "modules/mediasession/MediaMetadata.h"
@@ -16,13 +17,67 @@
namespace blink {
+namespace {
+
+using ::blink::mojom::blink::MediaSessionAction;
+
+const AtomicString& mojomActionToEventName(MediaSessionAction action) {
+ DEFINE_STATIC_LOCAL(AtomicString, emptyString, (""));
+
+ switch (action) {
+ case MediaSessionAction::PLAY:
+ return EventTypeNames::play;
+ case MediaSessionAction::PAUSE:
+ return EventTypeNames::pause;
+ case MediaSessionAction::PLAY_PAUSE:
+ return EventTypeNames::playpause;
+ case MediaSessionAction::PREVIOUS_TRACK:
+ return EventTypeNames::previoustrack;
+ case MediaSessionAction::NEXT_TRACK:
+ return EventTypeNames::nexttrack;
+ case MediaSessionAction::SEEK_FORWARD:
+ return EventTypeNames::seekforward;
+ case MediaSessionAction::SEEK_BACKWARD:
+ return EventTypeNames::seekbackward;
+ default:
+ NOTREACHED();
+ }
+ return emptyString;
haraken 2016/10/19 12:58:05 Return WTF::emptyString() ?
Zhiqiang Zhang (Slow) 2016/10/21 14:52:45 Actually WTF::emptyAtom :)
+}
+
+MediaSessionAction eventNameToMojomAction(const AtomicString& eventName) {
+ if (EventTypeNames::play == eventName)
+ return MediaSessionAction::PLAY;
+ if (EventTypeNames::pause == eventName)
+ return MediaSessionAction::PAUSE;
+ if (EventTypeNames::playpause == eventName)
+ return MediaSessionAction::PLAY_PAUSE;
+ if (EventTypeNames::previoustrack == eventName)
+ return MediaSessionAction::PREVIOUS_TRACK;
+ if (EventTypeNames::nexttrack == eventName)
+ return MediaSessionAction::NEXT_TRACK;
+ if (EventTypeNames::seekforward == eventName)
+ return MediaSessionAction::SEEK_FORWARD;
+ if (EventTypeNames::seekbackward == eventName)
+ return MediaSessionAction::SEEK_BACKWARD;
+
+ NOTREACHED();
+ 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.
+}
+
+} // anonymous namespace
+
MediaSession::MediaSession(ScriptState* scriptState)
- : m_scriptState(scriptState) {}
+ : m_scriptState(scriptState), m_clientBinding(this) {}
MediaSession* MediaSession::create(ScriptState* scriptState) {
return new MediaSession(scriptState);
}
+void MediaSession::dispose() {
+ m_clientBinding.Close();
+}
+
void MediaSession::setMetadata(MediaMetadata* metadata) {
if (mojom::blink::MediaSessionService* service =
getService(m_scriptState.get())) {
@@ -45,17 +100,23 @@ ExecutionContext* MediaSession::getExecutionContext() const {
mojom::blink::MediaSessionService* MediaSession::getService(
ScriptState* scriptState) {
- if (!m_service) {
- InterfaceProvider* interfaceProvider = nullptr;
- DCHECK(scriptState->getExecutionContext()->isDocument())
- << "MediaSession::getService() is only available from a frame";
- Document* document = toDocument(scriptState->getExecutionContext());
- if (document->frame())
- interfaceProvider = document->frame()->interfaceProvider();
-
- if (interfaceProvider)
- interfaceProvider->getInterface(mojo::GetProxy(&m_service));
- }
+ if (m_service)
+ return m_service.get();
+
+ DCHECK(scriptState->getExecutionContext()->isDocument())
+ << "MediaSession::getService() is only available from a frame";
+ Document* document = toDocument(scriptState->getExecutionContext());
+ if (!document->frame())
+ return nullptr;
+
+ InterfaceProvider* interfaceProvider = document->frame()->interfaceProvider();
+ if (!interfaceProvider)
+ return nullptr;
+
+ interfaceProvider->getInterface(mojo::GetProxy(&m_service));
+ if (m_service.get())
+ m_service->SetClient(m_clientBinding.CreateInterfacePtrAndBind());
+
return m_service.get();
}
@@ -63,8 +124,10 @@ bool MediaSession::addEventListenerInternal(
const AtomicString& eventType,
EventListener* listener,
const AddEventListenerOptionsResolved& options) {
- // TODO(zqzhang): Notify MediaSessionService the handler has been set. See
- // https://crbug.com/656563
+ if (mojom::blink::MediaSessionService* service =
+ getService(m_scriptState.get())) {
+ service->EnableAction(eventNameToMojomAction(eventType));
+ }
return EventTarget::addEventListenerInternal(eventType, listener, options);
}
@@ -72,11 +135,18 @@ bool MediaSession::removeEventListenerInternal(
const AtomicString& eventType,
const EventListener* listener,
const EventListenerOptions& options) {
- // TODO(zqzhang): Notify MediaSessionService the handler has been unset. See
- // https://crbug.com/656563
+ if (mojom::blink::MediaSessionService* service =
+ getService(m_scriptState.get())) {
+ service->DisableAction(eventNameToMojomAction(eventType));
+ }
return EventTarget::removeEventListenerInternal(eventType, listener, options);
}
+void MediaSession::DidReceiveAction(
+ blink::mojom::blink::MediaSessionAction action) {
+ dispatchEvent(Event::create(mojomActionToEventName(action)));
+}
+
DEFINE_TRACE(MediaSession) {
visitor->trace(m_metadata);
EventTargetWithInlineData::trace(visitor);

Powered by Google App Engine
This is Rietveld 408576698