-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathplaylist_data_source.cc
More file actions
315 lines (271 loc) · 10.2 KB
/
Copy pathplaylist_data_source.cc
File metadata and controls
315 lines (271 loc) · 10.2 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "brave/browser/playlist/playlist_data_source.h"
#include <algorithm>
#include <limits>
#include <memory>
#include <utility>
#include <vector>
#include "base/byte_size.h"
#include "base/check.h"
#include "base/check_op.h"
#include "base/containers/heap_array.h"
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/files/memory_mapped_file.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/memory/ref_counted_memory.h"
#include "base/memory/scoped_refptr.h"
#include "base/notreached.h"
#include "base/strings/escape.h"
#include "base/strings/string_split.h"
#include "base/task/thread_pool.h"
#include "brave/components/playlist/content/browser/mime_util.h"
#include "brave/components/playlist/content/browser/playlist_service.h"
#include "components/favicon_base/favicon_url_parser.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/url_data_source.h"
#include "net/base/filename_util.h"
#include "url/gurl.h"
namespace playlist {
namespace {
#define CHECK_CURRENTLY_NOT_ON_UI_THREAD() \
CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) \
<< "This must be called on a background thread."
constexpr base::ByteSize kMediaChunkSize = base::MiBU(1); // 1MB
class RefCountedMemMap : public base::RefCountedMemory {
public:
explicit RefCountedMemMap(const base::FilePath& path) {
base::File file = base::File(
path, base::File::Flags::FLAG_OPEN | base::File::Flags::FLAG_READ);
if (!file.IsValid() ||
file.GetLength() > static_cast<int64_t>(base::MiBU(100).InBytes())) {
// In order to avoid OOM crash, limits the file size to 100MB.
return;
}
initialized_ = memory_mapped_file_.Initialize(std::move(file));
}
bool initialized() const { return initialized_; }
private:
~RefCountedMemMap() override = default;
// RefCountedMemory:
base::span<const uint8_t> AsSpan() const LIFETIME_BOUND override {
return memory_mapped_file_.bytes();
}
base::MemoryMappedFile memory_mapped_file_;
bool initialized_ = false;
};
scoped_refptr<base::RefCountedMemory> ReadMemoryMappedFile(
const base::FilePath& path) {
CHECK_CURRENTLY_NOT_ON_UI_THREAD();
auto mem_mapped_file = base::MakeRefCounted<RefCountedMemMap>(path);
if (!mem_mapped_file->initialized()) {
return nullptr;
}
return mem_mapped_file;
}
content::URLDataSource::RangeDataResult ReadFileRange(
const base::FilePath& file_path,
net::HttpByteRange range) {
CHECK_CURRENTLY_NOT_ON_UI_THREAD();
base::File file(file_path,
base::File::Flags::FLAG_OPEN | base::File::FLAG_READ);
if (!file.IsValid()) {
return {};
}
// Note that HTTP range's first and last position are inclusive.
int64_t first_byte_position =
range.HasFirstBytePosition() ? range.first_byte_position() : 0;
auto file_length = file.GetLength();
if (first_byte_position == file_length) {
// It looks like the media player tries to make sure that it's the end of
// file by sending the first byte position as the file size.
content::URLDataSource::RangeDataResult result;
result.buffer = base::MakeRefCounted<base::RefCountedBytes>();
result.file_size = 0;
result.range =
net::HttpByteRange::Bounded(first_byte_position, first_byte_position);
auto mime_type = playlist::mime_util::GetMimeTypeForFileExtension(
file_path.FinalExtension())
.value_or("video/mp4");
result.mime_type = mime_type;
return result;
}
int64_t last_byte_position =
range.HasLastBytePosition()
? range.last_byte_position()
: first_byte_position +
static_cast<int64_t>(kMediaChunkSize.InBytes()) - 1;
int64_t read_size = std::min(static_cast<int64_t>(kMediaChunkSize.InBytes()),
last_byte_position - first_byte_position + 1);
CHECK_GE(read_size, 0);
std::vector<unsigned char> buffer(read_size);
auto read_result = file.Read(first_byte_position, buffer);
if (!read_result.has_value()) {
return {};
}
read_size = read_result.value();
buffer.resize(read_size);
content::URLDataSource::RangeDataResult result;
result.buffer =
base::MakeRefCounted<base::RefCountedBytes>(std::move(buffer));
result.file_size = file_length;
result.range = net::HttpByteRange::Bounded(
first_byte_position, first_byte_position + read_size - 1);
auto mime_type = playlist::mime_util::GetMimeTypeForFileExtension(
file_path.FinalExtension())
.value_or("video/mp4");
result.mime_type = mime_type;
return result;
}
} // namespace
PlaylistDataSource::DataRequest::DataRequest(const GURL& url) {
const auto full_path = content::URLDataSource::URLToRequestPath(url);
const auto paths = base::SplitStringPiece(
full_path, "/", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
if (paths.size() != 2) {
LOG(ERROR) << "Invalid playlist data source URL, might be routed from "
"saved .m3u8 file: "
<< url.spec();
return;
}
id = paths.at(0);
const auto& type_string = paths.at(1);
if (type_string == "thumbnail") {
type = DataRequest::Type::kThumbnail;
} else if (type_string == "media") {
type = DataRequest::Type::kMedia;
} else if (type_string == "favicon") {
type = DataRequest::Type::kFavicon;
} else {
type = DataRequest::Type::kNone;
LOG(ERROR) << "Invalid playlist data source URL, might be routed from "
"saved .m3u8 file: "
<< url.spec();
}
}
PlaylistDataSource::DataRequest::~DataRequest() = default;
PlaylistDataSource::PlaylistDataSource(Profile* profile,
PlaylistService* service)
: FaviconSource(profile, chrome::FaviconUrlFormat::kFavicon2),
service_(service) {}
PlaylistDataSource::~PlaylistDataSource() = default;
std::string PlaylistDataSource::GetSource() {
return "chrome-untrusted://playlist-data/";
}
void PlaylistDataSource::StartDataRequest(
const GURL& url,
const content::WebContents::Getter& wc_getter,
GotDataCallback got_data_callback) {
if (!service_) {
std::move(got_data_callback).Run(nullptr);
return;
}
switch (DataRequest data_request(url); data_request.type) {
case DataRequest::Type::kNone:
std::move(got_data_callback).Run(nullptr);
break;
case DataRequest::Type::kThumbnail:
GetThumbnail(data_request, wc_getter, std::move(got_data_callback));
break;
case DataRequest::Type::kFavicon:
GetFavicon(data_request, wc_getter, std::move(got_data_callback));
break;
case DataRequest::Type::kMedia:
NOTREACHED() << "This request should call StartRangeDataRequest()";
}
}
void PlaylistDataSource::StartRangeDataRequest(
const GURL& url,
const content::WebContents::Getter& wc_getter,
const net::HttpByteRange& range,
GotRangeDataCallback callback) {
DataRequest data_request(url);
if (data_request.type != DataRequest::Type::kMedia || !range.IsValid()) {
std::move(callback).Run({});
return;
}
GetMediaFile(data_request, wc_getter, range, std::move(callback));
}
void PlaylistDataSource::GetThumbnail(
const DataRequest& request,
const content::WebContents::Getter& wc_getter,
GotDataCallback got_data_callback) {
base::FilePath thumbnail_path;
if (!service_->GetThumbnailPath(request.id, &thumbnail_path)) {
std::move(got_data_callback).Run(nullptr);
return;
}
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, base::MayBlock(),
base::BindOnce(&ReadMemoryMappedFile, thumbnail_path),
std::move(got_data_callback));
}
void PlaylistDataSource::GetMediaFile(
const DataRequest& request,
const content::WebContents::Getter& wc_getter,
const net::HttpByteRange& range,
GotRangeDataCallback got_data_callback) {
base::FilePath media_path;
if (!service_->HasPlaylistItem(request.id)) {
std::move(got_data_callback).Run({});
return;
}
auto item = service_->GetPlaylistItem(request.id);
DCHECK(item->cached);
if (!net::FileURLToFilePath(item->media_path, &media_path)) {
std::move(got_data_callback).Run({});
return;
}
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, base::MayBlock(),
base::BindOnce(&ReadFileRange, media_path, range),
std::move(got_data_callback));
}
void PlaylistDataSource::GetFavicon(
const DataRequest& request,
const content::WebContents::Getter& wc_getter,
GotDataCallback got_data_callback) {
if (!service_->HasPlaylistItem(request.id)) {
std::move(got_data_callback).Run(nullptr);
return;
}
auto item = service_->GetPlaylistItem(request.id);
GURL favicon_url(
"chrome://favicon2?allowGoogleServerFallback=0&size=32&pageUrl=" +
base::EscapeUrlEncodedData(item->page_source.spec(),
/*use_plus=*/false));
FaviconSource::StartDataRequest(favicon_url, wc_getter,
std::move(got_data_callback));
}
std::string PlaylistDataSource::GetMimeType(const GURL& url) {
if (url.is_empty()) {
// This could be reached on start up.
return {};
}
switch (DataRequest data_request(url); data_request.type) {
case DataRequest::Type::kThumbnail:
return "image/png";
case DataRequest::Type::kMedia:
return "video/mp4"; // Note that this will be fixed up based on the
// actual file extension in WebUIUrlLoader.
case DataRequest::Type::kFavicon:
return FaviconSource::GetMimeType(url);
case DataRequest::Type::kNone:
return {};
}
}
bool PlaylistDataSource::AllowCaching() {
return false;
}
bool PlaylistDataSource::SupportsRangeRequests(const GURL& url) const {
if (url.is_empty()) {
// This could be reached on start up.
return false;
}
return DataRequest(url).type == DataRequest::Type::kMedia;
}
} // namespace playlist