| OLD | NEW |
| 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_ELEMENTS_UPLOAD_DATA_STREAM_H_ | 5 #ifndef NET_BASE_ELEMENTS_UPLOAD_DATA_STREAM_H_ |
| 6 #define NET_BASE_ELEMENTS_UPLOAD_DATA_STREAM_H_ | 6 #define NET_BASE_ELEMENTS_UPLOAD_DATA_STREAM_H_ |
| 7 | 7 |
| 8 #include <vector> |
| 9 |
| 8 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 9 #include "base/macros.h" | 11 #include "base/macros.h" |
| 10 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/memory/scoped_vector.h" | |
| 13 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 14 #include "net/base/net_export.h" | 15 #include "net/base/net_export.h" |
| 15 #include "net/base/upload_data_stream.h" | 16 #include "net/base/upload_data_stream.h" |
| 16 | 17 |
| 17 namespace net { | 18 namespace net { |
| 18 | 19 |
| 19 class DrainableIOBuffer; | 20 class DrainableIOBuffer; |
| 20 class IOBuffer; | 21 class IOBuffer; |
| 21 class UploadElementReader; | 22 class UploadElementReader; |
| 22 | 23 |
| 23 // A non-chunked UploadDataStream consisting of one or more UploadElements. | 24 // A non-chunked UploadDataStream consisting of one or more UploadElements. |
| 24 class NET_EXPORT ElementsUploadDataStream : public UploadDataStream { | 25 class NET_EXPORT ElementsUploadDataStream : public UploadDataStream { |
| 25 public: | 26 public: |
| 26 ElementsUploadDataStream(ScopedVector<UploadElementReader> element_readers, | 27 ElementsUploadDataStream( |
| 27 int64_t identifier); | 28 std::vector<scoped_ptr<UploadElementReader>> element_readers, |
| 29 int64_t identifier); |
| 28 | 30 |
| 29 ~ElementsUploadDataStream() override; | 31 ~ElementsUploadDataStream() override; |
| 30 | 32 |
| 31 // Creates an ElementsUploadDataStream with a single reader. Returns a | 33 // Creates an ElementsUploadDataStream with a single reader. Returns a |
| 32 // scoped_ptr<UploadDataStream> for ease of use. | 34 // scoped_ptr<UploadDataStream> for ease of use. |
| 33 static scoped_ptr<UploadDataStream> CreateWithReader( | 35 static scoped_ptr<UploadDataStream> CreateWithReader( |
| 34 scoped_ptr<UploadElementReader> reader, | 36 scoped_ptr<UploadElementReader> reader, |
| 35 int64_t identifier); | 37 int64_t identifier); |
| 36 | 38 |
| 37 private: | 39 private: |
| 38 // UploadDataStream implementation. | 40 // UploadDataStream implementation. |
| 39 bool IsInMemory() const override; | 41 bool IsInMemory() const override; |
| 40 const ScopedVector<UploadElementReader>* GetElementReaders() const override; | 42 const std::vector<scoped_ptr<UploadElementReader>>* GetElementReaders() |
| 43 const override; |
| 41 int InitInternal() override; | 44 int InitInternal() override; |
| 42 int ReadInternal(IOBuffer* buf, int buf_len) override; | 45 int ReadInternal(IOBuffer* buf, int buf_len) override; |
| 43 void ResetInternal() override; | 46 void ResetInternal() override; |
| 44 | 47 |
| 45 // Runs Init() for all element readers. | 48 // Runs Init() for all element readers. |
| 46 // This method is used to implement InitInternal(). | 49 // This method is used to implement InitInternal(). |
| 47 int InitElements(size_t start_index); | 50 int InitElements(size_t start_index); |
| 48 | 51 |
| 49 // Called when the |index| element finishes initialization. If it succeeded, | 52 // Called when the |index| element finishes initialization. If it succeeded, |
| 50 // continues with the |index + 1| element. Calls OnInitCompleted on error or | 53 // continues with the |index + 1| element. Calls OnInitCompleted on error or |
| 51 // when all elements have been initialized. | 54 // when all elements have been initialized. |
| 52 void OnInitElementCompleted(size_t index, int result); | 55 void OnInitElementCompleted(size_t index, int result); |
| 53 | 56 |
| 54 // Reads data from the element readers. | 57 // Reads data from the element readers. |
| 55 // This method is used to implement Read(). | 58 // This method is used to implement Read(). |
| 56 int ReadElements(const scoped_refptr<DrainableIOBuffer>& buf); | 59 int ReadElements(const scoped_refptr<DrainableIOBuffer>& buf); |
| 57 | 60 |
| 58 // Resumes pending read and calls OnReadCompleted with a result when | 61 // Resumes pending read and calls OnReadCompleted with a result when |
| 59 // necessary. | 62 // necessary. |
| 60 void OnReadElementCompleted(const scoped_refptr<DrainableIOBuffer>& buf, | 63 void OnReadElementCompleted(const scoped_refptr<DrainableIOBuffer>& buf, |
| 61 int result); | 64 int result); |
| 62 | 65 |
| 63 // Processes result of UploadElementReader::Read(). If |result| indicates | 66 // Processes result of UploadElementReader::Read(). If |result| indicates |
| 64 // success, updates |buf|'s offset. Otherwise, sets |read_failed_| to true. | 67 // success, updates |buf|'s offset. Otherwise, sets |read_failed_| to true. |
| 65 void ProcessReadResult(const scoped_refptr<DrainableIOBuffer>& buf, | 68 void ProcessReadResult(const scoped_refptr<DrainableIOBuffer>& buf, |
| 66 int result); | 69 int result); |
| 67 | 70 |
| 68 ScopedVector<UploadElementReader> element_readers_; | 71 std::vector<scoped_ptr<UploadElementReader>> element_readers_; |
| 69 | 72 |
| 70 // Index of the current upload element (i.e. the element currently being | 73 // Index of the current upload element (i.e. the element currently being |
| 71 // read). The index is used as a cursor to iterate over elements in | 74 // read). The index is used as a cursor to iterate over elements in |
| 72 // |upload_data_|. | 75 // |upload_data_|. |
| 73 size_t element_index_; | 76 size_t element_index_; |
| 74 | 77 |
| 75 // True if an error occcured during read operation. | 78 // True if an error occcured during read operation. |
| 76 bool read_failed_; | 79 bool read_failed_; |
| 77 | 80 |
| 78 base::WeakPtrFactory<ElementsUploadDataStream> weak_ptr_factory_; | 81 base::WeakPtrFactory<ElementsUploadDataStream> weak_ptr_factory_; |
| 79 | 82 |
| 80 DISALLOW_COPY_AND_ASSIGN(ElementsUploadDataStream); | 83 DISALLOW_COPY_AND_ASSIGN(ElementsUploadDataStream); |
| 81 }; | 84 }; |
| 82 | 85 |
| 83 } // namespace net | 86 } // namespace net |
| 84 | 87 |
| 85 #endif // NET_BASE_ELEMENTS_UPLOAD_DATA_STREAM_H_ | 88 #endif // NET_BASE_ELEMENTS_UPLOAD_DATA_STREAM_H_ |
| OLD | NEW |