Skip to content

Commit e96f5f5

Browse files
Refactor MessagePortChannel family classes for an easier multi-process split.
https://bugs.webkit.org/show_bug.cgi?id=180981 Reviewed by Andy Estes. No new tests (Refactor, no behavior change) - Make MessagePortChannel an abstract class instead of a wrapper around a mysterious "platform" class. - Implement the "in-process" channel for WK1 and WK2-for-now. - Other random cleanup and modernization of this code. * Sources.txt: * WebCore.xcodeproj/project.pbxproj: * dom/InProcessMessagePortChannel.cpp: Added. (WebCore::InProcessMessagePortChannel::createChannelBetweenPorts): (WebCore::InProcessMessagePortChannel::create): (WebCore::InProcessMessagePortChannel::InProcessMessagePortChannel): (WebCore::InProcessMessagePortChannel::~InProcessMessagePortChannel): (WebCore::InProcessMessagePortChannel::postMessageToRemote): (WebCore::InProcessMessagePortChannel::takeAllMessagesFromRemote): (WebCore::InProcessMessagePortChannel::isConnectedTo): (WebCore::InProcessMessagePortChannel::entangleIfOpen): (WebCore::InProcessMessagePortChannel::disentangle): (WebCore::InProcessMessagePortChannel::hasPendingActivity): (WebCore::InProcessMessagePortChannel::locallyEntangledPort): (WebCore::InProcessMessagePortChannel::takeEntangledChannel): (WebCore::InProcessMessagePortChannel::close): (WebCore::InProcessMessagePortChannel::setRemotePort): * dom/InProcessMessagePortChannel.h: Added. (WebCore::InProcessMessagePortChannel::MessagePortQueue::create): (WebCore::InProcessMessagePortChannel::MessagePortQueue::takeAllMessages): (WebCore::InProcessMessagePortChannel::MessagePortQueue::appendAndCheckEmpty): (WebCore::InProcessMessagePortChannel::MessagePortQueue::isEmpty): (WebCore::InProcessMessagePortChannel::MessagePortQueue::MessagePortQueue): * dom/MessageChannel.cpp: (WebCore::MessageChannel::MessageChannel): * dom/MessageChannel.h: (WebCore::MessageChannel::create): (WebCore::MessageChannel::port1 const): (WebCore::MessageChannel::port2 const): * dom/MessagePort.cpp: (WebCore::MessagePort::postMessage): (WebCore::MessagePort::disentangle): (WebCore::MessagePort::entangle): (WebCore::MessagePort::hasPendingActivity const): * dom/MessagePort.h: * dom/MessagePortChannel.cpp: Added. (WebCore::MessagePortChannel::createChannelBetweenPorts): (WebCore::MessagePortChannel::MessagePortChannel): * dom/MessagePortChannel.h: (WebCore::MessagePortChannel::EventData::EventData): (WebCore::MessagePortChannel::~MessagePortChannel): * dom/default/PlatformMessagePortChannel.cpp: Removed. * dom/default/PlatformMessagePortChannel.h: Removed. * workers/service/context/ServiceWorkerThread.h: git-svn-id: http://svn.webkit.org/repository/webkit/trunk@226252 268f45cc-cd09-0410-ab3c-d52691b4dbfc
1 parent 5afbaa2 commit e96f5f5

14 files changed

+457
-367
lines changed

Source/WebCore/ChangeLog

