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/MediaMetadata.h" | 5 #include "modules/mediasession/MediaMetadata.h" |
6 | 6 |
7 #include "core/dom/ExecutionContext.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
| 8 #include "bindings/core/v8/ScriptState.h" |
| 9 #include "bindings/core/v8/ToV8.h" |
8 #include "modules/mediasession/MediaImage.h" | 10 #include "modules/mediasession/MediaImage.h" |
9 #include "modules/mediasession/MediaMetadataInit.h" | 11 #include "modules/mediasession/MediaMetadataInit.h" |
10 #include "modules/mediasession/MediaSession.h" | 12 #include "modules/mediasession/MediaSession.h" |
11 | 13 |
12 namespace blink { | 14 namespace blink { |
13 | 15 |
14 // static | 16 // static |
15 MediaMetadata* MediaMetadata::create(ExecutionContext* context, | 17 MediaMetadata* MediaMetadata::create(ScriptState* scriptState, |
16 const MediaMetadataInit& metadata) { | 18 const MediaMetadataInit& metadata, |
17 return new MediaMetadata(context, metadata); | 19 ExceptionState& exceptionState) { |
| 20 return new MediaMetadata(scriptState, metadata, exceptionState); |
18 } | 21 } |
19 | 22 |
20 MediaMetadata::MediaMetadata(ExecutionContext* context, | 23 MediaMetadata::MediaMetadata(ScriptState* scriptState, |
21 const MediaMetadataInit& metadata) | 24 const MediaMetadataInit& metadata, |
| 25 ExceptionState& exceptionState) |
22 : m_notifySessionTimer(this, &MediaMetadata::notifySessionTimerFired) { | 26 : m_notifySessionTimer(this, &MediaMetadata::notifySessionTimerFired) { |
23 m_title = metadata.title(); | 27 m_title = metadata.title(); |
24 m_artist = metadata.artist(); | 28 m_artist = metadata.artist(); |
25 m_album = metadata.album(); | 29 m_album = metadata.album(); |
26 for (const auto& image : metadata.artwork()) | 30 setArtworkInternal(scriptState, metadata.artwork(), exceptionState); |
27 m_artwork.push_back(MediaImage::create(context, image)); | |
28 } | 31 } |
29 | 32 |
30 String MediaMetadata::title() const { | 33 String MediaMetadata::title() const { |
31 return m_title; | 34 return m_title; |
32 } | 35 } |
33 | 36 |
34 String MediaMetadata::artist() const { | 37 String MediaMetadata::artist() const { |
35 return m_artist; | 38 return m_artist; |
36 } | 39 } |
37 | 40 |
38 String MediaMetadata::album() const { | 41 String MediaMetadata::album() const { |
39 return m_album; | 42 return m_album; |
40 } | 43 } |
41 | 44 |
42 const HeapVector<Member<MediaImage>>& MediaMetadata::artwork() const { | 45 const HeapVector<MediaImage>& MediaMetadata::artwork() const { |
43 return m_artwork; | 46 return m_artwork; |
44 } | 47 } |
45 | 48 |
| 49 Vector<v8::Local<v8::Value>> MediaMetadata::artwork( |
| 50 ScriptState* scriptState) const { |
| 51 Vector<v8::Local<v8::Value>> result(m_artwork.size()); |
| 52 |
| 53 for (size_t i = 0; i < m_artwork.size(); ++i) { |
| 54 result[i] = |
| 55 freezeV8Object(ToV8(m_artwork[i], scriptState), scriptState->isolate()); |
| 56 } |
| 57 |
| 58 return result; |
| 59 } |
| 60 |
46 void MediaMetadata::setTitle(const String& title) { | 61 void MediaMetadata::setTitle(const String& title) { |
47 m_title = title; | 62 m_title = title; |
48 notifySessionAsync(); | 63 notifySessionAsync(); |
49 } | 64 } |
50 | 65 |
51 void MediaMetadata::setArtist(const String& artist) { | 66 void MediaMetadata::setArtist(const String& artist) { |
52 m_artist = artist; | 67 m_artist = artist; |
53 notifySessionAsync(); | 68 notifySessionAsync(); |
54 } | 69 } |
55 | 70 |
56 void MediaMetadata::setAlbum(const String& album) { | 71 void MediaMetadata::setAlbum(const String& album) { |
57 m_album = album; | 72 m_album = album; |
58 notifySessionAsync(); | 73 notifySessionAsync(); |
59 } | 74 } |
60 | 75 |
61 void MediaMetadata::setArtwork(const HeapVector<Member<MediaImage>>& artwork) { | 76 void MediaMetadata::setArtwork(ScriptState* scriptState, |
62 m_artwork = artwork; | 77 const HeapVector<MediaImage>& artwork, |
| 78 ExceptionState& exceptionState) { |
| 79 setArtworkInternal(scriptState, artwork, exceptionState); |
63 notifySessionAsync(); | 80 notifySessionAsync(); |
64 } | 81 } |
65 | 82 |
66 void MediaMetadata::setSession(MediaSession* session) { | 83 void MediaMetadata::setSession(MediaSession* session) { |
67 m_session = session; | 84 m_session = session; |
68 } | 85 } |
69 | 86 |
70 void MediaMetadata::notifySessionAsync() { | 87 void MediaMetadata::notifySessionAsync() { |
71 if (!m_session || m_notifySessionTimer.isActive()) | 88 if (!m_session || m_notifySessionTimer.isActive()) |
72 return; | 89 return; |
73 m_notifySessionTimer.startOneShot(0, BLINK_FROM_HERE); | 90 m_notifySessionTimer.startOneShot(0, BLINK_FROM_HERE); |
74 } | 91 } |
75 | 92 |
76 void MediaMetadata::notifySessionTimerFired(TimerBase*) { | 93 void MediaMetadata::notifySessionTimerFired(TimerBase*) { |
77 if (!m_session) | 94 if (!m_session) |
78 return; | 95 return; |
79 m_session->onMetadataChanged(); | 96 m_session->onMetadataChanged(); |
80 } | 97 } |
81 | 98 |
| 99 void MediaMetadata::setArtworkInternal(ScriptState* scriptState, |
| 100 const HeapVector<MediaImage>& artwork, |
| 101 ExceptionState& exceptionState) { |
| 102 HeapVector<MediaImage> processedArtwork(artwork); |
| 103 |
| 104 for (MediaImage& image : processedArtwork) { |
| 105 KURL url = scriptState->getExecutionContext()->completeURL(image.src()); |
| 106 if (!url.isValid()) { |
| 107 exceptionState.throwTypeError("'" + image.src() + |
| 108 "' can't be resolved to a valid URL."); |
| 109 return; |
| 110 } |
| 111 image.setSrc(url); |
| 112 } |
| 113 |
| 114 DCHECK(!exceptionState.hadException()); |
| 115 m_artwork.swap(processedArtwork); |
| 116 } |
| 117 |
82 DEFINE_TRACE(MediaMetadata) { | 118 DEFINE_TRACE(MediaMetadata) { |
83 visitor->trace(m_artwork); | 119 visitor->trace(m_artwork); |
84 visitor->trace(m_session); | 120 visitor->trace(m_session); |
85 } | 121 } |
86 | 122 |
87 } // namespace blink | 123 } // namespace blink |
OLD | NEW |