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(ChromeURLDataManager::DataSource* source, | 393 void GetMimeTypeOnUI(URLDataSource* 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->GetMimeType(path); | 397 std::string mime_type = source->delegate()->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 AddDataSource(new SharedResourcesDataSource()); | 442 content::URLDataSourceDelegate* shared_source = |
| 443 new SharedResourcesDataSource(); |
| 444 shared_source->url_data_source_ = |
| 445 new URLDataSource(shared_source->GetSource(), shared_source); |
| 446 AddDataSource(shared_source->url_data_source_); |
443 } | 447 } |
444 | 448 |
445 ChromeURLDataManagerBackend::~ChromeURLDataManagerBackend() { | 449 ChromeURLDataManagerBackend::~ChromeURLDataManagerBackend() { |
446 for (DataSourceMap::iterator i = data_sources_.begin(); | 450 for (DataSourceMap::iterator i = data_sources_.begin(); |
447 i != data_sources_.end(); ++i) { | 451 i != data_sources_.end(); ++i) { |
448 i->second->backend_ = NULL; | 452 i->second->backend_ = NULL; |
449 } | 453 } |
450 data_sources_.clear(); | 454 data_sources_.clear(); |
451 } | 455 } |
452 | 456 |
453 // static | 457 // static |
454 net::URLRequestJobFactory::ProtocolHandler* | 458 net::URLRequestJobFactory::ProtocolHandler* |
455 ChromeURLDataManagerBackend::CreateProtocolHandler( | 459 ChromeURLDataManagerBackend::CreateProtocolHandler( |
456 ChromeURLDataManagerBackend* backend) { | 460 ChromeURLDataManagerBackend* backend) { |
457 DCHECK(backend); | 461 DCHECK(backend); |
458 return new ChromeProtocolHandler(backend); | 462 return new ChromeProtocolHandler(backend); |
459 } | 463 } |
460 | 464 |
461 void ChromeURLDataManagerBackend::AddDataSource( | 465 void ChromeURLDataManagerBackend::AddDataSource( |
462 ChromeURLDataManager::DataSource* source) { | 466 URLDataSource* source) { |
463 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 467 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
464 DataSourceMap::iterator i = data_sources_.find(source->source_name()); | 468 DataSourceMap::iterator i = data_sources_.find(source->source_name()); |
465 if (i != data_sources_.end()) { | 469 if (i != data_sources_.end()) { |
466 if (!source->ShouldReplaceExistingSource()) | 470 if (!source->delegate()->ShouldReplaceExistingSource()) |
467 return; | 471 return; |
468 i->second->backend_ = NULL; | 472 i->second->backend_ = NULL; |
469 } | 473 } |
470 data_sources_[source->source_name()] = source; | 474 data_sources_[source->source_name()] = source; |
471 source->backend_ = this; | 475 source->backend_ = this; |
472 } | 476 } |
473 | 477 |
474 bool ChromeURLDataManagerBackend::HasPendingJob( | 478 bool ChromeURLDataManagerBackend::HasPendingJob( |
475 URLRequestChromeJob* job) const { | 479 URLRequestChromeJob* job) const { |
476 for (PendingRequestMap::const_iterator i = pending_requests_.begin(); | 480 for (PendingRequestMap::const_iterator i = pending_requests_.begin(); |
477 i != pending_requests_.end(); ++i) { | 481 i != pending_requests_.end(); ++i) { |
478 if (i->second == job) | 482 if (i->second == job) |
479 return true; | 483 return true; |
480 } | 484 } |
481 return false; | 485 return false; |
482 } | 486 } |
483 | 487 |
484 bool ChromeURLDataManagerBackend::StartRequest(const GURL& url, | 488 bool ChromeURLDataManagerBackend::StartRequest(const GURL& url, |
485 URLRequestChromeJob* job) { | 489 URLRequestChromeJob* job) { |
486 // Parse the URL into a request for a source and path. | 490 // Parse the URL into a request for a source and path. |
487 std::string source_name; | 491 std::string source_name; |
488 std::string path; | 492 std::string path; |
489 URLToRequest(url, &source_name, &path); | 493 URLToRequest(url, &source_name, &path); |
490 | 494 |
491 // Look up the data source for the request. | 495 // Look up the data source for the request. |
492 DataSourceMap::iterator i = data_sources_.find(source_name); | 496 DataSourceMap::iterator i = data_sources_.find(source_name); |
493 if (i == data_sources_.end()) | 497 if (i == data_sources_.end()) |
494 return false; | 498 return false; |
495 | 499 |
496 ChromeURLDataManager::DataSource* source = i->second; | 500 URLDataSource* source = i->second; |
497 | 501 |
498 // Save this request so we know where to send the data. | 502 // Save this request so we know where to send the data. |
499 RequestID request_id = next_request_id_++; | 503 RequestID request_id = next_request_id_++; |
500 pending_requests_.insert(std::make_pair(request_id, job)); | 504 pending_requests_.insert(std::make_pair(request_id, job)); |
501 | 505 |
502 job->set_allow_caching(source->AllowCaching()); | 506 job->set_allow_caching(source->delegate()->AllowCaching()); |
503 | 507 |
504 const ChromeURLRequestContext* context = | 508 const ChromeURLRequestContext* context = |
505 static_cast<const ChromeURLRequestContext*>(job->request()->context()); | 509 static_cast<const ChromeURLRequestContext*>(job->request()->context()); |
506 | 510 |
507 // Forward along the request to the data source. | 511 // Forward along the request to the data source. |
508 MessageLoop* target_message_loop = source->MessageLoopForRequestPath(path); | 512 MessageLoop* target_message_loop = |
| 513 source->delegate()->MessageLoopForRequestPath(path); |
509 if (!target_message_loop) { | 514 if (!target_message_loop) { |
510 job->MimeTypeAvailable(source->GetMimeType(path)); | 515 job->MimeTypeAvailable(source->delegate()->GetMimeType(path)); |
511 | 516 |
512 // The DataSource is agnostic to which thread StartDataRequest is called | 517 // The DataSource is agnostic to which thread StartDataRequest is called |
513 // on for this path. Call directly into it from this thread, the IO | 518 // on for this path. Call directly into it from this thread, the IO |
514 // thread. | 519 // thread. |
515 source->StartDataRequest(path, context->is_incognito(), request_id); | 520 source->delegate()->StartDataRequest( |
| 521 path, context->is_incognito(), request_id); |
516 } else { | 522 } else { |
517 // URLRequestChromeJob should receive mime type before data. This | 523 // URLRequestChromeJob should receive mime type before data. This |
518 // is guaranteed because request for mime type is placed in the | 524 // is guaranteed because request for mime type is placed in the |
519 // message loop before request for data. And correspondingly their | 525 // message loop before request for data. And correspondingly their |
520 // replies are put on the IO thread in the same order. | 526 // replies are put on the IO thread in the same order. |
521 target_message_loop->PostTask( | 527 target_message_loop->PostTask( |
522 FROM_HERE, | 528 FROM_HERE, |
523 base::Bind(&GetMimeTypeOnUI, | 529 base::Bind(&GetMimeTypeOnUI, |
524 scoped_refptr<ChromeURLDataManager::DataSource>(source), | 530 scoped_refptr<URLDataSource>(source), |
525 path, job->AsWeakPtr())); | 531 path, job->AsWeakPtr())); |
526 | 532 |
527 // The DataSource wants StartDataRequest to be called on a specific thread, | 533 // The DataSource wants StartDataRequest to be called on a specific thread, |
528 // usually the UI thread, for this path. | 534 // usually the UI thread, for this path. |
529 target_message_loop->PostTask( | 535 target_message_loop->PostTask( |
530 FROM_HERE, | 536 FROM_HERE, |
531 base::Bind(&ChromeURLDataManager::DataSource::StartDataRequest, source, | 537 base::Bind(&ChromeURLDataManagerBackend::CallStartRequest, |
532 path, context->is_incognito(), request_id)); | 538 make_scoped_refptr(source), path, context->is_incognito(), |
| 539 request_id)); |
533 } | 540 } |
534 return true; | 541 return true; |
535 } | 542 } |
536 | 543 |
| 544 void ChromeURLDataManagerBackend::CallStartRequest( |
| 545 scoped_refptr<URLDataSource> source, |
| 546 const std::string& path, |
| 547 bool is_incognito, |
| 548 int request_id) { |
| 549 source->delegate()->StartDataRequest(path, is_incognito, request_id); |
| 550 } |
| 551 |
537 void ChromeURLDataManagerBackend::RemoveRequest(URLRequestChromeJob* job) { | 552 void ChromeURLDataManagerBackend::RemoveRequest(URLRequestChromeJob* job) { |
538 // Remove the request from our list of pending requests. | 553 // Remove the request from our list of pending requests. |
539 // If/when the source sends the data that was requested, the data will just | 554 // If/when the source sends the data that was requested, the data will just |
540 // be thrown away. | 555 // be thrown away. |
541 for (PendingRequestMap::iterator i = pending_requests_.begin(); | 556 for (PendingRequestMap::iterator i = pending_requests_.begin(); |
542 i != pending_requests_.end(); ++i) { | 557 i != pending_requests_.end(); ++i) { |
543 if (i->second == job) { | 558 if (i->second == job) { |
544 pending_requests_.erase(i); | 559 pending_requests_.erase(i); |
545 return; | 560 return; |
546 } | 561 } |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
643 return new URLRequestChromeJob(request, network_delegate, backend_); | 658 return new URLRequestChromeJob(request, network_delegate, backend_); |
644 } | 659 } |
645 | 660 |
646 } // namespace | 661 } // namespace |
647 | 662 |
648 net::URLRequestJobFactory::ProtocolHandler* | 663 net::URLRequestJobFactory::ProtocolHandler* |
649 CreateDevToolsProtocolHandler(ChromeURLDataManagerBackend* backend, | 664 CreateDevToolsProtocolHandler(ChromeURLDataManagerBackend* backend, |
650 net::NetworkDelegate* network_delegate) { | 665 net::NetworkDelegate* network_delegate) { |
651 return new DevToolsJobFactory(backend, network_delegate); | 666 return new DevToolsJobFactory(backend, network_delegate); |
652 } | 667 } |
OLD | NEW |