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

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: Separate DispathEvent into two part 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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 58
59 // This class corresponds to a specific version of a ServiceWorker 59 // This class corresponds to a specific version of a ServiceWorker
60 // script for a given pattern. When a script is upgraded, there may be 60 // script for a given pattern. When a script is upgraded, there may be
61 // more than one ServiceWorkerVersion "running" at a time, but only 61 // more than one ServiceWorkerVersion "running" at a time, but only
62 // one of them is activated. This class connects the actual script with a 62 // one of them is activated. This class connects the actual script with a
63 // running worker. 63 // running worker.
64 class CONTENT_EXPORT ServiceWorkerVersion 64 class CONTENT_EXPORT ServiceWorkerVersion
65 : NON_EXPORTED_BASE(public base::RefCounted<ServiceWorkerVersion>), 65 : NON_EXPORTED_BASE(public base::RefCounted<ServiceWorkerVersion>),
66 public EmbeddedWorkerInstance::Listener { 66 public EmbeddedWorkerInstance::Listener {
67 public: 67 public:
68 typedef base::Callback<void(ServiceWorkerStatusCode)> StatusCallback; 68 using StatusCallback = base::Callback<void(ServiceWorkerStatusCode)>;
69 69
70 enum RunningStatus { 70 enum RunningStatus {
71 STOPPED = EmbeddedWorkerInstance::STOPPED, 71 STOPPED = EmbeddedWorkerInstance::STOPPED,
72 STARTING = EmbeddedWorkerInstance::STARTING, 72 STARTING = EmbeddedWorkerInstance::STARTING,
73 RUNNING = EmbeddedWorkerInstance::RUNNING, 73 RUNNING = EmbeddedWorkerInstance::RUNNING,
74 STOPPING = EmbeddedWorkerInstance::STOPPING, 74 STOPPING = EmbeddedWorkerInstance::STOPPING,
75 }; 75 };
76 76
77 // Current version status; some of the status (e.g. INSTALLED and ACTIVATED) 77 // Current version status; some of the status (e.g. INSTALLED and ACTIVATED)
78 // should be persisted unlike running status. 78 // should be persisted unlike running status.
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 // Connects to a specific mojo service exposed by the (running) service 225 // Connects to a specific mojo service exposed by the (running) service
226 // worker. If a connection to a service for the same Interface already exists 226 // worker. If a connection to a service for the same Interface already exists
227 // this will return that existing connection. The |request_id| must be a value 227 // this will return that existing connection. The |request_id| must be a value
228 // previously returned by StartRequest. If the connection to the service 228 // previously returned by StartRequest. If the connection to the service
229 // fails or closes before the request finished, the error callback associated 229 // fails or closes before the request finished, the error callback associated
230 // with |request_id| is called. 230 // with |request_id| is called.
231 // Only call GetMojoServiceForRequest once for a specific |request_id|. 231 // Only call GetMojoServiceForRequest once for a specific |request_id|.
232 template <typename Interface> 232 template <typename Interface>
233 base::WeakPtr<Interface> GetMojoServiceForRequest(int request_id); 233 base::WeakPtr<Interface> GetMojoServiceForRequest(int request_id);
234 234
235 // Dispatches an event. If dispatching the event fails, the error callback 235 // Dispatches an event. This method assumes you already registered callbacks
236 // associated with the |request_id| is called. Any messages sent back in 236 // to receive the IPCs and does not create EventResponseHandler. If
237 // response to this event are passed on to the response |callback|. 237 // dispatching the event fails, all of the error callbacks associated with the
238 // |request_id|s are called. Any messages sent back in response to this event
239 // are passed on to the response |callback|.
240 // This must be called when the worker is running.
241 template <typename... RequestIDs>
242 ServiceWorkerStatusCode DispatchEvent(const IPC::Message& message,
243 int request_id,
244 const RequestIDs&... rest);
falken 2016/06/06 10:01:40 Does RequestIDs need to be a template type? I'd ra
shimazu 2016/06/07 02:27:40 Yes, vector<int> and two-int arguments will also w
falken 2016/06/07 07:56:17 I'd prefer either vector<int> or two-int over the
shimazu 2016/06/09 05:44:21 I see. I don't have so much preference for the tem
245 ServiceWorkerStatusCode DispatchEvent(const IPC::Message& message);
246
247 // This method registers a callback corresponding to |request_id|, but
248 // this doesn't dispatch a message.
238 // ResponseMessage is the type of the IPC message that is used for the 249 // ResponseMessage is the type of the IPC message that is used for the
239 // response, and its first argument MUST be the request_id. 250 // response, and its first argument MUST be the request_id.
240 // This must be called when the worker is running. 251 // Callback registration should be done once for each request.
241 template <typename ResponseMessage, typename ResponseCallbackType> 252 template <typename ResponseMessage, typename ResponseCallbackType>
242 void DispatchEvent(int request_id, 253 void RegisterRequestCallback(int request_id,
243 const IPC::Message& message, 254 const ResponseCallbackType& callback);
244 const ResponseCallbackType& callback);
245 255
246 // For simple events where the full functionality of DispatchEvent is not 256 // This method registers the simple callback corresponding to |request_id|,
247 // needed, this method can be used instead. The ResponseMessage must consist 257 // but this doesn't dispatch a message. The ResponseMessage must consist
248 // of just a request_id and a blink::WebServiceWorkerEventResult field. The 258 // of just a request_id and a blink::WebServiceWorkerEventResult field. The
249 // result is converted to a ServiceWorkerStatusCode and passed to the error 259 // result is converted to a ServiceWorkerStatusCode and passed to the error
250 // handler associated with the request. Additionally this methods calls 260 // handler associated with the request. Additionally this methods calls
261 // FinishRequest before passing the reply to the callback.
262 // This is internally used for DispatchSimpleEvent.
263 // Callback registration should be done once for each request.
264 template <typename ResponseMessage>
265 void RegisterSimpleRequest(int request_id);
266
267 // This is a wrapper method integrating one RegisterSimpleRequest and one
268 // DispatchEvent. For simple events where the full functionality of
269 // RegisterRequestCallback/DispatchEvent is not needed, this method can be
270 // used instead. The ResponseMessage must consist
271 // of just a request_id and a blink::WebServiceWorkerEventResult field. The
272 // result is converted to a ServiceWorkerStatusCode and passed to the error
273 // handler associated with the request. Additionally this methods calls
251 // FinishRequest before passing the reply to the callback. 274 // FinishRequest before passing the reply to the callback.
252 template <typename ResponseMessage> 275 template <typename ResponseMessage>
253 void DispatchSimpleEvent(int request_id, const IPC::Message& message); 276 void DispatchSimpleEvent(int request_id, const IPC::Message& message);
254 277
255 // Adds and removes |provider_host| as a controllee of this ServiceWorker. 278 // Adds and removes |provider_host| as a controllee of this ServiceWorker.
256 // A potential controllee is a host having the version as its .installing 279 // A potential controllee is a host having the version as its .installing
257 // or .waiting version. 280 // or .waiting version.
258 void AddControllee(ServiceWorkerProviderHost* provider_host); 281 void AddControllee(ServiceWorkerProviderHost* provider_host);
259 void RemoveControllee(ServiceWorkerProviderHost* provider_host); 282 void RemoveControllee(ServiceWorkerProviderHost* provider_host);
260 283
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionBrowserTest, 371 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionBrowserTest,
349 TimeoutWorkerInEvent); 372 TimeoutWorkerInEvent);
350 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStallInStoppingTest, DetachThenStart); 373 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStallInStoppingTest, DetachThenStart);
351 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStallInStoppingTest, DetachThenRestart); 374 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStallInStoppingTest, DetachThenRestart);
352 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, 375 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest,
353 RegisterForeignFetchScopes); 376 RegisterForeignFetchScopes);
354 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, RequestCustomizedTimeout); 377 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, RequestCustomizedTimeout);
355 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, 378 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest,
356 RequestCustomizedTimeoutKill); 379 RequestCustomizedTimeoutKill);
357 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, MixedRequestTimeouts); 380 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, MixedRequestTimeouts);
381 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerURLRequestJobTest, EarlyResponse);
358 382
359 class Metrics; 383 class Metrics;
360 class PingController; 384 class PingController;
361 385
362 struct RequestInfo { 386 struct RequestInfo {
363 RequestInfo(int id, 387 RequestInfo(int id,
364 ServiceWorkerMetrics::EventType event_type, 388 ServiceWorkerMetrics::EventType event_type,
365 const base::TimeTicks& expiration, 389 const base::TimeTicks& expiration,
366 TimeoutBehavior timeout_behavior); 390 TimeoutBehavior timeout_behavior);
367 ~RequestInfo(); 391 ~RequestInfo();
(...skipping 13 matching lines...) Expand all
381 405
382 CallbackType callback; 406 CallbackType callback;
383 base::TimeTicks start_time; 407 base::TimeTicks start_time;
384 ServiceWorkerMetrics::EventType event_type; 408 ServiceWorkerMetrics::EventType event_type;
385 // Name of the mojo service this request is associated with. Used to call 409 // Name of the mojo service this request is associated with. Used to call
386 // the callback when a connection closes with outstanding requests. 410 // the callback when a connection closes with outstanding requests.
387 // Compared as pointer, so should only contain static strings. Typically 411 // Compared as pointer, so should only contain static strings. Typically
388 // this would be Interface::Name_ for some mojo interface. 412 // this would be Interface::Name_ for some mojo interface.
389 const char* mojo_service = nullptr; 413 const char* mojo_service = nullptr;
390 std::unique_ptr<EmbeddedWorkerInstance::Listener> listener; 414 std::unique_ptr<EmbeddedWorkerInstance::Listener> listener;
415 bool is_dispatched;
falken 2016/06/06 10:01:40 Need to initialize this.
shimazu 2016/06/09 05:44:21 Done.
391 }; 416 };
392 417
393 // Base class to enable storing a list of mojo interface pointers for 418 // Base class to enable storing a list of mojo interface pointers for
394 // arbitrary interfaces. The destructor is also responsible for calling the 419 // arbitrary interfaces. The destructor is also responsible for calling the
395 // error callbacks for any outstanding requests using this service. 420 // error callbacks for any outstanding requests using this service.
396 class CONTENT_EXPORT BaseMojoServiceWrapper { 421 class CONTENT_EXPORT BaseMojoServiceWrapper {
397 public: 422 public:
398 BaseMojoServiceWrapper(ServiceWorkerVersion* worker, 423 BaseMojoServiceWrapper(ServiceWorkerVersion* worker,
399 const char* service_name); 424 const char* service_name);
400 virtual ~BaseMojoServiceWrapper(); 425 virtual ~BaseMojoServiceWrapper();
(...skipping 24 matching lines...) Expand all
425 mojo::InterfacePtr<Interface> interface_; 450 mojo::InterfacePtr<Interface> interface_;
426 base::WeakPtrFactory<Interface> weak_ptr_factory_; 451 base::WeakPtrFactory<Interface> weak_ptr_factory_;
427 }; 452 };
428 453
429 typedef ServiceWorkerVersion self; 454 typedef ServiceWorkerVersion self;
430 using ServiceWorkerClients = std::vector<ServiceWorkerClientInfo>; 455 using ServiceWorkerClients = std::vector<ServiceWorkerClientInfo>;
431 using RequestInfoPriorityQueue = 456 using RequestInfoPriorityQueue =
432 std::priority_queue<RequestInfo, 457 std::priority_queue<RequestInfo,
433 std::vector<RequestInfo>, 458 std::vector<RequestInfo>,
434 std::greater<RequestInfo>>; 459 std::greater<RequestInfo>>;
460 using WebStatusCallback =
461 base::Callback<void(int, blink::WebServiceWorkerEventResult)>;
435 462
436 // EmbeddedWorkerInstance Listener implementation which calls a callback 463 // EmbeddedWorkerInstance Listener implementation which calls a callback
437 // on receiving a particular IPC message. ResponseMessage is the type of 464 // on receiving a particular IPC message. ResponseMessage is the type of
438 // the IPC message to listen for, while CallbackType should be a callback 465 // the IPC message to listen for, while CallbackType should be a callback
439 // with same arguments as the IPC message. 466 // with same arguments as the IPC message.
440 // Additionally only calls the callback for messages with a specific request 467 // Additionally only calls the callback for messages with a specific request
441 // id, which must be the first argument of the IPC message. 468 // id, which must be the first argument of the IPC message.
442 template <typename ResponseMessage, typename CallbackType> 469 template <typename ResponseMessage, typename CallbackType>
443 class EventResponseHandler : public EmbeddedWorkerInstance::Listener { 470 class EventResponseHandler : public EmbeddedWorkerInstance::Listener {
444 public: 471 public:
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
703 interface.set_connection_error_handler( 730 interface.set_connection_error_handler(
704 base::Bind(&ServiceWorkerVersion::OnMojoConnectionError, 731 base::Bind(&ServiceWorkerVersion::OnMojoConnectionError,
705 weak_factory_.GetWeakPtr(), Interface::Name_)); 732 weak_factory_.GetWeakPtr(), Interface::Name_));
706 service = new MojoServiceWrapper<Interface>(this, std::move(interface)); 733 service = new MojoServiceWrapper<Interface>(this, std::move(interface));
707 mojo_services_.add(Interface::Name_, base::WrapUnique(service)); 734 mojo_services_.add(Interface::Name_, base::WrapUnique(service));
708 } 735 }
709 request->mojo_service = Interface::Name_; 736 request->mojo_service = Interface::Name_;
710 return service->GetWeakPtr(); 737 return service->GetWeakPtr();
711 } 738 }
712 739
713 template <typename ResponseMessage, typename ResponseCallbackType> 740 template <typename... RequestIDs>
714 void ServiceWorkerVersion::DispatchEvent(int request_id, 741 ServiceWorkerStatusCode ServiceWorkerVersion::DispatchEvent(
715 const IPC::Message& message, 742 const IPC::Message& message,
716 const ResponseCallbackType& callback) { 743 int request_id,
744 const RequestIDs&... rest) {
717 DCHECK_EQ(RUNNING, running_status()); 745 DCHECK_EQ(RUNNING, running_status());
718 PendingRequest<StatusCallback>* request = custom_requests_.Lookup(request_id); 746 PendingRequest<StatusCallback>* request = custom_requests_.Lookup(request_id);
719 DCHECK(request) << "Invalid request id"; 747 DCHECK(request) << "Invalid request id";
720 DCHECK(!request->listener) << "Request already dispatched an IPC event"; 748 DCHECK(!request->is_dispatched) << "Request already dispatched an IPC event";
721 749 const ServiceWorkerStatusCode status = DispatchEvent(message, rest...);
722 ServiceWorkerStatusCode status = embedded_worker_->SendMessage(message);
723 if (status != SERVICE_WORKER_OK) { 750 if (status != SERVICE_WORKER_OK) {
724 base::ThreadTaskRunnerHandle::Get()->PostTask( 751 base::ThreadTaskRunnerHandle::Get()->PostTask(
725 FROM_HERE, base::Bind(request->callback, status)); 752 FROM_HERE, base::Bind(request->callback, status));
726 custom_requests_.Remove(request_id); 753 custom_requests_.Remove(request_id);
727 } else { 754 } else {
728 request->listener.reset( 755 request->is_dispatched = true;
729 new EventResponseHandler<ResponseMessage, ResponseCallbackType>(
730 embedded_worker()->AsWeakPtr(), request_id, callback));
731 } 756 }
757 return status;
732 } 758 }
733 759
734 template <typename ResponseMessage> 760 template <typename ResponseMessage>
735 void ServiceWorkerVersion::DispatchSimpleEvent(int request_id, 761 void ServiceWorkerVersion::DispatchSimpleEvent(int request_id,
736 const IPC::Message& message) { 762 const IPC::Message& message) {
737 DispatchEvent<ResponseMessage>( 763 RegisterSimpleRequest<ResponseMessage>(request_id);
738 request_id, message, 764 DispatchEvent(message, request_id);
765 }
766
767 template <typename ResponseMessage, typename ResponseCallbackType>
768 void ServiceWorkerVersion::RegisterRequestCallback(
769 int request_id,
770 const ResponseCallbackType& callback) {
771 PendingRequest<StatusCallback>* request = custom_requests_.Lookup(request_id);
772 DCHECK(request) << "Invalid request id";
773 DCHECK(!request->listener) << "Callback was already registered";
774 DCHECK(!request->is_dispatched) << "Request already dispatched an IPC event";
775 request->listener.reset(
776 new EventResponseHandler<ResponseMessage, ResponseCallbackType>(
777 embedded_worker()->AsWeakPtr(), request_id, callback));
778 }
779
780 template <typename ResponseMessage>
781 void ServiceWorkerVersion::RegisterSimpleRequest(int request_id) {
782 RegisterRequestCallback<ResponseMessage>(
783 request_id,
739 base::Bind(&ServiceWorkerVersion::OnSimpleEventResponse, this)); 784 base::Bind(&ServiceWorkerVersion::OnSimpleEventResponse, this));
740 } 785 }
741 786
742 template <typename ResponseMessage, typename CallbackType> 787 template <typename ResponseMessage, typename CallbackType>
743 bool ServiceWorkerVersion::EventResponseHandler<ResponseMessage, CallbackType>:: 788 bool ServiceWorkerVersion::EventResponseHandler<ResponseMessage, CallbackType>::
744 OnMessageReceived(const IPC::Message& message) { 789 OnMessageReceived(const IPC::Message& message) {
745 if (message.type() != ResponseMessage::ID) 790 if (message.type() != ResponseMessage::ID)
746 return false; 791 return false;
747 int received_request_id; 792 int received_request_id;
748 bool result = base::PickleIterator(message).ReadInt(&received_request_id); 793 bool result = base::PickleIterator(message).ReadInt(&received_request_id);
749 if (!result || received_request_id != request_id_) 794 if (!result || received_request_id != request_id_)
750 return false; 795 return false;
751 796
752 CallbackType protect(callback_); 797 CallbackType protect(callback_);
753 // Essentially same code as what IPC_MESSAGE_FORWARD expands to. 798 // Essentially same code as what IPC_MESSAGE_FORWARD expands to.
754 void* param = nullptr; 799 void* param = nullptr;
755 if (!ResponseMessage::Dispatch(&message, &callback_, this, param, 800 if (!ResponseMessage::Dispatch(&message, &callback_, this, param,
756 &CallbackType::Run)) 801 &CallbackType::Run))
757 message.set_dispatch_error(); 802 message.set_dispatch_error();
758 803
759 // At this point |this| can have been deleted, so don't do anything other 804 // At this point |this| can have been deleted, so don't do anything other
760 // than returning. 805 // than returning.
761 806
762 return true; 807 return true;
763 } 808 }
764 809
765 } // namespace content 810 } // namespace content
766 811
767 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ 812 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698