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)
11 #elif defined(OS_MACOSX)
12 #include <CoreFoundation/CoreFoundation.h>
18 #include "base/bind.h"
19 #include "base/command_line.h"
20 #include "base/lazy_instance.h"
21 #include "base/process_util.h"
22 #include "base/threading/thread_local.h"
23 #include "content/common/child_process.h"
24 #include "content/common/npobject_util.h"
25 #include "content/common/plugin_messages.h"
26 #include "content/public/common/content_switches.h"
27 #include "content/public/plugin/content_plugin_client.h"
28 #include "ipc/ipc_channel_handle.h"
29 #include "webkit/glue/webkit_glue.h"
30 #include "webkit/plugins/npapi/plugin_lib.h"
31 #include "webkit/plugins/npapi/plugin_list.h"
32 #include "webkit/plugins/npapi/webplugin_delegate_impl.h"
34 #if defined(TOOLKIT_GTK)
35 #include "ui/gfx/gtk_util.h"
39 #include "ui/base/x/x11_util.h"
46 class EnsureTerminateMessageFilter
: public IPC::ChannelProxy::MessageFilter
{
48 EnsureTerminateMessageFilter() {}
51 virtual ~EnsureTerminateMessageFilter() {}
53 // IPC::ChannelProxy::MessageFilter:
54 virtual void OnChannelError() {
55 // How long we wait before forcibly shutting down the process.
56 const base::TimeDelta kPluginProcessTerminateTimeout
=
57 base::TimeDelta::FromSeconds(3);
58 // Ensure that we don't wait indefinitely for the plugin to shutdown.
59 // as the browser does not terminate plugin processes on shutdown.
60 // We achieve this by posting an exit process task on the IO thread.
61 MessageLoop::current()->PostDelayedTask(
63 base::Bind(&EnsureTerminateMessageFilter::Terminate
, this),
64 kPluginProcessTerminateTimeout
);
69 base::KillProcess(base::GetCurrentProcessHandle(), 0, false);
75 static base::LazyInstance
<base::ThreadLocalPointer
<PluginThread
> > lazy_tls
=
76 LAZY_INSTANCE_INITIALIZER
;
78 PluginThread::PluginThread()
79 : preloaded_plugin_module_(NULL
) {
80 FilePath plugin_path
= CommandLine::ForCurrentProcess()->GetSwitchValuePath(
81 switches::kPluginPath
);
83 lazy_tls
.Pointer()->Set(this);
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))
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();
114 // Preload the library to avoid loading, unloading then reloading
115 preloaded_plugin_module_
= base::LoadNativeLibrary(plugin_path
, NULL
);
117 scoped_refptr
<webkit::npapi::PluginLib
> plugin(
118 webkit::npapi::PluginLib::CreatePluginLib(plugin_path
));
120 plugin
->NP_Initialize();
121 // For OOP plugins the plugin dll will be unloaded during process shutdown
123 plugin
->set_defer_unload(true);
126 GetContentClient()->plugin()->PluginProcessStarted(
127 plugin
.get() ? plugin
->plugin_info().name
: string16());
129 GetContentClient()->AddNPAPIPlugins(
130 webkit::npapi::PluginList::Singleton());
132 // Certain plugins, such as flash, steal the unhandled exception filter
133 // thus we never get crash reports when they fault. This call fixes it.
134 message_loop()->set_exception_restoration(true);
135 channel()->AddFilter(new EnsureTerminateMessageFilter());
138 PluginThread::~PluginThread() {
139 if (preloaded_plugin_module_
) {
140 base::UnloadNativeLibrary(preloaded_plugin_module_
);
141 preloaded_plugin_module_
= NULL
;
143 NPChannelBase::CleanupChannels();
144 webkit::npapi::PluginLib::UnloadAllPlugins();
146 if (webkit_glue::ShouldForcefullyTerminatePluginProcess())
147 base::KillProcess(base::GetCurrentProcessHandle(), 0, /* wait= */ false);
149 lazy_tls
.Pointer()->Set(NULL
);
152 PluginThread
* PluginThread::current() {
153 return lazy_tls
.Pointer()->Get();
156 bool PluginThread::OnControlMessageReceived(const IPC::Message
& msg
) {
158 IPC_BEGIN_MESSAGE_MAP(PluginThread
, msg
)
159 IPC_MESSAGE_HANDLER(PluginProcessMsg_CreateChannel
, OnCreateChannel
)
160 IPC_MESSAGE_HANDLER(PluginProcessMsg_NotifyRenderersOfPendingShutdown
,
161 OnNotifyRenderersOfPendingShutdown
)
162 IPC_MESSAGE_UNHANDLED(handled
= false)
163 IPC_END_MESSAGE_MAP()
167 void PluginThread::OnCreateChannel(int renderer_id
,
169 scoped_refptr
<PluginChannel
> channel(PluginChannel::GetPluginChannel(
170 renderer_id
, ChildProcess::current()->io_message_loop_proxy()));
171 IPC::ChannelHandle channel_handle
;
173 channel_handle
.name
= channel
->channel_handle().name
;
174 #if defined(OS_POSIX)
175 // On POSIX, pass the renderer-side FD.
176 channel_handle
.socket
=
177 base::FileDescriptor(channel
->TakeRendererFileDescriptor(), true);
179 channel
->set_incognito(incognito
);
182 Send(new PluginProcessHostMsg_ChannelCreated(channel_handle
));
185 void PluginThread::OnNotifyRenderersOfPendingShutdown() {
186 PluginChannel::NotifyRenderersOfPendingShutdown();
189 } // namespace content