• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 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_H_
6 #define MOJO_PUBLIC_CPP_SYSTEM_MESSAGE_H_
7 
8 #include <limits>
9 
10 #include "base/macros.h"
11 #include "base/strings/string_piece.h"
12 #include "mojo/public/c/system/message_pipe.h"
13 #include "mojo/public/cpp/system/handle.h"
14 
15 namespace mojo {
16 
17 const MojoMessageHandle kInvalidMessageHandleValue =
18     MOJO_MESSAGE_HANDLE_INVALID;
19 
20 // Handle wrapper base class for a |MojoMessageHandle|.
21 class MessageHandle {
22  public:
MessageHandle()23   MessageHandle() : value_(kInvalidMessageHandleValue) {}
MessageHandle(MojoMessageHandle value)24   explicit MessageHandle(MojoMessageHandle value) : value_(value) {}
~MessageHandle()25   ~MessageHandle() {}
26 
swap(MessageHandle & other)27   void swap(MessageHandle& other) {
28     MojoMessageHandle temp = value_;
29     value_ = other.value_;
30     other.value_ = temp;
31   }
32 
is_valid()33   bool is_valid() const { return value_ != kInvalidMessageHandleValue; }
34 
value()35   const MojoMessageHandle& value() const { return value_; }
mutable_value()36   MojoMessageHandle* mutable_value() { return &value_; }
set_value(MojoMessageHandle value)37   void set_value(MojoMessageHandle value) { value_ = value; }
38 
Close()39   void Close() {
40     DCHECK(is_valid());
41     MojoResult result = MojoFreeMessage(value_);
42     ALLOW_UNUSED_LOCAL(result);
43     DCHECK_EQ(MOJO_RESULT_OK, result);
44   }
45 
46  private:
47   MojoMessageHandle value_;
48 };
49 
50 using ScopedMessageHandle = ScopedHandleBase<MessageHandle>;
51 
AllocMessage(size_t num_bytes,const MojoHandle * handles,size_t num_handles,MojoAllocMessageFlags flags,ScopedMessageHandle * handle)52 inline MojoResult AllocMessage(size_t num_bytes,
53                                const MojoHandle* handles,
54                                size_t num_handles,
55                                MojoAllocMessageFlags flags,
56                                ScopedMessageHandle* handle) {
57   DCHECK_LE(num_bytes, std::numeric_limits<uint32_t>::max());
58   DCHECK_LE(num_handles, std::numeric_limits<uint32_t>::max());
59   MojoMessageHandle raw_handle;
60   MojoResult rv = MojoAllocMessage(static_cast<uint32_t>(num_bytes), handles,
61                                    static_cast<uint32_t>(num_handles), flags,
62                                    &raw_handle);
63   if (rv != MOJO_RESULT_OK)
64     return rv;
65 
66   handle->reset(MessageHandle(raw_handle));
67   return MOJO_RESULT_OK;
68 }
69 
GetMessageBuffer(MessageHandle message,void ** buffer)70 inline MojoResult GetMessageBuffer(MessageHandle message, void** buffer) {
71   DCHECK(message.is_valid());
72   return MojoGetMessageBuffer(message.value(), buffer);
73 }
74 
NotifyBadMessage(MessageHandle message,const base::StringPiece & error)75 inline MojoResult NotifyBadMessage(MessageHandle message,
76                                    const base::StringPiece& error) {
77   DCHECK(message.is_valid());
78   return MojoNotifyBadMessage(message.value(), error.data(), error.size());
79 }
80 
81 }  // namespace mojo
82 
83 #endif  // MOJO_PUBLIC_CPP_SYSTEM_MESSAGE_H_
84