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

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: Rebase 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 associated with |request_ids| which are passed at each
231 // response to this event are passed on to the response |callback|. 231 // StartRequest are called.
falken 2016/06/16 06:23:10 "the error callbacks that were associated with |re
shimazu 2016/06/21 02:43:38 Done.
232 // Use RegisterRequestCallback or RegisterSimpleRequest to register a callback
233 // to receive messages sent back in response to this event.
234 // This must be called when the worker is running.
235 void DispatchEvent(const std::vector<int>& request_ids,
236 const IPC::Message& message);
237
238 // This method registers a callback to receive messages sent back from the
239 // service worker in response to |request_id|.
232 // ResponseMessage is the type of the IPC message that is used for the 240 // ResponseMessage is the type of the IPC message that is used for the
233 // response, and its first argument MUST be the request_id. 241 // response, and its first argument MUST be the request_id.
234 // This must be called when the worker is running. 242 // Callback registration should be done once for one request_id.
235 template <typename ResponseMessage, typename ResponseCallbackType> 243 template <typename ResponseMessage, typename ResponseCallbackType>
236 void DispatchEvent(int request_id, 244 void RegisterRequestCallback(int request_id,
237 const IPC::Message& message, 245 const ResponseCallbackType& callback);
238 const ResponseCallbackType& callback);
239 246
240 // For simple events where the full functionality of DispatchEvent is not 247 // You can use this method instead of RegisterRequestCallback when the
241 // needed, this method can be used instead. The ResponseMessage must consist 248 // response message sent back from the service worker consists of just
249 // a request_id and a blink::WebServiceWorkerEventResult field. The result
250 // field is converted to a ServiceWorkerStatusCode and passed to the error
251 // handler associated with the request_id which is registered at StartRequest.
falken 2016/06/16 06:23:10 "registered by"
shimazu 2016/06/21 02:43:38 Done.
252 // Additionally if you use this method, FinishRequest will be called before
253 // passing the reply to the callback.
254 // Callback registration should be done once for one request_id.
255 template <typename ResponseMessage>
256 void RegisterSimpleRequest(int request_id);
257
258 // This is a wrapper method equivalent to one RegisterSimpleRequest and one
259 // DispatchEvent. For simple events where the full functionality of
260 // RegisterRequestCallback/DispatchEvent is not needed, this method can be
261 // used instead. The ResponseMessage must consist
242 // of just a request_id and a blink::WebServiceWorkerEventResult field. The 262 // of just a request_id and a blink::WebServiceWorkerEventResult field. The
243 // result is converted to a ServiceWorkerStatusCode and passed to the error 263 // result is converted to a ServiceWorkerStatusCode and passed to the error
244 // handler associated with the request. Additionally this methods calls 264 // handler associated with the request. Additionally this methods calls
245 // FinishRequest before passing the reply to the callback. 265 // FinishRequest before passing the reply to the callback.
246 template <typename ResponseMessage> 266 template <typename ResponseMessage>
247 void DispatchSimpleEvent(int request_id, const IPC::Message& message); 267 void DispatchSimpleEvent(int request_id, const IPC::Message& message);
248 268
249 // Adds and removes |provider_host| as a controllee of this ServiceWorker. 269 // Adds and removes |provider_host| as a controllee of this ServiceWorker.
250 // A potential controllee is a host having the version as its .installing 270 // A potential controllee is a host having the version as its .installing
251 // or .waiting version. 271 // or .waiting version.
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
346 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionBrowserTest, 366 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionBrowserTest,
347 TimeoutWorkerInEvent); 367 TimeoutWorkerInEvent);
348 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStallInStoppingTest, DetachThenStart); 368 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStallInStoppingTest, DetachThenStart);
349 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStallInStoppingTest, DetachThenRestart); 369 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStallInStoppingTest, DetachThenRestart);
350 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, 370 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest,
351 RegisterForeignFetchScopes); 371 RegisterForeignFetchScopes);
352 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, RequestCustomizedTimeout); 372 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, RequestCustomizedTimeout);
353 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, 373 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest,
354 RequestCustomizedTimeoutKill); 374 RequestCustomizedTimeoutKill);
355 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, MixedRequestTimeouts); 375 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, MixedRequestTimeouts);
376 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerURLRequestJobTest, EarlyResponse);
356 377
357 class Metrics; 378 class Metrics;
358 class PingController; 379 class PingController;
359 380
360 struct RequestInfo { 381 struct RequestInfo {
361 RequestInfo(int id, 382 RequestInfo(int id,
362 ServiceWorkerMetrics::EventType event_type, 383 ServiceWorkerMetrics::EventType event_type,
363 const base::TimeTicks& expiration, 384 const base::TimeTicks& expiration,
364 TimeoutBehavior timeout_behavior); 385 TimeoutBehavior timeout_behavior);
365 ~RequestInfo(); 386 ~RequestInfo();
(...skipping 13 matching lines...) Expand all
379 400
380 CallbackType callback; 401 CallbackType callback;
381 base::TimeTicks start_time; 402 base::TimeTicks start_time;
382 ServiceWorkerMetrics::EventType event_type; 403 ServiceWorkerMetrics::EventType event_type;
383 // Name of the mojo service this request is associated with. Used to call 404 // Name of the mojo service this request is associated with. Used to call
384 // the callback when a connection closes with outstanding requests. 405 // the callback when a connection closes with outstanding requests.
385 // Compared as pointer, so should only contain static strings. Typically 406 // Compared as pointer, so should only contain static strings. Typically
386 // this would be Interface::Name_ for some mojo interface. 407 // this would be Interface::Name_ for some mojo interface.
387 const char* mojo_service = nullptr; 408 const char* mojo_service = nullptr;
388 std::unique_ptr<EmbeddedWorkerInstance::Listener> listener; 409 std::unique_ptr<EmbeddedWorkerInstance::Listener> listener;
410 bool is_dispatched = false;
389 }; 411 };
390 412
391 // Base class to enable storing a list of mojo interface pointers for 413 // Base class to enable storing a list of mojo interface pointers for
392 // arbitrary interfaces. The destructor is also responsible for calling the 414 // arbitrary interfaces. The destructor is also responsible for calling the
393 // error callbacks for any outstanding requests using this service. 415 // error callbacks for any outstanding requests using this service.
394 class CONTENT_EXPORT BaseMojoServiceWrapper { 416 class CONTENT_EXPORT BaseMojoServiceWrapper {
395 public: 417 public:
396 BaseMojoServiceWrapper(ServiceWorkerVersion* worker, 418 BaseMojoServiceWrapper(ServiceWorkerVersion* worker,
397 const char* service_name); 419 const char* service_name);
398 virtual ~BaseMojoServiceWrapper(); 420 virtual ~BaseMojoServiceWrapper();
(...skipping 24 matching lines...) Expand all
423 mojo::InterfacePtr<Interface> interface_; 445 mojo::InterfacePtr<Interface> interface_;
424 base::WeakPtrFactory<Interface> weak_ptr_factory_; 446 base::WeakPtrFactory<Interface> weak_ptr_factory_;
425 }; 447 };
426 448
427 typedef ServiceWorkerVersion self; 449 typedef ServiceWorkerVersion self;
428 using ServiceWorkerClients = std::vector<ServiceWorkerClientInfo>; 450 using ServiceWorkerClients = std::vector<ServiceWorkerClientInfo>;
429 using RequestInfoPriorityQueue = 451 using RequestInfoPriorityQueue =
430 std::priority_queue<RequestInfo, 452 std::priority_queue<RequestInfo,
431 std::vector<RequestInfo>, 453 std::vector<RequestInfo>,
432 std::greater<RequestInfo>>; 454 std::greater<RequestInfo>>;
455 using WebStatusCallback =
456 base::Callback<void(int, blink::WebServiceWorkerEventResult)>;
433 457
434 // EmbeddedWorkerInstance Listener implementation which calls a callback 458 // EmbeddedWorkerInstance Listener implementation which calls a callback
435 // on receiving a particular IPC message. ResponseMessage is the type of 459 // on receiving a particular IPC message. ResponseMessage is the type of
436 // the IPC message to listen for, while CallbackType should be a callback 460 // the IPC message to listen for, while CallbackType should be a callback
437 // with same arguments as the IPC message. 461 // with same arguments as the IPC message.
438 // Additionally only calls the callback for messages with a specific request 462 // Additionally only calls the callback for messages with a specific request
439 // id, which must be the first argument of the IPC message. 463 // id, which must be the first argument of the IPC message.
440 template <typename ResponseMessage, typename CallbackType> 464 template <typename ResponseMessage, typename CallbackType>
441 class EventResponseHandler : public EmbeddedWorkerInstance::Listener { 465 class EventResponseHandler : public EmbeddedWorkerInstance::Listener {
442 public: 466 public:
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
701 interface.set_connection_error_handler( 725 interface.set_connection_error_handler(
702 base::Bind(&ServiceWorkerVersion::OnMojoConnectionError, 726 base::Bind(&ServiceWorkerVersion::OnMojoConnectionError,
703 weak_factory_.GetWeakPtr(), Interface::Name_)); 727 weak_factory_.GetWeakPtr(), Interface::Name_));
704 service = new MojoServiceWrapper<Interface>(this, std::move(interface)); 728 service = new MojoServiceWrapper<Interface>(this, std::move(interface));
705 mojo_services_.add(Interface::Name_, base::WrapUnique(service)); 729 mojo_services_.add(Interface::Name_, base::WrapUnique(service));
706 } 730 }
707 request->mojo_service = Interface::Name_; 731 request->mojo_service = Interface::Name_;
708 return service->GetWeakPtr(); 732 return service->GetWeakPtr();
709 } 733 }
710 734
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> 735 template <typename ResponseMessage>
733 void ServiceWorkerVersion::DispatchSimpleEvent(int request_id, 736 void ServiceWorkerVersion::DispatchSimpleEvent(int request_id,
734 const IPC::Message& message) { 737 const IPC::Message& message) {
735 DispatchEvent<ResponseMessage>( 738 RegisterSimpleRequest<ResponseMessage>(request_id);
736 request_id, message, 739 DispatchEvent({request_id}, message);
740 }
741
742 template <typename ResponseMessage, typename ResponseCallbackType>
743 void ServiceWorkerVersion::RegisterRequestCallback(
744 int request_id,
745 const ResponseCallbackType& callback) {
746 PendingRequest<StatusCallback>* request = custom_requests_.Lookup(request_id);
747 DCHECK(request) << "Invalid request id";
748 DCHECK(!request->listener) << "Callback was already registered";
749 DCHECK(!request->is_dispatched) << "Request already dispatched an IPC event";
750 request->listener.reset(
751 new EventResponseHandler<ResponseMessage, ResponseCallbackType>(
752 embedded_worker()->AsWeakPtr(), request_id, callback));
753 }
754
755 template <typename ResponseMessage>
756 void ServiceWorkerVersion::RegisterSimpleRequest(int request_id) {
757 RegisterRequestCallback<ResponseMessage>(
758 request_id,
737 base::Bind(&ServiceWorkerVersion::OnSimpleEventResponse, this)); 759 base::Bind(&ServiceWorkerVersion::OnSimpleEventResponse, this));
738 } 760 }
739 761
740 template <typename ResponseMessage, typename CallbackType> 762 template <typename ResponseMessage, typename CallbackType>
741 bool ServiceWorkerVersion::EventResponseHandler<ResponseMessage, CallbackType>:: 763 bool ServiceWorkerVersion::EventResponseHandler<ResponseMessage, CallbackType>::
742 OnMessageReceived(const IPC::Message& message) { 764 OnMessageReceived(const IPC::Message& message) {
743 if (message.type() != ResponseMessage::ID) 765 if (message.type() != ResponseMessage::ID)
744 return false; 766 return false;
745 int received_request_id; 767 int received_request_id;
746 bool result = base::PickleIterator(message).ReadInt(&received_request_id); 768 bool result = base::PickleIterator(message).ReadInt(&received_request_id);
747 if (!result || received_request_id != request_id_) 769 if (!result || received_request_id != request_id_)
748 return false; 770 return false;
749 771
750 CallbackType protect(callback_); 772 CallbackType protect(callback_);
751 // Essentially same code as what IPC_MESSAGE_FORWARD expands to. 773 // Essentially same code as what IPC_MESSAGE_FORWARD expands to.
752 void* param = nullptr; 774 void* param = nullptr;
753 if (!ResponseMessage::Dispatch(&message, &callback_, this, param, 775 if (!ResponseMessage::Dispatch(&message, &callback_, this, param,
754 &CallbackType::Run)) 776 &CallbackType::Run))
755 message.set_dispatch_error(); 777 message.set_dispatch_error();
756 778
757 // At this point |this| can have been deleted, so don't do anything other 779 // At this point |this| can have been deleted, so don't do anything other
758 // than returning. 780 // than returning.
759 781
760 return true; 782 return true;
761 } 783 }
762 784
763 } // namespace content 785 } // namespace content
764 786
765 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ 787 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698