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

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

Issue 2034663002: ServiceWorker: Keep the worker alive until FetchEvent.waitUntil settles (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix the comment and remove extra decrementPendingActivity Created 4 years, 6 months 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 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <functional> 10 #include <functional>
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 59
60 // This class corresponds to a specific version of a ServiceWorker 60 // This class corresponds to a specific version of a ServiceWorker
61 // script for a given pattern. When a script is upgraded, there may be 61 // script for a given pattern. When a script is upgraded, there may be
62 // more than one ServiceWorkerVersion "running" at a time, but only 62 // more than one ServiceWorkerVersion "running" at a time, but only
63 // one of them is activated. This class connects the actual script with a 63 // one of them is activated. This class connects the actual script with a
64 // running worker. 64 // running worker.
65 class CONTENT_EXPORT ServiceWorkerVersion 65 class CONTENT_EXPORT ServiceWorkerVersion
66 : NON_EXPORTED_BASE(public base::RefCounted<ServiceWorkerVersion>), 66 : NON_EXPORTED_BASE(public base::RefCounted<ServiceWorkerVersion>),
67 public EmbeddedWorkerInstance::Listener { 67 public EmbeddedWorkerInstance::Listener {
68 public: 68 public:
69 typedef base::Callback<void(ServiceWorkerStatusCode)> StatusCallback; 69 using StatusCallback = base::Callback<void(ServiceWorkerStatusCode)>;
70 70
71 // Current version status; some of the status (e.g. INSTALLED and ACTIVATED) 71 // Current version status; some of the status (e.g. INSTALLED and ACTIVATED)
72 // should be persisted unlike running status. 72 // should be persisted unlike running status.
73 enum Status { 73 enum Status {
74 NEW, // The version is just created. 74 NEW, // The version is just created.
75 INSTALLING, // Install event is dispatched and being handled. 75 INSTALLING, // Install event is dispatched and being handled.
76 INSTALLED, // Install event is finished and is ready to be activated. 76 INSTALLED, // Install event is finished and is ready to be activated.
77 ACTIVATING, // Activate event is dispatched and being handled. 77 ACTIVATING, // Activate event is dispatched and being handled.
78 ACTIVATED, // Activation is finished and can run as activated. 78 ACTIVATED, // Activation is finished and can run as activated.
79 REDUNDANT, // The version is no longer running as activated, due to 79 REDUNDANT, // The version is no longer running as activated, due to
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 // Connects to a specific mojo service exposed by the (running) service 219 // Connects to a specific mojo service exposed by the (running) service
220 // worker. If a connection to a service for the same Interface already exists 220 // worker. If a connection to a service for the same Interface already exists
221 // this will return that existing connection. The |request_id| must be a value 221 // this will return that existing connection. The |request_id| must be a value
222 // previously returned by StartRequest. If the connection to the service 222 // previously returned by StartRequest. If the connection to the service
223 // fails or closes before the request finished, the error callback associated 223 // fails or closes before the request finished, the error callback associated
224 // with |request_id| is called. 224 // with |request_id| is called.
225 // Only call GetMojoServiceForRequest once for a specific |request_id|. 225 // Only call GetMojoServiceForRequest once for a specific |request_id|.
226 template <typename Interface> 226 template <typename Interface>
227 base::WeakPtr<Interface> GetMojoServiceForRequest(int request_id); 227 base::WeakPtr<Interface> GetMojoServiceForRequest(int request_id);
228 228
229 // Dispatches an event. If dispatching the event fails, the error callback 229 // Dispatches an event. If dispatching the event fails, all of the error
230 // associated with the |request_id| is called. Any messages sent back in 230 // callbacks that were associated with |request_ids| via StartRequest are
231 // response to this event are passed on to the response |callback|. 231 // called.
232 // Use RegisterRequestCallback or RegisterSimpleRequest to register a callback
233 // to receive messages sent back in response to this event before calling this
234 // method.
235 // This must be called when the worker is running.
236 void DispatchEvent(const std::vector<int>& request_ids,
237 const IPC::Message& message);
238
239 // This method registers a callback to receive messages sent back from the
240 // service worker in response to |request_id|.
232 // ResponseMessage is the type of the IPC message that is used for the 241 // ResponseMessage is the type of the IPC message that is used for the
233 // response, and its first argument MUST be the request_id. 242 // response, and its first argument MUST be the request_id.
234 // This must be called when the worker is running. 243 // Callback registration should be done once for one request_id.
235 template <typename ResponseMessage, typename ResponseCallbackType> 244 template <typename ResponseMessage, typename ResponseCallbackType>
236 void DispatchEvent(int request_id, 245 void RegisterRequestCallback(int request_id,
237 const IPC::Message& message, 246 const ResponseCallbackType& callback);
238 const ResponseCallbackType& callback);
239 247
240 // For simple events where the full functionality of DispatchEvent is not 248 // You can use this method instead of RegisterRequestCallback when the
241 // needed, this method can be used instead. The ResponseMessage must consist 249 // response message sent back from the service worker consists of just
250 // a request_id and a blink::WebServiceWorkerEventResult field. The result
251 // field is converted to a ServiceWorkerStatusCode and passed to the error
252 // handler associated with the request_id which is registered by StartRequest.
253 // Additionally if you use this method, FinishRequest will be called before
254 // passing the reply to the callback.
255 // Callback registration should be done once for one request_id.
256 template <typename ResponseMessage>
257 void RegisterSimpleRequest(int request_id);
258
259 // This is a wrapper method equivalent to one RegisterSimpleRequest and one
260 // DispatchEvent. For simple events where the full functionality of
261 // RegisterRequestCallback/DispatchEvent is not needed, this method can be
262 // used instead. The ResponseMessage must consist
242 // of just a request_id and a blink::WebServiceWorkerEventResult field. The 263 // of just a request_id and a blink::WebServiceWorkerEventResult field. The
243 // result is converted to a ServiceWorkerStatusCode and passed to the error 264 // result is converted to a ServiceWorkerStatusCode and passed to the error
244 // handler associated with the request. Additionally this methods calls 265 // handler associated with the request. Additionally this methods calls
245 // FinishRequest before passing the reply to the callback. 266 // FinishRequest before passing the reply to the callback.
246 template <typename ResponseMessage> 267 template <typename ResponseMessage>
247 void DispatchSimpleEvent(int request_id, const IPC::Message& message); 268 void DispatchSimpleEvent(int request_id, const IPC::Message& message);
248 269
249 // Adds and removes |provider_host| as a controllee of this ServiceWorker. 270 // Adds and removes |provider_host| as a controllee of this ServiceWorker.
250 // A potential controllee is a host having the version as its .installing 271 // A potential controllee is a host having the version as its .installing
251 // or .waiting version. 272 // or .waiting version.
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
346 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionBrowserTest, 367 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionBrowserTest,
347 TimeoutWorkerInEvent); 368 TimeoutWorkerInEvent);
348 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStallInStoppingTest, DetachThenStart); 369 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStallInStoppingTest, DetachThenStart);
349 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStallInStoppingTest, DetachThenRestart); 370 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStallInStoppingTest, DetachThenRestart);
350 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, 371 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest,
351 RegisterForeignFetchScopes); 372 RegisterForeignFetchScopes);
352 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, RequestCustomizedTimeout); 373 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, RequestCustomizedTimeout);
353 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, 374 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest,
354 RequestCustomizedTimeoutKill); 375 RequestCustomizedTimeoutKill);
355 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, MixedRequestTimeouts); 376 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, MixedRequestTimeouts);
377 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerURLRequestJobTest, EarlyResponse);
356 378
357 class Metrics; 379 class Metrics;
358 class PingController; 380 class PingController;
359 381
360 struct RequestInfo { 382 struct RequestInfo {
361 RequestInfo(int id, 383 RequestInfo(int id,
362 ServiceWorkerMetrics::EventType event_type, 384 ServiceWorkerMetrics::EventType event_type,
363 const base::TimeTicks& expiration, 385 const base::TimeTicks& expiration,
364 TimeoutBehavior timeout_behavior); 386 TimeoutBehavior timeout_behavior);
365 ~RequestInfo(); 387 ~RequestInfo();
(...skipping 13 matching lines...) Expand all
379 401
380 CallbackType callback; 402 CallbackType callback;
381 base::TimeTicks start_time; 403 base::TimeTicks start_time;
382 ServiceWorkerMetrics::EventType event_type; 404 ServiceWorkerMetrics::EventType event_type;
383 // Name of the mojo service this request is associated with. Used to call 405 // Name of the mojo service this request is associated with. Used to call
384 // the callback when a connection closes with outstanding requests. 406 // the callback when a connection closes with outstanding requests.
385 // Compared as pointer, so should only contain static strings. Typically 407 // Compared as pointer, so should only contain static strings. Typically
386 // this would be Interface::Name_ for some mojo interface. 408 // this would be Interface::Name_ for some mojo interface.
387 const char* mojo_service = nullptr; 409 const char* mojo_service = nullptr;
388 std::unique_ptr<EmbeddedWorkerInstance::Listener> listener; 410 std::unique_ptr<EmbeddedWorkerInstance::Listener> listener;
411 bool is_dispatched = false;
389 }; 412 };
390 413
391 // Base class to enable storing a list of mojo interface pointers for 414 // Base class to enable storing a list of mojo interface pointers for
392 // arbitrary interfaces. The destructor is also responsible for calling the 415 // arbitrary interfaces. The destructor is also responsible for calling the
393 // error callbacks for any outstanding requests using this service. 416 // error callbacks for any outstanding requests using this service.
394 class CONTENT_EXPORT BaseMojoServiceWrapper { 417 class CONTENT_EXPORT BaseMojoServiceWrapper {
395 public: 418 public:
396 BaseMojoServiceWrapper(ServiceWorkerVersion* worker, 419 BaseMojoServiceWrapper(ServiceWorkerVersion* worker,
397 const char* service_name); 420 const char* service_name);
398 virtual ~BaseMojoServiceWrapper(); 421 virtual ~BaseMojoServiceWrapper();
(...skipping 24 matching lines...) Expand all
423 mojo::InterfacePtr<Interface> interface_; 446 mojo::InterfacePtr<Interface> interface_;
424 base::WeakPtrFactory<Interface> weak_ptr_factory_; 447 base::WeakPtrFactory<Interface> weak_ptr_factory_;
425 }; 448 };
426 449
427 typedef ServiceWorkerVersion self; 450 typedef ServiceWorkerVersion self;
428 using ServiceWorkerClients = std::vector<ServiceWorkerClientInfo>; 451 using ServiceWorkerClients = std::vector<ServiceWorkerClientInfo>;
429 using RequestInfoPriorityQueue = 452 using RequestInfoPriorityQueue =
430 std::priority_queue<RequestInfo, 453 std::priority_queue<RequestInfo,
431 std::vector<RequestInfo>, 454 std::vector<RequestInfo>,
432 std::greater<RequestInfo>>; 455 std::greater<RequestInfo>>;
456 using WebStatusCallback =
457 base::Callback<void(int, blink::WebServiceWorkerEventResult)>;
433 458
434 // EmbeddedWorkerInstance Listener implementation which calls a callback 459 // EmbeddedWorkerInstance Listener implementation which calls a callback
435 // on receiving a particular IPC message. ResponseMessage is the type of 460 // on receiving a particular IPC message. ResponseMessage is the type of
436 // the IPC message to listen for, while CallbackType should be a callback 461 // the IPC message to listen for, while CallbackType should be a callback
437 // with same arguments as the IPC message. 462 // with same arguments as the IPC message.
438 // Additionally only calls the callback for messages with a specific request 463 // Additionally only calls the callback for messages with a specific request
439 // id, which must be the first argument of the IPC message. 464 // id, which must be the first argument of the IPC message.
440 template <typename ResponseMessage, typename CallbackType> 465 template <typename ResponseMessage, typename CallbackType>
441 class EventResponseHandler : public EmbeddedWorkerInstance::Listener { 466 class EventResponseHandler : public EmbeddedWorkerInstance::Listener {
442 public: 467 public:
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
701 interface.set_connection_error_handler( 726 interface.set_connection_error_handler(
702 base::Bind(&ServiceWorkerVersion::OnMojoConnectionError, 727 base::Bind(&ServiceWorkerVersion::OnMojoConnectionError,
703 weak_factory_.GetWeakPtr(), Interface::Name_)); 728 weak_factory_.GetWeakPtr(), Interface::Name_));
704 service = new MojoServiceWrapper<Interface>(this, std::move(interface)); 729 service = new MojoServiceWrapper<Interface>(this, std::move(interface));
705 mojo_services_.add(Interface::Name_, base::WrapUnique(service)); 730 mojo_services_.add(Interface::Name_, base::WrapUnique(service));
706 } 731 }
707 request->mojo_service = Interface::Name_; 732 request->mojo_service = Interface::Name_;
708 return service->GetWeakPtr(); 733 return service->GetWeakPtr();
709 } 734 }
710 735
711 template <typename ResponseMessage, typename ResponseCallbackType>
712 void ServiceWorkerVersion::DispatchEvent(int request_id,
713 const IPC::Message& message,
714 const ResponseCallbackType& callback) {
715 DCHECK_EQ(EmbeddedWorkerStatus::RUNNING, running_status());
716 PendingRequest<StatusCallback>* request = custom_requests_.Lookup(request_id);
717 DCHECK(request) << "Invalid request id";
718 DCHECK(!request->listener) << "Request already dispatched an IPC event";
719
720 ServiceWorkerStatusCode status = embedded_worker_->SendMessage(message);
721 if (status != SERVICE_WORKER_OK) {
722 base::ThreadTaskRunnerHandle::Get()->PostTask(
723 FROM_HERE, base::Bind(request->callback, status));
724 custom_requests_.Remove(request_id);
725 } else {
726 request->listener.reset(
727 new EventResponseHandler<ResponseMessage, ResponseCallbackType>(
728 embedded_worker()->AsWeakPtr(), request_id, callback));
729 }
730 }
731
732 template <typename ResponseMessage> 736 template <typename ResponseMessage>
733 void ServiceWorkerVersion::DispatchSimpleEvent(int request_id, 737 void ServiceWorkerVersion::DispatchSimpleEvent(int request_id,
734 const IPC::Message& message) { 738 const IPC::Message& message) {
735 DispatchEvent<ResponseMessage>( 739 RegisterSimpleRequest<ResponseMessage>(request_id);
736 request_id, message, 740 DispatchEvent({request_id}, message);
741 }
742
743 template <typename ResponseMessage, typename ResponseCallbackType>
744 void ServiceWorkerVersion::RegisterRequestCallback(
745 int request_id,
746 const ResponseCallbackType& callback) {
747 PendingRequest<StatusCallback>* request = custom_requests_.Lookup(request_id);
748 DCHECK(request) << "Invalid request id";
749 DCHECK(!request->listener) << "Callback was already registered";
750 DCHECK(!request->is_dispatched) << "Request already dispatched an IPC event";
751 request->listener.reset(
752 new EventResponseHandler<ResponseMessage, ResponseCallbackType>(
753 embedded_worker()->AsWeakPtr(), request_id, callback));
754 }
755
756 template <typename ResponseMessage>
757 void ServiceWorkerVersion::RegisterSimpleRequest(int request_id) {
758 RegisterRequestCallback<ResponseMessage>(
759 request_id,
737 base::Bind(&ServiceWorkerVersion::OnSimpleEventResponse, this)); 760 base::Bind(&ServiceWorkerVersion::OnSimpleEventResponse, this));
738 } 761 }
739 762
740 template <typename ResponseMessage, typename CallbackType> 763 template <typename ResponseMessage, typename CallbackType>
741 bool ServiceWorkerVersion::EventResponseHandler<ResponseMessage, CallbackType>:: 764 bool ServiceWorkerVersion::EventResponseHandler<ResponseMessage, CallbackType>::
742 OnMessageReceived(const IPC::Message& message) { 765 OnMessageReceived(const IPC::Message& message) {
743 if (message.type() != ResponseMessage::ID) 766 if (message.type() != ResponseMessage::ID)
744 return false; 767 return false;
745 int received_request_id; 768 int received_request_id;
746 bool result = base::PickleIterator(message).ReadInt(&received_request_id); 769 bool result = base::PickleIterator(message).ReadInt(&received_request_id);
747 if (!result || received_request_id != request_id_) 770 if (!result || received_request_id != request_id_)
748 return false; 771 return false;
749 772
750 CallbackType protect(callback_); 773 CallbackType protect(callback_);
751 // Essentially same code as what IPC_MESSAGE_FORWARD expands to. 774 // Essentially same code as what IPC_MESSAGE_FORWARD expands to.
752 void* param = nullptr; 775 void* param = nullptr;
753 if (!ResponseMessage::Dispatch(&message, &callback_, this, param, 776 if (!ResponseMessage::Dispatch(&message, &callback_, this, param,
754 &CallbackType::Run)) 777 &CallbackType::Run))
755 message.set_dispatch_error(); 778 message.set_dispatch_error();
756 779
757 // At this point |this| can have been deleted, so don't do anything other 780 // At this point |this| can have been deleted, so don't do anything other
758 // than returning. 781 // than returning.
759 782
760 return true; 783 return true;
761 } 784 }
762 785
763 } // namespace content 786 } // namespace content
764 787
765 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ 788 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698