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