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

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: rebase 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
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 IOBuffer; 18 class IOBuffer;
19 class UploadElementReader; 19 class UploadElementReader;
20 20
21 class NET_EXPORT UploadDataStream { 21 class NET_EXPORT UploadDataStream {
mmenke 2012/10/11 18:05:40 While you're here, could you add a description of
hashimoto 2012/10/12 07:36:56 Added a comment about this class. Or, should I als
22 public: 22 public:
23 explicit UploadDataStream(UploadData* upload_data); 23 explicit UploadDataStream(UploadData* upload_data);
24 ~UploadDataStream(); 24 ~UploadDataStream();
25 25
26 // Initializes the stream. This function must be called exactly once, 26 // Initializes the stream. This function must be called exactly once,
27 // before calling any other method. It is not valid to call any method 27 // before calling any other method. It is not valid to call any method
28 // (other than the destructor) if Init() returns a failure. 28 // (other than the destructor) if Init() returns a failure.
29 // 29 //
30 // Does the initialization synchronously and returns the result if possible, 30 // Does the initialization synchronously and returns the result if possible,
31 // otherwise returns ERR_IO_PENDING and runs the callback with the result. 31 // otherwise returns ERR_IO_PENDING and runs the callback with the result.
32 // 32 //
33 // Returns OK on success. Returns ERR_UPLOAD_FILE_CHANGED if the expected 33 // 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 34 // file modification time is set (usually not set, but set for sliced
35 // files) and the target file is changed. 35 // files) and the target file is changed.
36 int Init(const CompletionCallback& callback); 36 int Init(const CompletionCallback& callback);
37 37
38 // Initializes the stream synchronously. 38 // Initializes the stream synchronously.
39 // Use this method only in tests and Chrome Frame. 39 // Use this method only in tests and Chrome Frame.
mmenke 2012/10/11 18:05:40 While you're here, mentioning Chrome frame in net/
hashimoto 2012/10/12 07:36:56 Stopped mentioning Chrome Frame.
40 int InitSync(); 40 int InitSync();
41 41
42 // Reads up to |buf_len| bytes from the upload data stream to |buf|. The 42 // Reads up to |buf_len| bytes synchronously from the upload data stream to
43 // number of bytes read is returned. Partial reads are allowed. Zero is 43 // |buf| and returns the number of bytes read when possible, otherwise,
44 // returned on a call to Read when there are no remaining bytes in the 44 // returns ERR_IO_PENDING and calls |callback| with the result. Partial reads
45 // stream, and IsEof() will return true hereafter. 45 // are allowed. Zero is returned on a call to Read when there are no
46 // remaining bytes in the stream, and IsEof() will return true hereafter.
46 // 47 //
47 // If there's less data to read than we initially observed (i.e. the actual 48 // 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 49 // 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. 50 // size() bytes can be read, which can happen for TYPE_FILE payloads.
50 // 51 //
51 // If the upload data stream is chunked (i.e. is_chunked() is true), 52 // If the upload data stream is chunked (i.e. is_chunked() is true),
52 // ERR_IO_PENDING is returned to indicate there is nothing to read at the 53 // ERR_IO_PENDING is returned to indicate there is nothing to read at the
53 // moment, but more data to come at a later time. If not chunked, reads 54 // moment, but more data to come at a later time.
mmenke 2012/10/11 18:05:40 This comment isn't accurate: HttpStreamParser all
hashimoto 2012/10/12 07:36:56 Revised the comment.
54 // won't fail. 55 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
55 int Read(IOBuffer* buf, int buf_len); 56
57 // Reads data always synchronously.
58 // Use this method only in tests and Chrome Frame.
mmenke 2012/10/11 18:05:40 Mentioning Chrome Frame here is a layering violati
hashimoto 2012/10/12 07:36:56 Done.
59 int ReadSync(IOBuffer* buf, int buf_len);
56 60
57 // Sets the callback to be invoked when new chunks are available to upload. 61 // Sets the callback to be invoked when new chunks are available to upload.
58 void set_chunk_callback(ChunkCallback* callback) { 62 void set_chunk_callback(ChunkCallback* callback) {
59 upload_data_->set_chunk_callback(callback); 63 upload_data_->set_chunk_callback(callback);
60 } 64 }
61 65
62 // Returns the total size of the data stream and the current position. 66 // 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 67 // 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, 68 // because it is possible for the stream to end before its size is reached,
65 // for example, if the file is truncated. 69 // for example, if the file is truncated.
(...skipping 13 matching lines...) Expand all
79 friend class SpdyHttpStreamSpdy2Test; 83 friend class SpdyHttpStreamSpdy2Test;
80 friend class SpdyHttpStreamSpdy3Test; 84 friend class SpdyHttpStreamSpdy3Test;
81 friend class SpdyNetworkTransactionSpdy2Test; 85 friend class SpdyNetworkTransactionSpdy2Test;
82 friend class SpdyNetworkTransactionSpdy3Test; 86 friend class SpdyNetworkTransactionSpdy3Test;
83 87
84 // TODO(hashimoto): Stop directly accsssing element_readers_ from tests and 88 // TODO(hashimoto): Stop directly accsssing element_readers_ from tests and
85 // remove these friend declarations. 89 // remove these friend declarations.
86 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsync); 90 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsync);
87 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsyncFailureAsync); 91 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsyncFailureAsync);
88 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsyncFailureSync); 92 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsyncFailureSync);
93 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, ReadAsync);
89 94
90 // Runs Init() for all element readers. 95 // Runs Init() for all element readers.
91 // This method is used to implement Init(). 96 // This method is used to implement Init().
92 void InitInternal(int start_index, 97 void InitInternal(int start_index,
93 const CompletionCallback& callback, 98 const CompletionCallback& callback,
94 int previous_result); 99 int previous_result);
95 100
96 // Finalizes the initialization process. 101 // Finalizes the initialization process.
97 // This method is used to implement Init(). 102 // This method is used to implement Init().
98 void FinalizeInitialization(); 103 void FinalizeInitialization();
99 104
105 // Reads data from the element readers.
106 // This method is used to implement Read().
107 int ReadInternal(scoped_refptr<IOBuffer> buf,
108 int buf_len,
109 int bytes_copied,
110 bool invoked_asynchronously,
111 const CompletionCallback& callback,
112 int previous_result);
113
100 // These methods are provided only to be used by unit tests. 114 // These methods are provided only to be used by unit tests.
101 static void ResetMergeChunks(); 115 static void ResetMergeChunks();
102 static void set_merge_chunks(bool merge) { merge_chunks_ = merge; } 116 static void set_merge_chunks(bool merge) { merge_chunks_ = merge; }
103 117
104 scoped_refptr<UploadData> upload_data_; 118 scoped_refptr<UploadData> upload_data_;
105 ScopedVector<UploadElementReader> element_readers_; 119 ScopedVector<UploadElementReader> element_readers_;
106 120
107 // Index of the current upload element (i.e. the element currently being 121 // 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 122 // read). The index is used as a cursor to iterate over elements in
109 // |upload_data_|. 123 // |upload_data_|.
(...skipping 11 matching lines...) Expand all
121 // TODO(satish): Remove this once we have a better way to unit test POST 135 // TODO(satish): Remove this once we have a better way to unit test POST
122 // requests with chunked uploads. 136 // requests with chunked uploads.
123 static bool merge_chunks_; 137 static bool merge_chunks_;
124 138
125 DISALLOW_COPY_AND_ASSIGN(UploadDataStream); 139 DISALLOW_COPY_AND_ASSIGN(UploadDataStream);
126 }; 140 };
127 141
128 } // namespace net 142 } // namespace net
129 143
130 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_ 144 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698