worker_thread.cc revision ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16
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/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/worker_messages.h"
17#include "content/public/common/content_switches.h"
18#include "content/worker/websharedworker_stub.h"
19#include "content/worker/worker_webkitplatformsupport_impl.h"
20#include "ipc/ipc_sync_channel.h"
21#include "third_party/WebKit/public/platform/WebBlobRegistry.h"
22#include "third_party/WebKit/public/web/WebDatabase.h"
23#include "third_party/WebKit/public/web/WebKit.h"
24#include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
25#include "webkit/glue/webkit_glue.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(new WorkerWebKitPlatformSupportImpl(
37      thread_safe_sender(),
38      sync_message_filter(),
39      quota_message_filter()));
40  WebKit::initialize(webkit_platform_support_.get());
41
42  appcache_dispatcher_.reset(
43      new AppCacheDispatcher(this, new AppCacheFrontendImpl()));
44
45  web_database_observer_impl_.reset(
46      new WebDatabaseObserverImpl(sync_message_filter()));
47  WebKit::WebDatabase::setObserver(web_database_observer_impl_.get());
48  db_message_filter_ = new DBMessageFilter();
49  channel()->AddFilter(db_message_filter_.get());
50
51  indexed_db_message_filter_ = new IndexedDBMessageFilter(
52      thread_safe_sender());
53  channel()->AddFilter(indexed_db_message_filter_.get());
54
55  const CommandLine& command_line = *CommandLine::ForCurrentProcess();
56  SetRuntimeFeaturesDefaultsAndUpdateFromArgs(command_line);
57}
58
59WorkerThread::~WorkerThread() {
60}
61
62void WorkerThread::Shutdown() {
63  ChildThread::Shutdown();
64
65  // Shutdown in reverse of the initialization order.
66  channel()->RemoveFilter(indexed_db_message_filter_.get());
67  indexed_db_message_filter_ = NULL;
68
69  channel()->RemoveFilter(db_message_filter_.get());
70  db_message_filter_ = NULL;
71
72  WebKit::shutdown();
73  lazy_tls.Pointer()->Set(NULL);
74}
75
76WorkerThread* WorkerThread::current() {
77  return lazy_tls.Pointer()->Get();
78}
79
80bool WorkerThread::OnControlMessageReceived(const IPC::Message& msg) {
81  // Appcache messages are handled by a delegate.
82  if (appcache_dispatcher_->OnMessageReceived(msg))
83    return true;
84
85  bool handled = true;
86  IPC_BEGIN_MESSAGE_MAP(WorkerThread, msg)
87    IPC_MESSAGE_HANDLER(WorkerProcessMsg_CreateWorker, OnCreateWorker)
88    IPC_MESSAGE_UNHANDLED(handled = false)
89  IPC_END_MESSAGE_MAP()
90  return handled;
91}
92
93void WorkerThread::OnCreateWorker(
94    const WorkerProcessMsg_CreateWorker_Params& params) {
95  WorkerAppCacheInitInfo appcache_init_info(
96      params.creator_process_id,
97      params.shared_worker_appcache_id);
98
99  // WebSharedWorkerStub own themselves.
100  new WebSharedWorkerStub(params.name, params.route_id, appcache_init_info);
101}
102
103// The browser process is likely dead. Terminate all workers.
104void WorkerThread::OnChannelError() {
105  set_on_channel_error_called(true);
106
107  for (WorkerStubsList::iterator it = worker_stubs_.begin();
108       it != worker_stubs_.end(); ++it) {
109    (*it)->OnChannelError();
110  }
111}
112
113void WorkerThread::RemoveWorkerStub(WebSharedWorkerStub* stub) {
114  worker_stubs_.erase(stub);
115}
116
117void WorkerThread::AddWorkerStub(WebSharedWorkerStub* stub) {
118  worker_stubs_.insert(stub);
119}
120
121}  // namespace content
122