1 // Copyright 2013 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_EMBEDDED_WORKER_CONTEXT_CLIENT_H_
6 #define CONTENT_RENDERER_SERVICE_WORKER_EMBEDDED_WORKER_CONTEXT_CLIENT_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/strings/string16.h"
11 #include "content/common/service_worker/service_worker_types.h"
12 #include "ipc/ipc_listener.h"
13 #include "third_party/WebKit/public/web/WebServiceWorkerContextClient.h"
17 class SingleThreadTaskRunner
;
23 class WebServiceWorkerProvider
;
28 class ServiceWorkerProviderContext
;
29 class ServiceWorkerScriptContext
;
30 class ThreadSafeSender
;
32 // This class provides access to/from an embedded worker's WorkerGlobalScope.
33 // Unless otherwise noted, all methods are called on the worker thread.
35 // TODO(kinuko): Currently EW/SW separation is made a little hazily.
36 // This should implement WebEmbeddedWorkerContextClient
37 // or sort of it (which doesn't exist yet) rather than
38 // WebServiceWorkerContextClient if we want to separate them more cleanly,
39 // or ServiceWorkerScriptContext should be merged into this class
40 // if we consider EW == SW script context.
41 class EmbeddedWorkerContextClient
42 : public blink::WebServiceWorkerContextClient
{
44 // Returns a thread-specific client instance. This does NOT create a
46 static EmbeddedWorkerContextClient
* ThreadSpecificInstance();
48 // Called on the main thread.
49 EmbeddedWorkerContextClient(int embedded_worker_id
,
50 int64 service_worker_version_id
,
51 const GURL
& service_worker_scope
,
52 const GURL
& script_url
,
53 int worker_devtools_agent_route_id
);
55 virtual ~EmbeddedWorkerContextClient();
57 bool OnMessageReceived(const IPC::Message
& msg
);
59 void Send(IPC::Message
* message
);
61 // WebServiceWorkerContextClient overrides, some of them are just dispatched
62 // on to script_context_.
63 virtual blink::WebURL
scope() const;
64 virtual blink::WebServiceWorkerCacheStorage
* cacheStorage();
65 virtual void didPauseAfterDownload();
66 virtual void getClients(blink::WebServiceWorkerClientsCallbacks
*);
67 virtual void openWindow(const blink::WebURL
&,
68 blink::WebServiceWorkerClientCallbacks
*);
69 virtual void setCachedMetadata(const blink::WebURL
&,
72 virtual void clearCachedMetadata(const blink::WebURL
&);
73 virtual void workerReadyForInspection();
75 // Called on the main thread.
76 virtual void workerContextFailedToStart();
78 virtual void workerContextStarted(blink::WebServiceWorkerContextProxy
* proxy
);
79 virtual void didEvaluateWorkerScript(bool success
);
80 virtual void willDestroyWorkerContext();
81 virtual void workerContextDestroyed();
82 virtual void reportException(const blink::WebString
& error_message
,
85 const blink::WebString
& source_url
);
86 virtual void reportConsoleMessage(int source
,
88 const blink::WebString
& message
,
90 const blink::WebString
& source_url
);
91 virtual void sendDevToolsMessage(int call_id
,
92 const blink::WebString
& message
,
93 const blink::WebString
& state
);
94 virtual void didHandleActivateEvent(int request_id
,
95 blink::WebServiceWorkerEventResult
);
96 virtual void didHandleInstallEvent(int request_id
,
97 blink::WebServiceWorkerEventResult result
);
98 virtual void didHandleFetchEvent(int request_id
);
99 virtual void didHandleFetchEvent(
101 const blink::WebServiceWorkerResponse
& response
);
102 virtual void didHandleNotificationClickEvent(
104 blink::WebServiceWorkerEventResult result
);
105 virtual void didHandlePushEvent(int request_id
,
106 blink::WebServiceWorkerEventResult result
);
107 virtual void didHandleSyncEvent(int request_id
);
108 virtual void didHandleCrossOriginConnectEvent(int request_id
,
109 bool accept_connection
);
111 // Called on the main thread.
112 virtual blink::WebServiceWorkerNetworkProvider
*
113 createServiceWorkerNetworkProvider(blink::WebDataSource
* data_source
);
114 virtual blink::WebServiceWorkerProvider
* createServiceWorkerProvider();
116 virtual void postMessageToClient(
118 const blink::WebString
& message
,
119 blink::WebMessagePortChannelArray
* channels
);
120 virtual void postMessageToCrossOriginClient(
121 const blink::WebCrossOriginServiceWorkerClient
& client
,
122 const blink::WebString
& message
,
123 blink::WebMessagePortChannelArray
* channels
);
124 virtual void focus(int client_id
,
125 blink::WebServiceWorkerClientCallbacks
*);
126 virtual void skipWaiting(
127 blink::WebServiceWorkerSkipWaitingCallbacks
* callbacks
);
128 virtual void claim(blink::WebServiceWorkerClientsClaimCallbacks
* callbacks
);
130 // TODO: Implement DevTools related method overrides.
132 int embedded_worker_id() const { return embedded_worker_id_
; }
133 base::SingleThreadTaskRunner
* main_thread_task_runner() const {
134 return main_thread_task_runner_
.get();
136 ThreadSafeSender
* thread_safe_sender() { return sender_
.get(); }
139 void OnMessageToWorker(int thread_id
,
140 int embedded_worker_id
,
141 const IPC::Message
& message
);
142 void SendWorkerStarted();
143 void SetRegistrationInServiceWorkerGlobalScope();
145 const int embedded_worker_id_
;
146 const int64 service_worker_version_id_
;
147 const GURL service_worker_scope_
;
148 const GURL script_url_
;
149 const int worker_devtools_agent_route_id_
;
150 scoped_refptr
<ThreadSafeSender
> sender_
;
151 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner_
;
152 scoped_refptr
<base::TaskRunner
> worker_task_runner_
;
154 scoped_ptr
<ServiceWorkerScriptContext
> script_context_
;
155 scoped_refptr
<ServiceWorkerProviderContext
> provider_context_
;
157 base::WeakPtrFactory
<EmbeddedWorkerContextClient
> weak_factory_
;
159 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerContextClient
);
162 } // namespace content
164 #endif // CONTENT_RENDERER_SERVICE_WORKER_EMBEDDED_WORKER_CONTEXT_CLIENT_H_