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

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

Issue 717353004: ServiceWorker: Add support for .skipWaiting and controllerchange event(2/3) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase again 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/memory/ref_counted.h" 8 #include "base/memory/ref_counted.h"
9 #include "base/stl_util.h" 9 #include "base/stl_util.h"
10 #include "base/strings/string16.h" 10 #include "base/strings/string16.h"
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 const GURL& script_url, 162 const GURL& script_url,
163 int64 version_id, 163 int64 version_id,
164 base::WeakPtr<ServiceWorkerContextCore> context) 164 base::WeakPtr<ServiceWorkerContextCore> context)
165 : version_id_(version_id), 165 : version_id_(version_id),
166 registration_id_(kInvalidServiceWorkerVersionId), 166 registration_id_(kInvalidServiceWorkerVersionId),
167 script_url_(script_url), 167 script_url_(script_url),
168 status_(NEW), 168 status_(NEW),
169 context_(context), 169 context_(context),
170 script_cache_map_(this, context), 170 script_cache_map_(this, context),
171 is_doomed_(false), 171 is_doomed_(false),
172 skip_waiting_(false),
172 weak_factory_(this) { 173 weak_factory_(this) {
173 DCHECK(context_); 174 DCHECK(context_);
174 DCHECK(registration); 175 DCHECK(registration);
175 if (registration) { 176 if (registration) {
176 registration_id_ = registration->id(); 177 registration_id_ = registration->id();
177 scope_ = registration->pattern(); 178 scope_ = registration->pattern();
178 } 179 }
179 context_->AddLiveVersion(this); 180 context_->AddLiveVersion(this);
180 embedded_worker_ = context_->embedded_worker_registry()->CreateWorker(); 181 embedded_worker_ = context_->embedded_worker_registry()->CreateWorker();
181 embedded_worker_->AddListener(this); 182 embedded_worker_->AddListener(this);
182 } 183 }
183 184
184 ServiceWorkerVersion::~ServiceWorkerVersion() { 185 ServiceWorkerVersion::~ServiceWorkerVersion() {
185 embedded_worker_->RemoveListener(this); 186 embedded_worker_->RemoveListener(this);
186 if (context_) 187 if (context_)
187 context_->RemoveLiveVersion(version_id_); 188 context_->RemoveLiveVersion(version_id_);
188 // EmbeddedWorker's dtor sends StopWorker if it's still running. 189 // EmbeddedWorker's dtor sends StopWorker if it's still running.
189 } 190 }
190 191
191 void ServiceWorkerVersion::SetStatus(Status status) { 192 void ServiceWorkerVersion::SetStatus(Status status) {
192 if (status_ == status) 193 if (status_ == status)
193 return; 194 return;
194 195
195 // Schedule to stop worker after registration successfully completed. 196 // Schedule to stop worker after registration successfully completed.
196 if (status_ == ACTIVATING && status == ACTIVATED && !HasControllee()) 197 if (status_ == ACTIVATING && status == ACTIVATED && !HasControllee())
197 ScheduleStopWorker(); 198 ScheduleStopWorker();
198 199
199 status_ = status; 200 status_ = status;
200 201
202 if (skip_waiting_ && status_ == ACTIVATED) {
203 for (int request_id : pending_skip_waiting_requests_)
204 DidSkipWaiting(request_id);
205 pending_skip_waiting_requests_.clear();
206 }
207
201 std::vector<base::Closure> callbacks; 208 std::vector<base::Closure> callbacks;
202 callbacks.swap(status_change_callbacks_); 209 callbacks.swap(status_change_callbacks_);
203 for (const auto& callback : callbacks) 210 for (const auto& callback : callbacks)
204 callback.Run(); 211 callback.Run();
205 212
206 FOR_EACH_OBSERVER(Listener, listeners_, OnVersionStateChanged(this)); 213 FOR_EACH_OBSERVER(Listener, listeners_, OnVersionStateChanged(this));
207 } 214 }
208 215
209 void ServiceWorkerVersion::RegisterStatusChangeCallback( 216 void ServiceWorkerVersion::RegisterStatusChangeCallback(
210 const base::Closure& callback) { 217 const base::Closure& callback) {
(...skipping 492 matching lines...) Expand 10 before | Expand all | Expand 10 after
703 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GeofencingEventFinished, 710 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GeofencingEventFinished,
704 OnGeofencingEventFinished) 711 OnGeofencingEventFinished)
705 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToDocument, 712 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToDocument,
706 OnPostMessageToDocument) 713 OnPostMessageToDocument)
707 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FocusClient, 714 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FocusClient,
708 OnFocusClient) 715 OnFocusClient)
709 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetClientInfoSuccess, 716 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetClientInfoSuccess,
710 OnGetClientInfoSuccess) 717 OnGetClientInfoSuccess)
711 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetClientInfoError, 718 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetClientInfoError,
712 OnGetClientInfoError) 719 OnGetClientInfoError)
720 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SkipWaiting,
721 OnSkipWaiting)
713 IPC_MESSAGE_UNHANDLED(handled = false) 722 IPC_MESSAGE_UNHANDLED(handled = false)
714 IPC_END_MESSAGE_MAP() 723 IPC_END_MESSAGE_MAP()
715 return handled; 724 return handled;
716 } 725 }
717 726
718 void ServiceWorkerVersion::OnStartMessageSent( 727 void ServiceWorkerVersion::OnStartMessageSent(
719 ServiceWorkerStatusCode status) { 728 ServiceWorkerStatusCode status) {
720 if (status != SERVICE_WORKER_OK) 729 if (status != SERVICE_WORKER_OK)
721 RunCallbacks(this, &start_callbacks_, status); 730 RunCallbacks(this, &start_callbacks_, status);
722 } 731 }
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
965 void ServiceWorkerVersion::OnFocusClientFinished(int request_id, bool result) { 974 void ServiceWorkerVersion::OnFocusClientFinished(int request_id, bool result) {
966 DCHECK_CURRENTLY_ON(BrowserThread::IO); 975 DCHECK_CURRENTLY_ON(BrowserThread::IO);
967 976
968 if (running_status() != RUNNING) 977 if (running_status() != RUNNING)
969 return; 978 return;
970 979
971 embedded_worker_->SendMessage(ServiceWorkerMsg_FocusClientResponse( 980 embedded_worker_->SendMessage(ServiceWorkerMsg_FocusClientResponse(
972 request_id, result)); 981 request_id, result));
973 } 982 }
974 983
984 void ServiceWorkerVersion::OnSkipWaiting(int request_id) {
985 skip_waiting_ = true;
986 if (status_ != INSTALLED)
987 return DidSkipWaiting(request_id);
988
989 if (!context_)
990 return;
991 ServiceWorkerRegistration* registration =
992 context_->GetLiveRegistration(registration_id_);
993 if (!registration)
994 return;
995 pending_skip_waiting_requests_.push_back(request_id);
996 if (pending_skip_waiting_requests_.size() == 1)
997 registration->ActivateWaitingVersionWhenReady();
998 }
999
1000 void ServiceWorkerVersion::DidSkipWaiting(int request_id) {
1001 if (running_status() == STARTING || running_status() == RUNNING)
1002 embedded_worker_->SendMessage(ServiceWorkerMsg_DidSkipWaiting(request_id));
1003 }
1004
975 void ServiceWorkerVersion::ScheduleStopWorker() { 1005 void ServiceWorkerVersion::ScheduleStopWorker() {
976 if (running_status() != RUNNING) 1006 if (running_status() != RUNNING)
977 return; 1007 return;
978 if (stop_worker_timer_.IsRunning()) { 1008 if (stop_worker_timer_.IsRunning()) {
979 stop_worker_timer_.Reset(); 1009 stop_worker_timer_.Reset();
980 return; 1010 return;
981 } 1011 }
982 stop_worker_timer_.Start( 1012 stop_worker_timer_.Start(
983 FROM_HERE, base::TimeDelta::FromSeconds(kStopWorkerDelay), 1013 FROM_HERE, base::TimeDelta::FromSeconds(kStopWorkerDelay),
984 base::Bind(&ServiceWorkerVersion::StopWorker, 1014 base::Bind(&ServiceWorkerVersion::StopWorker,
985 weak_factory_.GetWeakPtr(), 1015 weak_factory_.GetWeakPtr(),
986 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback))); 1016 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)));
987 } 1017 }
988 1018
989 void ServiceWorkerVersion::DoomInternal() { 1019 void ServiceWorkerVersion::DoomInternal() {
990 DCHECK(!HasControllee()); 1020 DCHECK(!HasControllee());
991 SetStatus(REDUNDANT); 1021 SetStatus(REDUNDANT);
992 StopWorker(base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); 1022 StopWorker(base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
993 if (!context_) 1023 if (!context_)
994 return; 1024 return;
995 std::vector<ServiceWorkerDatabase::ResourceRecord> resources; 1025 std::vector<ServiceWorkerDatabase::ResourceRecord> resources;
996 script_cache_map_.GetResources(&resources); 1026 script_cache_map_.GetResources(&resources);
997 context_->storage()->PurgeResources(resources); 1027 context_->storage()->PurgeResources(resources);
998 } 1028 }
999 1029
1000 } // namespace content 1030 } // 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