File: chunked_upload_data_stream.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (108 lines) | stat: -rw-r--r-- 4,165 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_BASE_CHUNKED_UPLOAD_DATA_STREAM_H_
#define NET_BASE_CHUNKED_UPLOAD_DATA_STREAM_H_

#include <stddef.h>
#include <stdint.h>

#include <memory>
#include <vector>

#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "net/base/net_export.h"
#include "net/base/upload_data_stream.h"

namespace net {

class IOBuffer;

// Class with a push-based interface for uploading data. Buffers all data until
// the request is completed. Not recommended for uploading large amounts of
// seekable data, due to this buffering behavior.
class NET_EXPORT ChunkedUploadDataStream : public UploadDataStream {
 public:
  // Utility class that allows writing data to a particular
  // ChunkedUploadDataStream. It can outlive the associated
  // ChunkedUploadDataStream, and the URLRequest it is associated with, and
  // still be safely used. This allows the consumer to not have to worry about
  // the lifetime of the ChunkedUploadDataStream, which the owning URLRequest
  // may delete without warning.
  //
  // The writer may only be used on the ChunkedUploadDataStream's thread.
  class NET_EXPORT Writer {
   public:
    Writer(const Writer&) = delete;
    Writer& operator=(const Writer&) = delete;
    ~Writer();

    // Adds data to the stream. |is_done| should be true if this is the last
    // data to be appended. |data| must not be empty unless |is_done| is true.
    // Once called with |is_done| being true, must never be called again.
    // Returns true if write was passed successfully on to the next layer,
    // though the data may not actually have been written to the underlying
    // URLRequest.  Returns false if unable to write the data failed because the
    // underlying ChunkedUploadDataStream was destroyed.
    bool AppendData(base::span<const uint8_t> data, bool is_done);

   private:
    friend class ChunkedUploadDataStream;

    explicit Writer(base::WeakPtr<ChunkedUploadDataStream> upload_data_stream);

    const base::WeakPtr<ChunkedUploadDataStream> upload_data_stream_;
  };

  explicit ChunkedUploadDataStream(int64_t identifier,
                                   bool has_null_source = false);

  ChunkedUploadDataStream(const ChunkedUploadDataStream&) = delete;
  ChunkedUploadDataStream& operator=(const ChunkedUploadDataStream&) = delete;
  ~ChunkedUploadDataStream() override;

  // Creates a Writer for appending data to |this|.  It's generally expected
  // that only one writer is created per stream, though multiple writers are
  // allowed.  All writers write to the same stream, and once one of them
  // appends data with |is_done| being true, no other writers may be used to
  // append data.
  std::unique_ptr<Writer> CreateWriter();

  // Adds data to the stream. |is_done| should be true if this is the last
  // data to be appended. |data| must not be empty unless |is_done| is true.
  // Once called with |is_done| being true, must never be called again.
  // TODO(mmenke):  Consider using IOBuffers instead, to reduce data copies.
  // TODO(mmenke):  Consider making private, and having all consumers use
  //     Writers.
  void AppendData(base::span<const uint8_t> data, bool is_done);

 private:
  // UploadDataStream implementation.
  int InitInternal(const NetLogWithSource& net_log) override;
  int ReadInternal(IOBuffer* buf, int buf_len) override;
  void ResetInternal() override;

  int ReadChunk(IOBuffer* buf, int buf_len);

  // Index and offset of next element of |upload_data_| to be read.
  size_t read_index_ = 0;
  size_t read_offset_ = 0;

  // True once all data has been appended to the stream.
  bool all_data_appended_ = false;

  std::vector<std::unique_ptr<std::vector<uint8_t>>> upload_data_;

  // Buffer to write the next read's data to. Only set when a call to
  // ReadInternal reads no data.
  scoped_refptr<IOBuffer> read_buffer_;
  int read_buffer_len_ = 0;

  base::WeakPtrFactory<ChunkedUploadDataStream> weak_factory_{this};
};

}  // namespace net

#endif  // NET_BASE_CHUNKED_UPLOAD_DATA_STREAM_H_