upload_bytes_element_reader.h 2.3 KB

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