OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 BASE_PICKLE_H_ | 5 #ifndef BASE_PICKLE_H_ |
6 #define BASE_PICKLE_H_ | 6 #define BASE_PICKLE_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/base_export.h" | 13 #include "base/base_export.h" |
14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
15 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/memory/ref_counted.h" |
17 #include "base/strings/string16.h" | 18 #include "base/strings/string16.h" |
18 #include "base/strings/string_piece.h" | 19 #include "base/strings/string_piece.h" |
19 | 20 |
| 21 #if defined(OS_POSIX) |
| 22 #include "base/files/file.h" |
| 23 #endif |
| 24 |
20 namespace base { | 25 namespace base { |
21 | 26 |
22 class Pickle; | 27 class Pickle; |
23 | 28 |
24 // PickleIterator reads data from a Pickle. The Pickle object must remain valid | 29 // PickleIterator reads data from a Pickle. The Pickle object must remain valid |
25 // while the PickleIterator object is in use. | 30 // while the PickleIterator object is in use. |
26 class BASE_EXPORT PickleIterator { | 31 class BASE_EXPORT PickleIterator { |
27 public: | 32 public: |
28 PickleIterator() : payload_(NULL), read_index_(0), end_index_(0) {} | 33 PickleIterator() : payload_(NULL), read_index_(0), end_index_(0) {} |
29 explicit PickleIterator(const Pickle& pickle); | 34 explicit PickleIterator(const Pickle& pickle); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 // what value types to read and in what order to read them as the Pickle does | 121 // what value types to read and in what order to read them as the Pickle does |
117 // not keep track of the type of data written to it. | 122 // not keep track of the type of data written to it. |
118 // | 123 // |
119 // The Pickle's data has a header which contains the size of the Pickle's | 124 // The Pickle's data has a header which contains the size of the Pickle's |
120 // payload. It can optionally support additional space in the header. That | 125 // payload. It can optionally support additional space in the header. That |
121 // space is controlled by the header_size parameter passed to the Pickle | 126 // space is controlled by the header_size parameter passed to the Pickle |
122 // constructor. | 127 // constructor. |
123 // | 128 // |
124 class BASE_EXPORT Pickle { | 129 class BASE_EXPORT Pickle { |
125 public: | 130 public: |
| 131 // Auxiliary data attached to a Pickle. Pickle must be subclassed along with |
| 132 // this interface in order to provide a concrete implementation of support |
| 133 // for attachments. The base Pickle implementation does not accept |
| 134 // attachments. |
| 135 class BASE_EXPORT Attachment : public RefCountedThreadSafe<Attachment> { |
| 136 public: |
| 137 Attachment(); |
| 138 |
| 139 protected: |
| 140 friend class RefCountedThreadSafe<Attachment>; |
| 141 virtual ~Attachment(); |
| 142 |
| 143 DISALLOW_COPY_AND_ASSIGN(Attachment); |
| 144 }; |
| 145 |
126 // Initialize a Pickle object using the default header size. | 146 // Initialize a Pickle object using the default header size. |
127 Pickle(); | 147 Pickle(); |
128 | 148 |
129 // Initialize a Pickle object with the specified header size in bytes, which | 149 // Initialize a Pickle object with the specified header size in bytes, which |
130 // must be greater-than-or-equal-to sizeof(Pickle::Header). The header size | 150 // must be greater-than-or-equal-to sizeof(Pickle::Header). The header size |
131 // will be rounded up to ensure that the header size is 32bit-aligned. | 151 // will be rounded up to ensure that the header size is 32bit-aligned. |
132 explicit Pickle(int header_size); | 152 explicit Pickle(int header_size); |
133 | 153 |
134 // Initializes a Pickle from a const block of data. The data is not copied; | 154 // Initializes a Pickle from a const block of data. The data is not copied; |
135 // instead the data is merely referenced by this Pickle. Only const methods | 155 // instead the data is merely referenced by this Pickle. Only const methods |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 bool WriteString(const StringPiece& value); | 219 bool WriteString(const StringPiece& value); |
200 bool WriteString16(const StringPiece16& value); | 220 bool WriteString16(const StringPiece16& value); |
201 // "Data" is a blob with a length. When you read it out you will be given the | 221 // "Data" is a blob with a length. When you read it out you will be given the |
202 // length. See also WriteBytes. | 222 // length. See also WriteBytes. |
203 bool WriteData(const char* data, int length); | 223 bool WriteData(const char* data, int length); |
204 // "Bytes" is a blob with no length. The caller must specify the length both | 224 // "Bytes" is a blob with no length. The caller must specify the length both |
205 // when reading and writing. It is normally used to serialize PoD types of a | 225 // when reading and writing. It is normally used to serialize PoD types of a |
206 // known size. See also WriteData. | 226 // known size. See also WriteData. |
207 bool WriteBytes(const void* data, int length); | 227 bool WriteBytes(const void* data, int length); |
208 | 228 |
| 229 // WriteAttachment appends |attachment| to the pickle. It returns |
| 230 // false iff the set is full or if the Pickle implementation does not support |
| 231 // attachments. |
| 232 virtual bool WriteAttachment(scoped_refptr<Attachment> attachment); |
| 233 |
| 234 // ReadAttachment parses an attachment given the parsing state |iter| and |
| 235 // writes it to |*attachment|. It returns true on success. |
| 236 virtual bool ReadAttachment(base::PickleIterator* iter, |
| 237 scoped_refptr<Attachment>* attachment) const; |
| 238 |
| 239 // Indicates whether the pickle has any attachments. |
| 240 virtual bool HasAttachments() const; |
| 241 |
209 // Reserves space for upcoming writes when multiple writes will be made and | 242 // Reserves space for upcoming writes when multiple writes will be made and |
210 // their sizes are computed in advance. It can be significantly faster to call | 243 // their sizes are computed in advance. It can be significantly faster to call |
211 // Reserve() before calling WriteFoo() multiple times. | 244 // Reserve() before calling WriteFoo() multiple times. |
212 void Reserve(size_t additional_capacity); | 245 void Reserve(size_t additional_capacity); |
213 | 246 |
214 // Payload follows after allocation of Header (header size is customizable). | 247 // Payload follows after allocation of Header (header size is customizable). |
215 struct Header { | 248 struct Header { |
216 uint32_t payload_size; // Specifies the size of the payload. | 249 uint32_t payload_size; // Specifies the size of the payload. |
217 }; | 250 }; |
218 | 251 |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 FRIEND_TEST_ALL_PREFIXES(PickleTest, PeekNext); | 349 FRIEND_TEST_ALL_PREFIXES(PickleTest, PeekNext); |
317 FRIEND_TEST_ALL_PREFIXES(PickleTest, PeekNextOverflow); | 350 FRIEND_TEST_ALL_PREFIXES(PickleTest, PeekNextOverflow); |
318 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext); | 351 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext); |
319 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader); | 352 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader); |
320 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextOverflow); | 353 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextOverflow); |
321 }; | 354 }; |
322 | 355 |
323 } // namespace base | 356 } // namespace base |
324 | 357 |
325 #endif // BASE_PICKLE_H_ | 358 #endif // BASE_PICKLE_H_ |
OLD | NEW |