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
3 // found in the LICENSE file.
5 #include "content/browser/webui/url_data_manager_backend.h"
9 #include "base/basictypes.h"
10 #include "base/bind.h"
11 #include "base/command_line.h"
12 #include "base/compiler_specific.h"
13 #include "base/debug/alias.h"
14 #include "base/lazy_instance.h"
15 #include "base/location.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/ref_counted_memory.h"
18 #include "base/memory/weak_ptr.h"
19 #include "base/profiler/scoped_tracker.h"
20 #include "base/single_thread_task_runner.h"
21 #include "base/strings/string_util.h"
22 #include "base/strings/stringprintf.h"
23 #include "base/trace_event/trace_event.h"
24 #include "content/browser/appcache/view_appcache_internals_job.h"
25 #include "content/browser/fileapi/chrome_blob_storage_context.h"
26 #include "content/browser/histogram_internals_request_job.h"
27 #include "content/browser/net/view_blob_internals_job_factory.h"
28 #include "content/browser/net/view_http_cache_job_factory.h"
29 #include "content/browser/resource_context_impl.h"
30 #include "content/browser/tcmalloc_internals_request_job.h"
31 #include "content/browser/webui/shared_resources_data_source.h"
32 #include "content/browser/webui/url_data_source_impl.h"
33 #include "content/public/browser/browser_context.h"
34 #include "content/public/browser/browser_thread.h"
35 #include "content/public/browser/content_browser_client.h"
36 #include "content/public/browser/render_process_host.h"
37 #include "content/public/browser/resource_request_info.h"
38 #include "content/public/common/url_constants.h"
39 #include "net/base/io_buffer.h"
40 #include "net/base/net_errors.h"
41 #include "net/http/http_response_headers.h"
42 #include "net/http/http_status_code.h"
43 #include "net/url_request/url_request.h"
44 #include "net/url_request/url_request_context.h"
45 #include "net/url_request/url_request_job.h"
46 #include "net/url_request/url_request_job_factory.h"
47 #include "url/url_util.h"
53 // TODO(tsepez) remove unsafe-eval when bidichecker_packaged.js fixed.
54 const char kChromeURLContentSecurityPolicyHeaderBase
[] =
55 "Content-Security-Policy: script-src chrome://resources "
56 "'self' 'unsafe-eval'; ";
58 const char kChromeURLXFrameOptionsHeader
[] = "X-Frame-Options: DENY";
60 const int kNoRenderProcessId
= -1;
62 bool SchemeIsInSchemes(const std::string
& scheme
,
63 const std::vector
<std::string
>& schemes
) {
64 return std::find(schemes
.begin(), schemes
.end(), scheme
) != schemes
.end();
67 // Returns whether |url| passes some sanity checks and is a valid GURL.
68 bool CheckURLIsValid(const GURL
& url
) {
69 std::vector
<std::string
> additional_schemes
;
70 DCHECK(url
.SchemeIs(kChromeDevToolsScheme
) || url
.SchemeIs(kChromeUIScheme
) ||
71 (GetContentClient()->browser()->GetAdditionalWebUISchemes(
73 SchemeIsInSchemes(url
.scheme(), additional_schemes
)));
75 if (!url
.is_valid()) {
83 // Parse |url| to get the path which will be used to resolve the request. The
84 // path is the remaining portion after the scheme and hostname.
85 void URLToRequestPath(const GURL
& url
, std::string
* path
) {
86 const std::string
& spec
= url
.possibly_invalid_spec();
87 const url::Parsed
& parsed
= url
.parsed_for_possibly_invalid_spec();
88 // + 1 to skip the slash at the beginning of the path.
89 int offset
= parsed
.CountCharactersBefore(url::Parsed::PATH
, false) + 1;
91 if (offset
< static_cast<int>(spec
.size()))
92 path
->assign(spec
.substr(offset
));
95 // Returns a value of 'Origin:' header for the |request| if the header is set.
96 // Otherwise returns an empty string.
97 std::string
GetOriginHeaderValue(const net::URLRequest
* request
) {
99 if (request
->extra_request_headers().GetHeader(
100 net::HttpRequestHeaders::kOrigin
, &result
))
102 net::HttpRequestHeaders headers
;
103 if (request
->GetFullRequestHeaders(&headers
))
104 headers
.GetHeader(net::HttpRequestHeaders::kOrigin
, &result
);
110 // URLRequestChromeJob is a net::URLRequestJob that manages running
111 // chrome-internal resource requests asynchronously.
112 // It hands off URL requests to ChromeURLDataManager, which asynchronously
113 // calls back once the data is available.
114 class URLRequestChromeJob
: public net::URLRequestJob
,
115 public base::SupportsWeakPtr
<URLRequestChromeJob
> {
117 // |is_incognito| set when job is generated from an incognito profile.
118 URLRequestChromeJob(net::URLRequest
* request
,
119 net::NetworkDelegate
* network_delegate
,
120 URLDataManagerBackend
* backend
,
123 // net::URLRequestJob implementation.
124 void Start() override
;
125 void Kill() override
;
126 bool ReadRawData(net::IOBuffer
* buf
, int buf_size
, int* bytes_read
) override
;
127 bool GetMimeType(std::string
* mime_type
) const override
;
128 int GetResponseCode() const override
;
129 void GetResponseInfo(net::HttpResponseInfo
* info
) override
;
131 // Used to notify that the requested data's |mime_type| is ready.
132 void MimeTypeAvailable(const std::string
& mime_type
);
134 // Called by ChromeURLDataManager to notify us that the data blob is ready
136 void DataAvailable(base::RefCountedMemory
* bytes
);
138 void set_mime_type(const std::string
& mime_type
) {
139 mime_type_
= mime_type
;
142 void set_allow_caching(bool allow_caching
) {
143 allow_caching_
= allow_caching
;
146 void set_add_content_security_policy(bool add_content_security_policy
) {
147 add_content_security_policy_
= add_content_security_policy
;
150 void set_content_security_policy_object_source(
151 const std::string
& data
) {
152 content_security_policy_object_source_
= data
;
155 void set_content_security_policy_frame_source(
156 const std::string
& data
) {
157 content_security_policy_frame_source_
= data
;
160 void set_deny_xframe_options(bool deny_xframe_options
) {
161 deny_xframe_options_
= deny_xframe_options
;
164 void set_send_content_type_header(bool send_content_type_header
) {
165 send_content_type_header_
= send_content_type_header
;
168 void set_access_control_allow_origin(const std::string
& value
) {
169 access_control_allow_origin_
= value
;
172 // Returns true when job was generated from an incognito profile.
173 bool is_incognito() const {
174 return is_incognito_
;
178 ~URLRequestChromeJob() override
;
180 // Helper for Start(), to let us start asynchronously.
181 // (This pattern is shared by most net::URLRequestJob implementations.)
182 void StartAsync(bool allowed
);
184 // Called on the UI thread to check if this request is allowed.
185 static void CheckStoragePartitionMatches(
186 int render_process_id
,
188 const base::WeakPtr
<URLRequestChromeJob
>& job
);
190 // Do the actual copy from data_ (the data we're serving) into |buf|.
191 // Separate from ReadRawData so we can handle async I/O.
192 void CompleteRead(net::IOBuffer
* buf
, int buf_size
, int* bytes_read
);
194 // The actual data we're serving. NULL until it's been fetched.
195 scoped_refptr
<base::RefCountedMemory
> data_
;
196 // The current offset into the data that we're handing off to our
197 // callers via the Read interfaces.
200 // For async reads, we keep around a pointer to the buffer that
201 // we're reading into.
202 scoped_refptr
<net::IOBuffer
> pending_buf_
;
203 int pending_buf_size_
;
204 std::string mime_type_
;
206 // If true, set a header in the response to prevent it from being cached.
209 // If true, set the Content Security Policy (CSP) header.
210 bool add_content_security_policy_
;
212 // These are used with the CSP.
213 std::string content_security_policy_object_source_
;
214 std::string content_security_policy_frame_source_
;
216 // If true, sets the "X-Frame-Options: DENY" header.
217 bool deny_xframe_options_
;
219 // If true, sets the "Content-Type: <mime-type>" header.
220 bool send_content_type_header_
;
222 // If not empty, "Access-Control-Allow-Origin:" is set to the value of this
224 std::string access_control_allow_origin_
;
226 // True when job is generated from an incognito profile.
227 const bool is_incognito_
;
229 // The backend is owned by net::URLRequestContext and always outlives us.
230 URLDataManagerBackend
* backend_
;
232 base::WeakPtrFactory
<URLRequestChromeJob
> weak_factory_
;
234 DISALLOW_COPY_AND_ASSIGN(URLRequestChromeJob
);
237 URLRequestChromeJob::URLRequestChromeJob(net::URLRequest
* request
,
238 net::NetworkDelegate
* network_delegate
,
239 URLDataManagerBackend
* backend
,
241 : net::URLRequestJob(request
, network_delegate
),
243 pending_buf_size_(0),
244 allow_caching_(true),
245 add_content_security_policy_(true),
246 content_security_policy_object_source_("object-src 'none';"),
247 content_security_policy_frame_source_("frame-src 'none';"),
248 deny_xframe_options_(true),
249 send_content_type_header_(false),
250 is_incognito_(is_incognito
),
252 weak_factory_(this) {
256 URLRequestChromeJob::~URLRequestChromeJob() {
257 CHECK(!backend_
->HasPendingJob(this));
260 void URLRequestChromeJob::Start() {
261 int render_process_id
, unused
;
262 bool is_renderer_request
= ResourceRequestInfo::GetRenderFrameForRequest(
263 request_
, &render_process_id
, &unused
);
264 if (!is_renderer_request
)
265 render_process_id
= kNoRenderProcessId
;
266 BrowserThread::PostTask(
269 base::Bind(&URLRequestChromeJob::CheckStoragePartitionMatches
,
270 render_process_id
, request_
->url(), AsWeakPtr()));
271 TRACE_EVENT_ASYNC_BEGIN1("browser", "DataManager:Request", this, "URL",
272 request_
->url().possibly_invalid_spec());
275 void URLRequestChromeJob::Kill() {
276 backend_
->RemoveRequest(this);
279 bool URLRequestChromeJob::GetMimeType(std::string
* mime_type
) const {
280 *mime_type
= mime_type_
;
281 return !mime_type_
.empty();
284 int URLRequestChromeJob::GetResponseCode() const {
288 void URLRequestChromeJob::GetResponseInfo(net::HttpResponseInfo
* info
) {
289 DCHECK(!info
->headers
.get());
290 // Set the headers so that requests serviced by ChromeURLDataManager return a
291 // status code of 200. Without this they return a 0, which makes the status
292 // indistiguishable from other error types. Instant relies on getting a 200.
293 info
->headers
= new net::HttpResponseHeaders("HTTP/1.1 200 OK");
295 // Determine the least-privileged content security policy header, if any,
296 // that is compatible with a given WebUI URL, and append it to the existing
298 if (add_content_security_policy_
) {
299 std::string base
= kChromeURLContentSecurityPolicyHeaderBase
;
300 base
.append(content_security_policy_object_source_
);
301 base
.append(content_security_policy_frame_source_
);
302 info
->headers
->AddHeader(base
);
305 if (deny_xframe_options_
)
306 info
->headers
->AddHeader(kChromeURLXFrameOptionsHeader
);
309 info
->headers
->AddHeader("Cache-Control: no-cache");
311 if (send_content_type_header_
&& !mime_type_
.empty()) {
312 std::string content_type
=
313 base::StringPrintf("%s:%s", net::HttpRequestHeaders::kContentType
,
315 info
->headers
->AddHeader(content_type
);
318 if (!access_control_allow_origin_
.empty()) {
319 info
->headers
->AddHeader("Access-Control-Allow-Origin: " +
320 access_control_allow_origin_
);
321 info
->headers
->AddHeader("Vary: Origin");
325 void URLRequestChromeJob::MimeTypeAvailable(const std::string
& mime_type
) {
326 set_mime_type(mime_type
);
327 NotifyHeadersComplete();
330 void URLRequestChromeJob::DataAvailable(base::RefCountedMemory
* bytes
) {
331 TRACE_EVENT_ASYNC_END0("browser", "DataManager:Request", this);
333 // The request completed, and we have all the data.
334 // Clear any IO pending status.
335 SetStatus(net::URLRequestStatus());
339 if (pending_buf_
.get()) {
340 CHECK(pending_buf_
->data());
341 CompleteRead(pending_buf_
.get(), pending_buf_size_
, &bytes_read
);
343 NotifyReadComplete(bytes_read
);
346 // The request failed.
347 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED
,
352 bool URLRequestChromeJob::ReadRawData(net::IOBuffer
* buf
, int buf_size
,
355 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING
, 0));
356 DCHECK(!pending_buf_
.get());
359 pending_buf_size_
= buf_size
;
360 return false; // Tell the caller we're still waiting for data.
363 // Otherwise, the data is available.
364 CompleteRead(buf
, buf_size
, bytes_read
);
368 void URLRequestChromeJob::CompleteRead(net::IOBuffer
* buf
, int buf_size
,
370 int remaining
= static_cast<int>(data_
->size()) - data_offset_
;
371 if (buf_size
> remaining
)
372 buf_size
= remaining
;
374 // TODO(pkasting): Remove ScopedTracker below once crbug.com/455423 is
376 tracked_objects::ScopedTracker
tracking_profile(
377 FROM_HERE_WITH_EXPLICIT_FUNCTION(
378 "455423 URLRequestChromeJob::CompleteRead memcpy"));
379 memcpy(buf
->data(), data_
->front() + data_offset_
, buf_size
);
380 data_offset_
+= buf_size
;
382 *bytes_read
= buf_size
;
385 void URLRequestChromeJob::CheckStoragePartitionMatches(
386 int render_process_id
,
388 const base::WeakPtr
<URLRequestChromeJob
>& job
) {
389 // The embedder could put some webui pages in separate storage partition.
390 // RenderProcessHostImpl::IsSuitableHost would guard against top level pages
391 // being in the same process. We do an extra check to guard against an
392 // exploited renderer pretending to add them as a subframe. We skip this check
394 bool allowed
= false;
395 std::vector
<std::string
> hosts
;
397 browser()->GetAdditionalWebUIHostsToIgnoreParititionCheck(&hosts
);
398 if (url
.SchemeIs(kChromeUIScheme
) &&
399 (url
.SchemeIs(kChromeUIScheme
) ||
400 std::find(hosts
.begin(), hosts
.end(), url
.host()) != hosts
.end())) {
402 } else if (render_process_id
== kNoRenderProcessId
) {
403 // Request was not issued by renderer.
406 RenderProcessHost
* process
= RenderProcessHost::FromID(render_process_id
);
408 StoragePartition
* partition
= BrowserContext::GetStoragePartitionForSite(
409 process
->GetBrowserContext(), url
);
410 allowed
= partition
== process
->GetStoragePartition();
414 BrowserThread::PostTask(
417 base::Bind(&URLRequestChromeJob::StartAsync
, job
, allowed
));
420 void URLRequestChromeJob::StartAsync(bool allowed
) {
424 if (!allowed
|| !backend_
->StartRequest(request_
, this)) {
425 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED
,
426 net::ERR_INVALID_URL
));
432 // Gets mime type for data that is available from |source| by |path|.
433 // After that, notifies |job| that mime type is available. This method
434 // should be called on the UI thread, but notification is performed on
436 void GetMimeTypeOnUI(URLDataSourceImpl
* source
,
437 const std::string
& path
,
438 const base::WeakPtr
<URLRequestChromeJob
>& job
) {
439 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
440 std::string mime_type
= source
->source()->GetMimeType(path
);
441 BrowserThread::PostTask(
442 BrowserThread::IO
, FROM_HERE
,
443 base::Bind(&URLRequestChromeJob::MimeTypeAvailable
, job
, mime_type
));
450 class ChromeProtocolHandler
451 : public net::URLRequestJobFactory::ProtocolHandler
{
453 // |is_incognito| should be set for incognito profiles.
454 ChromeProtocolHandler(ResourceContext
* resource_context
,
456 AppCacheServiceImpl
* appcache_service
,
457 ChromeBlobStorageContext
* blob_storage_context
)
458 : resource_context_(resource_context
),
459 is_incognito_(is_incognito
),
460 appcache_service_(appcache_service
),
461 blob_storage_context_(blob_storage_context
) {}
462 ~ChromeProtocolHandler() override
{}
464 net::URLRequestJob
* MaybeCreateJob(
465 net::URLRequest
* request
,
466 net::NetworkDelegate
* network_delegate
) const override
{
469 // Check for chrome://view-http-cache/*, which uses its own job type.
470 if (ViewHttpCacheJobFactory::IsSupportedURL(request
->url()))
471 return ViewHttpCacheJobFactory::CreateJobForRequest(request
,
474 // Next check for chrome://appcache-internals/, which uses its own job type.
475 if (request
->url().SchemeIs(kChromeUIScheme
) &&
476 request
->url().host() == kChromeUIAppCacheInternalsHost
) {
477 return ViewAppCacheInternalsJobFactory::CreateJobForRequest(
478 request
, network_delegate
, appcache_service_
);
481 // Next check for chrome://blob-internals/, which uses its own job type.
482 if (ViewBlobInternalsJobFactory::IsSupportedURL(request
->url())) {
483 return ViewBlobInternalsJobFactory::CreateJobForRequest(
484 request
, network_delegate
, blob_storage_context_
->context());
487 #if defined(USE_TCMALLOC)
488 // Next check for chrome://tcmalloc/, which uses its own job type.
489 if (request
->url().SchemeIs(kChromeUIScheme
) &&
490 request
->url().host() == kChromeUITcmallocHost
) {
491 return new TcmallocInternalsRequestJob(request
, network_delegate
);
495 // Next check for chrome://histograms/, which uses its own job type.
496 if (request
->url().SchemeIs(kChromeUIScheme
) &&
497 request
->url().host() == kChromeUIHistogramHost
) {
498 return new HistogramInternalsRequestJob(request
, network_delegate
);
501 // Fall back to using a custom handler
502 return new URLRequestChromeJob(
503 request
, network_delegate
,
504 GetURLDataManagerForResourceContext(resource_context_
), is_incognito_
);
507 bool IsSafeRedirectTarget(const GURL
& location
) const override
{
512 // These members are owned by ProfileIOData, which owns this ProtocolHandler.
513 content::ResourceContext
* const resource_context_
;
515 // True when generated from an incognito profile.
516 const bool is_incognito_
;
517 AppCacheServiceImpl
* appcache_service_
;
518 ChromeBlobStorageContext
* blob_storage_context_
;
520 DISALLOW_COPY_AND_ASSIGN(ChromeProtocolHandler
);
525 URLDataManagerBackend::URLDataManagerBackend()
526 : next_request_id_(0) {
527 URLDataSource
* shared_source
= new SharedResourcesDataSource();
528 URLDataSourceImpl
* source_impl
=
529 new URLDataSourceImpl(shared_source
->GetSource(), shared_source
);
530 AddDataSource(source_impl
);
533 URLDataManagerBackend::~URLDataManagerBackend() {
534 for (DataSourceMap::iterator i
= data_sources_
.begin();
535 i
!= data_sources_
.end(); ++i
) {
536 i
->second
->backend_
= NULL
;
538 data_sources_
.clear();
542 net::URLRequestJobFactory::ProtocolHandler
*
543 URLDataManagerBackend::CreateProtocolHandler(
544 content::ResourceContext
* resource_context
,
546 AppCacheServiceImpl
* appcache_service
,
547 ChromeBlobStorageContext
* blob_storage_context
) {
548 DCHECK(resource_context
);
549 return new ChromeProtocolHandler(
550 resource_context
, is_incognito
, appcache_service
, blob_storage_context
);
553 void URLDataManagerBackend::AddDataSource(
554 URLDataSourceImpl
* source
) {
555 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
556 DataSourceMap::iterator i
= data_sources_
.find(source
->source_name());
557 if (i
!= data_sources_
.end()) {
558 if (!source
->source()->ShouldReplaceExistingSource())
560 i
->second
->backend_
= NULL
;
562 data_sources_
[source
->source_name()] = source
;
563 source
->backend_
= this;
566 bool URLDataManagerBackend::HasPendingJob(
567 URLRequestChromeJob
* job
) const {
568 for (PendingRequestMap::const_iterator i
= pending_requests_
.begin();
569 i
!= pending_requests_
.end(); ++i
) {
570 if (i
->second
== job
)
576 bool URLDataManagerBackend::StartRequest(const net::URLRequest
* request
,
577 URLRequestChromeJob
* job
) {
578 if (!CheckURLIsValid(request
->url()))
581 URLDataSourceImpl
* source
= GetDataSourceFromURL(request
->url());
585 if (!source
->source()->ShouldServiceRequest(request
))
589 URLToRequestPath(request
->url(), &path
);
590 source
->source()->WillServiceRequest(request
, &path
);
592 // Save this request so we know where to send the data.
593 RequestID request_id
= next_request_id_
++;
594 pending_requests_
.insert(std::make_pair(request_id
, job
));
596 job
->set_allow_caching(source
->source()->AllowCaching());
597 job
->set_add_content_security_policy(
598 source
->source()->ShouldAddContentSecurityPolicy());
599 job
->set_content_security_policy_object_source(
600 source
->source()->GetContentSecurityPolicyObjectSrc());
601 job
->set_content_security_policy_frame_source(
602 source
->source()->GetContentSecurityPolicyFrameSrc());
603 job
->set_deny_xframe_options(
604 source
->source()->ShouldDenyXFrameOptions());
605 job
->set_send_content_type_header(
606 source
->source()->ShouldServeMimeTypeAsContentTypeHeader());
608 std::string origin
= GetOriginHeaderValue(request
);
609 if (!origin
.empty()) {
611 source
->source()->GetAccessControlAllowOriginForOrigin(origin
);
612 DCHECK(header
.empty() || header
== origin
|| header
== "*" ||
614 job
->set_access_control_allow_origin(header
);
617 // Look up additional request info to pass down.
618 int render_process_id
= -1;
619 int render_frame_id
= -1;
620 ResourceRequestInfo::GetRenderFrameForRequest(request
,
624 // Forward along the request to the data source.
625 base::MessageLoop
* target_message_loop
=
626 source
->source()->MessageLoopForRequestPath(path
);
627 if (!target_message_loop
) {
628 job
->MimeTypeAvailable(source
->source()->GetMimeType(path
));
629 // Eliminate potentially dangling pointer to avoid future use.
632 // The DataSource is agnostic to which thread StartDataRequest is called
633 // on for this path. Call directly into it from this thread, the IO
635 source
->source()->StartDataRequest(
636 path
, render_process_id
, render_frame_id
,
637 base::Bind(&URLDataSourceImpl::SendResponse
, source
, request_id
));
639 // URLRequestChromeJob should receive mime type before data. This
640 // is guaranteed because request for mime type is placed in the
641 // message loop before request for data. And correspondingly their
642 // replies are put on the IO thread in the same order.
643 target_message_loop
->task_runner()->PostTask(
645 base::Bind(&GetMimeTypeOnUI
, scoped_refptr
<URLDataSourceImpl
>(source
),
646 path
, job
->AsWeakPtr()));
648 // The DataSource wants StartDataRequest to be called on a specific thread,
649 // usually the UI thread, for this path.
650 target_message_loop
->task_runner()->PostTask(
651 FROM_HERE
, base::Bind(&URLDataManagerBackend::CallStartRequest
,
652 make_scoped_refptr(source
), path
,
653 render_process_id
, render_frame_id
, request_id
));
658 URLDataSourceImpl
* URLDataManagerBackend::GetDataSourceFromURL(
660 // The input usually looks like: chrome://source_name/extra_bits?foo
661 // so do a lookup using the host of the URL.
662 DataSourceMap::iterator i
= data_sources_
.find(url
.host());
663 if (i
!= data_sources_
.end())
664 return i
->second
.get();
666 // No match using the host of the URL, so do a lookup using the scheme for
667 // URLs on the form source_name://extra_bits/foo .
668 i
= data_sources_
.find(url
.scheme() + "://");
669 if (i
!= data_sources_
.end())
670 return i
->second
.get();
672 // No matches found, so give up.
676 void URLDataManagerBackend::CallStartRequest(
677 scoped_refptr
<URLDataSourceImpl
> source
,
678 const std::string
& path
,
679 int render_process_id
,
682 if (BrowserThread::CurrentlyOn(BrowserThread::UI
) &&
683 render_process_id
!= -1 &&
684 !RenderProcessHost::FromID(render_process_id
)) {
685 // Make the request fail if its initiating renderer is no longer valid.
686 // This can happen when the IO thread posts this task just before the
687 // renderer shuts down.
688 source
->SendResponse(request_id
, NULL
);
691 source
->source()->StartDataRequest(
695 base::Bind(&URLDataSourceImpl::SendResponse
, source
, request_id
));
698 void URLDataManagerBackend::RemoveRequest(URLRequestChromeJob
* job
) {
699 // Remove the request from our list of pending requests.
700 // If/when the source sends the data that was requested, the data will just
702 for (PendingRequestMap::iterator i
= pending_requests_
.begin();
703 i
!= pending_requests_
.end(); ++i
) {
704 if (i
->second
== job
) {
705 pending_requests_
.erase(i
);
711 void URLDataManagerBackend::DataAvailable(RequestID request_id
,
712 base::RefCountedMemory
* bytes
) {
713 // Forward this data on to the pending net::URLRequest, if it exists.
714 PendingRequestMap::iterator i
= pending_requests_
.find(request_id
);
715 if (i
!= pending_requests_
.end()) {
716 URLRequestChromeJob
* job
= i
->second
;
717 pending_requests_
.erase(i
);
718 job
->DataAvailable(bytes
);
724 class DevToolsJobFactory
725 : public net::URLRequestJobFactory::ProtocolHandler
{
727 // |is_incognito| should be set for incognito profiles.
728 DevToolsJobFactory(content::ResourceContext
* resource_context
,
730 ~DevToolsJobFactory() override
;
732 net::URLRequestJob
* MaybeCreateJob(
733 net::URLRequest
* request
,
734 net::NetworkDelegate
* network_delegate
) const override
;
737 // |resource_context_| and |network_delegate_| are owned by ProfileIOData,
738 // which owns this ProtocolHandler.
739 content::ResourceContext
* const resource_context_
;
741 // True when generated from an incognito profile.
742 const bool is_incognito_
;
744 DISALLOW_COPY_AND_ASSIGN(DevToolsJobFactory
);
747 DevToolsJobFactory::DevToolsJobFactory(
748 content::ResourceContext
* resource_context
,
750 : resource_context_(resource_context
),
751 is_incognito_(is_incognito
) {
752 DCHECK(resource_context_
);
755 DevToolsJobFactory::~DevToolsJobFactory() {}
758 DevToolsJobFactory::MaybeCreateJob(
759 net::URLRequest
* request
, net::NetworkDelegate
* network_delegate
) const {
760 return new URLRequestChromeJob(
761 request
, network_delegate
,
762 GetURLDataManagerForResourceContext(resource_context_
), is_incognito_
);
767 net::URLRequestJobFactory::ProtocolHandler
*
768 CreateDevToolsProtocolHandler(content::ResourceContext
* resource_context
,
770 return new DevToolsJobFactory(resource_context
, is_incognito
);
773 } // namespace content