Ignore title parameter for navigator.registerProtocolHandler
[chromium-blink-merge.git] / mojo / public / cpp / bindings / message.h
blob5653f05eb6022aaeeeb9e9a8bff1257fb5a6854e
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.
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_MESSAGE_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_MESSAGE_H_
8 #include <assert.h>
10 #include <vector>
12 #include "mojo/public/cpp/bindings/lib/message_internal.h"
14 namespace mojo {
16 // Message is a holder for the data and handles to be sent over a MessagePipe.
17 // Message owns its data and handles, but a consumer of Message is free to
18 // mutate the data and handles. The message's data is comprised of a header
19 // followed by payload.
20 class Message {
21 public:
22 Message();
23 ~Message();
25 // These may only be called on a newly created Message object.
26 void AllocUninitializedData(uint32_t num_bytes);
27 void AdoptData(uint32_t num_bytes, internal::MessageData* data);
29 // Swaps data and handles between this Message and another.
30 void Swap(Message* other);
32 uint32_t data_num_bytes() const { return data_num_bytes_; }
34 // Access the raw bytes of the message.
35 const uint8_t* data() const { return
36 reinterpret_cast<const uint8_t*>(data_);
38 uint8_t* mutable_data() { return reinterpret_cast<uint8_t*>(data_); }
40 // Access the header.
41 const internal::MessageHeader* header() const { return &data_->header; }
43 uint32_t name() const { return data_->header.name; }
44 bool has_flag(uint32_t flag) const { return !!(data_->header.flags & flag); }
46 // Access the request_id field (if present).
47 bool has_request_id() const { return data_->header.num_fields >= 3; }
48 uint64_t request_id() const {
49 assert(has_request_id());
50 return static_cast<const internal::MessageHeaderWithRequestID*>(
51 &data_->header)->request_id;
53 void set_request_id(uint64_t request_id) {
54 assert(has_request_id());
55 static_cast<internal::MessageHeaderWithRequestID*>(&data_->header)->
56 request_id = request_id;
59 // Access the payload.
60 const uint8_t* payload() const {
61 return reinterpret_cast<const uint8_t*>(data_) + data_->header.num_bytes;
63 uint8_t* mutable_payload() {
64 return reinterpret_cast<uint8_t*>(data_) + data_->header.num_bytes;
67 // Access the handles.
68 const std::vector<Handle>* handles() const { return &handles_; }
69 std::vector<Handle>* mutable_handles() { return &handles_; }
71 private:
72 uint32_t data_num_bytes_;
73 internal::MessageData* data_; // Heap-allocated using malloc.
74 std::vector<Handle> handles_;
76 MOJO_DISALLOW_COPY_AND_ASSIGN(Message);
79 class MessageReceiver {
80 public:
81 virtual ~MessageReceiver() {}
83 // The receiver may mutate the given message. Returns true if the message
84 // was accepted and false otherwise, indicating that the message was invalid
85 // or malformed.
86 virtual bool Accept(Message* message) MOJO_WARN_UNUSED_RESULT = 0;
88 // A variant on Accept that registers a MessageReceiver (known as the
89 // responder) to handle the response message generated from the given
90 // message. The responder's Accept method may be called during
91 // AcceptWithResponder or some time after its return.
93 // NOTE: Upon returning true, AcceptWithResponder assumes ownership of
94 // |responder| and will delete it after calling |responder->Accept| or upon
95 // its own destruction.
97 virtual bool AcceptWithResponder(
98 Message* message, MessageReceiver* responder) MOJO_WARN_UNUSED_RESULT = 0;
101 // Read a single message from the pipe and dispatch to the given receiver. The
102 // receiver may be null, in which case the message is simply discarded.
103 // Returns MOJO_RESULT_SHOULD_WAIT if the caller should wait on the handle to
104 // become readable. Returns MOJO_RESULT_OK if a message was dispatched and
105 // otherwise returns an error code if something went wrong.
106 MojoResult ReadAndDispatchMessage(MessagePipeHandle handle,
107 MessageReceiver* receiver,
108 bool* receiver_result);
110 } // namespace mojo
112 #endif // MOJO_PUBLIC_CPP_BINDINGS_MESSAGE_H_