1/*
2 * Copyright (C) 2011 Google 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 INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "config.h"
26#include "MediaStreamContext.h"
27
28#if ENABLE(MEDIA_STREAM)
29
30#include "DOMWindow.h"
31#include "Document.h"
32#include "Frame.h"
33#include "MediaStreamController.h"
34#include "Page.h"
35#include "SecurityOrigin.h"
36#include <wtf/RefCounted.h>
37
38namespace WebCore {
39
40class MediaStreamContext::Request : public RefCounted<Request> {
41public:
42 Request(ScriptExecutionContext* scriptContext)
43 : m_scriptContext(scriptContext) { }
44
45 virtual ~Request() { }
46
47 ScriptExecutionContext* scriptContext() const { return m_scriptContext; }
48
49 virtual void abort() = 0;
50
51private:
52 // This script execution context is the document referenced by the frame. It will
53 // remain alive and valid the same time that the frame is, aborting all requests
54 // when receiving disconnectFrame. Its purpose is to allow async request
55 // abortions to be properly scheduled to comply with the specification.
56 ScriptExecutionContext* m_scriptContext;
57};
58
59void MediaStreamContext::RequestMap::abort(int requestId)
60{
61 ASSERT(contains(requestId));
62 get(requestId)->abort();
63 remove(requestId);
64}
65
66void MediaStreamContext::RequestMap::abortAll()
67{
68 for (iterator it = begin(); it != end(); it = begin()) {
69 it->second->abort();
70 remove(it);
71 }
72}
73
74MediaStreamContext::MediaStreamContext(Frame* frame)
75 : m_frame(frame)
76 , m_detachedState(false)
77{
78}
79
80MediaStreamContext::~MediaStreamContext()
81{
82}
83
84SecurityOrigin* MediaStreamContext::securityOrigin() const
85{
86 return m_frame ? m_frame->existingDOMWindow()->securityOrigin() : 0;
87}
88
89ScriptExecutionContext* MediaStreamContext::scriptExecutionContext() const
90{
91 return m_frame ? m_frame->existingDOMWindow()->scriptExecutionContext() : 0;
92}
93
94MediaStreamController* MediaStreamContext::pageController() const
95{
96 return !m_detachedState && m_frame && m_frame->page() ? m_frame->page()->mediaStreamController() : 0;
97}
98
99void MediaStreamContext::enterDetachedState()
100{
101 m_requests.abortAll();
102 m_detachedState = true;
103}
104
105void MediaStreamContext::pageDetached()
106{
107 if (m_detachedState)
108 return;
109
110 if (pageController())
111 pageController()->contextDetached(this);
112
113 enterDetachedState();
114}
115
116// Called on frame destruction despite the name.
117void MediaStreamContext::disconnectFrame()
118{
119 m_requests.abortAll();
120
121 if (pageController())
122 pageController()->contextDestroyed(this);
123
124 m_frame = 0;
125}
126
127void MediaStreamContext::controllerDestroyed(MediaStreamController* controller)
128{
129 ASSERT(controller);
130
131 if (pageController() == controller)
132 enterDetachedState();
133}
134
135void MediaStreamContext::transferToNewPage(Page* newPage)
136{
137 if (pageController())
138 pageController()->transferContext(this, newPage ? newPage->mediaStreamController() : 0);
139
140 if (!newPage) {
141 enterDetachedState();
142 return;
143 }
144
145 // FIXME: in the future we should keep running the media stream services while transfering frames.
146 // However, until a proper way to do this is decided, we're shutting down services.
147 enterDetachedState();
148}
149
150} // namespace WebCore
151
152#endif // ENABLE(MEDIA_STREAM)