| 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_DATA_PIPE_CONSUMER_DISPATCHER_H_ | 5 #ifndef MOJO_EDK_SYSTEM_DATA_PIPE_CONSUMER_DISPATCHER_H_ |
| 6 #define MOJO_EDK_SYSTEM_DATA_PIPE_CONSUMER_DISPATCHER_H_ | 6 #define MOJO_EDK_SYSTEM_DATA_PIPE_CONSUMER_DISPATCHER_H_ |
| 7 | 7 |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "mojo/edk/system/awakable_list.h" |
| 9 #include "mojo/edk/system/dispatcher.h" | 10 #include "mojo/edk/system/dispatcher.h" |
| 11 #include "mojo/edk/system/raw_channel.h" |
| 10 #include "mojo/edk/system/system_impl_export.h" | 12 #include "mojo/edk/system/system_impl_export.h" |
| 11 #include "mojo/public/cpp/system/macros.h" | 13 #include "mojo/public/cpp/system/macros.h" |
| 12 | 14 |
| 13 namespace mojo { | 15 namespace mojo { |
| 14 namespace system { | 16 namespace edk { |
| 15 | |
| 16 class DataPipe; | |
| 17 | 17 |
| 18 // This is the |Dispatcher| implementation for the consumer handle for data | 18 // This is the |Dispatcher| implementation for the consumer handle for data |
| 19 // pipes (created by the Mojo primitive |MojoCreateDataPipe()|). This class is | 19 // pipes (created by the Mojo primitive |MojoCreateDataPipe()|). This class is |
| 20 // thread-safe. | 20 // thread-safe. |
| 21 class MOJO_SYSTEM_IMPL_EXPORT DataPipeConsumerDispatcher final | 21 class MOJO_SYSTEM_IMPL_EXPORT DataPipeConsumerDispatcher final |
| 22 : public Dispatcher { | 22 : public Dispatcher, public RawChannel::Delegate { |
| 23 public: | 23 public: |
| 24 static scoped_refptr<DataPipeConsumerDispatcher> Create() { | 24 static scoped_refptr<DataPipeConsumerDispatcher> Create( |
| 25 return make_scoped_refptr(new DataPipeConsumerDispatcher()); | 25 const MojoCreateDataPipeOptions& options) { |
| 26 return make_scoped_refptr(new DataPipeConsumerDispatcher(options)); |
| 26 } | 27 } |
| 27 | 28 |
| 28 // Must be called before any other methods. | 29 // Must be called before any other methods. |
| 29 void Init(scoped_refptr<DataPipe> data_pipe) MOJO_NOT_THREAD_SAFE; | 30 void Init(ScopedPlatformHandle message_pipe); |
| 30 | 31 |
| 31 // |Dispatcher| public methods: | 32 // |Dispatcher| public methods: |
| 32 Type GetType() const override; | 33 Type GetType() const override; |
| 33 | 34 |
| 34 // The "opposite" of |SerializeAndClose()|. (Typically this is called by | 35 // The "opposite" of |SerializeAndClose()|. (Typically this is called by |
| 35 // |Dispatcher::Deserialize()|.) | 36 // |Dispatcher::Deserialize()|.) |
| 36 static scoped_refptr<DataPipeConsumerDispatcher> | 37 static scoped_refptr<DataPipeConsumerDispatcher> |
| 37 Deserialize(Channel* channel, const void* source, size_t size); | 38 Deserialize(const void* source, |
| 38 | 39 size_t size, |
| 39 // Get access to the |DataPipe| for testing. | 40 PlatformHandleVector* platform_handles); |
| 40 DataPipe* GetDataPipeForTest(); | |
| 41 | 41 |
| 42 private: | 42 private: |
| 43 DataPipeConsumerDispatcher(); | 43 DataPipeConsumerDispatcher(const MojoCreateDataPipeOptions& options); |
| 44 ~DataPipeConsumerDispatcher() override; | 44 ~DataPipeConsumerDispatcher() override; |
| 45 | 45 |
| 46 void InitOnIO(); |
| 47 void CloseOnIO(); |
| 48 |
| 46 // |Dispatcher| protected methods: | 49 // |Dispatcher| protected methods: |
| 47 void CancelAllAwakablesNoLock() override; | 50 void CancelAllAwakablesNoLock() override; |
| 48 void CloseImplNoLock() override; | 51 void CloseImplNoLock() override; |
| 49 scoped_refptr<Dispatcher> CreateEquivalentDispatcherAndCloseImplNoLock() | 52 scoped_refptr<Dispatcher> CreateEquivalentDispatcherAndCloseImplNoLock() |
| 50 override; | 53 override; |
| 51 MojoResult ReadDataImplNoLock(UserPointer<void> elements, | 54 MojoResult ReadDataImplNoLock(UserPointer<void> elements, |
| 52 UserPointer<uint32_t> num_bytes, | 55 UserPointer<uint32_t> num_bytes, |
| 53 MojoReadDataFlags flags) override; | 56 MojoReadDataFlags flags) override; |
| 54 MojoResult BeginReadDataImplNoLock(UserPointer<const void*> buffer, | 57 MojoResult BeginReadDataImplNoLock(UserPointer<const void*> buffer, |
| 55 UserPointer<uint32_t> buffer_num_bytes, | 58 UserPointer<uint32_t> buffer_num_bytes, |
| 56 MojoReadDataFlags flags) override; | 59 MojoReadDataFlags flags) override; |
| 57 MojoResult EndReadDataImplNoLock(uint32_t num_bytes_read) override; | 60 MojoResult EndReadDataImplNoLock(uint32_t num_bytes_read) override; |
| 58 HandleSignalsState GetHandleSignalsStateImplNoLock() const override; | 61 HandleSignalsState GetHandleSignalsStateImplNoLock() const override; |
| 59 MojoResult AddAwakableImplNoLock(Awakable* awakable, | 62 MojoResult AddAwakableImplNoLock(Awakable* awakable, |
| 60 MojoHandleSignals signals, | 63 MojoHandleSignals signals, |
| 61 uint32_t context, | 64 uint32_t context, |
| 62 HandleSignalsState* signals_state) override; | 65 HandleSignalsState* signals_state) override; |
| 63 void RemoveAwakableImplNoLock(Awakable* awakable, | 66 void RemoveAwakableImplNoLock(Awakable* awakable, |
| 64 HandleSignalsState* signals_state) override; | 67 HandleSignalsState* signals_state) override; |
| 65 void StartSerializeImplNoLock(Channel* channel, | 68 void StartSerializeImplNoLock(size_t* max_size, |
| 66 size_t* max_size, | 69 size_t* max_platform_handles) override; |
| 67 size_t* max_platform_handles) override | |
| 68 MOJO_NOT_THREAD_SAFE; | |
| 69 bool EndSerializeAndCloseImplNoLock( | 70 bool EndSerializeAndCloseImplNoLock( |
| 70 Channel* channel, | |
| 71 void* destination, | 71 void* destination, |
| 72 size_t* actual_size, | 72 size_t* actual_size, |
| 73 embedder::PlatformHandleVector* platform_handles) override | 73 PlatformHandleVector* platform_handles) override; |
| 74 MOJO_NOT_THREAD_SAFE; | 74 void TransportStarted() override; |
| 75 void TransportEnded() override; |
| 75 bool IsBusyNoLock() const override; | 76 bool IsBusyNoLock() const override; |
| 76 | 77 |
| 77 // This will be null if closed. | 78 // |RawChannel::Delegate methods: |
| 78 scoped_refptr<DataPipe> data_pipe_ MOJO_GUARDED_BY(mutex()); | 79 void OnReadMessage( |
| 80 const MessageInTransit::View& message_view, |
| 81 ScopedPlatformHandleVectorPtr platform_handles) override; |
| 82 void OnError(Error error) override; |
| 83 |
| 84 // See comment in MessagePipeDispatcher for this method. |
| 85 void SerializeInternal(); |
| 86 |
| 87 MojoCreateDataPipeOptions options_; |
| 88 |
| 89 // Protected by |lock()|: |
| 90 RawChannel* channel_; // This will be null if closed. |
| 91 |
| 92 // Queue of incoming messages. |
| 93 std::vector<char> data_; |
| 94 AwakableList awakable_list_; |
| 95 |
| 96 // if DispatcherTransport crated. must be set before lock() is called to avoid |
| 97 // deadlocks with RawChannel calling us. |
| 98 base::Lock started_transport_; |
| 99 |
| 100 bool calling_init_; |
| 101 |
| 102 bool in_two_phase_read_; |
| 103 uint32_t two_phase_max_bytes_read_; |
| 104 |
| 105 bool error_; |
| 106 |
| 107 bool serialized_; |
| 108 std::vector<char> serialized_read_buffer_; |
| 109 ScopedPlatformHandle serialized_platform_handle_; |
| 79 | 110 |
| 80 MOJO_DISALLOW_COPY_AND_ASSIGN(DataPipeConsumerDispatcher); | 111 MOJO_DISALLOW_COPY_AND_ASSIGN(DataPipeConsumerDispatcher); |
| 81 }; | 112 }; |
| 82 | 113 |
| 83 } // namespace system | 114 } // namespace edk |
| 84 } // namespace mojo | 115 } // namespace mojo |
| 85 | 116 |
| 86 #endif // MOJO_EDK_SYSTEM_DATA_PIPE_CONSUMER_DISPATCHER_H_ | 117 #endif // MOJO_EDK_SYSTEM_DATA_PIPE_CONSUMER_DISPATCHER_H_ |
| OLD | NEW |