File: upload_data_sink.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (97 lines) | stat: -rw-r--r-- 3,732 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
// Copyright 2018 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_NATIVE_UPLOAD_DATA_SINK_H_
#define COMPONENTS_CRONET_NATIVE_UPLOAD_DATA_SINK_H_

#include <memory>

#include "base/memory/raw_ptr.h"
#include "base/synchronization/lock.h"
#include "base/synchronization/waitable_event.h"
#include "base/task/single_thread_task_runner.h"
#include "components/cronet/cronet_context.h"
#include "components/cronet/cronet_upload_data_stream.h"
#include "components/cronet/cronet_url_request.h"
#include "components/cronet/native/generated/cronet.idl_impl_interface.h"

namespace cronet {

class Cronet_UrlRequestImpl;
class Cronet_BufferWithIOBuffer;

// Implementation of Cronet_UploadDataSink that uses CronetUploadDataStream.
// Always accessed on client executor.
class Cronet_UploadDataSinkImpl : public Cronet_UploadDataSink {
 public:
  Cronet_UploadDataSinkImpl(Cronet_UrlRequestImpl* url_request,
                            Cronet_UploadDataProvider* upload_data_provider,
                            Cronet_Executor* upload_data_provider_executor);

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

  ~Cronet_UploadDataSinkImpl() override;

  // Initialize length and attach upload to request. Called on client thread.
  void InitRequest(CronetURLRequest* request);

  // Mark stream as closed and post |Close()| callback to consumer.
  void PostCloseToExecutor();

 private:
  class NetworkTasks;
  enum UserCallback { READ, REWIND, GET_LENGTH, NOT_IN_CALLBACK };

  // Cronet_UploadDataSink
  void OnReadSucceeded(uint64_t bytes_read, bool final_chunk) override;
  void OnReadError(Cronet_String error_message) override;
  void OnRewindSucceeded() override;
  void OnRewindError(Cronet_String error_message) override;

  // CronetUploadDataStream::Delegate methods posted from the network thread.
  void InitializeUploadDataStream(
      base::WeakPtr<CronetUploadDataStream> upload_data_stream,
      scoped_refptr<base::SingleThreadTaskRunner> network_task_runner);
  void Read(scoped_refptr<net::IOBuffer> buffer, int buf_len);
  void Rewind();
  void Close();

  void CheckState(UserCallback expected_state);

  // Cronet objects not owned by |this| and accessed on client thread.

  // The request, which owns |this|.
  const raw_ptr<Cronet_UrlRequestImpl> url_request_ = nullptr;
  // Executor for provider callback, used, but not owned, by |this|. Always
  // outlives |this| callback.
  Cronet_ExecutorPtr const upload_data_provider_executor_ = nullptr;

  // These are initialized in InitializeUploadDataStream(), so are safe to
  // access during client callbacks, which all happen after initialization.
  scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
  base::WeakPtr<CronetUploadDataStream> upload_data_stream_;

  bool is_chunked_ = false;
  uint64_t length_ = 0;
  uint64_t remaining_length_ = 0;

  // Synchronize access to |buffer_| and other objects below from different
  // threads.
  base::Lock lock_;
  // Data provider callback interface, used, but not owned, by |this|.
  // Set to nullptr when data provider is closed.
  Cronet_UploadDataProviderPtr upload_data_provider_ = nullptr;

  UserCallback in_which_user_callback_ = NOT_IN_CALLBACK;
  // Close data provider once it returns from the callback.
  bool close_when_not_in_callback_ = false;
  // Keeps the net::IOBuffer and Cronet ByteBuffer alive until the next Read().
  std::unique_ptr<Cronet_BufferWithIOBuffer> buffer_;
};

}  // namespace cronet

#endif  // COMPONENTS_CRONET_NATIVE_UPLOAD_DATA_SINK_H_