File: shared_dictionary_data_pipe_writer.cc

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 (180 lines) | stat: -rw-r--r-- 6,222 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "services/network/shared_dictionary/shared_dictionary_data_pipe_writer.h"

#include "base/containers/span.h"
#include "base/memory/ptr_util.h"
#include "net/base/net_errors.h"
#include "services/network/public/cpp/loading_params.h"
#include "services/network/shared_dictionary/shared_dictionary_writer.h"

namespace network {

// static

uint32_t SharedDictionaryDataPipeWriter::GetDataPipeBufferSize() {
  return GetDataPipeDefaultAllocationSize(
      DataPipeAllocationSize::kLargerSizeIfPossible);
}

// static
std::unique_ptr<SharedDictionaryDataPipeWriter>
SharedDictionaryDataPipeWriter::Create(
    mojo::ScopedDataPipeConsumerHandle& body,
    scoped_refptr<SharedDictionaryWriter> writer,
    base::OnceCallback<void(bool)> finish_callback) {
  MojoCreateDataPipeOptions options;
  options.struct_size = sizeof(MojoCreateDataPipeOptions);
  options.flags = MOJO_CREATE_DATA_PIPE_FLAG_NONE;
  options.element_num_bytes = 1;
  options.capacity_num_bytes = GetDataPipeBufferSize();

  mojo::ScopedDataPipeProducerHandle producer_handle;
  mojo::ScopedDataPipeConsumerHandle consumer_handle;
  MojoResult result =
      mojo::CreateDataPipe(&options, producer_handle, consumer_handle);
  if (result != MOJO_RESULT_OK) {
    return nullptr;
  }
  auto data_pipe_writer = base::WrapUnique(new SharedDictionaryDataPipeWriter(
      std::move(body), std::move(producer_handle), std::move(writer),
      std::move(finish_callback)));
  body = std::move(consumer_handle);
  return data_pipe_writer;
}

SharedDictionaryDataPipeWriter::SharedDictionaryDataPipeWriter(
    mojo::ScopedDataPipeConsumerHandle consumer_handle,
    mojo::ScopedDataPipeProducerHandle producer_handle,
    scoped_refptr<SharedDictionaryWriter> writer,
    base::OnceCallback<void(bool)> finish_callback)
    : consumer_handle_(std::move(consumer_handle)),
      producer_handle_(std::move(producer_handle)),
      writer_(std::move(writer)),
      consumer_watcher_(FROM_HERE, mojo::SimpleWatcher::ArmingPolicy::MANUAL),
      producer_writable_watcher_(FROM_HERE,
                                 mojo::SimpleWatcher::ArmingPolicy::MANUAL),
      producer_closed_watcher_(FROM_HERE,
                               mojo::SimpleWatcher::ArmingPolicy::MANUAL),
      finish_callback_(std::move(finish_callback)) {
  consumer_watcher_.Watch(
      consumer_handle_.get(),
      MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
      MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
      base::BindRepeating(&SharedDictionaryDataPipeWriter::ContinueReadWrite,
                          base::Unretained(this)));
  producer_writable_watcher_.Watch(
      producer_handle_.get(), MOJO_HANDLE_SIGNAL_WRITABLE,
      MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
      base::BindRepeating(&SharedDictionaryDataPipeWriter::ContinueReadWrite,
                          base::Unretained(this)));
  producer_closed_watcher_.Watch(
      producer_handle_.get(), MOJO_HANDLE_SIGNAL_PEER_CLOSED,
      MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
      base::BindRepeating(&SharedDictionaryDataPipeWriter::OnPeerClosed,
                          base::Unretained(this)));

  consumer_watcher_.ArmOrNotify();
  producer_closed_watcher_.ArmOrNotify();
}

SharedDictionaryDataPipeWriter::~SharedDictionaryDataPipeWriter() = default;

void SharedDictionaryDataPipeWriter::OnComplete(bool success) {
  DCHECK(!completion_result_.has_value());
  completion_result_ = success;
  MaybeFinish();
}

void SharedDictionaryDataPipeWriter::ContinueReadWrite(
    MojoResult,
    const mojo::HandleSignalsState& state) {
  base::span<const uint8_t> buffer;
  MojoResult result =
      consumer_handle_->BeginReadData(MOJO_BEGIN_READ_DATA_FLAG_NONE, buffer);
  switch (result) {
    case MOJO_RESULT_OK:
      break;
    case MOJO_RESULT_FAILED_PRECONDITION:
      // Successfully read the whole data.
      FinishDataPipeOperation(/*success=*/true);
      return;
    case MOJO_RESULT_SHOULD_WAIT:
      // `consumer_handle_` must be readable or closed here.
      NOTREACHED();
    default:
      NOTREACHED();
  }

  size_t actually_written_bytes = 0;
  result = producer_handle_->WriteData(buffer, MOJO_WRITE_DATA_FLAG_NONE,
                                       actually_written_bytes);
  buffer = buffer.first(actually_written_bytes);
  switch (result) {
    case MOJO_RESULT_OK:
      break;
    case MOJO_RESULT_SHOULD_WAIT:
      consumer_handle_->EndReadData(0);
      producer_writable_watcher_.ArmOrNotify();
      return;
    case MOJO_RESULT_FAILED_PRECONDITION:
      // The data pipe consumer is aborted.
      FinishDataPipeOperation(/*success=*/false);
      return;
    default:
      NOTREACHED();
  }
  writer_->Append(buffer);
  consumer_handle_->EndReadData(buffer.size());
  consumer_watcher_.ArmOrNotify();
}

void SharedDictionaryDataPipeWriter::OnPeerClosed(
    MojoResult,
    const mojo::HandleSignalsState& state) {
  // The data pipe consumer is aborted.
  FinishDataPipeOperation(/*success=*/false);
}

void SharedDictionaryDataPipeWriter::FinishDataPipeOperation(bool success) {
  DCHECK(!data_pipe_operation_result_.has_value());
  data_pipe_operation_result_ = success;

  consumer_watcher_.Cancel();
  producer_writable_watcher_.Cancel();
  producer_closed_watcher_.Cancel();
  consumer_handle_.reset();
  producer_handle_.reset();

  MaybeFinish();
}

void SharedDictionaryDataPipeWriter::MaybeFinish() {
  if (!writer_ || !finish_callback_) {
    return;
  }

  if (!data_pipe_operation_result_.value_or(true) ||
      !completion_result_.value_or(true)) {
    // The data pipe consumer is aborted, or OnComplete() is called with false.
    writer_.reset();
    std::move(finish_callback_).Run(false);
    return;
  }

  if (!data_pipe_operation_result_.has_value() ||
      !completion_result_.has_value()) {
    return;
  }

  DCHECK(*data_pipe_operation_result_);
  DCHECK(*completion_result_);
  // Successfully read the whole data, and OnComplete() is called with true.
  writer_->Finish();
  writer_.reset();
  std::move(finish_callback_).Run(true);
}

}  // namespace network