• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef MOJO_PUBLIC_CPP_SYSTEM_MESSAGE_PIPE_H_
6 #define MOJO_PUBLIC_CPP_SYSTEM_MESSAGE_PIPE_H_
7 
8 #include <assert.h>
9 
10 #include "mojo/public/c/system/message_pipe.h"
11 #include "mojo/public/cpp/system/handle.h"
12 #include "mojo/public/cpp/system/macros.h"
13 
14 namespace mojo {
15 
16 // MessagePipeHandle -----------------------------------------------------------
17 
18 class MessagePipeHandle : public Handle {
19  public:
MessagePipeHandle()20   MessagePipeHandle() {}
MessagePipeHandle(MojoHandle value)21   explicit MessagePipeHandle(MojoHandle value) : Handle(value) {}
22 
23   // Copying and assignment allowed.
24 };
25 
26 MOJO_COMPILE_ASSERT(sizeof(MessagePipeHandle) == sizeof(Handle),
27                     bad_size_for_cpp_MessagePipeHandle);
28 
29 typedef ScopedHandleBase<MessagePipeHandle> ScopedMessagePipeHandle;
30 MOJO_COMPILE_ASSERT(sizeof(ScopedMessagePipeHandle) ==
31                         sizeof(MessagePipeHandle),
32                     bad_size_for_cpp_ScopedMessagePipeHandle);
33 
CreateMessagePipe(const MojoCreateMessagePipeOptions * options,ScopedMessagePipeHandle * message_pipe0,ScopedMessagePipeHandle * message_pipe1)34 inline MojoResult CreateMessagePipe(const MojoCreateMessagePipeOptions* options,
35                                     ScopedMessagePipeHandle* message_pipe0,
36                                     ScopedMessagePipeHandle* message_pipe1) {
37   assert(message_pipe0);
38   assert(message_pipe1);
39   MessagePipeHandle handle0;
40   MessagePipeHandle handle1;
41   MojoResult rv = MojoCreateMessagePipe(options,
42                                         handle0.mutable_value(),
43                                         handle1.mutable_value());
44   // Reset even on failure (reduces the chances that a "stale"/incorrect handle
45   // will be used).
46   message_pipe0->reset(handle0);
47   message_pipe1->reset(handle1);
48   return rv;
49 }
50 
51 // These "raw" versions fully expose the underlying API, but don't help with
52 // ownership of handles (especially when writing messages).
53 // TODO(vtl): Write "baked" versions.
WriteMessageRaw(MessagePipeHandle message_pipe,const void * bytes,uint32_t num_bytes,const MojoHandle * handles,uint32_t num_handles,MojoWriteMessageFlags flags)54 inline MojoResult WriteMessageRaw(MessagePipeHandle message_pipe,
55                                   const void* bytes,
56                                   uint32_t num_bytes,
57                                   const MojoHandle* handles,
58                                   uint32_t num_handles,
59                                   MojoWriteMessageFlags flags) {
60   return MojoWriteMessage(message_pipe.value(), bytes, num_bytes, handles,
61                           num_handles, flags);
62 }
63 
ReadMessageRaw(MessagePipeHandle message_pipe,void * bytes,uint32_t * num_bytes,MojoHandle * handles,uint32_t * num_handles,MojoReadMessageFlags flags)64 inline MojoResult ReadMessageRaw(MessagePipeHandle message_pipe,
65                                  void* bytes,
66                                  uint32_t* num_bytes,
67                                  MojoHandle* handles,
68                                  uint32_t* num_handles,
69                                  MojoReadMessageFlags flags) {
70   return MojoReadMessage(message_pipe.value(), bytes, num_bytes, handles,
71                          num_handles, flags);
72 }
73 
74 // A wrapper class that automatically creates a message pipe and owns both
75 // handles.
76 class MessagePipe {
77  public:
78   MessagePipe();
79   explicit MessagePipe(const MojoCreateMessagePipeOptions& options);
80   ~MessagePipe();
81 
82   ScopedMessagePipeHandle handle0;
83   ScopedMessagePipeHandle handle1;
84 };
85 
MessagePipe()86 inline MessagePipe::MessagePipe() {
87   MojoResult result MOJO_ALLOW_UNUSED =
88       CreateMessagePipe(NULL, &handle0, &handle1);
89   assert(result == MOJO_RESULT_OK);
90 }
91 
MessagePipe(const MojoCreateMessagePipeOptions & options)92 inline MessagePipe::MessagePipe(const MojoCreateMessagePipeOptions& options) {
93   MojoResult result MOJO_ALLOW_UNUSED =
94       CreateMessagePipe(&options, &handle0, &handle1);
95   assert(result == MOJO_RESULT_OK);
96 }
97 
~MessagePipe()98 inline MessagePipe::~MessagePipe() {
99 }
100 
101 }  // namespace mojo
102 
103 #endif  // MOJO_PUBLIC_CPP_SYSTEM_MESSAGE_PIPE_H_
104