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 #include "content/renderer/service_worker/embedded_worker_context_client.h"
7 #include "base/lazy_instance.h"
8 #include "base/message_loop/message_loop_proxy.h"
9 #include "base/pickle.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/threading/thread_local.h"
13 #include "content/child/request_extra_data.h"
14 #include "content/child/service_worker/service_worker_network_provider.h"
15 #include "content/child/thread_safe_sender.h"
16 #include "content/child/worker_task_runner.h"
17 #include "content/child/worker_thread_task_runner.h"
18 #include "content/common/devtools_messages.h"
19 #include "content/common/service_worker/embedded_worker_messages.h"
20 #include "content/common/service_worker/service_worker_types.h"
21 #include "content/public/renderer/document_state.h"
22 #include "content/renderer/render_thread_impl.h"
23 #include "content/renderer/service_worker/embedded_worker_dispatcher.h"
24 #include "content/renderer/service_worker/service_worker_script_context.h"
25 #include "ipc/ipc_message_macros.h"
26 #include "third_party/WebKit/public/platform/WebServiceWorkerResponse.h"
27 #include "third_party/WebKit/public/platform/WebString.h"
28 #include "third_party/WebKit/public/web/WebDataSource.h"
29 #include "third_party/WebKit/public/web/WebServiceWorkerNetworkProvider.h"
35 // For now client must be a per-thread instance.
36 // TODO(kinuko): This needs to be refactored when we start using thread pool
37 // or having multiple clients per one thread.
38 base::LazyInstance
<base::ThreadLocalPointer
<EmbeddedWorkerContextClient
> >::
39 Leaky g_worker_client_tls
= LAZY_INSTANCE_INITIALIZER
;
41 void CallWorkerContextDestroyedOnMainThread(int embedded_worker_id
) {
42 if (!RenderThreadImpl::current() ||
43 !RenderThreadImpl::current()->embedded_worker_dispatcher())
45 RenderThreadImpl::current()->embedded_worker_dispatcher()->
46 WorkerContextDestroyed(embedded_worker_id
);
49 // We store an instance of this class in the "extra data" of the WebDataSource
50 // and attach a ServiceWorkerNetworkProvider to it as base::UserData.
51 // (see createServiceWorkerNetworkProvider).
52 class DataSourceExtraData
53 : public blink::WebDataSource::ExtraData
,
54 public base::SupportsUserData
{
56 DataSourceExtraData() {}
57 virtual ~DataSourceExtraData() {}
60 // Called on the main thread only and blink owns it.
61 class WebServiceWorkerNetworkProviderImpl
62 : public blink::WebServiceWorkerNetworkProvider
{
64 // Blink calls this method for each request starting with the main script,
65 // we tag them with the provider id.
66 virtual void willSendRequest(
67 blink::WebDataSource
* data_source
,
68 blink::WebURLRequest
& request
) {
69 ServiceWorkerNetworkProvider
* provider
=
70 ServiceWorkerNetworkProvider::FromDocumentState(
71 static_cast<DataSourceExtraData
*>(data_source
->extraData()));
72 scoped_ptr
<RequestExtraData
> extra_data(new RequestExtraData
);
73 extra_data
->set_service_worker_provider_id(provider
->provider_id());
74 request
.setExtraData(extra_data
.release());
80 EmbeddedWorkerContextClient
*
81 EmbeddedWorkerContextClient::ThreadSpecificInstance() {
82 return g_worker_client_tls
.Pointer()->Get();
85 EmbeddedWorkerContextClient::EmbeddedWorkerContextClient(
86 int embedded_worker_id
,
87 int64 service_worker_version_id
,
88 const GURL
& service_worker_scope
,
89 const GURL
& script_url
,
90 int worker_devtools_agent_route_id
)
91 : embedded_worker_id_(embedded_worker_id
),
92 service_worker_version_id_(service_worker_version_id
),
93 service_worker_scope_(service_worker_scope
),
94 script_url_(script_url
),
95 worker_devtools_agent_route_id_(worker_devtools_agent_route_id
),
96 sender_(ChildThread::current()->thread_safe_sender()),
97 main_thread_proxy_(base::MessageLoopProxy::current()),
101 EmbeddedWorkerContextClient::~EmbeddedWorkerContextClient() {
104 bool EmbeddedWorkerContextClient::OnMessageReceived(
105 const IPC::Message
& msg
) {
107 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerContextClient
, msg
)
108 IPC_MESSAGE_HANDLER(EmbeddedWorkerContextMsg_MessageToWorker
,
110 IPC_MESSAGE_UNHANDLED(handled
= false)
111 IPC_END_MESSAGE_MAP()
115 void EmbeddedWorkerContextClient::Send(IPC::Message
* message
) {
116 sender_
->Send(message
);
119 blink::WebURL
EmbeddedWorkerContextClient::scope() const {
120 return service_worker_scope_
;
123 void EmbeddedWorkerContextClient::getClients(
124 blink::WebServiceWorkerClientsCallbacks
* callbacks
) {
125 DCHECK(script_context_
);
126 script_context_
->GetClientDocuments(callbacks
);
129 void EmbeddedWorkerContextClient::workerContextFailedToStart() {
130 DCHECK(main_thread_proxy_
->RunsTasksOnCurrentThread());
131 DCHECK(!script_context_
);
133 Send(new EmbeddedWorkerHostMsg_WorkerScriptLoadFailed(embedded_worker_id_
));
135 RenderThreadImpl::current()->embedded_worker_dispatcher()->
136 WorkerContextDestroyed(embedded_worker_id_
);
139 void EmbeddedWorkerContextClient::workerContextStarted(
140 blink::WebServiceWorkerContextProxy
* proxy
) {
141 DCHECK(!worker_task_runner_
);
142 worker_task_runner_
= new WorkerThreadTaskRunner(
143 WorkerTaskRunner::Instance()->CurrentWorkerId());
144 DCHECK_NE(0, WorkerTaskRunner::Instance()->CurrentWorkerId());
145 // g_worker_client_tls.Pointer()->Get() could return NULL if this context
146 // gets deleted before workerContextStarted() is called.
147 DCHECK(g_worker_client_tls
.Pointer()->Get() == NULL
);
148 DCHECK(!script_context_
);
149 g_worker_client_tls
.Pointer()->Set(this);
150 script_context_
.reset(new ServiceWorkerScriptContext(this, proxy
));
152 Send(new EmbeddedWorkerHostMsg_WorkerScriptLoaded(embedded_worker_id_
));
154 // Schedule a task to send back WorkerStarted asynchronously,
155 // so that at the time we send it we can be sure that the worker
156 // script has been evaluated and worker run loop has been started.
157 worker_task_runner_
->PostTask(
159 base::Bind(&EmbeddedWorkerContextClient::SendWorkerStarted
,
160 weak_factory_
.GetWeakPtr()));
163 void EmbeddedWorkerContextClient::willDestroyWorkerContext() {
164 // At this point OnWorkerRunLoopStopped is already called, so
165 // worker_task_runner_->RunsTasksOnCurrentThread() returns false
166 // (while we're still on the worker thread).
167 script_context_
.reset();
169 // This also lets the message filter stop dispatching messages to
171 g_worker_client_tls
.Pointer()->Set(NULL
);
174 void EmbeddedWorkerContextClient::workerContextDestroyed() {
175 DCHECK(g_worker_client_tls
.Pointer()->Get() == NULL
);
177 // Now we should be able to free the WebEmbeddedWorker container on the
179 main_thread_proxy_
->PostTask(
181 base::Bind(&CallWorkerContextDestroyedOnMainThread
,
182 embedded_worker_id_
));
185 void EmbeddedWorkerContextClient::reportException(
186 const blink::WebString
& error_message
,
189 const blink::WebString
& source_url
) {
190 Send(new EmbeddedWorkerHostMsg_ReportException(
191 embedded_worker_id_
, error_message
, line_number
,
192 column_number
, GURL(source_url
)));
195 void EmbeddedWorkerContextClient::reportConsoleMessage(
198 const blink::WebString
& message
,
200 const blink::WebString
& source_url
) {
201 EmbeddedWorkerHostMsg_ReportConsoleMessage_Params params
;
202 params
.source_identifier
= source
;
203 params
.message_level
= level
;
204 params
.message
= message
;
205 params
.line_number
= line_number
;
206 params
.source_url
= GURL(source_url
);
208 Send(new EmbeddedWorkerHostMsg_ReportConsoleMessage(
209 embedded_worker_id_
, params
));
212 void EmbeddedWorkerContextClient::dispatchDevToolsMessage(
213 const blink::WebString
& message
) {
214 sender_
->Send(new DevToolsClientMsg_DispatchOnInspectorFrontend(
215 worker_devtools_agent_route_id_
, message
.utf8()));
218 void EmbeddedWorkerContextClient::saveDevToolsAgentState(
219 const blink::WebString
& state
) {
220 sender_
->Send(new DevToolsHostMsg_SaveAgentRuntimeState(
221 worker_devtools_agent_route_id_
, state
.utf8()));
224 void EmbeddedWorkerContextClient::didHandleActivateEvent(
226 blink::WebServiceWorkerEventResult result
) {
227 DCHECK(script_context_
);
228 script_context_
->DidHandleActivateEvent(request_id
, result
);
231 void EmbeddedWorkerContextClient::didHandleInstallEvent(
233 blink::WebServiceWorkerEventResult result
) {
234 DCHECK(script_context_
);
235 script_context_
->DidHandleInstallEvent(request_id
, result
);
238 void EmbeddedWorkerContextClient::didHandleFetchEvent(int request_id
) {
239 DCHECK(script_context_
);
240 script_context_
->DidHandleFetchEvent(
242 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK
,
243 ServiceWorkerResponse());
246 void EmbeddedWorkerContextClient::didHandleFetchEvent(
248 const blink::WebServiceWorkerResponse
& web_response
) {
249 DCHECK(script_context_
);
250 #ifdef NEW_SERVICE_WORKER_RESPONSE_INTERFACE
251 std::map
<std::string
, std::string
> headers
;
252 const blink::WebVector
<blink::WebString
>& header_keys
=
253 web_response
.getHeaderKeys();
254 for (size_t i
= 0; i
< header_keys
.size(); ++i
) {
255 const base::string16
& key
= header_keys
[i
];
256 headers
[base::UTF16ToUTF8(key
)] =
257 base::UTF16ToUTF8(web_response
.getHeader(key
));
259 ServiceWorkerResponse
response(web_response
.status(),
260 web_response
.statusText().utf8(),
263 // TODO(kinuko): Cleanup this once blink side patch is rolled.
264 ServiceWorkerResponse
response(web_response
.statusCode(),
265 web_response
.statusText().utf8(),
266 web_response
.method().utf8(),
267 std::map
<std::string
, std::string
>());
269 script_context_
->DidHandleFetchEvent(
270 request_id
, SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE
, response
);
273 void EmbeddedWorkerContextClient::didHandleSyncEvent(int request_id
) {
274 DCHECK(script_context_
);
275 script_context_
->DidHandleSyncEvent(request_id
);
278 blink::WebServiceWorkerNetworkProvider
*
279 EmbeddedWorkerContextClient::createServiceWorkerNetworkProvider(
280 blink::WebDataSource
* data_source
) {
281 // Create a content::ServiceWorkerNetworkProvider for this data source so
282 // we can observe its requests.
283 scoped_ptr
<ServiceWorkerNetworkProvider
> provider(
284 new ServiceWorkerNetworkProvider());
286 // Tell the network provider about which version to load.
287 provider
->SetServiceWorkerVersionId(service_worker_version_id_
);
289 // The provider is kept around for the lifetime of the DataSource
290 // and ownership is transferred to the DataSource.
291 DataSourceExtraData
* extra_data
= new DataSourceExtraData();
292 data_source
->setExtraData(extra_data
);
293 ServiceWorkerNetworkProvider::AttachToDocumentState(
294 extra_data
, provider
.Pass());
296 // Blink is responsible for deleting the returned object.
297 return new WebServiceWorkerNetworkProviderImpl();
300 void EmbeddedWorkerContextClient::postMessageToClient(
302 const blink::WebString
& message
,
303 blink::WebMessagePortChannelArray
* channels
) {
304 DCHECK(script_context_
);
305 script_context_
->PostMessageToDocument(client_id
, message
,
306 make_scoped_ptr(channels
));
309 void EmbeddedWorkerContextClient::OnMessageToWorker(
311 int embedded_worker_id
,
312 const IPC::Message
& message
) {
313 if (!script_context_
)
315 DCHECK_EQ(embedded_worker_id_
, embedded_worker_id
);
316 script_context_
->OnMessageReceived(message
);
319 void EmbeddedWorkerContextClient::SendWorkerStarted() {
320 DCHECK(worker_task_runner_
->RunsTasksOnCurrentThread());
321 Send(new EmbeddedWorkerHostMsg_WorkerStarted(
322 WorkerTaskRunner::Instance()->CurrentWorkerId(),
323 embedded_worker_id_
));
326 } // namespace content