OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "content/browser/worker_host/worker_process_host.h" | 5 #include "content/browser/worker_host/worker_process_host.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/base_switches.h" | 10 #include "base/base_switches.h" |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 } | 274 } |
275 | 275 |
276 void WorkerProcessHost::CreateWorker(const WorkerInstance& instance) { | 276 void WorkerProcessHost::CreateWorker(const WorkerInstance& instance) { |
277 ChildProcessSecurityPolicy::GetInstance()->GrantRequestURL( | 277 ChildProcessSecurityPolicy::GetInstance()->GrantRequestURL( |
278 id(), instance.url()); | 278 id(), instance.url()); |
279 | 279 |
280 instances_.push_back(instance); | 280 instances_.push_back(instance); |
281 | 281 |
282 WorkerProcessMsg_CreateWorker_Params params; | 282 WorkerProcessMsg_CreateWorker_Params params; |
283 params.url = instance.url(); | 283 params.url = instance.url(); |
284 params.is_shared = instance.shared(); | |
285 params.name = instance.name(); | 284 params.name = instance.name(); |
286 params.route_id = instance.worker_route_id(); | 285 params.route_id = instance.worker_route_id(); |
287 params.creator_process_id = instance.parent_process_id(); | 286 params.creator_process_id = instance.parent_process_id(); |
288 params.creator_appcache_host_id = instance.parent_appcache_host_id(); | |
289 params.shared_worker_appcache_id = instance.main_resource_appcache_id(); | 287 params.shared_worker_appcache_id = instance.main_resource_appcache_id(); |
290 Send(new WorkerProcessMsg_CreateWorker(params)); | 288 Send(new WorkerProcessMsg_CreateWorker(params)); |
291 | 289 |
292 UpdateTitle(); | 290 UpdateTitle(); |
293 | 291 |
294 // Walk all pending filters and let them know the worker has been created | 292 // Walk all pending filters and let them know the worker has been created |
295 // (could be more than one in the case where we had to queue up worker | 293 // (could be more than one in the case where we had to queue up worker |
296 // creation because the worker process limit was reached). | 294 // creation because the worker process limit was reached). |
297 for (WorkerInstance::FilterList::const_iterator i = | 295 for (WorkerInstance::FilterList::const_iterator i = |
298 instance.filters().begin(); | 296 instance.filters().begin(); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 if (handled) | 334 if (handled) |
337 return true; | 335 return true; |
338 | 336 |
339 if (message.type() == WorkerHostMsg_WorkerContextDestroyed::ID) { | 337 if (message.type() == WorkerHostMsg_WorkerContextDestroyed::ID) { |
340 WorkerService::GetInstance()->NotifyWorkerDestroyed(this, | 338 WorkerService::GetInstance()->NotifyWorkerDestroyed(this, |
341 message.routing_id()); | 339 message.routing_id()); |
342 } | 340 } |
343 | 341 |
344 for (Instances::iterator i = instances_.begin(); i != instances_.end(); ++i) { | 342 for (Instances::iterator i = instances_.begin(); i != instances_.end(); ++i) { |
345 if (i->worker_route_id() == message.routing_id()) { | 343 if (i->worker_route_id() == message.routing_id()) { |
346 if (!i->shared()) { | |
347 // Don't relay messages from shared workers (all communication is via | |
348 // the message port). | |
349 WorkerInstance::FilterInfo info = i->GetFilter(); | |
350 RelayMessage(message, info.first, info.second); | |
351 } | |
352 | |
353 if (message.type() == WorkerHostMsg_WorkerContextDestroyed::ID) { | 344 if (message.type() == WorkerHostMsg_WorkerContextDestroyed::ID) { |
354 instances_.erase(i); | 345 instances_.erase(i); |
355 UpdateTitle(); | 346 UpdateTitle(); |
356 } | 347 } |
357 return true; | 348 return true; |
358 } | 349 } |
359 } | 350 } |
360 return false; | 351 return false; |
361 } | 352 } |
362 | 353 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 if (message.type() == WorkerMsg_StartWorkerContext::ID) | 439 if (message.type() == WorkerMsg_StartWorkerContext::ID) |
449 WorkerService::GetInstance()->NotifyWorkerContextStarted(this, route_id); | 440 WorkerService::GetInstance()->NotifyWorkerContextStarted(this, route_id); |
450 return; | 441 return; |
451 } | 442 } |
452 } | 443 } |
453 | 444 |
454 void WorkerProcessHost::FilterShutdown(WorkerMessageFilter* filter) { | 445 void WorkerProcessHost::FilterShutdown(WorkerMessageFilter* filter) { |
455 for (Instances::iterator i = instances_.begin(); i != instances_.end();) { | 446 for (Instances::iterator i = instances_.begin(); i != instances_.end();) { |
456 bool shutdown = false; | 447 bool shutdown = false; |
457 i->RemoveFilters(filter); | 448 i->RemoveFilters(filter); |
458 if (i->shared()) { | 449 |
459 i->worker_document_set()->RemoveAll(filter); | 450 i->worker_document_set()->RemoveAll(filter); |
460 if (i->worker_document_set()->IsEmpty()) { | 451 if (i->worker_document_set()->IsEmpty()) { |
461 shutdown = true; | |
462 } | |
463 } else if (i->NumFilters() == 0) { | |
464 shutdown = true; | 452 shutdown = true; |
465 } | 453 } |
466 if (shutdown) { | 454 if (shutdown) { |
467 Send(new WorkerMsg_TerminateWorkerContext(i->worker_route_id())); | 455 Send(new WorkerMsg_TerminateWorkerContext(i->worker_route_id())); |
468 i = instances_.erase(i); | 456 i = instances_.erase(i); |
469 } else { | 457 } else { |
470 ++i; | 458 ++i; |
471 } | 459 } |
472 } | 460 } |
473 } | 461 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
506 display_title += *i; | 494 display_title += *i; |
507 } | 495 } |
508 | 496 |
509 set_name(ASCIIToUTF16(display_title)); | 497 set_name(ASCIIToUTF16(display_title)); |
510 } | 498 } |
511 | 499 |
512 void WorkerProcessHost::DocumentDetached(WorkerMessageFilter* filter, | 500 void WorkerProcessHost::DocumentDetached(WorkerMessageFilter* filter, |
513 unsigned long long document_id) { | 501 unsigned long long document_id) { |
514 // Walk all instances and remove the document from their document set. | 502 // Walk all instances and remove the document from their document set. |
515 for (Instances::iterator i = instances_.begin(); i != instances_.end();) { | 503 for (Instances::iterator i = instances_.begin(); i != instances_.end();) { |
516 if (!i->shared()) { | 504 i->worker_document_set()->Remove(filter, document_id); |
| 505 if (i->worker_document_set()->IsEmpty()) { |
| 506 // This worker has no more associated documents - shut it down. |
| 507 Send(new WorkerMsg_TerminateWorkerContext(i->worker_route_id())); |
| 508 i = instances_.erase(i); |
| 509 } else { |
517 ++i; | 510 ++i; |
518 } else { | |
519 i->worker_document_set()->Remove(filter, document_id); | |
520 if (i->worker_document_set()->IsEmpty()) { | |
521 // This worker has no more associated documents - shut it down. | |
522 Send(new WorkerMsg_TerminateWorkerContext(i->worker_route_id())); | |
523 i = instances_.erase(i); | |
524 } else { | |
525 ++i; | |
526 } | |
527 } | 511 } |
528 } | 512 } |
529 } | 513 } |
530 | 514 |
531 WorkerProcessHost::WorkerInstance::WorkerInstance( | 515 WorkerProcessHost::WorkerInstance::WorkerInstance( |
532 const GURL& url, | 516 const GURL& url, |
533 bool shared, | |
534 const string16& name, | 517 const string16& name, |
535 int worker_route_id, | 518 int worker_route_id, |
536 int parent_process_id, | 519 int parent_process_id, |
537 int parent_appcache_host_id, | |
538 int64 main_resource_appcache_id, | 520 int64 main_resource_appcache_id, |
539 const content::ResourceContext* resource_context) | 521 const content::ResourceContext* resource_context) |
540 : url_(url), | 522 : url_(url), |
541 shared_(shared), | |
542 closed_(false), | 523 closed_(false), |
543 name_(name), | 524 name_(name), |
544 worker_route_id_(worker_route_id), | 525 worker_route_id_(worker_route_id), |
545 parent_process_id_(parent_process_id), | 526 parent_process_id_(parent_process_id), |
546 parent_appcache_host_id_(parent_appcache_host_id), | |
547 main_resource_appcache_id_(main_resource_appcache_id), | 527 main_resource_appcache_id_(main_resource_appcache_id), |
548 worker_document_set_(new WorkerDocumentSet()), | 528 worker_document_set_(new WorkerDocumentSet()), |
549 resource_context_(resource_context) { | 529 resource_context_(resource_context) { |
550 DCHECK(resource_context_); | 530 DCHECK(resource_context_); |
551 } | 531 } |
552 | 532 |
553 WorkerProcessHost::WorkerInstance::WorkerInstance( | 533 WorkerProcessHost::WorkerInstance::WorkerInstance( |
554 const GURL& url, | 534 const GURL& url, |
555 bool shared, | 535 bool shared, |
556 const string16& name, | 536 const string16& name, |
557 const content::ResourceContext* resource_context) | 537 const content::ResourceContext* resource_context) |
558 : url_(url), | 538 : url_(url), |
559 shared_(shared), | |
560 closed_(false), | 539 closed_(false), |
561 name_(name), | 540 name_(name), |
562 worker_route_id_(MSG_ROUTING_NONE), | 541 worker_route_id_(MSG_ROUTING_NONE), |
563 parent_process_id_(0), | 542 parent_process_id_(0), |
564 parent_appcache_host_id_(0), | |
565 main_resource_appcache_id_(0), | 543 main_resource_appcache_id_(0), |
566 worker_document_set_(new WorkerDocumentSet()), | 544 worker_document_set_(new WorkerDocumentSet()), |
567 resource_context_(resource_context) { | 545 resource_context_(resource_context) { |
568 DCHECK(resource_context_); | 546 DCHECK(resource_context_); |
569 } | 547 } |
570 | 548 |
571 WorkerProcessHost::WorkerInstance::~WorkerInstance() { | 549 WorkerProcessHost::WorkerInstance::~WorkerInstance() { |
572 } | 550 } |
573 | 551 |
574 // Compares an instance based on the algorithm in the WebWorkers spec - an | 552 // Compares an instance based on the algorithm in the WebWorkers spec - an |
575 // instance matches if the origins of the URLs match, and: | 553 // instance matches if the origins of the URLs match, and: |
576 // a) the names are non-empty and equal | 554 // a) the names are non-empty and equal |
577 // -or- | 555 // -or- |
578 // b) the names are both empty, and the urls are equal | 556 // b) the names are both empty, and the urls are equal |
579 bool WorkerProcessHost::WorkerInstance::Matches( | 557 bool WorkerProcessHost::WorkerInstance::Matches( |
580 const GURL& match_url, | 558 const GURL& match_url, |
581 const string16& match_name, | 559 const string16& match_name, |
582 const content::ResourceContext* resource_context) const { | 560 const content::ResourceContext* resource_context) const { |
583 // Only match open shared workers. | 561 // Only match open shared workers. |
584 if (!shared_ || closed_) | 562 if (closed_) |
585 return false; | 563 return false; |
586 | 564 |
587 // Have to match the same ResourceContext. | 565 // Have to match the same ResourceContext. |
588 if (resource_context_ != resource_context) | 566 if (resource_context_ != resource_context) |
589 return false; | 567 return false; |
590 | 568 |
591 if (url_.GetOrigin() != match_url.GetOrigin()) | 569 if (url_.GetOrigin() != match_url.GetOrigin()) |
592 return false; | 570 return false; |
593 | 571 |
594 if (name_.empty() && match_name.empty()) | 572 if (name_.empty() && match_name.empty()) |
595 return url_ == match_url; | 573 return url_ == match_url; |
596 | 574 |
597 return name_ == match_name; | 575 return name_ == match_name; |
598 } | 576 } |
599 | 577 |
600 void WorkerProcessHost::WorkerInstance::AddFilter(WorkerMessageFilter* filter, | 578 void WorkerProcessHost::WorkerInstance::AddFilter(WorkerMessageFilter* filter, |
601 int route_id) { | 579 int route_id) { |
602 if (!HasFilter(filter, route_id)) { | 580 if (!HasFilter(filter, route_id)) { |
603 FilterInfo info(filter, route_id); | 581 FilterInfo info(filter, route_id); |
604 filters_.push_back(info); | 582 filters_.push_back(info); |
605 } | 583 } |
606 // Only shared workers can have more than one associated filter. | |
607 DCHECK(shared_ || filters_.size() == 1); | |
608 } | 584 } |
609 | 585 |
610 void WorkerProcessHost::WorkerInstance::RemoveFilter( | 586 void WorkerProcessHost::WorkerInstance::RemoveFilter( |
611 WorkerMessageFilter* filter, int route_id) { | 587 WorkerMessageFilter* filter, int route_id) { |
612 for (FilterList::iterator i = filters_.begin(); i != filters_.end();) { | 588 for (FilterList::iterator i = filters_.begin(); i != filters_.end();) { |
613 if (i->first == filter && i->second == route_id) | 589 if (i->first == filter && i->second == route_id) |
614 i = filters_.erase(i); | 590 i = filters_.erase(i); |
615 else | 591 else |
616 ++i; | 592 ++i; |
617 } | 593 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
652 } | 628 } |
653 } | 629 } |
654 return false; | 630 return false; |
655 } | 631 } |
656 | 632 |
657 WorkerProcessHost::WorkerInstance::FilterInfo | 633 WorkerProcessHost::WorkerInstance::FilterInfo |
658 WorkerProcessHost::WorkerInstance::GetFilter() const { | 634 WorkerProcessHost::WorkerInstance::GetFilter() const { |
659 DCHECK(NumFilters() == 1); | 635 DCHECK(NumFilters() == 1); |
660 return *filters_.begin(); | 636 return *filters_.begin(); |
661 } | 637 } |
OLD | NEW |