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