File: blob_reader.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 (114 lines) | stat: -rw-r--r-- 3,713 bytes parent folder | download | duplicates (3)
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
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/browser/blob_reader.h"

#include <limits>
#include <utility>

#include "base/containers/span.h"
#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/strings/string_view_util.h"
#include "content/public/browser/browser_thread.h"

// static
void BlobReader::Read(mojo::PendingRemote<blink::mojom::Blob> blob,
                      BlobReadCallback callback,
                      uint64_t offset,
                      uint64_t length) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  CHECK_GE(offset, 0U);
  CHECK_GT(length, 0U);
  CHECK_LE(offset, std::numeric_limits<uint64_t>::max() - length);

  Read(std::move(blob), std::move(callback), Range{offset, length});
}

// static
void BlobReader::Read(mojo::PendingRemote<blink::mojom::Blob> blob,
                      BlobReadCallback callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  Read(std::move(blob), std::move(callback), /*range=*/std::nullopt);
}

BlobReader::~BlobReader() { DCHECK_CURRENTLY_ON(content::BrowserThread::UI); }

// static
void BlobReader::Read(mojo::PendingRemote<blink::mojom::Blob> blob,
                      BlobReadCallback callback,
                      std::optional<BlobReader::Range> range) {
  auto reader =
      base::WrapUnique(new BlobReader(std::move(blob), std::move(range)));

  // Move the reader to be owned by the callback, so hold onto a temporary
  // pointer to it so we can still call Start on it.
  BlobReader* raw_reader = reader.get();
  base::OnceClosure wrapped = base::BindOnce(
      [](BlobReadCallback callback, std::unique_ptr<BlobReader> reader) {
        std::move(callback).Run(std::move(reader->blob_data_),
                                *reader->blob_length_);
      },
      std::move(callback), std::move(reader));
  raw_reader->Start(std::move(wrapped));
}

BlobReader::BlobReader(mojo::PendingRemote<blink::mojom::Blob> blob,
                       std::optional<Range> range)
    : blob_(std::move(blob)), read_range_(std::move(range)) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  blob_.set_disconnect_handler(
      base::BindOnce(&BlobReader::Failed, base::Unretained(this)));
}

void BlobReader::Start(base::OnceClosure callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  callback_ = std::move(callback);

  mojo::ScopedDataPipeProducerHandle producer_handle;
  mojo::ScopedDataPipeConsumerHandle consumer_handle;
  MojoResult result = CreateDataPipe(nullptr, producer_handle, consumer_handle);
  if (result != MOJO_RESULT_OK) {
    Failed();
    return;
  }
  if (read_range_) {
    blob_->ReadRange(read_range_->offset, read_range_->length,
                     std::move(producer_handle),
                     receiver_.BindNewPipeAndPassRemote());
  } else {
    blob_->ReadAll(std::move(producer_handle),
                   receiver_.BindNewPipeAndPassRemote());
  }
  data_pipe_drainer_ =
      std::make_unique<mojo::DataPipeDrainer>(this, std::move(consumer_handle));
}

void BlobReader::OnCalculatedSize(uint64_t total_size,
                                  uint64_t expected_content_size) {
  blob_length_ = total_size;
  if (data_complete_) {
    Succeeded();
  }
}

void BlobReader::OnDataAvailable(base::span<const uint8_t> data) {
  blob_data_.append(base::as_string_view(data));
}

void BlobReader::OnDataComplete() {
  data_complete_ = true;
  if (blob_length_) {
    Succeeded();
  }
}

void BlobReader::Failed() {
  blob_length_ = 0;
  std::move(callback_).Run();
}

void BlobReader::Succeeded() {
  std::move(callback_).Run();
}