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 NET_BASE_UPLOAD_DATA_STREAM_H_ | 5 #ifndef NET_BASE_UPLOAD_DATA_STREAM_H_ |
6 #define NET_BASE_UPLOAD_DATA_STREAM_H_ | 6 #define NET_BASE_UPLOAD_DATA_STREAM_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/memory/scoped_vector.h" | 10 #include "base/memory/scoped_vector.h" |
11 #include "net/base/completion_callback.h" | 11 #include "net/base/completion_callback.h" |
12 #include "net/base/net_export.h" | 12 #include "net/base/net_export.h" |
13 | 13 |
14 namespace net { | 14 namespace net { |
15 | 15 |
16 class DrainableIOBuffer; | 16 class DrainableIOBuffer; |
17 class IOBuffer; | 17 class IOBuffer; |
18 class UploadElementReader; | 18 class UploadElementReader; |
19 | 19 |
20 // A class for retrieving all data to be sent as a request body. Supports both | 20 // A class for retrieving all data to be sent as a request body. Supports both |
21 // chunked and non-chunked uploads. | 21 // chunked and non-chunked uploads. |
22 class NET_EXPORT UploadDataStream { | 22 class NET_EXPORT UploadDataStream { |
23 public: | 23 public: |
24 // |identifier| identifies a particular upload instance, which is used by the | 24 // |identifier| identifies a particular upload instance, which is used by the |
25 // cache to formulate a cache key. This value should be unique across browser | 25 // cache to formulate a cache key. This value should be unique across browser |
26 // sessions. A value of 0 is used to indicate an unspecified identifier. | 26 // sessions. A value of 0 is used to indicate an unspecified identifier. |
27 UploadDataStream(bool is_chunked, int64 identifier); | 27 UploadDataStream(bool is_chunked, int64_t identifier); |
28 | 28 |
29 virtual ~UploadDataStream(); | 29 virtual ~UploadDataStream(); |
30 | 30 |
31 // Initializes the stream. This function must be called before calling any | 31 // Initializes the stream. This function must be called before calling any |
32 // other method. It is not valid to call any method (other than the | 32 // other method. It is not valid to call any method (other than the |
33 // destructor) if Init() fails. This method can be called multiple times. | 33 // destructor) if Init() fails. This method can be called multiple times. |
34 // Calling this method after an Init() success results in resetting the | 34 // Calling this method after an Init() success results in resetting the |
35 // state (i.e. the stream is rewound). | 35 // state (i.e. the stream is rewound). |
36 // | 36 // |
37 // Does the initialization synchronously and returns the result if possible, | 37 // Does the initialization synchronously and returns the result if possible, |
(...skipping 17 matching lines...) Expand all Loading... |
55 // | 55 // |
56 // Reads are currently not allowed to fail - they must either return | 56 // Reads are currently not allowed to fail - they must either return |
57 // a value >= 0 or ERR_IO_PENDING, and call OnReadCompleted with a | 57 // a value >= 0 or ERR_IO_PENDING, and call OnReadCompleted with a |
58 // value >= 0. | 58 // value >= 0. |
59 // TODO(mmenke): Investigate letting reads fail. | 59 // TODO(mmenke): Investigate letting reads fail. |
60 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | 60 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
61 | 61 |
62 // Returns the total size of the data stream and the current position. | 62 // Returns the total size of the data stream and the current position. |
63 // When the data is chunked, always returns zero. Must always return the same | 63 // When the data is chunked, always returns zero. Must always return the same |
64 // value after each call to Initialize(). | 64 // value after each call to Initialize(). |
65 uint64 size() const { return total_size_; } | 65 uint64_t size() const { return total_size_; } |
66 uint64 position() const { return current_position_; } | 66 uint64_t position() const { return current_position_; } |
67 | 67 |
68 // See constructor for description. | 68 // See constructor for description. |
69 int64 identifier() const { return identifier_; } | 69 int64_t identifier() const { return identifier_; } |
70 | 70 |
71 bool is_chunked() const { return is_chunked_; } | 71 bool is_chunked() const { return is_chunked_; } |
72 | 72 |
73 // Returns true if all data has been consumed from this upload data | 73 // Returns true if all data has been consumed from this upload data |
74 // stream. For chunked uploads, returns false until the first read attempt. | 74 // stream. For chunked uploads, returns false until the first read attempt. |
75 // This makes some state machines a little simpler. | 75 // This makes some state machines a little simpler. |
76 bool IsEOF() const; | 76 bool IsEOF() const; |
77 | 77 |
78 // Cancels all pending callbacks, and resets state. Any IOBuffer currently | 78 // Cancels all pending callbacks, and resets state. Any IOBuffer currently |
79 // being read to is not safe for future use, as it may be in use on another | 79 // being read to is not safe for future use, as it may be in use on another |
(...skipping 10 matching lines...) Expand all Loading... |
90 GetElementReaders() const; | 90 GetElementReaders() const; |
91 | 91 |
92 protected: | 92 protected: |
93 // Must be called by subclasses when InitInternal and ReadInternal complete | 93 // Must be called by subclasses when InitInternal and ReadInternal complete |
94 // asynchronously. | 94 // asynchronously. |
95 void OnInitCompleted(int result); | 95 void OnInitCompleted(int result); |
96 void OnReadCompleted(int result); | 96 void OnReadCompleted(int result); |
97 | 97 |
98 // Must be called before InitInternal completes, for non-chunked uploads. | 98 // Must be called before InitInternal completes, for non-chunked uploads. |
99 // Must not be called for chunked uploads. | 99 // Must not be called for chunked uploads. |
100 void SetSize(uint64 size); | 100 void SetSize(uint64_t size); |
101 | 101 |
102 // Must be called for chunked uploads before the final ReadInternal call | 102 // Must be called for chunked uploads before the final ReadInternal call |
103 // completes. Must not be called for non-chunked uploads. | 103 // completes. Must not be called for non-chunked uploads. |
104 void SetIsFinalChunk(); | 104 void SetIsFinalChunk(); |
105 | 105 |
106 private: | 106 private: |
107 // See Init(). If it returns ERR_IO_PENDING, OnInitCompleted must be called | 107 // See Init(). If it returns ERR_IO_PENDING, OnInitCompleted must be called |
108 // once it completes. If the upload is not chunked, SetSize must be called | 108 // once it completes. If the upload is not chunked, SetSize must be called |
109 // before it completes. | 109 // before it completes. |
110 virtual int InitInternal() = 0; | 110 virtual int InitInternal() = 0; |
111 | 111 |
112 // See Read(). For chunked uploads, must call SetIsFinalChunk if this is the | 112 // See Read(). For chunked uploads, must call SetIsFinalChunk if this is the |
113 // final chunk. For non-chunked uploads, the UploadDataStream determins which | 113 // final chunk. For non-chunked uploads, the UploadDataStream determins which |
114 // read is the last based on size. Must read 1 or more bytes on every call, | 114 // read is the last based on size. Must read 1 or more bytes on every call, |
115 // though the final chunk may be 0 bytes, for chunked requests. If it returns | 115 // though the final chunk may be 0 bytes, for chunked requests. If it returns |
116 // ERR_IO_PENDING, OnInitCompleted must be called once it completes. Must not | 116 // ERR_IO_PENDING, OnInitCompleted must be called once it completes. Must not |
117 // return any error, other than ERR_IO_PENDING. | 117 // return any error, other than ERR_IO_PENDING. |
118 virtual int ReadInternal(IOBuffer* buf, int buf_len) = 0; | 118 virtual int ReadInternal(IOBuffer* buf, int buf_len) = 0; |
119 | 119 |
120 // Resets state and cancels any pending callbacks. Guaranteed to be called | 120 // Resets state and cancels any pending callbacks. Guaranteed to be called |
121 // before all but the first call to InitInternal. | 121 // before all but the first call to InitInternal. |
122 virtual void ResetInternal() = 0; | 122 virtual void ResetInternal() = 0; |
123 | 123 |
124 uint64 total_size_; | 124 uint64_t total_size_; |
125 uint64 current_position_; | 125 uint64_t current_position_; |
126 | 126 |
127 const int64 identifier_; | 127 const int64_t identifier_; |
128 | 128 |
129 const bool is_chunked_; | 129 const bool is_chunked_; |
130 | 130 |
131 // True if the initialization was successful. | 131 // True if the initialization was successful. |
132 bool initialized_successfully_; | 132 bool initialized_successfully_; |
133 | 133 |
134 bool is_eof_; | 134 bool is_eof_; |
135 | 135 |
136 CompletionCallback callback_; | 136 CompletionCallback callback_; |
137 | 137 |
138 DISALLOW_COPY_AND_ASSIGN(UploadDataStream); | 138 DISALLOW_COPY_AND_ASSIGN(UploadDataStream); |
139 }; | 139 }; |
140 | 140 |
141 } // namespace net | 141 } // namespace net |
142 | 142 |
143 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_ | 143 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_ |
OLD | NEW |