File: cronet_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 (114 lines) | stat: -rw-r--r-- 4,495 bytes parent folder | download | duplicates (6)
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
109
110
111
112
113
114
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_CRONET_CRONET_UPLOAD_DATA_STREAM_H_
#define COMPONENTS_CRONET_CRONET_UPLOAD_DATA_STREAM_H_

#include <stdint.h>

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

namespace net {
class IOBuffer;
}  // namespace net

namespace cronet {

// The CronetUploadDataStream is created on a client thread, but afterwards,
// lives and is deleted on the network thread. It's responsible for ensuring
// only one read/rewind request sent to client is outstanding at a time.
// The main complexity is around Reset/Initialize calls while there's a pending
// read or rewind.
class CronetUploadDataStream : public net::UploadDataStream {
 public:
  class Delegate {
   public:
    Delegate(const Delegate&) = delete;
    Delegate& operator=(const Delegate&) = delete;

    // Called once during initial setup on the network thread, called before
    // all other methods.
    virtual void InitializeOnNetworkThread(
        base::WeakPtr<CronetUploadDataStream> upload_data_stream) = 0;

    // Called for each read request. Delegate must respond by calling
    // OnReadSuccess on the network thread asynchronous, or failing the request.
    // Only called when there's no other pending read or rewind operation.
    virtual void Read(scoped_refptr<net::IOBuffer> buffer, int buf_len) = 0;

    // Called to rewind the stream. Not called when already at the start of the
    // stream. The delegate must respond by calling OnRewindSuccess
    // asynchronously on the network thread, or failing the request. Only called
    // when there's no other pending read or rewind operation.
    virtual void Rewind() = 0;

    // Called when the CronetUploadDataStream is destroyed. The Delegate is then
    // responsible for destroying itself. May be called when there's a pending
    // read or rewind operation.
    virtual void OnUploadDataStreamDestroyed() = 0;

   protected:
    Delegate() = default;
    virtual ~Delegate() = default;
  };

  CronetUploadDataStream(Delegate* delegate, int64_t size);

  CronetUploadDataStream(const CronetUploadDataStream&) = delete;
  CronetUploadDataStream& operator=(const CronetUploadDataStream&) = delete;

  ~CronetUploadDataStream() override;

  // Failure is handled at the Java layer. These two success callbacks are
  // invoked by client UploadDataSink upon completion of the operation.
  void OnReadSuccess(int bytes_read, bool final_chunk);
  void OnRewindSuccess();

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

  // Starts rewinding the stream. Only called when not already at the front of
  // the stream, and no operation is pending. Completes asynchronously.
  void StartRewind();

  // Size of the upload. -1 if chunked.
  const int64_t size_;

  // True if ReadInternal has been called, the read hasn't completed, and there
  // hasn't been a ResetInternal call yet.
  bool waiting_on_read_;
  // True if there's a read operation in progress. This will always be true
  // when |waiting_on_read_| is true. This will only be set to false once it
  // completes, even though ResetInternal may have been called since the read
  // started.
  bool read_in_progress_;

  // True if InitInternal has been called, the rewind hasn't completed, and
  // there hasn't been a ResetInternal call yet. Note that this may be true
  // even when the rewind hasn't yet started, if there's a read in progress.
  bool waiting_on_rewind_;
  // True if there's a rewind operation in progress. Rewinding will only start
  // when |waiting_on_rewind_| is true, and |read_in_progress_| is false. This
  // will only be set to false once it completes, even though ResetInternal may
  // have been called since the rewind started.
  bool rewind_in_progress_;

  // Set to false when a read starts, true when a rewind completes.
  bool at_front_of_stream_;

  const raw_ptr<Delegate, DanglingUntriaged> delegate_;

  // Vends pointers on the network thread, though created on a client thread.
  base::WeakPtrFactory<CronetUploadDataStream> weak_factory_{this};
};

}  // namespace cronet

#endif  // COMPONENTS_CRONET_CRONET_UPLOAD_DATA_STREAM_H_