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
|
// Copyright 2019 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.
#include "services/data_decoder/web_bundle_parser_factory.h"
#include "base/bind_helpers.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/http/http_util.h"
#include "services/data_decoder/web_bundle_parser.h"
namespace data_decoder {
namespace {
class FileDataSource final : public mojom::BundleDataSource {
public:
FileDataSource(mojo::PendingReceiver<mojom::BundleDataSource> receiver,
base::File file)
: receiver_(this, std::move(receiver)), file_(std::move(file)) {
receiver_.set_disconnect_handler(base::BindOnce(
&base::DeletePointer<FileDataSource>, base::Unretained(this)));
}
private:
// Implements mojom::BundleDataSource.
void GetSize(GetSizeCallback callback) override {
std::move(callback).Run(file_.GetLength());
}
void Read(uint64_t offset, uint64_t length, ReadCallback callback) override {
std::vector<uint8_t> buf(length);
uint64_t bytes =
file_.Read(offset, reinterpret_cast<char*>(buf.data()), length);
if (bytes != length)
std::move(callback).Run(base::nullopt);
else
std::move(callback).Run(std::move(buf));
}
mojo::Receiver<mojom::BundleDataSource> receiver_;
base::File file_;
DISALLOW_COPY_AND_ASSIGN(FileDataSource);
};
} // namespace
WebBundleParserFactory::WebBundleParserFactory() = default;
WebBundleParserFactory::~WebBundleParserFactory() = default;
std::unique_ptr<mojom::BundleDataSource>
WebBundleParserFactory::CreateFileDataSourceForTesting(
mojo::PendingReceiver<mojom::BundleDataSource> receiver,
base::File file) {
return std::make_unique<FileDataSource>(std::move(receiver), std::move(file));
}
void WebBundleParserFactory::GetParserForFile(
mojo::PendingReceiver<mojom::WebBundleParser> receiver,
base::File file) {
mojo::PendingRemote<mojom::BundleDataSource> remote_data_source;
auto data_source = std::make_unique<FileDataSource>(
remote_data_source.InitWithNewPipeAndPassReceiver(), std::move(file));
GetParserForDataSource(std::move(receiver), std::move(remote_data_source));
// |data_source| will be destructed on |remote_data_source| destruction.
data_source.release();
}
void WebBundleParserFactory::GetParserForDataSource(
mojo::PendingReceiver<mojom::WebBundleParser> receiver,
mojo::PendingRemote<mojom::BundleDataSource> data_source) {
auto parser = std::make_unique<WebBundleParser>(std::move(receiver),
std::move(data_source));
// |parser| will be destructed on remote mojo ends' disconnection.
parser.release();
}
} // namespace data_decoder
|