| 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 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 777 "Client id", client_id); | 785 "Client id", client_id); |
| 778 ServiceWorkerProviderHost* provider_host = | 786 ServiceWorkerProviderHost* provider_host = |
| 779 controllee_by_id_.Lookup(client_id); | 787 controllee_by_id_.Lookup(client_id); |
| 780 if (!provider_host) { | 788 if (!provider_host) { |
| 781 // The client may already have been closed, just ignore. | 789 // The client may already have been closed, just ignore. |
| 782 return; | 790 return; |
| 783 } | 791 } |
| 784 provider_host->PostMessage(message, sent_message_port_ids); | 792 provider_host->PostMessage(message, sent_message_port_ids); |
| 785 } | 793 } |
| 786 | 794 |
| 795 void ServiceWorkerVersion::OnSkipWaiting(int request_id) { |
| 796 skip_waiting_ = true; |
| 797 if (status_ != INSTALLED) |
| 798 return DidSkipWaiting(request_id); |
| 799 |
| 800 if (!context_) |
| 801 return; |
| 802 ServiceWorkerRegistration* registration = |
| 803 context_->GetLiveRegistration(registration_id_); |
| 804 if (!registration) |
| 805 return; |
| 806 pending_skip_waiting_requests_.push_back(request_id); |
| 807 if (pending_skip_waiting_requests_.size() == 1) |
| 808 registration->ActivateWaitingVersionWhenReady(); |
| 809 } |
| 810 |
| 811 void ServiceWorkerVersion::DidSkipWaiting(int request_id) { |
| 812 if (running_status() == STARTING || running_status() == RUNNING) |
| 813 embedded_worker_->SendMessage(ServiceWorkerMsg_DidSkipWaiting(request_id)); |
| 814 } |
| 815 |
| 787 void ServiceWorkerVersion::ScheduleStopWorker() { | 816 void ServiceWorkerVersion::ScheduleStopWorker() { |
| 788 if (running_status() != RUNNING) | 817 if (running_status() != RUNNING) |
| 789 return; | 818 return; |
| 790 if (stop_worker_timer_.IsRunning()) { | 819 if (stop_worker_timer_.IsRunning()) { |
| 791 stop_worker_timer_.Reset(); | 820 stop_worker_timer_.Reset(); |
| 792 return; | 821 return; |
| 793 } | 822 } |
| 794 stop_worker_timer_.Start( | 823 stop_worker_timer_.Start( |
| 795 FROM_HERE, base::TimeDelta::FromSeconds(kStopWorkerDelay), | 824 FROM_HERE, base::TimeDelta::FromSeconds(kStopWorkerDelay), |
| 796 base::Bind(&ServiceWorkerVersion::StopWorker, | 825 base::Bind(&ServiceWorkerVersion::StopWorker, |
| 797 weak_factory_.GetWeakPtr(), | 826 weak_factory_.GetWeakPtr(), |
| 798 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback))); | 827 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback))); |
| 799 } | 828 } |
| 800 | 829 |
| 801 void ServiceWorkerVersion::DoomInternal() { | 830 void ServiceWorkerVersion::DoomInternal() { |
| 802 DCHECK(!HasControllee()); | 831 DCHECK(!HasControllee()); |
| 803 SetStatus(REDUNDANT); | 832 SetStatus(REDUNDANT); |
| 804 StopWorker(base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 833 StopWorker(base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 805 if (!context_) | 834 if (!context_) |
| 806 return; | 835 return; |
| 807 std::vector<ServiceWorkerDatabase::ResourceRecord> resources; | 836 std::vector<ServiceWorkerDatabase::ResourceRecord> resources; |
| 808 script_cache_map_.GetResources(&resources); | 837 script_cache_map_.GetResources(&resources); |
| 809 context_->storage()->PurgeResources(resources); | 838 context_->storage()->PurgeResources(resources); |
| 810 } | 839 } |
| 811 | 840 |
| 812 } // namespace content | 841 } // namespace content |
| OLD | NEW |