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/CallbackPromiseAdapter.h" | 7 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
| 8 #include "bindings/core/v8/ScriptPromiseResolver.h" | 8 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 9 #include "bindings/core/v8/ScriptState.h" | 9 #include "bindings/core/v8/ScriptState.h" |
| 10 #include "core/dom/DOMException.h" | 10 #include "core/dom/DOMException.h" |
| 11 #include "core/dom/ExceptionCode.h" | 11 #include "core/dom/ExceptionCode.h" |
| 12 #include "core/frame/LocalDOMWindow.h" | 12 #include "core/frame/LocalDOMWindow.h" |
| 13 #include "core/frame/LocalFrame.h" | 13 #include "core/frame/LocalFrame.h" |
| 14 #include "core/loader/FrameLoaderClient.h" | 14 #include "core/loader/FrameLoaderClient.h" |
| 15 #include "modules/mediasession/MediaMetadata.h" | 15 #include "modules/mediasession/MediaMetadata.h" |
| 16 #include "modules/mediasession/MediaSessionError.h" | 16 #include "modules/mediasession/MediaArtwork.h" |
| 17 #include "public/platform/WebIconSizesParser.h" | |
| 18 #include "public/platform/InterfaceProvider.h" | |
| 17 #include <memory> | 19 #include <memory> |
| 18 | 20 |
| 19 namespace blink { | 21 namespace blink { |
| 20 | 22 |
| 21 MediaSession::MediaSession(std::unique_ptr<WebMediaSession> webMediaSession) | 23 namespace { |
| 22 : m_webMediaSession(std::move(webMediaSession)) | 24 |
| 23 { | 25 mojom::blink::MediaMetadataIconPtr ToMojoMediaMetadataIcon( |
| 24 DCHECK(m_webMediaSession); | 26 const MediaArtwork* icon) { |
| 27 DCHECK(icon); | |
| 28 mojom::blink::MediaMetadataIconPtr mojo_icon | |
| 29 = mojom::blink::MediaMetadataIcon::New(); | |
| 30 mojo_icon->src = KURL(ParsedURLString, icon->src()); | |
| 31 mojo_icon->type = icon->type(); | |
| 32 WebVector<WebSize> web_sizes = WebIconSizesParser::parseIconSizes(icon->size s()); | |
| 33 mojo_icon->sizes.resize(web_sizes.size()); | |
| 34 memcpy(mojo_icon->sizes.data(), web_sizes.data(), web_sizes.size() * sizeof( WebSize)); | |
| 35 return mojo_icon; | |
| 25 } | 36 } |
| 26 | 37 |
| 27 MediaSession* MediaSession::create(ExecutionContext* context, ExceptionState& ex ceptionState) | 38 mojom::blink::MediaMetadataPtr ToMojoMediaMetadata( |
| 28 { | 39 const MediaMetadata* metadata) { |
| 29 Document* document = toDocument(context); | 40 mojom::blink::MediaMetadataPtr mojo_metadata; |
| 30 LocalFrame* frame = document->frame(); | 41 if (!metadata) |
| 31 FrameLoaderClient* client = frame->loader().client(); | 42 return mojo_metadata; |
| 32 std::unique_ptr<WebMediaSession> webMediaSession = client->createWebMediaSes sion(); | 43 mojo_metadata = mojom::blink::MediaMetadata::New(); |
| 33 if (!webMediaSession) { | 44 mojo_metadata->title = metadata->title(); |
| 34 exceptionState.throwDOMException(NotSupportedError, "Missing platform im plementation."); | 45 mojo_metadata->artist = metadata->artist(); |
| 35 return nullptr; | 46 mojo_metadata->album = metadata->album(); |
| 47 for (const auto& icon : metadata->artwork()) { | |
| 48 // TODO(zqzhang): do sanitization | |
|
mlamouri (slow - plz ping)
2016/09/29 10:26:40
It would be better if you could do this in this CL
Zhiqiang Zhang (Slow)
2016/10/03 20:21:00
Done.
| |
| 49 mojo_metadata->artwork.append(ToMojoMediaMetadataIcon(icon.get())); | |
| 36 } | 50 } |
| 37 return new MediaSession(std::move(webMediaSession)); | 51 return mojo_metadata; |
| 38 } | 52 } |
| 39 | 53 |
| 40 ScriptPromise MediaSession::activate(ScriptState* scriptState) | 54 } // anonymous namespace |
| 55 | |
| 56 MediaSession::MediaSession() | |
| 41 { | 57 { |
| 42 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | |
| 43 ScriptPromise promise = resolver->promise(); | |
| 44 | |
| 45 m_webMediaSession->activate(new CallbackPromiseAdapter<void, MediaSessionErr or>(resolver)); | |
| 46 return promise; | |
| 47 } | 58 } |
| 48 | 59 |
| 49 ScriptPromise MediaSession::deactivate(ScriptState* scriptState) | 60 MediaSession* MediaSession::create() |
| 50 { | 61 { |
| 51 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 62 return new MediaSession(); |
| 52 ScriptPromise promise = resolver->promise(); | |
| 53 | |
| 54 m_webMediaSession->deactivate(new CallbackPromiseAdapter<void, void>(resolve r)); | |
| 55 return promise; | |
| 56 } | 63 } |
| 57 | 64 |
| 58 void MediaSession::setMetadata(MediaMetadata* metadata) | 65 void MediaSession::setMetadata(ScriptState* scriptState, MediaMetadata* metadata ) |
| 59 { | 66 { |
| 60 m_metadata = metadata; | 67 if (getService(scriptState)) |
| 61 if (metadata) { | 68 getService(scriptState)->SetMetadata(ToMojoMediaMetadata(metadata)); |
| 62 WebMediaMetadata webMetadata = (WebMediaMetadata) *metadata; | |
| 63 m_webMediaSession->setMetadata(&webMetadata); | |
| 64 } else { | |
| 65 m_webMediaSession->setMetadata(nullptr); | |
| 66 } | |
| 67 } | 69 } |
| 68 | 70 |
| 69 MediaMetadata* MediaSession::metadata() const | 71 MediaMetadata* MediaSession::metadata(ScriptState*) const |
| 70 { | 72 { |
| 71 return m_metadata; | 73 return m_metadata; |
| 72 } | 74 } |
| 73 | 75 |
| 76 mojom::blink::MediaSessionService* MediaSession::getService(ScriptState* scriptS tate) | |
| 77 { | |
| 78 if (!m_service) { | |
| 79 InterfaceProvider* interfaceProvider = nullptr; | |
| 80 if (scriptState->getExecutionContext()->isDocument()) { | |
| 81 Document* document = toDocument(scriptState->getExecutionContext()); | |
| 82 if (document->frame()) | |
| 83 interfaceProvider = document->frame()->interfaceProvider(); | |
| 84 } else { | |
| 85 // Can only getService from a frame!!! | |
|
mlamouri (slow - plz ping)
2016/09/29 10:26:40
I think a DCHECK would be better.
Zhiqiang Zhang (Slow)
2016/09/30 19:56:15
Done.
| |
| 86 } | |
| 87 | |
| 88 if (interfaceProvider) | |
| 89 interfaceProvider->getInterface(mojo::GetProxy(&m_service)); | |
| 90 } | |
| 91 return m_service.get(); | |
| 92 } | |
| 93 | |
| 74 DEFINE_TRACE(MediaSession) | 94 DEFINE_TRACE(MediaSession) |
| 75 { | 95 { |
| 76 visitor->trace(m_metadata); | 96 visitor->trace(m_metadata); |
| 77 } | 97 } |
| 78 | 98 |
| 79 } // namespace blink | 99 } // namespace blink |
| OLD | NEW |