Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef TrackListBase_h | |
| 6 #define TrackListBase_h | |
| 7 | |
| 8 #include "core/events/EventTarget.h" | |
| 9 | |
| 10 #include "core/html/HTMLMediaElement.h" | |
| 11 #include "core/html/track/TrackEvent.h" | |
| 12 | |
| 13 namespace WebCore { | |
| 14 | |
| 15 template<class T> | |
| 16 class TrackListBase : public RefCounted<TrackListBase<T> >, public EventTargetWi thInlineData { | |
| 17 REFCOUNTED_EVENT_TARGET(TrackListBase<T>); | |
| 18 | |
| 19 public: | |
| 20 explicit TrackListBase(HTMLMediaElement* mediaElement) | |
| 21 : m_mediaElement(mediaElement) | |
| 22 { | |
| 23 } | |
| 24 | |
| 25 virtual ~TrackListBase() | |
| 26 { | |
| 27 ASSERT(m_tracks.isEmpty()); | |
| 28 ASSERT(!m_mediaElement); | |
| 29 } | |
| 30 | |
| 31 unsigned length() const { return m_tracks.size(); } | |
| 32 PassRefPtr<T> anonymousIndexedGetter(unsigned index) const | |
| 33 { | |
| 34 if (index >= m_tracks.size()) | |
| 35 return nullptr; | |
| 36 return m_tracks[index]; | |
| 37 } | |
| 38 | |
| 39 PassRefPtr<T> getTrackById(const String& id) const | |
| 40 { | |
| 41 for (unsigned i = 0; i < m_tracks.size(); ++i) { | |
| 42 if (m_tracks[i]->id() == id) | |
| 43 return m_tracks[i]; | |
| 44 } | |
| 45 | |
| 46 return nullptr; | |
| 47 } | |
| 48 | |
| 49 DEFINE_ATTRIBUTE_EVENT_LISTENER(change); | |
| 50 DEFINE_ATTRIBUTE_EVENT_LISTENER(addtrack); | |
| 51 DEFINE_ATTRIBUTE_EVENT_LISTENER(removetrack); | |
| 52 | |
| 53 // EventTarget interface | |
| 54 virtual ExecutionContext* executionContext() const OVERRIDE | |
| 55 { | |
| 56 if (m_mediaElement) | |
| 57 return m_mediaElement->executionContext(); | |
| 58 return 0; | |
| 59 } | |
| 60 | |
| 61 void shutdown() | |
| 62 { | |
| 63 removeAll(); | |
| 64 m_mediaElement = 0; | |
| 65 } | |
| 66 | |
| 67 void add(PassRefPtr<T> track) | |
| 68 { | |
| 69 ASSERT(track); | |
|
acolwell GONE FROM CHROMIUM
2014/05/16 19:47:56
nit: No need for this assert since the code would
| |
| 70 track->setMediaElement(m_mediaElement); | |
| 71 m_tracks.append(track.get()); | |
| 72 scheduleTrackEvent(EventTypeNames::addtrack, track); | |
| 73 } | |
| 74 | |
| 75 void remove(blink::WebTrackId webId) | |
| 76 { | |
| 77 for (unsigned i = 0; i < m_tracks.size(); ++i) { | |
| 78 if (m_tracks[i]->webId() != webId) | |
| 79 continue; | |
| 80 | |
| 81 m_tracks[i]->setMediaElement(0); | |
| 82 scheduleTrackEvent(EventTypeNames::removetrack, m_tracks[i]); | |
| 83 m_tracks.remove(i); | |
| 84 return; | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 void removeAll() | |
| 89 { | |
| 90 for (unsigned i = 0; i < m_tracks.size(); ++i) | |
| 91 m_tracks[i]->setMediaElement(0); | |
| 92 | |
| 93 m_tracks.clear(); | |
| 94 } | |
| 95 | |
| 96 void scheduleChangeEvent() | |
| 97 { | |
| 98 EventInit initializer; | |
| 99 initializer.bubbles = false; | |
| 100 initializer.cancelable = false; | |
| 101 RefPtr<Event> event = Event::create(EventTypeNames::change, initializer) ; | |
| 102 event->setTarget(this); | |
| 103 m_mediaElement->scheduleEvent(event); | |
| 104 } | |
| 105 | |
| 106 private: | |
| 107 void scheduleTrackEvent(const AtomicString& eventName, PassRefPtr<T> track) | |
| 108 { | |
| 109 TrackEventInit initializer; | |
| 110 initializer.track = track; | |
| 111 initializer.bubbles = false; | |
| 112 initializer.cancelable = false; | |
| 113 RefPtr<Event> event = TrackEvent::create(eventName, initializer); | |
| 114 event->setTarget(this); | |
| 115 m_mediaElement->scheduleEvent(event); | |
| 116 } | |
| 117 | |
| 118 Vector<RefPtr<T> > m_tracks; | |
|
acolwell GONE FROM CHROMIUM
2014/05/16 19:47:56
I think this needs to be WillBeHeapVector<RefPtrWi
| |
| 119 HTMLMediaElement* m_mediaElement; | |
|
acolwell GONE FROM CHROMIUM
2014/05/16 19:47:56
I think this should be RawPtrWillBeMember like in
| |
| 120 }; | |
| 121 | |
| 122 } | |
| 123 | |
| 124 #endif | |
| OLD | NEW |