OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_CHUNKED_UPLOAD_DATA_STREAM_H_ | 5 #ifndef NET_BASE_CHUNKED_UPLOAD_DATA_STREAM_H_ |
6 #define NET_BASE_CHUNKED_UPLOAD_DATA_STREAM_H_ | 6 #define NET_BASE_CHUNKED_UPLOAD_DATA_STREAM_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "base/memory/weak_ptr.h" | |
15 #include "net/base/completion_callback.h" | 17 #include "net/base/completion_callback.h" |
16 #include "net/base/net_export.h" | 18 #include "net/base/net_export.h" |
17 #include "net/base/upload_data_stream.h" | 19 #include "net/base/upload_data_stream.h" |
18 | 20 |
19 namespace net { | 21 namespace net { |
20 | 22 |
21 class IOBuffer; | 23 class IOBuffer; |
22 | 24 |
23 // Class with a push-based interface for uploading data. Buffers all data until | 25 // Class with a push-based interface for uploading data. Buffers all data until |
24 // the request is completed. Not recommended for uploading large amounts of | 26 // the request is completed. Not recommended for uploading large amounts of |
25 // seekable data, due to this buffering behavior. | 27 // seekable data, due to this buffering behavior. |
26 class NET_EXPORT ChunkedUploadDataStream : public UploadDataStream { | 28 class NET_EXPORT ChunkedUploadDataStream : public UploadDataStream { |
27 public: | 29 public: |
30 // Utility class that allows writing data to a particular | |
31 // ChunkedUploadDataStream. It's needed because URLRequest owns the | |
32 // ChunkedUploadDataStream and manages its lifetime (And can delete it without | |
33 // warning, if failures are intercepted and then redirected), but higher level | |
34 // code is responsible for writing to the ChunkedUploadDataStream. | |
35 // | |
36 // The writer may only be used on the ChunkedUploadDataStream's thread. | |
37 class NET_EXPORT Writer { | |
38 public: | |
39 ~Writer(); | |
40 | |
41 // Adds data to the stream. |is_done| should be true if this is the last | |
42 // data to be appended. |data_len| must not be 0 unless |is_done| is true. | |
43 // Once called with |is_done| being true, must never be called again. | |
eroman
2016/03/22 19:05:37
This description of is_done is different than that
mmenke
2016/03/22 20:01:50
It's the same, word-for-word, as the comment befor
| |
44 // Returns true if write succeeded, false if it failed (Generally because | |
eroman
2016/03/22 19:05:37
Might be worth clarifying that true doesn't mean t
mmenke
2016/03/22 20:01:51
Done.
| |
45 // the underlying ChunkedUploadDataStream was destroyed). | |
46 bool AppendData(const char* data, int data_len, bool is_done); | |
eroman
2016/03/22 19:05:37
Rather than having this is_done boolean, have you
mmenke
2016/03/22 20:01:50
Yea, it's to mirror ChunkedUploadDataStream's meth
| |
47 | |
48 private: | |
49 friend class ChunkedUploadDataStream; | |
50 | |
51 explicit Writer(base::WeakPtr<ChunkedUploadDataStream> upload_data_stream); | |
52 | |
53 const base::WeakPtr<ChunkedUploadDataStream> upload_data_stream_; | |
54 | |
55 DISALLOW_COPY_AND_ASSIGN(Writer); | |
56 }; | |
57 | |
28 explicit ChunkedUploadDataStream(int64_t identifier); | 58 explicit ChunkedUploadDataStream(int64_t identifier); |
29 | 59 |
30 ~ChunkedUploadDataStream() override; | 60 ~ChunkedUploadDataStream() override; |
31 | 61 |
62 // Creates a Writer for appending data to |this|. | |
63 scoped_ptr<Writer> CreateWriter(); | |
eroman
2016/03/22 19:05:37
What is the effect of creating multiple writers? I
mmenke
2016/03/22 20:01:51
Added a comment (Allowing multiple writers, mostly
| |
64 | |
32 // Adds data to the stream. |is_done| should be true if this is the last | 65 // Adds data to the stream. |is_done| should be true if this is the last |
33 // data to be appended. |data_len| must not be 0 unless |is_done| is true. | 66 // data to be appended. |data_len| must not be 0 unless |is_done| is true. |
34 // Once called with |is_done| being true, must never be called again. | 67 // Once called with |is_done| being true, must never be called again. |
35 // TODO(mmenke): Consider using IOBuffers instead, to reduce data copies. | 68 // TODO(mmenke): Consider using IOBuffers instead, to reduce data copies. |
69 // TODO(mmenke): Consider making private, and having all consumers use | |
70 // Writers. | |
36 void AppendData(const char* data, int data_len, bool is_done); | 71 void AppendData(const char* data, int data_len, bool is_done); |
37 | 72 |
38 private: | 73 private: |
39 // UploadDataStream implementation. | 74 // UploadDataStream implementation. |
40 int InitInternal() override; | 75 int InitInternal() override; |
41 int ReadInternal(IOBuffer* buf, int buf_len) override; | 76 int ReadInternal(IOBuffer* buf, int buf_len) override; |
42 void ResetInternal() override; | 77 void ResetInternal() override; |
43 | 78 |
44 int ReadChunk(IOBuffer* buf, int buf_len); | 79 int ReadChunk(IOBuffer* buf, int buf_len); |
45 | 80 |
46 // Index and offset of next element of |upload_data_| to be read. | 81 // Index and offset of next element of |upload_data_| to be read. |
47 size_t read_index_; | 82 size_t read_index_; |
48 size_t read_offset_; | 83 size_t read_offset_; |
49 | 84 |
50 // True once all data has been appended to the stream. | 85 // True once all data has been appended to the stream. |
51 bool all_data_appended_; | 86 bool all_data_appended_; |
52 | 87 |
53 std::vector<scoped_ptr<std::vector<char>>> upload_data_; | 88 std::vector<scoped_ptr<std::vector<char>>> upload_data_; |
54 | 89 |
55 // Buffer to write the next read's data to. Only set when a call to | 90 // Buffer to write the next read's data to. Only set when a call to |
56 // ReadInternal reads no data. | 91 // ReadInternal reads no data. |
57 scoped_refptr<IOBuffer> read_buffer_; | 92 scoped_refptr<IOBuffer> read_buffer_; |
58 int read_buffer_len_; | 93 int read_buffer_len_; |
59 | 94 |
95 base::WeakPtrFactory<ChunkedUploadDataStream> weak_factory_; | |
eroman
2016/03/22 19:05:37
Generally use of weak-pointers feels like we faile
mmenke
2016/03/22 20:01:50
First off, let's discuss design constraints:
1) L
mmenke
2016/03/22 20:19:19
Ah, right...Keeping AppendData as a URLRequest met
| |
96 | |
60 DISALLOW_COPY_AND_ASSIGN(ChunkedUploadDataStream); | 97 DISALLOW_COPY_AND_ASSIGN(ChunkedUploadDataStream); |
61 }; | 98 }; |
62 | 99 |
63 } // namespace net | 100 } // namespace net |
64 | 101 |
65 #endif // NET_BASE_CHUNKED_UPLOAD_DATA_STREAM_H_ | 102 #endif // NET_BASE_CHUNKED_UPLOAD_DATA_STREAM_H_ |
OLD | NEW |