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/common/appcache/appcache_dispatcher.h"
11 #include "content/common/db_message_filter.h"
12 #include "content/common/indexed_db/indexed_db_message_filter.h"
13 #include "content/common/web_database_observer_impl.h"
14 #include "content/common/worker_messages.h"
15 #include "content/public/common/content_switches.h"
16 #include "content/worker/websharedworker_stub.h"
17 #include "content/worker/worker_webkitplatformsupport_impl.h"
18 #include "ipc/ipc_sync_channel.h"
19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebBlobRegistry.h"
20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDatabase.h"
21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebRuntimeFeatures.h"
23 #include "webkit/glue/webkit_glue.h"
25 using WebKit::WebRuntimeFeatures
;
29 static base::LazyInstance
<base::ThreadLocalPointer
<WorkerThread
> > lazy_tls
=
30 LAZY_INSTANCE_INITIALIZER
;
32 WorkerThread::WorkerThread() {
33 lazy_tls
.Pointer()->Set(this);
34 webkit_platform_support_
.reset(new WorkerWebKitPlatformSupportImpl
);
35 WebKit::initialize(webkit_platform_support_
.get());
37 appcache_dispatcher_
.reset(new content::AppCacheDispatcher(this));
39 web_database_observer_impl_
.reset(
40 new WebDatabaseObserverImpl(sync_message_filter()));
41 WebKit::WebDatabase::setObserver(web_database_observer_impl_
.get());
42 db_message_filter_
= new DBMessageFilter();
43 channel()->AddFilter(db_message_filter_
.get());
45 indexed_db_message_filter_
= new content::IndexedDBMessageFilter
;
46 channel()->AddFilter(indexed_db_message_filter_
.get());
48 const CommandLine
& command_line
= *CommandLine::ForCurrentProcess();
50 webkit_glue::EnableWebCoreLogChannels(
51 command_line
.GetSwitchValueASCII(switches::kWebCoreLogChannels
));
53 WebKit::WebRuntimeFeatures::enableDatabase(
54 !command_line
.HasSwitch(switches::kDisableDatabases
));
56 WebKit::WebRuntimeFeatures::enableApplicationCache(
57 !command_line
.HasSwitch(switches::kDisableApplicationCache
));
60 // We don't yet support notifications on non-Windows, so hide it from pages.
61 WebRuntimeFeatures::enableNotifications(
62 !command_line
.HasSwitch(switches::kDisableDesktopNotifications
));
65 WebRuntimeFeatures::enableSockets(
66 !command_line
.HasSwitch(switches::kDisableWebSockets
));
68 WebRuntimeFeatures::enableFileSystem(
69 !command_line
.HasSwitch(switches::kDisableFileSystem
));
71 WebRuntimeFeatures::enableIndexedDatabase(true);
74 WorkerThread::~WorkerThread() {
75 // Shutdown in reverse of the initialization order.
76 channel()->RemoveFilter(indexed_db_message_filter_
.get());
77 indexed_db_message_filter_
= NULL
;
79 channel()->RemoveFilter(db_message_filter_
.get());
80 db_message_filter_
= NULL
;
83 lazy_tls
.Pointer()->Set(NULL
);
86 WorkerThread
* WorkerThread::current() {
87 return lazy_tls
.Pointer()->Get();
90 bool WorkerThread::OnControlMessageReceived(const IPC::Message
& msg
) {
91 // Appcache messages are handled by a delegate.
92 if (appcache_dispatcher_
->OnMessageReceived(msg
))
96 IPC_BEGIN_MESSAGE_MAP(WorkerThread
, msg
)
97 IPC_MESSAGE_HANDLER(WorkerProcessMsg_CreateWorker
, OnCreateWorker
)
98 IPC_MESSAGE_UNHANDLED(handled
= false)
103 void WorkerThread::OnCreateWorker(
104 const WorkerProcessMsg_CreateWorker_Params
& params
) {
105 WorkerAppCacheInitInfo
appcache_init_info(
106 params
.creator_process_id
,
107 params
.shared_worker_appcache_id
);
109 // WebSharedWorkerStub own themselves.
110 new WebSharedWorkerStub(params
.name
, params
.route_id
, appcache_init_info
);
113 // The browser process is likely dead. Terminate all workers.
114 void WorkerThread::OnChannelError() {
115 set_on_channel_error_called(true);
117 for (WorkerStubsList::iterator it
= worker_stubs_
.begin();
118 it
!= worker_stubs_
.end(); ++it
) {
119 (*it
)->OnChannelError();
123 void WorkerThread::RemoveWorkerStub(WebSharedWorkerStub
* stub
) {
124 worker_stubs_
.erase(stub
);
127 void WorkerThread::AddWorkerStub(WebSharedWorkerStub
* stub
) {
128 worker_stubs_
.insert(stub
);
131 } // namespace content