2014-11-03 15:03:49 +03:00
|
|
|
/* -*- 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/. */
|
|
|
|
|
2015-06-15 15:44:03 +03:00
|
|
|
#include "DaemonSocket.h"
|
|
|
|
#include "mozilla/ipc/DaemonSocketConsumer.h"
|
|
|
|
#include "mozilla/ipc/DaemonSocketPDU.h"
|
2016-03-22 13:26:45 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2015-07-21 09:06:29 +03:00
|
|
|
#include "nsISupportsImpl.h" // for MOZ_COUNT_CTOR, MOZ_COUNT_DTOR
|
2014-11-03 15:03:49 +03:00
|
|
|
|
|
|
|
#ifdef CHROMIUM_LOG
|
|
|
|
#undef CHROMIUM_LOG
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MOZ_WIDGET_GONK)
|
|
|
|
#include <android/log.h>
|
|
|
|
#define CHROMIUM_LOG(args...) __android_log_print(ANDROID_LOG_INFO, "I/O", args);
|
|
|
|
#else
|
|
|
|
#include <stdio.h>
|
|
|
|
#define IODEBUG true
|
|
|
|
#define CHROMIUM_LOG(args...) if (IODEBUG) printf(args);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
|
|
|
//
|
2015-06-15 15:44:03 +03:00
|
|
|
// DaemonSocketIO
|
2014-11-03 15:03:49 +03:00
|
|
|
//
|
|
|
|
|
2015-06-15 15:44:03 +03:00
|
|
|
class DaemonSocketIO final : public ConnectionOrientedSocketIO
|
2014-11-03 15:03:49 +03:00
|
|
|
{
|
|
|
|
public:
|
2015-06-15 15:44:03 +03:00
|
|
|
DaemonSocketIO(MessageLoop* aConsumerLoop,
|
|
|
|
MessageLoop* aIOLoop,
|
|
|
|
int aFd, ConnectionStatus aConnectionStatus,
|
|
|
|
UnixSocketConnector* aConnector,
|
2015-06-15 15:44:03 +03:00
|
|
|
DaemonSocket* aConnection,
|
2015-06-15 15:44:03 +03:00
|
|
|
DaemonSocketIOConsumer* aConsumer);
|
2014-11-03 15:03:49 +03:00
|
|
|
|
2015-07-21 09:06:29 +03:00
|
|
|
~DaemonSocketIO();
|
|
|
|
|
2015-04-30 13:55:37 +03:00
|
|
|
// Methods for |DataSocketIO|
|
|
|
|
//
|
2014-11-03 15:03:49 +03:00
|
|
|
|
2015-04-30 13:55:37 +03:00
|
|
|
nsresult QueryReceiveBuffer(UnixSocketIOBuffer** aBuffer) override;
|
|
|
|
void ConsumeBuffer() override;
|
|
|
|
void DiscardBuffer() override;
|
2014-11-03 15:03:49 +03:00
|
|
|
|
2015-04-30 13:55:37 +03:00
|
|
|
// Methods for |SocketIOBase|
|
|
|
|
//
|
2014-11-03 15:03:49 +03:00
|
|
|
|
2015-04-30 13:55:37 +03:00
|
|
|
SocketBase* GetSocketBase() override;
|
2014-11-03 15:03:49 +03:00
|
|
|
|
2015-06-02 11:01:58 +03:00
|
|
|
bool IsShutdownOnConsumerThread() const override;
|
2015-04-30 13:55:37 +03:00
|
|
|
bool IsShutdownOnIOThread() const override;
|
|
|
|
|
2015-06-02 11:01:58 +03:00
|
|
|
void ShutdownOnConsumerThread() override;
|
2015-04-30 13:55:37 +03:00
|
|
|
void ShutdownOnIOThread() override;
|
2014-11-03 15:03:49 +03:00
|
|
|
|
2015-04-30 13:55:37 +03:00
|
|
|
private:
|
2015-06-15 15:44:03 +03:00
|
|
|
DaemonSocket* mConnection;
|
2015-06-15 15:44:03 +03:00
|
|
|
DaemonSocketIOConsumer* mConsumer;
|
2016-03-22 13:26:45 +03:00
|
|
|
UniquePtr<DaemonSocketPDU> mPDU;
|
2014-11-03 15:03:49 +03:00
|
|
|
bool mShuttingDownOnIOThread;
|
|
|
|
};
|
|
|
|
|
2015-06-15 15:44:03 +03:00
|
|
|
DaemonSocketIO::DaemonSocketIO(
|
2015-06-09 10:50:10 +03:00
|
|
|
MessageLoop* aConsumerLoop,
|
2015-06-02 11:01:57 +03:00
|
|
|
MessageLoop* aIOLoop,
|
|
|
|
int aFd,
|
2015-01-20 05:17:25 +03:00
|
|
|
ConnectionStatus aConnectionStatus,
|
2015-06-03 12:53:50 +03:00
|
|
|
UnixSocketConnector* aConnector,
|
2015-06-15 15:44:03 +03:00
|
|
|
DaemonSocket* aConnection,
|
2015-06-15 15:44:03 +03:00
|
|
|
DaemonSocketIOConsumer* aConsumer)
|
2015-06-09 10:50:10 +03:00
|
|
|
: ConnectionOrientedSocketIO(aConsumerLoop,
|
2015-06-03 12:53:50 +03:00
|
|
|
aIOLoop,
|
|
|
|
aFd,
|
2015-06-03 12:53:50 +03:00
|
|
|
aConnectionStatus,
|
|
|
|
aConnector)
|
2015-06-02 11:01:57 +03:00
|
|
|
, mConnection(aConnection)
|
|
|
|
, mConsumer(aConsumer)
|
|
|
|
, mShuttingDownOnIOThread(false)
|
2014-11-03 15:03:49 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mConnection);
|
|
|
|
MOZ_ASSERT(mConsumer);
|
2015-07-21 09:06:29 +03:00
|
|
|
|
|
|
|
MOZ_COUNT_CTOR_INHERITED(DaemonSocketIO, ConnectionOrientedSocketIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
DaemonSocketIO::~DaemonSocketIO()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR_INHERITED(DaemonSocketIO, ConnectionOrientedSocketIO);
|
2015-01-20 05:17:25 +03:00
|
|
|
}
|
|
|
|
|
2015-04-30 13:55:37 +03:00
|
|
|
// |DataSocketIO|
|
|
|
|
|
|
|
|
nsresult
|
2015-06-15 15:44:03 +03:00
|
|
|
DaemonSocketIO::QueryReceiveBuffer(UnixSocketIOBuffer** aBuffer)
|
2015-04-30 13:55:37 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aBuffer);
|
|
|
|
|
|
|
|
if (!mPDU) {
|
|
|
|
/* There's only one PDU for receiving. We reuse it every time. */
|
2016-03-22 13:26:45 +03:00
|
|
|
mPDU = MakeUnique<DaemonSocketPDU>(DaemonSocketPDU::PDU_MAX_PAYLOAD_LENGTH);
|
2015-04-30 13:55:37 +03:00
|
|
|
}
|
|
|
|
*aBuffer = mPDU.get();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-06-15 15:44:03 +03:00
|
|
|
DaemonSocketIO::ConsumeBuffer()
|
2015-04-30 13:55:37 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mConsumer);
|
|
|
|
|
|
|
|
mConsumer->Handle(*mPDU);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-06-15 15:44:03 +03:00
|
|
|
DaemonSocketIO::DiscardBuffer()
|
2015-04-30 13:55:37 +03:00
|
|
|
{
|
|
|
|
// Nothing to do.
|
|
|
|
}
|
|
|
|
|
|
|
|
// |SocketIOBase|
|
|
|
|
|
|
|
|
SocketBase*
|
2015-06-15 15:44:03 +03:00
|
|
|
DaemonSocketIO::GetSocketBase()
|
2015-04-30 13:55:37 +03:00
|
|
|
{
|
|
|
|
return mConnection;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-06-15 15:44:03 +03:00
|
|
|
DaemonSocketIO::IsShutdownOnConsumerThread() const
|
2015-04-30 13:55:37 +03:00
|
|
|
{
|
2015-06-02 11:01:57 +03:00
|
|
|
MOZ_ASSERT(IsConsumerThread());
|
2015-04-30 13:55:37 +03:00
|
|
|
|
|
|
|
return mConnection == nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-06-15 15:44:03 +03:00
|
|
|
DaemonSocketIO::IsShutdownOnIOThread() const
|
2015-04-30 13:55:37 +03:00
|
|
|
{
|
|
|
|
return mShuttingDownOnIOThread;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-06-15 15:44:03 +03:00
|
|
|
DaemonSocketIO::ShutdownOnConsumerThread()
|
2015-04-30 13:55:37 +03:00
|
|
|
{
|
2015-06-02 11:01:57 +03:00
|
|
|
MOZ_ASSERT(IsConsumerThread());
|
2015-06-02 11:01:58 +03:00
|
|
|
MOZ_ASSERT(!IsShutdownOnConsumerThread());
|
2015-04-30 13:55:37 +03:00
|
|
|
|
|
|
|
mConnection = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-06-15 15:44:03 +03:00
|
|
|
DaemonSocketIO::ShutdownOnIOThread()
|
2015-04-30 13:55:37 +03:00
|
|
|
{
|
2015-06-02 11:01:57 +03:00
|
|
|
MOZ_ASSERT(!IsConsumerThread());
|
2015-04-30 13:55:37 +03:00
|
|
|
MOZ_ASSERT(!mShuttingDownOnIOThread);
|
|
|
|
|
|
|
|
Close(); // will also remove fd from I/O loop
|
|
|
|
mShuttingDownOnIOThread = true;
|
|
|
|
}
|
|
|
|
|
2014-11-03 15:03:49 +03:00
|
|
|
//
|
2015-06-15 15:44:03 +03:00
|
|
|
// DaemonSocket
|
2014-11-03 15:03:49 +03:00
|
|
|
//
|
|
|
|
|
2015-06-15 15:44:03 +03:00
|
|
|
DaemonSocket::DaemonSocket(
|
2015-06-15 15:44:03 +03:00
|
|
|
DaemonSocketIOConsumer* aIOConsumer,
|
2015-06-15 15:44:03 +03:00
|
|
|
DaemonSocketConsumer* aConsumer,
|
2015-05-26 19:29:02 +03:00
|
|
|
int aIndex)
|
2015-06-02 11:01:57 +03:00
|
|
|
: mIO(nullptr)
|
2015-06-15 15:44:03 +03:00
|
|
|
, mIOConsumer(aIOConsumer)
|
2015-05-26 19:29:02 +03:00
|
|
|
, mConsumer(aConsumer)
|
|
|
|
, mIndex(aIndex)
|
2015-05-21 14:34:37 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mConsumer);
|
2015-07-21 09:06:29 +03:00
|
|
|
|
|
|
|
MOZ_COUNT_CTOR_INHERITED(DaemonSocket, ConnectionOrientedSocket);
|
2015-05-21 14:34:37 +03:00
|
|
|
}
|
2014-11-03 15:03:49 +03:00
|
|
|
|
2015-06-15 15:44:03 +03:00
|
|
|
DaemonSocket::~DaemonSocket()
|
2015-07-21 09:06:29 +03:00
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR_INHERITED(DaemonSocket, ConnectionOrientedSocket);
|
|
|
|
}
|
2014-11-03 15:03:49 +03:00
|
|
|
|
2015-05-21 14:34:37 +03:00
|
|
|
// |ConnectionOrientedSocket|
|
|
|
|
|
|
|
|
nsresult
|
2015-06-15 15:44:03 +03:00
|
|
|
DaemonSocket::PrepareAccept(UnixSocketConnector* aConnector,
|
|
|
|
MessageLoop* aConsumerLoop,
|
|
|
|
MessageLoop* aIOLoop,
|
|
|
|
ConnectionOrientedSocketIO*& aIO)
|
2014-11-03 15:03:49 +03:00
|
|
|
{
|
2015-04-30 13:55:37 +03:00
|
|
|
MOZ_ASSERT(!mIO);
|
2014-11-03 15:03:49 +03:00
|
|
|
|
2015-04-30 13:55:37 +03:00
|
|
|
SetConnectionStatus(SOCKET_CONNECTING);
|
2014-11-03 15:03:49 +03:00
|
|
|
|
2015-06-15 15:44:03 +03:00
|
|
|
mIO = new DaemonSocketIO(
|
2015-06-09 10:50:10 +03:00
|
|
|
aConsumerLoop, aIOLoop, -1, UnixSocketWatcher::SOCKET_IS_CONNECTING,
|
2015-06-15 15:44:03 +03:00
|
|
|
aConnector, this, mIOConsumer);
|
2015-05-21 14:34:37 +03:00
|
|
|
aIO = mIO;
|
2014-11-03 15:03:49 +03:00
|
|
|
|
2015-05-21 14:34:37 +03:00
|
|
|
return NS_OK;
|
2015-01-20 05:17:25 +03:00
|
|
|
}
|
|
|
|
|
2015-04-30 13:55:37 +03:00
|
|
|
// |DataSocket|
|
|
|
|
|
|
|
|
void
|
2015-06-15 15:44:03 +03:00
|
|
|
DaemonSocket::SendSocketData(UnixSocketIOBuffer* aBuffer)
|
2015-01-20 05:17:25 +03:00
|
|
|
{
|
2015-04-30 13:55:37 +03:00
|
|
|
MOZ_ASSERT(mIO);
|
2015-06-02 11:01:57 +03:00
|
|
|
MOZ_ASSERT(mIO->IsConsumerThread());
|
2015-01-20 05:17:25 +03:00
|
|
|
|
2015-06-02 11:01:57 +03:00
|
|
|
mIO->GetIOLoop()->PostTask(
|
2016-05-02 20:27:15 +03:00
|
|
|
MakeAndAddRef<SocketIOSendTask<DaemonSocketIO, UnixSocketIOBuffer>>(
|
|
|
|
mIO, aBuffer));
|
2015-04-30 13:55:37 +03:00
|
|
|
}
|
2015-01-20 05:17:25 +03:00
|
|
|
|
2015-04-30 13:55:37 +03:00
|
|
|
// |SocketBase|
|
2015-01-20 05:17:25 +03:00
|
|
|
|
2015-04-30 13:55:37 +03:00
|
|
|
void
|
2015-06-15 15:44:03 +03:00
|
|
|
DaemonSocket::Close()
|
2015-04-30 13:55:37 +03:00
|
|
|
{
|
|
|
|
if (!mIO) {
|
2015-06-15 15:44:03 +03:00
|
|
|
CHROMIUM_LOG("HAL daemon already disconnected!");
|
2015-04-30 13:55:37 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-02 11:01:57 +03:00
|
|
|
MOZ_ASSERT(mIO->IsConsumerThread());
|
|
|
|
|
2015-06-02 11:01:58 +03:00
|
|
|
mIO->ShutdownOnConsumerThread();
|
2016-05-02 20:27:15 +03:00
|
|
|
mIO->GetIOLoop()->PostTask(MakeAndAddRef<SocketIOShutdownTask>(mIO));
|
2015-04-30 13:55:37 +03:00
|
|
|
mIO = nullptr;
|
|
|
|
|
|
|
|
NotifyDisconnect();
|
2015-01-20 05:17:25 +03:00
|
|
|
}
|
|
|
|
|
2015-05-26 19:29:02 +03:00
|
|
|
void
|
2015-06-15 15:44:03 +03:00
|
|
|
DaemonSocket::OnConnectSuccess()
|
2015-05-26 19:29:02 +03:00
|
|
|
{
|
|
|
|
mConsumer->OnConnectSuccess(mIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-06-15 15:44:03 +03:00
|
|
|
DaemonSocket::OnConnectError()
|
2015-05-26 19:29:02 +03:00
|
|
|
{
|
|
|
|
mConsumer->OnConnectError(mIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-06-15 15:44:03 +03:00
|
|
|
DaemonSocket::OnDisconnect()
|
2015-05-26 19:29:02 +03:00
|
|
|
{
|
|
|
|
mConsumer->OnDisconnect(mIndex);
|
|
|
|
}
|
|
|
|
|
2014-11-03 15:03:49 +03:00
|
|
|
}
|
|
|
|
}
|