OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/chrome_url_data_manager_backend.h" | 5 #include "chrome/browser/ui/webui/chrome_url_data_manager_backend.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 net::ERR_INVALID_URL)); | 383 net::ERR_INVALID_URL)); |
384 } | 384 } |
385 } | 385 } |
386 | 386 |
387 namespace { | 387 namespace { |
388 | 388 |
389 // Gets mime type for data that is available from |source| by |path|. | 389 // Gets mime type for data that is available from |source| by |path|. |
390 // After that, notifies |job| that mime type is available. This method | 390 // After that, notifies |job| that mime type is available. This method |
391 // should be called on the UI thread, but notification is performed on | 391 // should be called on the UI thread, but notification is performed on |
392 // the IO thread. | 392 // the IO thread. |
393 void GetMimeTypeOnUI(URLDataSource* source, | 393 void GetMimeTypeOnUI(URLDataSourceImpl* source, |
394 const std::string& path, | 394 const std::string& path, |
395 const base::WeakPtr<URLRequestChromeJob>& job) { | 395 const base::WeakPtr<URLRequestChromeJob>& job) { |
396 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 396 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
397 std::string mime_type = source->delegate()->GetMimeType(path); | 397 std::string mime_type = source->source()->GetMimeType(path); |
398 BrowserThread::PostTask( | 398 BrowserThread::PostTask( |
399 BrowserThread::IO, FROM_HERE, | 399 BrowserThread::IO, FROM_HERE, |
400 base::Bind(&URLRequestChromeJob::MimeTypeAvailable, job, mime_type)); | 400 base::Bind(&URLRequestChromeJob::MimeTypeAvailable, job, mime_type)); |
401 } | 401 } |
402 | 402 |
403 } // namespace | 403 } // namespace |
404 | 404 |
405 namespace { | 405 namespace { |
406 | 406 |
407 class ChromeProtocolHandler | 407 class ChromeProtocolHandler |
(...skipping 24 matching lines...) Expand all Loading... |
432 DCHECK(request); | 432 DCHECK(request); |
433 | 433 |
434 // Fall back to using a custom handler | 434 // Fall back to using a custom handler |
435 return new URLRequestChromeJob(request, network_delegate, backend_); | 435 return new URLRequestChromeJob(request, network_delegate, backend_); |
436 } | 436 } |
437 | 437 |
438 } // namespace | 438 } // namespace |
439 | 439 |
440 ChromeURLDataManagerBackend::ChromeURLDataManagerBackend() | 440 ChromeURLDataManagerBackend::ChromeURLDataManagerBackend() |
441 : next_request_id_(0) { | 441 : next_request_id_(0) { |
442 content::URLDataSourceDelegate* shared_source = | 442 content::URLDataSource* shared_source = new SharedResourcesDataSource(); |
443 new SharedResourcesDataSource(); | 443 URLDataSourceImpl* source_impl = |
444 shared_source->url_data_source_ = | 444 new URLDataSourceImpl(shared_source->GetSource(), shared_source); |
445 new URLDataSource(shared_source->GetSource(), shared_source); | 445 AddDataSource(source_impl); |
446 AddDataSource(shared_source->url_data_source_); | |
447 } | 446 } |
448 | 447 |
449 ChromeURLDataManagerBackend::~ChromeURLDataManagerBackend() { | 448 ChromeURLDataManagerBackend::~ChromeURLDataManagerBackend() { |
450 for (DataSourceMap::iterator i = data_sources_.begin(); | 449 for (DataSourceMap::iterator i = data_sources_.begin(); |
451 i != data_sources_.end(); ++i) { | 450 i != data_sources_.end(); ++i) { |
452 i->second->backend_ = NULL; | 451 i->second->backend_ = NULL; |
453 } | 452 } |
454 data_sources_.clear(); | 453 data_sources_.clear(); |
455 } | 454 } |
456 | 455 |
457 // static | 456 // static |
458 net::URLRequestJobFactory::ProtocolHandler* | 457 net::URLRequestJobFactory::ProtocolHandler* |
459 ChromeURLDataManagerBackend::CreateProtocolHandler( | 458 ChromeURLDataManagerBackend::CreateProtocolHandler( |
460 ChromeURLDataManagerBackend* backend) { | 459 ChromeURLDataManagerBackend* backend) { |
461 DCHECK(backend); | 460 DCHECK(backend); |
462 return new ChromeProtocolHandler(backend); | 461 return new ChromeProtocolHandler(backend); |
463 } | 462 } |
464 | 463 |
465 void ChromeURLDataManagerBackend::AddDataSource( | 464 void ChromeURLDataManagerBackend::AddDataSource( |
466 URLDataSource* source) { | 465 URLDataSourceImpl* source) { |
467 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 466 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
468 DataSourceMap::iterator i = data_sources_.find(source->source_name()); | 467 DataSourceMap::iterator i = data_sources_.find(source->source_name()); |
469 if (i != data_sources_.end()) { | 468 if (i != data_sources_.end()) { |
470 if (!source->delegate()->ShouldReplaceExistingSource()) | 469 if (!source->source()->ShouldReplaceExistingSource()) |
471 return; | 470 return; |
472 i->second->backend_ = NULL; | 471 i->second->backend_ = NULL; |
473 } | 472 } |
474 data_sources_[source->source_name()] = source; | 473 data_sources_[source->source_name()] = source; |
475 source->backend_ = this; | 474 source->backend_ = this; |
476 } | 475 } |
477 | 476 |
478 bool ChromeURLDataManagerBackend::HasPendingJob( | 477 bool ChromeURLDataManagerBackend::HasPendingJob( |
479 URLRequestChromeJob* job) const { | 478 URLRequestChromeJob* job) const { |
480 for (PendingRequestMap::const_iterator i = pending_requests_.begin(); | 479 for (PendingRequestMap::const_iterator i = pending_requests_.begin(); |
481 i != pending_requests_.end(); ++i) { | 480 i != pending_requests_.end(); ++i) { |
482 if (i->second == job) | 481 if (i->second == job) |
483 return true; | 482 return true; |
484 } | 483 } |
485 return false; | 484 return false; |
486 } | 485 } |
487 | 486 |
488 bool ChromeURLDataManagerBackend::StartRequest(const GURL& url, | 487 bool ChromeURLDataManagerBackend::StartRequest(const GURL& url, |
489 URLRequestChromeJob* job) { | 488 URLRequestChromeJob* job) { |
490 // Parse the URL into a request for a source and path. | 489 // Parse the URL into a request for a source and path. |
491 std::string source_name; | 490 std::string source_name; |
492 std::string path; | 491 std::string path; |
493 URLToRequest(url, &source_name, &path); | 492 URLToRequest(url, &source_name, &path); |
494 | 493 |
495 // Look up the data source for the request. | 494 // Look up the data source for the request. |
496 DataSourceMap::iterator i = data_sources_.find(source_name); | 495 DataSourceMap::iterator i = data_sources_.find(source_name); |
497 if (i == data_sources_.end()) | 496 if (i == data_sources_.end()) |
498 return false; | 497 return false; |
499 | 498 |
500 URLDataSource* source = i->second; | 499 URLDataSourceImpl* source = i->second; |
501 | 500 |
502 // Save this request so we know where to send the data. | 501 // Save this request so we know where to send the data. |
503 RequestID request_id = next_request_id_++; | 502 RequestID request_id = next_request_id_++; |
504 pending_requests_.insert(std::make_pair(request_id, job)); | 503 pending_requests_.insert(std::make_pair(request_id, job)); |
505 | 504 |
506 job->set_allow_caching(source->delegate()->AllowCaching()); | 505 job->set_allow_caching(source->source()->AllowCaching()); |
507 | 506 |
508 const ChromeURLRequestContext* context = | 507 const ChromeURLRequestContext* context = |
509 static_cast<const ChromeURLRequestContext*>(job->request()->context()); | 508 static_cast<const ChromeURLRequestContext*>(job->request()->context()); |
510 | 509 |
511 // Forward along the request to the data source. | 510 // Forward along the request to the data source. |
512 MessageLoop* target_message_loop = | 511 MessageLoop* target_message_loop = |
513 source->delegate()->MessageLoopForRequestPath(path); | 512 source->source()->MessageLoopForRequestPath(path); |
514 if (!target_message_loop) { | 513 if (!target_message_loop) { |
515 job->MimeTypeAvailable(source->delegate()->GetMimeType(path)); | 514 job->MimeTypeAvailable(source->source()->GetMimeType(path)); |
516 | 515 |
517 // The DataSource is agnostic to which thread StartDataRequest is called | 516 // The DataSource is agnostic to which thread StartDataRequest is called |
518 // on for this path. Call directly into it from this thread, the IO | 517 // on for this path. Call directly into it from this thread, the IO |
519 // thread. | 518 // thread. |
520 source->delegate()->StartDataRequest( | 519 source->source()->StartDataRequest( |
521 path, context->is_incognito(), request_id); | 520 path, context->is_incognito(), |
| 521 base::Bind(&URLDataSourceImpl::SendResponse, source, request_id)); |
522 } else { | 522 } else { |
523 // URLRequestChromeJob should receive mime type before data. This | 523 // URLRequestChromeJob should receive mime type before data. This |
524 // is guaranteed because request for mime type is placed in the | 524 // is guaranteed because request for mime type is placed in the |
525 // message loop before request for data. And correspondingly their | 525 // message loop before request for data. And correspondingly their |
526 // replies are put on the IO thread in the same order. | 526 // replies are put on the IO thread in the same order. |
527 target_message_loop->PostTask( | 527 target_message_loop->PostTask( |
528 FROM_HERE, | 528 FROM_HERE, |
529 base::Bind(&GetMimeTypeOnUI, | 529 base::Bind(&GetMimeTypeOnUI, |
530 scoped_refptr<URLDataSource>(source), | 530 scoped_refptr<URLDataSourceImpl>(source), |
531 path, job->AsWeakPtr())); | 531 path, job->AsWeakPtr())); |
532 | 532 |
533 // The DataSource wants StartDataRequest to be called on a specific thread, | 533 // The DataSource wants StartDataRequest to be called on a specific thread, |
534 // usually the UI thread, for this path. | 534 // usually the UI thread, for this path. |
535 target_message_loop->PostTask( | 535 target_message_loop->PostTask( |
536 FROM_HERE, | 536 FROM_HERE, |
537 base::Bind(&ChromeURLDataManagerBackend::CallStartRequest, | 537 base::Bind(&ChromeURLDataManagerBackend::CallStartRequest, |
538 make_scoped_refptr(source), path, context->is_incognito(), | 538 make_scoped_refptr(source), path, context->is_incognito(), |
539 request_id)); | 539 request_id)); |
540 } | 540 } |
541 return true; | 541 return true; |
542 } | 542 } |
543 | 543 |
544 void ChromeURLDataManagerBackend::CallStartRequest( | 544 void ChromeURLDataManagerBackend::CallStartRequest( |
545 scoped_refptr<URLDataSource> source, | 545 scoped_refptr<URLDataSourceImpl> source, |
546 const std::string& path, | 546 const std::string& path, |
547 bool is_incognito, | 547 bool is_incognito, |
548 int request_id) { | 548 int request_id) { |
549 source->delegate()->StartDataRequest(path, is_incognito, request_id); | 549 source->source()->StartDataRequest( |
| 550 path, |
| 551 is_incognito, |
| 552 base::Bind(&URLDataSourceImpl::SendResponse, source, request_id)); |
550 } | 553 } |
551 | 554 |
552 void ChromeURLDataManagerBackend::RemoveRequest(URLRequestChromeJob* job) { | 555 void ChromeURLDataManagerBackend::RemoveRequest(URLRequestChromeJob* job) { |
553 // Remove the request from our list of pending requests. | 556 // Remove the request from our list of pending requests. |
554 // If/when the source sends the data that was requested, the data will just | 557 // If/when the source sends the data that was requested, the data will just |
555 // be thrown away. | 558 // be thrown away. |
556 for (PendingRequestMap::iterator i = pending_requests_.begin(); | 559 for (PendingRequestMap::iterator i = pending_requests_.begin(); |
557 i != pending_requests_.end(); ++i) { | 560 i != pending_requests_.end(); ++i) { |
558 if (i->second == job) { | 561 if (i->second == job) { |
559 pending_requests_.erase(i); | 562 pending_requests_.erase(i); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
658 return new URLRequestChromeJob(request, network_delegate, backend_); | 661 return new URLRequestChromeJob(request, network_delegate, backend_); |
659 } | 662 } |
660 | 663 |
661 } // namespace | 664 } // namespace |
662 | 665 |
663 net::URLRequestJobFactory::ProtocolHandler* | 666 net::URLRequestJobFactory::ProtocolHandler* |
664 CreateDevToolsProtocolHandler(ChromeURLDataManagerBackend* backend, | 667 CreateDevToolsProtocolHandler(ChromeURLDataManagerBackend* backend, |
665 net::NetworkDelegate* network_delegate) { | 668 net::NetworkDelegate* network_delegate) { |
666 return new DevToolsJobFactory(backend, network_delegate); | 669 return new DevToolsJobFactory(backend, network_delegate); |
667 } | 670 } |
OLD | NEW |