Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/events/Event.h" | |
| 10 #include "core/frame/LocalFrame.h" | 11 #include "core/frame/LocalFrame.h" |
| 11 #include "modules/EventTargetModules.h" | 12 #include "modules/EventTargetModules.h" |
| 12 #include "modules/mediasession/MediaMetadata.h" | 13 #include "modules/mediasession/MediaMetadata.h" |
| 13 #include "modules/mediasession/MediaMetadataSanitizer.h" | 14 #include "modules/mediasession/MediaMetadataSanitizer.h" |
| 14 #include "public/platform/InterfaceProvider.h" | 15 #include "public/platform/InterfaceProvider.h" |
| 15 #include <memory> | 16 #include <memory> |
| 16 | 17 |
| 17 namespace blink { | 18 namespace blink { |
| 18 | 19 |
| 20 namespace { | |
| 21 | |
| 22 Vector<AtomicString>* createActionEnumToEventNameVec() { | |
| 23 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
| |
| 24 // Must be in the same order. There's a possible HashMap bug that prevents | |
| 25 // mapping 0 to String. See https://crbug.com/656795 | |
| 26 vec->append(EventTypeNames::play); | |
| 27 vec->append(EventTypeNames::pause); | |
| 28 vec->append(EventTypeNames::playpause); | |
| 29 vec->append(EventTypeNames::previoustrack); | |
| 30 vec->append(EventTypeNames::nexttrack); | |
| 31 vec->append(EventTypeNames::seekforward); | |
| 32 vec->append(EventTypeNames::seekbackward); | |
| 33 | |
| 34 return vec; | |
| 35 } | |
| 36 | |
| 37 // Map from MediaSessionAction to EventTypeName. | |
| 38 const Vector<AtomicString>& getActionEnumToEventNameVec() { | |
| 39 DEFINE_THREAD_SAFE_STATIC_LOCAL(Vector<AtomicString>, | |
| 40 actionEnumToEventNameVec, | |
| 41 createActionEnumToEventNameVec()); | |
| 42 return actionEnumToEventNameVec; | |
| 43 } | |
| 44 | |
| 45 using StringToActionEnumMap = | |
| 46 HashMap<AtomicString, blink::mojom::blink::MediaSessionAction>; | |
| 47 | |
| 48 StringToActionEnumMap* createEventNameToActionEnumMap() { | |
| 49 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.
| |
| 50 map->add(EventTypeNames::play, blink::mojom::blink::MediaSessionAction::PLAY); | |
| 51 map->add(EventTypeNames::pause, | |
| 52 blink::mojom::blink::MediaSessionAction::PAUSE); | |
| 53 map->add(EventTypeNames::playpause, | |
| 54 blink::mojom::blink::MediaSessionAction::PLAY_PAUSE); | |
| 55 map->add(EventTypeNames::previoustrack, | |
| 56 blink::mojom::blink::MediaSessionAction::PREVIOUS_TRACK); | |
| 57 map->add(EventTypeNames::nexttrack, | |
| 58 blink::mojom::blink::MediaSessionAction::NEXT_TRACK); | |
| 59 map->add(EventTypeNames::seekforward, | |
| 60 blink::mojom::blink::MediaSessionAction::SEEK_FORWARD); | |
| 61 map->add(EventTypeNames::seekbackward, | |
| 62 blink::mojom::blink::MediaSessionAction::SEEK_BACKWARD); | |
| 63 | |
| 64 return map; | |
| 65 } | |
| 66 | |
| 67 // Map from EventTypeName to MediaSessionAction. | |
| 68 const StringToActionEnumMap& getEventNameToActionEnumMap() { | |
| 69 DEFINE_THREAD_SAFE_STATIC_LOCAL(StringToActionEnumMap, | |
| 70 eventNameToActionEnumMap, | |
| 71 createEventNameToActionEnumMap()); | |
| 72 return eventNameToActionEnumMap; | |
| 73 } | |
| 74 | |
| 75 } // anonymous namespace | |
| 76 | |
| 19 MediaSession::MediaSession(ScriptState* scriptState) | 77 MediaSession::MediaSession(ScriptState* scriptState) |
| 20 : m_scriptState(scriptState) {} | 78 : m_scriptState(scriptState), m_clientBinding(this) {} |
| 21 | 79 |
| 22 MediaSession* MediaSession::create(ScriptState* scriptState) { | 80 MediaSession* MediaSession::create(ScriptState* scriptState) { |
| 23 return new MediaSession(scriptState); | 81 return new MediaSession(scriptState); |
| 24 } | 82 } |
| 25 | 83 |
| 26 void MediaSession::setMetadata(MediaMetadata* metadata) { | 84 void MediaSession::setMetadata(MediaMetadata* metadata) { |
| 27 if (mojom::blink::MediaSessionService* service = | 85 if (mojom::blink::MediaSessionService* service = |
| 28 getService(m_scriptState.get())) { | 86 getService(m_scriptState.get())) { |
| 29 service->SetMetadata( | 87 service->SetMetadata( |
| 30 MediaMetadataSanitizer::sanitizeAndConvertToMojo(metadata)); | 88 MediaMetadataSanitizer::sanitizeAndConvertToMojo(metadata)); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 48 if (!m_service) { | 106 if (!m_service) { |
| 49 InterfaceProvider* interfaceProvider = nullptr; | 107 InterfaceProvider* interfaceProvider = nullptr; |
| 50 DCHECK(scriptState->getExecutionContext()->isDocument()) | 108 DCHECK(scriptState->getExecutionContext()->isDocument()) |
| 51 << "MediaSession::getService() is only available from a frame"; | 109 << "MediaSession::getService() is only available from a frame"; |
| 52 Document* document = toDocument(scriptState->getExecutionContext()); | 110 Document* document = toDocument(scriptState->getExecutionContext()); |
| 53 if (document->frame()) | 111 if (document->frame()) |
| 54 interfaceProvider = document->frame()->interfaceProvider(); | 112 interfaceProvider = document->frame()->interfaceProvider(); |
| 55 | 113 |
| 56 if (interfaceProvider) | 114 if (interfaceProvider) |
| 57 interfaceProvider->getInterface(mojo::GetProxy(&m_service)); | 115 interfaceProvider->getInterface(mojo::GetProxy(&m_service)); |
| 116 | |
| 117 m_service->SetClient(m_clientBinding.CreateInterfacePtrAndBind()); | |
| 58 } | 118 } |
| 59 return m_service.get(); | 119 return m_service.get(); |
| 60 } | 120 } |
| 61 | 121 |
| 62 bool MediaSession::addEventListenerInternal( | 122 bool MediaSession::addEventListenerInternal( |
| 63 const AtomicString& eventType, | 123 const AtomicString& eventType, |
| 64 EventListener* listener, | 124 EventListener* listener, |
| 65 const AddEventListenerOptionsResolved& options) { | 125 const AddEventListenerOptionsResolved& options) { |
| 66 // TODO(zqzhang): Notify MediaSessionService the handler has been set. See | 126 bool result = |
| 67 // https://crbug.com/656563 | 127 EventTarget::addEventListenerInternal(eventType, listener, options); |
| 68 return EventTarget::addEventListenerInternal(eventType, listener, options); | 128 |
| 129 const auto& map = getEventNameToActionEnumMap(); | |
| 130 auto iter = map.find(eventType); | |
| 131 if (iter != map.end()) { | |
| 132 if (mojom::blink::MediaSessionService* service = | |
| 133 getService(m_scriptState.get())) { | |
| 134 service->EnableAction(iter->value); | |
| 135 } | |
| 136 } | |
| 137 return result; | |
| 69 } | 138 } |
| 70 | 139 |
| 71 bool MediaSession::removeEventListenerInternal( | 140 bool MediaSession::removeEventListenerInternal( |
| 72 const AtomicString& eventType, | 141 const AtomicString& eventType, |
| 73 const EventListener* listener, | 142 const EventListener* listener, |
| 74 const EventListenerOptions& options) { | 143 const EventListenerOptions& options) { |
| 75 // TODO(zqzhang): Notify MediaSessionService the handler has been unset. See | 144 bool result = |
| 76 // https://crbug.com/656563 | 145 EventTarget::removeEventListenerInternal(eventType, listener, options); |
| 77 return EventTarget::removeEventListenerInternal(eventType, listener, options); | 146 |
| 147 const auto& map = getEventNameToActionEnumMap(); | |
| 148 auto iter = map.find(eventType); | |
| 149 if (iter != map.end()) { | |
| 150 if (mojom::blink::MediaSessionService* service = | |
| 151 getService(m_scriptState.get())) { | |
| 152 service->DisableAction(iter->value); | |
| 153 } | |
| 154 } | |
| 155 return result; | |
| 156 } | |
| 157 | |
| 158 void MediaSession::DidReceivedAction( | |
| 159 blink::mojom::blink::MediaSessionAction action) { | |
| 160 LOG(INFO) << static_cast<int>(action); | |
| 161 const auto& vec = getActionEnumToEventNameVec(); | |
| 162 if (static_cast<int>(action) >= 0 && | |
| 163 static_cast<size_t>(action) < vec.size()) { | |
| 164 dispatchEvent(Event::create(vec[static_cast<int>(action)])); | |
| 165 } | |
| 78 } | 166 } |
| 79 | 167 |
| 80 DEFINE_TRACE(MediaSession) { | 168 DEFINE_TRACE(MediaSession) { |
| 81 visitor->trace(m_metadata); | 169 visitor->trace(m_metadata); |
| 82 EventTargetWithInlineData::trace(visitor); | 170 EventTargetWithInlineData::trace(visitor); |
| 83 } | 171 } |
| 84 | 172 |
| 85 } // namespace blink | 173 } // namespace blink |
| OLD | NEW |