[email protected] | 6db833d1 | 2012-01-21 00:45:19 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
| 5 | #ifndef NET_BASE_UPLOAD_DATA_STREAM_H_ |
| 6 | #define NET_BASE_UPLOAD_DATA_STREAM_H_ |
| 7 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8 | #include "base/basictypes.h" |
| 9 | #include "base/macros.h" |
[email protected] | d9896165 | 2012-09-11 20:27:21 | [diff] [blame] | 10 | #include "base/memory/scoped_vector.h" |
[email protected] | df7adc6 | 2012-09-18 14:01:53 | [diff] [blame] | 11 | #include "net/base/completion_callback.h" |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 12 | #include "net/base/net_export.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 13 | |
| 14 | namespace net { |
| 15 | |
[email protected] | 5b76814 | 2012-10-17 10:15:17 | [diff] [blame] | 16 | class DrainableIOBuffer; |
[email protected] | 597cf6e | 2009-05-29 09:43:26 | [diff] [blame] | 17 | class IOBuffer; |
[email protected] | d9896165 | 2012-09-11 20:27:21 | [diff] [blame] | 18 | class UploadElementReader; |
[email protected] | 597cf6e | 2009-05-29 09:43:26 | [diff] [blame] | 19 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 20 | // A class for retrieving all data to be sent as a request body. Supports both |
| 21 | // chunked and non-chunked uploads. |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 22 | class NET_EXPORT UploadDataStream { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 23 | public: |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 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 |
| 26 | // sessions. A value of 0 is used to indicate an unspecified identifier. |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame^] | 27 | UploadDataStream(bool is_chunked, int64_t identifier); |
[email protected] | b2d26cfd | 2012-12-11 10:36:06 | [diff] [blame] | 28 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 29 | virtual ~UploadDataStream(); |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 30 | |
[email protected] | e5d47734 | 2012-11-02 15:18:46 | [diff] [blame] | 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 |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 33 | // destructor) if Init() fails. This method can be called multiple times. |
| 34 | // Calling this method after an Init() success results in resetting the |
| 35 | // state (i.e. the stream is rewound). |
[email protected] | 49ed6cc | 2012-02-02 08:59:16 | [diff] [blame] | 36 | // |
[email protected] | df7adc6 | 2012-09-18 14:01:53 | [diff] [blame] | 37 | // Does the initialization synchronously and returns the result if possible, |
| 38 | // otherwise returns ERR_IO_PENDING and runs the callback with the result. |
| 39 | // |
[email protected] | 49ed6cc | 2012-02-02 08:59:16 | [diff] [blame] | 40 | // Returns OK on success. Returns ERR_UPLOAD_FILE_CHANGED if the expected |
| 41 | // file modification time is set (usually not set, but set for sliced |
| 42 | // files) and the target file is changed. |
[email protected] | df7adc6 | 2012-09-18 14:01:53 | [diff] [blame] | 43 | int Init(const CompletionCallback& callback); |
| 44 | |
[email protected] | 5b76814 | 2012-10-17 10:15:17 | [diff] [blame] | 45 | // When possible, reads up to |buf_len| bytes synchronously from the upload |
| 46 | // data stream to |buf| and returns the number of bytes read; otherwise, |
| 47 | // returns ERR_IO_PENDING and calls |callback| with the number of bytes read. |
| 48 | // Partial reads are allowed. Zero is returned on a call to Read when there |
| 49 | // are no remaining bytes in the stream, and IsEof() will return true |
| 50 | // hereafter. |
[email protected] | 6db833d1 | 2012-01-21 00:45:19 | [diff] [blame] | 51 | // |
[email protected] | 0ab2e24 | 2012-02-08 06:07:00 | [diff] [blame] | 52 | // If there's less data to read than we initially observed (i.e. the actual |
| 53 | // upload data is smaller than size()), zeros are padded to ensure that |
| 54 | // size() bytes can be read, which can happen for TYPE_FILE payloads. |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 55 | // |
| 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 |
| 58 | // value >= 0. |
| 59 | // TODO(mmenke): Investigate letting reads fail. |
[email protected] | 5b76814 | 2012-10-17 10:15:17 | [diff] [blame] | 60 | int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
| 61 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 62 | // Returns the total size of the data stream and the current position. |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 63 | // When the data is chunked, always returns zero. Must always return the same |
| 64 | // value after each call to Initialize(). |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame^] | 65 | uint64_t size() const { return total_size_; } |
| 66 | uint64_t position() const { return current_position_; } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 67 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 68 | // See constructor for description. |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame^] | 69 | int64_t identifier() const { return identifier_; } |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 70 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 71 | bool is_chunked() const { return is_chunked_; } |
[email protected] | 0736d9e | 2012-11-28 19:50:40 | [diff] [blame] | 72 | |
[email protected] | 0ab2e24 | 2012-02-08 06:07:00 | [diff] [blame] | 73 | // Returns true if all data has been consumed from this upload data |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 74 | // stream. For chunked uploads, returns false until the first read attempt. |
| 75 | // This makes some state machines a little simpler. |
[email protected] | 0ab2e24 | 2012-02-08 06:07:00 | [diff] [blame] | 76 | bool IsEOF() const; |
[email protected] | 0c9bf87 | 2011-03-04 17:53:22 | [diff] [blame] | 77 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 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 |
| 80 | // thread. |
[email protected] | e5d47734 | 2012-11-02 15:18:46 | [diff] [blame] | 81 | void Reset(); |
| 82 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 83 | // Returns true if the upload data in the stream is entirely in memory, and |
| 84 | // all read requests will succeed synchronously. Expected to return false for |
| 85 | // chunked requests. |
| 86 | virtual bool IsInMemory() const; |
| 87 | |
| 88 | // Returns a list of element readers owned by |this|, if it has any. |
| 89 | virtual const ScopedVector<UploadElementReader>* |
| 90 | GetElementReaders() const; |
| 91 | |
| 92 | protected: |
| 93 | // Must be called by subclasses when InitInternal and ReadInternal complete |
| 94 | // asynchronously. |
| 95 | void OnInitCompleted(int result); |
| 96 | void OnReadCompleted(int result); |
| 97 | |
| 98 | // Must be called before InitInternal completes, for non-chunked uploads. |
| 99 | // Must not be called for chunked uploads. |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame^] | 100 | void SetSize(uint64_t size); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 101 | |
| 102 | // Must be called for chunked uploads before the final ReadInternal call |
| 103 | // completes. Must not be called for non-chunked uploads. |
| 104 | void SetIsFinalChunk(); |
| 105 | |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 106 | private: |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 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 |
| 109 | // before it completes. |
| 110 | virtual int InitInternal() = 0; |
[email protected] | e5d47734 | 2012-11-02 15:18:46 | [diff] [blame] | 111 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 112 | // See Read(). For chunked uploads, must call SetIsFinalChunk if this is the |
| 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, |
| 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 |
| 117 | // return any error, other than ERR_IO_PENDING. |
| 118 | virtual int ReadInternal(IOBuffer* buf, int buf_len) = 0; |
[email protected] | df7adc6 | 2012-09-18 14:01:53 | [diff] [blame] | 119 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 120 | // Resets state and cancels any pending callbacks. Guaranteed to be called |
| 121 | // before all but the first call to InitInternal. |
| 122 | virtual void ResetInternal() = 0; |
[email protected] | 9fa94db | 2012-10-22 03:34:07 | [diff] [blame] | 123 | |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame^] | 124 | uint64_t total_size_; |
| 125 | uint64_t current_position_; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 126 | |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame^] | 127 | const int64_t identifier_; |
[email protected] | b2d26cfd | 2012-12-11 10:36:06 | [diff] [blame] | 128 | |
| 129 | const bool is_chunked_; |
[email protected] | fac16e2 | 2012-12-29 18:46:31 | [diff] [blame] | 130 | |
[email protected] | 49ed6cc | 2012-02-02 08:59:16 | [diff] [blame] | 131 | // True if the initialization was successful. |
| 132 | bool initialized_successfully_; |
| 133 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 134 | bool is_eof_; |
[email protected] | 9fa94db | 2012-10-22 03:34:07 | [diff] [blame] | 135 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 136 | CompletionCallback callback_; |
[email protected] | df7adc6 | 2012-09-18 14:01:53 | [diff] [blame] | 137 | |
[email protected] | 73a797fb | 2010-06-07 02:10:18 | [diff] [blame] | 138 | DISALLOW_COPY_AND_ASSIGN(UploadDataStream); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | } // namespace net |
| 142 | |
| 143 | #endif // NET_BASE_UPLOAD_DATA_STREAM_H_ |