Update instrumentation for many different bugs based on new UMA data.
[chromium-blink-merge.git] / content / browser / webui / url_data_manager_backend.cc
blobd79a617832fed80158281fdd2a9a3f9e880c27c6
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"
7 #include <set>
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/memory/ref_counted.h"
16 #include "base/memory/ref_counted_memory.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/message_loop/message_loop.h"
19 #include "base/profiler/scoped_tracker.h"
20 #include "base/strings/string_util.h"
21 #include "base/strings/stringprintf.h"
22 #include "base/trace_event/trace_event.h"
23 #include "content/browser/appcache/view_appcache_internals_job.h"
24 #include "content/browser/fileapi/chrome_blob_storage_context.h"
25 #include "content/browser/histogram_internals_request_job.h"
26 #include "content/browser/net/view_blob_internals_job_factory.h"
27 #include "content/browser/net/view_http_cache_job_factory.h"
28 #include "content/browser/resource_context_impl.h"
29 #include "content/browser/tcmalloc_internals_request_job.h"
30 #include "content/browser/webui/shared_resources_data_source.h"
31 #include "content/browser/webui/url_data_source_impl.h"
32 #include "content/public/browser/browser_context.h"
33 #include "content/public/browser/browser_thread.h"
34 #include "content/public/browser/content_browser_client.h"
35 #include "content/public/browser/render_process_host.h"
36 #include "content/public/browser/resource_request_info.h"
37 #include "content/public/common/url_constants.h"
38 #include "net/base/io_buffer.h"
39 #include "net/base/net_errors.h"
40 #include "net/http/http_response_headers.h"
41 #include "net/http/http_status_code.h"
42 #include "net/url_request/url_request.h"
43 #include "net/url_request/url_request_context.h"
44 #include "net/url_request/url_request_job.h"
45 #include "net/url_request/url_request_job_factory.h"
46 #include "url/url_util.h"
48 namespace content {
50 namespace {
52 // TODO(tsepez) remove unsafe-eval when bidichecker_packaged.js fixed.
53 const char kChromeURLContentSecurityPolicyHeaderBase[] =
54 "Content-Security-Policy: script-src chrome://resources "
55 "'self' 'unsafe-eval'; ";
57 const char kChromeURLXFrameOptionsHeader[] = "X-Frame-Options: DENY";
59 const int kNoRenderProcessId = -1;
61 bool SchemeIsInSchemes(const std::string& scheme,
62 const std::vector<std::string>& schemes) {
63 return std::find(schemes.begin(), schemes.end(), scheme) != schemes.end();
66 // Returns whether |url| passes some sanity checks and is a valid GURL.
67 bool CheckURLIsValid(const GURL& url) {
68 std::vector<std::string> additional_schemes;
69 DCHECK(url.SchemeIs(kChromeDevToolsScheme) || url.SchemeIs(kChromeUIScheme) ||
70 (GetContentClient()->browser()->GetAdditionalWebUISchemes(
71 &additional_schemes),
72 SchemeIsInSchemes(url.scheme(), additional_schemes)));
74 if (!url.is_valid()) {
75 NOTREACHED();
76 return false;
79 return true;
82 // Parse |url| to get the path which will be used to resolve the request. The
83 // path is the remaining portion after the scheme and hostname.
84 void URLToRequestPath(const GURL& url, std::string* path) {
85 const std::string& spec = url.possibly_invalid_spec();
86 const url::Parsed& parsed = url.parsed_for_possibly_invalid_spec();
87 // + 1 to skip the slash at the beginning of the path.
88 int offset = parsed.CountCharactersBefore(url::Parsed::PATH, false) + 1;
90 if (offset < static_cast<int>(spec.size()))
91 path->assign(spec.substr(offset));
94 // Returns a value of 'Origin:' header for the |request| if the header is set.
95 // Otherwise returns an empty string.
96 std::string GetOriginHeaderValue(const net::URLRequest* request) {
97 std::string result;
98 if (request->extra_request_headers().GetHeader(
99 net::HttpRequestHeaders::kOrigin, &result))
100 return result;
101 net::HttpRequestHeaders headers;
102 if (request->GetFullRequestHeaders(&headers))
103 headers.GetHeader(net::HttpRequestHeaders::kOrigin, &result);
104 return result;
107 } // namespace
109 // URLRequestChromeJob is a net::URLRequestJob that manages running
110 // chrome-internal resource requests asynchronously.
111 // It hands off URL requests to ChromeURLDataManager, which asynchronously
112 // calls back once the data is available.
113 class URLRequestChromeJob : public net::URLRequestJob,
114 public base::SupportsWeakPtr<URLRequestChromeJob> {
115 public:
116 // |is_incognito| set when job is generated from an incognito profile.
117 URLRequestChromeJob(net::URLRequest* request,
118 net::NetworkDelegate* network_delegate,
119 URLDataManagerBackend* backend,
120 bool is_incognito);
122 // net::URLRequestJob implementation.
123 void Start() override;
124 void Kill() override;
125 bool ReadRawData(net::IOBuffer* buf, int buf_size, int* bytes_read) override;
126 bool GetMimeType(std::string* mime_type) const override;
127 int GetResponseCode() const override;
128 void GetResponseInfo(net::HttpResponseInfo* info) override;
130 // Used to notify that the requested data's |mime_type| is ready.
131 void MimeTypeAvailable(const std::string& mime_type);
133 // Called by ChromeURLDataManager to notify us that the data blob is ready
134 // for us.
135 void DataAvailable(base::RefCountedMemory* bytes);
137 void set_mime_type(const std::string& mime_type) {
138 mime_type_ = mime_type;
141 void set_allow_caching(bool allow_caching) {
142 allow_caching_ = allow_caching;
145 void set_add_content_security_policy(bool add_content_security_policy) {
146 add_content_security_policy_ = add_content_security_policy;
149 void set_content_security_policy_object_source(
150 const std::string& data) {
151 content_security_policy_object_source_ = data;
154 void set_content_security_policy_frame_source(
155 const std::string& data) {
156 content_security_policy_frame_source_ = data;
159 void set_deny_xframe_options(bool deny_xframe_options) {
160 deny_xframe_options_ = deny_xframe_options;
163 void set_send_content_type_header(bool send_content_type_header) {
164 send_content_type_header_ = send_content_type_header;
167 void set_access_control_allow_origin(const std::string& value) {
168 access_control_allow_origin_ = value;
171 // Returns true when job was generated from an incognito profile.
172 bool is_incognito() const {
173 return is_incognito_;
176 private:
177 ~URLRequestChromeJob() override;
179 // Helper for Start(), to let us start asynchronously.
180 // (This pattern is shared by most net::URLRequestJob implementations.)
181 void StartAsync(bool allowed);
183 // Called on the UI thread to check if this request is allowed.
184 static void CheckStoragePartitionMatches(
185 int render_process_id,
186 const GURL& url,
187 const base::WeakPtr<URLRequestChromeJob>& job);
189 // Do the actual copy from data_ (the data we're serving) into |buf|.
190 // Separate from ReadRawData so we can handle async I/O.
191 void CompleteRead(net::IOBuffer* buf, int buf_size, int* bytes_read);
193 // The actual data we're serving. NULL until it's been fetched.
194 scoped_refptr<base::RefCountedMemory> data_;
195 // The current offset into the data that we're handing off to our
196 // callers via the Read interfaces.
197 int data_offset_;
199 // For async reads, we keep around a pointer to the buffer that
200 // we're reading into.
201 scoped_refptr<net::IOBuffer> pending_buf_;
202 int pending_buf_size_;
203 std::string mime_type_;
205 // If true, set a header in the response to prevent it from being cached.
206 bool allow_caching_;
208 // If true, set the Content Security Policy (CSP) header.
209 bool add_content_security_policy_;
211 // These are used with the CSP.
212 std::string content_security_policy_object_source_;
213 std::string content_security_policy_frame_source_;
215 // If true, sets the "X-Frame-Options: DENY" header.
216 bool deny_xframe_options_;
218 // If true, sets the "Content-Type: <mime-type>" header.
219 bool send_content_type_header_;
221 // If not empty, "Access-Control-Allow-Origin:" is set to the value of this
222 // string.
223 std::string access_control_allow_origin_;
225 // True when job is generated from an incognito profile.
226 const bool is_incognito_;
228 // The backend is owned by net::URLRequestContext and always outlives us.
229 URLDataManagerBackend* backend_;
231 base::WeakPtrFactory<URLRequestChromeJob> weak_factory_;
233 DISALLOW_COPY_AND_ASSIGN(URLRequestChromeJob);
236 URLRequestChromeJob::URLRequestChromeJob(net::URLRequest* request,
237 net::NetworkDelegate* network_delegate,
238 URLDataManagerBackend* backend,
239 bool is_incognito)
240 : net::URLRequestJob(request, network_delegate),
241 data_offset_(0),
242 pending_buf_size_(0),
243 allow_caching_(true),
244 add_content_security_policy_(true),
245 content_security_policy_object_source_("object-src 'none';"),
246 content_security_policy_frame_source_("frame-src 'none';"),
247 deny_xframe_options_(true),
248 send_content_type_header_(false),
249 is_incognito_(is_incognito),
250 backend_(backend),
251 weak_factory_(this) {
252 DCHECK(backend);
255 URLRequestChromeJob::~URLRequestChromeJob() {
256 CHECK(!backend_->HasPendingJob(this));
259 void URLRequestChromeJob::Start() {
260 int render_process_id, unused;
261 bool is_renderer_request = ResourceRequestInfo::GetRenderFrameForRequest(
262 request_, &render_process_id, &unused);
263 if (!is_renderer_request)
264 render_process_id = kNoRenderProcessId;
265 BrowserThread::PostTask(
266 BrowserThread::UI,
267 FROM_HERE,
268 base::Bind(&URLRequestChromeJob::CheckStoragePartitionMatches,
269 render_process_id, request_->url(), AsWeakPtr()));
270 TRACE_EVENT_ASYNC_BEGIN1("browser", "DataManager:Request", this, "URL",
271 request_->url().possibly_invalid_spec());
274 void URLRequestChromeJob::Kill() {
275 backend_->RemoveRequest(this);
278 bool URLRequestChromeJob::GetMimeType(std::string* mime_type) const {
279 *mime_type = mime_type_;
280 return !mime_type_.empty();
283 int URLRequestChromeJob::GetResponseCode() const {
284 return net::HTTP_OK;
287 void URLRequestChromeJob::GetResponseInfo(net::HttpResponseInfo* info) {
288 DCHECK(!info->headers.get());
289 // Set the headers so that requests serviced by ChromeURLDataManager return a
290 // status code of 200. Without this they return a 0, which makes the status
291 // indistiguishable from other error types. Instant relies on getting a 200.
292 info->headers = new net::HttpResponseHeaders("HTTP/1.1 200 OK");
294 // Determine the least-privileged content security policy header, if any,
295 // that is compatible with a given WebUI URL, and append it to the existing
296 // response headers.
297 if (add_content_security_policy_) {
298 std::string base = kChromeURLContentSecurityPolicyHeaderBase;
299 base.append(content_security_policy_object_source_);
300 base.append(content_security_policy_frame_source_);
301 info->headers->AddHeader(base);
304 if (deny_xframe_options_)
305 info->headers->AddHeader(kChromeURLXFrameOptionsHeader);
307 if (!allow_caching_)
308 info->headers->AddHeader("Cache-Control: no-cache");
310 if (send_content_type_header_ && !mime_type_.empty()) {
311 std::string content_type =
312 base::StringPrintf("%s:%s", net::HttpRequestHeaders::kContentType,
313 mime_type_.c_str());
314 info->headers->AddHeader(content_type);
317 if (!access_control_allow_origin_.empty()) {
318 info->headers->AddHeader("Access-Control-Allow-Origin: " +
319 access_control_allow_origin_);
320 info->headers->AddHeader("Vary: Origin");
324 void URLRequestChromeJob::MimeTypeAvailable(const std::string& mime_type) {
325 set_mime_type(mime_type);
326 NotifyHeadersComplete();
329 void URLRequestChromeJob::DataAvailable(base::RefCountedMemory* bytes) {
330 TRACE_EVENT_ASYNC_END0("browser", "DataManager:Request", this);
331 if (bytes) {
332 // The request completed, and we have all the data.
333 // Clear any IO pending status.
334 SetStatus(net::URLRequestStatus());
336 data_ = bytes;
337 int bytes_read;
338 if (pending_buf_.get()) {
339 CHECK(pending_buf_->data());
340 CompleteRead(pending_buf_.get(), pending_buf_size_, &bytes_read);
341 pending_buf_ = NULL;
342 NotifyReadComplete(bytes_read);
344 } else {
345 // The request failed.
346 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED,
347 net::ERR_FAILED));
351 bool URLRequestChromeJob::ReadRawData(net::IOBuffer* buf, int buf_size,
352 int* bytes_read) {
353 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
354 tracked_objects::ScopedTracker tracking_profile(
355 FROM_HERE_WITH_EXPLICIT_FUNCTION(
356 "423948 URLRequestChromeJob::ReadRawData"));
358 if (!data_.get()) {
359 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0));
360 DCHECK(!pending_buf_.get());
361 CHECK(buf->data());
362 pending_buf_ = buf;
363 pending_buf_size_ = buf_size;
364 return false; // Tell the caller we're still waiting for data.
367 // Otherwise, the data is available.
368 CompleteRead(buf, buf_size, bytes_read);
369 return true;
372 void URLRequestChromeJob::CompleteRead(net::IOBuffer* buf, int buf_size,
373 int* bytes_read) {
374 int remaining = static_cast<int>(data_->size()) - data_offset_;
375 if (buf_size > remaining)
376 buf_size = remaining;
377 if (buf_size > 0) {
378 // TODO(pkasting): Remove ScopedTracker below once crbug.com/455423 is
379 // fixed.
380 tracked_objects::ScopedTracker tracking_profile(
381 FROM_HERE_WITH_EXPLICIT_FUNCTION(
382 "455423 URLRequestChromeJob::CompleteRead memcpy"));
383 memcpy(buf->data(), data_->front() + data_offset_, buf_size);
384 data_offset_ += buf_size;
386 *bytes_read = buf_size;
389 void URLRequestChromeJob::CheckStoragePartitionMatches(
390 int render_process_id,
391 const GURL& url,
392 const base::WeakPtr<URLRequestChromeJob>& job) {
393 // The embedder could put some webui pages in separate storage partition.
394 // RenderProcessHostImpl::IsSuitableHost would guard against top level pages
395 // being in the same process. We do an extra check to guard against an
396 // exploited renderer pretending to add them as a subframe. We skip this check
397 // for resources.
398 bool allowed = false;
399 std::vector<std::string> hosts;
400 GetContentClient()->
401 browser()->GetAdditionalWebUIHostsToIgnoreParititionCheck(&hosts);
402 if (url.SchemeIs(kChromeUIScheme) &&
403 (url.SchemeIs(kChromeUIScheme) ||
404 std::find(hosts.begin(), hosts.end(), url.host()) != hosts.end())) {
405 allowed = true;
406 } else if (render_process_id == kNoRenderProcessId) {
407 // Request was not issued by renderer.
408 allowed = true;
409 } else {
410 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id);
411 if (process) {
412 StoragePartition* partition = BrowserContext::GetStoragePartitionForSite(
413 process->GetBrowserContext(), url);
414 allowed = partition == process->GetStoragePartition();
418 BrowserThread::PostTask(
419 BrowserThread::IO,
420 FROM_HERE,
421 base::Bind(&URLRequestChromeJob::StartAsync, job, allowed));
424 void URLRequestChromeJob::StartAsync(bool allowed) {
425 if (!request_)
426 return;
428 if (!allowed || !backend_->StartRequest(request_, this)) {
429 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED,
430 net::ERR_INVALID_URL));
434 namespace {
436 // Gets mime type for data that is available from |source| by |path|.
437 // After that, notifies |job| that mime type is available. This method
438 // should be called on the UI thread, but notification is performed on
439 // the IO thread.
440 void GetMimeTypeOnUI(URLDataSourceImpl* source,
441 const std::string& path,
442 const base::WeakPtr<URLRequestChromeJob>& job) {
443 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
444 std::string mime_type = source->source()->GetMimeType(path);
445 BrowserThread::PostTask(
446 BrowserThread::IO, FROM_HERE,
447 base::Bind(&URLRequestChromeJob::MimeTypeAvailable, job, mime_type));
450 } // namespace
452 namespace {
454 class ChromeProtocolHandler
455 : public net::URLRequestJobFactory::ProtocolHandler {
456 public:
457 // |is_incognito| should be set for incognito profiles.
458 ChromeProtocolHandler(ResourceContext* resource_context,
459 bool is_incognito,
460 AppCacheServiceImpl* appcache_service,
461 ChromeBlobStorageContext* blob_storage_context)
462 : resource_context_(resource_context),
463 is_incognito_(is_incognito),
464 appcache_service_(appcache_service),
465 blob_storage_context_(blob_storage_context) {}
466 ~ChromeProtocolHandler() override {}
468 net::URLRequestJob* MaybeCreateJob(
469 net::URLRequest* request,
470 net::NetworkDelegate* network_delegate) const override {
471 DCHECK(request);
473 // Check for chrome://view-http-cache/*, which uses its own job type.
474 if (ViewHttpCacheJobFactory::IsSupportedURL(request->url()))
475 return ViewHttpCacheJobFactory::CreateJobForRequest(request,
476 network_delegate);
478 // Next check for chrome://appcache-internals/, which uses its own job type.
479 if (request->url().SchemeIs(kChromeUIScheme) &&
480 request->url().host() == kChromeUIAppCacheInternalsHost) {
481 return ViewAppCacheInternalsJobFactory::CreateJobForRequest(
482 request, network_delegate, appcache_service_);
485 // Next check for chrome://blob-internals/, which uses its own job type.
486 if (ViewBlobInternalsJobFactory::IsSupportedURL(request->url())) {
487 return ViewBlobInternalsJobFactory::CreateJobForRequest(
488 request, network_delegate, blob_storage_context_->context());
491 #if defined(USE_TCMALLOC)
492 // Next check for chrome://tcmalloc/, which uses its own job type.
493 if (request->url().SchemeIs(kChromeUIScheme) &&
494 request->url().host() == kChromeUITcmallocHost) {
495 return new TcmallocInternalsRequestJob(request, network_delegate);
497 #endif
499 // Next check for chrome://histograms/, which uses its own job type.
500 if (request->url().SchemeIs(kChromeUIScheme) &&
501 request->url().host() == kChromeUIHistogramHost) {
502 return new HistogramInternalsRequestJob(request, network_delegate);
505 // Fall back to using a custom handler
506 return new URLRequestChromeJob(
507 request, network_delegate,
508 GetURLDataManagerForResourceContext(resource_context_), is_incognito_);
511 bool IsSafeRedirectTarget(const GURL& location) const override {
512 return false;
515 private:
516 // These members are owned by ProfileIOData, which owns this ProtocolHandler.
517 content::ResourceContext* const resource_context_;
519 // True when generated from an incognito profile.
520 const bool is_incognito_;
521 AppCacheServiceImpl* appcache_service_;
522 ChromeBlobStorageContext* blob_storage_context_;
524 DISALLOW_COPY_AND_ASSIGN(ChromeProtocolHandler);
527 } // namespace
529 URLDataManagerBackend::URLDataManagerBackend()
530 : next_request_id_(0) {
531 URLDataSource* shared_source = new SharedResourcesDataSource();
532 URLDataSourceImpl* source_impl =
533 new URLDataSourceImpl(shared_source->GetSource(), shared_source);
534 AddDataSource(source_impl);
537 URLDataManagerBackend::~URLDataManagerBackend() {
538 for (DataSourceMap::iterator i = data_sources_.begin();
539 i != data_sources_.end(); ++i) {
540 i->second->backend_ = NULL;
542 data_sources_.clear();
545 // static
546 net::URLRequestJobFactory::ProtocolHandler*
547 URLDataManagerBackend::CreateProtocolHandler(
548 content::ResourceContext* resource_context,
549 bool is_incognito,
550 AppCacheServiceImpl* appcache_service,
551 ChromeBlobStorageContext* blob_storage_context) {
552 DCHECK(resource_context);
553 return new ChromeProtocolHandler(
554 resource_context, is_incognito, appcache_service, blob_storage_context);
557 void URLDataManagerBackend::AddDataSource(
558 URLDataSourceImpl* source) {
559 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
560 DataSourceMap::iterator i = data_sources_.find(source->source_name());
561 if (i != data_sources_.end()) {
562 if (!source->source()->ShouldReplaceExistingSource())
563 return;
564 i->second->backend_ = NULL;
566 data_sources_[source->source_name()] = source;
567 source->backend_ = this;
570 bool URLDataManagerBackend::HasPendingJob(
571 URLRequestChromeJob* job) const {
572 for (PendingRequestMap::const_iterator i = pending_requests_.begin();
573 i != pending_requests_.end(); ++i) {
574 if (i->second == job)
575 return true;
577 return false;
580 bool URLDataManagerBackend::StartRequest(const net::URLRequest* request,
581 URLRequestChromeJob* job) {
582 if (!CheckURLIsValid(request->url()))
583 return false;
585 URLDataSourceImpl* source = GetDataSourceFromURL(request->url());
586 if (!source)
587 return false;
589 if (!source->source()->ShouldServiceRequest(request))
590 return false;
592 std::string path;
593 URLToRequestPath(request->url(), &path);
594 source->source()->WillServiceRequest(request, &path);
596 // Save this request so we know where to send the data.
597 RequestID request_id = next_request_id_++;
598 pending_requests_.insert(std::make_pair(request_id, job));
600 job->set_allow_caching(source->source()->AllowCaching());
601 job->set_add_content_security_policy(
602 source->source()->ShouldAddContentSecurityPolicy());
603 job->set_content_security_policy_object_source(
604 source->source()->GetContentSecurityPolicyObjectSrc());
605 job->set_content_security_policy_frame_source(
606 source->source()->GetContentSecurityPolicyFrameSrc());
607 job->set_deny_xframe_options(
608 source->source()->ShouldDenyXFrameOptions());
609 job->set_send_content_type_header(
610 source->source()->ShouldServeMimeTypeAsContentTypeHeader());
612 std::string origin = GetOriginHeaderValue(request);
613 if (!origin.empty()) {
614 std::string header =
615 source->source()->GetAccessControlAllowOriginForOrigin(origin);
616 DCHECK(header.empty() || header == origin || header == "*" ||
617 header == "null");
618 job->set_access_control_allow_origin(header);
621 // Look up additional request info to pass down.
622 int render_process_id = -1;
623 int render_frame_id = -1;
624 ResourceRequestInfo::GetRenderFrameForRequest(request,
625 &render_process_id,
626 &render_frame_id);
628 // Forward along the request to the data source.
629 base::MessageLoop* target_message_loop =
630 source->source()->MessageLoopForRequestPath(path);
631 if (!target_message_loop) {
632 job->MimeTypeAvailable(source->source()->GetMimeType(path));
633 // Eliminate potentially dangling pointer to avoid future use.
634 job = NULL;
636 // The DataSource is agnostic to which thread StartDataRequest is called
637 // on for this path. Call directly into it from this thread, the IO
638 // thread.
639 source->source()->StartDataRequest(
640 path, render_process_id, render_frame_id,
641 base::Bind(&URLDataSourceImpl::SendResponse, source, request_id));
642 } else {
643 // URLRequestChromeJob should receive mime type before data. This
644 // is guaranteed because request for mime type is placed in the
645 // message loop before request for data. And correspondingly their
646 // replies are put on the IO thread in the same order.
647 target_message_loop->PostTask(
648 FROM_HERE,
649 base::Bind(&GetMimeTypeOnUI,
650 scoped_refptr<URLDataSourceImpl>(source),
651 path, job->AsWeakPtr()));
653 // The DataSource wants StartDataRequest to be called on a specific thread,
654 // usually the UI thread, for this path.
655 target_message_loop->PostTask(
656 FROM_HERE,
657 base::Bind(&URLDataManagerBackend::CallStartRequest,
658 make_scoped_refptr(source), path, render_process_id,
659 render_frame_id, request_id));
661 return true;
664 URLDataSourceImpl* URLDataManagerBackend::GetDataSourceFromURL(
665 const GURL& url) {
666 // The input usually looks like: chrome://source_name/extra_bits?foo
667 // so do a lookup using the host of the URL.
668 DataSourceMap::iterator i = data_sources_.find(url.host());
669 if (i != data_sources_.end())
670 return i->second.get();
672 // No match using the host of the URL, so do a lookup using the scheme for
673 // URLs on the form source_name://extra_bits/foo .
674 i = data_sources_.find(url.scheme() + "://");
675 if (i != data_sources_.end())
676 return i->second.get();
678 // No matches found, so give up.
679 return NULL;
682 void URLDataManagerBackend::CallStartRequest(
683 scoped_refptr<URLDataSourceImpl> source,
684 const std::string& path,
685 int render_process_id,
686 int render_frame_id,
687 int request_id) {
688 if (BrowserThread::CurrentlyOn(BrowserThread::UI) &&
689 render_process_id != -1 &&
690 !RenderProcessHost::FromID(render_process_id)) {
691 // Make the request fail if its initiating renderer is no longer valid.
692 // This can happen when the IO thread posts this task just before the
693 // renderer shuts down.
694 source->SendResponse(request_id, NULL);
695 return;
697 source->source()->StartDataRequest(
698 path,
699 render_process_id,
700 render_frame_id,
701 base::Bind(&URLDataSourceImpl::SendResponse, source, request_id));
704 void URLDataManagerBackend::RemoveRequest(URLRequestChromeJob* job) {
705 // Remove the request from our list of pending requests.
706 // If/when the source sends the data that was requested, the data will just
707 // be thrown away.
708 for (PendingRequestMap::iterator i = pending_requests_.begin();
709 i != pending_requests_.end(); ++i) {
710 if (i->second == job) {
711 pending_requests_.erase(i);
712 return;
717 void URLDataManagerBackend::DataAvailable(RequestID request_id,
718 base::RefCountedMemory* bytes) {
719 // Forward this data on to the pending net::URLRequest, if it exists.
720 PendingRequestMap::iterator i = pending_requests_.find(request_id);
721 if (i != pending_requests_.end()) {
722 URLRequestChromeJob* job = i->second;
723 pending_requests_.erase(i);
724 job->DataAvailable(bytes);
728 namespace {
730 class DevToolsJobFactory
731 : public net::URLRequestJobFactory::ProtocolHandler {
732 public:
733 // |is_incognito| should be set for incognito profiles.
734 DevToolsJobFactory(content::ResourceContext* resource_context,
735 bool is_incognito);
736 ~DevToolsJobFactory() override;
738 net::URLRequestJob* MaybeCreateJob(
739 net::URLRequest* request,
740 net::NetworkDelegate* network_delegate) const override;
742 private:
743 // |resource_context_| and |network_delegate_| are owned by ProfileIOData,
744 // which owns this ProtocolHandler.
745 content::ResourceContext* const resource_context_;
747 // True when generated from an incognito profile.
748 const bool is_incognito_;
750 DISALLOW_COPY_AND_ASSIGN(DevToolsJobFactory);
753 DevToolsJobFactory::DevToolsJobFactory(
754 content::ResourceContext* resource_context,
755 bool is_incognito)
756 : resource_context_(resource_context),
757 is_incognito_(is_incognito) {
758 DCHECK(resource_context_);
761 DevToolsJobFactory::~DevToolsJobFactory() {}
763 net::URLRequestJob*
764 DevToolsJobFactory::MaybeCreateJob(
765 net::URLRequest* request, net::NetworkDelegate* network_delegate) const {
766 return new URLRequestChromeJob(
767 request, network_delegate,
768 GetURLDataManagerForResourceContext(resource_context_), is_incognito_);
771 } // namespace
773 net::URLRequestJobFactory::ProtocolHandler*
774 CreateDevToolsProtocolHandler(content::ResourceContext* resource_context,
775 bool is_incognito) {
776 return new DevToolsJobFactory(resource_context, is_incognito);
779 } // namespace content