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

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

Issue 10913179: net: Make UploadDataStream::Init() asynchronous. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review fix Created 8 years, 3 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_bytes_element_reader.cc ('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/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_vector.h" 10 #include "base/memory/scoped_vector.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 IOBuffer; 18 class IOBuffer;
16 class UploadElementReader; 19 class UploadElementReader;
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. This function must be called exactly once,
24 // 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
25 // (other than the destructor) if Init() returns a failure. 28 // (other than the destructor) if Init() returns a failure.
26 // 29 //
30 // Does the initialization synchronously and returns the result if possible,
31 // otherwise returns ERR_IO_PENDING and runs the callback with the result.
32 //
27 // 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
28 // 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
29 // files) and the target file is changed. 35 // files) and the target file is changed.
30 int Init(); 36 int Init(const CompletionCallback& callback);
37
38 // Initializes the stream synchronously.
39 // Use this method only in tests and Chrome Frame.
40 int InitSync();
31 41
32 // Reads up to |buf_len| bytes from the upload data stream to |buf|. The 42 // 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 43 // 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 44 // returned on a call to Read when there are no remaining bytes in the
35 // stream, and IsEof() will return true hereafter. 45 // stream, and IsEof() will return true hereafter.
36 // 46 //
37 // If there's less data to read than we initially observed (i.e. the actual 47 // 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 48 // 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. 49 // size() bytes can be read, which can happen for TYPE_FILE payloads.
40 // 50 //
(...skipping 23 matching lines...) Expand all
64 74
65 // Returns true if the upload data in the stream is entirely in memory. 75 // Returns true if the upload data in the stream is entirely in memory.
66 bool IsInMemory() const; 76 bool IsInMemory() const;
67 77
68 private: 78 private:
69 friend class SpdyHttpStreamSpdy2Test; 79 friend class SpdyHttpStreamSpdy2Test;
70 friend class SpdyHttpStreamSpdy3Test; 80 friend class SpdyHttpStreamSpdy3Test;
71 friend class SpdyNetworkTransactionSpdy2Test; 81 friend class SpdyNetworkTransactionSpdy2Test;
72 friend class SpdyNetworkTransactionSpdy3Test; 82 friend class SpdyNetworkTransactionSpdy3Test;
73 83
84 // TODO(hashimoto): Stop directly accsssing element_readers_ from tests and
85 // remove these friend declarations.
86 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsync);
87 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsyncFailureAsync);
88 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsyncFailureSync);
89
90 // Runs Init() for all element readers.
91 // This method is used to implement Init().
92 void InitInternal(const CompletionCallback& callback,
willchan no longer on Chromium 2012/09/17 17:34:43 nit: it's more common in net/ code to have the cal
hashimoto 2012/09/18 11:07:33 |callback| cannot be the last parameter since |pre
93 int start_index,
94 int previous_result);
95
96 // Finalizes the initialization process.
97 // This method is used to implement Init().
98 void FinalizeInitialization();
99
74 // These methods are provided only to be used by unit tests. 100 // These methods are provided only to be used by unit tests.
75 static void ResetMergeChunks(); 101 static void ResetMergeChunks();
76 static void set_merge_chunks(bool merge) { merge_chunks_ = merge; } 102 static void set_merge_chunks(bool merge) { merge_chunks_ = merge; }
77 103
78 scoped_refptr<UploadData> upload_data_; 104 scoped_refptr<UploadData> upload_data_;
79 ScopedVector<UploadElementReader> element_readers_; 105 ScopedVector<UploadElementReader> element_readers_;
80 106
81 // Index of the current upload element (i.e. the element currently being 107 // Index of the current upload element (i.e. the element currently being
82 // read). The index is used as a cursor to iterate over elements in 108 // read). The index is used as a cursor to iterate over elements in
83 // |upload_data_|. 109 // |upload_data_|.
84 size_t element_index_; 110 size_t element_index_;
85 111
86 // Size and current read position within the upload data stream. 112 // Size and current read position within the upload data stream.
87 uint64 total_size_; 113 uint64 total_size_;
88 uint64 current_position_; 114 uint64 current_position_;
89 115
90 // True if the initialization was successful. 116 // True if the initialization was successful.
91 bool initialized_successfully_; 117 bool initialized_successfully_;
92 118
119 base::WeakPtrFactory<UploadDataStream> weak_ptr_factory_;
120
93 // TODO(satish): Remove this once we have a better way to unit test POST 121 // TODO(satish): Remove this once we have a better way to unit test POST
94 // requests with chunked uploads. 122 // requests with chunked uploads.
95 static bool merge_chunks_; 123 static bool merge_chunks_;
96 124
97 DISALLOW_COPY_AND_ASSIGN(UploadDataStream); 125 DISALLOW_COPY_AND_ASSIGN(UploadDataStream);
98 }; 126 };
99 127
100 } // namespace net 128 } // namespace net
101 129
102 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_ 130 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_
OLDNEW
« no previous file with comments | « net/base/upload_bytes_element_reader.cc ('k') | net/base/upload_data_stream.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698