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 RefCountedWillBeRefCountedGarbageCollected<TrackLis
tBase<T> >, public EventTargetWithInlineData { |
| 17 DEFINE_EVENT_TARGET_REFCOUNTING(RefCountedWillBeRefCountedGarbageCollected<T
rackListBase<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 T* anonymousIndexedGetter(unsigned index) const |
| 33 { |
| 34 if (index >= m_tracks.size()) |
| 35 return 0; |
| 36 return m_tracks[index].get(); |
| 37 } |
| 38 |
| 39 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].get(); |
| 44 } |
| 45 |
| 46 return 0; |
| 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 #if !ENABLE(OILPAN) |
| 62 void shutdown() |
| 63 { |
| 64 removeAll(); |
| 65 m_mediaElement = nullptr; |
| 66 } |
| 67 #endif |
| 68 |
| 69 void add(PassRefPtrWillBeRawPtr<T> prpTrack) |
| 70 { |
| 71 RefPtrWillBeRawPtr<T> track = prpTrack; |
| 72 |
| 73 track->setMediaElement(m_mediaElement); |
| 74 m_tracks.append(track); |
| 75 scheduleTrackEvent(EventTypeNames::addtrack, track.release()); |
| 76 } |
| 77 |
| 78 void remove(blink::WebTrackId webId) |
| 79 { |
| 80 for (unsigned i = 0; i < m_tracks.size(); ++i) { |
| 81 if (m_tracks[i]->webId() != webId) |
| 82 continue; |
| 83 |
| 84 m_tracks[i]->setMediaElement(0); |
| 85 scheduleTrackEvent(EventTypeNames::removetrack, m_tracks[i]); |
| 86 m_tracks.remove(i); |
| 87 return; |
| 88 } |
| 89 } |
| 90 |
| 91 void removeAll() |
| 92 { |
| 93 for (unsigned i = 0; i < m_tracks.size(); ++i) |
| 94 m_tracks[i]->setMediaElement(0); |
| 95 |
| 96 m_tracks.clear(); |
| 97 } |
| 98 |
| 99 void scheduleChangeEvent() |
| 100 { |
| 101 EventInit initializer; |
| 102 initializer.bubbles = false; |
| 103 initializer.cancelable = false; |
| 104 RefPtrWillBeRawPtr<Event> event = Event::create(EventTypeNames::change,
initializer); |
| 105 event->setTarget(this); |
| 106 m_mediaElement->scheduleEvent(event); |
| 107 } |
| 108 |
| 109 void trace(Visitor* visitor) |
| 110 { |
| 111 visitor->trace(m_tracks); |
| 112 visitor->trace(m_mediaElement); |
| 113 } |
| 114 |
| 115 private: |
| 116 void scheduleTrackEvent(const AtomicString& eventName, PassRefPtrWillBeRawPt
r<T> track) |
| 117 { |
| 118 TrackEventInit initializer; |
| 119 initializer.track = track; |
| 120 initializer.bubbles = false; |
| 121 initializer.cancelable = false; |
| 122 RefPtrWillBeRawPtr<Event> event = TrackEvent::create(eventName, initiali
zer); |
| 123 event->setTarget(this); |
| 124 m_mediaElement->scheduleEvent(event); |
| 125 } |
| 126 |
| 127 WillBeHeapVector<RefPtrWillBeMember<T> > m_tracks; |
| 128 RawPtrWillBeMember<HTMLMediaElement> m_mediaElement; |
| 129 }; |
| 130 |
| 131 } |
| 132 |
| 133 #endif |
OLD | NEW |