| 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 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 bool was_handled, | 272 bool was_handled, |
| 273 base::Time dispatch_event_time); | 273 base::Time dispatch_event_time); |
| 274 | 274 |
| 275 // Finishes an external request that was started by StartExternalRequest(). | 275 // Finishes an external request that was started by StartExternalRequest(). |
| 276 // Returns false if there was an error finishing the request: e.g. the request | 276 // Returns false if there was an error finishing the request: e.g. the request |
| 277 // was not found or the worker already terminated. | 277 // was not found or the worker already terminated. |
| 278 bool FinishExternalRequest(const std::string& request_uuid); | 278 bool FinishExternalRequest(const std::string& request_uuid); |
| 279 | 279 |
| 280 // Creates a callback that is to be used for marking simple events dispatched | 280 // Creates a callback that is to be used for marking simple events dispatched |
| 281 // through the ServiceWorkerEventDispatcher as finished for the |request_id|. | 281 // through the ServiceWorkerEventDispatcher as finished for the |request_id|. |
| 282 // Simple event means those events expecting a response with only a status |
| 283 // code and the dispatch time. See service_worker_event_dispatcher.mojom. |
| 282 SimpleEventCallback CreateSimpleEventCallback(int request_id); | 284 SimpleEventCallback CreateSimpleEventCallback(int request_id); |
| 283 | 285 |
| 284 // This must be called when the worker is running. | 286 // This must be called when the worker is running. |
| 285 mojom::ServiceWorkerEventDispatcher* event_dispatcher() { | 287 mojom::ServiceWorkerEventDispatcher* event_dispatcher() { |
| 286 DCHECK(event_dispatcher_.is_bound()); | 288 DCHECK(event_dispatcher_.is_bound()); |
| 287 return event_dispatcher_.get(); | 289 return event_dispatcher_.get(); |
| 288 } | 290 } |
| 289 | 291 |
| 290 // Dispatches an event. If dispatching the event fails, all of the error | 292 // Dispatches an event. If dispatching the event fails, all of the error |
| 291 // callbacks that were associated with |request_ids| via StartRequest are | 293 // callbacks that were associated with |request_ids| via StartRequest are |
| 292 // called. | 294 // called. |
| 293 // Use RegisterRequestCallback or RegisterSimpleRequest to register a callback | 295 // Use RegisterRequestCallback to register a callback to receive messages sent |
| 294 // to receive messages sent back in response to this event before calling this | 296 // back in response to this event before calling this method. This must be |
| 295 // method. | 297 // called when the worker is running. |
| 296 // This must be called when the worker is running. | |
| 297 void DispatchEvent(const std::vector<int>& request_ids, | 298 void DispatchEvent(const std::vector<int>& request_ids, |
| 298 const IPC::Message& message); | 299 const IPC::Message& message); |
| 299 | 300 |
| 300 // This method registers a callback to receive messages sent back from the | 301 // This method registers a callback to receive messages sent back from the |
| 301 // service worker in response to |request_id|. | 302 // service worker in response to |request_id|. |
| 302 // ResponseMessage is the type of the IPC message that is used for the | 303 // ResponseMessage is the type of the IPC message that is used for the |
| 303 // response, and its first argument MUST be the request_id. | 304 // response, and its first argument MUST be the request_id. |
| 304 // Callback registration should be done once for one request_id. | 305 // Callback registration should be done once for one request_id. |
| 305 template <typename ResponseMessage, typename ResponseCallbackType> | 306 template <typename ResponseMessage, typename ResponseCallbackType> |
| 306 void RegisterRequestCallback(int request_id, | 307 void RegisterRequestCallback(int request_id, |
| 307 const ResponseCallbackType& callback); | 308 const ResponseCallbackType& callback); |
| 308 | 309 |
| 309 // You can use this method instead of RegisterRequestCallback when the | |
| 310 // response message sent back from the service worker consists of just | |
| 311 // a request_id and a blink::WebServiceWorkerEventResult field. The result | |
| 312 // field is converted to a ServiceWorkerStatusCode and passed to the error | |
| 313 // handler associated with the request_id which is registered by StartRequest. | |
| 314 // Additionally if you use this method, FinishRequest will be called before | |
| 315 // passing the reply to the callback. | |
| 316 // Callback registration should be done once for one request_id. | |
| 317 template <typename ResponseMessage> | |
| 318 void RegisterSimpleRequest(int request_id); | |
| 319 | |
| 320 // This is a wrapper method equivalent to one RegisterSimpleRequest and one | |
| 321 // DispatchEvent. For simple events where the full functionality of | |
| 322 // RegisterRequestCallback/DispatchEvent is not needed, this method can be | |
| 323 // used instead. The ResponseMessage must consist | |
| 324 // of just a request_id and a blink::WebServiceWorkerEventResult field. The | |
| 325 // result is converted to a ServiceWorkerStatusCode and passed to the error | |
| 326 // handler associated with the request. Additionally this methods calls | |
| 327 // FinishRequest before passing the reply to the callback. | |
| 328 template <typename ResponseMessage> | |
| 329 void DispatchSimpleEvent(int request_id, const IPC::Message& message); | |
| 330 | |
| 331 // Adds and removes |provider_host| as a controllee of this ServiceWorker. | 310 // Adds and removes |provider_host| as a controllee of this ServiceWorker. |
| 332 void AddControllee(ServiceWorkerProviderHost* provider_host); | 311 void AddControllee(ServiceWorkerProviderHost* provider_host); |
| 333 void RemoveControllee(ServiceWorkerProviderHost* provider_host); | 312 void RemoveControllee(ServiceWorkerProviderHost* provider_host); |
| 334 | 313 |
| 335 // Returns if it has controllee. | 314 // Returns if it has controllee. |
| 336 bool HasControllee() const { return !controllee_map_.empty(); } | 315 bool HasControllee() const { return !controllee_map_.empty(); } |
| 337 std::map<std::string, ServiceWorkerProviderHost*> controllee_map() { | 316 std::map<std::string, ServiceWorkerProviderHost*> controllee_map() { |
| 338 return controllee_map_; | 317 return controllee_map_; |
| 339 } | 318 } |
| 340 | 319 |
| (...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 645 | 624 |
| 646 // Message handlers. | 625 // Message handlers. |
| 647 | 626 |
| 648 // This corresponds to the spec's get(id) steps. | 627 // This corresponds to the spec's get(id) steps. |
| 649 void OnGetClient(int request_id, const std::string& client_uuid); | 628 void OnGetClient(int request_id, const std::string& client_uuid); |
| 650 | 629 |
| 651 // This corresponds to the spec's matchAll(options) steps. | 630 // This corresponds to the spec's matchAll(options) steps. |
| 652 void OnGetClients(int request_id, | 631 void OnGetClients(int request_id, |
| 653 const ServiceWorkerClientQueryOptions& options); | 632 const ServiceWorkerClientQueryOptions& options); |
| 654 | 633 |
| 655 // Receiver function of responses of simple events dispatched through chromium | |
| 656 // IPCs. This is internally the same with OnSimpleEventFinished and will be | |
| 657 // replaced with OnSimpleEventFinished after all of simple events are | |
| 658 // dispatched via mojo. | |
| 659 void OnSimpleEventResponse(int request_id, | |
| 660 blink::WebServiceWorkerEventResult result, | |
| 661 base::Time dispatch_event_time); | |
| 662 void OnOpenWindow(int request_id, GURL url); | 634 void OnOpenWindow(int request_id, GURL url); |
| 663 void OnOpenWindowFinished(int request_id, | 635 void OnOpenWindowFinished(int request_id, |
| 664 ServiceWorkerStatusCode status, | 636 ServiceWorkerStatusCode status, |
| 665 const ServiceWorkerClientInfo& client_info); | 637 const ServiceWorkerClientInfo& client_info); |
| 666 | 638 |
| 667 void OnSetCachedMetadata(const GURL& url, const std::vector<char>& data); | 639 void OnSetCachedMetadata(const GURL& url, const std::vector<char>& data); |
| 668 void OnSetCachedMetadataFinished(int64_t callback_id, int result); | 640 void OnSetCachedMetadataFinished(int64_t callback_id, int result); |
| 669 void OnClearCachedMetadata(const GURL& url); | 641 void OnClearCachedMetadata(const GURL& url); |
| 670 void OnClearCachedMetadataFinished(int64_t callback_id, int result); | 642 void OnClearCachedMetadataFinished(int64_t callback_id, int result); |
| 671 | 643 |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 865 // This is the set of features that were used up until installation of this | 837 // This is the set of features that were used up until installation of this |
| 866 // version completed, or used during the lifetime of |this|. The values must | 838 // version completed, or used during the lifetime of |this|. The values must |
| 867 // be from blink::UseCounter::Feature enum. | 839 // be from blink::UseCounter::Feature enum. |
| 868 std::set<uint32_t> used_features_; | 840 std::set<uint32_t> used_features_; |
| 869 | 841 |
| 870 base::WeakPtrFactory<ServiceWorkerVersion> weak_factory_; | 842 base::WeakPtrFactory<ServiceWorkerVersion> weak_factory_; |
| 871 | 843 |
| 872 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersion); | 844 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersion); |
| 873 }; | 845 }; |
| 874 | 846 |
| 875 template <typename ResponseMessage> | |
| 876 void ServiceWorkerVersion::DispatchSimpleEvent(int request_id, | |
| 877 const IPC::Message& message) { | |
| 878 RegisterSimpleRequest<ResponseMessage>(request_id); | |
| 879 DispatchEvent({request_id}, message); | |
| 880 } | |
| 881 | |
| 882 template <typename ResponseMessage, typename ResponseCallbackType> | 847 template <typename ResponseMessage, typename ResponseCallbackType> |
| 883 void ServiceWorkerVersion::RegisterRequestCallback( | 848 void ServiceWorkerVersion::RegisterRequestCallback( |
| 884 int request_id, | 849 int request_id, |
| 885 const ResponseCallbackType& callback) { | 850 const ResponseCallbackType& callback) { |
| 886 PendingRequest* request = pending_requests_.Lookup(request_id); | 851 PendingRequest* request = pending_requests_.Lookup(request_id); |
| 887 DCHECK(request) << "Invalid request id"; | 852 DCHECK(request) << "Invalid request id"; |
| 888 DCHECK(!request->listener) << "Callback was already registered"; | 853 DCHECK(!request->listener) << "Callback was already registered"; |
| 889 DCHECK(!request->is_dispatched) << "Request already dispatched an IPC event"; | 854 DCHECK(!request->is_dispatched) << "Request already dispatched an IPC event"; |
| 890 request->listener.reset( | 855 request->listener.reset( |
| 891 new EventResponseHandler<ResponseMessage, ResponseCallbackType>( | 856 new EventResponseHandler<ResponseMessage, ResponseCallbackType>( |
| 892 embedded_worker()->AsWeakPtr(), request_id, callback)); | 857 embedded_worker()->AsWeakPtr(), request_id, callback)); |
| 893 } | 858 } |
| 894 | 859 |
| 895 template <typename ResponseMessage> | |
| 896 void ServiceWorkerVersion::RegisterSimpleRequest(int request_id) { | |
| 897 RegisterRequestCallback<ResponseMessage>( | |
| 898 request_id, | |
| 899 base::Bind(&ServiceWorkerVersion::OnSimpleEventResponse, this)); | |
| 900 } | |
| 901 | |
| 902 template <typename ResponseMessage, typename CallbackType, typename... Args> | 860 template <typename ResponseMessage, typename CallbackType, typename... Args> |
| 903 bool ServiceWorkerVersion::EventResponseHandler< | 861 bool ServiceWorkerVersion::EventResponseHandler< |
| 904 ResponseMessage, | 862 ResponseMessage, |
| 905 CallbackType, | 863 CallbackType, |
| 906 void(Args...)>::OnMessageReceived(const IPC::Message& message) { | 864 void(Args...)>::OnMessageReceived(const IPC::Message& message) { |
| 907 if (message.type() != ResponseMessage::ID) | 865 if (message.type() != ResponseMessage::ID) |
| 908 return false; | 866 return false; |
| 909 int received_request_id; | 867 int received_request_id; |
| 910 bool result = base::PickleIterator(message).ReadInt(&received_request_id); | 868 bool result = base::PickleIterator(message).ReadInt(&received_request_id); |
| 911 if (!result || received_request_id != request_id_) | 869 if (!result || received_request_id != request_id_) |
| 912 return false; | 870 return false; |
| 913 | 871 |
| 914 CallbackType protect(callback_); | 872 CallbackType protect(callback_); |
| 915 // Essentially same code as what IPC_MESSAGE_FORWARD expands to. | 873 // Essentially same code as what IPC_MESSAGE_FORWARD expands to. |
| 916 void* param = nullptr; | 874 void* param = nullptr; |
| 917 if (!ResponseMessage::Dispatch(&message, this, this, param, | 875 if (!ResponseMessage::Dispatch(&message, this, this, param, |
| 918 &EventResponseHandler::RunCallback)) | 876 &EventResponseHandler::RunCallback)) |
| 919 message.set_dispatch_error(); | 877 message.set_dispatch_error(); |
| 920 | 878 |
| 921 // At this point |this| can have been deleted, so don't do anything other | 879 // At this point |this| can have been deleted, so don't do anything other |
| 922 // than returning. | 880 // than returning. |
| 923 | 881 |
| 924 return true; | 882 return true; |
| 925 } | 883 } |
| 926 | 884 |
| 927 } // namespace content | 885 } // namespace content |
| 928 | 886 |
| 929 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ | 887 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ |
| OLD | NEW |