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_LOCAL_DATA_PIPE_IMPL_H_ | 5 #ifndef MOJO_EDK_SYSTEM_LOCAL_DATA_PIPE_IMPL_H_ |
6 #define MOJO_EDK_SYSTEM_LOCAL_DATA_PIPE_IMPL_H_ | 6 #define MOJO_EDK_SYSTEM_LOCAL_DATA_PIPE_IMPL_H_ |
7 | 7 |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/memory/aligned_memory.h" | 9 #include "base/memory/aligned_memory.h" |
10 #include "base/memory/ref_counted.h" | |
11 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
12 #include "mojo/edk/system/data_pipe.h" | 11 #include "mojo/edk/system/data_pipe_impl.h" |
13 #include "mojo/edk/system/system_impl_export.h" | 12 #include "mojo/edk/system/system_impl_export.h" |
14 | 13 |
15 namespace mojo { | 14 namespace mojo { |
16 namespace system { | 15 namespace system { |
17 | 16 |
| 17 class DataPipe; |
| 18 |
18 // |LocalDataPipeImpl| is a subclass that "implements" |DataPipe| for data pipes | 19 // |LocalDataPipeImpl| is a subclass that "implements" |DataPipe| for data pipes |
19 // whose producer and consumer are both local. This class is thread-safe (with | 20 // whose producer and consumer are both local. See |DataPipeImpl| for more |
20 // protection provided by |DataPipe|'s |lock_|. | 21 // details. |
21 class MOJO_SYSTEM_IMPL_EXPORT LocalDataPipeImpl : public DataPipe { | 22 class MOJO_SYSTEM_IMPL_EXPORT LocalDataPipeImpl : public DataPipeImpl { |
22 public: | 23 public: |
23 // |validated_options| should be the output of |DataPipe::ValidateOptions()|. | 24 LocalDataPipeImpl(); |
24 // In particular: |struct_size| is ignored (so |validated_options| must be the | 25 ~LocalDataPipeImpl() override; |
25 // current version of the struct) and |capacity_num_bytes| must be nonzero. | |
26 explicit LocalDataPipeImpl( | |
27 const MojoCreateDataPipeOptions& validated_options); | |
28 | 26 |
29 private: | 27 private: |
30 friend class base::RefCountedThreadSafe<LocalDataPipeImpl>; | 28 // |DataPipeImpl| implementation: |
31 ~LocalDataPipeImpl() override; | 29 void ProducerClose() override; |
32 | 30 MojoResult ProducerWriteData(UserPointer<const void> elements, |
33 // |DataPipe| implementation: | 31 UserPointer<uint32_t> num_bytes, |
34 void ProducerCloseImplNoLock() override; | 32 uint32_t max_num_bytes_to_write, |
35 MojoResult ProducerWriteDataImplNoLock( | 33 uint32_t min_num_bytes_to_write) override; |
36 UserPointer<const void> elements, | 34 MojoResult ProducerBeginWriteData(UserPointer<void*> buffer, |
37 UserPointer<uint32_t> num_bytes, | 35 UserPointer<uint32_t> buffer_num_bytes, |
38 uint32_t max_num_bytes_to_write, | 36 uint32_t min_num_bytes_to_write) override; |
39 uint32_t min_num_bytes_to_write) override; | 37 MojoResult ProducerEndWriteData(uint32_t num_bytes_written) override; |
40 MojoResult ProducerBeginWriteDataImplNoLock( | 38 HandleSignalsState ProducerGetHandleSignalsState() const override; |
41 UserPointer<void*> buffer, | 39 void ProducerStartSerialize(Channel* channel, |
42 UserPointer<uint32_t> buffer_num_bytes, | 40 size_t* max_size, |
43 uint32_t min_num_bytes_to_write) override; | 41 size_t* max_platform_handles) override; |
44 MojoResult ProducerEndWriteDataImplNoLock( | 42 bool ProducerEndSerialize( |
45 uint32_t num_bytes_written) override; | |
46 HandleSignalsState ProducerGetHandleSignalsStateImplNoLock() const override; | |
47 void ProducerStartSerializeImplNoLock(Channel* channel, | |
48 size_t* max_size, | |
49 size_t* max_platform_handles) override; | |
50 bool ProducerEndSerializeImplNoLock( | |
51 Channel* channel, | 43 Channel* channel, |
52 void* destination, | 44 void* destination, |
53 size_t* actual_size, | 45 size_t* actual_size, |
54 embedder::PlatformHandleVector* platform_handles) override; | 46 embedder::PlatformHandleVector* platform_handles) override; |
55 void ConsumerCloseImplNoLock() override; | 47 void ConsumerClose() override; |
56 MojoResult ConsumerReadDataImplNoLock(UserPointer<void> elements, | 48 MojoResult ConsumerReadData(UserPointer<void> elements, |
57 UserPointer<uint32_t> num_bytes, | 49 UserPointer<uint32_t> num_bytes, |
58 uint32_t max_num_bytes_to_read, | 50 uint32_t max_num_bytes_to_read, |
59 uint32_t min_num_bytes_to_read, | 51 uint32_t min_num_bytes_to_read, |
60 bool peek) override; | 52 bool peek) override; |
61 MojoResult ConsumerDiscardDataImplNoLock( | 53 MojoResult ConsumerDiscardData(UserPointer<uint32_t> num_bytes, |
62 UserPointer<uint32_t> num_bytes, | 54 uint32_t max_num_bytes_to_discard, |
63 uint32_t max_num_bytes_to_discard, | 55 uint32_t min_num_bytes_to_discard) override; |
64 uint32_t min_num_bytes_to_discard) override; | 56 MojoResult ConsumerQueryData(UserPointer<uint32_t> num_bytes) override; |
65 MojoResult ConsumerQueryDataImplNoLock( | 57 MojoResult ConsumerBeginReadData(UserPointer<const void*> buffer, |
66 UserPointer<uint32_t> num_bytes) override; | 58 UserPointer<uint32_t> buffer_num_bytes, |
67 MojoResult ConsumerBeginReadDataImplNoLock( | 59 uint32_t min_num_bytes_to_read) override; |
68 UserPointer<const void*> buffer, | 60 MojoResult ConsumerEndReadData(uint32_t num_bytes_read) override; |
69 UserPointer<uint32_t> buffer_num_bytes, | 61 HandleSignalsState ConsumerGetHandleSignalsState() const override; |
70 uint32_t min_num_bytes_to_read) override; | 62 void ConsumerStartSerialize(Channel* channel, |
71 MojoResult ConsumerEndReadDataImplNoLock(uint32_t num_bytes_read) override; | 63 size_t* max_size, |
72 HandleSignalsState ConsumerGetHandleSignalsStateImplNoLock() const override; | 64 size_t* max_platform_handles) override; |
73 void ConsumerStartSerializeImplNoLock(Channel* channel, | 65 bool ConsumerEndSerialize( |
74 size_t* max_size, | |
75 size_t* max_platform_handles) override; | |
76 bool ConsumerEndSerializeImplNoLock( | |
77 Channel* channel, | 66 Channel* channel, |
78 void* destination, | 67 void* destination, |
79 size_t* actual_size, | 68 size_t* actual_size, |
80 embedder::PlatformHandleVector* platform_handles) override; | 69 embedder::PlatformHandleVector* platform_handles) override; |
81 | 70 |
82 void EnsureBufferNoLock(); | 71 void EnsureBuffer(); |
83 void DestroyBufferNoLock(); | 72 void DestroyBuffer(); |
84 | 73 |
85 // Get the maximum (single) write/read size right now (in number of elements); | 74 // Get the maximum (single) write/read size right now (in number of elements); |
86 // result fits in a |uint32_t|. | 75 // result fits in a |uint32_t|. |
87 size_t GetMaxNumBytesToWriteNoLock(); | 76 size_t GetMaxNumBytesToWrite(); |
88 size_t GetMaxNumBytesToReadNoLock(); | 77 size_t GetMaxNumBytesToRead(); |
89 | 78 |
90 // Marks the given number of bytes as consumed/discarded. |num_bytes| must be | 79 // Marks the given number of bytes as consumed/discarded. |num_bytes| must be |
91 // greater than |current_num_bytes_|. | 80 // greater than |current_num_bytes_|. |
92 void MarkDataAsConsumedNoLock(size_t num_bytes); | 81 void MarkDataAsConsumed(size_t num_bytes); |
93 | 82 |
94 // The members below are protected by |DataPipe|'s |lock_|: | |
95 scoped_ptr<char, base::AlignedFreeDeleter> buffer_; | 83 scoped_ptr<char, base::AlignedFreeDeleter> buffer_; |
96 // Circular buffer. | 84 // Circular buffer. |
97 size_t start_index_; | 85 size_t start_index_; |
98 size_t current_num_bytes_; | 86 size_t current_num_bytes_; |
99 | 87 |
100 DISALLOW_COPY_AND_ASSIGN(LocalDataPipeImpl); | 88 DISALLOW_COPY_AND_ASSIGN(LocalDataPipeImpl); |
101 }; | 89 }; |
102 | 90 |
103 } // namespace system | 91 } // namespace system |
104 } // namespace mojo | 92 } // namespace mojo |
105 | 93 |
106 #endif // MOJO_EDK_SYSTEM_LOCAL_DATA_PIPE_IMPL_H_ | 94 #endif // MOJO_EDK_SYSTEM_LOCAL_DATA_PIPE_IMPL_H_ |
OLD | NEW |