| 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/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "net/base/completion_callback.h" |
| 9 #include "net/base/net_export.h" | 10 #include "net/base/net_export.h" |
| 10 #include "net/base/upload_data.h" | 11 #include "net/base/upload_data.h" |
| 11 | 12 |
| 12 namespace net { | 13 namespace net { |
| 13 | 14 |
| 14 class FileStream; | 15 class FileStream; |
| 15 class IOBuffer; | 16 class IOBuffer; |
| 16 | 17 |
| 17 class NET_EXPORT UploadDataStream { | 18 class NET_EXPORT UploadDataStream { |
| 18 public: | 19 public: |
| 19 explicit UploadDataStream(UploadData* upload_data); | 20 explicit UploadDataStream(UploadData* upload_data); |
| 20 ~UploadDataStream(); | 21 ~UploadDataStream(); |
| 21 | 22 |
| 22 // Initializes the stream. This function must be called exactly once, | 23 // Initializes the stream synchronously or asynchronously depending on |
| 23 // before calling any other method. It is not valid to call any method | 24 // whether the upload data is in memory or not. This function must be called |
| 24 // (other than the destructor) if Init() returns a failure. | 25 // exactly once, before calling any other method. It is not valid to call any |
| 26 // method (other than the destructor) if the initialization failed. |
| 25 // | 27 // |
| 26 // Returns OK on success. Returns ERR_UPLOAD_FILE_CHANGED if the expected | 28 // If the upload data is in memory, the initialization is done synchronously. |
| 27 // file modification time is set (usually not set, but set for sliced | 29 // Returns OK or some other error code. |
| 28 // files) and the target file is changed. | 30 // |
| 29 int Init(); | 31 // If initialization requires file IO, returns ERR_IO_PENDING, and runs |
| 32 // |callback| with a result code on the original thread once the |
| 33 // initialization is done. |
| 34 // |
| 35 // ERR_UPLOAD_FILE_CHANGED is passed to |callback| if the expected file |
| 36 // modification time is set (usually not set, but set for sliced files) and |
| 37 // the target file is changed. |
| 38 int Init(const CompletionCallback& callback); |
| 39 |
| 40 // Similar to Init() but performs the operation always synchronously. This |
| 41 // version may perform file IO on the current thread. Usually used for |
| 42 // testing, but Chrome Frame also uses this version. |
| 43 int InitSync(); |
| 30 | 44 |
| 31 // Reads up to |buf_len| bytes from the upload data stream to |buf|. The | 45 // Reads up to |buf_len| bytes from the upload data stream to |buf|. The |
| 32 // number of bytes read is returned. Partial reads are allowed. Zero is | 46 // number of bytes read is returned. Partial reads are allowed. Zero is |
| 33 // returned on a call to Read when there are no remaining bytes in the | 47 // returned on a call to Read when there are no remaining bytes in the |
| 34 // stream, and IsEof() will return true hereafter. | 48 // stream, and IsEof() will return true hereafter. |
| 35 // | 49 // |
| 36 // If there's less data to read than we initially observed (i.e. the actual | 50 // If there's less data to read than we initially observed (i.e. the actual |
| 37 // upload data is smaller than size()), zeros are padded to ensure that | 51 // upload data is smaller than size()), zeros are padded to ensure that |
| 38 // size() bytes can be read, which can happen for TYPE_FILE payloads. | 52 // size() bytes can be read, which can happen for TYPE_FILE payloads. |
| 39 // | 53 // |
| (...skipping 27 matching lines...) Expand all Loading... |
| 67 private: | 81 private: |
| 68 friend class SpdyHttpStreamSpdy2Test; | 82 friend class SpdyHttpStreamSpdy2Test; |
| 69 friend class SpdyHttpStreamSpdy3Test; | 83 friend class SpdyHttpStreamSpdy3Test; |
| 70 friend class SpdyNetworkTransactionSpdy2Test; | 84 friend class SpdyNetworkTransactionSpdy2Test; |
| 71 friend class SpdyNetworkTransactionSpdy3Test; | 85 friend class SpdyNetworkTransactionSpdy3Test; |
| 72 | 86 |
| 73 // These methods are provided only to be used by unit tests. | 87 // These methods are provided only to be used by unit tests. |
| 74 static void ResetMergeChunks(); | 88 static void ResetMergeChunks(); |
| 75 static void set_merge_chunks(bool merge) { merge_chunks_ = merge; } | 89 static void set_merge_chunks(bool merge) { merge_chunks_ = merge; } |
| 76 | 90 |
| 91 // Called when InitInternal() is completed. Used to implement Init(). |
| 92 void OnInitComplete(const CompletionCallback& callback, |
| 93 uint64* content_length, |
| 94 int* result); |
| 95 |
| 77 scoped_refptr<UploadData> upload_data_; | 96 scoped_refptr<UploadData> upload_data_; |
| 78 | 97 |
| 79 // Index of the current upload element (i.e. the element currently being | 98 // Index of the current upload element (i.e. the element currently being |
| 80 // read). The index is used as a cursor to iterate over elements in | 99 // read). The index is used as a cursor to iterate over elements in |
| 81 // |upload_data_|. | 100 // |upload_data_|. |
| 82 size_t element_index_; | 101 size_t element_index_; |
| 83 | 102 |
| 84 // Size and current read position within the upload data stream. | 103 // Size and current read position within the upload data stream. |
| 85 uint64 total_size_; | 104 uint64 total_size_; |
| 86 uint64 current_position_; | 105 uint64 current_position_; |
| 87 | 106 |
| 88 // True if the initialization was successful. | 107 // True if the initialization was successful. |
| 89 bool initialized_successfully_; | 108 bool initialized_successfully_; |
| 90 | 109 |
| 91 // TODO(satish): Remove this once we have a better way to unit test POST | 110 // TODO(satish): Remove this once we have a better way to unit test POST |
| 92 // requests with chunked uploads. | 111 // requests with chunked uploads. |
| 93 static bool merge_chunks_; | 112 static bool merge_chunks_; |
| 94 | 113 |
| 114 base::WeakPtrFactory<UploadDataStream> weak_ptr_factory_; |
| 115 |
| 95 DISALLOW_COPY_AND_ASSIGN(UploadDataStream); | 116 DISALLOW_COPY_AND_ASSIGN(UploadDataStream); |
| 96 }; | 117 }; |
| 97 | 118 |
| 98 } // namespace net | 119 } // namespace net |
| 99 | 120 |
| 100 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_ | 121 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_ |
| OLD | NEW |