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/threading/thread_local.h"
11 #include "content/child/thread_safe_sender.h"
12 #include "content/common/service_worker/embedded_worker_messages.h"
13 #include "content/renderer/render_thread_impl.h"
14 #include "content/renderer/service_worker/embedded_worker_dispatcher.h"
15 #include "ipc/ipc_message_macros.h"
16 #include "third_party/WebKit/public/platform/WebString.h"
17 #include "webkit/child/worker_task_runner.h"
19 using webkit_glue::WorkerTaskRunner
;
25 // For now client must be a per-thread instance.
26 // TODO(kinuko): This needs to be refactored when we start using thread pool
27 // or having multiple clients per one thread.
28 base::LazyInstance
<base::ThreadLocalPointer
<EmbeddedWorkerContextClient
> >::
29 Leaky g_worker_client_tls
= LAZY_INSTANCE_INITIALIZER
;
31 void CallWorkerContextDestroyedOnMainThread(int embedded_worker_id
) {
32 if (!RenderThreadImpl::current() ||
33 !RenderThreadImpl::current()->embedded_worker_dispatcher())
35 RenderThreadImpl::current()->embedded_worker_dispatcher()->
36 WorkerContextDestroyed(embedded_worker_id
);
41 EmbeddedWorkerContextClient
*
42 EmbeddedWorkerContextClient::ThreadSpecificInstance() {
43 return g_worker_client_tls
.Pointer()->Get();
46 EmbeddedWorkerContextClient::EmbeddedWorkerContextClient(
47 int embedded_worker_id
,
48 int64 service_worker_version_id
,
49 const GURL
& script_url
)
50 : embedded_worker_id_(embedded_worker_id
),
51 service_worker_version_id_(service_worker_version_id
),
52 script_url_(script_url
),
53 sender_(ChildThread::current()->thread_safe_sender()),
54 main_thread_proxy_(base::MessageLoopProxy::current()),
58 EmbeddedWorkerContextClient::~EmbeddedWorkerContextClient() {
59 DCHECK(g_worker_client_tls
.Pointer()->Get() != NULL
);
60 g_worker_client_tls
.Pointer()->Set(NULL
);
63 bool EmbeddedWorkerContextClient::OnMessageReceived(
64 const IPC::Message
& msg
) {
66 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerContextClient
, msg
)
67 IPC_MESSAGE_HANDLER(EmbeddedWorkerContextMsg_FetchEvent
, OnFetchEvent
)
68 IPC_MESSAGE_UNHANDLED(handled
= false)
73 void EmbeddedWorkerContextClient::workerContextFailedToStart() {
74 DCHECK(main_thread_proxy_
->RunsTasksOnCurrentThread());
77 RenderThreadImpl::current()->embedded_worker_dispatcher()->
78 WorkerContextDestroyed(embedded_worker_id_
);
81 void EmbeddedWorkerContextClient::workerContextStarted(
82 blink::WebServiceWorkerContextProxy
* proxy
) {
83 DCHECK_NE(0, WorkerTaskRunner::Instance()->CurrentWorkerId());
84 DCHECK(g_worker_client_tls
.Pointer()->Get() == NULL
);
85 g_worker_client_tls
.Pointer()->Set(this);
88 sender_
->Send(new EmbeddedWorkerHostMsg_WorkerStarted(
89 WorkerTaskRunner::Instance()->CurrentWorkerId(),
90 embedded_worker_id_
));
93 void EmbeddedWorkerContextClient::workerContextDestroyed() {
94 DCHECK_NE(0, WorkerTaskRunner::Instance()->CurrentWorkerId());
96 main_thread_proxy_
->PostTask(
98 base::Bind(&CallWorkerContextDestroyedOnMainThread
,
99 embedded_worker_id_
));
102 void EmbeddedWorkerContextClient::OnFetchEvent(
104 int embedded_worker_id
,
105 const ServiceWorkerFetchRequest
& request
) {
106 // TODO(kinuko): Implement.
107 // This is to call WebServiceWorkerContextProxy's dispatchFetchEvent method.
111 } // namespace content