| 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 <string> | 13 #include <string> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/callback.h" |
| 16 #include "base/logging.h" | 17 #include "base/logging.h" |
| 17 #include "mojo/public/cpp/bindings/lib/message_buffer.h" | 18 #include "mojo/public/cpp/bindings/lib/message_buffer.h" |
| 18 #include "mojo/public/cpp/bindings/lib/message_internal.h" | 19 #include "mojo/public/cpp/bindings/lib/message_internal.h" |
| 19 #include "mojo/public/cpp/system/message.h" | 20 #include "mojo/public/cpp/system/message.h" |
| 20 | 21 |
| 21 namespace mojo { | 22 namespace mojo { |
| 22 | 23 |
| 24 using ReportBadMessageCallback = base::Callback<void(const std::string& error)>; |
| 25 |
| 23 // Message is a holder for the data and handles to be sent over a MessagePipe. | 26 // Message is a holder for the data and handles to be sent over a MessagePipe. |
| 24 // Message owns its data and handles, but a consumer of Message is free to | 27 // Message owns its data and handles, but a consumer of Message is free to |
| 25 // mutate the data and handles. The message's data is comprised of a header | 28 // mutate the data and handles. The message's data is comprised of a header |
| 26 // followed by payload. | 29 // followed by payload. |
| 27 class Message { | 30 class Message { |
| 28 public: | 31 public: |
| 29 static const uint32_t kFlagExpectsResponse = 1 << 0; | 32 static const uint32_t kFlagExpectsResponse = 1 << 0; |
| 30 static const uint32_t kFlagIsResponse = 1 << 1; | 33 static const uint32_t kFlagIsResponse = 1 << 1; |
| 31 static const uint32_t kFlagIsSync = 1 << 2; | 34 static const uint32_t kFlagIsSync = 1 << 2; |
| 32 | 35 |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 // |responder| and will delete it after calling |responder->Accept| or upon | 182 // |responder| and will delete it after calling |responder->Accept| or upon |
| 180 // its own destruction. | 183 // its own destruction. |
| 181 // | 184 // |
| 182 // TODO(yzshen): consider changing |responder| to | 185 // TODO(yzshen): consider changing |responder| to |
| 183 // std::unique_ptr<MessageReceiver>. | 186 // std::unique_ptr<MessageReceiver>. |
| 184 virtual bool AcceptWithResponder(Message* message, | 187 virtual bool AcceptWithResponder(Message* message, |
| 185 MessageReceiverWithStatus* responder) | 188 MessageReceiverWithStatus* responder) |
| 186 WARN_UNUSED_RESULT = 0; | 189 WARN_UNUSED_RESULT = 0; |
| 187 }; | 190 }; |
| 188 | 191 |
| 192 namespace internal { |
| 193 class SyncMessageResponseSetup; |
| 194 } |
| 195 |
| 196 // An object which should be constructed on the stack immediately before making |
| 197 // a sync request for which the caller wishes to perform custom validation of |
| 198 // the response value(s). It is illegal to make more than one sync call during |
| 199 // the lifetime of the topmost SyncMessageResponseContext, but it is legal to |
| 200 // nest contexts to support reentrancy. |
| 201 // |
| 202 // Usage should look something like: |
| 203 // |
| 204 // SyncMessageResponseContext response_context; |
| 205 // foo_interface->SomeSyncCall(&response_value); |
| 206 // if (response_value.IsBad()) |
| 207 // response_context.ReportBadMessage("Bad response_value!"); |
| 208 // |
| 209 class SyncMessageResponseContext { |
| 210 public: |
| 211 SyncMessageResponseContext(); |
| 212 ~SyncMessageResponseContext(); |
| 213 |
| 214 static SyncMessageResponseContext* current(); |
| 215 |
| 216 void ReportBadMessage(const std::string& error); |
| 217 |
| 218 const ReportBadMessageCallback& GetBadMessageCallback(); |
| 219 |
| 220 private: |
| 221 friend class internal::SyncMessageResponseSetup; |
| 222 |
| 223 SyncMessageResponseContext* outer_context_; |
| 224 Message response_; |
| 225 ReportBadMessageCallback bad_message_callback_; |
| 226 |
| 227 DISALLOW_COPY_AND_ASSIGN(SyncMessageResponseContext); |
| 228 }; |
| 229 |
| 189 // Read a single message from the pipe. The caller should have created the | 230 // Read a single message from the pipe. The caller should have created the |
| 190 // Message, but not called Initialize(). Returns MOJO_RESULT_SHOULD_WAIT if | 231 // Message, but not called Initialize(). Returns MOJO_RESULT_SHOULD_WAIT if |
| 191 // the caller should wait on the handle to become readable. Returns | 232 // the caller should wait on the handle to become readable. Returns |
| 192 // MOJO_RESULT_OK if the message was read successfully and should be | 233 // MOJO_RESULT_OK if the message was read successfully and should be |
| 193 // dispatched, otherwise returns an error code if something went wrong. | 234 // dispatched, otherwise returns an error code if something went wrong. |
| 194 // | 235 // |
| 195 // NOTE: The message hasn't been validated and may be malformed! | 236 // NOTE: The message hasn't been validated and may be malformed! |
| 196 MojoResult ReadMessage(MessagePipeHandle handle, Message* message); | 237 MojoResult ReadMessage(MessagePipeHandle handle, Message* message); |
| 197 | 238 |
| 239 // Reports the currently dispatching Message as bad. Note that this is only |
| 240 // legal to call from directly within the stack frame of a message dispatch. If |
| 241 // you need to do asynchronous work before you can determine the legitimacy of |
| 242 // a message, use TakeBadMessageCallback() and retain its result until you're |
| 243 // ready to invoke or discard it. |
| 244 void ReportBadMessage(const std::string& error); |
| 245 |
| 246 // Acquires a callback which may be run to report the currently dispatching |
| 247 // Message as bad. Note that this is only legal to call from directly within the |
| 248 // stack frame of a message dispatch, but the returned callback may be called |
| 249 // exactly once any time thereafter to report the message as bad. This may only |
| 250 // be called once per message. |
| 251 ReportBadMessageCallback GetBadMessageCallback(); |
| 252 |
| 198 } // namespace mojo | 253 } // namespace mojo |
| 199 | 254 |
| 200 #endif // MOJO_PUBLIC_CPP_BINDINGS_MESSAGE_H_ | 255 #endif // MOJO_PUBLIC_CPP_BINDINGS_MESSAGE_H_ |
| OLD | NEW |