Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(551)

Side by Side Diff: net/base/upload_data_stream.h

Issue 10910268: net: Make UploadDataStream::Read() asynchronous (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/base/upload_data.h ('k') | net/base/upload_data_stream.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/gtest_prod_util.h" 8 #include "base/gtest_prod_util.h"
9 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_vector.h" 10 #include "base/memory/scoped_vector.h"
11 #include "base/memory/weak_ptr.h" 11 #include "base/memory/weak_ptr.h"
12 #include "net/base/completion_callback.h" 12 #include "net/base/completion_callback.h"
13 #include "net/base/net_export.h" 13 #include "net/base/net_export.h"
14 #include "net/base/upload_data.h" 14 #include "net/base/upload_data.h"
15 15
16 namespace net { 16 namespace net {
17 17
18 class DrainableIOBuffer;
18 class IOBuffer; 19 class IOBuffer;
19 class UploadElementReader; 20 class UploadElementReader;
20 21
22 // A class to read all elements from an UploadData object.
21 class NET_EXPORT UploadDataStream { 23 class NET_EXPORT UploadDataStream {
22 public: 24 public:
23 explicit UploadDataStream(UploadData* upload_data); 25 explicit UploadDataStream(UploadData* upload_data);
24 ~UploadDataStream(); 26 ~UploadDataStream();
25 27
26 // Initializes the stream. This function must be called exactly once, 28 // Initializes the stream. This function must be called exactly once,
27 // before calling any other method. It is not valid to call any method 29 // before calling any other method. It is not valid to call any method
28 // (other than the destructor) if Init() returns a failure. 30 // (other than the destructor) if Init() returns a failure.
29 // 31 //
30 // Does the initialization synchronously and returns the result if possible, 32 // Does the initialization synchronously and returns the result if possible,
31 // otherwise returns ERR_IO_PENDING and runs the callback with the result. 33 // otherwise returns ERR_IO_PENDING and runs the callback with the result.
32 // 34 //
33 // Returns OK on success. Returns ERR_UPLOAD_FILE_CHANGED if the expected 35 // Returns OK on success. Returns ERR_UPLOAD_FILE_CHANGED if the expected
34 // file modification time is set (usually not set, but set for sliced 36 // file modification time is set (usually not set, but set for sliced
35 // files) and the target file is changed. 37 // files) and the target file is changed.
36 int Init(const CompletionCallback& callback); 38 int Init(const CompletionCallback& callback);
37 39
38 // Initializes the stream synchronously. 40 // Initializes the stream synchronously.
39 // Use this method only in tests and Chrome Frame. 41 // Use this method only if the thread is IO allowed or the data is in-memory.
40 int InitSync(); 42 int InitSync();
41 43
42 // Reads up to |buf_len| bytes from the upload data stream to |buf|. The 44 // When possible, reads up to |buf_len| bytes synchronously from the upload
43 // number of bytes read is returned. Partial reads are allowed. Zero is 45 // data stream to |buf| and returns the number of bytes read; otherwise,
44 // returned on a call to Read when there are no remaining bytes in the 46 // returns ERR_IO_PENDING and calls |callback| with the number of bytes read.
45 // stream, and IsEof() will return true hereafter. 47 // Partial reads are allowed. Zero is returned on a call to Read when there
48 // are no remaining bytes in the stream, and IsEof() will return true
49 // hereafter.
46 // 50 //
47 // If there's less data to read than we initially observed (i.e. the actual 51 // If there's less data to read than we initially observed (i.e. the actual
48 // upload data is smaller than size()), zeros are padded to ensure that 52 // upload data is smaller than size()), zeros are padded to ensure that
49 // size() bytes can be read, which can happen for TYPE_FILE payloads. 53 // size() bytes can be read, which can happen for TYPE_FILE payloads.
50 // 54 //
51 // If the upload data stream is chunked (i.e. is_chunked() is true), 55 // If the data is chunked (i.e. is_chunked() is true) and there is not enough
52 // ERR_IO_PENDING is returned to indicate there is nothing to read at the 56 // data ready to be uploaded, ERR_IO_PENDING is returned and the callback set
53 // moment, but more data to come at a later time. If not chunked, reads 57 // by set_chunk_callback is called later when data gets available.
54 // won't fail. 58 // When data is available, the data is read synchronously and the number of
55 int Read(IOBuffer* buf, int buf_len); 59 // bytes read is returned.
60 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
61
62 // Reads data always synchronously.
63 // Use this method only if the thread is IO allowed or the data is in-memory.
64 int ReadSync(IOBuffer* buf, int buf_len);
56 65
57 // Sets the callback to be invoked when new chunks are available to upload. 66 // Sets the callback to be invoked when new chunks are available to upload.
58 void set_chunk_callback(ChunkCallback* callback) { 67 void set_chunk_callback(ChunkCallback* callback) {
59 upload_data_->set_chunk_callback(callback); 68 upload_data_->set_chunk_callback(callback);
60 } 69 }
61 70
62 // Returns the total size of the data stream and the current position. 71 // Returns the total size of the data stream and the current position.
63 // size() is not to be used to determine whether the stream has ended 72 // size() is not to be used to determine whether the stream has ended
64 // because it is possible for the stream to end before its size is reached, 73 // because it is possible for the stream to end before its size is reached,
65 // for example, if the file is truncated. 74 // for example, if the file is truncated. When the data is chunked, size()
75 // always returns zero.
66 uint64 size() const { return total_size_; } 76 uint64 size() const { return total_size_; }
67 uint64 position() const { return current_position_; } 77 uint64 position() const { return current_position_; }
68 78
69 bool is_chunked() const { return upload_data_->is_chunked(); } 79 bool is_chunked() const { return upload_data_->is_chunked(); }
70 80
71 // Returns true if all data has been consumed from this upload data 81 // Returns true if all data has been consumed from this upload data
72 // stream. 82 // stream.
73 bool IsEOF() const; 83 bool IsEOF() const;
74 84
75 // Returns true if the upload data in the stream is entirely in memory. 85 // Returns true if the upload data in the stream is entirely in memory.
76 bool IsInMemory() const; 86 bool IsInMemory() const;
77 87
78 private: 88 private:
79 friend class SpdyHttpStreamSpdy2Test; 89 friend class SpdyHttpStreamSpdy2Test;
80 friend class SpdyHttpStreamSpdy3Test; 90 friend class SpdyHttpStreamSpdy3Test;
81 friend class SpdyNetworkTransactionSpdy2Test; 91 friend class SpdyNetworkTransactionSpdy2Test;
82 friend class SpdyNetworkTransactionSpdy3Test; 92 friend class SpdyNetworkTransactionSpdy3Test;
83 93
84 // TODO(hashimoto): Stop directly accsssing element_readers_ from tests and 94 // TODO(hashimoto): Stop directly accsssing element_readers_ from tests and
85 // remove these friend declarations. 95 // remove these friend declarations.
86 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsync); 96 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsync);
87 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsyncFailureAsync); 97 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsyncFailureAsync);
88 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsyncFailureSync); 98 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsyncFailureSync);
99 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, ReadAsync);
89 100
90 // Runs Init() for all element readers. 101 // Runs Init() for all element readers.
91 // This method is used to implement Init(). 102 // This method is used to implement Init().
92 void InitInternal(int start_index, 103 void InitInternal(int start_index,
93 const CompletionCallback& callback, 104 const CompletionCallback& callback,
94 int previous_result); 105 int previous_result);
95 106
96 // Finalizes the initialization process. 107 // Finalizes the initialization process.
97 // This method is used to implement Init(). 108 // This method is used to implement Init().
98 void FinalizeInitialization(); 109 void FinalizeInitialization();
99 110
111 // Reads data from the element readers.
112 // This method is used to implement Read().
113 int ReadInternal(scoped_refptr<DrainableIOBuffer> buf,
114 bool invoked_asynchronously,
115 const CompletionCallback& callback,
116 int previous_result);
117
100 // These methods are provided only to be used by unit tests. 118 // These methods are provided only to be used by unit tests.
101 static void ResetMergeChunks(); 119 static void ResetMergeChunks();
102 static void set_merge_chunks(bool merge) { merge_chunks_ = merge; } 120 static void set_merge_chunks(bool merge) { merge_chunks_ = merge; }
103 121
104 scoped_refptr<UploadData> upload_data_; 122 scoped_refptr<UploadData> upload_data_;
105 ScopedVector<UploadElementReader> element_readers_; 123 ScopedVector<UploadElementReader> element_readers_;
106 124
107 // Index of the current upload element (i.e. the element currently being 125 // Index of the current upload element (i.e. the element currently being
108 // read). The index is used as a cursor to iterate over elements in 126 // read). The index is used as a cursor to iterate over elements in
109 // |upload_data_|. 127 // |upload_data_|.
110 size_t element_index_; 128 size_t element_index_;
111 129
112 // Size and current read position within the upload data stream. 130 // Size and current read position within the upload data stream.
131 // |total_size_| is set to zero when the data is chunked.
113 uint64 total_size_; 132 uint64 total_size_;
114 uint64 current_position_; 133 uint64 current_position_;
115 134
116 // True if the initialization was successful. 135 // True if the initialization was successful.
117 bool initialized_successfully_; 136 bool initialized_successfully_;
118 137
119 base::WeakPtrFactory<UploadDataStream> weak_ptr_factory_; 138 base::WeakPtrFactory<UploadDataStream> weak_ptr_factory_;
120 139
121 // TODO(satish): Remove this once we have a better way to unit test POST 140 // TODO(satish): Remove this once we have a better way to unit test POST
122 // requests with chunked uploads. 141 // requests with chunked uploads.
123 static bool merge_chunks_; 142 static bool merge_chunks_;
124 143
125 DISALLOW_COPY_AND_ASSIGN(UploadDataStream); 144 DISALLOW_COPY_AND_ASSIGN(UploadDataStream);
126 }; 145 };
127 146
128 } // namespace net 147 } // namespace net
129 148
130 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_ 149 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_
OLDNEW
« no previous file with comments | « net/base/upload_data.h ('k') | net/base/upload_data_stream.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698