Files
palemoon27/ipc/unixsocket/StreamSocket.cpp
T
roytam1 36b2354fd4 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1186307 - StructuredCloneHelper in workers.postMessage(), r=smaug (0d3c817afa)
- Bug 1183824 - Remove NS_ENSURE_TRUE(mCallback, NS_ERROR_NOT_INITIALIZED) warning from nsFrameMessageManager::DispatchAsyncMessageInternal. r=smaug (64dfd50f7f)
- Bug 1167189: Use a size annotation on the OOM abort. r=bholley (5e7fb331a0)
- missing crash reporter (47f183fab4)
- Bug 1137151: Marked destructor of |nsClipboard| as protected, r=mwu (5655bf8862)
- Bug 1194497 - Convert a few remaining PRUnichar to char16_t. r=roc (1554ffcec2)
- Bug 952456 - Part 1: Implement gonk/nsClipboard for rich text and raw image. r=fabrice (419381f7af)
- Bug 1198795 - ipc/StructuredCloneUtils should be merged with StructuredCloneHelper, r=smaug (02183262bd)
- Bug 1201806 - part 1 - Remove OwningSerializedStructuredCloneBuffer and use StructuredCloneIPCHelper everywhere in IPC code, r=smaug (6dc69151b4)
- Bug 1201806 - part 2 - nsStructuredCloneContainer should use StructuredCloneIPCHelper, r=smaug (b6ebaa8e95)
- Bug 1202028 - Implement range based for loops for SavedFrame stacks. r=terrence (57c198910c)
- missing bit of  Bug 1106264 (fa67f8ab56)
- Bug 1168806: Configurable I/O thread for socket IPC classes, r=kmachulis (213a8fb2b9)
- Bug 1159209: Remove template parameters from |DataSocketIO|, r=kmachulis (0d658c5cf8)
- Bug 1168806: Configurable consumer thread for socket IPC classes, r=kmachulis (ee054dd6b0)
- Bug 1167207: Add |BluetoothDaemonConnectionConsumer|, r=shuang (b8b89290df)
- Bug 1154281: Remove |UnixSocketConsumer| and its helpers, r=kmachulis (d40430b10f)
- Bug 1166638: Add |StreamSocketConsumer|, r=kmachulis (59b5c1abf3)
- Bug 1166638: Add |ListenSocketConsumer|, r=kmachulis (d119bcaa2a)
- Bug 1168806: Use 'consumer thread' in socket interface, r=kmachulis (65d232d21c)
- Bug 1170466: Inherit |ConnectionOrientedSocketIO| from |UnixSocketWatcher|, r=kmachulis (b91d0c624a)
- Bug 1170466: Share socket I/O methods in |ConnectionOrientedSocketIO|, r=kmachulis (108fb6696e)
- Bug 1170466: Implement accept and connect in |ConnectionOrientedSocketIO|, r=kmachulis (590b3b4617)
- Bug 1172479: Replace |nsIThread| by |MessageLoop| in socket I/O code, r=kmachulis (618073dcef)
- Bug 1203426 - Rename StructuredCloneIPCHelper to StructuredCloneData, r=smaug (dc31124113)
- Bug 1167392 - Change nsFrameMessageManager::mParentManager to a smart pointer. r=ehsan (98d60c0398)
- Bug 1186843 - Always cache delayed scripts in message manager (b0ab501959)
- Bug 1189550 - In parent process, cpmm.initialProcessData should be same as ppmm.initialProcessData (r=smaug) (7a8ab62432)
- Bug 1196057 - Use channel->Open2() in dom/base/nsFrameMessageManager.cpp (r=sicking) (a032d483d6)
- missing parts of Bug 1132072 - Tab switch refactoring (r=mconley) (e6fbbb722a)
- Bug 1175736 - Implement the iframe referrer attribute. r=ckerschb, r=bz (a5fc4e683b)
- Bug 1171017: Rename |BluetoothDaemonConnectionConsumer| to |DaemonSocketConsumer|, r=shuang (0af5957609)
- Bug 1170993: Manage socket buffers in sub-classes of |UnixSocketBuffer|, r=kmachulis (960de8be45)
- Bug 1171017: Rename |BluetoothDaemonPDUConsumer| to |DaemonSocketIOConsumer|, r=shuang (eea1681ab8)
- Bug 1171100 - Fix incorrect logic of handling Bluetooth Gatt Responses in daemon backend. r=tzimmermann (c385364d1e)
- Bug 1171866 - Remove ReversedUuidToString; f=tzimmermann, r=jocelyn (9b58176c39)
- Bug 1171017: Rename |BluetoothSocketPDU| to |DaemonSocketPDU|, r=shuang (ce1bbd9d4a)
- Bug 1171017: Rename |BluetoothDaemonConnectionIO| to |DaemonSocketIO|, r=shuang (00f2a38220)
- Bug 1171017: Rename |BluetoothDaemonConnection| to |DaemonSocket|, r=shuang (b3c3522719)
- Bug 1171017: Move classes from ipc/bluetooth to ipc/hal, r=shuang (047714aee4)
- Bug 1171994: Add |RilSocket| and |RilSocketConsumer|, r=htsai (276211bbb7)
- Bug 1171994: Forward received RIL socket I/O via |WorkerCrossThreadDispatcher|, r=htsai (7825d1c837)
- Bug 1185478: Add leak checks to socket I/O classes (40e2c01ce4)
- Bug 1185478: Remove strong references from socket I/O class to socket classes (6fa94f34ff)
- Bug 1163912: Set |Type| property of BluetoothDevice a initial value during LeScan; f=elin, r=jocelyn (bda66689ed)
- Bug 1166215 - Cast 'BluetoothTypeOfDevice' to uint32_t before the BT signal is distributed to BluetoothAdapter. r=joliu (d38ad4e55f)
- Bug 1170086 - Fix the parameter type of DispatchReplyError() in StartLeScan result handler. r=joliu (a24e23e02f)
- Bug 1161956 - Fix crashes while using GATT client functions. r=jocelyn (cebfb8c700)
- Bug 1171868 - Remove GeneratePathFromGattId(aId, aPath, aUuidStr); r=jocelyn (0eac291b3c)
- Bug 1161945 - Fix empty characteristics and descriptors after searching services. f=elin, r=jocelyn (a0d816fee4)
- Bug 1164387 - Use |btgatt_client_interface_t.get_device_type| to get the device type. r=jocelyn (c7b5c97a00)
- Bug 1173139 - Reorder global creation on workers, r=khuey. (acb79ae54e)
- Bug 1170071 - Part 1: Revise BluetoothGattInterface, result handler, and interface helper for daemon support of gatt server. r=shuang (0ad0c9244e)
- Bug 1170071 - Part 2: Add helpers for Bluetooth daemon GATT server support. r=shuang (46a4062579)
- Bug 1170071 - Part 3: Add gatt server support to GATT module for Bluetooth daemon. r=shuang (71d80aabea)
- Bug 1170071 - Part 4: Add Gatt server interfaces for Bluetooth daemon. r=shuang (a556aaf048)
- Bug 1187230: Convert Bluetooth to use daemon runnables, r=shuang (9be6501225)
- Bug 1204382: Add #include for nsIInputStream.h to MediaParent.cpp, to fix a build error in --disable-webrtc builds. r=jib (8b20e934)
2022-03-18 12:44:46 +08:00

