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

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

Issue 2426653002: Adding mojo MediaSessionClient to support media controls (Closed)
Patch Set: addressed Anton's 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..40b4cf91ec85a78fe472cd91c28c4203ebed8dc5 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,8 +17,65 @@
namespace blink {
+namespace {
+
+Vector<AtomicString>* createActionEnumToEventNameVec() {
+ Vector<AtomicString>* vec = new Vector<AtomicString>();
haraken 2016/10/17 22:37:05 Use std::unique_ptr. However, why do you need to
Zhiqiang Zhang (Slow) 2016/10/18 14:42:14 It is actually used in DEFINE_THREAD_SAFE_STATIC_L
+ // Must be in the same order. There's a possible HashMap bug that prevents
+ // mapping 0 to String. See https://crbug.com/656795
+ vec->append(EventTypeNames::play);
+ vec->append(EventTypeNames::pause);
+ vec->append(EventTypeNames::playpause);
+ vec->append(EventTypeNames::previoustrack);
+ vec->append(EventTypeNames::nexttrack);
+ vec->append(EventTypeNames::seekforward);
+ vec->append(EventTypeNames::seekbackward);
+
+ return vec;
+}
+
+// Map from MediaSessionAction to EventTypeName.
+const Vector<AtomicString>& getActionEnumToEventNameVec() {
+ DEFINE_THREAD_SAFE_STATIC_LOCAL(Vector<AtomicString>,
+ actionEnumToEventNameVec,
+ createActionEnumToEventNameVec());
+ return actionEnumToEventNameVec;
+}
+
+using StringToActionEnumMap =
+ HashMap<AtomicString, blink::mojom::blink::MediaSessionAction>;
+
+StringToActionEnumMap* createEventNameToActionEnumMap() {
+ StringToActionEnumMap* map = new StringToActionEnumMap();
haraken 2016/10/17 22:37:05 Ditto.
Zhiqiang Zhang (Slow) 2016/10/18 14:42:14 Ditto as the other reply.
+ map->add(EventTypeNames::play, blink::mojom::blink::MediaSessionAction::PLAY);
+ map->add(EventTypeNames::pause,
+ blink::mojom::blink::MediaSessionAction::PAUSE);
+ map->add(EventTypeNames::playpause,
+ blink::mojom::blink::MediaSessionAction::PLAY_PAUSE);
+ map->add(EventTypeNames::previoustrack,
+ blink::mojom::blink::MediaSessionAction::PREVIOUS_TRACK);
+ map->add(EventTypeNames::nexttrack,
+ blink::mojom::blink::MediaSessionAction::NEXT_TRACK);
+ map->add(EventTypeNames::seekforward,
+ blink::mojom::blink::MediaSessionAction::SEEK_FORWARD);
+ map->add(EventTypeNames::seekbackward,
+ blink::mojom::blink::MediaSessionAction::SEEK_BACKWARD);
+
+ return map;
+}
+
+// Map from EventTypeName to MediaSessionAction.
+const StringToActionEnumMap& getEventNameToActionEnumMap() {
+ DEFINE_THREAD_SAFE_STATIC_LOCAL(StringToActionEnumMap,
+ eventNameToActionEnumMap,
+ createEventNameToActionEnumMap());
+ return eventNameToActionEnumMap;
+}
+
+} // anonymous namespace
+
MediaSession::MediaSession(ScriptState* scriptState)
- : m_scriptState(scriptState) {}
+ : m_scriptState(scriptState), m_clientBinding(this) {}
MediaSession* MediaSession::create(ScriptState* scriptState) {
return new MediaSession(scriptState);
@@ -55,6 +113,8 @@ mojom::blink::MediaSessionService* MediaSession::getService(
if (interfaceProvider)
interfaceProvider->getInterface(mojo::GetProxy(&m_service));
+
+ m_service->SetClient(m_clientBinding.CreateInterfacePtrAndBind());
}
return m_service.get();
}
@@ -63,18 +123,46 @@ 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
- return EventTarget::addEventListenerInternal(eventType, listener, options);
+ bool result =
+ EventTarget::addEventListenerInternal(eventType, listener, options);
+
+ const auto& map = getEventNameToActionEnumMap();
+ auto iter = map.find(eventType);
+ if (iter != map.end()) {
+ if (mojom::blink::MediaSessionService* service =
+ getService(m_scriptState.get())) {
+ service->EnableAction(iter->value);
+ }
+ }
+ return result;
}
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
- return EventTarget::removeEventListenerInternal(eventType, listener, options);
+ bool result =
+ EventTarget::removeEventListenerInternal(eventType, listener, options);
+
+ const auto& map = getEventNameToActionEnumMap();
+ auto iter = map.find(eventType);
+ if (iter != map.end()) {
+ if (mojom::blink::MediaSessionService* service =
+ getService(m_scriptState.get())) {
+ service->DisableAction(iter->value);
+ }
+ }
+ return result;
+}
+
+void MediaSession::DidReceivedAction(
+ blink::mojom::blink::MediaSessionAction action) {
+ LOG(INFO) << static_cast<int>(action);
+ const auto& vec = getActionEnumToEventNameVec();
+ if (static_cast<int>(action) >= 0 &&
+ static_cast<size_t>(action) < vec.size()) {
+ dispatchEvent(Event::create(vec[static_cast<int>(action)]));
+ }
}
DEFINE_TRACE(MediaSession) {

Powered by Google App Engine
This is Rietveld 408576698