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 16 matching lines...) Expand all Loading... | |
27 #include "base/optional.h" | 27 #include "base/optional.h" |
28 #include "base/threading/thread_task_runner_handle.h" | 28 #include "base/threading/thread_task_runner_handle.h" |
29 #include "base/time/time.h" | 29 #include "base/time/time.h" |
30 #include "base/timer/timer.h" | 30 #include "base/timer/timer.h" |
31 #include "content/browser/service_worker/embedded_worker_instance.h" | 31 #include "content/browser/service_worker/embedded_worker_instance.h" |
32 #include "content/browser/service_worker/embedded_worker_status.h" | 32 #include "content/browser/service_worker/embedded_worker_status.h" |
33 #include "content/browser/service_worker/service_worker_metrics.h" | 33 #include "content/browser/service_worker/service_worker_metrics.h" |
34 #include "content/browser/service_worker/service_worker_script_cache_map.h" | 34 #include "content/browser/service_worker/service_worker_script_cache_map.h" |
35 #include "content/common/content_export.h" | 35 #include "content/common/content_export.h" |
36 #include "content/common/origin_trials/trial_token_validator.h" | 36 #include "content/common/origin_trials/trial_token_validator.h" |
37 #include "content/common/service_worker/service_worker_event_dispatcher.mojom.h" | |
37 #include "content/common/service_worker/service_worker_status_code.h" | 38 #include "content/common/service_worker/service_worker_status_code.h" |
38 #include "content/common/service_worker/service_worker_types.h" | 39 #include "content/common/service_worker/service_worker_types.h" |
39 #include "ipc/ipc_message.h" | 40 #include "ipc/ipc_message.h" |
40 #include "mojo/public/cpp/bindings/interface_ptr.h" | 41 #include "mojo/public/cpp/bindings/interface_ptr.h" |
41 #include "services/service_manager/public/cpp/interface_provider.h" | 42 #include "services/service_manager/public/cpp/interface_provider.h" |
42 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWor kerEventResult.h" | 43 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWor kerEventResult.h" |
43 #include "url/gurl.h" | 44 #include "url/gurl.h" |
44 #include "url/origin.h" | 45 #include "url/origin.h" |
45 | 46 |
46 // Windows headers will redefine SendMessage. | 47 // Windows headers will redefine SendMessage. |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
260 // TODO(mek): Use something other than a bool for event status. | 261 // TODO(mek): Use something other than a bool for event status. |
261 bool FinishRequest(int request_id, | 262 bool FinishRequest(int request_id, |
262 bool was_handled, | 263 bool was_handled, |
263 base::Time dispatch_event_time); | 264 base::Time dispatch_event_time); |
264 | 265 |
265 // Finishes an external request that was started by StartExternalRequest(). | 266 // Finishes an external request that was started by StartExternalRequest(). |
266 // Returns false if there was an error finishing the request: e.g. the request | 267 // Returns false if there was an error finishing the request: e.g. the request |
267 // was not found or the worker already terminated. | 268 // was not found or the worker already terminated. |
268 bool FinishExternalRequest(const std::string& request_uuid); | 269 bool FinishExternalRequest(const std::string& request_uuid); |
269 | 270 |
270 // Connects to a specific mojo service exposed by the (running) service | 271 // The dispatcher should be valid after the worker is started and invalid |
271 // worker. If a connection to a service for the same Interface already exists | 272 // after StopWorker or OnDetached is called. |
falken
2016/11/23 14:32:31
nit: the "should be" makes it unclear whether this
shimazu
2016/11/24 06:47:32
Thanks, that's right.
Updated.
| |
272 // this will return that existing connection. The |request_id| must be a value | 273 mojom::ServiceWorkerEventDispatcher* event_dispatcher() { |
273 // previously returned by StartRequest. If the connection to the service | 274 DCHECK(event_dispatcher_.is_bound()); |
274 // fails or closes before the request finished, the error callback associated | 275 return event_dispatcher_.get(); |
275 // with |request_id| is called. | 276 } |
276 // Only call GetMojoServiceForRequest once for a specific |request_id|. | |
277 template <typename Interface> | |
278 base::WeakPtr<Interface> GetMojoServiceForRequest(int request_id); | |
279 | 277 |
280 // Dispatches an event. If dispatching the event fails, all of the error | 278 // Dispatches an event. If dispatching the event fails, all of the error |
281 // callbacks that were associated with |request_ids| via StartRequest are | 279 // callbacks that were associated with |request_ids| via StartRequest are |
282 // called. | 280 // called. |
283 // Use RegisterRequestCallback or RegisterSimpleRequest to register a callback | 281 // Use RegisterRequestCallback or RegisterSimpleRequest to register a callback |
284 // to receive messages sent back in response to this event before calling this | 282 // to receive messages sent back in response to this event before calling this |
285 // method. | 283 // method. |
286 // This must be called when the worker is running. | 284 // This must be called when the worker is running. |
287 void DispatchEvent(const std::vector<int>& request_ids, | 285 void DispatchEvent(const std::vector<int>& request_ids, |
288 const IPC::Message& message); | 286 const IPC::Message& message); |
(...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
748 | 746 |
749 // Holds in-flight requests, including requests due to outstanding push, | 747 // Holds in-flight requests, including requests due to outstanding push, |
750 // fetch, sync, etc. events. | 748 // fetch, sync, etc. events. |
751 IDMap<PendingRequest, IDMapOwnPointer> pending_requests_; | 749 IDMap<PendingRequest, IDMapOwnPointer> pending_requests_; |
752 | 750 |
753 // Container for pending external requests for this service worker. | 751 // Container for pending external requests for this service worker. |
754 // (key, value): (request uuid, request id). | 752 // (key, value): (request uuid, request id). |
755 using RequestUUIDToRequestIDMap = std::map<std::string, int>; | 753 using RequestUUIDToRequestIDMap = std::map<std::string, int>; |
756 RequestUUIDToRequestIDMap external_request_uuid_to_request_id_; | 754 RequestUUIDToRequestIDMap external_request_uuid_to_request_id_; |
757 | 755 |
758 // Stores all open connections to mojo services. Maps the service name to | 756 // Connected to ServiceWorkerContextClient while the worker is running. |
759 // the actual interface pointer. When a connection is closed it is removed | 757 mojom::ServiceWorkerEventDispatcherPtr event_dispatcher_; |
760 // from this map. | |
761 // mojo_services_[Interface::Name_] is assumed to always contain a | |
762 // MojoServiceWrapper<Interface> instance. | |
763 base::ScopedPtrHashMap<const char*, std::unique_ptr<BaseMojoServiceWrapper>> | |
764 mojo_services_; | |
765 | 758 |
766 std::set<const ServiceWorkerURLRequestJob*> streaming_url_request_jobs_; | 759 std::set<const ServiceWorkerURLRequestJob*> streaming_url_request_jobs_; |
767 | 760 |
768 std::map<std::string, ServiceWorkerProviderHost*> controllee_map_; | 761 std::map<std::string, ServiceWorkerProviderHost*> controllee_map_; |
769 // Will be null while shutting down. | 762 // Will be null while shutting down. |
770 base::WeakPtr<ServiceWorkerContextCore> context_; | 763 base::WeakPtr<ServiceWorkerContextCore> context_; |
771 base::ObserverList<Listener> listeners_; | 764 base::ObserverList<Listener> listeners_; |
772 ServiceWorkerScriptCacheMap script_cache_map_; | 765 ServiceWorkerScriptCacheMap script_cache_map_; |
773 base::OneShotTimer update_timer_; | 766 base::OneShotTimer update_timer_; |
774 | 767 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
817 | 810 |
818 // Keeps the first purpose of starting the worker for UMA. Cleared in | 811 // Keeps the first purpose of starting the worker for UMA. Cleared in |
819 // FinishStartWorker(). | 812 // FinishStartWorker(). |
820 base::Optional<ServiceWorkerMetrics::EventType> start_worker_first_purpose_; | 813 base::Optional<ServiceWorkerMetrics::EventType> start_worker_first_purpose_; |
821 | 814 |
822 base::WeakPtrFactory<ServiceWorkerVersion> weak_factory_; | 815 base::WeakPtrFactory<ServiceWorkerVersion> weak_factory_; |
823 | 816 |
824 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersion); | 817 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersion); |
825 }; | 818 }; |
826 | 819 |
827 template <typename Interface> | |
828 base::WeakPtr<Interface> ServiceWorkerVersion::GetMojoServiceForRequest( | |
829 int request_id) { | |
830 DCHECK_EQ(EmbeddedWorkerStatus::RUNNING, running_status()); | |
831 PendingRequest* request = pending_requests_.Lookup(request_id); | |
832 DCHECK(request) << "Invalid request id"; | |
833 DCHECK(!request->mojo_service) | |
834 << "Request is already associated with a mojo service"; | |
835 | |
836 MojoServiceWrapper<Interface>* service = | |
837 static_cast<MojoServiceWrapper<Interface>*>( | |
838 mojo_services_.get(Interface::Name_)); | |
839 if (!service) { | |
840 mojo::InterfacePtr<Interface> interface_ptr; | |
841 embedded_worker_->GetRemoteInterfaces()->GetInterface(&interface_ptr); | |
842 interface_ptr.set_connection_error_handler( | |
843 base::Bind(&ServiceWorkerVersion::OnMojoConnectionError, | |
844 weak_factory_.GetWeakPtr(), Interface::Name_)); | |
845 service = new MojoServiceWrapper<Interface>(this, std::move(interface_ptr)); | |
846 mojo_services_.add(Interface::Name_, base::WrapUnique(service)); | |
847 } | |
848 request->mojo_service = Interface::Name_; | |
849 return service->GetWeakPtr(); | |
850 } | |
851 | |
852 template <typename ResponseMessage> | 820 template <typename ResponseMessage> |
853 void ServiceWorkerVersion::DispatchSimpleEvent(int request_id, | 821 void ServiceWorkerVersion::DispatchSimpleEvent(int request_id, |
854 const IPC::Message& message) { | 822 const IPC::Message& message) { |
855 RegisterSimpleRequest<ResponseMessage>(request_id); | 823 RegisterSimpleRequest<ResponseMessage>(request_id); |
856 DispatchEvent({request_id}, message); | 824 DispatchEvent({request_id}, message); |
857 } | 825 } |
858 | 826 |
859 template <typename ResponseMessage, typename ResponseCallbackType> | 827 template <typename ResponseMessage, typename ResponseCallbackType> |
860 void ServiceWorkerVersion::RegisterRequestCallback( | 828 void ServiceWorkerVersion::RegisterRequestCallback( |
861 int request_id, | 829 int request_id, |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
895 | 863 |
896 // At this point |this| can have been deleted, so don't do anything other | 864 // At this point |this| can have been deleted, so don't do anything other |
897 // than returning. | 865 // than returning. |
898 | 866 |
899 return true; | 867 return true; |
900 } | 868 } |
901 | 869 |
902 } // namespace content | 870 } // namespace content |
903 | 871 |
904 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ | 872 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ |
OLD | NEW |