+63
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,66 @@
1+
2017-12-21 Brady Eidson <[email protected]>
2+
3+
Refactor MessagePortChannel family classes for an easier multi-process split.
4+
https://bugs.webkit.org/show_bug.cgi?id=180981
5+
6+
Reviewed by Andy Estes.
7+
8+
No new tests (Refactor, no behavior change)
9+
10+
- Make MessagePortChannel an abstract class instead of a wrapper around a mysterious "platform" class.
11+
- Implement the "in-process" channel for WK1 and WK2-for-now.
12+
- Other random cleanup and modernization of this code.
13+
14+
* Sources.txt:
15+
* WebCore.xcodeproj/project.pbxproj:
16+
17+
* dom/InProcessMessagePortChannel.cpp: Added.
18+
(WebCore::InProcessMessagePortChannel::createChannelBetweenPorts):
19+
(WebCore::InProcessMessagePortChannel::create):
20+
(WebCore::InProcessMessagePortChannel::InProcessMessagePortChannel):
21+
(WebCore::InProcessMessagePortChannel::~InProcessMessagePortChannel):
22+
(WebCore::InProcessMessagePortChannel::postMessageToRemote):
23+
(WebCore::InProcessMessagePortChannel::takeAllMessagesFromRemote):
24+
(WebCore::InProcessMessagePortChannel::isConnectedTo):
25+
(WebCore::InProcessMessagePortChannel::entangleIfOpen):
26+
(WebCore::InProcessMessagePortChannel::disentangle):
27+
(WebCore::InProcessMessagePortChannel::hasPendingActivity):
28+
(WebCore::InProcessMessagePortChannel::locallyEntangledPort):
29+
(WebCore::InProcessMessagePortChannel::takeEntangledChannel):
30+
(WebCore::InProcessMessagePortChannel::close):
31+
(WebCore::InProcessMessagePortChannel::setRemotePort):
32+
* dom/InProcessMessagePortChannel.h: Added.
33+
(WebCore::InProcessMessagePortChannel::MessagePortQueue::create):
34+
(WebCore::InProcessMessagePortChannel::MessagePortQueue::takeAllMessages):
35+
(WebCore::InProcessMessagePortChannel::MessagePortQueue::appendAndCheckEmpty):
36+
(WebCore::InProcessMessagePortChannel::MessagePortQueue::isEmpty):
37+
(WebCore::InProcessMessagePortChannel::MessagePortQueue::MessagePortQueue):
38+
39+
* dom/MessageChannel.cpp:
40+
(WebCore::MessageChannel::MessageChannel):
41+
* dom/MessageChannel.h:
42+
(WebCore::MessageChannel::create):
43+
(WebCore::MessageChannel::port1 const):
44+
(WebCore::MessageChannel::port2 const):
45+
46+
* dom/MessagePort.cpp:
47+
(WebCore::MessagePort::postMessage):
48+
(WebCore::MessagePort::disentangle):
49+
(WebCore::MessagePort::entangle):
50+
(WebCore::MessagePort::hasPendingActivity const):
51+
* dom/MessagePort.h:
52+
53+
* dom/MessagePortChannel.cpp: Added.
54+
(WebCore::MessagePortChannel::createChannelBetweenPorts):
55+
(WebCore::MessagePortChannel::MessagePortChannel):
56+
* dom/MessagePortChannel.h:
57+
(WebCore::MessagePortChannel::EventData::EventData):
58+
(WebCore::MessagePortChannel::~MessagePortChannel):
59+
60+
* dom/default/PlatformMessagePortChannel.cpp: Removed.
61+
* dom/default/PlatformMessagePortChannel.h: Removed.
62+
* workers/service/context/ServiceWorkerThread.h:
63+
164
2017-12-21 Ryosuke Niwa <[email protected]>
265

366
Rename NoEventDispatchAssertion to ScriptDisallowedScope

Source/WebCore/Sources.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,7 @@ dom/FocusEvent.cpp
727727
dom/GenericEventQueue.cpp
728728
dom/IdTargetObserver.cpp
729729
dom/IdTargetObserverRegistry.cpp
730+
dom/InProcessMessagePortChannel.cpp
730731
dom/InlineClassicScript.cpp
731732
dom/InlineStyleSheetOwner.cpp
732733
dom/InputEvent.cpp
@@ -738,6 +739,7 @@ dom/LoadableScript.cpp
738739
dom/MessageChannel.cpp
739740
dom/MessageEvent.cpp
740741
dom/MessagePort.cpp
742+
dom/MessagePortChannel.cpp
741743
dom/Microtasks.cpp
742744
dom/MouseEvent.cpp
743745
dom/MouseRelatedEvent.cpp
@@ -812,8 +814,6 @@ dom/WebKitAnimationEvent.cpp
812814
dom/WebKitTransitionEvent.cpp
813815
dom/WheelEvent.cpp
814816

