Chromium Code Reviews| 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 | |
|
eroman
2016/03/26 00:55:16
Before jumping into the "why" it's needed, I sugge
mmenke
2016/03/28 17:33:11
Done.
| |
| 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. | |
| 44 // Returns true if write was passed successfully on to the next layer, | |
| 45 // though the data may not actually have been written to the underlying | |
| 46 // URLRequest. Returns false if unable to write the data failed because the | |
| 47 // underlying ChunkedUploadDataStream was destroyed. | |
| 48 bool AppendData(const char* data, int data_len, bool is_done); | |
| 49 | |
| 50 private: | |
| 51 friend class ChunkedUploadDataStream; | |
| 52 | |
| 53 explicit Writer(base::WeakPtr<ChunkedUploadDataStream> upload_data_stream); | |
| 54 | |
| 55 const base::WeakPtr<ChunkedUploadDataStream> upload_data_stream_; | |
| 56 | |
| 57 DISALLOW_COPY_AND_ASSIGN(Writer); | |
| 58 }; | |
| 59 | |
| 28 explicit ChunkedUploadDataStream(int64_t identifier); | 60 explicit ChunkedUploadDataStream(int64_t identifier); |
| 29 | 61 |
| 30 ~ChunkedUploadDataStream() override; | 62 ~ChunkedUploadDataStream() override; |
| 31 | 63 |
| 64 // Creates a Writer for appending data to |this|. It's generally expected | |
| 65 // that only one writer is created per stream, though multiple writers are | |
| 66 // allowed. All writers write to the same stream, and once one of them | |
| 67 // appends data with |is_done| being true, no other writers may be used to | |
| 68 // append data. | |
| 69 scoped_ptr<Writer> CreateWriter(); | |
| 70 | |
| 32 // Adds data to the stream. |is_done| should be true if this is the last | 71 // 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. | 72 // 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. | 73 // Once called with |is_done| being true, must never be called again. |
| 35 // TODO(mmenke): Consider using IOBuffers instead, to reduce data copies. | 74 // TODO(mmenke): Consider using IOBuffers instead, to reduce data copies. |
| 75 // TODO(mmenke): Consider making private, and having all consumers use | |
| 76 // Writers. | |
| 36 void AppendData(const char* data, int data_len, bool is_done); | 77 void AppendData(const char* data, int data_len, bool is_done); |
| 37 | 78 |
| 38 private: | 79 private: |
| 39 // UploadDataStream implementation. | 80 // UploadDataStream implementation. |
| 40 int InitInternal() override; | 81 int InitInternal() override; |
| 41 int ReadInternal(IOBuffer* buf, int buf_len) override; | 82 int ReadInternal(IOBuffer* buf, int buf_len) override; |
| 42 void ResetInternal() override; | 83 void ResetInternal() override; |
| 43 | 84 |
| 44 int ReadChunk(IOBuffer* buf, int buf_len); | 85 int ReadChunk(IOBuffer* buf, int buf_len); |
| 45 | 86 |
| 46 // Index and offset of next element of |upload_data_| to be read. | 87 // Index and offset of next element of |upload_data_| to be read. |
| 47 size_t read_index_; | 88 size_t read_index_; |
| 48 size_t read_offset_; | 89 size_t read_offset_; |
| 49 | 90 |
| 50 // True once all data has been appended to the stream. | 91 // True once all data has been appended to the stream. |
| 51 bool all_data_appended_; | 92 bool all_data_appended_; |
| 52 | 93 |
| 53 std::vector<scoped_ptr<std::vector<char>>> upload_data_; | 94 std::vector<scoped_ptr<std::vector<char>>> upload_data_; |
| 54 | 95 |
| 55 // Buffer to write the next read's data to. Only set when a call to | 96 // Buffer to write the next read's data to. Only set when a call to |
| 56 // ReadInternal reads no data. | 97 // ReadInternal reads no data. |
| 57 scoped_refptr<IOBuffer> read_buffer_; | 98 scoped_refptr<IOBuffer> read_buffer_; |
| 58 int read_buffer_len_; | 99 int read_buffer_len_; |
| 59 | 100 |
| 101 base::WeakPtrFactory<ChunkedUploadDataStream> weak_factory_; | |
| 102 | |
| 60 DISALLOW_COPY_AND_ASSIGN(ChunkedUploadDataStream); | 103 DISALLOW_COPY_AND_ASSIGN(ChunkedUploadDataStream); |
| 61 }; | 104 }; |
| 62 | 105 |
| 63 } // namespace net | 106 } // namespace net |
| 64 | 107 |
| 65 #endif // NET_BASE_CHUNKED_UPLOAD_DATA_STREAM_H_ | 108 #endif // NET_BASE_CHUNKED_UPLOAD_DATA_STREAM_H_ |
| OLD | NEW |