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_DATA_PIPE_H_ | 5 #ifndef MOJO_EDK_SYSTEM_DATA_PIPE_H_ |
6 #define MOJO_EDK_SYSTEM_DATA_PIPE_H_ | 6 #define MOJO_EDK_SYSTEM_DATA_PIPE_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <vector> | |
9 | 10 |
11 #include "base/callback_forward.h" | |
10 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/memory/ref_counted.h" | |
11 #include "mojo/edk/embedder/platform_handle_vector.h" | 14 #include "mojo/edk/embedder/platform_handle_vector.h" |
15 #include "mojo/edk/embedder/platform_shared_buffer.h" | |
12 #include "mojo/edk/embedder/scoped_platform_handle.h" | 16 #include "mojo/edk/embedder/scoped_platform_handle.h" |
13 #include "mojo/edk/system/system_impl_export.h" | 17 #include "mojo/edk/system/system_impl_export.h" |
14 #include "mojo/public/c/system/data_pipe.h" | 18 #include "mojo/public/c/system/data_pipe.h" |
15 #include "mojo/public/c/system/types.h" | 19 #include "mojo/public/c/system/types.h" |
16 #include "mojo/public/cpp/system/macros.h" | 20 #include "mojo/public/cpp/system/macros.h" |
17 | 21 |
18 namespace mojo { | 22 namespace mojo { |
19 namespace edk { | 23 namespace edk { |
20 class RawChannel; | 24 class RawChannel; |
21 | 25 |
26 enum DataPipeCommand { | |
Anand Mistry (off Chromium)
2016/01/08 03:28:02
: uint32_t
That way, you can just use this type in
Eliot Courtney
2016/01/08 04:44:39
Done.
| |
27 PLEASE_CREATE_SHARED_BUFFER, | |
Anand Mistry (off Chromium)
2016/01/08 03:28:02
With the broker, you no longer need to do this. It
Eliot Courtney
2016/01/08 04:44:39
Whoops, I already did this but forgot to upload it
| |
28 NOTIFY_SHARED_BUFFER, | |
29 DATA_WRITTEN, | |
30 DATA_READ | |
31 }; | |
32 | |
33 struct DataPipeCommandHeader { | |
34 uint32_t command; | |
35 size_t num_bytes; | |
Anand Mistry (off Chromium)
2016/01/08 03:28:02
Never use size_t in a serialised message.
Eliot Courtney
2016/01/08 04:44:39
Done.
| |
36 }; | |
37 | |
22 // Shared code between DataPipeConsumerDispatcher and | 38 // Shared code between DataPipeConsumerDispatcher and |
23 // DataPipeProducerDispatcher. | 39 // DataPipeProducerDispatcher. This class is not thread safe -- all locking must |
24 class MOJO_SYSTEM_IMPL_EXPORT DataPipe { | 40 // be performed by the caller. This class is only intended to be used as a |
41 // writer or a reader, not both. | |
42 class MOJO_SYSTEM_IMPL_EXPORT DataPipe final | |
43 : public base::RefCountedThreadSafe<DataPipe> { | |
25 public: | 44 public: |
26 // The default options for |MojoCreateDataPipe()|. (Real uses should obtain | 45 // The default options for |MojoCreateDataPipe()|. (Real uses should obtain |
27 // this via |ValidateCreateOptions()| with a null |in_options|; this is | 46 // this via |ValidateCreateOptions()| with a null |in_options|; this is |
28 // exposed directly for testing convenience.) | 47 // exposed directly for testing convenience.) |
29 static MojoCreateDataPipeOptions GetDefaultCreateOptions(); | 48 static MojoCreateDataPipeOptions GetDefaultCreateOptions(); |
30 | 49 |
31 // Validates and/or sets default options for |MojoCreateDataPipeOptions|. If | 50 // Validates and/or sets default options for |MojoCreateDataPipeOptions|. If |
32 // non-null, |in_options| must point to a struct of at least | 51 // non-null, |in_options| must point to a struct of at least |
33 // |in_options->struct_size| bytes. |out_options| must point to a (current) | 52 // |in_options->struct_size| bytes. |out_options| must point to a (current) |
34 // |MojoCreateDataPipeOptions| and will be entirely overwritten on success (it | 53 // |MojoCreateDataPipeOptions| and will be entirely overwritten on success (it |
35 // may be partly overwritten on failure). | 54 // may be partly overwritten on failure). |
36 static MojoResult ValidateCreateOptions( | 55 static MojoResult ValidateCreateOptions( |
37 const MojoCreateDataPipeOptions* in_options, | 56 const MojoCreateDataPipeOptions* in_options, |
38 MojoCreateDataPipeOptions* out_options); | 57 MojoCreateDataPipeOptions* out_options); |
39 | 58 |
59 explicit DataPipe(const MojoCreateDataPipeOptions& options); | |
60 | |
61 void Init(ScopedPlatformHandle message_pipe, | |
62 char* serialized_write_buffer, | |
63 size_t serialized_write_buffer_size, | |
64 char* serialized_read_buffer, | |
65 size_t serialized_read_buffer_size, | |
66 ScopedPlatformHandle shared_buffer_handle, | |
67 size_t ring_buffer_start, | |
68 size_t ring_buffer_size, | |
69 bool is_producer, | |
70 const base::Closure& init_callback); | |
71 | |
40 // Helper methods used by DataPipeConsumerDispatcher and | 72 // Helper methods used by DataPipeConsumerDispatcher and |
41 // DataPipeProducerDispatcher for serialization and deserialization. | 73 // DataPipeProducerDispatcher for serialization and deserialization. |
42 static void StartSerialize(bool have_channel_handle, | 74 void StartSerialize(size_t* max_size, size_t* max_platform_handles); |
43 bool have_shared_memory, | 75 void EndSerialize(void* destination, |
44 size_t* max_size, | 76 size_t* actual_size, |
45 size_t* max_platform_handles); | 77 PlatformHandleVector* platform_handles); |
46 static void EndSerialize(const MojoCreateDataPipeOptions& options, | |
47 ScopedPlatformHandle channel_handle, | |
48 ScopedPlatformHandle shared_memory_handle, | |
49 size_t shared_memory_size, | |
50 void* destination, | |
51 size_t* actual_size, | |
52 PlatformHandleVector* platform_handles); | |
53 static ScopedPlatformHandle Deserialize( | 78 static ScopedPlatformHandle Deserialize( |
54 const void* source, | 79 const void* source, |
55 size_t size, | 80 size_t size, |
56 PlatformHandleVector* platform_handles, | 81 PlatformHandleVector* platform_handles, |
57 MojoCreateDataPipeOptions* options, | 82 MojoCreateDataPipeOptions* options, |
58 ScopedPlatformHandle* shared_memory_handle, | 83 ScopedPlatformHandle* channel_shared_handle, |
59 size_t* shared_memory_size); | 84 size_t* serialized_read_buffer_size, |
85 size_t* serialized_write_buffer_size, | |
86 ScopedPlatformHandle* shared_buffer_handle, | |
87 size_t* ring_buffer_start, | |
88 size_t* ring_buffer_size); | |
89 | |
90 // Returns the number of readable or writable bytes. If there is no valid | |
91 // shared buffer, returns 0. | |
92 size_t GetReadableBytes() const; | |
93 size_t GetWritableBytes() const; | |
94 | |
95 // Two phase reads and writes. Note that since returned memory has to be | |
96 // contiguous, these may return less than the values returned by | |
97 // |GetReadableBytes| / |GetWritableBytes|. | |
98 void* GetWriteBuffer(size_t* num_bytes); | |
99 const void* GetReadBuffer(size_t* num_bytes); | |
100 | |
101 // Normal reads and writes. Will return false if they can't read or write | |
102 // all the data given. | |
103 bool WriteDataIntoSharedBuffer(const void* data, size_t num_bytes); | |
104 bool ReadDataFromSharedBuffer(void* buf, size_t num_bytes); | |
105 | |
106 // Notify other end of a read or write. Returns false if the other side was | |
107 // closed (on a RawChannel error). | |
108 bool NotifyWrite(size_t num_bytes); | |
109 bool NotifyRead(size_t num_bytes); | |
110 | |
111 // Sends the other side our shared buffer. | |
112 void NotifySharedBuffer(); | |
113 | |
114 // Asks the other side to give us a shared buffer. | |
115 void RequestSharedBuffer(); | |
116 | |
117 // Set our shared buffer. | |
118 void UpdateSharedBuffer(scoped_refptr<PlatformSharedBuffer> shared_buffer); | |
119 | |
120 // Reading and writing are split into three parts: 1. reading / writing the | |
121 // data, telling the other side what we did, and updating the ring buffer to | |
122 // reflect what we did. The functions |UpdateFromRead| and |UpdateFromWrite| | |
123 // update the ring buffer state. | |
124 void UpdateFromRead(size_t num_bytes); | |
125 void UpdateFromWrite(size_t num_bytes); | |
126 | |
127 // Releases the raw channel. | |
128 void Serialize(); | |
129 | |
130 RawChannel* GetChannel() { return channel_; } | |
131 const MojoCreateDataPipeOptions& GetOptions() { return options_; } | |
132 | |
133 // Returns whether readable or writable state changed. | |
134 bool ProcessCommand(const DataPipeCommandHeader& command, | |
135 ScopedPlatformHandleVectorPtr platform_handles); | |
136 | |
137 // Shuts down the channel, and releases the shared buffer. | |
138 void Shutdown(); | |
139 | |
140 // Named after |CreateEquivalentDispatcherAndCloseImplNoLock|. This will | |
141 // Release() the RawChannel and save its returned read and write buffer, then | |
142 // swap ourselves into |out|. | |
143 void CreateEquivalentAndClose(DataPipe* out); | |
144 | |
145 private: | |
146 friend class base::RefCountedThreadSafe<DataPipe>; | |
147 ~DataPipe(); | |
148 | |
149 void* GetSharedBufferBase(); | |
Anand Mistry (off Chromium)
2016/01/08 03:28:02
Make this a uint8_t*. That way, you can avoid all
Eliot Courtney
2016/01/08 04:44:39
Done.
| |
150 | |
151 RawChannel* channel_; | |
152 | |
153 MojoCreateDataPipeOptions options_; | |
154 | |
155 // True if we've released the channel because we're about to get serialised | |
156 // and transported. We also store the channel's read and write buffer, and | |
157 // handle. | |
158 bool channel_released_; | |
159 std::vector<char> serialized_read_buffer_; | |
160 std::vector<char> serialized_write_buffer_; | |
161 ScopedPlatformHandle serialized_channel_handle_; | |
162 | |
163 scoped_refptr<PlatformSharedBuffer> shared_buffer_; | |
164 // Keep the full mapping of the shared buffer around so we don't have to | |
165 // recreate it. | |
166 scoped_ptr<PlatformSharedBufferMapping> mapping_; | |
167 size_t ring_buffer_start_; | |
168 size_t ring_buffer_size_; | |
169 | |
170 // By default, the producer will attempt to create the shared buffer. | |
171 bool is_producer_; | |
60 }; | 172 }; |
61 | 173 |
62 } // namespace edk | 174 } // namespace edk |
63 } // namespace mojo | 175 } // namespace mojo |
64 | 176 |
65 #endif // MOJO_EDK_SYSTEM_DATA_PIPE_H_ | 177 #endif // MOJO_EDK_SYSTEM_DATA_PIPE_H_ |
OLD | NEW |