IndexedDBFactory now ForceCloses databases.
[chromium-blink-merge.git] / content / plugin / plugin_thread.cc
blobf3dd0fdaaa78ec6e05095348e89bfc9858f067d5
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/plugin/plugin_thread.h"
7 #include "build/build_config.h"
9 #if defined(TOOLKIT_GTK)
10 #include <gtk/gtk.h>
11 #elif defined(OS_MACOSX)
12 #include <CoreFoundation/CoreFoundation.h>
13 #endif
15 #include <string>
16 #include <vector>
18 #include "base/bind.h"
19 #include "base/command_line.h"
20 #include "base/lazy_instance.h"
21 #include "base/process/kill.h"
22 #include "base/process/process_handle.h"
23 #include "base/threading/thread_local.h"
24 #include "content/child/child_process.h"
25 #include "content/child/npapi/npobject_util.h"
26 #include "content/child/npapi/plugin_lib.h"
27 #include "content/common/plugin_process_messages.h"
28 #include "content/public/common/content_switches.h"
29 #include "content/public/plugin/content_plugin_client.h"
30 #include "ipc/ipc_channel_handle.h"
32 #if defined(TOOLKIT_GTK)
33 #include "ui/gfx/gtk_util.h"
34 #endif
36 #if defined(USE_X11)
37 #include "ui/base/x/x11_util.h"
38 #endif
40 namespace content {
42 namespace {
44 class EnsureTerminateMessageFilter : public IPC::ChannelProxy::MessageFilter {
45 public:
46 EnsureTerminateMessageFilter() {}
48 protected:
49 virtual ~EnsureTerminateMessageFilter() {}
51 // IPC::ChannelProxy::MessageFilter:
52 virtual void OnChannelError() OVERRIDE {
53 // How long we wait before forcibly shutting down the process.
54 const base::TimeDelta kPluginProcessTerminateTimeout =
55 base::TimeDelta::FromSeconds(3);
56 // Ensure that we don't wait indefinitely for the plugin to shutdown.
57 // as the browser does not terminate plugin processes on shutdown.
58 // We achieve this by posting an exit process task on the IO thread.
59 base::MessageLoop::current()->PostDelayedTask(
60 FROM_HERE,
61 base::Bind(&EnsureTerminateMessageFilter::Terminate, this),
62 kPluginProcessTerminateTimeout);
65 private:
66 void Terminate() {
67 base::KillProcess(base::GetCurrentProcessHandle(), 0, false);
71 } // namespace
73 static base::LazyInstance<base::ThreadLocalPointer<PluginThread> > lazy_tls =
74 LAZY_INSTANCE_INITIALIZER;
76 PluginThread::PluginThread()
77 : preloaded_plugin_module_(NULL),
78 forcefully_terminate_plugin_process_(false) {
79 base::FilePath plugin_path =
80 CommandLine::ForCurrentProcess()->GetSwitchValuePath(
81 switches::kPluginPath);
83 lazy_tls.Pointer()->Set(this);
84 #if defined(USE_AURA)
85 // TODO(saintlou):
86 #elif defined(TOOLKIT_GTK)
88 // XEmbed plugins assume they are hosted in a Gtk application, so we need
89 // to initialize Gtk in the plugin process.
90 // g_thread_init API is deprecated since glib 2.31.0, see release note:
91 // http://mail.gnome.org/archives/gnome-announce-list/2011-October/msg00041.html
92 #if !(GLIB_CHECK_VERSION(2, 31, 0))
93 g_thread_init(NULL);
94 #endif
96 // Flash has problems receiving clicks with newer GTKs due to the
97 // client-side windows change. To be safe, we just always set the
98 // backwards-compat environment variable.
99 setenv("GDK_NATIVE_WINDOWS", "1", 1);
101 gfx::GtkInitFromCommandLine(*CommandLine::ForCurrentProcess());
103 // GTK after 2.18 resets the environment variable. But if we're using
104 // nspluginwrapper, that means it'll spawn its subprocess without the
105 // environment variable! So set it again.
106 setenv("GDK_NATIVE_WINDOWS", "1", 1);
109 ui::SetDefaultX11ErrorHandlers();
110 #endif
112 PatchNPNFunctions();
114 // Preload the library to avoid loading, unloading then reloading
115 preloaded_plugin_module_ = base::LoadNativeLibrary(plugin_path, NULL);
117 scoped_refptr<PluginLib> plugin(PluginLib::CreatePluginLib(plugin_path));
118 if (plugin.get()) {
119 plugin->NP_Initialize();
120 // For OOP plugins the plugin dll will be unloaded during process shutdown
121 // time.
122 plugin->set_defer_unload(true);
125 GetContentClient()->plugin()->PluginProcessStarted(
126 plugin.get() ? plugin->plugin_info().name : base::string16());
128 channel()->AddFilter(new EnsureTerminateMessageFilter());
131 PluginThread::~PluginThread() {
134 void PluginThread::SetForcefullyTerminatePluginProcess() {
135 forcefully_terminate_plugin_process_ = true;
138 void PluginThread::Shutdown() {
139 ChildThread::Shutdown();
141 if (preloaded_plugin_module_) {
142 base::UnloadNativeLibrary(preloaded_plugin_module_);
143 preloaded_plugin_module_ = NULL;
145 NPChannelBase::CleanupChannels();
146 PluginLib::UnloadAllPlugins();
148 if (forcefully_terminate_plugin_process_)
149 base::KillProcess(base::GetCurrentProcessHandle(), 0, /* wait= */ false);
151 lazy_tls.Pointer()->Set(NULL);
154 PluginThread* PluginThread::current() {
155 return lazy_tls.Pointer()->Get();
158 bool PluginThread::OnControlMessageReceived(const IPC::Message& msg) {
159 bool handled = true;
160 IPC_BEGIN_MESSAGE_MAP(PluginThread, msg)
161 IPC_MESSAGE_HANDLER(PluginProcessMsg_CreateChannel, OnCreateChannel)
162 IPC_MESSAGE_HANDLER(PluginProcessMsg_NotifyRenderersOfPendingShutdown,
163 OnNotifyRenderersOfPendingShutdown)
164 IPC_MESSAGE_UNHANDLED(handled = false)
165 IPC_END_MESSAGE_MAP()
166 return handled;
169 void PluginThread::OnCreateChannel(int renderer_id,
170 bool incognito) {
171 scoped_refptr<PluginChannel> channel(PluginChannel::GetPluginChannel(
172 renderer_id, ChildProcess::current()->io_message_loop_proxy()));
173 IPC::ChannelHandle channel_handle;
174 if (channel.get()) {
175 channel_handle.name = channel->channel_handle().name;
176 #if defined(OS_POSIX)
177 // On POSIX, pass the renderer-side FD.
178 channel_handle.socket =
179 base::FileDescriptor(channel->TakeRendererFileDescriptor(), true);
180 #endif
181 channel->set_incognito(incognito);
184 Send(new PluginProcessHostMsg_ChannelCreated(channel_handle));
187 void PluginThread::OnNotifyRenderersOfPendingShutdown() {
188 PluginChannel::NotifyRenderersOfPendingShutdown();
191 } // namespace content