| 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 #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 Loading... |
| 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 Loading... |
| 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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionBrowserTest, | 365 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionBrowserTest, |
| 345 TimeoutWorkerInEvent); | 366 TimeoutWorkerInEvent); |
| 346 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStallInStoppingTest, DetachThenStart); | 367 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStallInStoppingTest, DetachThenStart); |
| 347 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStallInStoppingTest, DetachThenRestart); | 368 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStallInStoppingTest, DetachThenRestart); |
| 348 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, | 369 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, |
| 349 RegisterForeignFetchScopes); | 370 RegisterForeignFetchScopes); |
| 350 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, RequestCustomizedTimeout); | 371 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, RequestCustomizedTimeout); |
| 351 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, | 372 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, |
| 352 RequestCustomizedTimeoutKill); | 373 RequestCustomizedTimeoutKill); |
| 353 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, MixedRequestTimeouts); | 374 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, MixedRequestTimeouts); |
| 375 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerURLRequestJobTest, EarlyResponse); |
| 354 | 376 |
| 355 class Metrics; | 377 class Metrics; |
| 356 class PingController; | 378 class PingController; |
| 357 | 379 |
| 358 struct RequestInfo { | 380 struct RequestInfo { |
| 359 RequestInfo(int id, | 381 RequestInfo(int id, |
| 360 ServiceWorkerMetrics::EventType event_type, | 382 ServiceWorkerMetrics::EventType event_type, |
| 361 const base::TimeTicks& expiration, | 383 const base::TimeTicks& expiration, |
| 362 TimeoutBehavior timeout_behavior); | 384 TimeoutBehavior timeout_behavior); |
| 363 ~RequestInfo(); | 385 ~RequestInfo(); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 377 | 399 |
| 378 CallbackType callback; | 400 CallbackType callback; |
| 379 base::TimeTicks start_time; | 401 base::TimeTicks start_time; |
| 380 ServiceWorkerMetrics::EventType event_type; | 402 ServiceWorkerMetrics::EventType event_type; |
| 381 // Name of the mojo service this request is associated with. Used to call | 403 // Name of the mojo service this request is associated with. Used to call |
| 382 // the callback when a connection closes with outstanding requests. | 404 // the callback when a connection closes with outstanding requests. |
| 383 // Compared as pointer, so should only contain static strings. Typically | 405 // Compared as pointer, so should only contain static strings. Typically |
| 384 // this would be Interface::Name_ for some mojo interface. | 406 // this would be Interface::Name_ for some mojo interface. |
| 385 const char* mojo_service = nullptr; | 407 const char* mojo_service = nullptr; |
| 386 std::unique_ptr<EmbeddedWorkerInstance::Listener> listener; | 408 std::unique_ptr<EmbeddedWorkerInstance::Listener> listener; |
| 409 bool is_dispatched = false; |
| 387 }; | 410 }; |
| 388 | 411 |
| 389 // Base class to enable storing a list of mojo interface pointers for | 412 // Base class to enable storing a list of mojo interface pointers for |
| 390 // arbitrary interfaces. The destructor is also responsible for calling the | 413 // arbitrary interfaces. The destructor is also responsible for calling the |
| 391 // error callbacks for any outstanding requests using this service. | 414 // error callbacks for any outstanding requests using this service. |
| 392 class CONTENT_EXPORT BaseMojoServiceWrapper { | 415 class CONTENT_EXPORT BaseMojoServiceWrapper { |
| 393 public: | 416 public: |
| 394 BaseMojoServiceWrapper(ServiceWorkerVersion* worker, | 417 BaseMojoServiceWrapper(ServiceWorkerVersion* worker, |
| 395 const char* service_name); | 418 const char* service_name); |
| 396 virtual ~BaseMojoServiceWrapper(); | 419 virtual ~BaseMojoServiceWrapper(); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 421 mojo::InterfacePtr<Interface> interface_; | 444 mojo::InterfacePtr<Interface> interface_; |
| 422 base::WeakPtrFactory<Interface> weak_ptr_factory_; | 445 base::WeakPtrFactory<Interface> weak_ptr_factory_; |
| 423 }; | 446 }; |
| 424 | 447 |
| 425 typedef ServiceWorkerVersion self; | 448 typedef ServiceWorkerVersion self; |
| 426 using ServiceWorkerClients = std::vector<ServiceWorkerClientInfo>; | 449 using ServiceWorkerClients = std::vector<ServiceWorkerClientInfo>; |
| 427 using RequestInfoPriorityQueue = | 450 using RequestInfoPriorityQueue = |
| 428 std::priority_queue<RequestInfo, | 451 std::priority_queue<RequestInfo, |
| 429 std::vector<RequestInfo>, | 452 std::vector<RequestInfo>, |
| 430 std::greater<RequestInfo>>; | 453 std::greater<RequestInfo>>; |
| 454 using WebStatusCallback = |
| 455 base::Callback<void(int, blink::WebServiceWorkerEventResult)>; |
| 431 | 456 |
| 432 // EmbeddedWorkerInstance Listener implementation which calls a callback | 457 // EmbeddedWorkerInstance Listener implementation which calls a callback |
| 433 // on receiving a particular IPC message. ResponseMessage is the type of | 458 // on receiving a particular IPC message. ResponseMessage is the type of |
| 434 // the IPC message to listen for, while CallbackType should be a callback | 459 // the IPC message to listen for, while CallbackType should be a callback |
| 435 // with same arguments as the IPC message. | 460 // with same arguments as the IPC message. |
| 436 // Additionally only calls the callback for messages with a specific request | 461 // Additionally only calls the callback for messages with a specific request |
| 437 // id, which must be the first argument of the IPC message. | 462 // id, which must be the first argument of the IPC message. |
| 438 template <typename ResponseMessage, typename CallbackType> | 463 template <typename ResponseMessage, typename CallbackType> |
| 439 class EventResponseHandler : public EmbeddedWorkerInstance::Listener { | 464 class EventResponseHandler : public EmbeddedWorkerInstance::Listener { |
| 440 public: | 465 public: |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 699 interface.set_connection_error_handler( | 724 interface.set_connection_error_handler( |
| 700 base::Bind(&ServiceWorkerVersion::OnMojoConnectionError, | 725 base::Bind(&ServiceWorkerVersion::OnMojoConnectionError, |
| 701 weak_factory_.GetWeakPtr(), Interface::Name_)); | 726 weak_factory_.GetWeakPtr(), Interface::Name_)); |
| 702 service = new MojoServiceWrapper<Interface>(this, std::move(interface)); | 727 service = new MojoServiceWrapper<Interface>(this, std::move(interface)); |
| 703 mojo_services_.add(Interface::Name_, base::WrapUnique(service)); | 728 mojo_services_.add(Interface::Name_, base::WrapUnique(service)); |
| 704 } | 729 } |
| 705 request->mojo_service = Interface::Name_; | 730 request->mojo_service = Interface::Name_; |
| 706 return service->GetWeakPtr(); | 731 return service->GetWeakPtr(); |
| 707 } | 732 } |
| 708 | 733 |
| 709 template <typename ResponseMessage, typename ResponseCallbackType> | |
| 710 void ServiceWorkerVersion::DispatchEvent(int request_id, | |
| 711 const IPC::Message& message, | |
| 712 const ResponseCallbackType& callback) { | |
| 713 DCHECK_EQ(EmbeddedWorkerStatus::RUNNING, running_status()); | |
| 714 PendingRequest<StatusCallback>* request = custom_requests_.Lookup(request_id); | |
| 715 DCHECK(request) << "Invalid request id"; | |
| 716 DCHECK(!request->listener) << "Request already dispatched an IPC event"; | |
| 717 | |
| 718 ServiceWorkerStatusCode status = embedded_worker_->SendMessage(message); | |
| 719 if (status != SERVICE_WORKER_OK) { | |
| 720 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 721 FROM_HERE, base::Bind(request->callback, status)); | |
| 722 custom_requests_.Remove(request_id); | |
| 723 } else { | |
| 724 request->listener.reset( | |
| 725 new EventResponseHandler<ResponseMessage, ResponseCallbackType>( | |
| 726 embedded_worker()->AsWeakPtr(), request_id, callback)); | |
| 727 } | |
| 728 } | |
| 729 | |
| 730 template <typename ResponseMessage> | 734 template <typename ResponseMessage> |
| 731 void ServiceWorkerVersion::DispatchSimpleEvent(int request_id, | 735 void ServiceWorkerVersion::DispatchSimpleEvent(int request_id, |
| 732 const IPC::Message& message) { | 736 const IPC::Message& message) { |
| 733 DispatchEvent<ResponseMessage>( | 737 RegisterSimpleRequest<ResponseMessage>(request_id); |
| 734 request_id, message, | 738 DispatchEvent({request_id}, message); |
| 739 } |
| 740 |
| 741 template <typename ResponseMessage, typename ResponseCallbackType> |
| 742 void ServiceWorkerVersion::RegisterRequestCallback( |
| 743 int request_id, |
| 744 const ResponseCallbackType& callback) { |
| 745 PendingRequest<StatusCallback>* request = custom_requests_.Lookup(request_id); |
| 746 DCHECK(request) << "Invalid request id"; |
| 747 DCHECK(!request->listener) << "Callback was already registered"; |
| 748 DCHECK(!request->is_dispatched) << "Request already dispatched an IPC event"; |
| 749 request->listener.reset( |
| 750 new EventResponseHandler<ResponseMessage, ResponseCallbackType>( |
| 751 embedded_worker()->AsWeakPtr(), request_id, callback)); |
| 752 } |
| 753 |
| 754 template <typename ResponseMessage> |
| 755 void ServiceWorkerVersion::RegisterSimpleRequest(int request_id) { |
| 756 RegisterRequestCallback<ResponseMessage>( |
| 757 request_id, |
| 735 base::Bind(&ServiceWorkerVersion::OnSimpleEventResponse, this)); | 758 base::Bind(&ServiceWorkerVersion::OnSimpleEventResponse, this)); |
| 736 } | 759 } |
| 737 | 760 |
| 738 template <typename ResponseMessage, typename CallbackType> | 761 template <typename ResponseMessage, typename CallbackType> |
| 739 bool ServiceWorkerVersion::EventResponseHandler<ResponseMessage, CallbackType>:: | 762 bool ServiceWorkerVersion::EventResponseHandler<ResponseMessage, CallbackType>:: |
| 740 OnMessageReceived(const IPC::Message& message) { | 763 OnMessageReceived(const IPC::Message& message) { |
| 741 if (message.type() != ResponseMessage::ID) | 764 if (message.type() != ResponseMessage::ID) |
| 742 return false; | 765 return false; |
| 743 int received_request_id; | 766 int received_request_id; |
| 744 bool result = base::PickleIterator(message).ReadInt(&received_request_id); | 767 bool result = base::PickleIterator(message).ReadInt(&received_request_id); |
| 745 if (!result || received_request_id != request_id_) | 768 if (!result || received_request_id != request_id_) |
| 746 return false; | 769 return false; |
| 747 | 770 |
| 748 CallbackType protect(callback_); | 771 CallbackType protect(callback_); |
| 749 // Essentially same code as what IPC_MESSAGE_FORWARD expands to. | 772 // Essentially same code as what IPC_MESSAGE_FORWARD expands to. |
| 750 void* param = nullptr; | 773 void* param = nullptr; |
| 751 if (!ResponseMessage::Dispatch(&message, &callback_, this, param, | 774 if (!ResponseMessage::Dispatch(&message, &callback_, this, param, |
| 752 &CallbackType::Run)) | 775 &CallbackType::Run)) |
| 753 message.set_dispatch_error(); | 776 message.set_dispatch_error(); |
| 754 | 777 |
| 755 // At this point |this| can have been deleted, so don't do anything other | 778 // At this point |this| can have been deleted, so don't do anything other |
| 756 // than returning. | 779 // than returning. |
| 757 | 780 |
| 758 return true; | 781 return true; |
| 759 } | 782 } |
| 760 | 783 |
| 761 } // namespace content | 784 } // namespace content |
| 762 | 785 |
| 763 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ | 786 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ |
| OLD | NEW |