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 553 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
564 std::greater<RequestInfo>>; | 564 std::greater<RequestInfo>>; |
565 using WebStatusCallback = | 565 using WebStatusCallback = |
566 base::Callback<void(int, blink::WebServiceWorkerEventResult)>; | 566 base::Callback<void(int, blink::WebServiceWorkerEventResult)>; |
567 | 567 |
568 // EmbeddedWorkerInstance Listener implementation which calls a callback | 568 // EmbeddedWorkerInstance Listener implementation which calls a callback |
569 // on receiving a particular IPC message. ResponseMessage is the type of | 569 // on receiving a particular IPC message. ResponseMessage is the type of |
570 // the IPC message to listen for, while CallbackType should be a callback | 570 // the IPC message to listen for, while CallbackType should be a callback |
571 // with same arguments as the IPC message. | 571 // with same arguments as the IPC message. |
572 // Additionally only calls the callback for messages with a specific request | 572 // Additionally only calls the callback for messages with a specific request |
573 // id, which must be the first argument of the IPC message. | 573 // id, which must be the first argument of the IPC message. |
574 template <typename ResponseMessage, typename CallbackType> | 574 template <typename ResponseMessage, |
575 class EventResponseHandler : public EmbeddedWorkerInstance::Listener { | 575 typename CallbackType, |
| 576 typename Signature = typename CallbackType::RunType> |
| 577 class EventResponseHandler; |
| 578 |
| 579 template <typename ResponseMessage, typename CallbackType, typename... Args> |
| 580 class EventResponseHandler<ResponseMessage, CallbackType, void(Args...)> |
| 581 : public EmbeddedWorkerInstance::Listener { |
576 public: | 582 public: |
577 EventResponseHandler(const base::WeakPtr<EmbeddedWorkerInstance>& worker, | 583 EventResponseHandler(const base::WeakPtr<EmbeddedWorkerInstance>& worker, |
578 int request_id, | 584 int request_id, |
579 const CallbackType& callback) | 585 const CallbackType& callback) |
580 : worker_(worker), request_id_(request_id), callback_(callback) { | 586 : worker_(worker), request_id_(request_id), callback_(callback) { |
581 worker_->AddListener(this); | 587 worker_->AddListener(this); |
582 } | 588 } |
583 ~EventResponseHandler() override { | 589 ~EventResponseHandler() override { |
584 if (worker_) | 590 if (worker_) |
585 worker_->RemoveListener(this); | 591 worker_->RemoveListener(this); |
586 } | 592 } |
587 bool OnMessageReceived(const IPC::Message& message) override; | 593 bool OnMessageReceived(const IPC::Message& message) override; |
588 | 594 |
589 private: | 595 private: |
| 596 void RunCallback(Args... args) { |
| 597 callback_.Run(std::forward<Args>(args)...); |
| 598 } |
| 599 |
590 base::WeakPtr<EmbeddedWorkerInstance> const worker_; | 600 base::WeakPtr<EmbeddedWorkerInstance> const worker_; |
591 const int request_id_; | 601 const int request_id_; |
592 const CallbackType callback_; | 602 const CallbackType callback_; |
593 }; | 603 }; |
594 | 604 |
595 // The timeout timer interval. | 605 // The timeout timer interval. |
596 static const int kTimeoutTimerDelaySeconds; | 606 static const int kTimeoutTimerDelaySeconds; |
597 // Timeout for an installed worker to start. | 607 // Timeout for an installed worker to start. |
598 static const int kStartInstalledWorkerTimeoutSeconds; | 608 static const int kStartInstalledWorkerTimeoutSeconds; |
599 // Timeout for a new worker to start. | 609 // Timeout for a new worker to start. |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
888 embedded_worker()->AsWeakPtr(), request_id, callback)); | 898 embedded_worker()->AsWeakPtr(), request_id, callback)); |
889 } | 899 } |
890 | 900 |
891 template <typename ResponseMessage> | 901 template <typename ResponseMessage> |
892 void ServiceWorkerVersion::RegisterSimpleRequest(int request_id) { | 902 void ServiceWorkerVersion::RegisterSimpleRequest(int request_id) { |
893 RegisterRequestCallback<ResponseMessage>( | 903 RegisterRequestCallback<ResponseMessage>( |
894 request_id, | 904 request_id, |
895 base::Bind(&ServiceWorkerVersion::OnSimpleEventResponse, this)); | 905 base::Bind(&ServiceWorkerVersion::OnSimpleEventResponse, this)); |
896 } | 906 } |
897 | 907 |
898 template <typename ResponseMessage, typename CallbackType> | 908 template <typename ResponseMessage, typename CallbackType, typename... Args> |
899 bool ServiceWorkerVersion::EventResponseHandler<ResponseMessage, CallbackType>:: | 909 bool ServiceWorkerVersion::EventResponseHandler< |
900 OnMessageReceived(const IPC::Message& message) { | 910 ResponseMessage, |
| 911 CallbackType, |
| 912 void(Args...)>::OnMessageReceived(const IPC::Message& message) { |
901 if (message.type() != ResponseMessage::ID) | 913 if (message.type() != ResponseMessage::ID) |
902 return false; | 914 return false; |
903 int received_request_id; | 915 int received_request_id; |
904 bool result = base::PickleIterator(message).ReadInt(&received_request_id); | 916 bool result = base::PickleIterator(message).ReadInt(&received_request_id); |
905 if (!result || received_request_id != request_id_) | 917 if (!result || received_request_id != request_id_) |
906 return false; | 918 return false; |
907 | 919 |
908 CallbackType protect(callback_); | 920 CallbackType protect(callback_); |
909 // Essentially same code as what IPC_MESSAGE_FORWARD expands to. | 921 // Essentially same code as what IPC_MESSAGE_FORWARD expands to. |
910 void* param = nullptr; | 922 void* param = nullptr; |
911 if (!ResponseMessage::Dispatch(&message, &callback_, this, param, | 923 if (!ResponseMessage::Dispatch(&message, this, this, param, |
912 &CallbackType::Run)) | 924 &EventResponseHandler::RunCallback)) |
913 message.set_dispatch_error(); | 925 message.set_dispatch_error(); |
914 | 926 |
915 // At this point |this| can have been deleted, so don't do anything other | 927 // At this point |this| can have been deleted, so don't do anything other |
916 // than returning. | 928 // than returning. |
917 | 929 |
918 return true; | 930 return true; |
919 } | 931 } |
920 | 932 |
921 } // namespace content | 933 } // namespace content |
922 | 934 |
923 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ | 935 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ |
OLD | NEW |