worker_thread.cc revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
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/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"
26
27using WebKit::WebRuntimeFeatures;
28
29namespace content {
30
31static base::LazyInstance<base::ThreadLocalPointer<WorkerThread> > lazy_tls =
32    LAZY_INSTANCE_INITIALIZER;
33
34WorkerThread::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());
39
40  appcache_dispatcher_.reset(
41      new AppCacheDispatcher(this, new appcache::AppCacheFrontendImpl()));
42
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());
48
49  indexed_db_message_filter_ = new IndexedDBMessageFilter;
50  channel()->AddFilter(indexed_db_message_filter_.get());
51
52  const CommandLine& command_line = *CommandLine::ForCurrentProcess();
53  SetRuntimeFeaturesDefaultsAndUpdateFromArgs(command_line);
54}
55
56WorkerThread::~WorkerThread() {
57}
58
59void WorkerThread::Shutdown() {
60  // Shutdown in reverse of the initialization order.
61  channel()->RemoveFilter(indexed_db_message_filter_.get());
62  indexed_db_message_filter_ = NULL;
63
64  channel()->RemoveFilter(db_message_filter_.get());
65  db_message_filter_ = NULL;
66
67  WebKit::shutdown();
68  lazy_tls.Pointer()->Set(NULL);
69}
70
71WorkerThread* WorkerThread::current() {
72  return lazy_tls.Pointer()->Get();
73}
74
75bool WorkerThread::OnControlMessageReceived(const IPC::Message& msg) {
76  // Appcache messages are handled by a delegate.
77  if (appcache_dispatcher_->OnMessageReceived(msg))
78    return true;
79
80  bool handled = true;
81  IPC_BEGIN_MESSAGE_MAP(WorkerThread, msg)
82    IPC_MESSAGE_HANDLER(WorkerProcessMsg_CreateWorker, OnCreateWorker)
83    IPC_MESSAGE_UNHANDLED(handled = false)
84  IPC_END_MESSAGE_MAP()
85  return handled;
86}
87
88void WorkerThread::OnCreateWorker(
89    const WorkerProcessMsg_CreateWorker_Params& params) {
90  WorkerAppCacheInitInfo appcache_init_info(
91      params.creator_process_id,
92      params.shared_worker_appcache_id);
93
94  // WebSharedWorkerStub own themselves.
95  new WebSharedWorkerStub(params.name, params.route_id, appcache_init_info);
96}
97
98// The browser process is likely dead. Terminate all workers.
99void WorkerThread::OnChannelError() {
100  set_on_channel_error_called(true);
101
102  for (WorkerStubsList::iterator it = worker_stubs_.begin();
103       it != worker_stubs_.end(); ++it) {
104    (*it)->OnChannelError();
105  }
106}
107
108void WorkerThread::RemoveWorkerStub(WebSharedWorkerStub* stub) {
109  worker_stubs_.erase(stub);
110}
111
112void WorkerThread::AddWorkerStub(WebSharedWorkerStub* stub) {
113  worker_stubs_.insert(stub);
114}
115
116}  // namespace content
117