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