File: data_pipe_element_reader.h

package info (click to toggle)
chromium 145.0.7632.159-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,976,224 kB
  • sloc: cpp: 36,198,469; ansic: 7,634,080; javascript: 3,564,060; python: 1,649,622; xml: 838,470; asm: 717,087; pascal: 185,708; sh: 88,786; perl: 88,718; objc: 79,984; sql: 59,811; cs: 42,452; fortran: 24,101; makefile: 21,144; tcl: 15,277; php: 14,022; yacc: 9,066; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (91 lines) | stat: -rw-r--r-- 3,376 bytes parent folder | download | duplicates (10)
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
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef SERVICES_NETWORK_DATA_PIPE_ELEMENT_READER_H_
#define SERVICES_NETWORK_DATA_PIPE_ELEMENT_READER_H_

#include <stdint.h>

#include "base/component_export.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/system/data_pipe.h"
#include "mojo/public/cpp/system/simple_watcher.h"
#include "net/base/completion_once_callback.h"
#include "net/base/upload_element_reader.h"
#include "services/network/public/cpp/resource_request_body.h"
#include "services/network/public/mojom/data_pipe_getter.mojom.h"

namespace net {
class IOBuffer;
}

namespace network {

// A subclass of net::UploadElementReader to read data pipes.
class COMPONENT_EXPORT(NETWORK_SERVICE) DataPipeElementReader
    : public net::UploadElementReader {
 public:
  // |resource_request_body| is just passed in to keep the object around for the
  // life of the ElementReader.
  //
  // TODO(mmenke): This class doesn't handle the case where the DataPipeGetter
  // pipe is closed. That should be fixed.
  DataPipeElementReader(
      scoped_refptr<ResourceRequestBody> resource_request_body,
      mojo::PendingRemote<mojom::DataPipeGetter> data_pipe_getter);

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

  ~DataPipeElementReader() override;

  // net::UploadElementReader implementation:
  int Init(net::CompletionOnceCallback callback) override;
  uint64_t GetContentLength() const override;
  uint64_t BytesRemaining() const override;
  int Read(net::IOBuffer* buf,
           int buf_length,
           net::CompletionOnceCallback callback) override;

 private:
  // Callback invoked by DataPipeGetter::Read.
  void ReadCallback(int32_t status, uint64_t size);

  // Called by |handle_watcher_| when data is available or the pipe was closed,
  // and there's a pending Read() call.
  void OnHandleReadable(MojoResult result);

  // Attempts to read data from |data_pipe_| and write it to |buf|. On success,
  // writes the amount of data written. On failure, returns a net error code. If
  // no data was available yet, tells |handle_watcher_| to start watching the
  // pipe for data to become available and returns ERR_IO_PENDING. It's up to
  // the caller to update |buf_| and |buf_length_| if needed.
  int ReadInternal(net::IOBuffer* buf, int buf_length);

  scoped_refptr<ResourceRequestBody> resource_request_body_;
  mojo::Remote<mojom::DataPipeGetter> data_pipe_getter_;
  mojo::ScopedDataPipeConsumerHandle data_pipe_;
  mojo::SimpleWatcher handle_watcher_;

  // Write buffer and its length. Populated when Read() is called but returns
  // ERR_IO_PENDING. Cleared once the read completes.
  scoped_refptr<net::IOBuffer> buf_;
  int buf_length_ = 0;

  // Total size of input, as passed to ReadCallback().
  uint64_t size_ = 0;

  uint64_t bytes_read_ = 0;
  net::CompletionOnceCallback init_callback_;
  net::CompletionOnceCallback read_callback_;

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

}  // namespace network

#endif  // SERVICES_NETWORK_DATA_PIPE_ELEMENT_READER_H_