Disable PDF tests that are failing after combining PDF plugin into chrome.
[chromium-blink-merge.git] / content / browser / webui / url_data_manager_backend.cc
blob100f3cb6396d69e03fe35f5a158f9b1fa5c12c8c
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 // TODO(pkasting): Remove ScopedTracker below once crbug.com/455423 is
341 // fixed.
342 tracked_objects::ScopedTracker tracking_profile(
343 FROM_HERE_WITH_EXPLICIT_FUNCTION(
344 "455423 URLRequestChromeJob::CompleteRead"));
345 CompleteRead(pending_buf_.get(), pending_buf_size_, &bytes_read);
346 pending_buf_ = NULL;
347 NotifyReadComplete(bytes_read);
349 } else {
350 // TODO(pkasting): Remove ScopedTracker below once crbug.com/455423 is
351 // fixed.
352 tracked_objects::ScopedTracker tracking_profile(
353 FROM_HERE_WITH_EXPLICIT_FUNCTION("455423 URLRequestJob::NotifyDone"));
354 // The request failed.
355 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED,
356 net::ERR_FAILED));
360 bool URLRequestChromeJob::ReadRawData(net::IOBuffer* buf, int buf_size,
361 int* bytes_read) {
362 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
363 tracked_objects::ScopedTracker tracking_profile(
364 FROM_HERE_WITH_EXPLICIT_FUNCTION(
365 "423948 URLRequestChromeJob::ReadRawData"));
367 if (!data_.get()) {
368 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0));
369 DCHECK(!pending_buf_.get());
370 CHECK(buf->data());
371 pending_buf_ = buf;
372 pending_buf_size_ = buf_size;
373 return false; // Tell the caller we're still waiting for data.
376 // Otherwise, the data is available.
377 CompleteRead(buf, buf_size, bytes_read);
378 return true;
381 void URLRequestChromeJob::CompleteRead(net::IOBuffer* buf, int buf_size,
382 int* bytes_read) {
383 // http://crbug.com/373841
384 char url_buf[128];
385 base::strlcpy(url_buf, request_->url().spec().c_str(), arraysize(url_buf));
386 base::debug::Alias(url_buf);
388 int remaining = static_cast<int>(data_->size()) - data_offset_;
389 if (buf_size > remaining)
390 buf_size = remaining;
391 if (buf_size > 0) {
392 memcpy(buf->data(), data_->front() + data_offset_, buf_size);
393 data_offset_ += buf_size;
395 *bytes_read = buf_size;
398 void URLRequestChromeJob::CheckStoragePartitionMatches(
399 int render_process_id,
400 const GURL& url,
401 const base::WeakPtr<URLRequestChromeJob>& job) {
402 // The embedder could put some webui pages in separate storage partition.
403 // RenderProcessHostImpl::IsSuitableHost would guard against top level pages
404 // being in the same process. We do an extra check to guard against an
405 // exploited renderer pretending to add them as a subframe. We skip this check
406 // for resources.
407 bool allowed = false;
408 std::vector<std::string> hosts;
409 GetContentClient()->
410 browser()->GetAdditionalWebUIHostsToIgnoreParititionCheck(&hosts);
411 if (url.SchemeIs(kChromeUIScheme) &&
412 (url.SchemeIs(kChromeUIScheme) ||
413 std::find(hosts.begin(), hosts.end(), url.host()) != hosts.end())) {
414 allowed = true;
415 } else if (render_process_id == kNoRenderProcessId) {
416 // Request was not issued by renderer.
417 allowed = true;
418 } else {
419 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id);
420 if (process) {
421 StoragePartition* partition = BrowserContext::GetStoragePartitionForSite(
422 process->GetBrowserContext(), url);
423 allowed = partition == process->GetStoragePartition();
427 BrowserThread::PostTask(
428 BrowserThread::IO,
429 FROM_HERE,
430 base::Bind(&URLRequestChromeJob::StartAsync, job, allowed));
433 void URLRequestChromeJob::StartAsync(bool allowed) {
434 if (!request_)
435 return;
437 if (!allowed || !backend_->StartRequest(request_, this)) {
438 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED,
439 net::ERR_INVALID_URL));
443 namespace {
445 // Gets mime type for data that is available from |source| by |path|.
446 // After that, notifies |job| that mime type is available. This method
447 // should be called on the UI thread, but notification is performed on
448 // the IO thread.
449 void GetMimeTypeOnUI(URLDataSourceImpl* source,
450 const std::string& path,
451 const base::WeakPtr<URLRequestChromeJob>& job) {
452 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
453 std::string mime_type = source->source()->GetMimeType(path);
454 BrowserThread::PostTask(
455 BrowserThread::IO, FROM_HERE,
456 base::Bind(&URLRequestChromeJob::MimeTypeAvailable, job, mime_type));
459 } // namespace
461 namespace {
463 class ChromeProtocolHandler
464 : public net::URLRequestJobFactory::ProtocolHandler {
465 public:
466 // |is_incognito| should be set for incognito profiles.
467 ChromeProtocolHandler(ResourceContext* resource_context,
468 bool is_incognito,
469 AppCacheServiceImpl* appcache_service,
470 ChromeBlobStorageContext* blob_storage_context)
471 : resource_context_(resource_context),
472 is_incognito_(is_incognito),
473 appcache_service_(appcache_service),
474 blob_storage_context_(blob_storage_context) {}
475 ~ChromeProtocolHandler() override {}
477 net::URLRequestJob* MaybeCreateJob(
478 net::URLRequest* request,
479 net::NetworkDelegate* network_delegate) const override {
480 DCHECK(request);
482 // Check for chrome://view-http-cache/*, which uses its own job type.
483 if (ViewHttpCacheJobFactory::IsSupportedURL(request->url()))
484 return ViewHttpCacheJobFactory::CreateJobForRequest(request,
485 network_delegate);
487 // Next check for chrome://appcache-internals/, which uses its own job type.
488 if (request->url().SchemeIs(kChromeUIScheme) &&
489 request->url().host() == kChromeUIAppCacheInternalsHost) {
490 return ViewAppCacheInternalsJobFactory::CreateJobForRequest(
491 request, network_delegate, appcache_service_);
494 // Next check for chrome://blob-internals/, which uses its own job type.
495 if (ViewBlobInternalsJobFactory::IsSupportedURL(request->url())) {
496 return ViewBlobInternalsJobFactory::CreateJobForRequest(
497 request, network_delegate, blob_storage_context_->context());
500 #if defined(USE_TCMALLOC)
501 // Next check for chrome://tcmalloc/, which uses its own job type.
502 if (request->url().SchemeIs(kChromeUIScheme) &&
503 request->url().host() == kChromeUITcmallocHost) {
504 return new TcmallocInternalsRequestJob(request, network_delegate);
506 #endif
508 // Next check for chrome://histograms/, which uses its own job type.
509 if (request->url().SchemeIs(kChromeUIScheme) &&
510 request->url().host() == kChromeUIHistogramHost) {
511 return new HistogramInternalsRequestJob(request, network_delegate);
514 // Fall back to using a custom handler
515 return new URLRequestChromeJob(
516 request, network_delegate,
517 GetURLDataManagerForResourceContext(resource_context_), is_incognito_);
520 bool IsSafeRedirectTarget(const GURL& location) const override {
521 return false;
524 private:
525 // These members are owned by ProfileIOData, which owns this ProtocolHandler.
526 content::ResourceContext* const resource_context_;
528 // True when generated from an incognito profile.
529 const bool is_incognito_;
530 AppCacheServiceImpl* appcache_service_;
531 ChromeBlobStorageContext* blob_storage_context_;
533 DISALLOW_COPY_AND_ASSIGN(ChromeProtocolHandler);
536 } // namespace
538 URLDataManagerBackend::URLDataManagerBackend()
539 : next_request_id_(0) {
540 URLDataSource* shared_source = new SharedResourcesDataSource();
541 URLDataSourceImpl* source_impl =
542 new URLDataSourceImpl(shared_source->GetSource(), shared_source);
543 AddDataSource(source_impl);
546 URLDataManagerBackend::~URLDataManagerBackend() {
547 for (DataSourceMap::iterator i = data_sources_.begin();
548 i != data_sources_.end(); ++i) {
549 i->second->backend_ = NULL;
551 data_sources_.clear();
554 // static
555 net::URLRequestJobFactory::ProtocolHandler*
556 URLDataManagerBackend::CreateProtocolHandler(
557 content::ResourceContext* resource_context,
558 bool is_incognito,
559 AppCacheServiceImpl* appcache_service,
560 ChromeBlobStorageContext* blob_storage_context) {
561 DCHECK(resource_context);
562 return new ChromeProtocolHandler(
563 resource_context, is_incognito, appcache_service, blob_storage_context);
566 void URLDataManagerBackend::AddDataSource(
567 URLDataSourceImpl* source) {
568 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
569 DataSourceMap::iterator i = data_sources_.find(source->source_name());
570 if (i != data_sources_.end()) {
571 if (!source->source()->ShouldReplaceExistingSource())
572 return;
573 i->second->backend_ = NULL;
575 data_sources_[source->source_name()] = source;
576 source->backend_ = this;
579 bool URLDataManagerBackend::HasPendingJob(
580 URLRequestChromeJob* job) const {
581 for (PendingRequestMap::const_iterator i = pending_requests_.begin();
582 i != pending_requests_.end(); ++i) {
583 if (i->second == job)
584 return true;
586 return false;
589 bool URLDataManagerBackend::StartRequest(const net::URLRequest* request,
590 URLRequestChromeJob* job) {
591 if (!CheckURLIsValid(request->url()))
592 return false;
594 URLDataSourceImpl* source = GetDataSourceFromURL(request->url());
595 if (!source)
596 return false;
598 if (!source->source()->ShouldServiceRequest(request))
599 return false;
601 std::string path;
602 URLToRequestPath(request->url(), &path);
603 source->source()->WillServiceRequest(request, &path);
605 // Save this request so we know where to send the data.
606 RequestID request_id = next_request_id_++;
607 pending_requests_.insert(std::make_pair(request_id, job));
609 job->set_allow_caching(source->source()->AllowCaching());
610 job->set_add_content_security_policy(
611 source->source()->ShouldAddContentSecurityPolicy());
612 job->set_content_security_policy_object_source(
613 source->source()->GetContentSecurityPolicyObjectSrc());
614 job->set_content_security_policy_frame_source(
615 source->source()->GetContentSecurityPolicyFrameSrc());
616 job->set_deny_xframe_options(
617 source->source()->ShouldDenyXFrameOptions());
618 job->set_send_content_type_header(
619 source->source()->ShouldServeMimeTypeAsContentTypeHeader());
621 std::string origin = GetOriginHeaderValue(request);
622 if (!origin.empty()) {
623 std::string header =
624 source->source()->GetAccessControlAllowOriginForOrigin(origin);
625 DCHECK(header.empty() || header == origin || header == "*" ||
626 header == "null");
627 job->set_access_control_allow_origin(header);
630 // Look up additional request info to pass down.
631 int render_process_id = -1;
632 int render_frame_id = -1;
633 ResourceRequestInfo::GetRenderFrameForRequest(request,
634 &render_process_id,
635 &render_frame_id);
637 // Forward along the request to the data source.
638 base::MessageLoop* target_message_loop =
639 source->source()->MessageLoopForRequestPath(path);
640 if (!target_message_loop) {
641 job->MimeTypeAvailable(source->source()->GetMimeType(path));
642 // Eliminate potentially dangling pointer to avoid future use.
643 job = NULL;
645 // The DataSource is agnostic to which thread StartDataRequest is called
646 // on for this path. Call directly into it from this thread, the IO
647 // thread.
648 source->source()->StartDataRequest(
649 path, render_process_id, render_frame_id,
650 base::Bind(&URLDataSourceImpl::SendResponse, source, request_id));
651 } else {
652 // URLRequestChromeJob should receive mime type before data. This
653 // is guaranteed because request for mime type is placed in the
654 // message loop before request for data. And correspondingly their
655 // replies are put on the IO thread in the same order.
656 target_message_loop->PostTask(
657 FROM_HERE,
658 base::Bind(&GetMimeTypeOnUI,
659 scoped_refptr<URLDataSourceImpl>(source),
660 path, job->AsWeakPtr()));
662 // The DataSource wants StartDataRequest to be called on a specific thread,
663 // usually the UI thread, for this path.
664 target_message_loop->PostTask(
665 FROM_HERE,
666 base::Bind(&URLDataManagerBackend::CallStartRequest,
667 make_scoped_refptr(source), path, render_process_id,
668 render_frame_id, request_id));
670 return true;
673 URLDataSourceImpl* URLDataManagerBackend::GetDataSourceFromURL(
674 const GURL& url) {
675 // The input usually looks like: chrome://source_name/extra_bits?foo
676 // so do a lookup using the host of the URL.
677 DataSourceMap::iterator i = data_sources_.find(url.host());
678 if (i != data_sources_.end())
679 return i->second.get();
681 // No match using the host of the URL, so do a lookup using the scheme for
682 // URLs on the form source_name://extra_bits/foo .
683 i = data_sources_.find(url.scheme() + "://");
684 if (i != data_sources_.end())
685 return i->second.get();
687 // No matches found, so give up.
688 return NULL;
691 void URLDataManagerBackend::CallStartRequest(
692 scoped_refptr<URLDataSourceImpl> source,
693 const std::string& path,
694 int render_process_id,
695 int render_frame_id,
696 int request_id) {
697 if (BrowserThread::CurrentlyOn(BrowserThread::UI) &&
698 render_process_id != -1 &&
699 !RenderProcessHost::FromID(render_process_id)) {
700 // Make the request fail if its initiating renderer is no longer valid.
701 // This can happen when the IO thread posts this task just before the
702 // renderer shuts down.
703 source->SendResponse(request_id, NULL);
704 return;
706 source->source()->StartDataRequest(
707 path,
708 render_process_id,
709 render_frame_id,
710 base::Bind(&URLDataSourceImpl::SendResponse, source, request_id));
713 void URLDataManagerBackend::RemoveRequest(URLRequestChromeJob* job) {
714 // Remove the request from our list of pending requests.
715 // If/when the source sends the data that was requested, the data will just
716 // be thrown away.
717 for (PendingRequestMap::iterator i = pending_requests_.begin();
718 i != pending_requests_.end(); ++i) {
719 if (i->second == job) {
720 pending_requests_.erase(i);
721 return;
726 void URLDataManagerBackend::DataAvailable(RequestID request_id,
727 base::RefCountedMemory* bytes) {
728 // TODO(pkasting): Remove ScopedTracker below once crbug.com/455423 is fixed.
729 tracked_objects::ScopedTracker tracking_profile(
730 FROM_HERE_WITH_EXPLICIT_FUNCTION(
731 "455423 URLDataManagerBackend::DataAvailable"));
732 // Forward this data on to the pending net::URLRequest, if it exists.
733 PendingRequestMap::iterator i = pending_requests_.find(request_id);
734 if (i != pending_requests_.end()) {
735 URLRequestChromeJob* job = i->second;
736 pending_requests_.erase(i);
737 job->DataAvailable(bytes);
741 namespace {
743 class DevToolsJobFactory
744 : public net::URLRequestJobFactory::ProtocolHandler {
745 public:
746 // |is_incognito| should be set for incognito profiles.
747 DevToolsJobFactory(content::ResourceContext* resource_context,
748 bool is_incognito);
749 ~DevToolsJobFactory() override;
751 net::URLRequestJob* MaybeCreateJob(
752 net::URLRequest* request,
753 net::NetworkDelegate* network_delegate) const override;
755 private:
756 // |resource_context_| and |network_delegate_| are owned by ProfileIOData,
757 // which owns this ProtocolHandler.
758 content::ResourceContext* const resource_context_;
760 // True when generated from an incognito profile.
761 const bool is_incognito_;
763 DISALLOW_COPY_AND_ASSIGN(DevToolsJobFactory);
766 DevToolsJobFactory::DevToolsJobFactory(
767 content::ResourceContext* resource_context,
768 bool is_incognito)
769 : resource_context_(resource_context),
770 is_incognito_(is_incognito) {
771 DCHECK(resource_context_);
774 DevToolsJobFactory::~DevToolsJobFactory() {}
776 net::URLRequestJob*
777 DevToolsJobFactory::MaybeCreateJob(
778 net::URLRequest* request, net::NetworkDelegate* network_delegate) const {
779 return new URLRequestChromeJob(
780 request, network_delegate,
781 GetURLDataManagerForResourceContext(resource_context_), is_incognito_);
784 } // namespace
786 net::URLRequestJobFactory::ProtocolHandler*
787 CreateDevToolsProtocolHandler(content::ResourceContext* resource_context,
788 bool is_incognito) {
789 return new DevToolsJobFactory(resource_context, is_incognito);
792 } // namespace content