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();
101 virtual void workerScriptLoaded();
103 virtual void workerContextStarted(blink::WebServiceWorkerContextProxy
* proxy
);
104 virtual void didEvaluateWorkerScript(bool success
);
105 virtual void didInitializeWorkerContext(v8::Local
<v8::Context
> context
,
106 const blink::WebURL
& url
);
107 virtual void willDestroyWorkerContext(v8::Local
<v8::Context
> context
);
108 virtual void workerContextDestroyed();
109 virtual void reportException(const blink::WebString
& error_message
,
112 const blink::WebString
& source_url
);
113 virtual void reportConsoleMessage(int source
,
115 const blink::WebString
& message
,
117 const blink::WebString
& source_url
);
118 virtual void sendDevToolsMessage(int call_id
,
119 const blink::WebString
& message
,
120 const blink::WebString
& state
);
121 virtual void didHandleActivateEvent(int request_id
,
122 blink::WebServiceWorkerEventResult
);
123 virtual void didHandleInstallEvent(int request_id
,
124 blink::WebServiceWorkerEventResult result
);
125 virtual void didHandleFetchEvent(int request_id
);
126 virtual void didHandleFetchEvent(
128 const blink::WebServiceWorkerResponse
& response
);
129 virtual void didHandleNotificationClickEvent(
131 blink::WebServiceWorkerEventResult result
);
132 virtual void didHandlePushEvent(int request_id
,
133 blink::WebServiceWorkerEventResult result
);
134 virtual void didHandleSyncEvent(int request_id
,
135 blink::WebServiceWorkerEventResult result
);
137 // Called on the main thread.
138 virtual blink::WebServiceWorkerNetworkProvider
*
139 createServiceWorkerNetworkProvider(blink::WebDataSource
* data_source
);
140 virtual blink::WebServiceWorkerProvider
* createServiceWorkerProvider();
142 virtual void postMessageToClient(
143 const blink::WebString
& uuid
,
144 const blink::WebString
& message
,
145 blink::WebMessagePortChannelArray
* channels
);
146 virtual void postMessageToCrossOriginClient(
147 const blink::WebCrossOriginServiceWorkerClient
& client
,
148 const blink::WebString
& message
,
149 blink::WebMessagePortChannelArray
* channels
);
150 virtual void focus(const blink::WebString
& uuid
,
151 blink::WebServiceWorkerClientCallbacks
*);
152 virtual void navigate(const blink::WebString
& uuid
,
153 const blink::WebURL
&,
154 blink::WebServiceWorkerClientCallbacks
*);
155 virtual void skipWaiting(
156 blink::WebServiceWorkerSkipWaitingCallbacks
* callbacks
);
157 virtual void claim(blink::WebServiceWorkerClientsClaimCallbacks
* callbacks
);
159 virtual void DispatchSyncEvent(const blink::WebSyncRegistration
& registration
,
160 const SyncCallback
& callback
);
163 struct WorkerContextData
;
165 // Get routing_id for sending message to the ServiceWorkerVersion
166 // in the browser process.
167 int GetRoutingID() const { return embedded_worker_id_
; }
169 void Send(IPC::Message
* message
);
170 void SendWorkerStarted();
171 void SetRegistrationInServiceWorkerGlobalScope();
173 void OnActivateEvent(int request_id
);
174 void OnInstallEvent(int request_id
);
175 void OnFetchEvent(int request_id
, const ServiceWorkerFetchRequest
& request
);
176 void OnNotificationClickEvent(
178 int64_t persistent_notification_id
,
179 const PlatformNotificationData
& notification_data
,
181 void OnPushEvent(int request_id
, const std::string
& data
);
182 void OnGeofencingEvent(int request_id
,
183 blink::WebGeofencingEventType event_type
,
184 const std::string
& region_id
,
185 const blink::WebCircularGeofencingRegion
& region
);
187 const base::string16
& message
,
188 const std::vector
<TransferredMessagePort
>& sent_message_ports
,
189 const std::vector
<int>& new_routing_ids
);
190 void OnCrossOriginMessageToWorker(
191 const NavigatorConnectClient
& client
,
192 const base::string16
& message
,
193 const std::vector
<TransferredMessagePort
>& sent_message_ports
,
194 const std::vector
<int>& new_routing_ids
);
195 void OnDidGetClients(
196 int request_id
, const std::vector
<ServiceWorkerClientInfo
>& clients
);
197 void OnOpenWindowResponse(int request_id
,
198 const ServiceWorkerClientInfo
& client
);
199 void OnOpenWindowError(int request_id
, const std::string
& message
);
200 void OnFocusClientResponse(int request_id
,
201 const ServiceWorkerClientInfo
& client
);
202 void OnNavigateClientResponse(int request_id
,
203 const ServiceWorkerClientInfo
& client
);
204 void OnNavigateClientError(int request_id
, const GURL
& url
);
205 void OnDidSkipWaiting(int request_id
);
206 void OnDidClaimClients(int request_id
);
207 void OnClaimClientsError(int request_id
,
208 blink::WebServiceWorkerError::ErrorType error_type
,
209 const base::string16
& message
);
212 base::WeakPtr
<ServiceWorkerContextClient
> GetWeakPtr();
214 const int embedded_worker_id_
;
215 const int64 service_worker_version_id_
;
216 const GURL service_worker_scope_
;
217 const GURL script_url_
;
218 const int worker_devtools_agent_route_id_
;
219 scoped_refptr
<ThreadSafeSender
> sender_
;
220 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner_
;
221 scoped_refptr
<base::TaskRunner
> worker_task_runner_
;
223 scoped_refptr
<ServiceWorkerProviderContext
> provider_context_
;
225 // Not owned; this object is destroyed when proxy_ becomes invalid.
226 blink::WebServiceWorkerContextProxy
* proxy_
;
228 // Used for incoming messages from the browser for which an outgoing response
229 // back to the browser is expected, the id must be sent back with the
231 int current_request_id_
;
233 // Initialized on the worker thread in workerContextStarted and
234 // destructed on the worker thread in willDestroyWorkerContext.
235 scoped_ptr
<WorkerContextData
> context_
;
237 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerContextClient
);
240 } // namespace content
242 #endif // CONTENT_RENDERER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_CLIENT_H_