2008-11-04 Anders Carlsson <andersca@apple.com>
[webkit/qt.git] / WebCore / dom / MessagePort.h
blobdc11d2371fccb3aecebd175d9023d7611a41a413
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 #ifndef MessagePort_h
28 #define MessagePort_h
30 #include "AtomicStringHash.h"
31 #include "EventListener.h"
32 #include "EventTarget.h"
34 #include <wtf/HashMap.h>
35 #include <wtf/MessageQueue.h>
36 #include <wtf/PassRefPtr.h>
37 #include <wtf/RefPtr.h>
38 #include <wtf/Threading.h>
39 #include <wtf/Vector.h>
41 namespace WebCore {
43 class AtomicStringImpl;
44 class Event;
45 class Frame;
46 class ScriptExecutionContext;
47 class String;
48 class WorkerContext;
50 class MessagePort : public ThreadSafeShared<MessagePort>, public EventTarget {
51 public:
52 static PassRefPtr<MessagePort> create(ScriptExecutionContext* scriptExecutionContext) { return adoptRef(new MessagePort(scriptExecutionContext)); }
53 ~MessagePort();
55 PassRefPtr<MessagePort> clone(ScriptExecutionContext*, ExceptionCode&);
57 bool active() const { return m_entangledPort; }
58 void postMessage(const String& message, ExceptionCode&);
59 void postMessage(const String& message, MessagePort*, ExceptionCode&);
60 PassRefPtr<MessagePort> startConversation(ScriptExecutionContext*, const String& message);
61 void start();
62 void close();
64 bool queueIsOpen() const { return m_queueIsOpen; }
66 MessagePort* entangledPort() { return m_entangledPort; }
67 static void entangle(MessagePort*, MessagePort*);
68 void unentangle();
70 void contextDestroyed();
71 virtual ScriptExecutionContext* scriptExecutionContext() const { return m_scriptExecutionContext; }
73 virtual MessagePort* toMessagePort() { return this; }
75 void queueCloseEvent();
76 void dispatchMessages();
78 virtual void addEventListener(const AtomicString& eventType, PassRefPtr<EventListener>, bool useCapture);
79 virtual void removeEventListener(const AtomicString& eventType, EventListener*, bool useCapture);
80 virtual bool dispatchEvent(PassRefPtr<Event>, ExceptionCode&);
82 typedef Vector<RefPtr<EventListener> > ListenerVector;
83 typedef HashMap<AtomicString, ListenerVector> EventListenersMap;
84 EventListenersMap& eventListeners() { return m_eventListeners; }
86 using ThreadSafeShared<MessagePort>::ref;
87 using ThreadSafeShared<MessagePort>::deref;
89 bool hasPendingActivity();
91 void setOnmessage(PassRefPtr<EventListener> eventListener) { m_onMessageListener = eventListener; }
92 EventListener* onmessage() const { return m_onMessageListener.get(); }
94 void setOnclose(PassRefPtr<EventListener> eventListener) { m_onCloseListener = eventListener; }
95 EventListener* onclose() const { return m_onCloseListener.get(); }
97 void setJSWrapperIsInaccessible() { m_jsWrapperIsInaccessible = true; }
98 bool jsWrapperIsInaccessible() const { return m_jsWrapperIsInaccessible; }
100 private:
101 friend class CloseMessagePortTimer;
103 MessagePort(ScriptExecutionContext*);
105 virtual void refEventTarget() { ref(); }
106 virtual void derefEventTarget() { deref(); }
108 void dispatchCloseEvent();
110 MessagePort* m_entangledPort;
111 MessageQueue<RefPtr<Event> > m_messageQueue;
112 bool m_queueIsOpen;
114 ScriptExecutionContext* m_scriptExecutionContext;
116 RefPtr<EventListener> m_onMessageListener;
117 RefPtr<EventListener> m_onCloseListener;
119 EventListenersMap m_eventListeners;
121 bool m_pendingCloseEvent;
122 bool m_jsWrapperIsInaccessible;
125 } // namespace WebCore
127 #endif // MessagePort_h