| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/service_worker/service_worker_version.h" | 5 #include "content/browser/service_worker/service_worker_version.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
| 10 #include "content/browser/service_worker/embedded_worker_instance.h" | 10 #include "content/browser/service_worker/embedded_worker_instance.h" |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 | 297 |
| 298 int request_id = sync_callbacks_.Add(new StatusCallback(callback)); | 298 int request_id = sync_callbacks_.Add(new StatusCallback(callback)); |
| 299 ServiceWorkerStatusCode status = embedded_worker_->SendMessage( | 299 ServiceWorkerStatusCode status = embedded_worker_->SendMessage( |
| 300 ServiceWorkerMsg_SyncEvent(request_id)); | 300 ServiceWorkerMsg_SyncEvent(request_id)); |
| 301 if (status != SERVICE_WORKER_OK) { | 301 if (status != SERVICE_WORKER_OK) { |
| 302 sync_callbacks_.Remove(request_id); | 302 sync_callbacks_.Remove(request_id); |
| 303 RunSoon(base::Bind(callback, status)); | 303 RunSoon(base::Bind(callback, status)); |
| 304 } | 304 } |
| 305 } | 305 } |
| 306 | 306 |
| 307 void ServiceWorkerVersion::DispatchPushEvent(const StatusCallback& callback, |
| 308 const std::string& data) { |
| 309 DCHECK_EQ(ACTIVE, status()) << status(); |
| 310 |
| 311 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
| 312 switches::kEnableExperimentalWebPlatformFeatures)) { |
| 313 callback.Run(SERVICE_WORKER_ERROR_ABORT); |
| 314 return; |
| 315 } |
| 316 |
| 317 if (running_status() != RUNNING) { |
| 318 // Schedule calling this method after starting the worker. |
| 319 StartWorker(base::Bind(&RunTaskAfterStartWorker, |
| 320 weak_factory_.GetWeakPtr(), callback, |
| 321 base::Bind(&self::DispatchPushEvent, |
| 322 weak_factory_.GetWeakPtr(), |
| 323 callback, data))); |
| 324 return; |
| 325 } |
| 326 |
| 327 int request_id = push_callbacks_.Add(new StatusCallback(callback)); |
| 328 ServiceWorkerStatusCode status = embedded_worker_->SendMessage( |
| 329 ServiceWorkerMsg_PushEvent(request_id, data)); |
| 330 if (status != SERVICE_WORKER_OK) { |
| 331 push_callbacks_.Remove(request_id); |
| 332 RunSoon(base::Bind(callback, status)); |
| 333 } |
| 334 } |
| 335 |
| 307 void ServiceWorkerVersion::AddProcessToWorker(int process_id) { | 336 void ServiceWorkerVersion::AddProcessToWorker(int process_id) { |
| 308 embedded_worker_->AddProcessReference(process_id); | 337 embedded_worker_->AddProcessReference(process_id); |
| 309 } | 338 } |
| 310 | 339 |
| 311 void ServiceWorkerVersion::RemoveProcessFromWorker(int process_id) { | 340 void ServiceWorkerVersion::RemoveProcessFromWorker(int process_id) { |
| 312 embedded_worker_->ReleaseProcessReference(process_id); | 341 embedded_worker_->ReleaseProcessReference(process_id); |
| 313 } | 342 } |
| 314 | 343 |
| 315 bool ServiceWorkerVersion::HasProcessToRun() const { | 344 bool ServiceWorkerVersion::HasProcessToRun() const { |
| 316 return embedded_worker_->HasProcessToRun(); | 345 return embedded_worker_->HasProcessToRun(); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 389 &StatusCallback::Run, | 418 &StatusCallback::Run, |
| 390 MakeTuple(SERVICE_WORKER_ERROR_INSTALL_WORKER_FAILED)); | 419 MakeTuple(SERVICE_WORKER_ERROR_INSTALL_WORKER_FAILED)); |
| 391 RunIDMapCallbacks(&fetch_callbacks_, | 420 RunIDMapCallbacks(&fetch_callbacks_, |
| 392 &FetchCallback::Run, | 421 &FetchCallback::Run, |
| 393 MakeTuple(SERVICE_WORKER_ERROR_FAILED, | 422 MakeTuple(SERVICE_WORKER_ERROR_FAILED, |
| 394 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, | 423 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, |
| 395 ServiceWorkerResponse())); | 424 ServiceWorkerResponse())); |
| 396 RunIDMapCallbacks(&sync_callbacks_, | 425 RunIDMapCallbacks(&sync_callbacks_, |
| 397 &StatusCallback::Run, | 426 &StatusCallback::Run, |
| 398 MakeTuple(SERVICE_WORKER_ERROR_FAILED)); | 427 MakeTuple(SERVICE_WORKER_ERROR_FAILED)); |
| 428 RunIDMapCallbacks(&push_callbacks_, |
| 429 &StatusCallback::Run, |
| 430 MakeTuple(SERVICE_WORKER_ERROR_FAILED)); |
| 399 | 431 |
| 400 FOR_EACH_OBSERVER(Listener, listeners_, OnWorkerStopped(this)); | 432 FOR_EACH_OBSERVER(Listener, listeners_, OnWorkerStopped(this)); |
| 401 } | 433 } |
| 402 | 434 |
| 403 void ServiceWorkerVersion::OnReportException( | 435 void ServiceWorkerVersion::OnReportException( |
| 404 const base::string16& error_message, | 436 const base::string16& error_message, |
| 405 int line_number, | 437 int line_number, |
| 406 int column_number, | 438 int column_number, |
| 407 const GURL& source_url) { | 439 const GURL& source_url) { |
| 408 FOR_EACH_OBSERVER( | 440 FOR_EACH_OBSERVER( |
| (...skipping 24 matching lines...) Expand all Loading... |
| 433 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetClientDocuments, | 465 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetClientDocuments, |
| 434 OnGetClientDocuments) | 466 OnGetClientDocuments) |
| 435 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ActivateEventFinished, | 467 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ActivateEventFinished, |
| 436 OnActivateEventFinished) | 468 OnActivateEventFinished) |
| 437 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_InstallEventFinished, | 469 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_InstallEventFinished, |
| 438 OnInstallEventFinished) | 470 OnInstallEventFinished) |
| 439 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FetchEventFinished, | 471 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FetchEventFinished, |
| 440 OnFetchEventFinished) | 472 OnFetchEventFinished) |
| 441 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SyncEventFinished, | 473 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SyncEventFinished, |
| 442 OnSyncEventFinished) | 474 OnSyncEventFinished) |
| 475 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PushEventFinished, |
| 476 OnPushEventFinished) |
| 443 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToDocument, | 477 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToDocument, |
| 444 OnPostMessageToDocument) | 478 OnPostMessageToDocument) |
| 445 IPC_MESSAGE_UNHANDLED(handled = false) | 479 IPC_MESSAGE_UNHANDLED(handled = false) |
| 446 IPC_END_MESSAGE_MAP() | 480 IPC_END_MESSAGE_MAP() |
| 447 return handled; | 481 return handled; |
| 448 } | 482 } |
| 449 | 483 |
| 450 void ServiceWorkerVersion::RunStartWorkerCallbacksOnError( | 484 void ServiceWorkerVersion::RunStartWorkerCallbacksOnError( |
| 451 ServiceWorkerStatusCode status) { | 485 ServiceWorkerStatusCode status) { |
| 452 if (status != SERVICE_WORKER_OK) | 486 if (status != SERVICE_WORKER_OK) |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 553 if (!callback) { | 587 if (!callback) { |
| 554 NOTREACHED() << "Got unexpected message: " << request_id; | 588 NOTREACHED() << "Got unexpected message: " << request_id; |
| 555 return; | 589 return; |
| 556 } | 590 } |
| 557 | 591 |
| 558 scoped_refptr<ServiceWorkerVersion> protect(this); | 592 scoped_refptr<ServiceWorkerVersion> protect(this); |
| 559 callback->Run(SERVICE_WORKER_OK); | 593 callback->Run(SERVICE_WORKER_OK); |
| 560 sync_callbacks_.Remove(request_id); | 594 sync_callbacks_.Remove(request_id); |
| 561 } | 595 } |
| 562 | 596 |
| 597 void ServiceWorkerVersion::OnPushEventFinished( |
| 598 int request_id) { |
| 599 StatusCallback* callback = push_callbacks_.Lookup(request_id); |
| 600 if (!callback) { |
| 601 NOTREACHED() << "Got unexpected message: " << request_id; |
| 602 return; |
| 603 } |
| 604 |
| 605 scoped_refptr<ServiceWorkerVersion> protect(this); |
| 606 callback->Run(SERVICE_WORKER_OK); |
| 607 push_callbacks_.Remove(request_id); |
| 608 } |
| 609 |
| 563 void ServiceWorkerVersion::OnPostMessageToDocument( | 610 void ServiceWorkerVersion::OnPostMessageToDocument( |
| 564 int client_id, | 611 int client_id, |
| 565 const base::string16& message, | 612 const base::string16& message, |
| 566 const std::vector<int>& sent_message_port_ids) { | 613 const std::vector<int>& sent_message_port_ids) { |
| 567 ServiceWorkerProviderHost* provider_host = | 614 ServiceWorkerProviderHost* provider_host = |
| 568 controllee_by_id_.Lookup(client_id); | 615 controllee_by_id_.Lookup(client_id); |
| 569 if (!provider_host) { | 616 if (!provider_host) { |
| 570 // The client may already have been closed, just ignore. | 617 // The client may already have been closed, just ignore. |
| 571 return; | 618 return; |
| 572 } | 619 } |
| 573 provider_host->PostMessage(message, sent_message_port_ids); | 620 provider_host->PostMessage(message, sent_message_port_ids); |
| 574 } | 621 } |
| 575 | 622 |
| 576 void ServiceWorkerVersion::ScheduleStopWorker() { | 623 void ServiceWorkerVersion::ScheduleStopWorker() { |
| 577 if (running_status() != RUNNING) | 624 if (running_status() != RUNNING) |
| 578 return; | 625 return; |
| 579 if (stop_worker_timer_.IsRunning()) { | 626 if (stop_worker_timer_.IsRunning()) { |
| 580 stop_worker_timer_.Reset(); | 627 stop_worker_timer_.Reset(); |
| 581 return; | 628 return; |
| 582 } | 629 } |
| 583 stop_worker_timer_.Start( | 630 stop_worker_timer_.Start( |
| 584 FROM_HERE, base::TimeDelta::FromSeconds(kStopWorkerDelay), | 631 FROM_HERE, base::TimeDelta::FromSeconds(kStopWorkerDelay), |
| 585 base::Bind(&ServiceWorkerVersion::StopWorker, | 632 base::Bind(&ServiceWorkerVersion::StopWorker, |
| 586 weak_factory_.GetWeakPtr(), | 633 weak_factory_.GetWeakPtr(), |
| 587 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback))); | 634 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback))); |
| 588 } | 635 } |
| 589 | 636 |
| 590 } // namespace content | 637 } // namespace content |
| OLD | NEW |