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/message_port_message_filter.h" | 10 #include "content/browser/message_port_message_filter.h" |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 const GURL& script_url, | 107 const GURL& script_url, |
108 int64 version_id, | 108 int64 version_id, |
109 base::WeakPtr<ServiceWorkerContextCore> context) | 109 base::WeakPtr<ServiceWorkerContextCore> context) |
110 : version_id_(version_id), | 110 : version_id_(version_id), |
111 registration_id_(kInvalidServiceWorkerVersionId), | 111 registration_id_(kInvalidServiceWorkerVersionId), |
112 script_url_(script_url), | 112 script_url_(script_url), |
113 status_(NEW), | 113 status_(NEW), |
114 context_(context), | 114 context_(context), |
115 script_cache_map_(this, context), | 115 script_cache_map_(this, context), |
116 is_doomed_(false), | 116 is_doomed_(false), |
| 117 skip_waiting_(false), |
117 weak_factory_(this) { | 118 weak_factory_(this) { |
118 DCHECK(context_); | 119 DCHECK(context_); |
119 DCHECK(registration); | 120 DCHECK(registration); |
120 if (registration) { | 121 if (registration) { |
121 registration_id_ = registration->id(); | 122 registration_id_ = registration->id(); |
122 scope_ = registration->pattern(); | 123 scope_ = registration->pattern(); |
123 } | 124 } |
124 context_->AddLiveVersion(this); | 125 context_->AddLiveVersion(this); |
125 embedded_worker_ = context_->embedded_worker_registry()->CreateWorker(); | 126 embedded_worker_ = context_->embedded_worker_registry()->CreateWorker(); |
126 embedded_worker_->AddListener(this); | 127 embedded_worker_->AddListener(this); |
127 } | 128 } |
128 | 129 |
129 ServiceWorkerVersion::~ServiceWorkerVersion() { | 130 ServiceWorkerVersion::~ServiceWorkerVersion() { |
130 embedded_worker_->RemoveListener(this); | 131 embedded_worker_->RemoveListener(this); |
131 if (context_) | 132 if (context_) |
132 context_->RemoveLiveVersion(version_id_); | 133 context_->RemoveLiveVersion(version_id_); |
133 // EmbeddedWorker's dtor sends StopWorker if it's still running. | 134 // EmbeddedWorker's dtor sends StopWorker if it's still running. |
134 } | 135 } |
135 | 136 |
136 void ServiceWorkerVersion::SetStatus(Status status) { | 137 void ServiceWorkerVersion::SetStatus(Status status) { |
137 if (status_ == status) | 138 if (status_ == status) |
138 return; | 139 return; |
139 | 140 |
140 // Schedule to stop worker after registration successfully completed. | 141 // Schedule to stop worker after registration successfully completed. |
141 if (status_ == ACTIVATING && status == ACTIVATED && !HasControllee()) | 142 if (status_ == ACTIVATING && status == ACTIVATED && !HasControllee()) |
142 ScheduleStopWorker(); | 143 ScheduleStopWorker(); |
143 | 144 |
144 status_ = status; | 145 status_ = status; |
145 | 146 |
| 147 if (skip_waiting_ && status_ == ACTIVATED) { |
| 148 for (int request_id : pending_skip_waiting_requests_) |
| 149 DidSkipWaiting(request_id); |
| 150 pending_skip_waiting_requests_.clear(); |
| 151 } |
| 152 |
146 std::vector<base::Closure> callbacks; | 153 std::vector<base::Closure> callbacks; |
147 callbacks.swap(status_change_callbacks_); | 154 callbacks.swap(status_change_callbacks_); |
148 for (std::vector<base::Closure>::const_iterator i = callbacks.begin(); | 155 for (std::vector<base::Closure>::const_iterator i = callbacks.begin(); |
149 i != callbacks.end(); ++i) { | 156 i != callbacks.end(); ++i) { |
150 (*i).Run(); | 157 (*i).Run(); |
151 } | 158 } |
152 | 159 |
153 FOR_EACH_OBSERVER(Listener, listeners_, OnVersionStateChanged(this)); | 160 FOR_EACH_OBSERVER(Listener, listeners_, OnVersionStateChanged(this)); |
154 } | 161 } |
155 | 162 |
(...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
594 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FetchEventFinished, | 601 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FetchEventFinished, |
595 OnFetchEventFinished) | 602 OnFetchEventFinished) |
596 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SyncEventFinished, | 603 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SyncEventFinished, |
597 OnSyncEventFinished) | 604 OnSyncEventFinished) |
598 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PushEventFinished, | 605 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PushEventFinished, |
599 OnPushEventFinished) | 606 OnPushEventFinished) |
600 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GeofencingEventFinished, | 607 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GeofencingEventFinished, |
601 OnGeofencingEventFinished) | 608 OnGeofencingEventFinished) |
602 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToDocument, | 609 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToDocument, |
603 OnPostMessageToDocument) | 610 OnPostMessageToDocument) |
| 611 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SkipWaiting, OnSkipWaiting) |
604 IPC_MESSAGE_UNHANDLED(handled = false) | 612 IPC_MESSAGE_UNHANDLED(handled = false) |
605 IPC_END_MESSAGE_MAP() | 613 IPC_END_MESSAGE_MAP() |
606 return handled; | 614 return handled; |
607 } | 615 } |
608 | 616 |
609 void ServiceWorkerVersion::OnStartMessageSent( | 617 void ServiceWorkerVersion::OnStartMessageSent( |
610 ServiceWorkerStatusCode status) { | 618 ServiceWorkerStatusCode status) { |
611 if (status != SERVICE_WORKER_OK) | 619 if (status != SERVICE_WORKER_OK) |
612 RunCallbacks(this, &start_callbacks_, status); | 620 RunCallbacks(this, &start_callbacks_, status); |
613 } | 621 } |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
781 "Client id", client_id); | 789 "Client id", client_id); |
782 ServiceWorkerProviderHost* provider_host = | 790 ServiceWorkerProviderHost* provider_host = |
783 controllee_by_id_.Lookup(client_id); | 791 controllee_by_id_.Lookup(client_id); |
784 if (!provider_host) { | 792 if (!provider_host) { |
785 // The client may already have been closed, just ignore. | 793 // The client may already have been closed, just ignore. |
786 return; | 794 return; |
787 } | 795 } |
788 provider_host->PostMessage(message, sent_message_port_ids); | 796 provider_host->PostMessage(message, sent_message_port_ids); |
789 } | 797 } |
790 | 798 |
| 799 void ServiceWorkerVersion::OnSkipWaiting(int request_id) { |
| 800 skip_waiting_ = true; |
| 801 if (status_ != INSTALLED) |
| 802 return DidSkipWaiting(request_id); |
| 803 |
| 804 if (!context_) |
| 805 return; |
| 806 ServiceWorkerRegistration* registration = |
| 807 context_->GetLiveRegistration(registration_id_); |
| 808 if (!registration) |
| 809 return; |
| 810 pending_skip_waiting_requests_.push_back(request_id); |
| 811 if (pending_skip_waiting_requests_.size() == 1) |
| 812 registration->ActivateWaitingVersionWhenReady(); |
| 813 } |
| 814 |
| 815 void ServiceWorkerVersion::DidSkipWaiting(int request_id) { |
| 816 if (running_status() == STARTING || running_status() == RUNNING) |
| 817 embedded_worker_->SendMessage(ServiceWorkerMsg_DidSkipWaiting(request_id)); |
| 818 } |
| 819 |
791 void ServiceWorkerVersion::ScheduleStopWorker() { | 820 void ServiceWorkerVersion::ScheduleStopWorker() { |
792 if (running_status() != RUNNING) | 821 if (running_status() != RUNNING) |
793 return; | 822 return; |
794 if (stop_worker_timer_.IsRunning()) { | 823 if (stop_worker_timer_.IsRunning()) { |
795 stop_worker_timer_.Reset(); | 824 stop_worker_timer_.Reset(); |
796 return; | 825 return; |
797 } | 826 } |
798 stop_worker_timer_.Start( | 827 stop_worker_timer_.Start( |
799 FROM_HERE, base::TimeDelta::FromSeconds(kStopWorkerDelay), | 828 FROM_HERE, base::TimeDelta::FromSeconds(kStopWorkerDelay), |
800 base::Bind(&ServiceWorkerVersion::StopWorker, | 829 base::Bind(&ServiceWorkerVersion::StopWorker, |
801 weak_factory_.GetWeakPtr(), | 830 weak_factory_.GetWeakPtr(), |
802 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback))); | 831 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback))); |
803 } | 832 } |
804 | 833 |
805 void ServiceWorkerVersion::DoomInternal() { | 834 void ServiceWorkerVersion::DoomInternal() { |
806 DCHECK(!HasControllee()); | 835 DCHECK(!HasControllee()); |
807 SetStatus(REDUNDANT); | 836 SetStatus(REDUNDANT); |
808 StopWorker(base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 837 StopWorker(base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
809 if (!context_) | 838 if (!context_) |
810 return; | 839 return; |
811 std::vector<ServiceWorkerDatabase::ResourceRecord> resources; | 840 std::vector<ServiceWorkerDatabase::ResourceRecord> resources; |
812 script_cache_map_.GetResources(&resources); | 841 script_cache_map_.GetResources(&resources); |
813 context_->storage()->PurgeResources(resources); | 842 context_->storage()->PurgeResources(resources); |
814 } | 843 } |
815 | 844 |
816 } // namespace content | 845 } // namespace content |
OLD | NEW |