Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / content / browser / webui / url_data_manager_backend.cc
blobac346dda7ca827cb786eabfff9963a3d313a1310
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/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/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 const char kChromeURLContentSecurityPolicyHeaderBase[] =
53 "Content-Security-Policy: script-src chrome://resources 'self'";
55 const char kChromeURLXFrameOptionsHeader[] = "X-Frame-Options: DENY";
57 const int kNoRenderProcessId = -1;
59 bool SchemeIsInSchemes(const std::string& scheme,
60 const std::vector<std::string>& schemes) {
61 return std::find(schemes.begin(), schemes.end(), scheme) != schemes.end();
64 // Returns whether |url| passes some sanity checks and is a valid GURL.
65 bool CheckURLIsValid(const GURL& url) {
66 std::vector<std::string> additional_schemes;
67 DCHECK(url.SchemeIs(kChromeDevToolsScheme) || url.SchemeIs(kChromeUIScheme) ||
68 (GetContentClient()->browser()->GetAdditionalWebUISchemes(
69 &additional_schemes),
70 SchemeIsInSchemes(url.scheme(), additional_schemes)));
72 if (!url.is_valid()) {
73 NOTREACHED();
74 return false;
77 return true;
80 // Parse |url| to get the path which will be used to resolve the request. The
81 // path is the remaining portion after the scheme and hostname.
82 void URLToRequestPath(const GURL& url, std::string* path) {
83 const std::string& spec = url.possibly_invalid_spec();
84 const url::Parsed& parsed = url.parsed_for_possibly_invalid_spec();
85 // + 1 to skip the slash at the beginning of the path.
86 int offset = parsed.CountCharactersBefore(url::Parsed::PATH, false) + 1;
88 if (offset < static_cast<int>(spec.size()))
89 path->assign(spec.substr(offset));
92 // Returns a value of 'Origin:' header for the |request| if the header is set.
93 // Otherwise returns an empty string.
94 std::string GetOriginHeaderValue(const net::URLRequest* request) {
95 std::string result;
96 if (request->extra_request_headers().GetHeader(
97 net::HttpRequestHeaders::kOrigin, &result))
98 return result;
99 net::HttpRequestHeaders headers;
100 if (request->GetFullRequestHeaders(&headers))
101 headers.GetHeader(net::HttpRequestHeaders::kOrigin, &result);
102 return result;
105 } // namespace
107 // URLRequestChromeJob is a net::URLRequestJob that manages running
108 // chrome-internal resource requests asynchronously.
109 // It hands off URL requests to ChromeURLDataManager, which asynchronously
110 // calls back once the data is available.
111 class URLRequestChromeJob : public net::URLRequestJob {
112 public:
113 // |is_incognito| set when job is generated from an incognito profile.
114 URLRequestChromeJob(net::URLRequest* request,
115 net::NetworkDelegate* network_delegate,
116 URLDataManagerBackend* backend,
117 bool is_incognito);
119 // net::URLRequestJob implementation.
120 void Start() override;
121 void Kill() override;
122 bool ReadRawData(net::IOBuffer* buf, int buf_size, int* bytes_read) override;
123 bool GetMimeType(std::string* mime_type) const override;
124 int GetResponseCode() const override;
125 void GetResponseInfo(net::HttpResponseInfo* info) override;
127 // Used to notify that the requested data's |mime_type| is ready.
128 void MimeTypeAvailable(const std::string& mime_type);
130 // Called by ChromeURLDataManager to notify us that the data blob is ready
131 // for us.
132 void DataAvailable(base::RefCountedMemory* bytes);
134 // Returns a weak pointer to the job.
135 base::WeakPtr<URLRequestChromeJob> AsWeakPtr();
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 // Specific resources require unsafe-eval in the Content Security Policy.
190 bool RequiresUnsafeEval() const;
192 // Do the actual copy from data_ (the data we're serving) into |buf|.
193 // Separate from ReadRawData so we can handle async I/O.
194 void CompleteRead(net::IOBuffer* buf, int buf_size, int* bytes_read);
196 // The actual data we're serving. NULL until it's been fetched.
197 scoped_refptr<base::RefCountedMemory> data_;
198 // The current offset into the data that we're handing off to our
199 // callers via the Read interfaces.
200 int data_offset_;
202 // For async reads, we keep around a pointer to the buffer that
203 // we're reading into.
204 scoped_refptr<net::IOBuffer> pending_buf_;
205 int pending_buf_size_;
206 std::string mime_type_;
208 // If true, set a header in the response to prevent it from being cached.
209 bool allow_caching_;
211 // If true, set the Content Security Policy (CSP) header.
212 bool add_content_security_policy_;
214 // These are used with the CSP.
215 std::string content_security_policy_object_source_;
216 std::string content_security_policy_frame_source_;
218 // If true, sets the "X-Frame-Options: DENY" header.
219 bool deny_xframe_options_;
221 // If true, sets the "Content-Type: <mime-type>" header.
222 bool send_content_type_header_;
224 // If not empty, "Access-Control-Allow-Origin:" is set to the value of this
225 // string.
226 std::string access_control_allow_origin_;
228 // True when job is generated from an incognito profile.
229 const bool is_incognito_;
231 // The backend is owned by net::URLRequestContext and always outlives us.
232 URLDataManagerBackend* backend_;
234 base::WeakPtrFactory<URLRequestChromeJob> weak_factory_;
236 DISALLOW_COPY_AND_ASSIGN(URLRequestChromeJob);
239 URLRequestChromeJob::URLRequestChromeJob(net::URLRequest* request,
240 net::NetworkDelegate* network_delegate,
241 URLDataManagerBackend* backend,
242 bool is_incognito)
243 : net::URLRequestJob(request, network_delegate),
244 data_offset_(0),
245 pending_buf_size_(0),
246 allow_caching_(true),
247 add_content_security_policy_(true),
248 content_security_policy_object_source_("object-src 'none';"),
249 content_security_policy_frame_source_("frame-src 'none';"),
250 deny_xframe_options_(true),
251 send_content_type_header_(false),
252 is_incognito_(is_incognito),
253 backend_(backend),
254 weak_factory_(this) {
255 DCHECK(backend);
258 URLRequestChromeJob::~URLRequestChromeJob() {
259 CHECK(!backend_->HasPendingJob(this));
262 void URLRequestChromeJob::Start() {
263 int render_process_id, unused;
264 bool is_renderer_request = ResourceRequestInfo::GetRenderFrameForRequest(
265 request_, &render_process_id, &unused);
266 if (!is_renderer_request)
267 render_process_id = kNoRenderProcessId;
268 BrowserThread::PostTask(
269 BrowserThread::UI,
270 FROM_HERE,
271 base::Bind(&URLRequestChromeJob::CheckStoragePartitionMatches,
272 render_process_id, request_->url(),
273 weak_factory_.GetWeakPtr()));
274 TRACE_EVENT_ASYNC_BEGIN1("browser", "DataManager:Request", this, "URL",
275 request_->url().possibly_invalid_spec());
278 void URLRequestChromeJob::Kill() {
279 weak_factory_.InvalidateWeakPtrs();
280 backend_->RemoveRequest(this);
281 URLRequestJob::Kill();
284 bool URLRequestChromeJob::GetMimeType(std::string* mime_type) const {
285 *mime_type = mime_type_;
286 return !mime_type_.empty();
289 int URLRequestChromeJob::GetResponseCode() const {
290 return net::HTTP_OK;
293 void URLRequestChromeJob::GetResponseInfo(net::HttpResponseInfo* info) {
294 DCHECK(!info->headers.get());
295 // Set the headers so that requests serviced by ChromeURLDataManager return a
296 // status code of 200. Without this they return a 0, which makes the status
297 // indistiguishable from other error types. Instant relies on getting a 200.
298 info->headers = new net::HttpResponseHeaders("HTTP/1.1 200 OK");
300 // Determine the least-privileged content security policy header, if any,
301 // that is compatible with a given WebUI URL, and append it to the existing
302 // response headers.
303 if (add_content_security_policy_) {
304 std::string base = kChromeURLContentSecurityPolicyHeaderBase;
305 base.append(RequiresUnsafeEval() ? " 'unsafe-eval'; " : "; ");
306 base.append(content_security_policy_object_source_);
307 base.append(content_security_policy_frame_source_);
308 info->headers->AddHeader(base);
311 if (deny_xframe_options_)
312 info->headers->AddHeader(kChromeURLXFrameOptionsHeader);
314 if (!allow_caching_)
315 info->headers->AddHeader("Cache-Control: no-cache");
317 if (send_content_type_header_ && !mime_type_.empty()) {
318 std::string content_type =
319 base::StringPrintf("%s:%s", net::HttpRequestHeaders::kContentType,
320 mime_type_.c_str());
321 info->headers->AddHeader(content_type);
324 if (!access_control_allow_origin_.empty()) {
325 info->headers->AddHeader("Access-Control-Allow-Origin: " +
326 access_control_allow_origin_);
327 info->headers->AddHeader("Vary: Origin");
331 void URLRequestChromeJob::MimeTypeAvailable(const std::string& mime_type) {
332 set_mime_type(mime_type);
333 NotifyHeadersComplete();
336 void URLRequestChromeJob::DataAvailable(base::RefCountedMemory* bytes) {
337 TRACE_EVENT_ASYNC_END0("browser", "DataManager:Request", this);
338 if (bytes) {
339 // The request completed, and we have all the data.
340 // Clear any IO pending status.
341 SetStatus(net::URLRequestStatus());
343 data_ = bytes;
344 int bytes_read;
345 if (pending_buf_.get()) {
346 CHECK(pending_buf_->data());
347 CompleteRead(pending_buf_.get(), pending_buf_size_, &bytes_read);
348 pending_buf_ = NULL;
349 NotifyReadComplete(bytes_read);
351 } else {
352 // The request failed.
353 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED,
354 net::ERR_FAILED));
358 base::WeakPtr<URLRequestChromeJob> URLRequestChromeJob::AsWeakPtr() {
359 return weak_factory_.GetWeakPtr();
362 bool URLRequestChromeJob::ReadRawData(net::IOBuffer* buf, int buf_size,
363 int* bytes_read) {
364 if (!data_.get()) {
365 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0));
366 DCHECK(!pending_buf_.get());
367 CHECK(buf->data());
368 pending_buf_ = buf;
369 pending_buf_size_ = buf_size;
370 return false; // Tell the caller we're still waiting for data.
373 // Otherwise, the data is available.
374 CompleteRead(buf, buf_size, bytes_read);
375 return true;
378 void URLRequestChromeJob::CompleteRead(net::IOBuffer* buf, int buf_size,
379 int* bytes_read) {
380 int remaining = static_cast<int>(data_->size()) - data_offset_;
381 if (buf_size > remaining)
382 buf_size = remaining;
383 if (buf_size > 0) {
384 // TODO(pkasting): Remove ScopedTracker below once crbug.com/455423 is
385 // fixed.
386 tracked_objects::ScopedTracker tracking_profile(
387 FROM_HERE_WITH_EXPLICIT_FUNCTION(
388 "455423 URLRequestChromeJob::CompleteRead memcpy"));
389 memcpy(buf->data(), data_->front() + data_offset_, buf_size);
390 data_offset_ += buf_size;
392 *bytes_read = buf_size;
395 void URLRequestChromeJob::CheckStoragePartitionMatches(
396 int render_process_id,
397 const GURL& url,
398 const base::WeakPtr<URLRequestChromeJob>& job) {
399 // The embedder could put some webui pages in separate storage partition.
400 // RenderProcessHostImpl::IsSuitableHost would guard against top level pages
401 // being in the same process. We do an extra check to guard against an
402 // exploited renderer pretending to add them as a subframe. We skip this check
403 // for resources.
404 bool allowed = false;
405 std::vector<std::string> hosts;
406 GetContentClient()->
407 browser()->GetAdditionalWebUIHostsToIgnoreParititionCheck(&hosts);
408 if (url.SchemeIs(kChromeUIScheme) &&
409 (url.SchemeIs(kChromeUIScheme) ||
410 std::find(hosts.begin(), hosts.end(), url.host()) != hosts.end())) {
411 allowed = true;
412 } else if (render_process_id == kNoRenderProcessId) {
413 // Request was not issued by renderer.
414 allowed = true;
415 } else {
416 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id);
417 if (process) {
418 StoragePartition* partition = BrowserContext::GetStoragePartitionForSite(
419 process->GetBrowserContext(), url);
420 allowed = partition == process->GetStoragePartition();
424 BrowserThread::PostTask(
425 BrowserThread::IO,
426 FROM_HERE,
427 base::Bind(&URLRequestChromeJob::StartAsync, job, allowed));
430 void URLRequestChromeJob::StartAsync(bool allowed) {
431 if (!request_)
432 return;
434 if (!allowed || !backend_->StartRequest(request_, this)) {
435 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED,
436 net::ERR_INVALID_URL));
440 // TODO(tsepez,mfoltz): Refine this method when tests have been fixed to not use
441 // eval()/new Function(). http://crbug.com/525224
442 bool URLRequestChromeJob::RequiresUnsafeEval() const {
443 return true;
446 namespace {
448 // Gets mime type for data that is available from |source| by |path|.
449 // After that, notifies |job| that mime type is available. This method
450 // should be called on the UI thread, but notification is performed on
451 // the IO thread.
452 void GetMimeTypeOnUI(URLDataSourceImpl* source,
453 const std::string& path,
454 const base::WeakPtr<URLRequestChromeJob>& job) {
455 DCHECK_CURRENTLY_ON(BrowserThread::UI);
456 std::string mime_type = source->source()->GetMimeType(path);
457 BrowserThread::PostTask(
458 BrowserThread::IO, FROM_HERE,
459 base::Bind(&URLRequestChromeJob::MimeTypeAvailable, job, mime_type));
462 } // namespace
464 namespace {
466 class ChromeProtocolHandler
467 : public net::URLRequestJobFactory::ProtocolHandler {
468 public:
469 // |is_incognito| should be set for incognito profiles.
470 ChromeProtocolHandler(ResourceContext* resource_context,
471 bool is_incognito,
472 AppCacheServiceImpl* appcache_service,
473 ChromeBlobStorageContext* blob_storage_context)
474 : resource_context_(resource_context),
475 is_incognito_(is_incognito),
476 appcache_service_(appcache_service),
477 blob_storage_context_(blob_storage_context) {}
478 ~ChromeProtocolHandler() override {}
480 net::URLRequestJob* MaybeCreateJob(
481 net::URLRequest* request,
482 net::NetworkDelegate* network_delegate) const override {
483 DCHECK(request);
485 // Check for chrome://view-http-cache/*, which uses its own job type.
486 if (ViewHttpCacheJobFactory::IsSupportedURL(request->url()))
487 return ViewHttpCacheJobFactory::CreateJobForRequest(request,
488 network_delegate);
490 // Next check for chrome://blob-internals/, which uses its own job type.
491 if (ViewBlobInternalsJobFactory::IsSupportedURL(request->url())) {
492 return ViewBlobInternalsJobFactory::CreateJobForRequest(
493 request, network_delegate, blob_storage_context_->context());
496 #if defined(USE_TCMALLOC)
497 // Next check for chrome://tcmalloc/, which uses its own job type.
498 if (request->url().SchemeIs(kChromeUIScheme) &&
499 request->url().host() == kChromeUITcmallocHost) {
500 return new TcmallocInternalsRequestJob(request, network_delegate);
502 #endif
504 // Next check for chrome://histograms/, which uses its own job type.
505 if (request->url().SchemeIs(kChromeUIScheme) &&
506 request->url().host() == kChromeUIHistogramHost) {
507 return new HistogramInternalsRequestJob(request, network_delegate);
510 // Fall back to using a custom handler
511 return new URLRequestChromeJob(
512 request, network_delegate,
513 GetURLDataManagerForResourceContext(resource_context_), is_incognito_);
516 bool IsSafeRedirectTarget(const GURL& location) const override {
517 return false;
520 private:
521 // These members are owned by ProfileIOData, which owns this ProtocolHandler.
522 content::ResourceContext* const resource_context_;
524 // True when generated from an incognito profile.
525 const bool is_incognito_;
526 AppCacheServiceImpl* appcache_service_;
527 ChromeBlobStorageContext* blob_storage_context_;
529 DISALLOW_COPY_AND_ASSIGN(ChromeProtocolHandler);
532 } // namespace
534 URLDataManagerBackend::URLDataManagerBackend()
535 : next_request_id_(0) {
536 URLDataSource* shared_source = new SharedResourcesDataSource();
537 URLDataSourceImpl* source_impl =
538 new URLDataSourceImpl(shared_source->GetSource(), shared_source);
539 AddDataSource(source_impl);
542 URLDataManagerBackend::~URLDataManagerBackend() {
543 for (DataSourceMap::iterator i = data_sources_.begin();
544 i != data_sources_.end(); ++i) {
545 i->second->backend_ = NULL;
547 data_sources_.clear();
550 // static
551 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>
552 URLDataManagerBackend::CreateProtocolHandler(
553 content::ResourceContext* resource_context,
554 bool is_incognito,
555 AppCacheServiceImpl* appcache_service,
556 ChromeBlobStorageContext* blob_storage_context) {
557 DCHECK(resource_context);
558 return make_scoped_ptr(new ChromeProtocolHandler(
559 resource_context, is_incognito, appcache_service, blob_storage_context));
562 void URLDataManagerBackend::AddDataSource(
563 URLDataSourceImpl* source) {
564 DCHECK_CURRENTLY_ON(BrowserThread::IO);
565 DataSourceMap::iterator i = data_sources_.find(source->source_name());
566 if (i != data_sources_.end()) {
567 if (!source->source()->ShouldReplaceExistingSource())
568 return;
569 i->second->backend_ = NULL;
571 data_sources_[source->source_name()] = source;
572 source->backend_ = this;
575 bool URLDataManagerBackend::HasPendingJob(
576 URLRequestChromeJob* job) const {
577 for (PendingRequestMap::const_iterator i = pending_requests_.begin();
578 i != pending_requests_.end(); ++i) {
579 if (i->second == job)
580 return true;
582 return false;
585 bool URLDataManagerBackend::StartRequest(const net::URLRequest* request,
586 URLRequestChromeJob* job) {
587 if (!CheckURLIsValid(request->url()))
588 return false;
590 URLDataSourceImpl* source = GetDataSourceFromURL(request->url());
591 if (!source)
592 return false;
594 if (!source->source()->ShouldServiceRequest(request))
595 return false;
597 std::string path;
598 URLToRequestPath(request->url(), &path);
599 source->source()->WillServiceRequest(request, &path);
601 // Save this request so we know where to send the data.
602 RequestID request_id = next_request_id_++;
603 pending_requests_.insert(std::make_pair(request_id, job));
605 job->set_allow_caching(source->source()->AllowCaching());
606 job->set_add_content_security_policy(
607 source->source()->ShouldAddContentSecurityPolicy());
608 job->set_content_security_policy_object_source(
609 source->source()->GetContentSecurityPolicyObjectSrc());
610 job->set_content_security_policy_frame_source(
611 source->source()->GetContentSecurityPolicyFrameSrc());
612 job->set_deny_xframe_options(
613 source->source()->ShouldDenyXFrameOptions());
614 job->set_send_content_type_header(
615 source->source()->ShouldServeMimeTypeAsContentTypeHeader());
617 std::string origin = GetOriginHeaderValue(request);
618 if (!origin.empty()) {
619 std::string header =
620 source->source()->GetAccessControlAllowOriginForOrigin(origin);
621 DCHECK(header.empty() || header == origin || header == "*" ||
622 header == "null");
623 job->set_access_control_allow_origin(header);
626 // Look up additional request info to pass down.
627 int render_process_id = -1;
628 int render_frame_id = -1;
629 ResourceRequestInfo::GetRenderFrameForRequest(request,
630 &render_process_id,
631 &render_frame_id);
633 // Forward along the request to the data source.
634 base::MessageLoop* target_message_loop =
635 source->source()->MessageLoopForRequestPath(path);
636 if (!target_message_loop) {
637 job->MimeTypeAvailable(source->source()->GetMimeType(path));
638 // Eliminate potentially dangling pointer to avoid future use.
639 job = NULL;
641 // The DataSource is agnostic to which thread StartDataRequest is called
642 // on for this path. Call directly into it from this thread, the IO
643 // thread.
644 source->source()->StartDataRequest(
645 path, render_process_id, render_frame_id,
646 base::Bind(&URLDataSourceImpl::SendResponse, source, request_id));
647 } else {
648 // URLRequestChromeJob should receive mime type before data. This
649 // is guaranteed because request for mime type is placed in the
650 // message loop before request for data. And correspondingly their
651 // replies are put on the IO thread in the same order.
652 target_message_loop->task_runner()->PostTask(
653 FROM_HERE,
654 base::Bind(&GetMimeTypeOnUI, scoped_refptr<URLDataSourceImpl>(source),
655 path, job->AsWeakPtr()));
657 // The DataSource wants StartDataRequest to be called on a specific thread,
658 // usually the UI thread, for this path.
659 target_message_loop->task_runner()->PostTask(
660 FROM_HERE, base::Bind(&URLDataManagerBackend::CallStartRequest,
661 make_scoped_refptr(source), path,
662 render_process_id, render_frame_id, request_id));
664 return true;
667 URLDataSourceImpl* URLDataManagerBackend::GetDataSourceFromURL(
668 const GURL& url) {
669 // The input usually looks like: chrome://source_name/extra_bits?foo
670 // so do a lookup using the host of the URL.
671 DataSourceMap::iterator i = data_sources_.find(url.host());
672 if (i != data_sources_.end())
673 return i->second.get();
675 // No match using the host of the URL, so do a lookup using the scheme for
676 // URLs on the form source_name://extra_bits/foo .
677 i = data_sources_.find(url.scheme() + "://");
678 if (i != data_sources_.end())
679 return i->second.get();
681 // No matches found, so give up.
682 return NULL;
685 void URLDataManagerBackend::CallStartRequest(
686 scoped_refptr<URLDataSourceImpl> source,
687 const std::string& path,
688 int render_process_id,
689 int render_frame_id,
690 int request_id) {
691 if (BrowserThread::CurrentlyOn(BrowserThread::UI) &&
692 render_process_id != -1 &&
693 !RenderProcessHost::FromID(render_process_id)) {
694 // Make the request fail if its initiating renderer is no longer valid.
695 // This can happen when the IO thread posts this task just before the
696 // renderer shuts down.
697 source->SendResponse(request_id, NULL);
698 return;
700 source->source()->StartDataRequest(
701 path,
702 render_process_id,
703 render_frame_id,
704 base::Bind(&URLDataSourceImpl::SendResponse, source, request_id));
707 void URLDataManagerBackend::RemoveRequest(URLRequestChromeJob* job) {
708 // Remove the request from our list of pending requests.
709 // If/when the source sends the data that was requested, the data will just
710 // be thrown away.
711 for (PendingRequestMap::iterator i = pending_requests_.begin();
712 i != pending_requests_.end(); ++i) {
713 if (i->second == job) {
714 pending_requests_.erase(i);
715 return;
720 void URLDataManagerBackend::DataAvailable(RequestID request_id,
721 base::RefCountedMemory* bytes) {
722 // Forward this data on to the pending net::URLRequest, if it exists.
723 PendingRequestMap::iterator i = pending_requests_.find(request_id);
724 if (i != pending_requests_.end()) {
725 URLRequestChromeJob* job = i->second;
726 pending_requests_.erase(i);
727 job->DataAvailable(bytes);
731 namespace {
733 class DevToolsJobFactory
734 : public net::URLRequestJobFactory::ProtocolHandler {
735 public:
736 // |is_incognito| should be set for incognito profiles.
737 DevToolsJobFactory(content::ResourceContext* resource_context,
738 bool is_incognito);
739 ~DevToolsJobFactory() override;
741 net::URLRequestJob* MaybeCreateJob(
742 net::URLRequest* request,
743 net::NetworkDelegate* network_delegate) const override;
745 private:
746 // |resource_context_| and |network_delegate_| are owned by ProfileIOData,
747 // which owns this ProtocolHandler.
748 content::ResourceContext* const resource_context_;
750 // True when generated from an incognito profile.
751 const bool is_incognito_;
753 DISALLOW_COPY_AND_ASSIGN(DevToolsJobFactory);
756 DevToolsJobFactory::DevToolsJobFactory(
757 content::ResourceContext* resource_context,
758 bool is_incognito)
759 : resource_context_(resource_context),
760 is_incognito_(is_incognito) {
761 DCHECK(resource_context_);
764 DevToolsJobFactory::~DevToolsJobFactory() {}
766 net::URLRequestJob*
767 DevToolsJobFactory::MaybeCreateJob(
768 net::URLRequest* request, net::NetworkDelegate* network_delegate) const {
769 return new URLRequestChromeJob(
770 request, network_delegate,
771 GetURLDataManagerForResourceContext(resource_context_), is_incognito_);
774 } // namespace
776 net::URLRequestJobFactory::ProtocolHandler*
777 CreateDevToolsProtocolHandler(content::ResourceContext* resource_context,
778 bool is_incognito) {
779 return new DevToolsJobFactory(resource_context, is_incognito);
782 } // namespace content