forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument_loader.cc
392 lines (339 loc) · 12 KB
/
document_loader.cc
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// Copyright (c) 2010 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 "pdf/document_loader.h"
#include <stddef.h>
#include <stdint.h>
#include <algorithm>
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/numerics/safe_math.h"
#include "base/strings/string_util.h"
#include "pdf/url_loader_wrapper.h"
#include "ppapi/c/pp_errors.h"
#include "ui/gfx/range/range.h"
namespace chrome_pdf {
namespace {
// The distance from last received chunk, when we wait requesting data, using
// current connection (like playing a cassette tape) and do not send new range
// request (like rewind a cassette tape, and continue playing after).
// Experimentally chosen value.
const int kChunkCloseDistance = 10;
bool IsValidContentType(const std::string& type) {
return (base::EndsWith(type, "/pdf", base::CompareCase::INSENSITIVE_ASCII) ||
base::EndsWith(type, ".pdf", base::CompareCase::INSENSITIVE_ASCII) ||
base::EndsWith(type, "/x-pdf",
base::CompareCase::INSENSITIVE_ASCII) ||
base::EndsWith(type, "/*", base::CompareCase::INSENSITIVE_ASCII) ||
base::EndsWith(type, "/acrobat",
base::CompareCase::INSENSITIVE_ASCII) ||
base::EndsWith(type, "/unknown",
base::CompareCase::INSENSITIVE_ASCII));
}
} // namespace
DocumentLoader::Client::~Client() {
}
DocumentLoader::Chunk::Chunk() {}
DocumentLoader::Chunk::~Chunk() {}
void DocumentLoader::Chunk::Clear() {
chunk_index = 0;
data_size = 0;
chunk_data.reset();
}
DocumentLoader::DocumentLoader(Client* client)
: client_(client), loader_factory_(this) {}
DocumentLoader::~DocumentLoader() {
}
bool DocumentLoader::Init(std::unique_ptr<URLLoaderWrapper> loader,
const std::string& url) {
DCHECK(url_.empty());
DCHECK(!loader_);
std::string type = loader->GetContentType();
// This happens for PDFs not loaded from http(s) sources.
if (type == "text/plain") {
if (!base::StartsWith(url, "http://",
base::CompareCase::INSENSITIVE_ASCII) &&
!base::StartsWith(url, "https://",
base::CompareCase::INSENSITIVE_ASCII)) {
type = "application/pdf";
}
}
if (!type.empty() && !IsValidContentType(type))
return false;
if (base::StartsWith(loader->GetContentDisposition(), "attachment",
base::CompareCase::INSENSITIVE_ASCII))
return false;
url_ = url;
loader_ = std::move(loader);
if (!loader_->IsContentEncoded()) {
chunk_stream_.set_eof_pos(std::max(0, loader_->GetContentLength()));
}
int64_t bytes_received = 0;
int64_t total_bytes_to_be_received = 0;
if (!chunk_stream_.eof_pos() &&
loader_->GetDownloadProgress(&bytes_received,
&total_bytes_to_be_received)) {
chunk_stream_.set_eof_pos(
std::max(0, static_cast<int>(total_bytes_to_be_received)));
}
SetPartialLoadingEnabled(
partial_loading_enabled_ &&
!base::StartsWith(url, "file://", base::CompareCase::INSENSITIVE_ASCII) &&
loader_->IsAcceptRangesBytes() && !loader_->IsContentEncoded() &&
GetDocumentSize());
ReadMore();
return true;
}
bool DocumentLoader::IsDocumentComplete() const {
return chunk_stream_.IsComplete();
}
uint32_t DocumentLoader::GetDocumentSize() const {
return chunk_stream_.eof_pos();
}
void DocumentLoader::ClearPendingRequests() {
pending_requests_.Clear();
}
bool DocumentLoader::GetBlock(uint32_t position,
uint32_t size,
void* buf) const {
base::CheckedNumeric<uint32_t> addition_result = position;
addition_result += size;
if (!addition_result.IsValid())
return false;
return chunk_stream_.ReadData(
gfx::Range(position, addition_result.ValueOrDie()), buf);
}
bool DocumentLoader::IsDataAvailable(uint32_t position, uint32_t size) const {
base::CheckedNumeric<uint32_t> addition_result = position;
addition_result += size;
if (!addition_result.IsValid())
return false;
return chunk_stream_.IsRangeAvailable(
gfx::Range(position, addition_result.ValueOrDie()));
}
void DocumentLoader::RequestData(uint32_t position, uint32_t size) {
if (!size || IsDataAvailable(position, size)) {
return;
}
{
// Check integer overflow.
base::CheckedNumeric<uint32_t> addition_result = position;
addition_result += size;
if (!addition_result.IsValid())
return;
}
if (GetDocumentSize() && (position + size > GetDocumentSize())) {
return;
}
// We have some artefact request from
// PDFiumEngine::OnDocumentComplete() -> FPDFAvail_IsPageAvail after
// document is complete.
// We need this fix in PDFIum. Adding this as a work around.
// Bug: http://code.google.com/p/chromium/issues/detail?id=79996
// Test url:
// http://www.icann.org/en/correspondence/holtzman-to-jeffrey-02mar11-en.pdf
if (!loader_)
return;
RangeSet requested_chunks(chunk_stream_.GetChunksRange(position, size));
requested_chunks.Subtract(chunk_stream_.filled_chunks());
if (requested_chunks.IsEmpty()) {
NOTREACHED();
return;
}
pending_requests_.Union(requested_chunks);
}
void DocumentLoader::SetPartialLoadingEnabled(bool enabled) {
partial_loading_enabled_ = enabled;
if (!enabled) {
is_partial_loader_active_ = false;
}
}
bool DocumentLoader::ShouldCancelLoading() const {
if (!loader_)
return true;
if (!partial_loading_enabled_ || pending_requests_.IsEmpty())
return false;
const gfx::Range current_range(chunk_.chunk_index,
chunk_.chunk_index + kChunkCloseDistance);
return !pending_requests_.Intersects(current_range);
}
void DocumentLoader::ContinueDownload() {
if (!ShouldCancelLoading())
return ReadMore();
DCHECK(partial_loading_enabled_);
DCHECK(!IsDocumentComplete());
DCHECK(GetDocumentSize());
const uint32_t range_start =
pending_requests_.IsEmpty() ? 0 : pending_requests_.First().start();
RangeSet candidates_for_request(
gfx::Range(range_start, chunk_stream_.total_chunks_count()));
candidates_for_request.Subtract(chunk_stream_.filled_chunks());
DCHECK(!candidates_for_request.IsEmpty());
gfx::Range next_request = candidates_for_request.First();
if (candidates_for_request.Size() == 1 &&
next_request.length() < kChunkCloseDistance) {
// We have only request at the end, try to enlarge it to improve back order
// reading.
const int additional_chunks_count =
kChunkCloseDistance - next_request.length();
int new_start = std::max(
0, static_cast<int>(next_request.start()) - additional_chunks_count);
candidates_for_request =
RangeSet(gfx::Range(new_start, next_request.end()));
candidates_for_request.Subtract(chunk_stream_.filled_chunks());
next_request = candidates_for_request.Last();
}
loader_.reset();
chunk_.Clear();
if (!is_partial_loader_active_) {
client_->CancelBrowserDownload();
is_partial_loader_active_ = true;
}
const uint32_t start = next_request.start() * DataStream::kChunkSize;
const uint32_t length =
std::min(chunk_stream_.eof_pos() - start,
next_request.length() * DataStream::kChunkSize);
loader_ = client_->CreateURLLoader();
loader_->OpenRange(
url_, url_, start, length,
loader_factory_.NewCallback(&DocumentLoader::DidOpenPartial));
}
void DocumentLoader::DidOpenPartial(int32_t result) {
if (result != PP_OK) {
return ReadComplete();
}
int32_t http_code = loader_->GetStatusCode();
if (http_code >= 400 && http_code < 500) {
// Error accessing resource. 4xx error indicate subsequent requests
// will fail too.
// E.g. resource has been removed from the server while loading it.
return ReadComplete();
}
// Leave position untouched for multiparted responce for now, when we read the
// data we'll get it.
if (!loader_->IsMultipart()) {
// Need to make sure that the server returned a byte-range, since it's
// possible for a server to just ignore our byte-range request and just
// return the entire document even if it supports byte-range requests.
// i.e. sniff response to
// http://www.act.org/compass/sample/pdf/geometry.pdf
int start_pos = 0;
int end_pos = 0;
if (loader_->GetByteRange(&start_pos, &end_pos)) {
if (start_pos % DataStream::kChunkSize != 0) {
return ReadComplete();
}
DCHECK(!chunk_.chunk_data);
chunk_.chunk_index = chunk_stream_.GetChunkIndex(start_pos);
} else {
SetPartialLoadingEnabled(false);
}
return ContinueDownload();
}
// Needs more data to calc chunk index.
return ReadMore();
}
void DocumentLoader::ReadMore() {
loader_->ReadResponseBody(
buffer_, sizeof(buffer_),
loader_factory_.NewCallback(&DocumentLoader::DidRead));
}
void DocumentLoader::DidRead(int32_t result) {
if (result < 0) {
// An error occurred.
// The renderer will detect that we're missing data and will display a
// message.
return ReadComplete();
}
if (result == 0) {
loader_.reset();
if (!is_partial_loader_active_)
return ReadComplete();
return ContinueDownload();
}
if (loader_->IsMultipart()) {
int start_pos = 0;
int end_pos = 0;
if (!loader_->GetByteRange(&start_pos, &end_pos)) {
return ReadComplete();
}
DCHECK(!chunk_.chunk_data);
chunk_.chunk_index = chunk_stream_.GetChunkIndex(start_pos);
}
if (!SaveChunkData(buffer_, result)) {
return ReadMore();
}
if (IsDocumentComplete()) {
return ReadComplete();
}
return ContinueDownload();
}
bool DocumentLoader::SaveChunkData(char* input, uint32_t input_size) {
count_of_bytes_received_ += input_size;
bool chunk_saved = false;
bool loading_pending_request = pending_requests_.Contains(chunk_.chunk_index);
while (input_size > 0) {
if (chunk_.data_size == 0) {
chunk_.chunk_data = base::MakeUnique<DataStream::ChunkData>();
}
const uint32_t new_chunk_data_len =
std::min(DataStream::kChunkSize - chunk_.data_size, input_size);
memcpy(chunk_.chunk_data->data() + chunk_.data_size, input,
new_chunk_data_len);
chunk_.data_size += new_chunk_data_len;
if (chunk_.data_size == DataStream::kChunkSize ||
chunk_stream_.eof_pos() ==
chunk_.chunk_index * DataStream::kChunkSize + chunk_.data_size) {
chunk_stream_.SetChunkData(chunk_.chunk_index,
std::move(chunk_.chunk_data));
pending_requests_.Subtract(
gfx::Range(chunk_.chunk_index, chunk_.chunk_index + 1));
chunk_.data_size = 0;
++(chunk_.chunk_index);
chunk_saved = true;
}
input += new_chunk_data_len;
input_size -= new_chunk_data_len;
}
if (IsDocumentComplete())
return true;
if (!chunk_saved)
return false;
if (loading_pending_request &&
!pending_requests_.Contains(chunk_.chunk_index)) {
client_->OnPendingRequestComplete();
}
client_->OnNewDataAvailable();
return true;
}
void DocumentLoader::ReadComplete() {
if (!GetDocumentSize()) {
uint32_t eof =
chunk_.chunk_index * DataStream::kChunkSize + chunk_.data_size;
if (!chunk_stream_.filled_chunks().IsEmpty()) {
eof = std::max(
chunk_stream_.filled_chunks().Last().end() * DataStream::kChunkSize,
eof);
}
chunk_stream_.set_eof_pos(eof);
if (eof == chunk_.chunk_index * DataStream::kChunkSize + chunk_.data_size) {
chunk_stream_.SetChunkData(chunk_.chunk_index,
std::move(chunk_.chunk_data));
}
}
loader_.reset();
if (IsDocumentComplete()) {
client_->OnDocumentComplete();
} else {
client_->OnDocumentCanceled();
}
}
float DocumentLoader::GetProgress() const {
if (!GetDocumentSize())
return -1;
if (IsDocumentComplete())
return 1;
return static_cast<float>(chunk_stream_.filled_chunks_count()) /
chunk_stream_.total_chunks_count();
}
} // namespace chrome_pdf