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 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 mojoIcon = mojom::blink::MediaMetadataIco
n::New(); |
| 29 mojoIcon->src = KURL(ParsedURLString, icon->src()); |
| 30 mojoIcon->type = icon->type(); |
| 31 WebVector<WebSize> webSizes = WebIconSizesParser::parseIconSizes(icon->sizes
()); |
| 32 mojoIcon->sizes.resize(webSizes.size()); |
| 33 memcpy(mojoIcon->sizes.data(), webSizes.data(), webSizes.size() * sizeof(Web
Size)); |
| 34 return mojoIcon; |
25 } | 35 } |
26 | 36 |
27 MediaSession* MediaSession::create(ExecutionContext* context, ExceptionState& ex
ceptionState) | 37 mojom::blink::MediaMetadataPtr ToMojoMediaMetadata( |
28 { | 38 const MediaMetadata* metadata) { |
29 Document* document = toDocument(context); | 39 mojom::blink::MediaMetadataPtr mojoMetadata; |
30 LocalFrame* frame = document->frame(); | 40 if (!metadata) |
31 FrameLoaderClient* client = frame->loader().client(); | 41 return mojoMetadata; |
32 std::unique_ptr<WebMediaSession> webMediaSession = client->createWebMediaSes
sion(); | 42 mojoMetadata = mojom::blink::MediaMetadata::New(); |
33 if (!webMediaSession) { | 43 mojoMetadata->title = metadata->title(); |
34 exceptionState.throwDOMException(NotSupportedError, "Missing platform im
plementation."); | 44 mojoMetadata->artist = metadata->artist(); |
35 return nullptr; | 45 mojoMetadata->album = metadata->album(); |
| 46 for (const auto& icon : metadata->artwork()) { |
| 47 // TODO(zqzhang): do sanitization |
| 48 mojoMetadata->artwork.append(ToMojoMediaMetadataIcon(icon.get())); |
36 } | 49 } |
37 return new MediaSession(std::move(webMediaSession)); | 50 return mojoMetadata; |
38 } | 51 } |
39 | 52 |
40 ScriptPromise MediaSession::activate(ScriptState* scriptState) | 53 } // anonymous namespace |
| 54 |
| 55 MediaSession::MediaSession() = default; |
| 56 |
| 57 MediaSession* MediaSession::create() |
41 { | 58 { |
42 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 59 return new MediaSession(); |
43 ScriptPromise promise = resolver->promise(); | |
44 | |
45 m_webMediaSession->activate(new CallbackPromiseAdapter<void, MediaSessionErr
or>(resolver)); | |
46 return promise; | |
47 } | 60 } |
48 | 61 |
49 ScriptPromise MediaSession::deactivate(ScriptState* scriptState) | 62 void MediaSession::setMetadata(ScriptState* scriptState, MediaMetadata* metadata
) |
50 { | 63 { |
51 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 64 if (getService(scriptState)) |
52 ScriptPromise promise = resolver->promise(); | 65 getService(scriptState)->SetMetadata(ToMojoMediaMetadata(metadata)); |
53 | |
54 m_webMediaSession->deactivate(new CallbackPromiseAdapter<void, void>(resolve
r)); | |
55 return promise; | |
56 } | 66 } |
57 | 67 |
58 void MediaSession::setMetadata(MediaMetadata* metadata) | 68 MediaMetadata* MediaSession::metadata(ScriptState*) const |
59 { | 69 { |
60 m_metadata = metadata; | 70 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 } | 71 } |
68 | 72 |
69 MediaMetadata* MediaSession::metadata() const | 73 mojom::blink::MediaSessionService* MediaSession::getService(ScriptState* scriptS
tate) |
70 { | 74 { |
71 return m_metadata; | 75 if (!m_service) { |
| 76 InterfaceProvider* interfaceProvider = nullptr; |
| 77 DCHECK(scriptState->getExecutionContext()->isDocument()) << "MediaSessio
n::getService() is only available from a frame"; |
| 78 Document* document = toDocument(scriptState->getExecutionContext()); |
| 79 if (document->frame()) |
| 80 interfaceProvider = document->frame()->interfaceProvider(); |
| 81 |
| 82 if (interfaceProvider) |
| 83 interfaceProvider->getInterface(mojo::GetProxy(&m_service)); |
| 84 } |
| 85 return m_service.get(); |
72 } | 86 } |
73 | 87 |
74 DEFINE_TRACE(MediaSession) | 88 DEFINE_TRACE(MediaSession) |
75 { | 89 { |
76 visitor->trace(m_metadata); | 90 visitor->trace(m_metadata); |
77 } | 91 } |
78 | 92 |
79 } // namespace blink | 93 } // namespace blink |
OLD | NEW |