Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / content / renderer / service_worker / service_worker_context_client.h
blob2f2c61f425ba17376b80af5fa6286a4b68ef8756
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/WebServiceWorkerError.h"
25 #include "third_party/WebKit/public/web/WebServiceWorkerContextClient.h"
27 namespace base {
28 class SingleThreadTaskRunner;
29 class TaskRunner;
32 namespace blink {
33 struct WebCircularGeofencingRegion;
34 struct WebCrossOriginServiceWorkerClient;
35 class WebDataSource;
36 struct WebServiceWorkerClientQueryOptions;
37 class WebServiceWorkerContextProxy;
38 class WebServiceWorkerProvider;
39 struct WebSyncRegistration;
42 namespace IPC {
43 class Message;
46 namespace content {
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 {
60 public:
61 using SyncCallback = mojo::Callback<void(ServiceWorkerEventStatus)>;
63 // Returns a thread-specific client instance. This does NOT create a
64 // new instance.
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&,
93 const char* data,
94 size_t size);
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,
106 int line_number,
107 int column_number,
108 const blink::WebString& source_url);
109 virtual void reportConsoleMessage(int source,
110 int level,
111 const blink::WebString& message,
112 int line_number,
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(
123 int request_id,
124 const blink::WebServiceWorkerResponse& response);
125 virtual void didHandleNotificationClickEvent(
126 int request_id,
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 navigate(const blink::WebString& uuid,
149 const blink::WebURL&,
150 blink::WebServiceWorkerClientCallbacks*);
151 virtual void skipWaiting(
152 blink::WebServiceWorkerSkipWaitingCallbacks* callbacks);
153 virtual void claim(blink::WebServiceWorkerClientsClaimCallbacks* callbacks);
155 virtual void DispatchSyncEvent(const blink::WebSyncRegistration& registration,
156 const SyncCallback& callback);
158 private:
159 struct WorkerContextData;
161 // Get routing_id for sending message to the ServiceWorkerVersion
162 // in the browser process.
163 int GetRoutingID() const { return embedded_worker_id_; }
165 void Send(IPC::Message* message);
166 void SendWorkerStarted();
167 void SetRegistrationInServiceWorkerGlobalScope();
169 void OnActivateEvent(int request_id);
170 void OnInstallEvent(int request_id);
171 void OnFetchEvent(int request_id, const ServiceWorkerFetchRequest& request);
172 void OnNotificationClickEvent(
173 int request_id,
174 int64_t persistent_notification_id,
175 const PlatformNotificationData& notification_data,
176 int action_index);
177 void OnPushEvent(int request_id, const std::string& data);
178 void OnGeofencingEvent(int request_id,
179 blink::WebGeofencingEventType event_type,
180 const std::string& region_id,
181 const blink::WebCircularGeofencingRegion& region);
182 void OnPostMessage(
183 const base::string16& message,
184 const std::vector<TransferredMessagePort>& sent_message_ports,
185 const std::vector<int>& new_routing_ids);
186 void OnCrossOriginMessageToWorker(
187 const NavigatorConnectClient& client,
188 const base::string16& message,
189 const std::vector<TransferredMessagePort>& sent_message_ports,
190 const std::vector<int>& new_routing_ids);
191 void OnDidGetClients(
192 int request_id, const std::vector<ServiceWorkerClientInfo>& clients);
193 void OnOpenWindowResponse(int request_id,
194 const ServiceWorkerClientInfo& client);
195 void OnOpenWindowError(int request_id, const std::string& message);
196 void OnFocusClientResponse(int request_id,
197 const ServiceWorkerClientInfo& client);
198 void OnNavigateClientResponse(int request_id,
199 const ServiceWorkerClientInfo& client);
200 void OnNavigateClientError(int request_id, const GURL& url);
201 void OnDidSkipWaiting(int request_id);
202 void OnDidClaimClients(int request_id);
203 void OnClaimClientsError(int request_id,
204 blink::WebServiceWorkerError::ErrorType error_type,
205 const base::string16& message);
206 void OnPing();
208 base::WeakPtr<ServiceWorkerContextClient> GetWeakPtr();
210 const int embedded_worker_id_;
211 const int64 service_worker_version_id_;
212 const GURL service_worker_scope_;
213 const GURL script_url_;
214 const int worker_devtools_agent_route_id_;
215 scoped_refptr<ThreadSafeSender> sender_;
216 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_;
217 scoped_refptr<base::TaskRunner> worker_task_runner_;
219 scoped_refptr<ServiceWorkerProviderContext> provider_context_;
221 // Not owned; this object is destroyed when proxy_ becomes invalid.
222 blink::WebServiceWorkerContextProxy* proxy_;
224 // Used for incoming messages from the browser for which an outgoing response
225 // back to the browser is expected, the id must be sent back with the
226 // response.
227 int current_request_id_;
229 // Initialized on the worker thread in workerContextStarted and
230 // destructed on the worker thread in willDestroyWorkerContext.
231 scoped_ptr<WorkerContextData> context_;
233 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerContextClient);
236 } // namespace content
238 #endif // CONTENT_RENDERER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_CLIENT_H_