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