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
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.
30 #include "EventListener.h"
31 #include "EventTarget.h"
33 #include <wtf/HashMap.h>
34 #include <wtf/MessageQueue.h>
35 #include <wtf/PassRefPtr.h>
36 #include <wtf/RefCounted.h>
37 #include <wtf/RefPtr.h>
38 #include <wtf/Vector.h>
42 class AtomicStringImpl;
48 class MessagePort : public RefCounted<MessagePort>, public EventTarget {
50 static PassRefPtr<MessagePort> create(Document* document) { return adoptRef(new MessagePort(document)); }
53 PassRefPtr<MessagePort> clone(Document*, ExceptionCode&);
55 bool active() const { return m_entangledPort; }
56 void postMessage(const String& message, ExceptionCode&);
57 void postMessage(const String& message, MessagePort*, ExceptionCode&);
58 PassRefPtr<MessagePort> startConversation(Document*, const String& message);
62 bool queueIsOpen() const { return m_queueIsOpen; }
64 MessagePort* entangledPort() { return m_entangledPort; }
65 static void entangle(MessagePort*, MessagePort*);
68 void contextDestroyed() { m_document = 0; }
69 Document* document() { return m_document; }
71 virtual MessagePort* toMessagePort() { return this; }
73 virtual Frame* associatedFrame() const;
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&, bool tempEvent = false);
82 typedef Vector<RefPtr<EventListener> > ListenerVector;
83 typedef HashMap<AtomicStringImpl*, ListenerVector> EventListenersMap;
84 EventListenersMap& eventListeners() { return m_eventListeners; }
86 using RefCounted<MessagePort>::ref;
87 using RefCounted<MessagePort>::deref;
89 bool hasPendingActivity() { return m_pendingActivity; }
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(); }
98 friend class CloseMessagePortTimer;
100 MessagePort(Document*);
102 virtual void refEventTarget() { ref(); }
103 virtual void derefEventTarget() { deref(); }
105 void dispatchCloseEvent();
107 void setPendingActivity();
108 void unsetPendingActivity();
110 MessagePort* m_entangledPort;
111 MessageQueue<RefPtr<Event> > m_messageQueue;
114 Document* m_document; // Will be 0 if the context does not contain a document (e.g. if it's a worker thread).
116 RefPtr<EventListener> m_onMessageListener;
117 RefPtr<EventListener> m_onCloseListener;
119 EventListenersMap m_eventListeners;
121 unsigned m_pendingActivity;
124 } // namespace WebCore
126 #endif // MessagePort_h