1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CONTENT_RENDERER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_CLIENT_H_
6 #define CONTENT_RENDERER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_CLIENT_H_
12 #include "base/id_map.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/strings/string16.h"
16 #include "base/time/time.h"
17 #include "content/child/webmessageportchannel_impl.h"
18 #include "content/common/service_worker/service_worker_types.h"
19 #include "content/public/common/service_worker_event_status.mojom.h"
20 #include "ipc/ipc_listener.h"
21 #include "mojo/application/public/interfaces/service_provider.mojom.h"
22 #include "third_party/WebKit/public/platform/WebGeofencingEventType.h"
23 #include "third_party/WebKit/public/platform/WebMessagePortChannel.h"
24 #include "third_party/WebKit/public/platform/WebServiceWorkerError.h"
25 #include "third_party/WebKit/public/web/WebServiceWorkerContextClient.h"
28 class SingleThreadTaskRunner
;
33 struct WebCircularGeofencingRegion
;
34 struct WebCrossOriginServiceWorkerClient
;
36 struct WebServiceWorkerClientQueryOptions
;
37 class WebServiceWorkerContextProxy
;
38 class WebServiceWorkerProvider
;
39 struct WebSyncRegistration
;
48 struct NavigatorConnectClient
;
49 struct PlatformNotificationData
;
50 struct ServiceWorkerClientInfo
;
51 class ServiceWorkerProviderContext
;
52 class ServiceWorkerContextClient
;
53 class ThreadSafeSender
;
54 class WebServiceWorkerRegistrationImpl
;
56 // This class provides access to/from an ServiceWorker's WorkerGlobalScope.
57 // Unless otherwise noted, all methods are called on the worker thread.
58 class ServiceWorkerContextClient
59 : public blink::WebServiceWorkerContextClient
{
61 using SyncCallback
= mojo::Callback
<void(ServiceWorkerEventStatus
)>;
63 // Returns a thread-specific client instance. This does NOT create a
65 static ServiceWorkerContextClient
* ThreadSpecificInstance();
67 // Called on the main thread.
68 ServiceWorkerContextClient(int embedded_worker_id
,
69 int64 service_worker_version_id
,
70 const GURL
& service_worker_scope
,
71 const GURL
& script_url
,
72 int worker_devtools_agent_route_id
);
73 ~ServiceWorkerContextClient() override
;
75 void OnMessageReceived(int thread_id
,
76 int embedded_worker_id
,
77 const IPC::Message
& message
);
79 // Called some time after the worker has started. Attempts to use the
80 // ServiceRegistry to connect to services before this method is called are
81 // queued up and will resolve after this method is called.
82 void BindServiceRegistry(
83 mojo::InterfaceRequest
<mojo::ServiceProvider
> services
,
84 mojo::ServiceProviderPtr exposed_services
);
86 // WebServiceWorkerContextClient overrides.
87 virtual blink::WebURL
scope() const;
88 virtual void getClients(const blink::WebServiceWorkerClientQueryOptions
&,
89 blink::WebServiceWorkerClientsCallbacks
*);
90 virtual void openWindow(const blink::WebURL
&,
91 blink::WebServiceWorkerClientCallbacks
*);
92 virtual void setCachedMetadata(const blink::WebURL
&,
95 virtual void clearCachedMetadata(const blink::WebURL
&);
96 virtual void workerReadyForInspection();
98 // Called on the main thread.
99 virtual void workerContextFailedToStart();
101 virtual void workerContextStarted(blink::WebServiceWorkerContextProxy
* proxy
);
102 virtual void didEvaluateWorkerScript(bool success
);
103 virtual void willDestroyWorkerContext();
104 virtual void workerContextDestroyed();
105 virtual void reportException(const blink::WebString
& error_message
,
108 const blink::WebString
& source_url
);
109 virtual void reportConsoleMessage(int source
,
111 const blink::WebString
& message
,
113 const blink::WebString
& source_url
);
114 virtual void sendDevToolsMessage(int call_id
,
115 const blink::WebString
& message
,
116 const blink::WebString
& state
);
117 virtual void didHandleActivateEvent(int request_id
,
118 blink::WebServiceWorkerEventResult
);
119 virtual void didHandleInstallEvent(int request_id
,
120 blink::WebServiceWorkerEventResult result
);
121 virtual void didHandleFetchEvent(int request_id
);
122 virtual void didHandleFetchEvent(
124 const blink::WebServiceWorkerResponse
& response
);
125 virtual void didHandleNotificationClickEvent(
127 blink::WebServiceWorkerEventResult result
);
128 virtual void didHandlePushEvent(int request_id
,
129 blink::WebServiceWorkerEventResult result
);
130 virtual void didHandleSyncEvent(int request_id
,
131 blink::WebServiceWorkerEventResult result
);
133 // Called on the main thread.
134 virtual blink::WebServiceWorkerNetworkProvider
*
135 createServiceWorkerNetworkProvider(blink::WebDataSource
* data_source
);
136 virtual blink::WebServiceWorkerProvider
* createServiceWorkerProvider();
138 virtual void postMessageToClient(
139 const blink::WebString
& uuid
,
140 const blink::WebString
& message
,
141 blink::WebMessagePortChannelArray
* channels
);
142 virtual void postMessageToCrossOriginClient(
143 const blink::WebCrossOriginServiceWorkerClient
& client
,
144 const blink::WebString
& message
,
145 blink::WebMessagePortChannelArray
* channels
);
146 virtual void focus(const blink::WebString
& uuid
,
147 blink::WebServiceWorkerClientCallbacks
*);
148 virtual void skipWaiting(
149 blink::WebServiceWorkerSkipWaitingCallbacks
* callbacks
);
150 virtual void claim(blink::WebServiceWorkerClientsClaimCallbacks
* callbacks
);
152 virtual void DispatchSyncEvent(const blink::WebSyncRegistration
& registration
,
153 const SyncCallback
& callback
);
156 struct WorkerContextData
;
158 // Get routing_id for sending message to the ServiceWorkerVersion
159 // in the browser process.
160 int GetRoutingID() const { return embedded_worker_id_
; }
162 void Send(IPC::Message
* message
);
163 void SendWorkerStarted();
164 void SetRegistrationInServiceWorkerGlobalScope();
166 void OnActivateEvent(int request_id
);
167 void OnInstallEvent(int request_id
);
168 void OnFetchEvent(int request_id
, const ServiceWorkerFetchRequest
& request
);
169 void OnNotificationClickEvent(
171 int64_t persistent_notification_id
,
172 const PlatformNotificationData
& notification_data
);
173 void OnPushEvent(int request_id
, const std::string
& data
);
174 void OnGeofencingEvent(int request_id
,
175 blink::WebGeofencingEventType event_type
,
176 const std::string
& region_id
,
177 const blink::WebCircularGeofencingRegion
& region
);
179 const base::string16
& message
,
180 const std::vector
<TransferredMessagePort
>& sent_message_ports
,
181 const std::vector
<int>& new_routing_ids
);
182 void OnCrossOriginMessageToWorker(
183 const NavigatorConnectClient
& client
,
184 const base::string16
& message
,
185 const std::vector
<TransferredMessagePort
>& sent_message_ports
,
186 const std::vector
<int>& new_routing_ids
);
187 void OnDidGetClients(
188 int request_id
, const std::vector
<ServiceWorkerClientInfo
>& clients
);
189 void OnOpenWindowResponse(int request_id
,
190 const ServiceWorkerClientInfo
& client
);
191 void OnOpenWindowError(int request_id
, const std::string
& message
);
192 void OnFocusClientResponse(int request_id
,
193 const ServiceWorkerClientInfo
& client
);
194 void OnDidSkipWaiting(int request_id
);
195 void OnDidClaimClients(int request_id
);
196 void OnClaimClientsError(int request_id
,
197 blink::WebServiceWorkerError::ErrorType error_type
,
198 const base::string16
& message
);
201 base::WeakPtr
<ServiceWorkerContextClient
> GetWeakPtr();
203 const int embedded_worker_id_
;
204 const int64 service_worker_version_id_
;
205 const GURL service_worker_scope_
;
206 const GURL script_url_
;
207 const int worker_devtools_agent_route_id_
;
208 scoped_refptr
<ThreadSafeSender
> sender_
;
209 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner_
;
210 scoped_refptr
<base::TaskRunner
> worker_task_runner_
;
212 scoped_refptr
<ServiceWorkerProviderContext
> provider_context_
;
214 // Not owned; this object is destroyed when proxy_ becomes invalid.
215 blink::WebServiceWorkerContextProxy
* proxy_
;
217 // Used for incoming messages from the browser for which an outgoing response
218 // back to the browser is expected, the id must be sent back with the
220 int current_request_id_
;
222 // Initialized on the worker thread in workerContextStarted and
223 // destructed on the worker thread in willDestroyWorkerContext.
224 scoped_ptr
<WorkerContextData
> context_
;
226 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerContextClient
);
229 } // namespace content
231 #endif // CONTENT_RENDERER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_CLIENT_H_