Replace DOMException TYPE_MISMATCH_ERR with TypeError
[WebKit-https.git] / Source / WebCore / Modules / mediastream / MediaStreamTrackList.cpp
1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  * Copyright (C) 2011 Ericsson AB. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #if ENABLE(MEDIA_STREAM)
28
29 #include "MediaStreamTrackList.h"
30
31 #include "ExceptionCode.h"
32 #include "MediaStream.h"
33 #include "MediaStreamCenter.h"
34 #include "MediaStreamTrackEvent.h"
35
36 namespace WebCore {
37
38 PassRefPtr<MediaStreamTrackList> MediaStreamTrackList::create(MediaStream* owner, const MediaStreamTrackVector& trackVector)
39 {
40     ASSERT(owner);
41     RefPtr<MediaStreamTrackList> trackList = adoptRef(new MediaStreamTrackList(owner, trackVector));
42     trackList->suspendIfNeeded();
43     return trackList.release();
44 }
45
46 MediaStreamTrackList::MediaStreamTrackList(MediaStream* owner, const MediaStreamTrackVector& trackVector)
47     : ActiveDOMObject(owner->scriptExecutionContext(), this)
48     , m_owner(owner)
49     , m_trackVector(trackVector)
50 {
51 }
52
53 MediaStreamTrackList::~MediaStreamTrackList()
54 {
55 }
56
57 void MediaStreamTrackList::detachOwner()
58 {
59     m_owner = 0;
60 }
61
62 unsigned MediaStreamTrackList::length() const
63 {
64     return m_trackVector.size();
65 }
66
67 MediaStreamTrack* MediaStreamTrackList::item(unsigned index) const
68 {
69     if (index >= m_trackVector.size())
70         return 0;
71     return m_trackVector[index].get();
72 }
73
74 void MediaStreamTrackList::add(PassRefPtr<MediaStreamTrack> prpTrack, ExceptionCode& ec)
75 {
76     if (!m_owner || m_owner->readyState() == MediaStream::ENDED) {
77         ec = INVALID_STATE_ERR;
78         return;
79     }
80
81     RefPtr<MediaStreamTrack> track = prpTrack;
82     if (!track) {
83         ec = NATIVE_TYPE_ERR;
84         return;
85     }
86
87     if (m_trackVector.contains(track))
88         return;
89
90     m_trackVector.append(track);
91     if (!MediaStreamCenter::instance().didAddMediaStreamTrack(m_owner->descriptor(), track->component())) {
92         ec = NOT_SUPPORTED_ERR;
93         return;
94     }
95
96     dispatchEvent(MediaStreamTrackEvent::create(eventNames().addtrackEvent, false, false, track));
97 }
98
99 void MediaStreamTrackList::remove(PassRefPtr<MediaStreamTrack> prpTrack, ExceptionCode& ec)
100 {
101     if (!m_owner || m_owner->readyState() == MediaStream::ENDED) {
102         ec = INVALID_STATE_ERR;
103         return;
104     }
105
106     RefPtr<MediaStreamTrack> track = prpTrack;
107     if (!track) {
108         ec = NATIVE_TYPE_ERR;
109         return;
110     }
111
112     size_t index = m_trackVector.find(track);
113     if (index == notFound)
114         return;
115
116     m_trackVector.remove(index);
117     if (!MediaStreamCenter::instance().didRemoveMediaStreamTrack(m_owner->descriptor(), track->component())) {
118         ec = NOT_SUPPORTED_ERR;
119         return;
120     }
121
122     dispatchEvent(MediaStreamTrackEvent::create(eventNames().removetrackEvent, false, false, track));
123 }
124
125 void MediaStreamTrackList::remove(MediaStreamComponent* component)
126 {
127     if (!m_owner || m_owner->readyState() == MediaStream::ENDED)
128         return;
129
130     size_t index = notFound;
131     for (unsigned i = 0; i < m_trackVector.size(); ++i) {
132         if (m_trackVector[i]->component() == component) {
133             index = i;
134             break;
135         }
136     }
137
138     if (index == notFound)
139         return;
140
141     RefPtr<MediaStreamTrack> track = m_trackVector[index];
142     m_trackVector.remove(index);
143     MediaStreamCenter::instance().didRemoveMediaStreamTrack(m_owner->descriptor(), component);
144     dispatchEvent(MediaStreamTrackEvent::create(eventNames().removetrackEvent, false, false, track));
145 }
146
147 void MediaStreamTrackList::stop()
148 {
149     detachOwner();
150 }
151
152 const AtomicString& MediaStreamTrackList::interfaceName() const
153 {
154     return eventNames().interfaceForMediaStreamTrackList;
155 }
156
157 ScriptExecutionContext* MediaStreamTrackList::scriptExecutionContext() const
158 {
159     return ActiveDOMObject::scriptExecutionContext();
160 }
161
162 EventTargetData* MediaStreamTrackList::eventTargetData()
163 {
164     return &m_eventTargetData;
165 }
166
167 EventTargetData* MediaStreamTrackList::ensureEventTargetData()
168 {
169     return &m_eventTargetData;
170 }
171
172 } // namespace WebCore
173
174 #endif // ENABLE(MEDIA_STREAM)