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" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/debug/alias.h" | 13 #include "base/debug/alias.h" |
14 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
15 #include "base/location.h" | 15 #include "base/location.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
18 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
19 #include "base/memory/ref_counted_memory.h" | 19 #include "base/memory/ref_counted_memory.h" |
20 #include "base/memory/weak_ptr.h" | 20 #include "base/memory/weak_ptr.h" |
21 #include "base/profiler/scoped_tracker.h" | 21 #include "base/profiler/scoped_tracker.h" |
22 #include "base/single_thread_task_runner.h" | 22 #include "base/single_thread_task_runner.h" |
23 #include "base/strings/string_number_conversions.h" | 23 #include "base/strings/string_number_conversions.h" |
24 #include "base/strings/string_util.h" | 24 #include "base/strings/string_util.h" |
25 #include "base/strings/stringprintf.h" | 25 #include "base/strings/stringprintf.h" |
| 26 #include "base/threading/worker_pool.h" |
26 #include "base/trace_event/trace_event.h" | 27 #include "base/trace_event/trace_event.h" |
27 #include "content/browser/blob_storage/chrome_blob_storage_context.h" | 28 #include "content/browser/blob_storage/chrome_blob_storage_context.h" |
28 #include "content/browser/histogram_internals_request_job.h" | 29 #include "content/browser/histogram_internals_request_job.h" |
29 #include "content/browser/net/view_blob_internals_job_factory.h" | 30 #include "content/browser/net/view_blob_internals_job_factory.h" |
30 #include "content/browser/net/view_http_cache_job_factory.h" | 31 #include "content/browser/net/view_http_cache_job_factory.h" |
31 #include "content/browser/resource_context_impl.h" | 32 #include "content/browser/resource_context_impl.h" |
32 #include "content/browser/webui/shared_resources_data_source.h" | 33 #include "content/browser/webui/shared_resources_data_source.h" |
33 #include "content/browser/webui/url_data_source_impl.h" | 34 #include "content/browser/webui/url_data_source_impl.h" |
34 #include "content/public/browser/browser_context.h" | 35 #include "content/public/browser/browser_context.h" |
35 #include "content/public/browser/browser_thread.h" | 36 #include "content/public/browser/browser_thread.h" |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 std::string result; | 100 std::string result; |
100 if (request->extra_request_headers().GetHeader( | 101 if (request->extra_request_headers().GetHeader( |
101 net::HttpRequestHeaders::kOrigin, &result)) | 102 net::HttpRequestHeaders::kOrigin, &result)) |
102 return result; | 103 return result; |
103 net::HttpRequestHeaders headers; | 104 net::HttpRequestHeaders headers; |
104 if (request->GetFullRequestHeaders(&headers)) | 105 if (request->GetFullRequestHeaders(&headers)) |
105 headers.GetHeader(net::HttpRequestHeaders::kOrigin, &result); | 106 headers.GetHeader(net::HttpRequestHeaders::kOrigin, &result); |
106 return result; | 107 return result; |
107 } | 108 } |
108 | 109 |
| 110 // Copy data from source buffer into IO buffer destination. |
| 111 // TODO(groby): Very similar to URLRequestSimpleJob, unify at some point. |
| 112 void CopyData(const scoped_refptr<net::IOBuffer>& buf, |
| 113 int buf_size, |
| 114 const scoped_refptr<base::RefCountedMemory>& data, |
| 115 int64_t data_offset) { |
| 116 // TODO(pkasting): Remove ScopedTracker below once crbug.com/455423 is |
| 117 // fixed. |
| 118 tracked_objects::ScopedTracker tracking_profile( |
| 119 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 120 "455423 URLRequestChromeJob::CompleteRead memcpy")); |
| 121 memcpy(buf->data(), data->front() + data_offset, buf_size); |
| 122 } |
| 123 |
109 } // namespace | 124 } // namespace |
110 | 125 |
111 // URLRequestChromeJob is a net::URLRequestJob that manages running | 126 // URLRequestChromeJob is a net::URLRequestJob that manages running |
112 // chrome-internal resource requests asynchronously. | 127 // chrome-internal resource requests asynchronously. |
113 // It hands off URL requests to ChromeURLDataManager, which asynchronously | 128 // It hands off URL requests to ChromeURLDataManager, which asynchronously |
114 // calls back once the data is available. | 129 // calls back once the data is available. |
115 class URLRequestChromeJob : public net::URLRequestJob { | 130 class URLRequestChromeJob : public net::URLRequestJob { |
116 public: | 131 public: |
117 // |is_incognito| set when job is generated from an incognito profile. | 132 // |is_incognito| set when job is generated from an incognito profile. |
118 URLRequestChromeJob(net::URLRequest* request, | 133 URLRequestChromeJob(net::URLRequest* request, |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 // (This pattern is shared by most net::URLRequestJob implementations.) | 219 // (This pattern is shared by most net::URLRequestJob implementations.) |
205 void StartAsync(); | 220 void StartAsync(); |
206 | 221 |
207 // Due to a race condition, DevTools relies on a legacy thread hop to the UI | 222 // Due to a race condition, DevTools relies on a legacy thread hop to the UI |
208 // thread before calling StartAsync. | 223 // thread before calling StartAsync. |
209 // TODO(caseq): Fix the race condition and remove this thread hop in | 224 // TODO(caseq): Fix the race condition and remove this thread hop in |
210 // https://crbug.com/616641. | 225 // https://crbug.com/616641. |
211 static void DelayStartForDevTools( | 226 static void DelayStartForDevTools( |
212 const base::WeakPtr<URLRequestChromeJob>& job); | 227 const base::WeakPtr<URLRequestChromeJob>& job); |
213 | 228 |
214 // Do the actual copy from data_ (the data we're serving) into |buf|. | 229 // Post a task to copy |data_| to |buf_| on a worker thread, to avoid browser |
215 // Separate from ReadRawData so we can handle async I/O. Returns the number of | 230 // jank. (|data_| might be mem-mapped, so a memcpy can trigger file ops). |
216 // bytes read. | 231 int PostReadTask(scoped_refptr<net::IOBuffer> buf, int buf_size); |
217 int CompleteRead(net::IOBuffer* buf, int buf_size); | |
218 | 232 |
219 // 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. |
220 scoped_refptr<base::RefCountedMemory> data_; | 234 scoped_refptr<base::RefCountedMemory> data_; |
221 // The current offset into the data that we're handing off to our | 235 // The current offset into the data that we're handing off to our |
222 // callers via the Read interfaces. | 236 // callers via the Read interfaces. |
223 int data_offset_; | 237 int data_offset_; |
224 | 238 |
225 // For async reads, we keep around a pointer to the buffer that | 239 // For async reads, we keep around a pointer to the buffer that |
226 // we're reading into. | 240 // we're reading into. |
227 scoped_refptr<net::IOBuffer> pending_buf_; | 241 scoped_refptr<net::IOBuffer> pending_buf_; |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 return is_gzipped_ ? net::Filter::GZipFactory() : nullptr; | 390 return is_gzipped_ ? net::Filter::GZipFactory() : nullptr; |
377 } | 391 } |
378 | 392 |
379 void URLRequestChromeJob::MimeTypeAvailable(const std::string& mime_type) { | 393 void URLRequestChromeJob::MimeTypeAvailable(const std::string& mime_type) { |
380 set_mime_type(mime_type); | 394 set_mime_type(mime_type); |
381 NotifyHeadersComplete(); | 395 NotifyHeadersComplete(); |
382 } | 396 } |
383 | 397 |
384 void URLRequestChromeJob::DataAvailable(base::RefCountedMemory* bytes) { | 398 void URLRequestChromeJob::DataAvailable(base::RefCountedMemory* bytes) { |
385 TRACE_EVENT_ASYNC_END0("browser", "DataManager:Request", this); | 399 TRACE_EVENT_ASYNC_END0("browser", "DataManager:Request", this); |
386 if (bytes) { | 400 DCHECK(!data_); |
387 data_ = bytes; | 401 |
388 if (pending_buf_.get()) { | 402 // A passed-in nullptr signals an error. |
389 CHECK(pending_buf_->data()); | 403 if (!bytes) { |
390 int result = CompleteRead(pending_buf_.get(), pending_buf_size_); | 404 ReadRawDataComplete(net::ERR_FAILED); |
391 pending_buf_ = nullptr; | 405 return; |
| 406 } |
| 407 |
| 408 // All further requests will be satisfied from the passed-in data. |
| 409 data_ = bytes; |
| 410 |
| 411 if (pending_buf_) { |
| 412 int result = PostReadTask(pending_buf_, pending_buf_size_); |
| 413 pending_buf_ = nullptr; |
| 414 if (result != net::ERR_IO_PENDING) |
392 ReadRawDataComplete(result); | 415 ReadRawDataComplete(result); |
393 } | |
394 } else { | |
395 // The request failed. | |
396 ReadRawDataComplete(net::ERR_FAILED); | |
397 } | 416 } |
398 } | 417 } |
399 | 418 |
400 base::WeakPtr<URLRequestChromeJob> URLRequestChromeJob::AsWeakPtr() { | 419 base::WeakPtr<URLRequestChromeJob> URLRequestChromeJob::AsWeakPtr() { |
401 return weak_factory_.GetWeakPtr(); | 420 return weak_factory_.GetWeakPtr(); |
402 } | 421 } |
403 | 422 |
404 int URLRequestChromeJob::ReadRawData(net::IOBuffer* buf, int buf_size) { | 423 int URLRequestChromeJob::ReadRawData(net::IOBuffer* buf, int buf_size) { |
| 424 DCHECK(!pending_buf_.get()); |
| 425 |
| 426 // If data isn't available yet, mark this as asynchronous. |
405 if (!data_.get()) { | 427 if (!data_.get()) { |
406 DCHECK(!pending_buf_.get()); | |
407 CHECK(buf->data()); | |
408 pending_buf_ = buf; | 428 pending_buf_ = buf; |
409 pending_buf_size_ = buf_size; | 429 pending_buf_size_ = buf_size; |
410 return net::ERR_IO_PENDING; | 430 return net::ERR_IO_PENDING; |
411 } | 431 } |
412 | 432 |
413 // Otherwise, the data is available. | 433 return PostReadTask(buf, buf_size); |
414 return CompleteRead(buf, buf_size); | |
415 } | 434 } |
416 | 435 |
417 int URLRequestChromeJob::CompleteRead(net::IOBuffer* buf, int buf_size) { | 436 int URLRequestChromeJob::PostReadTask(scoped_refptr<net::IOBuffer> buf, |
| 437 int buf_size) { |
| 438 DCHECK(buf); |
| 439 DCHECK(data_); |
| 440 CHECK(buf->data()); |
| 441 |
418 int remaining = data_->size() - data_offset_; | 442 int remaining = data_->size() - data_offset_; |
419 if (buf_size > remaining) | 443 if (buf_size > remaining) |
420 buf_size = remaining; | 444 buf_size = remaining; |
421 if (buf_size > 0) { | 445 |
422 // TODO(pkasting): Remove ScopedTracker below once crbug.com/455423 is | 446 if (buf_size == 0) |
423 // fixed. | 447 return 0; |
424 tracked_objects::ScopedTracker tracking_profile( | 448 |
425 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 449 base::WorkerPool::GetTaskRunner(false)->PostTaskAndReply( |
426 "455423 URLRequestChromeJob::CompleteRead memcpy")); | 450 FROM_HERE, base::Bind(&CopyData, base::RetainedRef(buf), buf_size, data_, |
427 memcpy(buf->data(), data_->front() + data_offset_, buf_size); | 451 data_offset_), |
428 data_offset_ += buf_size; | 452 base::Bind(&URLRequestChromeJob::ReadRawDataComplete, AsWeakPtr(), |
429 } | 453 buf_size)); |
430 return buf_size; | 454 data_offset_ += buf_size; |
| 455 |
| 456 return net::ERR_IO_PENDING; |
431 } | 457 } |
432 | 458 |
433 void URLRequestChromeJob::DelayStartForDevTools( | 459 void URLRequestChromeJob::DelayStartForDevTools( |
434 const base::WeakPtr<URLRequestChromeJob>& job) { | 460 const base::WeakPtr<URLRequestChromeJob>& job) { |
435 BrowserThread::PostTask( | 461 BrowserThread::PostTask( |
436 BrowserThread::IO, | 462 BrowserThread::IO, |
437 FROM_HERE, | 463 FROM_HERE, |
438 base::Bind(&URLRequestChromeJob::StartAsync, job)); | 464 base::Bind(&URLRequestChromeJob::StartAsync, job)); |
439 } | 465 } |
440 | 466 |
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
809 | 835 |
810 } // namespace | 836 } // namespace |
811 | 837 |
812 net::URLRequestJobFactory::ProtocolHandler* CreateDevToolsProtocolHandler( | 838 net::URLRequestJobFactory::ProtocolHandler* CreateDevToolsProtocolHandler( |
813 ResourceContext* resource_context, | 839 ResourceContext* resource_context, |
814 bool is_incognito) { | 840 bool is_incognito) { |
815 return new DevToolsJobFactory(resource_context, is_incognito); | 841 return new DevToolsJobFactory(resource_context, is_incognito); |
816 } | 842 } |
817 | 843 |
818 } // namespace content | 844 } // namespace content |
OLD | NEW |