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