File: cronet_upload_data_stream.cc

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 (137 lines) | stat: -rw-r--r-- 3,847 bytes parent folder | download | duplicates (7)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// 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.

#include "components/cronet/cronet_upload_data_stream.h"

#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"

namespace cronet {

CronetUploadDataStream::CronetUploadDataStream(Delegate* delegate, int64_t size)
    : UploadDataStream(size < 0, 0),
      size_(size),
      waiting_on_read_(false),
      read_in_progress_(false),
      waiting_on_rewind_(false),
      rewind_in_progress_(false),
      at_front_of_stream_(true),
      delegate_(delegate) {}

CronetUploadDataStream::~CronetUploadDataStream() {
  delegate_->OnUploadDataStreamDestroyed();
}

int CronetUploadDataStream::InitInternal(const net::NetLogWithSource& net_log) {
  // ResetInternal should have been called before init, if the stream was in
  // use.
  DCHECK(!waiting_on_read_);
  DCHECK(!waiting_on_rewind_);

  if (!weak_factory_.HasWeakPtrs())
    delegate_->InitializeOnNetworkThread(weak_factory_.GetWeakPtr());

  // Set size of non-chunked uploads.
  if (size_ >= 0)
    SetSize(static_cast<uint64_t>(size_));

  // If already at the front of the stream, nothing to do.
  if (at_front_of_stream_) {
    // Being at the front of the stream implies there's no read or rewind in
    // progress.
    DCHECK(!read_in_progress_);
    DCHECK(!rewind_in_progress_);
    return net::OK;
  }

  // Otherwise, the request is now waiting for the stream to be rewound.
  waiting_on_rewind_ = true;

  // Start rewinding the stream if no operation is in progress.
  if (!read_in_progress_ && !rewind_in_progress_)
    StartRewind();
  return net::ERR_IO_PENDING;
}

int CronetUploadDataStream::ReadInternal(net::IOBuffer* buf, int buf_len) {
  // All pending operations should have completed before a read can start.
  DCHECK(!waiting_on_read_);
  DCHECK(!read_in_progress_);
  DCHECK(!waiting_on_rewind_);
  DCHECK(!rewind_in_progress_);

  DCHECK(buf);
  DCHECK_GT(buf_len, 0);

  read_in_progress_ = true;
  waiting_on_read_ = true;
  at_front_of_stream_ = false;
  scoped_refptr<net::IOBuffer> buffer(base::WrapRefCounted(buf));
  delegate_->Read(std::move(buffer), buf_len);
  return net::ERR_IO_PENDING;
}

void CronetUploadDataStream::ResetInternal() {
  // Consumer is not waiting on any operation.  Note that the active operation,
  // if any, will continue.
  waiting_on_read_ = false;
  waiting_on_rewind_ = false;
}

void CronetUploadDataStream::OnReadSuccess(int bytes_read, bool final_chunk) {
  DCHECK(read_in_progress_);
  DCHECK(!rewind_in_progress_);
  DCHECK(bytes_read > 0 || (final_chunk && bytes_read == 0));
  if (!is_chunked()) {
    DCHECK(!final_chunk);
  }

  read_in_progress_ = false;

  if (waiting_on_rewind_) {
    DCHECK(!waiting_on_read_);
    // Since a read just completed, can't be at the front of the stream.
    StartRewind();
    return;
  }
  // ResetInternal has been called, but still waiting on InitInternal.
  if (!waiting_on_read_)
    return;

  waiting_on_read_ = false;
  if (final_chunk)
    SetIsFinalChunk();
  OnReadCompleted(bytes_read);
}

void CronetUploadDataStream::OnRewindSuccess() {
  DCHECK(!waiting_on_read_);
  DCHECK(!read_in_progress_);
  DCHECK(rewind_in_progress_);
  DCHECK(!at_front_of_stream_);

  rewind_in_progress_ = false;
  at_front_of_stream_ = true;

  // Possible that ResetInternal was called since the rewind was started, but
  // InitInternal has not been.
  if (!waiting_on_rewind_)
    return;

  waiting_on_rewind_ = false;
  OnInitCompleted(net::OK);
}

void CronetUploadDataStream::StartRewind() {
  DCHECK(!waiting_on_read_);
  DCHECK(!read_in_progress_);
  DCHECK(waiting_on_rewind_);
  DCHECK(!rewind_in_progress_);
  DCHECK(!at_front_of_stream_);

  rewind_in_progress_ = true;
  delegate_->Rewind();
}

}  // namespace cronet