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/modules/serviceworker/WebServiceWorkerError.h"
25 #include "third_party/WebKit/public/web/modules/serviceworker/WebServiceWorkerContextClient.h"
26 #include "v8/include/v8.h"
29 class SingleThreadTaskRunner
;
34 struct WebCircularGeofencingRegion
;
35 struct WebCrossOriginServiceWorkerClient
;
37 struct WebServiceWorkerClientQueryOptions
;
38 class WebServiceWorkerContextProxy
;
39 class WebServiceWorkerProvider
;
40 struct WebSyncRegistration
;
49 struct NavigatorConnectClient
;
50 struct PlatformNotificationData
;
51 struct ServiceWorkerClientInfo
;
52 class ServiceWorkerProviderContext
;
53 class ServiceWorkerContextClient
;
54 class ThreadSafeSender
;
55 class WebServiceWorkerRegistrationImpl
;
57 // This class provides access to/from an ServiceWorker's WorkerGlobalScope.
58 // Unless otherwise noted, all methods are called on the worker thread.
59 class ServiceWorkerContextClient
60 : public blink::WebServiceWorkerContextClient
{
62 using SyncCallback
= mojo::Callback
<void(ServiceWorkerEventStatus
)>;
64 // Returns a thread-specific client instance. This does NOT create a
66 static ServiceWorkerContextClient
* ThreadSpecificInstance();
68 // Called on the main thread.
69 ServiceWorkerContextClient(int embedded_worker_id
,
70 int64 service_worker_version_id
,
71 const GURL
& service_worker_scope
,
72 const GURL
& script_url
,
73 int worker_devtools_agent_route_id
);
74 ~ServiceWorkerContextClient() override
;
76 void OnMessageReceived(int thread_id
,
77 int embedded_worker_id
,
78 const IPC::Message
& message
);
80 // Called some time after the worker has started. Attempts to use the
81 // ServiceRegistry to connect to services before this method is called are
82 // queued up and will resolve after this method is called.
83 void BindServiceRegistry(
84 mojo::InterfaceRequest
<mojo::ServiceProvider
> services
,
85 mojo::ServiceProviderPtr exposed_services
);
87 // WebServiceWorkerContextClient overrides.
88 virtual blink::WebURL
scope() const;
89 virtual void getClients(const blink::WebServiceWorkerClientQueryOptions
&,
90 blink::WebServiceWorkerClientsCallbacks
*);
91 virtual void openWindow(const blink::WebURL
&,
92 blink::WebServiceWorkerClientCallbacks
*);
93 virtual void setCachedMetadata(const blink::WebURL
&,
96 virtual void clearCachedMetadata(const blink::WebURL
&);
97 virtual void workerReadyForInspection();
99 // Called on the main thread.
100 virtual void workerContextFailedToStart();
102 virtual void workerContextStarted(blink::WebServiceWorkerContextProxy
* proxy
);
103 virtual void didEvaluateWorkerScript(bool success
);
104 virtual void didInitializeWorkerContext(v8::Local
<v8::Context
> context
,
105 const blink::WebURL
& url
);
106 virtual void willDestroyWorkerContext(v8::Local
<v8::Context
> context
);
107 virtual void workerContextDestroyed();
108 virtual void reportException(const blink::WebString
& error_message
,
111 const blink::WebString
& source_url
);
112 virtual void reportConsoleMessage(int source
,
114 const blink::WebString
& message
,
116 const blink::WebString
& source_url
);
117 virtual void sendDevToolsMessage(int call_id
,
118 const blink::WebString
& message
,
119 const blink::WebString
& state
);
120 virtual void didHandleActivateEvent(int request_id
,
121 blink::WebServiceWorkerEventResult
);
122 virtual void didHandleInstallEvent(int request_id
,
123 blink::WebServiceWorkerEventResult result
);
124 virtual void didHandleFetchEvent(int request_id
);
125 virtual void didHandleFetchEvent(
127 const blink::WebServiceWorkerResponse
& response
);
128 virtual void didHandleNotificationClickEvent(
130 blink::WebServiceWorkerEventResult result
);
131 virtual void didHandlePushEvent(int request_id
,
132 blink::WebServiceWorkerEventResult result
);
133 virtual void didHandleSyncEvent(int request_id
,
134 blink::WebServiceWorkerEventResult result
);
136 // Called on the main thread.
137 virtual blink::WebServiceWorkerNetworkProvider
*
138 createServiceWorkerNetworkProvider(blink::WebDataSource
* data_source
);
139 virtual blink::WebServiceWorkerProvider
* createServiceWorkerProvider();
141 virtual void postMessageToClient(
142 const blink::WebString
& uuid
,
143 const blink::WebString
& message
,
144 blink::WebMessagePortChannelArray
* channels
);
145 virtual void postMessageToCrossOriginClient(
146 const blink::WebCrossOriginServiceWorkerClient
& client
,
147 const blink::WebString
& message
,
148 blink::WebMessagePortChannelArray
* channels
);
149 virtual void focus(const blink::WebString
& uuid
,
150 blink::WebServiceWorkerClientCallbacks
*);
151 virtual void navigate(const blink::WebString
& uuid
,
152 const blink::WebURL
&,
153 blink::WebServiceWorkerClientCallbacks
*);
154 virtual void skipWaiting(
155 blink::WebServiceWorkerSkipWaitingCallbacks
* callbacks
);
156 virtual void claim(blink::WebServiceWorkerClientsClaimCallbacks
* callbacks
);
158 virtual void DispatchSyncEvent(const blink::WebSyncRegistration
& registration
,
159 const SyncCallback
& callback
);
162 struct WorkerContextData
;
164 // Get routing_id for sending message to the ServiceWorkerVersion
165 // in the browser process.
166 int GetRoutingID() const { return embedded_worker_id_
; }
168 void Send(IPC::Message
* message
);
169 void SendWorkerStarted();
170 void SetRegistrationInServiceWorkerGlobalScope();
172 void OnActivateEvent(int request_id
);
173 void OnInstallEvent(int request_id
);
174 void OnFetchEvent(int request_id
, const ServiceWorkerFetchRequest
& request
);
175 void OnNotificationClickEvent(
177 int64_t persistent_notification_id
,
178 const PlatformNotificationData
& notification_data
,
180 void OnPushEvent(int request_id
, const std::string
& data
);
181 void OnGeofencingEvent(int request_id
,
182 blink::WebGeofencingEventType event_type
,
183 const std::string
& region_id
,
184 const blink::WebCircularGeofencingRegion
& region
);
186 const base::string16
& message
,
187 const std::vector
<TransferredMessagePort
>& sent_message_ports
,
188 const std::vector
<int>& new_routing_ids
);
189 void OnCrossOriginMessageToWorker(
190 const NavigatorConnectClient
& client
,
191 const base::string16
& message
,
192 const std::vector
<TransferredMessagePort
>& sent_message_ports
,
193 const std::vector
<int>& new_routing_ids
);
194 void OnDidGetClients(
195 int request_id
, const std::vector
<ServiceWorkerClientInfo
>& clients
);
196 void OnOpenWindowResponse(int request_id
,
197 const ServiceWorkerClientInfo
& client
);
198 void OnOpenWindowError(int request_id
, const std::string
& message
);
199 void OnFocusClientResponse(int request_id
,
200 const ServiceWorkerClientInfo
& client
);
201 void OnNavigateClientResponse(int request_id
,
202 const ServiceWorkerClientInfo
& client
);
203 void OnNavigateClientError(int request_id
, const GURL
& url
);
204 void OnDidSkipWaiting(int request_id
);
205 void OnDidClaimClients(int request_id
);
206 void OnClaimClientsError(int request_id
,
207 blink::WebServiceWorkerError::ErrorType error_type
,
208 const base::string16
& message
);
211 base::WeakPtr
<ServiceWorkerContextClient
> GetWeakPtr();
213 const int embedded_worker_id_
;
214 const int64 service_worker_version_id_
;
215 const GURL service_worker_scope_
;
216 const GURL script_url_
;
217 const int worker_devtools_agent_route_id_
;
218 scoped_refptr
<ThreadSafeSender
> sender_
;
219 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner_
;
220 scoped_refptr
<base::TaskRunner
> worker_task_runner_
;
222 scoped_refptr
<ServiceWorkerProviderContext
> provider_context_
;
224 // Not owned; this object is destroyed when proxy_ becomes invalid.
225 blink::WebServiceWorkerContextProxy
* proxy_
;
227 // Used for incoming messages from the browser for which an outgoing response
228 // back to the browser is expected, the id must be sent back with the
230 int current_request_id_
;
232 // Initialized on the worker thread in workerContextStarted and
233 // destructed on the worker thread in willDestroyWorkerContext.
234 scoped_ptr
<WorkerContextData
> context_
;
236 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerContextClient
);
239 } // namespace content
241 #endif // CONTENT_RENDERER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_CLIENT_H_