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/MediaArtwork.h" |
15 #include "modules/mediasession/MediaMetadata.h" | 16 #include "modules/mediasession/MediaMetadata.h" |
16 #include "modules/mediasession/MediaSessionError.h" | 17 #include "public/platform/InterfaceProvider.h" |
| 18 #include "public/platform/WebIconSizesParser.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 // TODO(zqzhang): Use mojo typemap to simplify the conversion. |
22 : m_webMediaSession(std::move(webMediaSession)) { | 24 // See https://crbug.com/649630 |
23 DCHECK(m_webMediaSession); | 25 |
| 26 namespace { |
| 27 |
| 28 mojom::blink::MediaImagePtr ToMojoMediaMetadataIcon(const MediaArtwork* icon) { |
| 29 DCHECK(icon); |
| 30 mojom::blink::MediaImagePtr mojoIcon = mojom::blink::MediaImage::New(); |
| 31 mojoIcon->src = KURL(ParsedURLString, icon->src()); |
| 32 mojoIcon->type = icon->type(); |
| 33 WebVector<WebSize> webSizes = |
| 34 WebIconSizesParser::parseIconSizes(icon->sizes()); |
| 35 mojoIcon->sizes.resize(webSizes.size()); |
| 36 memcpy(mojoIcon->sizes.data(), webSizes.data(), |
| 37 webSizes.size() * sizeof(WebSize)); |
| 38 return mojoIcon; |
24 } | 39 } |
25 | 40 |
26 MediaSession* MediaSession::create(ExecutionContext* context, | 41 mojom::blink::MediaMetadataPtr ToMojoMediaMetadata( |
27 ExceptionState& exceptionState) { | 42 const MediaMetadata* metadata) { |
28 Document* document = toDocument(context); | 43 mojom::blink::MediaMetadataPtr mojoMetadata; |
29 LocalFrame* frame = document->frame(); | 44 if (!metadata) |
30 FrameLoaderClient* client = frame->loader().client(); | 45 return mojoMetadata; |
31 std::unique_ptr<WebMediaSession> webMediaSession = | 46 mojoMetadata = mojom::blink::MediaMetadata::New(); |
32 client->createWebMediaSession(); | 47 mojoMetadata->title = metadata->title(); |
33 if (!webMediaSession) { | 48 mojoMetadata->artist = metadata->artist(); |
34 exceptionState.throwDOMException(NotSupportedError, | 49 mojoMetadata->album = metadata->album(); |
35 "Missing platform implementation."); | 50 for (const auto& icon : metadata->artwork()) { |
36 return nullptr; | 51 // TODO(zqzhang): do sanitization |
| 52 mojoMetadata->artwork.append(ToMojoMediaMetadataIcon(icon.get())); |
37 } | 53 } |
38 return new MediaSession(std::move(webMediaSession)); | 54 return mojoMetadata; |
39 } | 55 } |
40 | 56 |
41 ScriptPromise MediaSession::activate(ScriptState* scriptState) { | 57 } // anonymous namespace |
42 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | |
43 ScriptPromise promise = resolver->promise(); | |
44 | 58 |
45 m_webMediaSession->activate( | 59 MediaSession::MediaSession() = default; |
46 new CallbackPromiseAdapter<void, MediaSessionError>(resolver)); | 60 |
47 return promise; | 61 MediaSession* MediaSession::create() { |
| 62 return new MediaSession(); |
48 } | 63 } |
49 | 64 |
50 ScriptPromise MediaSession::deactivate(ScriptState* scriptState) { | 65 void MediaSession::setMetadata(ScriptState* scriptState, |
51 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 66 MediaMetadata* metadata) { |
52 ScriptPromise promise = resolver->promise(); | 67 if (getService(scriptState)) |
53 | 68 getService(scriptState)->SetMetadata(ToMojoMediaMetadata(metadata)); |
54 m_webMediaSession->deactivate( | |
55 new CallbackPromiseAdapter<void, void>(resolver)); | |
56 return promise; | |
57 } | 69 } |
58 | 70 |
59 void MediaSession::setMetadata(MediaMetadata* metadata) { | 71 MediaMetadata* MediaSession::metadata(ScriptState*) const { |
60 m_metadata = metadata; | 72 return m_metadata; |
61 if (metadata) { | |
62 WebMediaMetadata webMetadata = (WebMediaMetadata)*metadata; | |
63 m_webMediaSession->setMetadata(&webMetadata); | |
64 } else { | |
65 m_webMediaSession->setMetadata(nullptr); | |
66 } | |
67 } | 73 } |
68 | 74 |
69 MediaMetadata* MediaSession::metadata() const { | 75 mojom::blink::MediaSession* MediaSession::getService(ScriptState* scriptState) { |
70 return m_metadata; | 76 if (!m_service) { |
| 77 InterfaceProvider* interfaceProvider = nullptr; |
| 78 DCHECK(scriptState->getExecutionContext()->isDocument()) |
| 79 << "MediaSession::getService() is only available from a frame"; |
| 80 Document* document = toDocument(scriptState->getExecutionContext()); |
| 81 if (document->frame()) |
| 82 interfaceProvider = document->frame()->interfaceProvider(); |
| 83 |
| 84 if (interfaceProvider) |
| 85 interfaceProvider->getInterface(mojo::GetProxy(&m_service)); |
| 86 } |
| 87 return m_service.get(); |
71 } | 88 } |
72 | 89 |
73 DEFINE_TRACE(MediaSession) { | 90 DEFINE_TRACE(MediaSession) { |
74 visitor->trace(m_metadata); | 91 visitor->trace(m_metadata); |
75 } | 92 } |
76 | 93 |
77 } // namespace blink | 94 } // namespace blink |
OLD | NEW |