477 lines
10 KiB
C++

/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "StreamSocket.h"
#include <fcntl.h>
#include "mozilla/RefPtr.h"
#include "nsISupportsImpl.h" // for MOZ_COUNT_CTOR, MOZ_COUNT_DTOR
#include "nsXULAppAPI.h"
#include "StreamSocketConsumer.h"
#include "UnixSocketConnector.h"
static const size_t MAX_READ_SIZE = 1 << 16;
namespace mozilla {
namespace ipc {
//
// StreamSocketIO
//
class StreamSocketIO final : public ConnectionOrientedSocketIO
{
public:
class ConnectTask;
class DelayedConnectTask;
class ReceiveTask;
StreamSocketIO(MessageLoop* aConsumerLoop,
MessageLoop* aIOLoop,
StreamSocket* aStreamSocket,
UnixSocketConnector* aConnector);
StreamSocketIO(MessageLoop* aConsumerLoop,
MessageLoop* aIOLoop,
int aFd, ConnectionStatus aConnectionStatus,
StreamSocket* aStreamSocket,
UnixSocketConnector* aConnector);
~StreamSocketIO();
StreamSocket* GetStreamSocket();
DataSocket* GetDataSocket();
// Delayed-task handling
//
void SetDelayedConnectTask(CancelableTask* aTask);
void ClearDelayedConnectTask();
void CancelDelayedConnectTask();
// Methods for |DataSocket|
//
nsresult QueryReceiveBuffer(UnixSocketIOBuffer** aBuffer) override;
void ConsumeBuffer() override;
void DiscardBuffer() override;
// Methods for |SocketIOBase|
//
SocketBase* GetSocketBase() override;
bool IsShutdownOnConsumerThread() const override;
bool IsShutdownOnIOThread() const override;
void ShutdownOnConsumerThread() override;
void ShutdownOnIOThread() override;
private:
/**
* Consumer pointer. Non-thread-safe pointer, so should only be manipulated
* directly from consumer thread. All non-consumer-thread accesses should
* happen with mIO as container.
*/
StreamSocket* mStreamSocket;
/**
* If true, do not requeue whatever task we're running
*/
bool mShuttingDownOnIOThread;
/**
* Task member for delayed connect task. Should only be access on consumer
* thread.
*/
CancelableTask* mDelayedConnectTask;
/**
* I/O buffer for received data
*/
nsAutoPtr<UnixSocketRawData> mBuffer;
};
StreamSocketIO::StreamSocketIO(MessageLoop* aConsumerLoop,
MessageLoop* aIOLoop,
StreamSocket* aStreamSocket,
UnixSocketConnector* aConnector)
: ConnectionOrientedSocketIO(aConsumerLoop, aIOLoop, aConnector)
, mStreamSocket(aStreamSocket)
, mShuttingDownOnIOThread(false)
, mDelayedConnectTask(nullptr)
{
MOZ_ASSERT(mStreamSocket);
MOZ_COUNT_CTOR_INHERITED(StreamSocketIO, ConnectionOrientedSocketIO);
}
StreamSocketIO::StreamSocketIO(MessageLoop* aConsumerLoop,
MessageLoop* aIOLoop,
int aFd, ConnectionStatus aConnectionStatus,
StreamSocket* aStreamSocket,
UnixSocketConnector* aConnector)
: ConnectionOrientedSocketIO(aConsumerLoop,
aIOLoop,
aFd,
aConnectionStatus,
aConnector)
, mStreamSocket(aStreamSocket)
, mShuttingDownOnIOThread(false)
, mDelayedConnectTask(nullptr)
{
MOZ_ASSERT(mStreamSocket);
MOZ_COUNT_CTOR_INHERITED(StreamSocketIO, ConnectionOrientedSocketIO);
}
StreamSocketIO::~StreamSocketIO()
{
MOZ_ASSERT(IsConsumerThread());
MOZ_ASSERT(IsShutdownOnConsumerThread());
MOZ_COUNT_DTOR_INHERITED(StreamSocketIO, ConnectionOrientedSocketIO);
}
StreamSocket*
StreamSocketIO::GetStreamSocket()
{
return mStreamSocket;
}
DataSocket*
StreamSocketIO::GetDataSocket()
{
return GetStreamSocket();
}
void
StreamSocketIO::SetDelayedConnectTask(CancelableTask* aTask)
{
MOZ_ASSERT(IsConsumerThread());
mDelayedConnectTask = aTask;
}
void
StreamSocketIO::ClearDelayedConnectTask()
{
MOZ_ASSERT(IsConsumerThread());
mDelayedConnectTask = nullptr;
}
void
StreamSocketIO::CancelDelayedConnectTask()
{
MOZ_ASSERT(IsConsumerThread());
if (!mDelayedConnectTask) {
return;
}
mDelayedConnectTask->Cancel();
ClearDelayedConnectTask();
}
// |DataSocketIO|
nsresult
StreamSocketIO::QueryReceiveBuffer(UnixSocketIOBuffer** aBuffer)
{
MOZ_ASSERT(aBuffer);
if (!mBuffer) {
mBuffer = new UnixSocketRawData(MAX_READ_SIZE);
}
*aBuffer = mBuffer.get();
return NS_OK;
}
/**
* |ReceiveTask| transfers data received on the I/O thread
* to an instance of |StreamSocket| on the consumer thread.
*/
class StreamSocketIO::ReceiveTask final : public SocketTask<StreamSocketIO>
{
public:
ReceiveTask(StreamSocketIO* aIO, UnixSocketBuffer* aBuffer)
: SocketTask<StreamSocketIO>(aIO)
, mBuffer(aBuffer)
{
MOZ_COUNT_CTOR(ReceiveTask);
}
~ReceiveTask()
{
MOZ_COUNT_DTOR(ReceiveTask);
}
void Run() override
{
StreamSocketIO* io = SocketTask<StreamSocketIO>::GetIO();
MOZ_ASSERT(io->IsConsumerThread());
if (NS_WARN_IF(io->IsShutdownOnConsumerThread())) {
// Since we've already explicitly closed and the close
// happened before this, this isn't really an error.
return;
}
StreamSocket* streamSocket = io->GetStreamSocket();
MOZ_ASSERT(streamSocket);
streamSocket->ReceiveSocketData(mBuffer);
}
private:
nsAutoPtr<UnixSocketBuffer> mBuffer;
};
void
StreamSocketIO::ConsumeBuffer()
{
GetConsumerThread()->PostTask(FROM_HERE,
new ReceiveTask(this, mBuffer.forget()));
}
void
StreamSocketIO::DiscardBuffer()
{
// Nothing to do.
}
// |SocketIOBase|
SocketBase*
StreamSocketIO::GetSocketBase()
{
return GetDataSocket();
}
bool
StreamSocketIO::IsShutdownOnConsumerThread() const
{
MOZ_ASSERT(IsConsumerThread());
return mStreamSocket == nullptr;
}
bool
StreamSocketIO::IsShutdownOnIOThread() const
{
return mShuttingDownOnIOThread;
}
void
StreamSocketIO::ShutdownOnConsumerThread()
{
MOZ_ASSERT(IsConsumerThread());
MOZ_ASSERT(!IsShutdownOnConsumerThread());
mStreamSocket = nullptr;
}
void
StreamSocketIO::ShutdownOnIOThread()
{
MOZ_ASSERT(!IsConsumerThread());
MOZ_ASSERT(!mShuttingDownOnIOThread);
Close(); // will also remove fd from I/O loop
mShuttingDownOnIOThread = true;
}
//
// Socket tasks
//
class StreamSocketIO::ConnectTask final : public SocketIOTask<StreamSocketIO>
{
public:
ConnectTask(StreamSocketIO* aIO)
: SocketIOTask<StreamSocketIO>(aIO)
{
MOZ_COUNT_CTOR(ReceiveTask);
}
~ConnectTask()
{
MOZ_COUNT_DTOR(ReceiveTask);
}
void Run() override
{
MOZ_ASSERT(!GetIO()->IsConsumerThread());
MOZ_ASSERT(!IsCanceled());
GetIO()->Connect();
}
};
class StreamSocketIO::DelayedConnectTask final
: public SocketIOTask<StreamSocketIO>
{
public:
DelayedConnectTask(StreamSocketIO* aIO)
: SocketIOTask<StreamSocketIO>(aIO)
{
MOZ_COUNT_CTOR(DelayedConnectTask);
}
~DelayedConnectTask()
{
MOZ_COUNT_DTOR(DelayedConnectTask);
}
void Run() override
{
MOZ_ASSERT(GetIO()->IsConsumerThread());
if (IsCanceled()) {
return;
}
StreamSocketIO* io = GetIO();
if (io->IsShutdownOnConsumerThread()) {
return;
}
io->ClearDelayedConnectTask();
io->GetIOLoop()->PostTask(FROM_HERE, new ConnectTask(io));
}
};
//
// StreamSocket
//
StreamSocket::StreamSocket(StreamSocketConsumer* aConsumer, int aIndex)
: mIO(nullptr)
, mConsumer(aConsumer)
, mIndex(aIndex)
{
MOZ_ASSERT(mConsumer);
MOZ_COUNT_CTOR_INHERITED(StreamSocket, ConnectionOrientedSocket);
}
StreamSocket::~StreamSocket()
{
MOZ_ASSERT(!mIO);
MOZ_COUNT_DTOR_INHERITED(StreamSocket, ConnectionOrientedSocket);
}
void
StreamSocket::ReceiveSocketData(nsAutoPtr<UnixSocketBuffer>& aBuffer)
{
mConsumer->ReceiveSocketData(mIndex, aBuffer);
}
nsresult
StreamSocket::Connect(UnixSocketConnector* aConnector, int aDelayMs,
MessageLoop* aConsumerLoop, MessageLoop* aIOLoop)
{
MOZ_ASSERT(!mIO);
mIO = new StreamSocketIO(aConsumerLoop, aIOLoop, this, aConnector);
SetConnectionStatus(SOCKET_CONNECTING);
if (aDelayMs > 0) {
StreamSocketIO::DelayedConnectTask* connectTask =
new StreamSocketIO::DelayedConnectTask(mIO);
mIO->SetDelayedConnectTask(connectTask);
MessageLoop::current()->PostDelayedTask(FROM_HERE, connectTask, aDelayMs);
} else {
aIOLoop->PostTask(FROM_HERE, new StreamSocketIO::ConnectTask(mIO));
}
return NS_OK;
}
nsresult
StreamSocket::Connect(UnixSocketConnector* aConnector, int aDelayMs)
{
return Connect(aConnector, aDelayMs,
MessageLoop::current(), XRE_GetIOMessageLoop());
}
// |ConnectionOrientedSocket|
nsresult
StreamSocket::PrepareAccept(UnixSocketConnector* aConnector,
MessageLoop* aConsumerLoop,
MessageLoop* aIOLoop,
ConnectionOrientedSocketIO*& aIO)
{
MOZ_ASSERT(!mIO);
MOZ_ASSERT(aConnector);
SetConnectionStatus(SOCKET_CONNECTING);
mIO = new StreamSocketIO(aConsumerLoop, aIOLoop,
-1, UnixSocketWatcher::SOCKET_IS_CONNECTING,
this, aConnector);
aIO = mIO;
return NS_OK;
}
// |DataSocket|
void
StreamSocket::SendSocketData(UnixSocketIOBuffer* aBuffer)
{
MOZ_ASSERT(mIO);
MOZ_ASSERT(mIO->IsConsumerThread());
MOZ_ASSERT(!mIO->IsShutdownOnConsumerThread());
mIO->GetIOLoop()->PostTask(
FROM_HERE,
new SocketIOSendTask<StreamSocketIO, UnixSocketIOBuffer>(mIO, aBuffer));
}
// |SocketBase|
void
StreamSocket::Close()
{
MOZ_ASSERT(mIO);
MOZ_ASSERT(mIO->IsConsumerThread());
mIO->CancelDelayedConnectTask();
// From this point on, we consider |mIO| as being deleted. We sever
// the relationship here so any future calls to |Connect| will create
// a new I/O object.
mIO->ShutdownOnConsumerThread();
mIO->GetIOLoop()->PostTask(FROM_HERE, new SocketIOShutdownTask(mIO));
mIO = nullptr;
NotifyDisconnect();
}
void
StreamSocket::OnConnectSuccess()
{
mConsumer->OnConnectSuccess(mIndex);
}
void
StreamSocket::OnConnectError()
{
mConsumer->OnConnectError(mIndex);
}
void
StreamSocket::OnDisconnect()
{
mConsumer->OnDisconnect(mIndex);
}
} // namespace ipc
} // namespace mozilla