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 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
94 const GURL& script_url, | 94 const GURL& script_url, |
95 int64 version_id, | 95 int64 version_id, |
96 base::WeakPtr<ServiceWorkerContextCore> context) | 96 base::WeakPtr<ServiceWorkerContextCore> context) |
97 : version_id_(version_id), | 97 : version_id_(version_id), |
98 registration_id_(kInvalidServiceWorkerVersionId), | 98 registration_id_(kInvalidServiceWorkerVersionId), |
99 script_url_(script_url), | 99 script_url_(script_url), |
100 status_(NEW), | 100 status_(NEW), |
101 context_(context), | 101 context_(context), |
102 script_cache_map_(this, context), | 102 script_cache_map_(this, context), |
103 is_doomed_(false), | 103 is_doomed_(false), |
104 skip_waiting_(false), | |
104 weak_factory_(this) { | 105 weak_factory_(this) { |
105 DCHECK(context_); | 106 DCHECK(context_); |
106 DCHECK(registration); | 107 DCHECK(registration); |
107 if (registration) { | 108 if (registration) { |
108 registration_id_ = registration->id(); | 109 registration_id_ = registration->id(); |
109 scope_ = registration->pattern(); | 110 scope_ = registration->pattern(); |
110 } | 111 } |
111 context_->AddLiveVersion(this); | 112 context_->AddLiveVersion(this); |
112 embedded_worker_ = context_->embedded_worker_registry()->CreateWorker(); | 113 embedded_worker_ = context_->embedded_worker_registry()->CreateWorker(); |
113 embedded_worker_->AddListener(this); | 114 embedded_worker_->AddListener(this); |
114 } | 115 } |
115 | 116 |
116 ServiceWorkerVersion::~ServiceWorkerVersion() { | 117 ServiceWorkerVersion::~ServiceWorkerVersion() { |
117 embedded_worker_->RemoveListener(this); | 118 embedded_worker_->RemoveListener(this); |
118 if (context_) | 119 if (context_) |
119 context_->RemoveLiveVersion(version_id_); | 120 context_->RemoveLiveVersion(version_id_); |
120 // EmbeddedWorker's dtor sends StopWorker if it's still running. | 121 // EmbeddedWorker's dtor sends StopWorker if it's still running. |
121 } | 122 } |
122 | 123 |
123 void ServiceWorkerVersion::SetStatus(Status status) { | 124 void ServiceWorkerVersion::SetStatus(Status status) { |
124 if (status_ == status) | 125 if (status_ == status) |
125 return; | 126 return; |
126 | 127 |
127 // Schedule to stop worker after registration successfully completed. | 128 // Schedule to stop worker after registration successfully completed. |
128 if (status_ == ACTIVATING && status == ACTIVATED && !HasControllee()) | 129 if (status_ == ACTIVATING && status == ACTIVATED && !HasControllee()) |
129 ScheduleStopWorker(); | 130 ScheduleStopWorker(); |
130 | 131 |
131 status_ = status; | 132 status_ = status; |
132 | 133 |
134 if (skip_waiting_ && status_ == ACTIVATED) { | |
135 for (int request_id : pending_skip_waiting_requests_) | |
136 DidSkipWaiting(request_id); | |
137 pending_skip_waiting_requests_.clear(); | |
138 } | |
139 | |
133 std::vector<base::Closure> callbacks; | 140 std::vector<base::Closure> callbacks; |
134 callbacks.swap(status_change_callbacks_); | 141 callbacks.swap(status_change_callbacks_); |
135 for (std::vector<base::Closure>::const_iterator i = callbacks.begin(); | 142 for (std::vector<base::Closure>::const_iterator i = callbacks.begin(); |
136 i != callbacks.end(); ++i) { | 143 i != callbacks.end(); ++i) { |
137 (*i).Run(); | 144 (*i).Run(); |
138 } | 145 } |
139 | 146 |
140 FOR_EACH_OBSERVER(Listener, listeners_, OnVersionStateChanged(this)); | 147 FOR_EACH_OBSERVER(Listener, listeners_, OnVersionStateChanged(this)); |
141 } | 148 } |
142 | 149 |
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
544 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FetchEventFinished, | 551 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FetchEventFinished, |
545 OnFetchEventFinished) | 552 OnFetchEventFinished) |
546 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SyncEventFinished, | 553 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SyncEventFinished, |
547 OnSyncEventFinished) | 554 OnSyncEventFinished) |
548 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PushEventFinished, | 555 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PushEventFinished, |
549 OnPushEventFinished) | 556 OnPushEventFinished) |
550 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GeofencingEventFinished, | 557 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GeofencingEventFinished, |
551 OnGeofencingEventFinished) | 558 OnGeofencingEventFinished) |
552 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToDocument, | 559 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToDocument, |
553 OnPostMessageToDocument) | 560 OnPostMessageToDocument) |
561 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SkipWaiting, OnSkipWaiting) | |
554 IPC_MESSAGE_UNHANDLED(handled = false) | 562 IPC_MESSAGE_UNHANDLED(handled = false) |
555 IPC_END_MESSAGE_MAP() | 563 IPC_END_MESSAGE_MAP() |
556 return handled; | 564 return handled; |
557 } | 565 } |
558 | 566 |
559 void ServiceWorkerVersion::OnStartMessageSent( | 567 void ServiceWorkerVersion::OnStartMessageSent( |
560 ServiceWorkerStatusCode status) { | 568 ServiceWorkerStatusCode status) { |
561 if (status != SERVICE_WORKER_OK) | 569 if (status != SERVICE_WORKER_OK) |
562 RunCallbacks(this, &start_callbacks_, status); | 570 RunCallbacks(this, &start_callbacks_, status); |
563 } | 571 } |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
727 "Client id", client_id); | 735 "Client id", client_id); |
728 ServiceWorkerProviderHost* provider_host = | 736 ServiceWorkerProviderHost* provider_host = |
729 controllee_by_id_.Lookup(client_id); | 737 controllee_by_id_.Lookup(client_id); |
730 if (!provider_host) { | 738 if (!provider_host) { |
731 // The client may already have been closed, just ignore. | 739 // The client may already have been closed, just ignore. |
732 return; | 740 return; |
733 } | 741 } |
734 provider_host->PostMessage(message, sent_message_port_ids); | 742 provider_host->PostMessage(message, sent_message_port_ids); |
735 } | 743 } |
736 | 744 |
745 void ServiceWorkerVersion::OnSkipWaiting(int request_id) { | |
746 skip_waiting_ = true; | |
747 if (status_ != INSTALLED) | |
748 return DidSkipWaiting(request_id); | |
749 | |
750 if (!context_) | |
751 return; | |
752 ServiceWorkerRegistration* registration = | |
753 context_->GetLiveRegistration(registration_id_); | |
754 if (!registration) | |
755 return; | |
756 if (pending_skip_waiting_requests_.empty()) | |
757 registration->ActivateWaitingVersionWhenReady(); | |
falken
2014/11/19 04:19:56
this seems to depend on the activation to happen a
xiang
2014/11/24 07:04:01
Done.
| |
758 pending_skip_waiting_requests_.push_back(request_id); | |
759 } | |
760 | |
761 void ServiceWorkerVersion::DidSkipWaiting(int request_id) { | |
762 if (running_status() == STARTING || running_status() == RUNNING) | |
763 embedded_worker_->SendMessage(ServiceWorkerMsg_DidSkipWaiting(request_id)); | |
764 } | |
765 | |
737 void ServiceWorkerVersion::ScheduleStopWorker() { | 766 void ServiceWorkerVersion::ScheduleStopWorker() { |
738 if (running_status() != RUNNING) | 767 if (running_status() != RUNNING) |
739 return; | 768 return; |
740 if (stop_worker_timer_.IsRunning()) { | 769 if (stop_worker_timer_.IsRunning()) { |
741 stop_worker_timer_.Reset(); | 770 stop_worker_timer_.Reset(); |
742 return; | 771 return; |
743 } | 772 } |
744 stop_worker_timer_.Start( | 773 stop_worker_timer_.Start( |
745 FROM_HERE, base::TimeDelta::FromSeconds(kStopWorkerDelay), | 774 FROM_HERE, base::TimeDelta::FromSeconds(kStopWorkerDelay), |
746 base::Bind(&ServiceWorkerVersion::StopWorker, | 775 base::Bind(&ServiceWorkerVersion::StopWorker, |
747 weak_factory_.GetWeakPtr(), | 776 weak_factory_.GetWeakPtr(), |
748 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback))); | 777 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback))); |
749 } | 778 } |
750 | 779 |
751 void ServiceWorkerVersion::DoomInternal() { | 780 void ServiceWorkerVersion::DoomInternal() { |
752 DCHECK(!HasControllee()); | 781 DCHECK(!HasControllee()); |
753 SetStatus(REDUNDANT); | 782 SetStatus(REDUNDANT); |
754 StopWorker(base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 783 StopWorker(base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
755 if (!context_) | 784 if (!context_) |
756 return; | 785 return; |
757 std::vector<ServiceWorkerDatabase::ResourceRecord> resources; | 786 std::vector<ServiceWorkerDatabase::ResourceRecord> resources; |
758 script_cache_map_.GetResources(&resources); | 787 script_cache_map_.GetResources(&resources); |
759 context_->storage()->PurgeResources(resources); | 788 context_->storage()->PurgeResources(resources); |
760 } | 789 } |
761 | 790 |
762 } // namespace content | 791 } // namespace content |
OLD | NEW |