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