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