IndexedDBFactory now ForceCloses databases.
[chromium-blink-merge.git] / content / worker / worker_thread.cc
blob456457c4e780e6bb350d53946818d029557f7195
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/appcache_dispatcher.h"
11 #include "content/child/appcache/appcache_frontend_impl.h"
12 #include "content/child/db_message_filter.h"
13 #include "content/child/indexed_db/indexed_db_message_filter.h"
14 #include "content/child/runtime_features.h"
15 #include "content/child/web_database_observer_impl.h"
16 #include "content/common/child_process_messages.h"
17 #include "content/common/worker_messages.h"
18 #include "content/public/common/content_switches.h"
19 #include "content/worker/websharedworker_stub.h"
20 #include "content/worker/worker_webkitplatformsupport_impl.h"
21 #include "ipc/ipc_sync_channel.h"
22 #include "third_party/WebKit/public/platform/WebBlobRegistry.h"
23 #include "third_party/WebKit/public/web/WebDatabase.h"
24 #include "third_party/WebKit/public/web/WebKit.h"
25 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
26 #include "webkit/glue/webkit_glue.h"
28 using blink::WebRuntimeFeatures;
30 namespace content {
32 static base::LazyInstance<base::ThreadLocalPointer<WorkerThread> > lazy_tls =
33 LAZY_INSTANCE_INITIALIZER;
35 WorkerThread::WorkerThread() {
36 lazy_tls.Pointer()->Set(this);
37 webkit_platform_support_.reset(new WorkerWebKitPlatformSupportImpl(
38 thread_safe_sender(),
39 sync_message_filter(),
40 quota_message_filter()));
42 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
43 if (command_line.HasSwitch(switches::kJavaScriptFlags)) {
44 webkit_glue::SetJavaScriptFlags(
45 command_line.GetSwitchValueASCII(switches::kJavaScriptFlags));
47 SetRuntimeFeaturesDefaultsAndUpdateFromArgs(command_line);
49 blink::initialize(webkit_platform_support_.get());
51 appcache_dispatcher_.reset(
52 new AppCacheDispatcher(this, new AppCacheFrontendImpl()));
54 db_message_filter_ = new DBMessageFilter();
55 channel()->AddFilter(db_message_filter_.get());
57 indexed_db_message_filter_ = new IndexedDBMessageFilter(
58 thread_safe_sender());
59 channel()->AddFilter(indexed_db_message_filter_->GetFilter());
63 void WorkerThread::OnShutdown() {
64 // The worker process is to be shut down gracefully. Ask the browser
65 // process to shut it down forcefully instead and wait on the message, so that
66 // there are no races between threads when the process is shutting down.
67 Send(new WorkerProcessHostMsg_ForceKillWorker());
70 WorkerThread::~WorkerThread() {
73 void WorkerThread::Shutdown() {
74 ChildThread::Shutdown();
76 if (webkit_platform_support_) {
77 webkit_platform_support_->web_database_observer_impl()->
78 WaitForAllDatabasesToClose();
81 // Shutdown in reverse of the initialization order.
82 indexed_db_message_filter_ = NULL;
84 channel()->RemoveFilter(db_message_filter_.get());
85 db_message_filter_ = NULL;
87 blink::shutdown();
88 lazy_tls.Pointer()->Set(NULL);
91 WorkerThread* WorkerThread::current() {
92 return lazy_tls.Pointer()->Get();
95 bool WorkerThread::OnControlMessageReceived(const IPC::Message& msg) {
96 // Appcache messages are handled by a delegate.
97 if (appcache_dispatcher_->OnMessageReceived(msg))
98 return true;
100 bool handled = true;
101 IPC_BEGIN_MESSAGE_MAP(WorkerThread, msg)
102 IPC_MESSAGE_HANDLER(WorkerProcessMsg_CreateWorker, OnCreateWorker)
103 IPC_MESSAGE_UNHANDLED(handled = false)
104 IPC_END_MESSAGE_MAP()
105 return handled;
108 bool WorkerThread::OnMessageReceived(const IPC::Message& msg) {
109 bool handled = true;
110 IPC_BEGIN_MESSAGE_MAP(WorkerThread, msg)
111 IPC_MESSAGE_HANDLER(ChildProcessMsg_Shutdown, OnShutdown)
112 IPC_MESSAGE_UNHANDLED(handled = ChildThread::OnMessageReceived(msg))
113 IPC_END_MESSAGE_MAP()
114 return handled;
117 void WorkerThread::OnCreateWorker(
118 const WorkerProcessMsg_CreateWorker_Params& params) {
119 WorkerAppCacheInitInfo appcache_init_info(
120 params.creator_process_id,
121 params.shared_worker_appcache_id);
123 // WebSharedWorkerStub own themselves.
124 new WebSharedWorkerStub(
125 params.url,
126 params.name,
127 params.content_security_policy,
128 params.security_policy_type,
129 params.route_id,
130 appcache_init_info);
133 // The browser process is likely dead. Terminate all workers.
134 void WorkerThread::OnChannelError() {
135 set_on_channel_error_called(true);
137 for (WorkerStubsList::iterator it = worker_stubs_.begin();
138 it != worker_stubs_.end(); ++it) {
139 (*it)->OnChannelError();
143 void WorkerThread::RemoveWorkerStub(WebSharedWorkerStub* stub) {
144 worker_stubs_.erase(stub);
147 void WorkerThread::AddWorkerStub(WebSharedWorkerStub* stub) {
148 worker_stubs_.insert(stub);
151 } // namespace content