File: upstream_loader.h

package info (click to toggle)
qt6-webengine 6.4.2-final%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,090,536 kB
  • sloc: cpp: 17,808,031; ansic: 5,245,139; javascript: 3,178,881; python: 1,361,176; asm: 648,577; xml: 571,140; java: 196,952; sh: 96,408; objc: 88,289; perl: 70,982; cs: 39,145; fortran: 24,137; makefile: 20,242; pascal: 12,634; sql: 10,875; yacc: 9,671; tcl: 8,385; php: 6,188; lisp: 2,848; lex: 2,263; ada: 727; ruby: 623; awk: 339; sed: 37
file content (78 lines) | stat: -rw-r--r-- 2,879 bytes parent folder | download
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
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_SPEECH_UPSTREAM_LOADER_H_
#define COMPONENTS_SPEECH_UPSTREAM_LOADER_H_

#include <memory>
#include <string>

#include "base/memory/raw_ptr.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "mojo/public/cpp/system/data_pipe.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "services/network/public/mojom/chunked_data_pipe_getter.mojom.h"

namespace speech {

class UpstreamLoaderClient;

// Maximum amount of data written per Mojo write.
const uint32_t kMaxUploadWrite = 128 * 1024;

// Streams sound data up to the server. Buffers entire request body into memory,
// so it can be replayed in the case of redirects or retries.
class UpstreamLoader : public network::mojom::ChunkedDataPipeGetter {
 public:
  UpstreamLoader(std::unique_ptr<network::ResourceRequest> resource_request,
                 net::NetworkTrafficAnnotationTag upstream_traffic_annotation,
                 network::mojom::URLLoaderFactory* url_loader_factory,
                 UpstreamLoaderClient* upstream_loader_client);
  UpstreamLoader(const UpstreamLoader&) = delete;
  UpstreamLoader& operator=(const UpstreamLoader&) = delete;
  ~UpstreamLoader() override;

  void SendData();

  void AppendChunkToUpload(const std::string& data, bool is_last_chunk);

 private:
  void OnUploadPipeWriteable(MojoResult unused);
  void OnComplete(std::unique_ptr<std::string> response_body);

  // mojom::ChunkedDataPipeGetter implementation:
  void GetSize(GetSizeCallback get_size_callback) override;
  void StartReading(mojo::ScopedDataPipeProducerHandle pipe) override;

  // Partial upload body. Have to cache the entire thing in memory, in case have
  // to replay it.
  std::string upload_body_;

  // Current position in |upload_body_|.  All bytes before this point have been
  // written to |upload_pipe_|.
  size_t upload_position_ = 0;

  // Whether |upload_body_| is complete.
  bool has_last_chunk_ = false;

  // Current pipe being used to send the |upload_body_| to the URLLoader.
  mojo::ScopedDataPipeProducerHandle upload_pipe_;

  // Watches |upload_pipe_| for writeability.
  std::unique_ptr<mojo::SimpleWatcher> upload_pipe_watcher_;

  // If non-null, invoked once the size of the upload is known.
  network::mojom::ChunkedDataPipeGetter::GetSizeCallback get_size_callback_;

  // The UpstreamLoaderClient must outlive the UpstreamLoader.
  const raw_ptr<UpstreamLoaderClient> upstream_loader_client_;

  std::unique_ptr<network::SimpleURLLoader> simple_url_loader_;
  mojo::ReceiverSet<network::mojom::ChunkedDataPipeGetter> receiver_set_;
};

}  // namespace speech

#endif  // COMPONENTS_SPEECH_UPSTREAM_LOADER_H_