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); |
| 70 m_tracks.append(track.get()); |
| 71 scheduleTrackEvent(EventTypeNames::addtrack, track); |
| 72 } |
| 73 |
| 74 void remove(const String& id) |
| 75 { |
| 76 for (unsigned i = 0; i < m_tracks.size(); ++i) { |
| 77 if (m_tracks[i]->id() != id) |
| 78 continue; |
| 79 |
| 80 m_tracks[i]->setMediaElement(0); |
| 81 scheduleTrackEvent(EventTypeNames::removetrack, m_tracks[i]); |
| 82 m_tracks.remove(i); |
| 83 return; |
| 84 } |
| 85 } |
| 86 |
| 87 void removeAll() |
| 88 { |
| 89 for (unsigned i = 0; i < m_tracks.size(); ++i) |
| 90 m_tracks[i]->setMediaElement(0); |
| 91 |
| 92 m_tracks.clear(); |
| 93 } |
| 94 |
| 95 void scheduleChangeEvent() |
| 96 { |
| 97 EventInit initializer; |
| 98 initializer.bubbles = false; |
| 99 initializer.cancelable = false; |
| 100 RefPtr<Event> event = Event::create(EventTypeNames::change, initializer)
; |
| 101 event->setTarget(this); |
| 102 m_mediaElement->scheduleEvent(event); |
| 103 } |
| 104 |
| 105 private: |
| 106 void scheduleTrackEvent(const AtomicString& eventName, PassRefPtr<T> track) |
| 107 { |
| 108 TrackEventInit initializer; |
| 109 initializer.track = track; |
| 110 initializer.bubbles = false; |
| 111 initializer.cancelable = false; |
| 112 RefPtr<Event> event = TrackEvent::create(eventName, initializer); |
| 113 event->setTarget(this); |
| 114 m_mediaElement->scheduleEvent(event); |
| 115 } |
| 116 |
| 117 |
| 118 Vector<RefPtr<T> > m_tracks; |
| 119 HTMLMediaElement* m_mediaElement; |
| 120 }; |
| 121 |
| 122 } |
| 123 |
| 124 #endif |
OLD | NEW |