Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(246)

Side by Side Diff: pdf/url_loader_wrapper_impl.cc

Issue 2349753003: Improve linearized pdf load/show time. (Closed)
Patch Set: fix review issues and compilation. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« pdf/timer.h ('K') | « pdf/url_loader_wrapper_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "pdf/url_loader_wrapper_impl.h"
6
7 #include "base/logging.h"
8 #include "base/memory/ptr_util.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/stringprintf.h"
11 #include "net/http/http_util.h"
12 #include "pdf/timer.h"
13 #include "ppapi/c/pp_errors.h"
14 #include "ppapi/cpp/logging.h"
15 #include "ppapi/cpp/url_request_info.h"
16 #include "ppapi/cpp/url_response_info.h"
17
18 namespace chrome_pdf {
19
20 namespace {
21 // We should read with delay to prevent block UI thread, and reduce CPU usage.
22 const int kReadDelayMs = 2;
23
24 pp::URLRequestInfo MakeRangeRequest(pp::Instance* plugin_instance,
25 const std::string& url,
26 const std::string& referrer_url,
27 uint32_t position,
28 uint32_t size) {
29 pp::URLRequestInfo request(plugin_instance);
30 request.SetURL(url);
31 request.SetMethod("GET");
32 request.SetFollowRedirects(false);
33 request.SetCustomReferrerURL(referrer_url);
34
35 // According to rfc2616, byte range specifies position of the first and last
36 // bytes in the requested range inclusively. Therefore we should subtract 1
37 // from the position + size, to get index of the last byte that needs to be
38 // downloaded.
39 std::string str_header =
40 base::StringPrintf("Range: bytes=%d-%d", position, position + size - 1);
41 pp::Var header(str_header.c_str());
42 request.SetHeaders(header);
43
44 return request;
45 }
46
47 bool GetByteRangeFromStr(const std::string& content_range_str,
48 int* start,
49 int* end) {
50 std::string range = content_range_str;
51 if (!base::StartsWith(range, "bytes", base::CompareCase::INSENSITIVE_ASCII))
52 return false;
53
54 range = range.substr(strlen("bytes"));
55 std::string::size_type pos = range.find('-');
56 std::string range_end;
57 if (pos != std::string::npos)
58 range_end = range.substr(pos + 1);
59 base::TrimWhitespaceASCII(range, base::TRIM_LEADING, &range);
60 base::TrimWhitespaceASCII(range_end, base::TRIM_LEADING, &range_end);
61 *start = atoi(range.c_str());
62 *end = atoi(range_end.c_str());
63 return true;
64 }
65
66 // If the headers have a byte-range response, writes the start and end
67 // positions and returns true if at least the start position was parsed.
68 // The end position will be set to 0 if it was not found or parsed from the
69 // response.
70 // Returns false if not even a start position could be parsed.
71 bool GetByteRangeFromHeaders(const std::string& headers, int* start, int* end) {
72 net::HttpUtil::HeadersIterator it(headers.begin(), headers.end(), "\n");
73 while (it.GetNext()) {
74 if (base::LowerCaseEqualsASCII(it.name(), "content-range")) {
75 if (GetByteRangeFromStr(it.values().c_str(), start, end))
76 return true;
77 }
78 }
79 return false;
80 }
81
82 } // namespace
83
84 class URLLoaderWrapperImpl::ReadStarter : public Timer {
85 public:
86 explicit ReadStarter(URLLoaderWrapperImpl* owner)
87 : Timer(kReadDelayMs), owner_(owner) {}
88 ~ReadStarter() override {}
89
90 // Timer overrides:
91 void OnTimer() override { owner_->ReadResponseBodyImpl(); }
92
93 private:
94 URLLoaderWrapperImpl* owner_;
95 };
96
97 URLLoaderWrapperImpl::URLLoaderWrapperImpl(pp::Instance* plugin_instance,
98 const pp::URLLoader& url_loader)
99 : plugin_instance_(plugin_instance),
100 url_loader_(url_loader),
101 callback_factory_(this) {
102 SetHeadersFromLoader();
103 }
104
105 URLLoaderWrapperImpl::~URLLoaderWrapperImpl() {
106 Close();
107 }
108
109 int URLLoaderWrapperImpl::GetContentLength() const {
110 return content_length_;
111 }
112
113 bool URLLoaderWrapperImpl::IsAcceptRangesBytes() const {
114 return accept_ranges_bytes_;
115 }
116
117 bool URLLoaderWrapperImpl::IsContentEncoded() const {
118 return content_encoded_;
119 }
120
121 std::string URLLoaderWrapperImpl::GetContentType() const {
122 return content_type_;
123 }
124 std::string URLLoaderWrapperImpl::GetContentDisposition() const {
125 return content_disposition_;
126 }
127
128 int URLLoaderWrapperImpl::GetStatusCode() const {
129 return url_loader_.GetResponseInfo().GetStatusCode();
130 }
131
132 bool URLLoaderWrapperImpl::IsMultipart() const {
133 return is_multipart_;
134 }
135
136 bool URLLoaderWrapperImpl::GetByteRange(int* start, int* end) const {
137 DCHECK(start);
138 DCHECK(end);
139 *start = byte_range_.start();
140 *end = byte_range_.end();
141 return byte_range_.IsValid();
142 }
143
144 bool URLLoaderWrapperImpl::GetDownloadProgress(
145 int64_t* bytes_received,
146 int64_t* total_bytes_to_be_received) const {
147 return url_loader_.GetDownloadProgress(bytes_received,
148 total_bytes_to_be_received);
149 }
150
151 void URLLoaderWrapperImpl::Close() {
152 url_loader_.Close();
153 read_starter_.reset();
154 }
155
156 void URLLoaderWrapperImpl::OpenRange(const std::string& url,
157 const std::string& referrer_url,
158 uint32_t position,
159 uint32_t size,
160 const pp::CompletionCallback& cc) {
161 did_open_callback_ = cc;
162 pp::CompletionCallback callback =
163 callback_factory_.NewCallback(&URLLoaderWrapperImpl::DidOpen);
164 int rv = url_loader_.Open(
165 MakeRangeRequest(plugin_instance_, url, referrer_url, position, size),
166 callback);
167 if (rv != PP_OK_COMPLETIONPENDING)
168 callback.Run(rv);
169 }
170
171 void URLLoaderWrapperImpl::ReadResponseBody(char* buffer,
172 int buffer_size,
173 const pp::CompletionCallback& cc) {
174 did_read_callback_ = cc;
175 buffer_ = buffer;
176 buffer_size_ = buffer_size;
177 read_starter_ = base::MakeUnique<ReadStarter>(this);
178 }
179
180 void URLLoaderWrapperImpl::ReadResponseBodyImpl() {
181 read_starter_.reset();
182 pp::CompletionCallback callback =
183 callback_factory_.NewCallback(&URLLoaderWrapperImpl::DidRead);
184 int rv = url_loader_.ReadResponseBody(buffer_, buffer_size_, callback);
185 if (rv != PP_OK_COMPLETIONPENDING) {
186 callback.Run(rv);
187 }
188 }
189
190 void URLLoaderWrapperImpl::SetResponseHeaders(
191 const std::string& response_headers) {
192 response_headers_ = response_headers;
193 ParseHeaders();
194 }
195
196 void URLLoaderWrapperImpl::ParseHeaders() {
197 content_length_ = -1;
198 accept_ranges_bytes_ = false;
199 content_encoded_ = false;
200 content_type_.clear();
201 content_disposition_.clear();
202 multipart_boundary_.clear();
203 byte_range_ = gfx::Range::InvalidRange();
204 is_multipart_ = false;
205
206 if (response_headers_.empty())
207 return;
208
209 net::HttpUtil::HeadersIterator it(response_headers_.begin(),
210 response_headers_.end(), "\n");
211 while (it.GetNext()) {
212 if (base::LowerCaseEqualsASCII(it.name(), "content-length")) {
213 content_length_ = atoi(it.values().c_str());
214 } else if (base::LowerCaseEqualsASCII(it.name(), "accept-ranges")) {
215 accept_ranges_bytes_ = base::LowerCaseEqualsASCII(it.values(), "bytes");
216 } else if (base::LowerCaseEqualsASCII(it.name(), "content-encoding")) {
217 content_encoded_ = true;
218 } else if (base::LowerCaseEqualsASCII(it.name(), "content-type")) {
219 content_type_ = it.values();
220 size_t semi_colon_pos = content_type_.find(';');
221 if (semi_colon_pos != std::string::npos) {
222 content_type_ = content_type_.substr(0, semi_colon_pos);
223 }
224 base::TrimWhitespaceASCII(content_type_, base::TRIM_ALL, &content_type_);
225 // multipart boundary.
226 std::string type = base::ToLowerASCII(it.values());
227 if (base::StartsWith(type, "multipart/", base::CompareCase::SENSITIVE)) {
228 const char* boundary = strstr(type.c_str(), "boundary=");
229 DCHECK(boundary);
230 if (boundary) {
231 multipart_boundary_ = std::string(boundary + 9);
232 is_multipart_ = !multipart_boundary_.empty();
233 }
234 }
235 } else if (base::LowerCaseEqualsASCII(it.name(), "content-disposition")) {
236 content_disposition_ = it.values();
237 } else if (base::LowerCaseEqualsASCII(it.name(), "content-range")) {
238 int start = 0;
239 int end = 0;
240 if (GetByteRangeFromStr(it.values().c_str(), &start, &end)) {
241 byte_range_ = gfx::Range(start, end);
242 }
243 }
244 }
245 }
246
247 void URLLoaderWrapperImpl::DidOpen(int32_t result) {
248 SetHeadersFromLoader();
249 did_open_callback_.Run(result);
250 }
251
252 void URLLoaderWrapperImpl::DidRead(int32_t result) {
253 if (multi_part_processed_) {
254 // Reset this flag so we look inside the buffer in calls of DidRead for this
255 // response only once. Note that this code DOES NOT handle multi part
256 // responses with more than one part (we don't issue them at the moment, so
257 // they shouldn't arrive).
258 is_multipart_ = false;
259 }
260 if (result <= 0 || !is_multipart_) {
261 did_read_callback_.Run(result);
262 return;
263 }
264 if (result <= 2) {
265 // TODO(art-snake): Accumulate data for parse headers.
266 did_read_callback_.Run(result);
267 return;
268 }
269
270 char* start = buffer_;
271 size_t length = result;
272 multi_part_processed_ = true;
273 for (int i = 2; i < result; ++i) {
274 if ((buffer_[i - 1] == '\n' && buffer_[i - 2] == '\n') ||
275 (i >= 4 && buffer_[i - 1] == '\n' && buffer_[i - 2] == '\r' &&
276 buffer_[i - 3] == '\n' && buffer_[i - 4] == '\r')) {
277 int start_pos = 0;
278 int end_pos = 0;
279 if (GetByteRangeFromHeaders(std::string(buffer_, i), &start_pos,
280 &end_pos)) {
281 byte_range_ = gfx::Range(start_pos, end_pos);
282 start += i;
283 length -= i;
284 }
285 break;
286 }
287 }
288 result = length;
289 if (result == 0) {
290 // Continue receiving.
291 return ReadResponseBodyImpl();
292 }
293 DCHECK(result > 0);
294 memmove(buffer_, start, result);
295
296 did_read_callback_.Run(result);
297 }
298
299 void URLLoaderWrapperImpl::SetHeadersFromLoader() {
300 pp::URLResponseInfo response = url_loader_.GetResponseInfo();
301 pp::Var headers_var = response.GetHeaders();
302
303 SetResponseHeaders(headers_var.is_string() ? headers_var.AsString() : "");
304 }
305
306 } // namespace chrome_pdf
OLDNEW
« pdf/timer.h ('K') | « pdf/url_loader_wrapper_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698