| 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 <map> | 10 #include <map> |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 // associated with the |request_id| is called. Any messages sent back in | 219 // associated with the |request_id| is called. Any messages sent back in |
| 220 // response to this event are passed on to the response |callback|. | 220 // response to this event are passed on to the response |callback|. |
| 221 // ResponseMessage is the type of the IPC message that is used for the | 221 // ResponseMessage is the type of the IPC message that is used for the |
| 222 // response, and its first argument MUST be the request_id. | 222 // response, and its first argument MUST be the request_id. |
| 223 // This must be called when the worker is running. | 223 // This must be called when the worker is running. |
| 224 template <typename ResponseMessage, typename ResponseCallbackType> | 224 template <typename ResponseMessage, typename ResponseCallbackType> |
| 225 void DispatchEvent(int request_id, | 225 void DispatchEvent(int request_id, |
| 226 const IPC::Message& message, | 226 const IPC::Message& message, |
| 227 const ResponseCallbackType& callback); | 227 const ResponseCallbackType& callback); |
| 228 | 228 |
| 229 // Combines StartRequest and DispatchEvent, and automatically calls |
| 230 // FinishRequest before passing a response to the response callback. |
| 231 // ResponseMessage must have a int request_id and |
| 232 // blink::WebServiceWorkerEventResult. |
| 233 template <typename EventMessage, |
| 234 typename ResponseMessage, |
| 235 typename... EventArgs> |
| 236 int DispatchSimpleEvent(ServiceWorkerMetrics::EventType event_type, |
| 237 const StatusCallback& callback, |
| 238 EventArgs... event_args); |
| 239 |
| 229 // Sends a message event to the associated embedded worker. | 240 // Sends a message event to the associated embedded worker. |
| 230 void DispatchMessageEvent( | 241 void DispatchMessageEvent( |
| 231 const base::string16& message, | 242 const base::string16& message, |
| 232 const std::vector<TransferredMessagePort>& sent_message_ports, | 243 const std::vector<TransferredMessagePort>& sent_message_ports, |
| 233 const StatusCallback& callback); | 244 const StatusCallback& callback); |
| 234 | 245 |
| 235 // Sends install event to the associated embedded worker and asynchronously | 246 // Sends install event to the associated embedded worker and asynchronously |
| 236 // calls |callback| when it errors out or it gets a response from the worker | 247 // calls |callback| when it errors out or it gets a response from the worker |
| 237 // to notify install completion. | 248 // to notify install completion. |
| 238 // | 249 // |
| (...skipping 26 matching lines...) Expand all Loading... |
| 265 // asynchronously calls |callback| when it errors out or it gets a response | 276 // asynchronously calls |callback| when it errors out or it gets a response |
| 266 // from the worker to notify completion. | 277 // from the worker to notify completion. |
| 267 // | 278 // |
| 268 // This must be called when the status() is ACTIVATED. | 279 // This must be called when the status() is ACTIVATED. |
| 269 void DispatchNotificationClickEvent( | 280 void DispatchNotificationClickEvent( |
| 270 const StatusCallback& callback, | 281 const StatusCallback& callback, |
| 271 int64_t persistent_notification_id, | 282 int64_t persistent_notification_id, |
| 272 const PlatformNotificationData& notification_data, | 283 const PlatformNotificationData& notification_data, |
| 273 int action_index); | 284 int action_index); |
| 274 | 285 |
| 275 // Sends push event to the associated embedded worker and asynchronously calls | |
| 276 // |callback| when it errors out or it gets a response from the worker to | |
| 277 // notify completion. | |
| 278 // | |
| 279 // This must be called when the status() is ACTIVATED. | |
| 280 void DispatchPushEvent(const StatusCallback& callback, | |
| 281 const std::string& data); | |
| 282 | |
| 283 // Sends a cross origin message event to the associated embedded worker and | 286 // Sends a cross origin message event to the associated embedded worker and |
| 284 // asynchronously calls |callback| when the message was sent (or failed to | 287 // asynchronously calls |callback| when the message was sent (or failed to |
| 285 // sent). | 288 // sent). |
| 286 // It is the responsibility of the code calling this method to make sure that | 289 // It is the responsibility of the code calling this method to make sure that |
| 287 // any transferred message ports are put on hold while potentially a process | 290 // any transferred message ports are put on hold while potentially a process |
| 288 // for the service worker is spun up. | 291 // for the service worker is spun up. |
| 289 // | 292 // |
| 290 // This must be called when the status() is ACTIVATED. | 293 // This must be called when the status() is ACTIVATED. |
| 291 void DispatchCrossOriginMessageEvent( | 294 void DispatchCrossOriginMessageEvent( |
| 292 const NavigatorConnectClient& client, | 295 const NavigatorConnectClient& client, |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 399 MixedRequestTimeouts); | 402 MixedRequestTimeouts); |
| 400 | 403 |
| 401 class Metrics; | 404 class Metrics; |
| 402 class PingController; | 405 class PingController; |
| 403 | 406 |
| 404 enum RequestType { | 407 enum RequestType { |
| 405 REQUEST_ACTIVATE, | 408 REQUEST_ACTIVATE, |
| 406 REQUEST_INSTALL, | 409 REQUEST_INSTALL, |
| 407 REQUEST_FETCH, | 410 REQUEST_FETCH, |
| 408 REQUEST_NOTIFICATION_CLICK, | 411 REQUEST_NOTIFICATION_CLICK, |
| 409 REQUEST_PUSH, | |
| 410 REQUEST_CUSTOM, | 412 REQUEST_CUSTOM, |
| 411 NUM_REQUEST_TYPES | 413 NUM_REQUEST_TYPES |
| 412 }; | 414 }; |
| 413 | 415 |
| 414 struct RequestInfo { | 416 struct RequestInfo { |
| 415 RequestInfo(int id, | 417 RequestInfo(int id, |
| 416 RequestType type, | 418 RequestType type, |
| 417 ServiceWorkerMetrics::EventType event_type, | 419 ServiceWorkerMetrics::EventType event_type, |
| 418 const base::TimeTicks& expiration, | 420 const base::TimeTicks& expiration, |
| 419 TimeoutBehavior timeout_behavior); | 421 TimeoutBehavior timeout_behavior); |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 561 const ServiceWorkerClientQueryOptions& options); | 563 const ServiceWorkerClientQueryOptions& options); |
| 562 | 564 |
| 563 void OnActivateEventFinished(int request_id, | 565 void OnActivateEventFinished(int request_id, |
| 564 blink::WebServiceWorkerEventResult result); | 566 blink::WebServiceWorkerEventResult result); |
| 565 void OnInstallEventFinished(int request_id, | 567 void OnInstallEventFinished(int request_id, |
| 566 blink::WebServiceWorkerEventResult result); | 568 blink::WebServiceWorkerEventResult result); |
| 567 void OnFetchEventFinished(int request_id, | 569 void OnFetchEventFinished(int request_id, |
| 568 ServiceWorkerFetchEventResult result, | 570 ServiceWorkerFetchEventResult result, |
| 569 const ServiceWorkerResponse& response); | 571 const ServiceWorkerResponse& response); |
| 570 void OnNotificationClickEventFinished(int request_id); | 572 void OnNotificationClickEventFinished(int request_id); |
| 571 void OnPushEventFinished(int request_id, | |
| 572 blink::WebServiceWorkerEventResult result); | |
| 573 void OnOpenWindow(int request_id, GURL url); | 573 void OnOpenWindow(int request_id, GURL url); |
| 574 void OnOpenWindowFinished(int request_id, | 574 void OnOpenWindowFinished(int request_id, |
| 575 ServiceWorkerStatusCode status, | 575 ServiceWorkerStatusCode status, |
| 576 const std::string& client_uuid, | 576 const std::string& client_uuid, |
| 577 const ServiceWorkerClientInfo& client_info); | 577 const ServiceWorkerClientInfo& client_info); |
| 578 | 578 |
| 579 void OnSetCachedMetadata(const GURL& url, const std::vector<char>& data); | 579 void OnSetCachedMetadata(const GURL& url, const std::vector<char>& data); |
| 580 void OnSetCachedMetadataFinished(int64_t callback_id, int result); | 580 void OnSetCachedMetadataFinished(int64_t callback_id, int result); |
| 581 void OnClearCachedMetadata(const GURL& url); | 581 void OnClearCachedMetadata(const GURL& url); |
| 582 void OnClearCachedMetadataFinished(int64_t callback_id, int result); | 582 void OnClearCachedMetadataFinished(int64_t callback_id, int result); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 672 void OnStoppedInternal(EmbeddedWorkerInstance::Status old_status); | 672 void OnStoppedInternal(EmbeddedWorkerInstance::Status old_status); |
| 673 | 673 |
| 674 // Called when the remote side of a connection to a mojo service is lost. | 674 // Called when the remote side of a connection to a mojo service is lost. |
| 675 void OnMojoConnectionError(const char* service_name); | 675 void OnMojoConnectionError(const char* service_name); |
| 676 | 676 |
| 677 // Called at the beginning of each Dispatch*Event function: records | 677 // Called at the beginning of each Dispatch*Event function: records |
| 678 // the time elapsed since idle (generally the time since the previous | 678 // the time elapsed since idle (generally the time since the previous |
| 679 // event ended). | 679 // event ended). |
| 680 void OnBeginEvent(); | 680 void OnBeginEvent(); |
| 681 | 681 |
| 682 void ResponseCallbackWrapper(const StatusCallback& callback, |
| 683 int request_id, |
| 684 blink::WebServiceWorkerEventResult result) { |
| 685 // TODO(mek): trace? |
| 686 if (!FinishRequest(request_id)) |
| 687 NOTREACHED() << "Should only receive one reply per event"; |
| 688 |
| 689 ServiceWorkerStatusCode status = SERVICE_WORKER_OK; |
| 690 if (result == blink::WebServiceWorkerEventResultRejected) |
| 691 status = SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED; |
| 692 callback.Run(status); |
| 693 } |
| 694 |
| 682 const int64_t version_id_; | 695 const int64_t version_id_; |
| 683 const int64_t registration_id_; | 696 const int64_t registration_id_; |
| 684 const GURL script_url_; | 697 const GURL script_url_; |
| 685 const GURL scope_; | 698 const GURL scope_; |
| 686 std::vector<GURL> foreign_fetch_scopes_; | 699 std::vector<GURL> foreign_fetch_scopes_; |
| 687 | 700 |
| 688 Status status_ = NEW; | 701 Status status_ = NEW; |
| 689 scoped_ptr<EmbeddedWorkerInstance> embedded_worker_; | 702 scoped_ptr<EmbeddedWorkerInstance> embedded_worker_; |
| 690 std::vector<StatusCallback> start_callbacks_; | 703 std::vector<StatusCallback> start_callbacks_; |
| 691 std::vector<StatusCallback> stop_callbacks_; | 704 std::vector<StatusCallback> stop_callbacks_; |
| 692 std::vector<base::Closure> status_change_callbacks_; | 705 std::vector<base::Closure> status_change_callbacks_; |
| 693 | 706 |
| 694 // Message callbacks. (Update HasInflightRequests() too when you update this | 707 // Message callbacks. (Update HasInflightRequests() too when you update this |
| 695 // list.) | 708 // list.) |
| 696 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> activate_requests_; | 709 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> activate_requests_; |
| 697 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> install_requests_; | 710 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> install_requests_; |
| 698 IDMap<PendingRequest<FetchCallback>, IDMapOwnPointer> fetch_requests_; | 711 IDMap<PendingRequest<FetchCallback>, IDMapOwnPointer> fetch_requests_; |
| 699 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> | 712 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> |
| 700 notification_click_requests_; | 713 notification_click_requests_; |
| 701 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> push_requests_; | |
| 702 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> custom_requests_; | 714 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> custom_requests_; |
| 703 | 715 |
| 704 // Stores all open connections to mojo services. Maps the service name to | 716 // Stores all open connections to mojo services. Maps the service name to |
| 705 // the actual interface pointer. When a connection is closed it is removed | 717 // the actual interface pointer. When a connection is closed it is removed |
| 706 // from this map. | 718 // from this map. |
| 707 // mojo_services_[Interface::Name_] is assumed to always contain a | 719 // mojo_services_[Interface::Name_] is assumed to always contain a |
| 708 // MojoServiceWrapper<Interface> instance. | 720 // MojoServiceWrapper<Interface> instance. |
| 709 base::ScopedPtrHashMap<const char*, scoped_ptr<BaseMojoServiceWrapper>> | 721 base::ScopedPtrHashMap<const char*, scoped_ptr<BaseMojoServiceWrapper>> |
| 710 mojo_services_; | 722 mojo_services_; |
| 711 | 723 |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 803 base::ThreadTaskRunnerHandle::Get()->PostTask( | 815 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 804 FROM_HERE, base::Bind(request->callback, status)); | 816 FROM_HERE, base::Bind(request->callback, status)); |
| 805 custom_requests_.Remove(request_id); | 817 custom_requests_.Remove(request_id); |
| 806 } else { | 818 } else { |
| 807 request->listener.reset( | 819 request->listener.reset( |
| 808 new EventResponseHandler<ResponseMessage, ResponseCallbackType>( | 820 new EventResponseHandler<ResponseMessage, ResponseCallbackType>( |
| 809 embedded_worker(), request_id, callback)); | 821 embedded_worker(), request_id, callback)); |
| 810 } | 822 } |
| 811 } | 823 } |
| 812 | 824 |
| 825 template <typename EventMessage, |
| 826 typename ResponseMessage, |
| 827 typename... EventArgs> |
| 828 int ServiceWorkerVersion::DispatchSimpleEvent( |
| 829 ServiceWorkerMetrics::EventType event_type, |
| 830 const StatusCallback& callback, |
| 831 EventArgs... event_args) { |
| 832 int request_id = StartRequest(event_type, callback); |
| 833 DispatchEvent<ResponseMessage>( |
| 834 request_id, EventMessage(request_id, event_args...), |
| 835 base::Bind(&ServiceWorkerVersion::ResponseCallbackWrapper, this, |
| 836 callback)); |
| 837 return request_id; |
| 838 } |
| 839 |
| 813 template <typename ResponseMessage, typename CallbackType> | 840 template <typename ResponseMessage, typename CallbackType> |
| 814 bool ServiceWorkerVersion::EventResponseHandler<ResponseMessage, CallbackType>:: | 841 bool ServiceWorkerVersion::EventResponseHandler<ResponseMessage, CallbackType>:: |
| 815 OnMessageReceived(const IPC::Message& message) { | 842 OnMessageReceived(const IPC::Message& message) { |
| 816 if (message.type() != ResponseMessage::ID) | 843 if (message.type() != ResponseMessage::ID) |
| 817 return false; | 844 return false; |
| 818 int received_request_id; | 845 int received_request_id; |
| 819 bool result = base::PickleIterator(message).ReadInt(&received_request_id); | 846 bool result = base::PickleIterator(message).ReadInt(&received_request_id); |
| 820 if (!result || received_request_id != request_id_) | 847 if (!result || received_request_id != request_id_) |
| 821 return false; | 848 return false; |
| 822 | 849 |
| 850 CallbackType protect(callback_); |
| 823 // Essentially same code as what IPC_MESSAGE_FORWARD expands to. | 851 // Essentially same code as what IPC_MESSAGE_FORWARD expands to. |
| 824 void* param = nullptr; | 852 void* param = nullptr; |
| 825 if (!ResponseMessage::Dispatch(&message, &callback_, this, param, | 853 if (!ResponseMessage::Dispatch(&message, &callback_, this, param, |
| 826 &CallbackType::Run)) | 854 &CallbackType::Run)) |
| 827 message.set_dispatch_error(); | 855 message.set_dispatch_error(); |
| 828 | 856 |
| 829 return true; | 857 return true; |
| 830 } | 858 } |
| 831 | 859 |
| 832 } // namespace content | 860 } // namespace content |
| 833 | 861 |
| 834 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ | 862 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ |
| OLD | NEW |