815-
dom/default/PlatformMessagePortChannel.cpp
816-
817817
domjit/DOMJITHelpers.cpp
818818
domjit/JSDocumentDOMJIT.cpp
819819
domjit/JSDocumentFragmentDOMJIT.cpp

Source/WebCore/WebCore.xcodeproj/project.pbxproj

+8-5
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,6 @@
10431043
41B28B3D1F860EF300FB52AC /* LibWebRTCProviderCocoa.h in Headers */ = {isa = PBXBuildFile; fileRef = 41B28B361F860BD000FB52AC /* LibWebRTCProviderCocoa.h */; settings = {ATTRIBUTES = (Private, ); }; };
10441044
41B2A6261EF1BF6D002B9D7A /* WebAudioSourceProvider.h in Headers */ = {isa = PBXBuildFile; fileRef = 41B2A6251EF1BF60002B9D7A /* WebAudioSourceProvider.h */; settings = {ATTRIBUTES = (Private, ); }; };
10451045
41BF700C0FE86F49005E8DEC /* MessagePortChannel.h in Headers */ = {isa = PBXBuildFile; fileRef = 41BF700A0FE86F49005E8DEC /* MessagePortChannel.h */; settings = {ATTRIBUTES = (Private, ); }; };
1046-
41BF70100FE86F61005E8DEC /* PlatformMessagePortChannel.h in Headers */ = {isa = PBXBuildFile; fileRef = 41BF700E0FE86F61005E8DEC /* PlatformMessagePortChannel.h */; };
10471046
41C760B10EDE03D300C1655F /* ScriptState.h in Headers */ = {isa = PBXBuildFile; fileRef = 41C760B00EDE03D300C1655F /* ScriptState.h */; settings = {ATTRIBUTES = (Private, ); }; };
10481047
41CAD71E1EA090A100178164 /* VideoToolBoxEncoderFactory.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 41CAD71C1EA0905700178164 /* VideoToolBoxEncoderFactory.cpp */; };
10491048
41D015CA0F4B5C71004A662F /* ContentType.h in Headers */ = {isa = PBXBuildFile; fileRef = 41D015C80F4B5C71004A662F /* ContentType.h */; settings = {ATTRIBUTES = (Private, ); }; };
@@ -1289,6 +1288,7 @@
12891288
511EC12C1C50ABBF0032F983 /* SQLiteIDBTransaction.h in Headers */ = {isa = PBXBuildFile; fileRef = 511EC12A1C50ABBA0032F983 /* SQLiteIDBTransaction.h */; };
12901289
511EC1301C50ABF50032F983 /* SQLiteIDBCursor.h in Headers */ = {isa = PBXBuildFile; fileRef = 511EC12E1C50ABEC0032F983 /* SQLiteIDBCursor.h */; };
12911290
5120BBAF1F1CECE700EFEBF1 /* CookieStorageObserver.h in Headers */ = {isa = PBXBuildFile; fileRef = 5120BBAD1F1CE77000EFEBF1 /* CookieStorageObserver.h */; settings = {ATTRIBUTES = (Private, ); }; };
1291+
512B57C01FE99083000A1E5E /* InProcessMessagePortChannel.h in Headers */ = {isa = PBXBuildFile; fileRef = 512B57BC1FE9902C000A1E5E /* InProcessMessagePortChannel.h */; };
12921292
512BDB4B1C456FFA006494DF /* SQLiteIDBBackingStore.h in Headers */ = {isa = PBXBuildFile; fileRef = 512BDB491C456FAB006494DF /* SQLiteIDBBackingStore.h */; };
12931293
512DD8F50D91E6AF000F89EE /* LegacyWebArchive.h in Headers */ = {isa = PBXBuildFile; fileRef = 512DD8EB0D91E6AF000F89EE /* LegacyWebArchive.h */; settings = {ATTRIBUTES = (Private, ); }; };
12941294
512DD8F60D91E6AF000F89EE /* Archive.h in Headers */ = {isa = PBXBuildFile; fileRef = 512DD8EC0D91E6AF000F89EE /* Archive.h */; settings = {ATTRIBUTES = (Private, ); }; };
@@ -7060,8 +7060,6 @@
70607060
41B459DA1F4CADB90000F6FD /* ReadableStream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ReadableStream.h; sourceTree = "<group>"; };
70617061
41B459ED1F55EBC70000F6FD /* ReadableStream.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ReadableStream.cpp; sourceTree = "<group>"; };
70627062
41BF700A0FE86F49005E8DEC /* MessagePortChannel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MessagePortChannel.h; sourceTree = "<group>"; };
7063-
41BF700D0FE86F61005E8DEC /* PlatformMessagePortChannel.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PlatformMessagePortChannel.cpp; path = default/PlatformMessagePortChannel.cpp; sourceTree = "<group>"; };
7064-
41BF700E0FE86F61005E8DEC /* PlatformMessagePortChannel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = PlatformMessagePortChannel.h; path = default/PlatformMessagePortChannel.h; sourceTree = "<group>"; };
70657063
41C760B00EDE03D300C1655F /* ScriptState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ScriptState.h; sourceTree = "<group>"; };
70667064
41C7E1051E6A54360027B4DE /* CanvasCaptureMediaStreamTrack.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CanvasCaptureMediaStreamTrack.cpp; sourceTree = "<group>"; };
70677065
41C7E1061E6A54360027B4DE /* CanvasCaptureMediaStreamTrack.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CanvasCaptureMediaStreamTrack.h; sourceTree = "<group>"; };
@@ -7572,6 +7570,9 @@
75727570
5123AF161890A4CA0031CDC9 /* IDBKeyRangeData.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = IDBKeyRangeData.cpp; sourceTree = "<group>"; };
75737571
5123AF171890A4CA0031CDC9 /* IDBKeyRangeData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IDBKeyRangeData.h; sourceTree = "<group>"; };
75747572
5123AF1C18918AE40031CDC9 /* IDBGetResult.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IDBGetResult.h; sourceTree = "<group>"; };
7573+
512B57BC1FE9902C000A1E5E /* InProcessMessagePortChannel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = InProcessMessagePortChannel.h; sourceTree = "<group>"; };
7574+
512B57BE1FE9902D000A1E5E /* MessagePortChannel.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MessagePortChannel.cpp; sourceTree = "<group>"; };
7575+
512B57BF1FE9902E000A1E5E /* InProcessMessagePortChannel.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = InProcessMessagePortChannel.cpp; sourceTree = "<group>"; };
75757576
512BDB481C456FAB006494DF /* SQLiteIDBBackingStore.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SQLiteIDBBackingStore.cpp; sourceTree = "<group>"; };
75767577
512BDB491C456FAB006494DF /* SQLiteIDBBackingStore.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SQLiteIDBBackingStore.h; sourceTree = "<group>"; };
75777578
512BDB4C1C46B0FF006494DF /* JSIDBCursorCustom.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSIDBCursorCustom.cpp; sourceTree = "<group>"; };
@@ -25372,6 +25373,8 @@
2537225373
C3CF17A315B0063F00276D39 /* IdTargetObserverRegistry.h */,
2537325374
8AB4BC76126FDB7100DEB727 /* IgnoreDestructiveWriteCountIncrementer.h */,
2537425375
467302011C4EFE6600BCB357 /* IgnoreOpensDuringUnloadCountIncrementer.h */,
25376+
512B57BF1FE9902E000A1E5E /* InProcessMessagePortChannel.cpp */,
25377+
512B57BC1FE9902C000A1E5E /* InProcessMessagePortChannel.h */,
2537525378
E30592611E27A38C00D57C98 /* InlineClassicScript.cpp */,
2537625379
E30592621E27A38C00D57C98 /* InlineClassicScript.h */,
2537725380
AA4C3A740B2B1679002334A2 /* InlineStyleSheetOwner.cpp */,
@@ -25402,6 +25405,7 @@
2540225405
E1ADECBE0E76ACF1004A1A5E /* MessagePort.cpp */,
2540325406
E1ADECBD0E76ACF1004A1A5E /* MessagePort.h */,
2540425407
E1ADECC60E76AD1F004A1A5E /* MessagePort.idl */,
25408+
512B57BE1FE9902D000A1E5E /* MessagePortChannel.cpp */,
2540525409
41BF700A0FE86F49005E8DEC /* MessagePortChannel.h */,
2540625410
CB8CF0151A934B43000D510B /* Microtasks.cpp */,
2540725411
53B895AD19DC7C37009CAA93 /* Microtasks.h */,
@@ -25466,8 +25470,6 @@
2546625470
8A7CC96F12076F8A001D4588 /* PendingScript.cpp */,
2546725471
8A7CC96A12076D73001D4588 /* PendingScript.h */,
2546825472
E3FA38611D716E7600AA5950 /* PendingScriptClient.h */,
25469-
41BF700D0FE86F61005E8DEC /* PlatformMessagePortChannel.cpp */,
25470-
41BF700E0FE86F61005E8DEC /* PlatformMessagePortChannel.h */,
2547125473
5189F0DD10B46B0E00F3C739 /* PopStateEvent.cpp */,
2547225474
5174E20810A1F44F00F95E6F /* PopStateEvent.h */,
2547325475
5174E20B10A1F49A00F95E6F /* PopStateEvent.idl */,
@@ -29756,6 +29758,7 @@
2975629758
A5B81CB51FAA44620037D1E6 /* WebConsoleAgent.h in Headers */,
2975729759
9BBA2CAB1F679E0C00FD1C1E /* WebContentReader.h in Headers */,
2975829760
419BE7591BC7F42B00E1C85B /* WebCoreBuiltinNames.h in Headers */,
29761+
512B57C01FE99083000A1E5E /* InProcessMessagePortChannel.h in Headers */,
2975929762
2D3EF44A1917915C00034184 /* WebCoreCALayerExtras.h in Headers */,
2976029763
515F79541CFCA3D500CCED93 /* WebCoreCrossThreadCopier.h in Headers */,
2976129764
CD5D27781E8318E000D80A3D /* WebCoreDecompressionSession.h in Headers */,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
/*
2+
* Copyright (C) 2017 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. AND ITS CONTRIBUTORS ``AS IS''
14+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15+
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17+
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23+
* THE POSSIBILITY OF SUCH DAMAGE.
24+
*/
25+
26+
#include "config.h"
27+
#include "InProcessMessagePortChannel.h"
28+
29+
#include "MessagePort.h"
30+
#include <wtf/Locker.h>
31+
32+
namespace WebCore {
33+
34+
void InProcessMessagePortChannel::createChannelBetweenPorts(MessagePort& port1, MessagePort& port2)
35+
{
36+
auto queue1 = MessagePortQueue::create();
37+
auto queue2 = MessagePortQueue::create();
38+
39+
auto channel1 = InProcessMessagePortChannel::create(queue1.get(), queue2.get());
40+
auto channel2 = InProcessMessagePortChannel::create(queue2.get(), queue1.get());
41+
42+
channel1->m_entangledChannel = channel2.ptr();
43+
channel2->m_entangledChannel = channel1.ptr();
44+
45+
port1.entangle(WTFMove(channel2));
46+
port2.entangle(WTFMove(channel1));
47+
}
48+
49+
Ref<InProcessMessagePortChannel> InProcessMessagePortChannel::create(MessagePortQueue& incoming, MessagePortQueue& outgoing)
50+
{
51+
return adoptRef(*new InProcessMessagePortChannel(incoming, outgoing));
52+
}
53+
54+
InProcessMessagePortChannel::InProcessMessagePortChannel(MessagePortQueue& incoming, MessagePortQueue& outgoing)
55+
: m_incomingQueue(&incoming)
56+
, m_outgoingQueue(&outgoing)
57+
{
58+
}
59+
60+
InProcessMessagePortChannel::~InProcessMessagePortChannel()
61+
{
62+
// Channels being destroyed should to have been closed.
63+
ASSERT(!m_outgoingQueue);
64+
}
65+
66+
void InProcessMessagePortChannel::postMessageToRemote(Ref<SerializedScriptValue>&& message, std::unique_ptr<MessagePortChannelArray>&& channels)
67+
{
68+
Locker<Lock> locker(m_lock);
69+
70+
if (!m_outgoingQueue)
71+
return;
72+
73+
bool wasEmpty = m_outgoingQueue->appendAndCheckEmpty(std::make_unique<EventData>(WTFMove(message), WTFMove(channels)));
74+
if (wasEmpty && m_remotePort)
75+
m_remotePort->messageAvailable();
76+
}
77+
78+
Deque<std::unique_ptr<MessagePortChannel::EventData>> InProcessMessagePortChannel::takeAllMessagesFromRemote()
79+
{
80+
Locker<Lock> locker(m_lock);
81+
return m_incomingQueue->takeAllMessages();
82+
}
83+
84+
bool InProcessMessagePortChannel::isConnectedTo(MessagePort& port)
85+
{
86+
// FIXME: What guarantees that the result remains the same after we release the lock?
87+
Locker<Lock> locker(m_lock);
88+
return m_remotePort == &port;
89+
}
90+
91+
bool InProcessMessagePortChannel::entangleIfOpen(MessagePort& port)
92+
{
93+
// We can't call member functions on our remote pair while holding our mutex or we'll deadlock,
94+
// but we need to guard against the remote port getting closed/freed, so create a standalone reference.
95+
RefPtr<InProcessMessagePortChannel> remote;
96+
{
97+
Locker<Lock> locker(m_lock);
98+
remote = m_entangledChannel;
99+
}
100+
101+
if (!remote)
102+
return false;
103+
104+
remote->setRemotePort(&port);
105+
106+
return true;
107+
}
108+
109+
void InProcessMessagePortChannel::disentangle()
110+
{
111+
Locker<Lock> locker(m_lock);
112+
113+
if (m_entangledChannel)
114+
m_entangledChannel->setRemotePort(nullptr);
115+
}
116+
117+
bool InProcessMessagePortChannel::hasPendingActivity()
118+
{
119+
// FIXME: What guarantees that the result remains the same after we release the lock?
120+
Locker<Lock> locker(m_lock);
121+
return !m_incomingQueue->isEmpty();
122+
}
123+
124+
MessagePort* InProcessMessagePortChannel::locallyEntangledPort(const ScriptExecutionContext* context)
125+
{
126+
Locker<Lock> locker(m_lock);
127+
128+
// See if both contexts are run by the same thread (are the same context, or are both documents).
129+
if (!m_remotePort)
130+
return nullptr;
131+
132+
// The remote port's ScriptExecutionContext is guaranteed not to change here - MessagePort::contextDestroyed()
133+
// will close the port before the context goes away, and close() will block because we are holding the mutex.
134+
ScriptExecutionContext* remoteContext = m_remotePort->scriptExecutionContext();
135+
if (remoteContext == context || (remoteContext && remoteContext->isDocument() && context->isDocument()))
136+
return m_remotePort;
137+
138+
return nullptr;
139+
}
140+
141+
RefPtr<InProcessMessagePortChannel> InProcessMessagePortChannel::takeEntangledChannel()
142+
{
143+
RefPtr<InProcessMessagePortChannel> channel;
144+
145+
{
146+
Locker<Lock> locker(m_lock);
147+
channel = WTFMove(m_entangledChannel);
148+
}
149+
150+
return channel;
151+
}
152+
153+
void InProcessMessagePortChannel::close()
154+
{
155+
Locker<Lock> locker(m_lock);
156+
157+
RefPtr<InProcessMessagePortChannel> channel;
158+
if (m_entangledChannel) {
159+
channel = m_entangledChannel->takeEntangledChannel();
160+
ASSERT(channel == this);
161+
m_entangledChannel->close();
162+
}
163+
164+
// Disentangle ourselves from the other end. We still maintain a reference to our incoming queue, since previously-existing messages should still be delivered.
165+
m_remotePort = nullptr;
166+
m_outgoingQueue = nullptr;
167+
m_entangledChannel = nullptr;
168+
}
169+
170+
void InProcessMessagePortChannel::setRemotePort(MessagePort* port)
171+
{
172+
Locker<Lock> locker(m_lock);
173+
174+
// Should never set port if it is already set.
175+
ASSERT(!port || !m_remotePort);
176+
177+
m_remotePort = port;
178+
}
179+
180+
} // namespace WebCore

0 commit comments

Comments
 (0)