| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "mojo/edk/system/dispatcher.h" | 8 #include "mojo/edk/system/dispatcher.h" |
| 9 #include "mojo/edk/system/memory.h" | 9 #include "mojo/edk/system/memory.h" |
| 10 #include "mojo/edk/system/ref_ptr.h" | 10 #include "mojo/edk/util/ref_ptr.h" |
| 11 #include "mojo/public/cpp/system/macros.h" | 11 #include "mojo/public/cpp/system/macros.h" |
| 12 | 12 |
| 13 namespace mojo { | 13 namespace mojo { |
| 14 namespace system { | 14 namespace system { |
| 15 | 15 |
| 16 class ChannelEndpoint; | 16 class ChannelEndpoint; |
| 17 class MessagePipe; | 17 class MessagePipe; |
| 18 class MessagePipeDispatcherTransport; | 18 class MessagePipeDispatcherTransport; |
| 19 | 19 |
| 20 // This is the |Dispatcher| implementation for message pipes (created by the | 20 // This is the |Dispatcher| implementation for message pipes (created by the |
| 21 // Mojo primitive |MojoCreateMessagePipe()|). This class is thread-safe. | 21 // Mojo primitive |MojoCreateMessagePipe()|). This class is thread-safe. |
| 22 class MessagePipeDispatcher final : public Dispatcher { | 22 class MessagePipeDispatcher final : public Dispatcher { |
| 23 public: | 23 public: |
| 24 // The default options to use for |MojoCreateMessagePipe()|. (Real uses | 24 // The default options to use for |MojoCreateMessagePipe()|. (Real uses |
| 25 // should obtain this via |ValidateCreateOptions()| with a null |in_options|; | 25 // should obtain this via |ValidateCreateOptions()| with a null |in_options|; |
| 26 // this is exposed directly for testing convenience.) | 26 // this is exposed directly for testing convenience.) |
| 27 static const MojoCreateMessagePipeOptions kDefaultCreateOptions; | 27 static const MojoCreateMessagePipeOptions kDefaultCreateOptions; |
| 28 | 28 |
| 29 static RefPtr<MessagePipeDispatcher> Create( | 29 static util::RefPtr<MessagePipeDispatcher> Create( |
| 30 const MojoCreateMessagePipeOptions& /*validated_options*/) { | 30 const MojoCreateMessagePipeOptions& /*validated_options*/) { |
| 31 return AdoptRef(new MessagePipeDispatcher()); | 31 return AdoptRef(new MessagePipeDispatcher()); |
| 32 } | 32 } |
| 33 | 33 |
| 34 // Validates and/or sets default options for |MojoCreateMessagePipeOptions|. | 34 // Validates and/or sets default options for |MojoCreateMessagePipeOptions|. |
| 35 // If non-null, |in_options| must point to a struct of at least | 35 // 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) | 36 // |in_options->struct_size| bytes. |out_options| must point to a (current) |
| 37 // |MojoCreateMessagePipeOptions| and will be entirely overwritten on success | 37 // |MojoCreateMessagePipeOptions| and will be entirely overwritten on success |
| 38 // (it may be partly overwritten on failure). | 38 // (it may be partly overwritten on failure). |
| 39 static MojoResult ValidateCreateOptions( | 39 static MojoResult ValidateCreateOptions( |
| 40 UserPointer<const MojoCreateMessagePipeOptions> in_options, | 40 UserPointer<const MojoCreateMessagePipeOptions> in_options, |
| 41 MojoCreateMessagePipeOptions* out_options); | 41 MojoCreateMessagePipeOptions* out_options); |
| 42 | 42 |
| 43 // Must be called before any other methods. (This method is not thread-safe.) | 43 // Must be called before any other methods. (This method is not thread-safe.) |
| 44 void Init(RefPtr<MessagePipe>&& message_pipe, | 44 void Init(util::RefPtr<MessagePipe>&& message_pipe, |
| 45 unsigned port) MOJO_NOT_THREAD_SAFE; | 45 unsigned port) MOJO_NOT_THREAD_SAFE; |
| 46 | 46 |
| 47 // |Dispatcher| public methods: | 47 // |Dispatcher| public methods: |
| 48 Type GetType() const override; | 48 Type GetType() const override; |
| 49 | 49 |
| 50 // Creates a |MessagePipe| with a local endpoint (at port 0) and a proxy | 50 // Creates a |MessagePipe| with a local endpoint (at port 0) and a proxy |
| 51 // endpoint, and creates/initializes a |MessagePipeDispatcher| (attached to | 51 // endpoint, and creates/initializes a |MessagePipeDispatcher| (attached to |
| 52 // the message pipe, port 0). | 52 // the message pipe, port 0). |
| 53 // TODO(vtl): This currently uses |kDefaultCreateOptions|, which is okay since | 53 // TODO(vtl): This currently uses |kDefaultCreateOptions|, which is okay since |
| 54 // there aren't any options, but eventually options should be plumbed through. | 54 // there aren't any options, but eventually options should be plumbed through. |
| 55 static RefPtr<MessagePipeDispatcher> CreateRemoteMessagePipe( | 55 static util::RefPtr<MessagePipeDispatcher> CreateRemoteMessagePipe( |
| 56 RefPtr<ChannelEndpoint>* channel_endpoint); | 56 util::RefPtr<ChannelEndpoint>* channel_endpoint); |
| 57 | 57 |
| 58 // The "opposite" of |SerializeAndClose()|. (Typically this is called by | 58 // The "opposite" of |SerializeAndClose()|. (Typically this is called by |
| 59 // |Dispatcher::Deserialize()|.) | 59 // |Dispatcher::Deserialize()|.) |
| 60 static RefPtr<MessagePipeDispatcher> Deserialize(Channel* channel, | 60 static util::RefPtr<MessagePipeDispatcher> Deserialize(Channel* channel, |
| 61 const void* source, | 61 const void* source, |
| 62 size_t size); | 62 size_t size); |
| 63 | 63 |
| 64 private: | 64 private: |
| 65 friend class MessagePipeDispatcherTransport; | 65 friend class MessagePipeDispatcherTransport; |
| 66 | 66 |
| 67 MessagePipeDispatcher(); | 67 MessagePipeDispatcher(); |
| 68 ~MessagePipeDispatcher() override; | 68 ~MessagePipeDispatcher() override; |
| 69 | 69 |
| 70 // Gets a dumb pointer to |message_pipe_|. This must be called under the | 70 // Gets a dumb pointer to |message_pipe_|. This must be called under the |
| 71 // |Dispatcher| lock (that it's a dumb pointer is okay since it's under lock). | 71 // |Dispatcher| lock (that it's a dumb pointer is okay since it's under lock). |
| 72 // This is needed when sending handles across processes, where nontrivial, | 72 // This is needed when sending handles across processes, where nontrivial, |
| 73 // invasive work needs to be done. | 73 // invasive work needs to be done. |
| 74 MessagePipe* GetMessagePipeNoLock() const; | 74 MessagePipe* GetMessagePipeNoLock() const; |
| 75 // Similarly for the port. | 75 // Similarly for the port. |
| 76 unsigned GetPortNoLock() const; | 76 unsigned GetPortNoLock() const; |
| 77 | 77 |
| 78 // |Dispatcher| protected methods: | 78 // |Dispatcher| protected methods: |
| 79 void CancelAllAwakablesNoLock() override; | 79 void CancelAllAwakablesNoLock() override; |
| 80 void CloseImplNoLock() override; | 80 void CloseImplNoLock() override; |
| 81 RefPtr<Dispatcher> CreateEquivalentDispatcherAndCloseImplNoLock() override; | 81 util::RefPtr<Dispatcher> CreateEquivalentDispatcherAndCloseImplNoLock() |
| 82 override; |
| 82 MojoResult WriteMessageImplNoLock( | 83 MojoResult WriteMessageImplNoLock( |
| 83 UserPointer<const void> bytes, | 84 UserPointer<const void> bytes, |
| 84 uint32_t num_bytes, | 85 uint32_t num_bytes, |
| 85 std::vector<DispatcherTransport>* transports, | 86 std::vector<DispatcherTransport>* transports, |
| 86 MojoWriteMessageFlags flags) override; | 87 MojoWriteMessageFlags flags) override; |
| 87 MojoResult ReadMessageImplNoLock(UserPointer<void> bytes, | 88 MojoResult ReadMessageImplNoLock(UserPointer<void> bytes, |
| 88 UserPointer<uint32_t> num_bytes, | 89 UserPointer<uint32_t> num_bytes, |
| 89 DispatcherVector* dispatchers, | 90 DispatcherVector* dispatchers, |
| 90 uint32_t* num_dispatchers, | 91 uint32_t* num_dispatchers, |
| 91 MojoReadMessageFlags flags) override; | 92 MojoReadMessageFlags flags) override; |
| 92 HandleSignalsState GetHandleSignalsStateImplNoLock() const override; | 93 HandleSignalsState GetHandleSignalsStateImplNoLock() const override; |
| 93 MojoResult AddAwakableImplNoLock(Awakable* awakable, | 94 MojoResult AddAwakableImplNoLock(Awakable* awakable, |
| 94 MojoHandleSignals signals, | 95 MojoHandleSignals signals, |
| 95 uint32_t context, | 96 uint32_t context, |
| 96 HandleSignalsState* signals_state) override; | 97 HandleSignalsState* signals_state) override; |
| 97 void RemoveAwakableImplNoLock(Awakable* awakable, | 98 void RemoveAwakableImplNoLock(Awakable* awakable, |
| 98 HandleSignalsState* signals_state) override; | 99 HandleSignalsState* signals_state) override; |
| 99 void StartSerializeImplNoLock(Channel* channel, | 100 void StartSerializeImplNoLock(Channel* channel, |
| 100 size_t* max_size, | 101 size_t* max_size, |
| 101 size_t* max_platform_handles) override | 102 size_t* max_platform_handles) override |
| 102 MOJO_NOT_THREAD_SAFE; | 103 MOJO_NOT_THREAD_SAFE; |
| 103 bool EndSerializeAndCloseImplNoLock( | 104 bool EndSerializeAndCloseImplNoLock( |
| 104 Channel* channel, | 105 Channel* channel, |
| 105 void* destination, | 106 void* destination, |
| 106 size_t* actual_size, | 107 size_t* actual_size, |
| 107 embedder::PlatformHandleVector* platform_handles) override | 108 embedder::PlatformHandleVector* platform_handles) override |
| 108 MOJO_NOT_THREAD_SAFE; | 109 MOJO_NOT_THREAD_SAFE; |
| 109 | 110 |
| 110 // This will be null if closed. | 111 // This will be null if closed. |
| 111 RefPtr<MessagePipe> message_pipe_ MOJO_GUARDED_BY(mutex()); | 112 util::RefPtr<MessagePipe> message_pipe_ MOJO_GUARDED_BY(mutex()); |
| 112 unsigned port_ MOJO_GUARDED_BY(mutex()); | 113 unsigned port_ MOJO_GUARDED_BY(mutex()); |
| 113 | 114 |
| 114 MOJO_DISALLOW_COPY_AND_ASSIGN(MessagePipeDispatcher); | 115 MOJO_DISALLOW_COPY_AND_ASSIGN(MessagePipeDispatcher); |
| 115 }; | 116 }; |
| 116 | 117 |
| 117 class MessagePipeDispatcherTransport : public DispatcherTransport { | 118 class MessagePipeDispatcherTransport : public DispatcherTransport { |
| 118 public: | 119 public: |
| 119 explicit MessagePipeDispatcherTransport(DispatcherTransport transport); | 120 explicit MessagePipeDispatcherTransport(DispatcherTransport transport); |
| 120 | 121 |
| 121 MessagePipe* GetMessagePipe() { | 122 MessagePipe* GetMessagePipe() { |
| 122 return message_pipe_dispatcher()->GetMessagePipeNoLock(); | 123 return message_pipe_dispatcher()->GetMessagePipeNoLock(); |
| 123 } | 124 } |
| 124 unsigned GetPort() { return message_pipe_dispatcher()->GetPortNoLock(); } | 125 unsigned GetPort() { return message_pipe_dispatcher()->GetPortNoLock(); } |
| 125 | 126 |
| 126 private: | 127 private: |
| 127 MessagePipeDispatcher* message_pipe_dispatcher() { | 128 MessagePipeDispatcher* message_pipe_dispatcher() { |
| 128 return static_cast<MessagePipeDispatcher*>(dispatcher()); | 129 return static_cast<MessagePipeDispatcher*>(dispatcher()); |
| 129 } | 130 } |
| 130 | 131 |
| 131 // Copy and assign allowed. | 132 // Copy and assign allowed. |
| 132 }; | 133 }; |
| 133 | 134 |
| 134 } // namespace system | 135 } // namespace system |
| 135 } // namespace mojo | 136 } // namespace mojo |
| 136 | 137 |
| 137 #endif // MOJO_EDK_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_ | 138 #endif // MOJO_EDK_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_ |
| OLD | NEW |