OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/webui/url_data_manager_backend.h" | 5 #include "content/browser/webui/url_data_manager_backend.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 int ReadRawData(net::IOBuffer* buf, int buf_size) override; | 141 int ReadRawData(net::IOBuffer* buf, int buf_size) override; |
142 bool GetMimeType(std::string* mime_type) const override; | 142 bool GetMimeType(std::string* mime_type) const override; |
143 int GetResponseCode() const override; | 143 int GetResponseCode() const override; |
144 void GetResponseInfo(net::HttpResponseInfo* info) override; | 144 void GetResponseInfo(net::HttpResponseInfo* info) override; |
145 std::unique_ptr<net::Filter> SetupFilter() const override; | 145 std::unique_ptr<net::Filter> SetupFilter() const override; |
146 | 146 |
147 // Used to notify that the requested data's |mime_type| is ready. | 147 // Used to notify that the requested data's |mime_type| is ready. |
148 void MimeTypeAvailable(const std::string& mime_type); | 148 void MimeTypeAvailable(const std::string& mime_type); |
149 | 149 |
150 // Called by ChromeURLDataManager to notify us that the data blob is ready | 150 // Called by ChromeURLDataManager to notify us that the data blob is ready |
151 // for us. | 151 // for us. |bytes| may be null, indicating an error. |
152 void DataAvailable(base::RefCountedMemory* bytes); | 152 void DataAvailable(base::RefCountedMemory* bytes); |
153 | 153 |
154 // Returns a weak pointer to the job. | 154 // Returns a weak pointer to the job. |
155 base::WeakPtr<URLRequestChromeJob> AsWeakPtr(); | 155 base::WeakPtr<URLRequestChromeJob> AsWeakPtr(); |
156 | 156 |
157 void set_mime_type(const std::string& mime_type) { | 157 void set_mime_type(const std::string& mime_type) { |
158 mime_type_ = mime_type; | 158 mime_type_ = mime_type; |
159 } | 159 } |
160 | 160 |
161 void set_allow_caching(bool allow_caching) { | 161 void set_allow_caching(bool allow_caching) { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 // https://crbug.com/616641. | 225 // https://crbug.com/616641. |
226 static void DelayStartForDevTools( | 226 static void DelayStartForDevTools( |
227 const base::WeakPtr<URLRequestChromeJob>& job); | 227 const base::WeakPtr<URLRequestChromeJob>& job); |
228 | 228 |
229 // Post a task to copy |data_| to |buf_| on a worker thread, to avoid browser | 229 // Post a task to copy |data_| to |buf_| on a worker thread, to avoid browser |
230 // jank. (|data_| might be mem-mapped, so a memcpy can trigger file ops). | 230 // jank. (|data_| might be mem-mapped, so a memcpy can trigger file ops). |
231 int PostReadTask(scoped_refptr<net::IOBuffer> buf, int buf_size); | 231 int PostReadTask(scoped_refptr<net::IOBuffer> buf, int buf_size); |
232 | 232 |
233 // The actual data we're serving. NULL until it's been fetched. | 233 // The actual data we're serving. NULL until it's been fetched. |
234 scoped_refptr<base::RefCountedMemory> data_; | 234 scoped_refptr<base::RefCountedMemory> data_; |
| 235 |
235 // The current offset into the data that we're handing off to our | 236 // The current offset into the data that we're handing off to our |
236 // callers via the Read interfaces. | 237 // callers via the Read interfaces. |
237 int data_offset_; | 238 int data_offset_; |
238 | 239 |
| 240 // When DataAvailable() is called with a null argument, indicating an error, |
| 241 // this is set accordingly to a code for ReadRawData() to return. |
| 242 net::Error data_available_status_; |
| 243 |
239 // For async reads, we keep around a pointer to the buffer that | 244 // For async reads, we keep around a pointer to the buffer that |
240 // we're reading into. | 245 // we're reading into. |
241 scoped_refptr<net::IOBuffer> pending_buf_; | 246 scoped_refptr<net::IOBuffer> pending_buf_; |
242 int pending_buf_size_; | 247 int pending_buf_size_; |
243 std::string mime_type_; | 248 std::string mime_type_; |
244 | 249 |
245 // If true, set a header in the response to prevent it from being cached. | 250 // If true, set a header in the response to prevent it from being cached. |
246 bool allow_caching_; | 251 bool allow_caching_; |
247 | 252 |
248 // If true, set the Content Security Policy (CSP) header. | 253 // If true, set the Content Security Policy (CSP) header. |
(...skipping 30 matching lines...) Expand all Loading... |
279 | 284 |
280 DISALLOW_COPY_AND_ASSIGN(URLRequestChromeJob); | 285 DISALLOW_COPY_AND_ASSIGN(URLRequestChromeJob); |
281 }; | 286 }; |
282 | 287 |
283 URLRequestChromeJob::URLRequestChromeJob(net::URLRequest* request, | 288 URLRequestChromeJob::URLRequestChromeJob(net::URLRequest* request, |
284 net::NetworkDelegate* network_delegate, | 289 net::NetworkDelegate* network_delegate, |
285 URLDataManagerBackend* backend, | 290 URLDataManagerBackend* backend, |
286 bool is_incognito) | 291 bool is_incognito) |
287 : net::URLRequestJob(request, network_delegate), | 292 : net::URLRequestJob(request, network_delegate), |
288 data_offset_(0), | 293 data_offset_(0), |
| 294 data_available_status_(net::OK), |
289 pending_buf_size_(0), | 295 pending_buf_size_(0), |
290 allow_caching_(true), | 296 allow_caching_(true), |
291 add_content_security_policy_(true), | 297 add_content_security_policy_(true), |
292 deny_xframe_options_(true), | 298 deny_xframe_options_(true), |
293 send_content_type_header_(false), | 299 send_content_type_header_(false), |
294 is_incognito_(is_incognito), | 300 is_incognito_(is_incognito), |
295 is_gzipped_(false), | 301 is_gzipped_(false), |
296 backend_(backend), | 302 backend_(backend), |
297 weak_factory_(this) { | 303 weak_factory_(this) { |
298 DCHECK(backend); | 304 DCHECK(backend); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 | 398 |
393 void URLRequestChromeJob::MimeTypeAvailable(const std::string& mime_type) { | 399 void URLRequestChromeJob::MimeTypeAvailable(const std::string& mime_type) { |
394 set_mime_type(mime_type); | 400 set_mime_type(mime_type); |
395 NotifyHeadersComplete(); | 401 NotifyHeadersComplete(); |
396 } | 402 } |
397 | 403 |
398 void URLRequestChromeJob::DataAvailable(base::RefCountedMemory* bytes) { | 404 void URLRequestChromeJob::DataAvailable(base::RefCountedMemory* bytes) { |
399 TRACE_EVENT_ASYNC_END0("browser", "DataManager:Request", this); | 405 TRACE_EVENT_ASYNC_END0("browser", "DataManager:Request", this); |
400 DCHECK(!data_); | 406 DCHECK(!data_); |
401 | 407 |
402 // A passed-in nullptr signals an error. | |
403 if (!bytes) { | |
404 ReadRawDataComplete(net::ERR_FAILED); | |
405 return; | |
406 } | |
407 | |
408 // All further requests will be satisfied from the passed-in data. | 408 // All further requests will be satisfied from the passed-in data. |
409 data_ = bytes; | 409 data_ = bytes; |
| 410 if (!bytes) |
| 411 data_available_status_ = net::ERR_FAILED; |
410 | 412 |
411 if (pending_buf_) { | 413 if (pending_buf_) { |
412 int result = PostReadTask(pending_buf_, pending_buf_size_); | 414 // The request has already been marked async. |
| 415 int result = bytes ? PostReadTask(pending_buf_, pending_buf_size_) |
| 416 : data_available_status_; |
413 pending_buf_ = nullptr; | 417 pending_buf_ = nullptr; |
414 if (result != net::ERR_IO_PENDING) | 418 if (result != net::ERR_IO_PENDING) |
415 ReadRawDataComplete(result); | 419 ReadRawDataComplete(result); |
416 } | 420 } |
417 } | 421 } |
418 | 422 |
419 base::WeakPtr<URLRequestChromeJob> URLRequestChromeJob::AsWeakPtr() { | 423 base::WeakPtr<URLRequestChromeJob> URLRequestChromeJob::AsWeakPtr() { |
420 return weak_factory_.GetWeakPtr(); | 424 return weak_factory_.GetWeakPtr(); |
421 } | 425 } |
422 | 426 |
423 int URLRequestChromeJob::ReadRawData(net::IOBuffer* buf, int buf_size) { | 427 int URLRequestChromeJob::ReadRawData(net::IOBuffer* buf, int buf_size) { |
424 DCHECK(!pending_buf_.get()); | 428 DCHECK(!pending_buf_.get()); |
425 | 429 |
426 // If data isn't available yet, mark this as asynchronous. | 430 // Handle the cases when DataAvailable() has already been called. |
427 if (!data_.get()) { | 431 if (data_available_status_ != net::OK) |
428 pending_buf_ = buf; | 432 return data_available_status_; |
429 pending_buf_size_ = buf_size; | 433 if (data_) |
430 return net::ERR_IO_PENDING; | 434 return PostReadTask(buf, buf_size); |
431 } | |
432 | 435 |
433 return PostReadTask(buf, buf_size); | 436 // DataAvailable() has not been called yet. Mark the request as async. |
| 437 pending_buf_ = buf; |
| 438 pending_buf_size_ = buf_size; |
| 439 return net::ERR_IO_PENDING; |
434 } | 440 } |
435 | 441 |
436 int URLRequestChromeJob::PostReadTask(scoped_refptr<net::IOBuffer> buf, | 442 int URLRequestChromeJob::PostReadTask(scoped_refptr<net::IOBuffer> buf, |
437 int buf_size) { | 443 int buf_size) { |
438 DCHECK(buf); | 444 DCHECK(buf); |
439 DCHECK(data_); | 445 DCHECK(data_); |
440 CHECK(buf->data()); | 446 CHECK(buf->data()); |
441 | 447 |
442 int remaining = data_->size() - data_offset_; | 448 int remaining = data_->size() - data_offset_; |
443 if (buf_size > remaining) | 449 if (buf_size > remaining) |
(...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
835 | 841 |
836 } // namespace | 842 } // namespace |
837 | 843 |
838 net::URLRequestJobFactory::ProtocolHandler* CreateDevToolsProtocolHandler( | 844 net::URLRequestJobFactory::ProtocolHandler* CreateDevToolsProtocolHandler( |
839 ResourceContext* resource_context, | 845 ResourceContext* resource_context, |
840 bool is_incognito) { | 846 bool is_incognito) { |
841 return new DevToolsJobFactory(resource_context, is_incognito); | 847 return new DevToolsJobFactory(resource_context, is_incognito); |
842 } | 848 } |
843 | 849 |
844 } // namespace content | 850 } // namespace content |
OLD | NEW |