| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_MESSAGE_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_MESSAGE_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_MESSAGE_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_MESSAGE_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <limits> | 11 #include <limits> |
| 12 #include <memory> | 12 #include <memory> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "mojo/public/cpp/bindings/lib/message_buffer.h" |
| 16 #include "mojo/public/cpp/bindings/lib/message_internal.h" | 17 #include "mojo/public/cpp/bindings/lib/message_internal.h" |
| 17 #include "mojo/public/cpp/bindings/lib/pickle_buffer.h" | 18 #include "mojo/public/cpp/system/message.h" |
| 18 | 19 |
| 19 namespace mojo { | 20 namespace mojo { |
| 20 | 21 |
| 21 // Message is a holder for the data and handles to be sent over a MessagePipe. | 22 // Message is a holder for the data and handles to be sent over a MessagePipe. |
| 22 // Message owns its data and handles, but a consumer of Message is free to | 23 // Message owns its data and handles, but a consumer of Message is free to |
| 23 // mutate the data and handles. The message's data is comprised of a header | 24 // mutate the data and handles. The message's data is comprised of a header |
| 24 // followed by payload. | 25 // followed by payload. |
| 25 class Message { | 26 class Message { |
| 26 public: | 27 public: |
| 27 Message(); | 28 Message(); |
| 28 ~Message(); | 29 ~Message(); |
| 29 | 30 |
| 31 // Initializes a Message with enough space for |capacity| bytes. |
| 30 void Initialize(size_t capacity, bool zero_initialized); | 32 void Initialize(size_t capacity, bool zero_initialized); |
| 31 | 33 |
| 34 // Initializes a Message from an existing Mojo MessageHandle. |
| 35 void InitializeFromMojoMessage(ScopedMessageHandle message, |
| 36 uint32_t num_bytes, |
| 37 std::vector<Handle>* handles); |
| 38 |
| 32 // Transfers data and handles to |destination|. | 39 // Transfers data and handles to |destination|. |
| 33 void MoveTo(Message* destination); | 40 void MoveTo(Message* destination); |
| 34 | 41 |
| 35 uint32_t data_num_bytes() const { | 42 uint32_t data_num_bytes() const { return buffer_->data_num_bytes(); } |
| 36 DCHECK(buffer_->data_num_bytes() <= std::numeric_limits<uint32_t>::max()); | |
| 37 return static_cast<uint32_t>(buffer_->data_num_bytes()); | |
| 38 } | |
| 39 | 43 |
| 40 // Access the raw bytes of the message. | 44 // Access the raw bytes of the message. |
| 41 const uint8_t* data() const { | 45 const uint8_t* data() const { |
| 42 return static_cast<const uint8_t*>(buffer_->data()); | 46 return static_cast<const uint8_t*>(buffer_->data()); |
| 43 } | 47 } |
| 44 | 48 |
| 45 uint8_t* mutable_data() { return static_cast<uint8_t*>(buffer_->data()); } | 49 uint8_t* mutable_data() { return static_cast<uint8_t*>(buffer_->data()); } |
| 46 | 50 |
| 47 // Access the header. | 51 // Access the header. |
| 48 const internal::MessageHeader* header() const { | 52 const internal::MessageHeader* header() const { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 return static_cast<uint32_t>(num_bytes); | 87 return static_cast<uint32_t>(num_bytes); |
| 84 } | 88 } |
| 85 | 89 |
| 86 // Access the handles. | 90 // Access the handles. |
| 87 const std::vector<Handle>* handles() const { return &handles_; } | 91 const std::vector<Handle>* handles() const { return &handles_; } |
| 88 std::vector<Handle>* mutable_handles() { return &handles_; } | 92 std::vector<Handle>* mutable_handles() { return &handles_; } |
| 89 | 93 |
| 90 // Access the underlying Buffer interface. | 94 // Access the underlying Buffer interface. |
| 91 internal::Buffer* buffer() { return buffer_.get(); } | 95 internal::Buffer* buffer() { return buffer_.get(); } |
| 92 | 96 |
| 97 // Takes a scoped MessageHandle which may be passed to |WriteMessageNew()| for |
| 98 // transmission. Note that this invalidates this Message object, taking |
| 99 // ownership of its internal storage and any attached handles. |
| 100 ScopedMessageHandle TakeMojoMessage(); |
| 101 |
| 93 private: | 102 private: |
| 94 void CloseHandles(); | 103 void CloseHandles(); |
| 95 | 104 |
| 96 std::unique_ptr<internal::PickleBuffer> buffer_; | 105 std::unique_ptr<internal::MessageBuffer> buffer_; |
| 97 std::vector<Handle> handles_; | 106 std::vector<Handle> handles_; |
| 98 | 107 |
| 99 DISALLOW_COPY_AND_ASSIGN(Message); | 108 DISALLOW_COPY_AND_ASSIGN(Message); |
| 100 }; | 109 }; |
| 101 | 110 |
| 102 class MessageReceiver { | 111 class MessageReceiver { |
| 103 public: | 112 public: |
| 104 virtual ~MessageReceiver() {} | 113 virtual ~MessageReceiver() {} |
| 105 | 114 |
| 106 // The receiver may mutate the given message. Returns true if the message | 115 // The receiver may mutate the given message. Returns true if the message |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 // the caller should wait on the handle to become readable. Returns | 182 // the caller should wait on the handle to become readable. Returns |
| 174 // MOJO_RESULT_OK if the message was read successfully and should be | 183 // MOJO_RESULT_OK if the message was read successfully and should be |
| 175 // dispatched, otherwise returns an error code if something went wrong. | 184 // dispatched, otherwise returns an error code if something went wrong. |
| 176 // | 185 // |
| 177 // NOTE: The message hasn't been validated and may be malformed! | 186 // NOTE: The message hasn't been validated and may be malformed! |
| 178 MojoResult ReadMessage(MessagePipeHandle handle, Message* message); | 187 MojoResult ReadMessage(MessagePipeHandle handle, Message* message); |
| 179 | 188 |
| 180 } // namespace mojo | 189 } // namespace mojo |
| 181 | 190 |
| 182 #endif // MOJO_PUBLIC_CPP_BINDINGS_MESSAGE_H_ | 191 #endif // MOJO_PUBLIC_CPP_BINDINGS_MESSAGE_H_ |
| OLD | NEW |