worker_thread.cc revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
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.
4
5#include "content/worker/worker_thread.h"
6
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/worker_messages.h"
13#include "content/common_child/indexed_db/indexed_db_message_filter.h"
14#include "content/common_child/web_database_observer_impl.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/Platform/chromium/public/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"
24
25using WebKit::WebRuntimeFeatures;
26
27namespace content {
28
29static base::LazyInstance<base::ThreadLocalPointer<WorkerThread> > lazy_tls =
30    LAZY_INSTANCE_INITIALIZER;
31
32WorkerThread::WorkerThread() {
33  lazy_tls.Pointer()->Set(this);
34  webkit_platform_support_.reset(
35      new WorkerWebKitPlatformSupportImpl(thread_safe_sender()));
36  WebKit::initialize(webkit_platform_support_.get());
37  WebKit::setIDBFactory(
38      webkit_platform_support_.get()->idbFactory());
39
40  appcache_dispatcher_.reset(new AppCacheDispatcher(this));
41
42  web_database_observer_impl_.reset(
43      new WebDatabaseObserverImpl(sync_message_filter()));
44  WebKit::WebDatabase::setObserver(web_database_observer_impl_.get());
45  db_message_filter_ = new DBMessageFilter();
46  channel()->AddFilter(db_message_filter_.get());
47
48  indexed_db_message_filter_ = new IndexedDBMessageFilter;
49  channel()->AddFilter(indexed_db_message_filter_.get());
50
51  const CommandLine& command_line = *CommandLine::ForCurrentProcess();
52
53  webkit_glue::EnableWebCoreLogChannels(
54      command_line.GetSwitchValueASCII(switches::kWebCoreLogChannels));
55
56  // TODO(eseidel): Workers should not have separate code for initializing
57  // WebRuntimeFeatures.  This should just call WRF::enableStableFeatures()
58  // and share CommandLine handling code with RenderThreadImpl.
59  WebKit::WebRuntimeFeatures::enableDatabase(
60      !command_line.HasSwitch(switches::kDisableDatabases));
61
62  WebKit::WebRuntimeFeatures::enableApplicationCache(
63      !command_line.HasSwitch(switches::kDisableApplicationCache));
64
65#if defined(OS_WIN)
66  // We don't yet support notifications on non-Windows, so hide it from pages.
67  WebRuntimeFeatures::enableNotifications(
68      !command_line.HasSwitch(switches::kDisableDesktopNotifications));
69#endif
70
71  WebRuntimeFeatures::enableFileSystem(
72      !command_line.HasSwitch(switches::kDisableFileSystem));
73
74  WebRuntimeFeatures::enableIndexedDB(true);
75}
76
77WorkerThread::~WorkerThread() {
78}
79
80void WorkerThread::Shutdown() {
81  // Shutdown in reverse of the initialization order.
82  channel()->RemoveFilter(indexed_db_message_filter_.get());
83  indexed_db_message_filter_ = NULL;
84
85  channel()->RemoveFilter(db_message_filter_.get());
86  db_message_filter_ = NULL;
87
88  WebKit::shutdown();
89  lazy_tls.Pointer()->Set(NULL);
90}
91
92WorkerThread* WorkerThread::current() {
93  return lazy_tls.Pointer()->Get();
94}
95
96bool WorkerThread::OnControlMessageReceived(const IPC::Message& msg) {
97  // Appcache messages are handled by a delegate.
98  if (appcache_dispatcher_->OnMessageReceived(msg))
99    return true;
100
101  bool handled = true;
102  IPC_BEGIN_MESSAGE_MAP(WorkerThread, msg)
103    IPC_MESSAGE_HANDLER(WorkerProcessMsg_CreateWorker, OnCreateWorker)
104    IPC_MESSAGE_UNHANDLED(handled = false)
105  IPC_END_MESSAGE_MAP()
106  return handled;
107}
108
109void WorkerThread::OnCreateWorker(
110    const WorkerProcessMsg_CreateWorker_Params& params) {
111  WorkerAppCacheInitInfo appcache_init_info(
112      params.creator_process_id,
113      params.shared_worker_appcache_id);
114
115  // WebSharedWorkerStub own themselves.
116  new WebSharedWorkerStub(params.name, params.route_id, appcache_init_info);
117}
118
119// The browser process is likely dead. Terminate all workers.
120void WorkerThread::OnChannelError() {
121  set_on_channel_error_called(true);
122
123  for (WorkerStubsList::iterator it = worker_stubs_.begin();
124       it != worker_stubs_.end(); ++it) {
125    (*it)->OnChannelError();
126  }
127}
128
129void WorkerThread::RemoveWorkerStub(WebSharedWorkerStub* stub) {
130  worker_stubs_.erase(stub);
131}
132
133void WorkerThread::AddWorkerStub(WebSharedWorkerStub* stub) {
134  worker_stubs_.insert(stub);
135}
136
137}  // namespace content
138