1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_BASE_UPLOAD_BYTES_ELEMENT_READER_H_ 6 #define NET_BASE_UPLOAD_BYTES_ELEMENT_READER_H_ 7 8 #include <stdint.h> 9 10 #include <string> 11 #include <vector> 12 13 #include "base/compiler_specific.h" 14 #include "net/base/net_export.h" 15 #include "net/base/upload_element_reader.h" 16 17 namespace net { 18 19 // An UploadElementReader implementation for bytes. The caller owns |bytes|, 20 // and is responsible for ensuring it outlives the UploadBytesElementReader. 21 class NET_EXPORT UploadBytesElementReader : public UploadElementReader { 22 public: 23 UploadBytesElementReader(const char* bytes, uint64_t length); 24 UploadBytesElementReader(const UploadBytesElementReader&) = delete; 25 UploadBytesElementReader& operator=(const UploadBytesElementReader&) = delete; 26 ~UploadBytesElementReader() override; 27 bytes()28 const char* bytes() const { return bytes_; } length()29 uint64_t length() const { return length_; } 30 31 // UploadElementReader overrides: 32 const UploadBytesElementReader* AsBytesReader() const override; 33 int Init(CompletionOnceCallback callback) override; 34 uint64_t GetContentLength() const override; 35 uint64_t BytesRemaining() const override; 36 bool IsInMemory() const override; 37 int Read(IOBuffer* buf, 38 int buf_length, 39 CompletionOnceCallback callback) override; 40 41 private: 42 const char* const bytes_; 43 const uint64_t length_; 44 uint64_t offset_ = 0; 45 }; 46 47 // A subclass of UplodBytesElementReader which owns the data given as a vector. 48 class NET_EXPORT UploadOwnedBytesElementReader 49 : public UploadBytesElementReader { 50 public: 51 // |data| is cleared by this ctor. 52 explicit UploadOwnedBytesElementReader(std::vector<char>* data); 53 UploadOwnedBytesElementReader(const UploadOwnedBytesElementReader&) = delete; 54 UploadOwnedBytesElementReader& operator=( 55 const UploadOwnedBytesElementReader&) = delete; 56 ~UploadOwnedBytesElementReader() override; 57 58 // Creates UploadOwnedBytesElementReader with a string. 59 static std::unique_ptr<UploadOwnedBytesElementReader> CreateWithString( 60 const std::string& string); 61 62 private: 63 std::vector<char> data_; 64 }; 65 66 } // namespace net 67 68 #endif // NET_BASE_UPLOAD_BYTES_ELEMENT_READER_H_ 69