Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / mojo / public / bindings / lib / sync_dispatcher.cc
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "mojo/public/bindings/sync_dispatcher.h"
6
7 #include <stdlib.h>
8
9 #include "mojo/public/bindings/message.h"
10
11 namespace mojo {
12
13 bool WaitForMessageAndDispatch(MessagePipeHandle handle,
14                                mojo::MessageReceiver* receiver) {
15   uint32_t num_bytes = 0, num_handles = 0;
16   while (true) {
17     MojoResult rv = ReadMessageRaw(handle,
18                                    NULL,
19                                    &num_bytes,
20                                    NULL,
21                                    &num_handles,
22                                    MOJO_READ_MESSAGE_FLAG_NONE);
23     if (rv == MOJO_RESULT_RESOURCE_EXHAUSTED)
24       break;
25     if (rv != MOJO_RESULT_SHOULD_WAIT)
26       return false;
27     rv = Wait(handle, MOJO_WAIT_FLAG_READABLE, MOJO_DEADLINE_INDEFINITE);
28     if (rv != MOJO_RESULT_OK)
29       return false;
30   }
31
32   Message message;
33   message.data = static_cast<MessageData*>(malloc(num_bytes));
34   message.handles.resize(num_handles);
35
36   MojoResult rv =
37       ReadMessageRaw(handle,
38                      message.data,
39                      &num_bytes,
40                      message.handles.empty()
41                          ? NULL
42                          : reinterpret_cast<MojoHandle*>(&message.handles[0]),
43                      &num_handles,
44                      MOJO_READ_MESSAGE_FLAG_NONE);
45   if (rv != MOJO_RESULT_OK)
46     return false;
47   return receiver->Accept(&message);
48 }
49
50 }  // namespace mojo