OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 NET_BASE_IO_BUFFER_H_ | 5 #ifndef NET_BASE_IO_BUFFER_H_ |
6 #define NET_BASE_IO_BUFFER_H_ | 6 #define NET_BASE_IO_BUFFER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/pickle.h" | 13 #include "base/pickle.h" |
| 14 #include "net/base/net_api.h" |
14 | 15 |
15 namespace net { | 16 namespace net { |
16 | 17 |
17 // This is a simple wrapper around a buffer that provides ref counting for | 18 // This is a simple wrapper around a buffer that provides ref counting for |
18 // easier asynchronous IO handling. | 19 // easier asynchronous IO handling. |
19 class IOBuffer : public base::RefCountedThreadSafe<IOBuffer> { | 20 class NET_API IOBuffer : public base::RefCountedThreadSafe<IOBuffer> { |
20 public: | 21 public: |
21 IOBuffer(); | 22 IOBuffer(); |
22 explicit IOBuffer(int buffer_size); | 23 explicit IOBuffer(int buffer_size); |
23 | 24 |
24 char* data() { return data_; } | 25 char* data() { return data_; } |
25 | 26 |
26 protected: | 27 protected: |
27 friend class base::RefCountedThreadSafe<IOBuffer>; | 28 friend class base::RefCountedThreadSafe<IOBuffer>; |
28 | 29 |
29 // Only allow derived classes to specify data_. | 30 // Only allow derived classes to specify data_. |
30 // In all other cases, we own data_, and must delete it at destruction time. | 31 // In all other cases, we own data_, and must delete it at destruction time. |
31 explicit IOBuffer(char* data); | 32 explicit IOBuffer(char* data); |
32 | 33 |
33 virtual ~IOBuffer(); | 34 virtual ~IOBuffer(); |
34 | 35 |
35 char* data_; | 36 char* data_; |
36 }; | 37 }; |
37 | 38 |
38 // This version stores the size of the buffer so that the creator of the object | 39 // This version stores the size of the buffer so that the creator of the object |
39 // doesn't have to keep track of that value. | 40 // doesn't have to keep track of that value. |
40 // NOTE: This doesn't mean that we want to stop sending the size as an explicit | 41 // NOTE: This doesn't mean that we want to stop sending the size as an explicit |
41 // argument to IO functions. Please keep using IOBuffer* for API declarations. | 42 // argument to IO functions. Please keep using IOBuffer* for API declarations. |
42 class IOBufferWithSize : public IOBuffer { | 43 class NET_API IOBufferWithSize : public IOBuffer { |
43 public: | 44 public: |
44 explicit IOBufferWithSize(int size); | 45 explicit IOBufferWithSize(int size); |
45 | 46 |
46 int size() const { return size_; } | 47 int size() const { return size_; } |
47 | 48 |
48 private: | 49 private: |
49 virtual ~IOBufferWithSize(); | 50 virtual ~IOBufferWithSize(); |
50 | 51 |
51 int size_; | 52 int size_; |
52 }; | 53 }; |
53 | 54 |
54 // This is a read only IOBuffer. The data is stored in a string and | 55 // This is a read only IOBuffer. The data is stored in a string and |
55 // the IOBuffer interface does not provide a proper way to modify it. | 56 // the IOBuffer interface does not provide a proper way to modify it. |
56 class StringIOBuffer : public IOBuffer { | 57 class NET_API StringIOBuffer : public IOBuffer { |
57 public: | 58 public: |
58 explicit StringIOBuffer(const std::string& s); | 59 explicit StringIOBuffer(const std::string& s); |
59 | 60 |
60 int size() const { return string_data_.size(); } | 61 int size() const { return string_data_.size(); } |
61 | 62 |
62 private: | 63 private: |
63 virtual ~StringIOBuffer(); | 64 virtual ~StringIOBuffer(); |
64 | 65 |
65 std::string string_data_; | 66 std::string string_data_; |
66 }; | 67 }; |
67 | 68 |
68 // This version wraps an existing IOBuffer and provides convenient functions | 69 // This version wraps an existing IOBuffer and provides convenient functions |
69 // to progressively read all the data. | 70 // to progressively read all the data. |
70 class DrainableIOBuffer : public IOBuffer { | 71 class NET_API DrainableIOBuffer : public IOBuffer { |
71 public: | 72 public: |
72 DrainableIOBuffer(IOBuffer* base, int size); | 73 DrainableIOBuffer(IOBuffer* base, int size); |
73 | 74 |
74 // DidConsume() changes the |data_| pointer so that |data_| always points | 75 // DidConsume() changes the |data_| pointer so that |data_| always points |
75 // to the first unconsumed byte. | 76 // to the first unconsumed byte. |
76 void DidConsume(int bytes); | 77 void DidConsume(int bytes); |
77 | 78 |
78 // Returns the number of unconsumed bytes. | 79 // Returns the number of unconsumed bytes. |
79 int BytesRemaining() const; | 80 int BytesRemaining() const; |
80 | 81 |
81 // Returns the number of consumed bytes. | 82 // Returns the number of consumed bytes. |
82 int BytesConsumed() const; | 83 int BytesConsumed() const; |
83 | 84 |
84 // Seeks to an arbitrary point in the buffer. The notion of bytes consumed | 85 // Seeks to an arbitrary point in the buffer. The notion of bytes consumed |
85 // and remaining are updated appropriately. | 86 // and remaining are updated appropriately. |
86 void SetOffset(int bytes); | 87 void SetOffset(int bytes); |
87 | 88 |
88 int size() const { return size_; } | 89 int size() const { return size_; } |
89 | 90 |
90 private: | 91 private: |
91 virtual ~DrainableIOBuffer(); | 92 virtual ~DrainableIOBuffer(); |
92 | 93 |
93 scoped_refptr<IOBuffer> base_; | 94 scoped_refptr<IOBuffer> base_; |
94 int size_; | 95 int size_; |
95 int used_; | 96 int used_; |
96 }; | 97 }; |
97 | 98 |
98 // This version provides a resizable buffer and a changeable offset. | 99 // This version provides a resizable buffer and a changeable offset. |
99 class GrowableIOBuffer : public IOBuffer { | 100 class NET_API GrowableIOBuffer : public IOBuffer { |
100 public: | 101 public: |
101 GrowableIOBuffer(); | 102 GrowableIOBuffer(); |
102 | 103 |
103 // realloc memory to the specified capacity. | 104 // realloc memory to the specified capacity. |
104 void SetCapacity(int capacity); | 105 void SetCapacity(int capacity); |
105 int capacity() { return capacity_; } | 106 int capacity() { return capacity_; } |
106 | 107 |
107 // |offset| moves the |data_| pointer, allowing "seeking" in the data. | 108 // |offset| moves the |data_| pointer, allowing "seeking" in the data. |
108 void set_offset(int offset); | 109 void set_offset(int offset); |
109 int offset() { return offset_; } | 110 int offset() { return offset_; } |
110 | 111 |
111 int RemainingCapacity(); | 112 int RemainingCapacity(); |
112 char* StartOfBuffer(); | 113 char* StartOfBuffer(); |
113 | 114 |
114 private: | 115 private: |
115 virtual ~GrowableIOBuffer(); | 116 virtual ~GrowableIOBuffer(); |
116 | 117 |
117 scoped_ptr_malloc<char> real_data_; | 118 scoped_ptr_malloc<char> real_data_; |
118 int capacity_; | 119 int capacity_; |
119 int offset_; | 120 int offset_; |
120 }; | 121 }; |
121 | 122 |
122 // This versions allows a pickle to be used as the storage for a write-style | 123 // This versions allows a pickle to be used as the storage for a write-style |
123 // operation, avoiding an extra data copy. | 124 // operation, avoiding an extra data copy. |
124 class PickledIOBuffer : public IOBuffer { | 125 class NET_API PickledIOBuffer : public IOBuffer { |
125 public: | 126 public: |
126 PickledIOBuffer(); | 127 PickledIOBuffer(); |
127 | 128 |
128 Pickle* pickle() { return &pickle_; } | 129 Pickle* pickle() { return &pickle_; } |
129 | 130 |
130 // Signals that we are done writing to the picke and we can use it for a | 131 // Signals that we are done writing to the picke and we can use it for a |
131 // write-style IO operation. | 132 // write-style IO operation. |
132 void Done(); | 133 void Done(); |
133 | 134 |
134 private: | 135 private: |
135 virtual ~PickledIOBuffer(); | 136 virtual ~PickledIOBuffer(); |
136 | 137 |
137 Pickle pickle_; | 138 Pickle pickle_; |
138 }; | 139 }; |
139 | 140 |
140 // This class allows the creation of a temporary IOBuffer that doesn't really | 141 // This class allows the creation of a temporary IOBuffer that doesn't really |
141 // own the underlying buffer. Please use this class only as a last resort. | 142 // own the underlying buffer. Please use this class only as a last resort. |
142 // A good example is the buffer for a synchronous operation, where we can be | 143 // A good example is the buffer for a synchronous operation, where we can be |
143 // sure that nobody is keeping an extra reference to this object so the lifetime | 144 // sure that nobody is keeping an extra reference to this object so the lifetime |
144 // of the buffer can be completely managed by its intended owner. | 145 // of the buffer can be completely managed by its intended owner. |
145 class WrappedIOBuffer : public IOBuffer { | 146 class NET_API WrappedIOBuffer : public IOBuffer { |
146 public: | 147 public: |
147 explicit WrappedIOBuffer(const char* data); | 148 explicit WrappedIOBuffer(const char* data); |
148 | 149 |
149 protected: | 150 protected: |
150 virtual ~WrappedIOBuffer(); | 151 virtual ~WrappedIOBuffer(); |
151 }; | 152 }; |
152 | 153 |
153 } // namespace net | 154 } // namespace net |
154 | 155 |
155 #endif // NET_BASE_IO_BUFFER_H_ | 156 #endif // NET_BASE_IO_BUFFER_H_ |
OLD | NEW |