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

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: _ 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/memory/ref_counted.h" 8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_vector.h" 9 #include "base/memory/scoped_vector.h"
10 #include "base/memory/weak_ptr.h"
11 #include "net/base/completion_callback.h"
10 #include "net/base/net_export.h" 12 #include "net/base/net_export.h"
11 #include "net/base/upload_data.h" 13 #include "net/base/upload_data.h"
12 14
13 namespace net { 15 namespace net {
14 16
15 class IOBuffer; 17 class IOBuffer;
16 class UploadElementReader; 18 class UploadElementReader;
17 19
18 class NET_EXPORT UploadDataStream { 20 class NET_EXPORT UploadDataStream {
19 public: 21 public:
20 explicit UploadDataStream(UploadData* upload_data); 22 explicit UploadDataStream(UploadData* upload_data);
21 ~UploadDataStream(); 23 ~UploadDataStream();
22 24
23 // Initializes the stream. This function must be called exactly once, 25 // Initializes the stream. This function must be called exactly once,
24 // before calling any other method. It is not valid to call any method 26 // before calling any other method. It is not valid to call any method
25 // (other than the destructor) if Init() returns a failure. 27 // (other than the destructor) if Init() returns a failure.
26 // 28 //
29 // Does the initialization synchronously and returns the result if possible,
30 // otherwise returns ERR_IO_PENDING and runs the callback with the result.
31 //
27 // Returns OK on success. Returns ERR_UPLOAD_FILE_CHANGED if the expected 32 // 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 33 // file modification time is set (usually not set, but set for sliced
29 // files) and the target file is changed. 34 // files) and the target file is changed.
30 int Init(); 35 int Init(const CompletionCallback& callback);
36
37 // Initializes the stream synchronously.
38 // Use this method only in tests and Chrome Frame.
39 int InitSync();
31 40
32 // Reads up to |buf_len| bytes from the upload data stream to |buf|. The 41 // 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 42 // 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 43 // returned on a call to Read when there are no remaining bytes in the
35 // stream, and IsEof() will return true hereafter. 44 // stream, and IsEof() will return true hereafter.
36 // 45 //
37 // If there's less data to read than we initially observed (i.e. the actual 46 // 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 47 // 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. 48 // size() bytes can be read, which can happen for TYPE_FILE payloads.
40 // 49 //
(...skipping 23 matching lines...) Expand all
64 73
65 // Returns true if the upload data in the stream is entirely in memory. 74 // Returns true if the upload data in the stream is entirely in memory.
66 bool IsInMemory() const; 75 bool IsInMemory() const;
67 76
68 private: 77 private:
69 friend class SpdyHttpStreamSpdy2Test; 78 friend class SpdyHttpStreamSpdy2Test;
70 friend class SpdyHttpStreamSpdy3Test; 79 friend class SpdyHttpStreamSpdy3Test;
71 friend class SpdyNetworkTransactionSpdy2Test; 80 friend class SpdyNetworkTransactionSpdy2Test;
72 friend class SpdyNetworkTransactionSpdy3Test; 81 friend class SpdyNetworkTransactionSpdy3Test;
73 82
83 // Runs Init() for all element readers.
84 // This method is used to implement Init().
85 void InitInternal(const CompletionCallback& callback,
86 int index,
87 int last_result);
88
89 // Finalizes the initialization process.
90 // This method is used to implement Init().
91 void FinalizeInitialization();
92
74 // These methods are provided only to be used by unit tests. 93 // These methods are provided only to be used by unit tests.
75 static void ResetMergeChunks(); 94 static void ResetMergeChunks();
76 static void set_merge_chunks(bool merge) { merge_chunks_ = merge; } 95 static void set_merge_chunks(bool merge) { merge_chunks_ = merge; }
77 96
78 scoped_refptr<UploadData> upload_data_; 97 scoped_refptr<UploadData> upload_data_;
79 ScopedVector<UploadElementReader> element_readers_; 98 ScopedVector<UploadElementReader> element_readers_;
80 99
81 // Index of the current upload element (i.e. the element currently being 100 // 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 101 // read). The index is used as a cursor to iterate over elements in
83 // |upload_data_|. 102 // |upload_data_|.
84 size_t element_index_; 103 size_t element_index_;
85 104
86 // Size and current read position within the upload data stream. 105 // Size and current read position within the upload data stream.
87 uint64 total_size_; 106 uint64 total_size_;
88 uint64 current_position_; 107 uint64 current_position_;
89 108
90 // True if the initialization was successful. 109 // True if the initialization was successful.
91 bool initialized_successfully_; 110 bool initialized_successfully_;
92 111
112 base::WeakPtrFactory<UploadDataStream> weak_ptr_factory_;
113
93 // TODO(satish): Remove this once we have a better way to unit test POST 114 // TODO(satish): Remove this once we have a better way to unit test POST
94 // requests with chunked uploads. 115 // requests with chunked uploads.
95 static bool merge_chunks_; 116 static bool merge_chunks_;
96 117
97 DISALLOW_COPY_AND_ASSIGN(UploadDataStream); 118 DISALLOW_COPY_AND_ASSIGN(UploadDataStream);
98 }; 119 };
99 120
100 } // namespace net 121 } // namespace net
101 122
102 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_ 123 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698