Roll src/third_party/WebKit 9d2dfea:3aea697 (svn 201972:201973)
[chromium-blink-merge.git] / content / renderer / service_worker / service_worker_context_client.h
blob339d3b0120dfce665558f63fe07ef768f20dc221
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_
8 #include <map>
9 #include <string>
10 #include <vector>
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"
28 namespace base {
29 class SingleThreadTaskRunner;
30 class TaskRunner;
33 namespace blink {
34 struct WebCircularGeofencingRegion;
35 struct WebCrossOriginServiceWorkerClient;
36 class WebDataSource;
37 struct WebServiceWorkerClientQueryOptions;
38 class WebServiceWorkerContextProxy;
39 class WebServiceWorkerProvider;
40 struct WebSyncRegistration;
43 namespace IPC {
44 class Message;
47 namespace content {
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 {
61 public:
62 using SyncCallback = mojo::Callback<void(ServiceWorkerEventStatus)>;
64 // Returns a thread-specific client instance. This does NOT create a
65 // new instance.
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&,
94 const char* data,
95 size_t size);
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,
109 int line_number,
110 int column_number,
111 const blink::WebString& source_url);
112 virtual void reportConsoleMessage(int source,
113 int level,
114 const blink::WebString& message,
115 int line_number,
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(
126 int request_id,
127 const blink::WebServiceWorkerResponse& response);
128 virtual void didHandleNotificationClickEvent(
129 int request_id,
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);
161 private:
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(
176 int request_id,
177 int64_t persistent_notification_id,
178 const PlatformNotificationData& notification_data,
179 int action_index);
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);
185 void OnPostMessage(
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);
209 void OnPing();
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
229 // response.
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_