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

Side by Side Diff: pdf/url_loader_wrapper_impl.cc

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