Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: content/browser/service_worker/service_worker_version.cc

Issue 771103002: Implement ServiceWorkerClient attributes [2/3] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: comments addressed Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/message_port_message_filter.h" 10 #include "content/browser/message_port_message_filter.h"
11 #include "content/browser/message_port_service.h" 11 #include "content/browser/message_port_service.h"
12 #include "content/browser/service_worker/embedded_worker_instance.h" 12 #include "content/browser/service_worker/embedded_worker_instance.h"
13 #include "content/browser/service_worker/embedded_worker_registry.h" 13 #include "content/browser/service_worker/embedded_worker_registry.h"
14 #include "content/browser/service_worker/service_worker_context_core.h" 14 #include "content/browser/service_worker/service_worker_context_core.h"
15 #include "content/browser/service_worker/service_worker_registration.h" 15 #include "content/browser/service_worker/service_worker_registration.h"
16 #include "content/browser/service_worker/service_worker_utils.h" 16 #include "content/browser/service_worker/service_worker_utils.h"
17 #include "content/common/service_worker/service_worker_messages.h" 17 #include "content/common/service_worker/service_worker_messages.h"
18 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/common/content_switches.h" 19 #include "content/public/common/content_switches.h"
20 20
21 namespace content { 21 namespace content {
22 22
23 typedef ServiceWorkerVersion::StatusCallback StatusCallback; 23 typedef ServiceWorkerVersion::StatusCallback StatusCallback;
24 typedef ServiceWorkerVersion::MessageCallback MessageCallback; 24 typedef ServiceWorkerVersion::MessageCallback MessageCallback;
25 25
26 class ServiceWorkerVersion::GetClientInfoCallback {
27 public:
28 GetClientInfoCallback(int request_id)
29 : request_id_(request_id) {}
30 void Add(int client_request_id, int client_id) {
31 pending_requests_[client_request_id] = client_id;
32 }
33 void Success(int client_request_id, const ServiceWorkerClientInfo& info) {
34 auto found = pending_requests_.find(client_request_id);
35 DCHECK(found != pending_requests_.end());
36 clients_.push_back(info);
37 clients_.back().client_id = found->second;
38 pending_requests_.erase(found);
39 }
40 void Error(int client_request_id) { Cancel(client_request_id); }
41 void Cancel(int client_request_id) {
42 auto found = pending_requests_.find(client_request_id);
43 DCHECK(found != pending_requests_.end());
44 pending_requests_.erase(found);
45 }
46 int request_id() { return request_id_; }
47 bool HasPendingRequests() { return !pending_requests_.empty(); }
48 const std::vector<ServiceWorkerClientInfo>& clients() { return clients_; }
49
50 static void ClearCallbacks(IDMap<GetClientInfoCallback>* callbacks);
51
52 private:
53 std::vector<ServiceWorkerClientInfo> clients_;
54 int request_id_;
55 std::map<int, int> pending_requests_;
56 };
57
26 namespace { 58 namespace {
27 59
28 // Default delay for scheduled stop. 60 // Default delay for scheduled stop.
29 // (Note that if all references to the version is dropped the worker 61 // (Note that if all references to the version is dropped the worker
30 // is also stopped without delay) 62 // is also stopped without delay)
31 const int64 kStopWorkerDelay = 30; // 30 secs. 63 const int64 kStopWorkerDelay = 30; // 30 secs.
32 64
33 // Default delay for scheduled update. 65 // Default delay for scheduled update.
34 const int kUpdateDelaySeconds = 1; 66 const int kUpdateDelaySeconds = 1;
35 67
(...skipping 534 matching lines...) Expand 10 before | Expand all | Expand 10 after
570 SERVICE_WORKER_ERROR_FAILED, 602 SERVICE_WORKER_ERROR_FAILED,
571 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, 603 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK,
572 ServiceWorkerResponse()); 604 ServiceWorkerResponse());
573 RunIDMapCallbacks(&sync_callbacks_, 605 RunIDMapCallbacks(&sync_callbacks_,
574 SERVICE_WORKER_ERROR_FAILED); 606 SERVICE_WORKER_ERROR_FAILED);
575 RunIDMapCallbacks(&push_callbacks_, 607 RunIDMapCallbacks(&push_callbacks_,
576 SERVICE_WORKER_ERROR_FAILED); 608 SERVICE_WORKER_ERROR_FAILED);
577 RunIDMapCallbacks(&geofencing_callbacks_, 609 RunIDMapCallbacks(&geofencing_callbacks_,
578 SERVICE_WORKER_ERROR_FAILED); 610 SERVICE_WORKER_ERROR_FAILED);
579 611
612 GetClientInfoCallback::ClearCallbacks(&get_client_info_callbacks_);
613
580 FOR_EACH_OBSERVER(Listener, listeners_, OnWorkerStopped(this)); 614 FOR_EACH_OBSERVER(Listener, listeners_, OnWorkerStopped(this));
581 615
582 // There should be no more communication from/to a stopped worker. Deleting 616 // There should be no more communication from/to a stopped worker. Deleting
583 // the listener prevents any pending completion callbacks from causing 617 // the listener prevents any pending completion callbacks from causing
584 // messages to be sent to the stopped worker. 618 // messages to be sent to the stopped worker.
585 cache_listener_.reset(); 619 cache_listener_.reset();
586 620
587 // Restart worker if we have any start callbacks and the worker isn't doomed. 621 // Restart worker if we have any start callbacks and the worker isn't doomed.
588 if (should_restart) { 622 if (should_restart) {
589 cache_listener_.reset(new ServiceWorkerCacheListener(this, context_)); 623 cache_listener_.reset(new ServiceWorkerCacheListener(this, context_));
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
637 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_NotificationClickEventFinished, 671 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_NotificationClickEventFinished,
638 OnNotificationClickEventFinished) 672 OnNotificationClickEventFinished)
639 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PushEventFinished, 673 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PushEventFinished,
640 OnPushEventFinished) 674 OnPushEventFinished)
641 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GeofencingEventFinished, 675 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GeofencingEventFinished,
642 OnGeofencingEventFinished) 676 OnGeofencingEventFinished)
643 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToDocument, 677 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToDocument,
644 OnPostMessageToDocument) 678 OnPostMessageToDocument)
645 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FocusClient, 679 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FocusClient,
646 OnFocusClient) 680 OnFocusClient)
681 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetClientInfoSuccess,
682 OnGetClientInfoSuccess)
683 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetClientInfoError,
684 OnGetClientInfoError)
647 IPC_MESSAGE_UNHANDLED(handled = false) 685 IPC_MESSAGE_UNHANDLED(handled = false)
648 IPC_END_MESSAGE_MAP() 686 IPC_END_MESSAGE_MAP()
649 return handled; 687 return handled;
650 } 688 }
651 689
652 void ServiceWorkerVersion::OnStartMessageSent( 690 void ServiceWorkerVersion::OnStartMessageSent(
653 ServiceWorkerStatusCode status) { 691 ServiceWorkerStatusCode status) {
654 if (status != SERVICE_WORKER_OK) 692 if (status != SERVICE_WORKER_OK)
655 RunCallbacks(this, &start_callbacks_, status); 693 RunCallbacks(this, &start_callbacks_, status);
656 } 694 }
(...skipping 21 matching lines...) Expand all
678 int request_id = activate_callbacks_.Add(new StatusCallback(callback)); 716 int request_id = activate_callbacks_.Add(new StatusCallback(callback));
679 ServiceWorkerStatusCode status = 717 ServiceWorkerStatusCode status =
680 embedded_worker_->SendMessage(ServiceWorkerMsg_ActivateEvent(request_id)); 718 embedded_worker_->SendMessage(ServiceWorkerMsg_ActivateEvent(request_id));
681 if (status != SERVICE_WORKER_OK) { 719 if (status != SERVICE_WORKER_OK) {
682 activate_callbacks_.Remove(request_id); 720 activate_callbacks_.Remove(request_id);
683 RunSoon(base::Bind(callback, status)); 721 RunSoon(base::Bind(callback, status));
684 } 722 }
685 } 723 }
686 724
687 void ServiceWorkerVersion::OnGetClientDocuments(int request_id) { 725 void ServiceWorkerVersion::OnGetClientDocuments(int request_id) {
688 std::vector<int> client_ids; 726 if (controllee_by_id_.IsEmpty()) {
727 if (running_status() == RUNNING) {
728 embedded_worker_->SendMessage(
729 ServiceWorkerMsg_DidGetClientDocuments(request_id,
730 std::vector<ServiceWorkerClientInfo>()));
731 }
732 return;
733 }
734 GetClientInfoCallback* callback = new GetClientInfoCallback(request_id);
689 ControlleeByIDMap::iterator it(&controllee_by_id_); 735 ControlleeByIDMap::iterator it(&controllee_by_id_);
690 TRACE_EVENT0("ServiceWorker", 736 TRACE_EVENT0("ServiceWorker",
691 "ServiceWorkerVersion::OnGetClientDocuments"); 737 "ServiceWorkerVersion::OnGetClientDocuments");
692 while (!it.IsAtEnd()) { 738 while (!it.IsAtEnd()) {
693 client_ids.push_back(it.GetCurrentKey()); 739 int client_request_id = get_client_info_callbacks_.Add(callback);
740 callback->Add(client_request_id, it.GetCurrentKey());
741 it.GetCurrentValue()->GetClientInfo(embedded_worker_->embedded_worker_id(),
742 client_request_id);
694 it.Advance(); 743 it.Advance();
695 } 744 }
745 }
746
747 void ServiceWorkerVersion::OnGetClientInfoSuccess(
748 int request_id,
749 const ServiceWorkerClientInfo& info) {
750 GetClientInfoCallback* callback =
751 get_client_info_callbacks_.Lookup(request_id);
752 if (!callback)
753 return;
nhiroki 2014/12/08 07:09:33 How about having a comment about why we can ignore
Kunihiko Sakamoto 2014/12/08 07:46:58 Done.
754 get_client_info_callbacks_.Remove(request_id);
755
756 callback->Success(request_id, info);
757 if (callback->HasPendingRequests())
758 return;
759
696 // Don't bother if it's no longer running. 760 // Don't bother if it's no longer running.
697 if (running_status() == RUNNING) { 761 if (running_status() == RUNNING) {
698 embedded_worker_->SendMessage( 762 embedded_worker_->SendMessage(
699 ServiceWorkerMsg_DidGetClientDocuments(request_id, client_ids)); 763 ServiceWorkerMsg_DidGetClientDocuments(callback->request_id(),
764 callback->clients()));
700 } 765 }
766 delete callback;
767 }
768
769 void ServiceWorkerVersion::OnGetClientInfoError(int request_id) {
770 GetClientInfoCallback* callback =
771 get_client_info_callbacks_.Lookup(request_id);
772 if (!callback)
nhiroki 2014/12/08 07:09:33 ditto.
Kunihiko Sakamoto 2014/12/08 07:46:58 Done.
773 return;
774 get_client_info_callbacks_.Remove(request_id);
775
776 callback->Error(request_id);
777 if (callback->HasPendingRequests())
778 return;
779
780 // Don't bother if it's no longer running.
781 if (running_status() == RUNNING) {
782 embedded_worker_->SendMessage(
783 ServiceWorkerMsg_DidGetClientDocuments(callback->request_id(),
784 callback->clients()));
785 }
786 delete callback;
701 } 787 }
702 788
703 void ServiceWorkerVersion::OnActivateEventFinished( 789 void ServiceWorkerVersion::OnActivateEventFinished(
704 int request_id, 790 int request_id,
705 blink::WebServiceWorkerEventResult result) { 791 blink::WebServiceWorkerEventResult result) {
706 DCHECK(ACTIVATING == status() || 792 DCHECK(ACTIVATING == status() ||
707 REDUNDANT == status()) << status(); 793 REDUNDANT == status()) << status();
708 TRACE_EVENT0("ServiceWorker", 794 TRACE_EVENT0("ServiceWorker",
709 "ServiceWorkerVersion::OnActivateEventFinished"); 795 "ServiceWorkerVersion::OnActivateEventFinished");
710 796
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
893 DCHECK(!HasControllee()); 979 DCHECK(!HasControllee());
894 SetStatus(REDUNDANT); 980 SetStatus(REDUNDANT);
895 StopWorker(base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); 981 StopWorker(base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
896 if (!context_) 982 if (!context_)
897 return; 983 return;
898 std::vector<ServiceWorkerDatabase::ResourceRecord> resources; 984 std::vector<ServiceWorkerDatabase::ResourceRecord> resources;
899 script_cache_map_.GetResources(&resources); 985 script_cache_map_.GetResources(&resources);
900 context_->storage()->PurgeResources(resources); 986 context_->storage()->PurgeResources(resources);
901 } 987 }
902 988
989 void ServiceWorkerVersion::GetClientInfoCallback::ClearCallbacks(
990 IDMap<GetClientInfoCallback>* callbacks) {
991 IDMap<GetClientInfoCallback>::iterator iter(callbacks);
992 while (!iter.IsAtEnd()) {
993 GetClientInfoCallback* callback = iter.GetCurrentValue();
994 callback->Cancel(iter.GetCurrentKey());
995 if (!callback->HasPendingRequests())
996 delete callback;
997 iter.Advance();
998 }
999 callbacks->Clear();
1000 }
1001
903 } // namespace content 1002 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/service_worker/service_worker_version.h ('k') | content/child/service_worker/service_worker_dispatcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698