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(
37 new WorkerWebKitPlatformSupportImpl(thread_safe_sender()));
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 channel()->AddFilter(indexed_db_message_filter_
.get());
52 const CommandLine
& command_line
= *CommandLine::ForCurrentProcess();
53 SetRuntimeFeaturesDefaultsAndUpdateFromArgs(command_line
);
56 WorkerThread::~WorkerThread() {
59 void WorkerThread::Shutdown() {
60 // Shutdown in reverse of the initialization order.
61 channel()->RemoveFilter(indexed_db_message_filter_
.get());
62 indexed_db_message_filter_
= NULL
;
64 channel()->RemoveFilter(db_message_filter_
.get());
65 db_message_filter_
= NULL
;
68 lazy_tls
.Pointer()->Set(NULL
);
71 WorkerThread
* WorkerThread::current() {
72 return lazy_tls
.Pointer()->Get();
75 bool WorkerThread::OnControlMessageReceived(const IPC::Message
& msg
) {
76 // Appcache messages are handled by a delegate.
77 if (appcache_dispatcher_
->OnMessageReceived(msg
))
81 IPC_BEGIN_MESSAGE_MAP(WorkerThread
, msg
)
82 IPC_MESSAGE_HANDLER(WorkerProcessMsg_CreateWorker
, OnCreateWorker
)
83 IPC_MESSAGE_UNHANDLED(handled
= false)
88 void WorkerThread::OnCreateWorker(
89 const WorkerProcessMsg_CreateWorker_Params
& params
) {
90 WorkerAppCacheInitInfo
appcache_init_info(
91 params
.creator_process_id
,
92 params
.shared_worker_appcache_id
);
94 // WebSharedWorkerStub own themselves.
95 new WebSharedWorkerStub(params
.name
, params
.route_id
, appcache_init_info
);
98 // The browser process is likely dead. Terminate all workers.
99 void WorkerThread::OnChannelError() {
100 set_on_channel_error_called(true);
102 for (WorkerStubsList::iterator it
= worker_stubs_
.begin();
103 it
!= worker_stubs_
.end(); ++it
) {
104 (*it
)->OnChannelError();
108 void WorkerThread::RemoveWorkerStub(WebSharedWorkerStub
* stub
) {
109 worker_stubs_
.erase(stub
);
112 void WorkerThread::AddWorkerStub(WebSharedWorkerStub
* stub
) {
113 worker_stubs_
.insert(stub
);
116 } // namespace content