2008-11-04 Anders Carlsson <andersca@apple.com>
[webkit/qt.git] / WebCore / dom / ScriptExecutionContext.cpp
blob5bb076a00a408c3214f168a66317d80aa0812fa8
1 /*
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
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.
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.
27 #include "config.h"
28 #include "ScriptExecutionContext.h"
30 #include "ActiveDOMObject.h"
31 #include "MessagePort.h"
32 #include "Timer.h"
33 #include <wtf/PassRefPtr.h>
35 namespace WebCore {
37 class MessagePortTimer : public TimerBase {
38 public:
39 MessagePortTimer(PassRefPtr<ScriptExecutionContext> context)
40 : m_context(context)
44 private:
45 virtual void fired()
47 m_context->dispatchMessagePortEvents();
48 delete this;
51 RefPtr<ScriptExecutionContext> m_context;
54 ScriptExecutionContext::ScriptExecutionContext()
55 : m_firedMessagePortTimer(false)
59 ScriptExecutionContext::~ScriptExecutionContext()
61 HashMap<ActiveDOMObject*, void*>::iterator activeObjectsEnd = m_activeDOMObjects.end();
62 for (HashMap<ActiveDOMObject*, void*>::iterator iter = m_activeDOMObjects.begin(); iter != activeObjectsEnd; ++iter) {
63 ASSERT(iter->first->scriptExecutionContext() == this);
64 iter->first->contextDestroyed();
67 HashSet<MessagePort*>::iterator messagePortsEnd = m_messagePorts.end();
68 for (HashSet<MessagePort*>::iterator iter = m_messagePorts.begin(); iter != messagePortsEnd; ++iter) {
69 ASSERT((*iter)->scriptExecutionContext() == this);
70 (*iter)->contextDestroyed();
74 void ScriptExecutionContext::processMessagePortMessagesSoon()
76 if (m_firedMessagePortTimer)
77 return;
79 MessagePortTimer* timer = new MessagePortTimer(this);
80 timer->startOneShot(0);
82 m_firedMessagePortTimer = true;
85 void ScriptExecutionContext::dispatchMessagePortEvents()
87 RefPtr<ScriptExecutionContext> protect(this);
89 // Make a frozen copy.
90 Vector<MessagePort*> ports;
91 copyToVector(m_messagePorts, ports);
93 m_firedMessagePortTimer = false;
95 unsigned portCount = ports.size();
96 for (unsigned i = 0; i < portCount; ++i) {
97 MessagePort* port = ports[i];
98 if (m_messagePorts.contains(port) && port->queueIsOpen())
99 port->dispatchMessages();
103 void ScriptExecutionContext::createdMessagePort(MessagePort* port)
105 ASSERT(port);
106 m_messagePorts.add(port);
109 void ScriptExecutionContext::destroyedMessagePort(MessagePort* port)
111 ASSERT(port);
112 m_messagePorts.remove(port);
115 void ScriptExecutionContext::stopActiveDOMObjects()
117 HashMap<ActiveDOMObject*, void*>::iterator activeObjectsEnd = m_activeDOMObjects.end();
118 for (HashMap<ActiveDOMObject*, void*>::iterator iter = m_activeDOMObjects.begin(); iter != activeObjectsEnd; ++iter) {
119 ASSERT(iter->first->scriptExecutionContext() == this);
120 iter->first->stop();
124 void ScriptExecutionContext::createdActiveDOMObject(ActiveDOMObject* object, void* upcastPointer)
126 ASSERT(object);
127 ASSERT(upcastPointer);
128 m_activeDOMObjects.add(object, upcastPointer);
131 void ScriptExecutionContext::destroyedActiveDOMObject(ActiveDOMObject* object)
133 ASSERT(object);
134 m_activeDOMObjects.remove(object);
138 } // namespace WebCore