OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "content/renderer/service_worker/service_worker_context_client.h" | |
6 | |
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/renderer/render_thread_impl.h" | |
13 #include "content/renderer/service_worker/embedded_worker_dispatcher.h" | |
14 #include "ipc/ipc_message_macros.h" | |
15 #include "third_party/WebKit/public/platform/WebString.h" | |
16 #include "webkit/child/worker_task_runner.h" | |
17 | |
18 using webkit_glue::WorkerTaskRunner; | |
19 | |
20 namespace content { | |
21 | |
22 namespace { | |
23 | |
24 // For now client must be a per-thread instance. | |
25 // TODO(kinuko): This needs to be refactored when we start using thread pool | |
26 // or having multiple clients per one thread. | |
27 base::LazyInstance<base::ThreadLocalPointer<ServiceWorkerContextClient> >:: | |
28 Leaky g_worker_client_tls = LAZY_INSTANCE_INITIALIZER; | |
29 | |
30 void CallWorkerContextDestroyedOnMainThread(int embedded_worker_id) { | |
31 if (!RenderThreadImpl::current() || | |
32 !RenderThreadImpl::current()->embedded_worker_dispatcher()) | |
33 return; | |
34 RenderThreadImpl::current()->embedded_worker_dispatcher()-> | |
35 WorkerContextDestroyed(embedded_worker_id); | |
36 } | |
37 | |
38 } // namespace | |
39 | |
40 ServiceWorkerContextClient* | |
41 ServiceWorkerContextClient::ThreadSpecificInstance() { | |
42 return g_worker_client_tls.Pointer()->Get(); | |
43 } | |
44 | |
45 ServiceWorkerContextClient::ServiceWorkerContextClient( | |
46 int embedded_worker_id, | |
47 int64 service_worker_version_id, | |
48 const GURL& script_url) | |
49 : embedded_worker_id_(embedded_worker_id), | |
50 service_worker_version_id_(service_worker_version_id), | |
51 script_url_(script_url), | |
52 sender_(ChildThread::current()->thread_safe_sender()), | |
53 main_thread_proxy_(base::MessageLoopProxy::current()), | |
54 proxy_(NULL) { | |
55 } | |
56 | |
57 ServiceWorkerContextClient::~ServiceWorkerContextClient() { | |
58 DCHECK(g_worker_client_tls.Pointer()->Get() != NULL); | |
59 g_worker_client_tls.Pointer()->Set(NULL); | |
60 } | |
61 | |
62 bool ServiceWorkerContextClient::OnMessageReceived( | |
63 const IPC::Message& msg) { | |
64 NOTIMPLEMENTED(); | |
65 return false; | |
66 } | |
67 | |
68 void ServiceWorkerContextClient::workerContextFailedToStart() | |
69 { | |
70 DCHECK(main_thread_proxy_->RunsTasksOnCurrentThread()); | |
71 DCHECK(!proxy_); | |
72 | |
73 RenderThreadImpl::current()->embedded_worker_dispatcher()-> | |
74 WorkerContextDestroyed(embedded_worker_id_); | |
75 } | |
76 | |
77 void ServiceWorkerContextClient::workerContextStarted( | |
78 blink::WebServiceWorkerContextProxy* proxy) { | |
79 DCHECK_NE(0, WorkerTaskRunner::Instance()->CurrentWorkerId()); | |
80 DCHECK(g_worker_client_tls.Pointer()->Get() == NULL); | |
81 g_worker_client_tls.Pointer()->Set(this); | |
82 proxy_ = proxy; | |
83 | |
84 // TODO(kinuko): Send WorkerStarted message to the browser with the | |
85 // current thread ID so that the browser can start sending embedded worker | |
86 // messages directly to this client. | |
87 } | |
88 | |
89 void ServiceWorkerContextClient::workerContextDestroyed() { | |
90 DCHECK_NE(0, WorkerTaskRunner::Instance()->CurrentWorkerId()); | |
91 proxy_ = NULL; | |
92 main_thread_proxy_->PostTask( | |
93 FROM_HERE, | |
94 base::Bind(&CallWorkerContextDestroyedOnMainThread, | |
95 embedded_worker_id_)); | |
96 } | |
97 | |
98 } // namespace content | |
OLD | NEW |