1 | /* |
2 | * Copyright (C) 2008 Apple 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. ``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. |
24 | * |
25 | */ |
26 | |
27 | #pragma once |
28 | |
29 | #include "ActiveDOMObject.h" |
30 | #include "EventTarget.h" |
31 | #include "ExceptionOr.h" |
32 | #include "MessagePortChannel.h" |
33 | #include "MessagePortIdentifier.h" |
34 | #include "MessageWithMessagePorts.h" |
35 | #include <wtf/WeakPtr.h> |
36 | |
37 | namespace JSC { |
38 | class ExecState; |
39 | class JSObject; |
40 | class JSValue; |
41 | } |
42 | |
43 | namespace WebCore { |
44 | |
45 | class Frame; |
46 | |
47 | class MessagePort final : public ActiveDOMObject, public EventTargetWithInlineData, public CanMakeWeakPtr<MessagePort> { |
48 | WTF_MAKE_NONCOPYABLE(MessagePort); |
49 | WTF_MAKE_ISO_ALLOCATED(MessagePort); |
50 | public: |
51 | static Ref<MessagePort> create(ScriptExecutionContext&, const MessagePortIdentifier& local, const MessagePortIdentifier& remote); |
52 | virtual ~MessagePort(); |
53 | |
54 | ExceptionOr<void> postMessage(JSC::ExecState&, JSC::JSValue message, Vector<JSC::Strong<JSC::JSObject>>&&); |
55 | |
56 | void start(); |
57 | void close(); |
58 | void entangle(); |
59 | |
60 | // Returns nullptr if the passed-in vector is empty. |
61 | static ExceptionOr<TransferredMessagePortArray> disentanglePorts(Vector<RefPtr<MessagePort>>&&); |
62 | static Vector<RefPtr<MessagePort>> entanglePorts(ScriptExecutionContext&, TransferredMessagePortArray&&); |
63 | |
64 | WEBCORE_EXPORT static bool isExistingMessagePortLocallyReachable(const MessagePortIdentifier&); |
65 | WEBCORE_EXPORT static void notifyMessageAvailable(const MessagePortIdentifier&); |
66 | |
67 | WEBCORE_EXPORT void messageAvailable(); |
68 | bool started() const { return m_started; } |
69 | bool closed() const { return m_closed; } |
70 | |
71 | void dispatchMessages(); |
72 | |
73 | // Returns null if there is no entangled port, or if the entangled port is run by a different thread. |
74 | // This is used solely to enable a GC optimization. Some platforms may not be able to determine ownership |
75 | // of the remote port (since it may live cross-process) - those platforms may always return null. |
76 | MessagePort* locallyEntangledPort() const; |
77 | |
78 | const MessagePortIdentifier& identifier() const { return m_identifier; } |
79 | const MessagePortIdentifier& remoteIdentifier() const { return m_remoteIdentifier; } |
80 | |
81 | WEBCORE_EXPORT void ref() const; |
82 | WEBCORE_EXPORT void deref() const; |
83 | |
84 | // ActiveDOMObject |
85 | const char* activeDOMObjectName() const final; |
86 | bool canSuspendForDocumentSuspension() const final; |
87 | void contextDestroyed() final; |
88 | void stop() final { close(); } |
89 | bool hasPendingActivity() const final; |
90 | |
91 | WEBCORE_EXPORT bool isLocallyReachable() const; |
92 | |
93 | // EventTargetWithInlineData. |
94 | EventTargetInterface eventTargetInterface() const final { return MessagePortEventTargetInterfaceType; } |
95 | ScriptExecutionContext* scriptExecutionContext() const final { return ActiveDOMObject::scriptExecutionContext(); } |
96 | void refEventTarget() final { ref(); } |
97 | void derefEventTarget() final { deref(); } |
98 | |
99 | private: |
100 | explicit MessagePort(ScriptExecutionContext&, const MessagePortIdentifier& local, const MessagePortIdentifier& remote); |
101 | |
102 | bool addEventListener(const AtomicString& eventType, Ref<EventListener>&&, const AddEventListenerOptions&) final; |
103 | bool removeEventListener(const AtomicString& eventType, EventListener&, const ListenerOptions&) final; |
104 | |
105 | void disentangle(); |
106 | |
107 | void registerLocalActivity(); |
108 | |
109 | // A port starts out its life entangled, and remains entangled until it is closed or is cloned. |
110 | bool isEntangled() const { return !m_closed && m_entangled; } |
111 | |
112 | void updateActivity(MessagePortChannelProvider::HasActivity); |
113 | |
114 | bool m_started { false }; |
115 | bool m_closed { false }; |
116 | bool m_entangled { true }; |
117 | |
118 | // Flags to manage querying the remote port for GC purposes |
119 | mutable bool m_mightBeEligibleForGC { false }; |
120 | mutable bool m_hasHadLocalActivitySinceLastCheck { false }; |
121 | mutable bool m_isRemoteEligibleForGC { false }; |
122 | mutable bool m_isAskingRemoteAboutGC { false }; |
123 | bool m_hasMessageEventListener { false }; |
124 | |
125 | MessagePortIdentifier m_identifier; |
126 | MessagePortIdentifier m_remoteIdentifier; |
127 | |
128 | mutable std::atomic<unsigned> m_refCount { 1 }; |
129 | }; |
130 | |
131 | } // namespace WebCore |
132 | |