Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / mojo / system / local_data_pipe.h
blobba2b801319bb3443537d2a7a63a57a29f2744971
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
3 // found in the LICENSE file.
5 #ifndef MOJO_SYSTEM_LOCAL_DATA_PIPE_H_
6 #define MOJO_SYSTEM_LOCAL_DATA_PIPE_H_
8 #include "base/macros.h"
9 #include "base/memory/aligned_memory.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "mojo/system/data_pipe.h"
13 #include "mojo/system/system_impl_export.h"
15 namespace mojo {
16 namespace system {
18 // |LocalDataPipe| is a subclass that "implements" |DataPipe| for data pipes
19 // whose producer and consumer are both local. This class is thread-safe (with
20 // protection provided by |DataPipe|'s |lock_|.
21 class MOJO_SYSTEM_IMPL_EXPORT LocalDataPipe : public DataPipe {
22 public:
23 // |validated_options| should be the output of |DataPipe::ValidateOptions()|.
24 // In particular: |struct_size| is ignored (so |validated_options| must be the
25 // current version of the struct) and |capacity_num_bytes| must be nonzero.
26 explicit LocalDataPipe(const MojoCreateDataPipeOptions& validated_options);
28 private:
29 friend class base::RefCountedThreadSafe<LocalDataPipe>;
30 virtual ~LocalDataPipe();
32 // |DataPipe| implementation:
33 virtual void ProducerCloseImplNoLock() OVERRIDE;
34 virtual MojoResult ProducerWriteDataImplNoLock(
35 UserPointer<const void> elements,
36 UserPointer<uint32_t> num_bytes,
37 uint32_t max_num_bytes_to_write,
38 uint32_t min_num_bytes_to_write) OVERRIDE;
39 virtual MojoResult ProducerBeginWriteDataImplNoLock(
40 UserPointer<void*> buffer,
41 UserPointer<uint32_t> buffer_num_bytes,
42 uint32_t min_num_bytes_to_write) OVERRIDE;
43 virtual MojoResult ProducerEndWriteDataImplNoLock(
44 uint32_t num_bytes_written) OVERRIDE;
45 virtual HandleSignalsState ProducerGetHandleSignalsStateImplNoLock()
46 const OVERRIDE;
47 virtual void ConsumerCloseImplNoLock() OVERRIDE;
48 virtual MojoResult ConsumerReadDataImplNoLock(
49 UserPointer<void> elements,
50 UserPointer<uint32_t> num_bytes,
51 uint32_t max_num_bytes_to_read,
52 uint32_t min_num_bytes_to_read) OVERRIDE;
53 virtual MojoResult ConsumerDiscardDataImplNoLock(
54 UserPointer<uint32_t> num_bytes,
55 uint32_t max_num_bytes_to_discard,
56 uint32_t min_num_bytes_to_discard) OVERRIDE;
57 virtual MojoResult ConsumerQueryDataImplNoLock(
58 UserPointer<uint32_t> num_bytes) OVERRIDE;
59 virtual MojoResult ConsumerBeginReadDataImplNoLock(
60 UserPointer<const void*> buffer,
61 UserPointer<uint32_t> buffer_num_bytes,
62 uint32_t min_num_bytes_to_read) OVERRIDE;
63 virtual MojoResult ConsumerEndReadDataImplNoLock(
64 uint32_t num_bytes_read) OVERRIDE;
65 virtual HandleSignalsState ConsumerGetHandleSignalsStateImplNoLock()
66 const OVERRIDE;
68 void EnsureBufferNoLock();
69 void DestroyBufferNoLock();
71 // Get the maximum (single) write/read size right now (in number of elements);
72 // result fits in a |uint32_t|.
73 size_t GetMaxNumBytesToWriteNoLock();
74 size_t GetMaxNumBytesToReadNoLock();
76 // Marks the given number of bytes as consumed/discarded. |num_bytes| must be
77 // greater than |current_num_bytes_|.
78 void MarkDataAsConsumedNoLock(size_t num_bytes);
80 // The members below are protected by |DataPipe|'s |lock_|:
81 scoped_ptr<char, base::AlignedFreeDeleter> buffer_;
82 // Circular buffer.
83 size_t start_index_;
84 size_t current_num_bytes_;
86 DISALLOW_COPY_AND_ASSIGN(LocalDataPipe);
89 } // namespace system
90 } // namespace mojo
92 #endif // MOJO_SYSTEM_LOCAL_DATA_PIPE_H_