summaryrefslogtreecommitdiff
path: root/ipc/hal/DaemonSocket.cpp
blob: 94c36b9551149cc324c395470ccdfe5d01c0140e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* -*- 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 "DaemonSocket.h"
#include "mozilla/ipc/DaemonSocketConsumer.h"
#include "mozilla/ipc/DaemonSocketPDU.h"
#include "mozilla/UniquePtr.h"
#include "nsISupportsImpl.h" // for MOZ_COUNT_CTOR, MOZ_COUNT_DTOR

#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 {

//
// DaemonSocketIO
//

class DaemonSocketIO final : public ConnectionOrientedSocketIO
{
public:
  DaemonSocketIO(MessageLoop* aConsumerLoop,
                 MessageLoop* aIOLoop,
                 int aFd, ConnectionStatus aConnectionStatus,
                 UnixSocketConnector* aConnector,
                 DaemonSocket* aConnection,
                 DaemonSocketIOConsumer* aConsumer);

  ~DaemonSocketIO();

  // Methods for |DataSocketIO|
  //

  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:
  DaemonSocket* mConnection;
  DaemonSocketIOConsumer* mConsumer;
  UniquePtr<DaemonSocketPDU> mPDU;
  bool mShuttingDownOnIOThread;
};

DaemonSocketIO::DaemonSocketIO(
  MessageLoop* aConsumerLoop,
  MessageLoop* aIOLoop,
  int aFd,
  ConnectionStatus aConnectionStatus,
  UnixSocketConnector* aConnector,
  DaemonSocket* aConnection,
  DaemonSocketIOConsumer* aConsumer)
  : ConnectionOrientedSocketIO(aConsumerLoop,
                               aIOLoop,
                               aFd,
                               aConnectionStatus,
                               aConnector)
  , mConnection(aConnection)
  , mConsumer(aConsumer)
  , mShuttingDownOnIOThread(false)
{
  MOZ_ASSERT(mConnection);
  MOZ_ASSERT(mConsumer);

  MOZ_COUNT_CTOR_INHERITED(DaemonSocketIO, ConnectionOrientedSocketIO);
}

DaemonSocketIO::~DaemonSocketIO()
{
  MOZ_COUNT_DTOR_INHERITED(DaemonSocketIO, ConnectionOrientedSocketIO);
}

// |DataSocketIO|

nsresult
DaemonSocketIO::QueryReceiveBuffer(UnixSocketIOBuffer** aBuffer)
{
  MOZ_ASSERT(aBuffer);

  if (!mPDU) {
    /* There's only one PDU for receiving. We reuse it every time. */
    mPDU = MakeUnique<DaemonSocketPDU>(DaemonSocketPDU::PDU_MAX_PAYLOAD_LENGTH);
  }
  *aBuffer = mPDU.get();

  return NS_OK;
}

void
DaemonSocketIO::ConsumeBuffer()
{
  MOZ_ASSERT(mConsumer);

  mConsumer->Handle(*mPDU);
}

void
DaemonSocketIO::DiscardBuffer()
{
  // Nothing to do.
}

// |SocketIOBase|

SocketBase*
DaemonSocketIO::GetSocketBase()
{
  return mConnection;
}

bool
DaemonSocketIO::IsShutdownOnConsumerThread() const
{
  MOZ_ASSERT(IsConsumerThread());

  return mConnection == nullptr;
}

bool
DaemonSocketIO::IsShutdownOnIOThread() const
{
  return mShuttingDownOnIOThread;
}

void
DaemonSocketIO::ShutdownOnConsumerThread()
{
  MOZ_ASSERT(IsConsumerThread());
  MOZ_ASSERT(!IsShutdownOnConsumerThread());

  mConnection = nullptr;
}

void
DaemonSocketIO::ShutdownOnIOThread()
{
  MOZ_ASSERT(!IsConsumerThread());
  MOZ_ASSERT(!mShuttingDownOnIOThread);

  Close(); // will also remove fd from I/O loop
  mShuttingDownOnIOThread = true;
}

//
// DaemonSocket
//

DaemonSocket::DaemonSocket(
  DaemonSocketIOConsumer* aIOConsumer,
  DaemonSocketConsumer* aConsumer,
  int aIndex)
  : mIO(nullptr)
  , mIOConsumer(aIOConsumer)
  , mConsumer(aConsumer)
  , mIndex(aIndex)
{
  MOZ_ASSERT(mConsumer);

  MOZ_COUNT_CTOR_INHERITED(DaemonSocket, ConnectionOrientedSocket);
}

DaemonSocket::~DaemonSocket()
{
  MOZ_COUNT_DTOR_INHERITED(DaemonSocket, ConnectionOrientedSocket);
}

// |ConnectionOrientedSocket|

nsresult
DaemonSocket::PrepareAccept(UnixSocketConnector* aConnector,
                            MessageLoop* aConsumerLoop,
                            MessageLoop* aIOLoop,
                            ConnectionOrientedSocketIO*& aIO)
{
  MOZ_ASSERT(!mIO);

  SetConnectionStatus(SOCKET_CONNECTING);

  mIO = new DaemonSocketIO(
    aConsumerLoop, aIOLoop, -1, UnixSocketWatcher::SOCKET_IS_CONNECTING,
    aConnector, this, mIOConsumer);
  aIO = mIO;

  return NS_OK;
}

// |DataSocket|

void
DaemonSocket::SendSocketData(UnixSocketIOBuffer* aBuffer)
{
  MOZ_ASSERT(mIO);
  MOZ_ASSERT(mIO->IsConsumerThread());

  mIO->GetIOLoop()->PostTask(
    MakeAndAddRef<SocketIOSendTask<DaemonSocketIO, UnixSocketIOBuffer>>(
      mIO, aBuffer));
}

// |SocketBase|

void
DaemonSocket::Close()
{
  if (!mIO) {
    CHROMIUM_LOG("HAL daemon already disconnected!");
    return;
  }

  MOZ_ASSERT(mIO->IsConsumerThread());

  mIO->ShutdownOnConsumerThread();
  mIO->GetIOLoop()->PostTask(MakeAndAddRef<SocketIOShutdownTask>(mIO));
  mIO = nullptr;

  NotifyDisconnect();
}

void
DaemonSocket::OnConnectSuccess()
{
  mConsumer->OnConnectSuccess(mIndex);
}

void
DaemonSocket::OnConnectError()
{
  mConsumer->OnConnectError(mIndex);
}

void
DaemonSocket::OnDisconnect()
{
  mConsumer->OnDisconnect(mIndex);
}

}
}