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

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: Add tests, InitSync() implementation is now optional, fixed Win build. 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
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 22 matching lines...) Expand all
63 bool IsEOF() const; 73 bool IsEOF() const;
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;
83 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsync);
84 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsyncFailureAsync);
85 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsyncFailureSync);
86
87 // Runs Init() for all element readers.
88 // This method is used to implement Init().
89 void InitInternal(const CompletionCallback& callback,
90 int start_index,
91 int previous_result);
92
93 // Finalizes the initialization process.
94 // This method is used to implement Init().
95 void FinalizeInitialization();
73 96
74 // These methods are provided only to be used by unit tests. 97 // These methods are provided only to be used by unit tests.
75 static void ResetMergeChunks(); 98 static void ResetMergeChunks();
76 static void set_merge_chunks(bool merge) { merge_chunks_ = merge; } 99 static void set_merge_chunks(bool merge) { merge_chunks_ = merge; }
77 100
78 scoped_refptr<UploadData> upload_data_; 101 scoped_refptr<UploadData> upload_data_;
79 ScopedVector<UploadElementReader> element_readers_; 102 ScopedVector<UploadElementReader> element_readers_;
80 103
81 // Index of the current upload element (i.e. the element currently being 104 // 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 105 // read). The index is used as a cursor to iterate over elements in
83 // |upload_data_|. 106 // |upload_data_|.
84 size_t element_index_; 107 size_t element_index_;
85 108
86 // Size and current read position within the upload data stream. 109 // Size and current read position within the upload data stream.
87 uint64 total_size_; 110 uint64 total_size_;
88 uint64 current_position_; 111 uint64 current_position_;
89 112
90 // True if the initialization was successful. 113 // True if the initialization was successful.
91 bool initialized_successfully_; 114 bool initialized_successfully_;
92 115
116 base::WeakPtrFactory<UploadDataStream> weak_ptr_factory_;
117
93 // TODO(satish): Remove this once we have a better way to unit test POST 118 // TODO(satish): Remove this once we have a better way to unit test POST
94 // requests with chunked uploads. 119 // requests with chunked uploads.
95 static bool merge_chunks_; 120 static bool merge_chunks_;
96 121
97 DISALLOW_COPY_AND_ASSIGN(UploadDataStream); 122 DISALLOW_COPY_AND_ASSIGN(UploadDataStream);
98 }; 123 };
99 124
100 } // namespace net 125 } // namespace net
101 126
102 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_ 127 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698