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 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 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"
32 #include "MessagePortChannel.h"
34 #include <wtf/Forward.h>
35 #include <wtf/RefPtr.h>
36 #include <wtf/Vector.h>
43 class ScriptExecutionContext;
45 // The overwhelmingly common case is sending a single port, so handle that efficiently with an inline buffer of size 1.
46 typedef Vector<RefPtr<MessagePort>, 1> MessagePortArray;
48 class MessagePort final : public RefCounted<MessagePort>, public EventTargetWithInlineData {
50 static Ref<MessagePort> create(ScriptExecutionContext& scriptExecutionContext) { return adoptRef(*new MessagePort(scriptExecutionContext)); }
51 virtual ~MessagePort();
53 void postMessage(RefPtr<SerializedScriptValue>&& message, const MessagePortArray*, ExceptionCode&);
54 // Needed for Objective-C bindings (see bug 28774).
55 void postMessage(RefPtr<SerializedScriptValue>&& message, MessagePort*, ExceptionCode&);
60 void entangle(std::unique_ptr<MessagePortChannel>);
61 std::unique_ptr<MessagePortChannel> disentangle();
63 // Returns 0 if there is an exception, or if the passed-in array is 0/empty.
64 static std::unique_ptr<MessagePortChannelArray> disentanglePorts(const MessagePortArray*, ExceptionCode&);
66 // Returns 0 if the passed array is 0/empty.
67 static std::unique_ptr<MessagePortArray> entanglePorts(ScriptExecutionContext&, std::unique_ptr<MessagePortChannelArray>);
69 void messageAvailable();
70 bool started() const { return m_started; }
72 void contextDestroyed();
74 EventTargetInterface eventTargetInterface() const override { return MessagePortEventTargetInterfaceType; }
75 ScriptExecutionContext* scriptExecutionContext() const override { return m_scriptExecutionContext; }
77 void dispatchMessages();
79 using RefCounted<MessagePort>::ref;
80 using RefCounted<MessagePort>::deref;
82 bool hasPendingActivity();
84 // Returns null if there is no entangled port, or if the entangled port is run by a different thread.
85 // This is used solely to enable a GC optimization. Some platforms may not be able to determine ownership
86 // of the remote port (since it may live cross-process) - those platforms may always return null.
87 MessagePort* locallyEntangledPort();
89 // A port starts out its life entangled, and remains entangled until it is closed or is cloned.
90 bool isEntangled() { return !m_closed && !isNeutered(); }
92 // A port gets neutered when it is transferred to a new owner via postMessage().
93 bool isNeutered() { return !m_entangledChannel; }
95 bool addEventListener(const AtomicString& eventType, RefPtr<EventListener>&&, bool useCapture) override;
98 explicit MessagePort(ScriptExecutionContext&);
100 void refEventTarget() override { ref(); }
101 void derefEventTarget() override { deref(); }
102 bool isMessagePort() const override { return true; }
104 std::unique_ptr<MessagePortChannel> m_entangledChannel;
109 ScriptExecutionContext* m_scriptExecutionContext;
112 } // namespace WebCore
114 #endif // MessagePort_h