| 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 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 | 283 |
| 284 // This must be called when the worker is running. | 284 // This must be called when the worker is running. |
| 285 mojom::ServiceWorkerEventDispatcher* event_dispatcher() { | 285 mojom::ServiceWorkerEventDispatcher* event_dispatcher() { |
| 286 DCHECK(event_dispatcher_.is_bound()); | 286 DCHECK(event_dispatcher_.is_bound()); |
| 287 return event_dispatcher_.get(); | 287 return event_dispatcher_.get(); |
| 288 } | 288 } |
| 289 | 289 |
| 290 // Dispatches an event. If dispatching the event fails, all of the error | 290 // Dispatches an event. If dispatching the event fails, all of the error |
| 291 // callbacks that were associated with |request_ids| via StartRequest are | 291 // callbacks that were associated with |request_ids| via StartRequest are |
| 292 // called. | 292 // called. |
| 293 // Use RegisterRequestCallback or RegisterSimpleRequest to register a callback | 293 // Use RegisterRequestCallback to register a callback to receive messages sent |
| 294 // to receive messages sent back in response to this event before calling this | 294 // back in response to this event before calling this method. This must be |
| 295 // method. | 295 // 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, | 296 void DispatchEvent(const std::vector<int>& request_ids, |
| 298 const IPC::Message& message); | 297 const IPC::Message& message); |
| 299 | 298 |
| 300 // This method registers a callback to receive messages sent back from the | 299 // This method registers a callback to receive messages sent back from the |
| 301 // service worker in response to |request_id|. | 300 // service worker in response to |request_id|. |
| 302 // ResponseMessage is the type of the IPC message that is used for the | 301 // ResponseMessage is the type of the IPC message that is used for the |
| 303 // response, and its first argument MUST be the request_id. | 302 // response, and its first argument MUST be the request_id. |
| 304 // Callback registration should be done once for one request_id. | 303 // Callback registration should be done once for one request_id. |
| 305 template <typename ResponseMessage, typename ResponseCallbackType> | 304 template <typename ResponseMessage, typename ResponseCallbackType> |
| 306 void RegisterRequestCallback(int request_id, | 305 void RegisterRequestCallback(int request_id, |
| 307 const ResponseCallbackType& callback); | 306 const ResponseCallbackType& callback); |
| 308 | 307 |
| 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. | 308 // Adds and removes |provider_host| as a controllee of this ServiceWorker. |
| 332 void AddControllee(ServiceWorkerProviderHost* provider_host); | 309 void AddControllee(ServiceWorkerProviderHost* provider_host); |
| 333 void RemoveControllee(ServiceWorkerProviderHost* provider_host); | 310 void RemoveControllee(ServiceWorkerProviderHost* provider_host); |
| 334 | 311 |
| 335 // Returns if it has controllee. | 312 // Returns if it has controllee. |
| 336 bool HasControllee() const { return !controllee_map_.empty(); } | 313 bool HasControllee() const { return !controllee_map_.empty(); } |
| 337 std::map<std::string, ServiceWorkerProviderHost*> controllee_map() { | 314 std::map<std::string, ServiceWorkerProviderHost*> controllee_map() { |
| 338 return controllee_map_; | 315 return controllee_map_; |
| 339 } | 316 } |
| 340 | 317 |
| (...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 645 | 622 |
| 646 // Message handlers. | 623 // Message handlers. |
| 647 | 624 |
| 648 // This corresponds to the spec's get(id) steps. | 625 // This corresponds to the spec's get(id) steps. |
| 649 void OnGetClient(int request_id, const std::string& client_uuid); | 626 void OnGetClient(int request_id, const std::string& client_uuid); |
| 650 | 627 |
| 651 // This corresponds to the spec's matchAll(options) steps. | 628 // This corresponds to the spec's matchAll(options) steps. |
| 652 void OnGetClients(int request_id, | 629 void OnGetClients(int request_id, |
| 653 const ServiceWorkerClientQueryOptions& options); | 630 const ServiceWorkerClientQueryOptions& options); |
| 654 | 631 |
| 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); | 632 void OnOpenWindow(int request_id, GURL url); |
| 663 void OnOpenWindowFinished(int request_id, | 633 void OnOpenWindowFinished(int request_id, |
| 664 ServiceWorkerStatusCode status, | 634 ServiceWorkerStatusCode status, |
| 665 const ServiceWorkerClientInfo& client_info); | 635 const ServiceWorkerClientInfo& client_info); |
| 666 | 636 |
| 667 void OnSetCachedMetadata(const GURL& url, const std::vector<char>& data); | 637 void OnSetCachedMetadata(const GURL& url, const std::vector<char>& data); |
| 668 void OnSetCachedMetadataFinished(int64_t callback_id, int result); | 638 void OnSetCachedMetadataFinished(int64_t callback_id, int result); |
| 669 void OnClearCachedMetadata(const GURL& url); | 639 void OnClearCachedMetadata(const GURL& url); |
| 670 void OnClearCachedMetadataFinished(int64_t callback_id, int result); | 640 void OnClearCachedMetadataFinished(int64_t callback_id, int result); |
| 671 | 641 |
| (...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 | 835 // 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 | 836 // version completed, or used during the lifetime of |this|. The values must |
| 867 // be from blink::UseCounter::Feature enum. | 837 // be from blink::UseCounter::Feature enum. |
| 868 std::set<uint32_t> used_features_; | 838 std::set<uint32_t> used_features_; |
| 869 | 839 |
| 870 base::WeakPtrFactory<ServiceWorkerVersion> weak_factory_; | 840 base::WeakPtrFactory<ServiceWorkerVersion> weak_factory_; |
| 871 | 841 |
| 872 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersion); | 842 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersion); |
| 873 }; | 843 }; |
| 874 | 844 |
| 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> | 845 template <typename ResponseMessage, typename ResponseCallbackType> |
| 883 void ServiceWorkerVersion::RegisterRequestCallback( | 846 void ServiceWorkerVersion::RegisterRequestCallback( |
| 884 int request_id, | 847 int request_id, |
| 885 const ResponseCallbackType& callback) { | 848 const ResponseCallbackType& callback) { |
| 886 PendingRequest* request = pending_requests_.Lookup(request_id); | 849 PendingRequest* request = pending_requests_.Lookup(request_id); |
| 887 DCHECK(request) << "Invalid request id"; | 850 DCHECK(request) << "Invalid request id"; |
| 888 DCHECK(!request->listener) << "Callback was already registered"; | 851 DCHECK(!request->listener) << "Callback was already registered"; |
| 889 DCHECK(!request->is_dispatched) << "Request already dispatched an IPC event"; | 852 DCHECK(!request->is_dispatched) << "Request already dispatched an IPC event"; |
| 890 request->listener.reset( | 853 request->listener.reset( |
| 891 new EventResponseHandler<ResponseMessage, ResponseCallbackType>( | 854 new EventResponseHandler<ResponseMessage, ResponseCallbackType>( |
| 892 embedded_worker()->AsWeakPtr(), request_id, callback)); | 855 embedded_worker()->AsWeakPtr(), request_id, callback)); |
| 893 } | 856 } |
| 894 | 857 |
| 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> | 858 template <typename ResponseMessage, typename CallbackType, typename... Args> |
| 903 bool ServiceWorkerVersion::EventResponseHandler< | 859 bool ServiceWorkerVersion::EventResponseHandler< |
| 904 ResponseMessage, | 860 ResponseMessage, |
| 905 CallbackType, | 861 CallbackType, |
| 906 void(Args...)>::OnMessageReceived(const IPC::Message& message) { | 862 void(Args...)>::OnMessageReceived(const IPC::Message& message) { |
| 907 if (message.type() != ResponseMessage::ID) | 863 if (message.type() != ResponseMessage::ID) |
| 908 return false; | 864 return false; |
| 909 int received_request_id; | 865 int received_request_id; |
| 910 bool result = base::PickleIterator(message).ReadInt(&received_request_id); | 866 bool result = base::PickleIterator(message).ReadInt(&received_request_id); |
| 911 if (!result || received_request_id != request_id_) | 867 if (!result || received_request_id != request_id_) |
| 912 return false; | 868 return false; |
| 913 | 869 |
| 914 CallbackType protect(callback_); | 870 CallbackType protect(callback_); |
| 915 // Essentially same code as what IPC_MESSAGE_FORWARD expands to. | 871 // Essentially same code as what IPC_MESSAGE_FORWARD expands to. |
| 916 void* param = nullptr; | 872 void* param = nullptr; |
| 917 if (!ResponseMessage::Dispatch(&message, this, this, param, | 873 if (!ResponseMessage::Dispatch(&message, this, this, param, |
| 918 &EventResponseHandler::RunCallback)) | 874 &EventResponseHandler::RunCallback)) |
| 919 message.set_dispatch_error(); | 875 message.set_dispatch_error(); |
| 920 | 876 |
| 921 // At this point |this| can have been deleted, so don't do anything other | 877 // At this point |this| can have been deleted, so don't do anything other |
| 922 // than returning. | 878 // than returning. |
| 923 | 879 |
| 924 return true; | 880 return true; |
| 925 } | 881 } |
| 926 | 882 |
| 927 } // namespace content | 883 } // namespace content |
| 928 | 884 |
| 929 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ | 885 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ |
| OLD | NEW |