1/*
2 * Copyright (C) 2011, 2012 Apple Inc.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef TrackListBase_h
27#define TrackListBase_h
28
29#if ENABLE(VIDEO_TRACK)
30
31#include "EventListener.h"
32#include "EventTarget.h"
33#include "Timer.h"
34#include <wtf/PassRefPtr.h>
35#include <wtf/RefCounted.h>
36#include <wtf/Vector.h>
37
38namespace WebCore {
39
40class HTMLMediaElement;
41class Element;
42class TrackBase;
43
44class TrackListBase : public RefCounted<TrackListBase>, public EventTarget {
45public:
46    ~TrackListBase();
47
48    virtual unsigned length() const;
49    virtual bool contains(TrackBase*) const;
50    virtual void remove(TrackBase*);
51
52    // EventTarget
53    virtual const AtomicString& interfaceName() const = 0;
54    using RefCounted<TrackListBase>::ref;
55    using RefCounted<TrackListBase>::deref;
56    virtual ScriptExecutionContext* scriptExecutionContext() const { return m_context; }
57
58    DEFINE_ATTRIBUTE_EVENT_LISTENER(addtrack);
59    DEFINE_ATTRIBUTE_EVENT_LISTENER(change);
60    DEFINE_ATTRIBUTE_EVENT_LISTENER(removetrack);
61
62    void clearElement() { m_element = 0; }
63    Element* element() const;
64    HTMLMediaElement* mediaElement() const { return m_element; }
65
66    bool isFiringEventListeners() { return m_dispatchingEvents; }
67
68    // Needs to be public so tracks can call it
69    void scheduleChangeEvent();
70
71protected:
72    TrackListBase(HTMLMediaElement*, ScriptExecutionContext*);
73
74    void scheduleAddTrackEvent(PassRefPtr<TrackBase>);
75    void scheduleRemoveTrackEvent(PassRefPtr<TrackBase>);
76
77    Vector<RefPtr<TrackBase> > m_inbandTracks;
78
79private:
80
81    // EventTarget
82    virtual void refEventTarget() OVERRIDE { ref(); }
83    virtual void derefEventTarget() OVERRIDE { deref(); }
84    virtual EventTargetData* eventTargetData() OVERRIDE { return &m_eventTargetData; }
85    virtual EventTargetData* ensureEventTargetData() OVERRIDE { return &m_eventTargetData; }
86
87    void asyncEventTimerFired(Timer<TrackListBase>*);
88
89    ScriptExecutionContext* m_context;
90    HTMLMediaElement* m_element;
91
92    Vector<RefPtr<Event> > m_pendingEvents;
93    Timer<TrackListBase> m_pendingEventTimer;
94
95    EventTargetData m_eventTargetData;
96
97    int m_dispatchingEvents;
98};
99
100} // namespace WebCore
101
102#endif
103#endif
104