OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_EDK_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_ | 5 #ifndef MOJO_EDK_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_ |
6 #define MOJO_EDK_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_ | 6 #define MOJO_EDK_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_ |
7 | 7 |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "mojo/edk/embedder/platform_channel_pair.h" | 9 #include "mojo/edk/embedder/platform_channel_pair.h" |
10 #include "mojo/edk/system/awakable_list.h" | 10 #include "mojo/edk/system/awakable_list.h" |
11 #include "mojo/edk/system/dispatcher.h" | 11 #include "mojo/edk/system/dispatcher.h" |
| 12 #include "mojo/edk/system/message_in_transit_queue.h" |
12 #include "mojo/edk/system/raw_channel.h" | 13 #include "mojo/edk/system/raw_channel.h" |
13 #include "mojo/edk/system/system_impl_export.h" | 14 #include "mojo/edk/system/system_impl_export.h" |
14 #include "mojo/public/cpp/system/macros.h" | 15 #include "mojo/public/cpp/system/macros.h" |
15 | 16 |
16 namespace mojo { | 17 namespace mojo { |
17 namespace edk { | 18 namespace edk { |
18 | 19 |
19 // This is the |Dispatcher| implementation for message pipes (created by the | 20 // This is the |Dispatcher| implementation for message pipes (created by the |
20 // Mojo primitive |MojoCreateMessagePipe()|). This class is thread-safe. | 21 // Mojo primitive |MojoCreateMessagePipe()|). This class is thread-safe. |
21 class MOJO_SYSTEM_IMPL_EXPORT MessagePipeDispatcher final | 22 class MOJO_SYSTEM_IMPL_EXPORT MessagePipeDispatcher final |
22 : public Dispatcher, public RawChannel::Delegate { | 23 : public Dispatcher, public RawChannel::Delegate { |
23 public: | 24 public: |
24 // The default options to use for |MojoCreateMessagePipe()|. (Real uses | 25 // The default options to use for |MojoCreateMessagePipe()|. (Real uses |
25 // should obtain this via |ValidateCreateOptions()| with a null |in_options|; | 26 // should obtain this via |ValidateCreateOptions()| with a null |in_options|; |
26 // this is exposed directly for testing convenience.) | 27 // this is exposed directly for testing convenience.) |
27 static const MojoCreateMessagePipeOptions kDefaultCreateOptions; | 28 static const MojoCreateMessagePipeOptions kDefaultCreateOptions; |
28 | 29 |
29 static scoped_refptr<MessagePipeDispatcher> Create( | 30 static scoped_refptr<MessagePipeDispatcher> Create( |
30 const MojoCreateMessagePipeOptions& /*validated_options*/) { | 31 const MojoCreateMessagePipeOptions& validated_options) { |
31 return make_scoped_refptr(new MessagePipeDispatcher()); | 32 return make_scoped_refptr(new MessagePipeDispatcher( |
| 33 !!(validated_options.flags & |
| 34 MOJO_CREATE_MESSAGE_PIPE_OPTIONS_FLAG_TRANSFERABLE))); |
32 } | 35 } |
33 | 36 |
34 // Validates and/or sets default options for |MojoCreateMessagePipeOptions|. | 37 // Validates and/or sets default options for |MojoCreateMessagePipeOptions|. |
35 // If non-null, |in_options| must point to a struct of at least | 38 // If non-null, |in_options| must point to a struct of at least |
36 // |in_options->struct_size| bytes. |out_options| must point to a (current) | 39 // |in_options->struct_size| bytes. |out_options| must point to a (current) |
37 // |MojoCreateMessagePipeOptions| and will be entirely overwritten on success | 40 // |MojoCreateMessagePipeOptions| and will be entirely overwritten on success |
38 // (it may be partly overwritten on failure). | 41 // (it may be partly overwritten on failure). |
39 static MojoResult ValidateCreateOptions( | 42 static MojoResult ValidateCreateOptions( |
40 const MojoCreateMessagePipeOptions* in_options, | 43 const MojoCreateMessagePipeOptions* in_options, |
41 MojoCreateMessagePipeOptions* out_options); | 44 MojoCreateMessagePipeOptions* out_options); |
42 | 45 |
| 46 // Initializes a transferable message pipe. |
43 // Must be called before any other methods. (This method is not thread-safe.) | 47 // Must be called before any other methods. (This method is not thread-safe.) |
44 void Init( | 48 void Init( |
45 ScopedPlatformHandle message_pipe, | 49 ScopedPlatformHandle message_pipe, |
46 char* serialized_read_buffer, size_t serialized_read_buffer_size, | 50 char* serialized_read_buffer, size_t serialized_read_buffer_size, |
47 char* serialized_write_buffer, size_t serialized_write_buffer_size, | 51 char* serialized_write_buffer, size_t serialized_write_buffer_size, |
48 std::vector<int>* serialized_read_fds, | 52 std::vector<int>* serialized_read_fds, |
49 std::vector<int>* serialized_write_fds); | 53 std::vector<int>* serialized_write_fds); |
50 | 54 |
| 55 // Initializes a nontransferable message pipe. |
| 56 void InitNonTransferable(uint64_t pipe_id); |
| 57 |
51 // |Dispatcher| public methods: | 58 // |Dispatcher| public methods: |
52 Type GetType() const override; | 59 Type GetType() const override; |
53 | 60 |
| 61 // RawChannel::Delegate methods: |
| 62 void OnReadMessage( |
| 63 const MessageInTransit::View& message_view, |
| 64 ScopedPlatformHandleVectorPtr platform_handles) override; |
| 65 void OnError(Error error) override; |
| 66 |
| 67 // Called by broker when a route is established between this |
| 68 // MessagePipeDispatcher and another one. This object will receive messages |
| 69 // sent to its pipe_id. It should tag all outgoing messages by calling |
| 70 // MessageInTransit::set_route_id with pipe_id_. |
| 71 void GotNonTransferableChannel(RawChannel* channel); |
| 72 |
54 // The "opposite" of |SerializeAndClose()|. (Typically this is called by | 73 // The "opposite" of |SerializeAndClose()|. (Typically this is called by |
55 // |Dispatcher::Deserialize()|.) | 74 // |Dispatcher::Deserialize()|.) |
56 static scoped_refptr<MessagePipeDispatcher> Deserialize( | 75 static scoped_refptr<MessagePipeDispatcher> Deserialize( |
57 const void* source, | 76 const void* source, |
58 size_t size, | 77 size_t size, |
59 PlatformHandleVector* platform_handles); | 78 PlatformHandleVector* platform_handles); |
60 | 79 |
61 private: | 80 private: |
62 MessagePipeDispatcher(); | 81 // See MOJO_CREATE_MESSAGE_PIPE_OPTIONS_FLAG_TRANSFERABLE's definition for an |
| 82 // explanation of what is a transferable pipe. |
| 83 explicit MessagePipeDispatcher(bool transferable); |
63 ~MessagePipeDispatcher() override; | 84 ~MessagePipeDispatcher() override; |
64 | 85 |
65 void InitOnIO(); | 86 void InitOnIO(); |
66 void CloseOnIO(); | 87 void CloseOnIO(); |
67 | 88 |
68 // |Dispatcher| protected methods: | 89 // |Dispatcher| protected methods: |
69 void CancelAllAwakablesNoLock() override; | 90 void CancelAllAwakablesNoLock() override; |
70 void CloseImplNoLock() override; | 91 void CloseImplNoLock() override; |
71 scoped_refptr<Dispatcher> CreateEquivalentDispatcherAndCloseImplNoLock() | 92 scoped_refptr<Dispatcher> CreateEquivalentDispatcherAndCloseImplNoLock() |
72 override; | 93 override; |
(...skipping 16 matching lines...) Expand all Loading... |
89 HandleSignalsState* signals_state) override; | 110 HandleSignalsState* signals_state) override; |
90 void StartSerializeImplNoLock(size_t* max_size, | 111 void StartSerializeImplNoLock(size_t* max_size, |
91 size_t* max_platform_handles) override; | 112 size_t* max_platform_handles) override; |
92 bool EndSerializeAndCloseImplNoLock( | 113 bool EndSerializeAndCloseImplNoLock( |
93 void* destination, | 114 void* destination, |
94 size_t* actual_size, | 115 size_t* actual_size, |
95 PlatformHandleVector* platform_handles) override; | 116 PlatformHandleVector* platform_handles) override; |
96 void TransportStarted() override; | 117 void TransportStarted() override; |
97 void TransportEnded() override; | 118 void TransportEnded() override; |
98 | 119 |
99 // |RawChannel::Delegate methods: | |
100 void OnReadMessage( | |
101 const MessageInTransit::View& message_view, | |
102 ScopedPlatformHandleVectorPtr platform_handles) override; | |
103 void OnError(Error error) override; | |
104 | |
105 // Calls ReleaseHandle and serializes the raw channel. This is split into a | 120 // Calls ReleaseHandle and serializes the raw channel. This is split into a |
106 // function because it's called in two different ways: | 121 // function because it's called in two different ways: |
107 // 1) When serializing "live" dispatchers that are passed to MojoWriteMessage, | 122 // 1) When serializing "live" dispatchers that are passed to MojoWriteMessage, |
108 // CreateEquivalentDispatcherAndCloseImplNoLock calls this. | 123 // CreateEquivalentDispatcherAndCloseImplNoLock calls this. |
109 // 2) When serializing dispatchers that are attached to deserialized messages | 124 // 2) When serializing dispatchers that are attached to deserialized messages |
110 // which haven't been consumed by MojoReadMessage, StartSerializeImplNoLock | 125 // which haven't been consumed by MojoReadMessage, StartSerializeImplNoLock |
111 // calls this. | 126 // calls this. |
112 void SerializeInternal(); | 127 void SerializeInternal(); |
113 | 128 |
114 MojoResult AttachTransportsNoLock( | 129 MojoResult AttachTransportsNoLock( |
115 MessageInTransit* message, | 130 MessageInTransit* message, |
116 std::vector<DispatcherTransport>* transports); | 131 std::vector<DispatcherTransport>* transports); |
117 | 132 |
| 133 // Called whenever a read or write is done on a non-transferable pipe, which |
| 134 // "binds" the pipe id to this object. |
| 135 void RequestNontransferableChannel(); |
| 136 |
118 // Protected by |lock()|: | 137 // Protected by |lock()|: |
119 RawChannel* channel_; | 138 RawChannel* channel_; |
120 | 139 |
121 // Queue of incoming messages that we read from RawChannel but haven't been | 140 // Queue of incoming messages that we read from RawChannel but haven't been |
122 // consumed through MojoReadMessage yet. | 141 // consumed through MojoReadMessage yet. |
123 MessageInTransitQueue message_queue_; | 142 MessageInTransitQueue message_queue_; |
| 143 |
| 144 // The following members are only used when transferable_ is false; |
| 145 |
124 // When sending MP, contains serialized message_queue_. | 146 // When sending MP, contains serialized message_queue_. |
125 bool serialized_; | |
126 std::vector<char> serialized_message_queue_; | 147 std::vector<char> serialized_message_queue_; |
127 std::vector<char> serialized_read_buffer_; | 148 std::vector<char> serialized_read_buffer_; |
128 std::vector<char> serialized_write_buffer_; | 149 std::vector<char> serialized_write_buffer_; |
129 // Contains FDs from (in this order): the read buffer, the write buffer, and | 150 // Contains FDs from (in this order): the read buffer, the write buffer, and |
130 // message queue. | 151 // message queue. |
131 std::vector<int> serialized_fds_; | 152 std::vector<int> serialized_fds_; |
132 size_t serialized_read_fds_length_; | 153 size_t serialized_read_fds_length_; |
133 size_t serialized_write_fds_length_; | 154 size_t serialized_write_fds_length_; |
134 size_t serialized_message_fds_length_; | 155 size_t serialized_message_fds_length_; |
135 ScopedPlatformHandle serialized_platform_handle_; | 156 ScopedPlatformHandle serialized_platform_handle_; |
| 157 |
| 158 // The following members are only used when transferable_ is true; |
| 159 |
| 160 // The unique id shared by both ends of a non-transferable message pipe. This |
| 161 // is held on until a read or write are done, and at that point it's used to |
| 162 // get a RawChannel. |
| 163 uint64_t pipe_id_; |
| 164 enum NonTransferableState { |
| 165 WAITING_FOR_READ_OR_WRITE, |
| 166 CONNECT_CALLED, |
| 167 CONNECTED, |
| 168 WAITING_FOR_CONNECT_TO_CLOSE, |
| 169 CLOSED, |
| 170 SERIALISED, |
| 171 }; |
| 172 |
| 173 NonTransferableState non_transferable_state_; |
| 174 // Messages that were written while we were waiting to get a RawChannel. |
| 175 MessageInTransitQueue non_transferable_outgoing_message_queue_; |
| 176 |
| 177 |
| 178 // The following members are used for both modes of transferable_. |
| 179 |
136 AwakableList awakable_list_; | 180 AwakableList awakable_list_; |
137 | 181 |
138 // If DispatcherTransport is created. Must be set before lock() is called to | 182 // If DispatcherTransport is created. Must be set before lock() is called to |
139 // avoid deadlocks with RawChannel calling us. | 183 // avoid deadlocks with RawChannel calling us. |
140 base::Lock started_transport_; | 184 base::Lock started_transport_; |
141 | 185 |
| 186 bool serialized_; |
142 bool calling_init_; | 187 bool calling_init_; |
143 bool write_error_; | 188 bool write_error_; |
| 189 // Whether it can be sent after read or write. |
| 190 bool transferable_; |
144 | 191 |
145 MOJO_DISALLOW_COPY_AND_ASSIGN(MessagePipeDispatcher); | 192 MOJO_DISALLOW_COPY_AND_ASSIGN(MessagePipeDispatcher); |
146 }; | 193 }; |
147 | 194 |
148 } // namespace edk | 195 } // namespace edk |
149 } // namespace mojo | 196 } // namespace mojo |
150 | 197 |
151 #endif // MOJO_EDK_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_ | 198 #endif // MOJO_EDK_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_ |
OLD | NEW |