1 // Copyright (c) 2012 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/worker/worker_thread.h"
7 #include "base/command_line.h"
8 #include "base/lazy_instance.h"
9 #include "base/threading/thread_local.h"
10 #include "content/child/appcache_dispatcher.h"
11 #include "content/child/indexed_db/indexed_db_message_filter.h"
12 #include "content/child/runtime_features.h"
13 #include "content/child/web_database_observer_impl.h"
14 #include "content/common/db_message_filter.h"
15 #include "content/common/worker_messages.h"
16 #include "content/public/common/content_switches.h"
17 #include "content/worker/websharedworker_stub.h"
18 #include "content/worker/worker_webkitplatformsupport_impl.h"
19 #include "ipc/ipc_sync_channel.h"
20 #include "third_party/WebKit/public/web/WebDatabase.h"
21 #include "third_party/WebKit/public/web/WebKit.h"
22 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
23 #include "third_party/WebKit/public/platform/WebBlobRegistry.h"
24 #include "webkit/glue/webkit_glue.h"
25 #include "webkit/renderer/appcache/appcache_frontend_impl.h"
27 using WebKit::WebRuntimeFeatures
;
31 static base::LazyInstance
<base::ThreadLocalPointer
<WorkerThread
> > lazy_tls
=
32 LAZY_INSTANCE_INITIALIZER
;
34 WorkerThread::WorkerThread() {
35 lazy_tls
.Pointer()->Set(this);
36 webkit_platform_support_
.reset(new WorkerWebKitPlatformSupportImpl(
37 thread_safe_sender(), sync_message_filter()));
38 WebKit::initialize(webkit_platform_support_
.get());
40 appcache_dispatcher_
.reset(
41 new AppCacheDispatcher(this, new appcache::AppCacheFrontendImpl()));
43 web_database_observer_impl_
.reset(
44 new WebDatabaseObserverImpl(sync_message_filter()));
45 WebKit::WebDatabase::setObserver(web_database_observer_impl_
.get());
46 db_message_filter_
= new DBMessageFilter();
47 channel()->AddFilter(db_message_filter_
.get());
49 indexed_db_message_filter_
= new IndexedDBMessageFilter(
50 thread_safe_sender());
51 channel()->AddFilter(indexed_db_message_filter_
.get());
53 const CommandLine
& command_line
= *CommandLine::ForCurrentProcess();
54 SetRuntimeFeaturesDefaultsAndUpdateFromArgs(command_line
);
57 WorkerThread::~WorkerThread() {
60 void WorkerThread::Shutdown() {
61 // Shutdown in reverse of the initialization order.
62 channel()->RemoveFilter(indexed_db_message_filter_
.get());
63 indexed_db_message_filter_
= NULL
;
65 channel()->RemoveFilter(db_message_filter_
.get());
66 db_message_filter_
= NULL
;
69 lazy_tls
.Pointer()->Set(NULL
);
72 WorkerThread
* WorkerThread::current() {
73 return lazy_tls
.Pointer()->Get();
76 bool WorkerThread::OnControlMessageReceived(const IPC::Message
& msg
) {
77 // Appcache messages are handled by a delegate.
78 if (appcache_dispatcher_
->OnMessageReceived(msg
))
82 IPC_BEGIN_MESSAGE_MAP(WorkerThread
, msg
)
83 IPC_MESSAGE_HANDLER(WorkerProcessMsg_CreateWorker
, OnCreateWorker
)
84 IPC_MESSAGE_UNHANDLED(handled
= false)
89 void WorkerThread::OnCreateWorker(
90 const WorkerProcessMsg_CreateWorker_Params
& params
) {
91 WorkerAppCacheInitInfo
appcache_init_info(
92 params
.creator_process_id
,
93 params
.shared_worker_appcache_id
);
95 // WebSharedWorkerStub own themselves.
96 new WebSharedWorkerStub(params
.name
, params
.route_id
, appcache_init_info
);
99 // The browser process is likely dead. Terminate all workers.
100 void WorkerThread::OnChannelError() {
101 set_on_channel_error_called(true);
103 for (WorkerStubsList::iterator it
= worker_stubs_
.begin();
104 it
!= worker_stubs_
.end(); ++it
) {
105 (*it
)->OnChannelError();
109 void WorkerThread::RemoveWorkerStub(WebSharedWorkerStub
* stub
) {
110 worker_stubs_
.erase(stub
);
113 void WorkerThread::AddWorkerStub(WebSharedWorkerStub
* stub
) {
114 worker_stubs_
.insert(stub
);
117 } // namespace content