summaryrefslogtreecommitdiffstats
path: root/ipc/mojo/ipc_channel_mojo_readers.cc
blob: 2c231bfeaca5127f98495bea9a3c7a62c5cc362f (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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ipc/mojo/ipc_channel_mojo_readers.h"

#include "ipc/mojo/ipc_channel_mojo.h"
#include "mojo/edk/embedder/embedder.h"

#if defined(OS_POSIX) && !defined(OS_NACL)
#include "ipc/file_descriptor_set_posix.h"
#endif

namespace IPC {
namespace internal {

namespace {

// TODO(morrita): This should be built using higher-level Mojo construct
// for clarity and extensibility.
class HelloMessage {
 public:
  static Pickle CreateRequest(int32 pid) {
    Pickle request;
    request.WriteString(kHelloRequestMagic);
    request.WriteInt(pid);
    return request;
  }

  static bool ReadRequest(Pickle& pickle, int32* pid) {
    PickleIterator iter(pickle);
    std::string hello;
    if (!iter.ReadString(&hello)) {
      DLOG(WARNING) << "Failed to Read magic string.";
      return false;
    }

    if (hello != kHelloRequestMagic) {
      DLOG(WARNING) << "Magic mismatch:" << hello;
      return false;
    }

    int read_pid;
    if (!iter.ReadInt(&read_pid)) {
      DLOG(WARNING) << "Failed to Read PID.";
      return false;
    }

    *pid = read_pid;
    return true;
  }

  static Pickle CreateResponse(int32 pid) {
    Pickle request;
    request.WriteString(kHelloResponseMagic);
    request.WriteInt(pid);
    return request;
  }

  static bool ReadResponse(Pickle& pickle, int32* pid) {
    PickleIterator iter(pickle);
    std::string hello;
    if (!iter.ReadString(&hello)) {
      DLOG(WARNING) << "Failed to read magic string.";
      return false;
    }

    if (hello != kHelloResponseMagic) {
      DLOG(WARNING) << "Magic mismatch:" << hello;
      return false;
    }

    int read_pid;
    if (!iter.ReadInt(&read_pid)) {
      DLOG(WARNING) << "Failed to read PID.";
      return false;
    }

    *pid = read_pid;
    return true;
  }

 private:
  static const char* kHelloRequestMagic;
  static const char* kHelloResponseMagic;
};

const char* HelloMessage::kHelloRequestMagic = "MREQ";
const char* HelloMessage::kHelloResponseMagic = "MRES";

}  // namespace

//------------------------------------------------------------------------------

MessageReader::MessageReader(mojo::ScopedMessagePipeHandle pipe,
                             ChannelMojo* owner)
    : internal::MessagePipeReader(pipe.Pass()), owner_(owner) {
}

void MessageReader::OnMessageReceived() {
  Message message(data_buffer().empty() ? "" : &data_buffer()[0],
                  static_cast<uint32>(data_buffer().size()));

  std::vector<MojoHandle> handle_buffer;
  TakeHandleBuffer(&handle_buffer);
#if defined(OS_POSIX) && !defined(OS_NACL)
  MojoResult write_result =
      ChannelMojo::WriteToFileDescriptorSet(handle_buffer, &message);
  if (write_result != MOJO_RESULT_OK) {
    CloseWithError(write_result);
    return;
  }
#else
  DCHECK(handle_buffer.empty());
#endif

  message.TraceMessageEnd();
  owner_->OnMessageReceived(message);
}

void MessageReader::OnPipeClosed() {
  if (!owner_)
    return;
  owner_->OnPipeClosed(this);
  owner_ = NULL;
}

void MessageReader::OnPipeError(MojoResult error) {
  if (!owner_)
    return;
  owner_->OnPipeError(this);
}

bool MessageReader::Send(scoped_ptr<Message> message) {
  DCHECK(IsValid());

  message->TraceMessageBegin();
  std::vector<MojoHandle> handles;
#if defined(OS_POSIX) && !defined(OS_NACL)
  MojoResult read_result =
      ChannelMojo::ReadFromFileDescriptorSet(message.get(), &handles);
  if (read_result != MOJO_RESULT_OK) {
    std::for_each(handles.begin(), handles.end(), &MojoClose);
    CloseWithError(read_result);
    return false;
  }
#endif
  MojoResult write_result =
      MojoWriteMessage(handle(),
                       message->data(),
                       static_cast<uint32>(message->size()),
                       handles.empty() ? NULL : &handles[0],
                       static_cast<uint32>(handles.size()),
                       MOJO_WRITE_MESSAGE_FLAG_NONE);
  if (MOJO_RESULT_OK != write_result) {
    std::for_each(handles.begin(), handles.end(), &MojoClose);
    CloseWithError(write_result);
    return false;
  }

  return true;
}

//------------------------------------------------------------------------------

ControlReader::ControlReader(mojo::ScopedMessagePipeHandle pipe,
                             ChannelMojo* owner)
    : internal::MessagePipeReader(pipe.Pass()), owner_(owner) {
}

void ControlReader::OnPipeClosed() {
  if (!owner_)
    return;
  owner_->OnPipeClosed(this);
  owner_ = NULL;
}

void ControlReader::OnPipeError(MojoResult error) {
  if (!owner_)
    return;
  owner_->OnPipeError(this);
}

bool ControlReader::Connect() {
  return true;
}

//------------------------------------------------------------------------------

ServerControlReader::ServerControlReader(mojo::ScopedMessagePipeHandle pipe,
                                         ChannelMojo* owner)
    : ControlReader(pipe.Pass(), owner) {
}

ServerControlReader::~ServerControlReader() {
}

bool ServerControlReader::Connect() {
  MojoResult result = SendHelloRequest();
  if (result != MOJO_RESULT_OK) {
    CloseWithError(result);
    return false;
  }

  return true;
}

MojoResult ServerControlReader::SendHelloRequest() {
  DCHECK(IsValid());
  DCHECK(!message_pipe_.is_valid());

  mojo::ScopedMessagePipeHandle self;
  mojo::ScopedMessagePipeHandle peer;
  MojoResult create_result =
      mojo::CreateMessagePipe(NULL, &message_pipe_, &peer);
  if (MOJO_RESULT_OK != create_result) {
    DLOG(WARNING) << "mojo::CreateMessagePipe failed: " << create_result;
    return create_result;
  }

  MojoHandle peer_to_send = peer.get().value();
  Pickle request = HelloMessage::CreateRequest(owner_->GetSelfPID());
  MojoResult write_result =
      MojoWriteMessage(handle(),
                       request.data(),
                       static_cast<uint32>(request.size()),
                       &peer_to_send,
                       1,
                       MOJO_WRITE_MESSAGE_FLAG_NONE);
  if (MOJO_RESULT_OK != write_result) {
    DLOG(WARNING) << "Writing Hello request failed: " << create_result;
    return write_result;
  }

  // |peer| is sent and no longer owned by |this|.
  (void)peer.release();
  return MOJO_RESULT_OK;
}

MojoResult ServerControlReader::RespondHelloResponse() {
  Pickle request(data_buffer().empty() ? "" : &data_buffer()[0],
                 static_cast<uint32>(data_buffer().size()));

  int32 read_pid = 0;
  if (!HelloMessage::ReadResponse(request, &read_pid)) {
    DLOG(ERROR) << "Failed to parse Hello response.";
    return MOJO_RESULT_UNKNOWN;
  }

  base::ProcessId pid = static_cast<base::ProcessId>(read_pid);
  owner_->set_peer_pid(pid);
  owner_->OnConnected(message_pipe_.Pass());
  return MOJO_RESULT_OK;
}

void ServerControlReader::OnMessageReceived() {
  MojoResult result = RespondHelloResponse();
  if (result != MOJO_RESULT_OK)
    CloseWithError(result);
}

//------------------------------------------------------------------------------

ClientControlReader::ClientControlReader(mojo::ScopedMessagePipeHandle pipe,
                                         ChannelMojo* owner)
    : ControlReader(pipe.Pass(), owner) {
}

MojoResult ClientControlReader::RespondHelloRequest(
    MojoHandle message_channel) {
  DCHECK(IsValid());

  mojo::ScopedMessagePipeHandle received_pipe(
      (mojo::MessagePipeHandle(message_channel)));

  int32 read_request = 0;
  Pickle request(data_buffer().empty() ? "" : &data_buffer()[0],
                 static_cast<uint32>(data_buffer().size()));
  if (!HelloMessage::ReadRequest(request, &read_request)) {
    DLOG(ERROR) << "Hello request has wrong magic.";
    return MOJO_RESULT_UNKNOWN;
  }

  base::ProcessId pid = read_request;
  Pickle response = HelloMessage::CreateResponse(owner_->GetSelfPID());
  MojoResult write_result =
      MojoWriteMessage(handle(),
                       response.data(),
                       static_cast<uint32>(response.size()),
                       NULL,
                       0,
                       MOJO_WRITE_MESSAGE_FLAG_NONE);
  if (MOJO_RESULT_OK != write_result) {
    DLOG(ERROR) << "Writing Hello response failed: " << write_result;
    return write_result;
  }

  owner_->set_peer_pid(pid);
  owner_->OnConnected(received_pipe.Pass());
  return MOJO_RESULT_OK;
}

void ClientControlReader::OnMessageReceived() {
  std::vector<MojoHandle> handle_buffer;
  TakeHandleBuffer(&handle_buffer);
  if (handle_buffer.size() != 1) {
    DLOG(ERROR) << "Hello request doesn't contains required handle: "
                << handle_buffer.size();
    CloseWithError(MOJO_RESULT_UNKNOWN);
    return;
  }

  MojoResult result = RespondHelloRequest(handle_buffer[0]);
  if (result != MOJO_RESULT_OK) {
    DLOG(ERROR) << "Failed to respond Hello request. Closing: " << result;
    CloseWithError(result);
  }
}

}  // namespace internal
}  // namespace IPC