OLD | NEW |
1 // Copyright 2013 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" |
| 10 #include "mojo/edk/system/awakable_list.h" |
9 #include "mojo/edk/system/dispatcher.h" | 11 #include "mojo/edk/system/dispatcher.h" |
10 #include "mojo/edk/system/memory.h" | 12 #include "mojo/edk/system/memory.h" |
| 13 #include "mojo/edk/system/raw_channel.h" |
11 #include "mojo/edk/system/system_impl_export.h" | 14 #include "mojo/edk/system/system_impl_export.h" |
12 #include "mojo/public/cpp/system/macros.h" | 15 #include "mojo/public/cpp/system/macros.h" |
13 | 16 |
14 namespace mojo { | 17 namespace mojo { |
15 namespace system { | 18 namespace system { |
16 | 19 |
17 class ChannelEndpoint; | |
18 class MessagePipe; | |
19 class MessagePipeDispatcherTransport; | |
20 | |
21 // This is the |Dispatcher| implementation for message pipes (created by the | 20 // This is the |Dispatcher| implementation for message pipes (created by the |
22 // Mojo primitive |MojoCreateMessagePipe()|). This class is thread-safe. | 21 // Mojo primitive |MojoCreateMessagePipe()|). This class is thread-safe. |
23 class MOJO_SYSTEM_IMPL_EXPORT MessagePipeDispatcher final : public Dispatcher { | 22 class MOJO_SYSTEM_IMPL_EXPORT MessagePipeDispatcher final |
| 23 : public Dispatcher, public RawChannel::Delegate { |
24 public: | 24 public: |
25 // The default options to use for |MojoCreateMessagePipe()|. (Real uses | 25 // The default options to use for |MojoCreateMessagePipe()|. (Real uses |
26 // should obtain this via |ValidateCreateOptions()| with a null |in_options|; | 26 // should obtain this via |ValidateCreateOptions()| with a null |in_options|; |
27 // this is exposed directly for testing convenience.) | 27 // this is exposed directly for testing convenience.) |
28 static const MojoCreateMessagePipeOptions kDefaultCreateOptions; | 28 static const MojoCreateMessagePipeOptions kDefaultCreateOptions; |
29 | 29 |
30 static scoped_refptr<MessagePipeDispatcher> Create( | 30 static scoped_refptr<MessagePipeDispatcher> Create( |
31 const MojoCreateMessagePipeOptions& /*validated_options*/) { | 31 const MojoCreateMessagePipeOptions& /*validated_options*/) { |
32 return make_scoped_refptr(new MessagePipeDispatcher()); | 32 return make_scoped_refptr(new MessagePipeDispatcher()); |
33 } | 33 } |
34 | 34 |
35 // Validates and/or sets default options for |MojoCreateMessagePipeOptions|. | 35 // Validates and/or sets default options for |MojoCreateMessagePipeOptions|. |
36 // If non-null, |in_options| must point to a struct of at least | 36 // If non-null, |in_options| must point to a struct of at least |
37 // |in_options->struct_size| bytes. |out_options| must point to a (current) | 37 // |in_options->struct_size| bytes. |out_options| must point to a (current) |
38 // |MojoCreateMessagePipeOptions| and will be entirely overwritten on success | 38 // |MojoCreateMessagePipeOptions| and will be entirely overwritten on success |
39 // (it may be partly overwritten on failure). | 39 // (it may be partly overwritten on failure). |
40 static MojoResult ValidateCreateOptions( | 40 static MojoResult ValidateCreateOptions( |
41 UserPointer<const MojoCreateMessagePipeOptions> in_options, | 41 UserPointer<const MojoCreateMessagePipeOptions> in_options, |
42 MojoCreateMessagePipeOptions* out_options); | 42 MojoCreateMessagePipeOptions* out_options); |
43 | 43 |
44 // Must be called before any other methods. (This method is not thread-safe.) | 44 // Must be called before any other methods. (This method is not thread-safe.) |
45 void Init(scoped_refptr<MessagePipe> message_pipe, | 45 void Init(embedder::ScopedPlatformHandle message_pipe); |
46 unsigned port) MOJO_NOT_THREAD_SAFE; | |
47 | 46 |
48 // |Dispatcher| public methods: | 47 // |Dispatcher| public methods: |
49 Type GetType() const override; | 48 Type GetType() const override; |
50 | 49 |
51 // Creates a |MessagePipe| with a local endpoint (at port 0) and a proxy | |
52 // endpoint, and creates/initializes a |MessagePipeDispatcher| (attached to | |
53 // the message pipe, port 0). | |
54 // TODO(vtl): This currently uses |kDefaultCreateOptions|, which is okay since | |
55 // there aren't any options, but eventually options should be plumbed through. | |
56 static scoped_refptr<MessagePipeDispatcher> CreateRemoteMessagePipe( | |
57 scoped_refptr<ChannelEndpoint>* channel_endpoint); | |
58 | |
59 // The "opposite" of |SerializeAndClose()|. (Typically this is called by | 50 // The "opposite" of |SerializeAndClose()|. (Typically this is called by |
60 // |Dispatcher::Deserialize()|.) | 51 // |Dispatcher::Deserialize()|.) |
61 static scoped_refptr<MessagePipeDispatcher> Deserialize(Channel* channel, | 52 static scoped_refptr<MessagePipeDispatcher> Deserialize( |
62 const void* source, | 53 const void* source, |
63 size_t size); | 54 size_t size, |
| 55 embedder::PlatformHandleVector* platform_handles); |
64 | 56 |
65 private: | 57 private: |
66 friend class MessagePipeDispatcherTransport; | |
67 | |
68 MessagePipeDispatcher(); | 58 MessagePipeDispatcher(); |
69 ~MessagePipeDispatcher() override; | 59 ~MessagePipeDispatcher() override; |
70 | 60 |
71 // Gets a dumb pointer to |message_pipe_|. This must be called under the | 61 void InitWithReadBuffer(embedder::ScopedPlatformHandle message_pipe, |
72 // |Dispatcher| lock (that it's a dumb pointer is okay since it's under lock). | 62 char* data, |
73 // This is needed when sending handles across processes, where nontrivial, | 63 size_t size); |
74 // invasive work needs to be done. | 64 |
75 MessagePipe* GetMessagePipeNoLock() const; | 65 void InitOnIO(); |
76 // Similarly for the port. | 66 void CloseOnIO(); |
77 unsigned GetPortNoLock() const; | |
78 | 67 |
79 // |Dispatcher| protected methods: | 68 // |Dispatcher| protected methods: |
80 void CancelAllAwakablesNoLock() override; | 69 void CancelAllAwakablesNoLock() override; |
81 void CloseImplNoLock() override; | 70 void CloseImplNoLock() override; |
82 scoped_refptr<Dispatcher> CreateEquivalentDispatcherAndCloseImplNoLock() | 71 scoped_refptr<Dispatcher> CreateEquivalentDispatcherAndCloseImplNoLock() |
83 override; | 72 override; |
84 MojoResult WriteMessageImplNoLock( | 73 MojoResult WriteMessageImplNoLock( |
85 UserPointer<const void> bytes, | 74 UserPointer<const void> bytes, |
86 uint32_t num_bytes, | 75 uint32_t num_bytes, |
87 std::vector<DispatcherTransport>* transports, | 76 std::vector<DispatcherTransport>* transports, |
88 MojoWriteMessageFlags flags) override; | 77 MojoWriteMessageFlags flags) override; |
89 MojoResult ReadMessageImplNoLock(UserPointer<void> bytes, | 78 MojoResult ReadMessageImplNoLock(UserPointer<void> bytes, |
90 UserPointer<uint32_t> num_bytes, | 79 UserPointer<uint32_t> num_bytes, |
91 DispatcherVector* dispatchers, | 80 DispatcherVector* dispatchers, |
92 uint32_t* num_dispatchers, | 81 uint32_t* num_dispatchers, |
93 MojoReadMessageFlags flags) override; | 82 MojoReadMessageFlags flags) override; |
94 HandleSignalsState GetHandleSignalsStateImplNoLock() const override; | 83 HandleSignalsState GetHandleSignalsStateImplNoLock() const override; |
95 MojoResult AddAwakableImplNoLock(Awakable* awakable, | 84 MojoResult AddAwakableImplNoLock(Awakable* awakable, |
96 MojoHandleSignals signals, | 85 MojoHandleSignals signals, |
97 uint32_t context, | 86 uint32_t context, |
98 HandleSignalsState* signals_state) override; | 87 HandleSignalsState* signals_state) override; |
99 void RemoveAwakableImplNoLock(Awakable* awakable, | 88 void RemoveAwakableImplNoLock(Awakable* awakable, |
100 HandleSignalsState* signals_state) override; | 89 HandleSignalsState* signals_state) override; |
101 void StartSerializeImplNoLock(Channel* channel, | 90 void StartSerializeImplNoLock(size_t* max_size, |
102 size_t* max_size, | 91 size_t* max_platform_handles) override; |
103 size_t* max_platform_handles) override | |
104 MOJO_NOT_THREAD_SAFE; | |
105 bool EndSerializeAndCloseImplNoLock( | 92 bool EndSerializeAndCloseImplNoLock( |
106 Channel* channel, | |
107 void* destination, | 93 void* destination, |
108 size_t* actual_size, | 94 size_t* actual_size, |
109 embedder::PlatformHandleVector* platform_handles) override | 95 embedder::PlatformHandleVector* platform_handles) override; |
110 MOJO_NOT_THREAD_SAFE; | 96 void TransportStarted() override; |
| 97 void TransportEnded() override; |
111 | 98 |
112 // This will be null if closed. | 99 // |RawChannel::Delegate methods: |
113 scoped_refptr<MessagePipe> message_pipe_ MOJO_GUARDED_BY(mutex()); | 100 void OnReadMessage( |
114 unsigned port_ MOJO_GUARDED_BY(mutex()); | 101 const MessageInTransit::View& message_view, |
| 102 embedder::ScopedPlatformHandleVectorPtr platform_handles) override; |
| 103 void OnError(Error error) override; |
| 104 |
| 105 // Calls ReleaseHandle and serializes the raw channel. This is split into a |
| 106 // function because it's called in two different ways: |
| 107 // 1) When serializing "live" dispatchers that are passed to MojoWriteMessage, |
| 108 // CreateEquivalentDispatcherAndCloseImplNoLock calls this. |
| 109 // 2) When serializing dispatchers that are attached to deserialized messages |
| 110 // which haven't been consumed by MojoReadMessage, StartSerializeImplNoLock |
| 111 // calls this. |
| 112 void SerializeInternal(); |
| 113 |
| 114 // Protected by |lock()|: |
| 115 RawChannel* channel_; |
| 116 |
| 117 // Queue of incoming messages that we read from RawChannel but haven't been |
| 118 // consumed through MojoReadMessage yet. |
| 119 MessageInTransitQueue message_queue_; |
| 120 // When sending MP, contains serialized message_queue_. |
| 121 bool serialized_; |
| 122 std::vector<char> serialized_message_queue_; |
| 123 std::vector<char> serialized_read_buffer_; |
| 124 embedder::PlatformHandle serialized_platform_handle_; |
| 125 // TODO(jam): stop using this and use shared memory instead since we are |
| 126 // limited to 10K here. |
| 127 AwakableList awakable_list_; |
| 128 |
| 129 // if DispatcherTransport crated. must be set before lock() is called to avoid |
| 130 // deadlocks with RawChannel calling us. |
| 131 base::Lock started_transport_; |
| 132 |
| 133 bool calling_init_; |
| 134 bool error_; |
115 | 135 |
116 MOJO_DISALLOW_COPY_AND_ASSIGN(MessagePipeDispatcher); | 136 MOJO_DISALLOW_COPY_AND_ASSIGN(MessagePipeDispatcher); |
117 }; | 137 }; |
118 | 138 |
119 class MessagePipeDispatcherTransport : public DispatcherTransport { | |
120 public: | |
121 explicit MessagePipeDispatcherTransport(DispatcherTransport transport); | |
122 | |
123 MessagePipe* GetMessagePipe() { | |
124 return message_pipe_dispatcher()->GetMessagePipeNoLock(); | |
125 } | |
126 unsigned GetPort() { return message_pipe_dispatcher()->GetPortNoLock(); } | |
127 | |
128 private: | |
129 MessagePipeDispatcher* message_pipe_dispatcher() { | |
130 return static_cast<MessagePipeDispatcher*>(dispatcher()); | |
131 } | |
132 | |
133 // Copy and assign allowed. | |
134 }; | |
135 | |
136 } // namespace system | 139 } // namespace system |
137 } // namespace mojo | 140 } // namespace mojo |
138 | 141 |
139 #endif // MOJO_EDK_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_ | 142 #endif // MOJO_EDK_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_ |
OLD | NEW |