Fix build break
[chromium-blink-merge.git] / content / plugin / plugin_thread.cc
blobea0cbc11b7d6351e5b59cd85cfe9e73da77e9d20
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_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"
36 #endif
38 #if defined(USE_X11)
39 #include "ui/base/x/x11_util.h"
40 #endif
42 namespace content {
44 namespace {
46 class EnsureTerminateMessageFilter : public IPC::ChannelProxy::MessageFilter {
47 public:
48 EnsureTerminateMessageFilter() {}
50 protected:
51 virtual ~EnsureTerminateMessageFilter() {}
53 // IPC::ChannelProxy::MessageFilter:
54 virtual void OnChannelError() OVERRIDE {
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(
62 FROM_HERE,
63 base::Bind(&EnsureTerminateMessageFilter::Terminate, this),
64 kPluginProcessTerminateTimeout);
67 private:
68 void Terminate() {
69 base::KillProcess(base::GetCurrentProcessHandle(), 0, false);
73 } // namespace
75 static base::LazyInstance<base::ThreadLocalPointer<PluginThread> > lazy_tls =
76 LAZY_INSTANCE_INITIALIZER;
78 PluginThread::PluginThread()
79 : preloaded_plugin_module_(NULL) {
80 base::FilePath plugin_path =
81 CommandLine::ForCurrentProcess()->GetSwitchValuePath(
82 switches::kPluginPath);
84 lazy_tls.Pointer()->Set(this);
85 #if defined(USE_AURA)
86 // TODO(saintlou):
87 #elif defined(TOOLKIT_GTK)
89 // XEmbed plugins assume they are hosted in a Gtk application, so we need
90 // to initialize Gtk in the plugin process.
91 // g_thread_init API is deprecated since glib 2.31.0, see release note:
92 // http://mail.gnome.org/archives/gnome-announce-list/2011-October/msg00041.html
93 #if !(GLIB_CHECK_VERSION(2, 31, 0))
94 g_thread_init(NULL);
95 #endif
97 // Flash has problems receiving clicks with newer GTKs due to the
98 // client-side windows change. To be safe, we just always set the
99 // backwards-compat environment variable.
100 setenv("GDK_NATIVE_WINDOWS", "1", 1);
102 gfx::GtkInitFromCommandLine(*CommandLine::ForCurrentProcess());
104 // GTK after 2.18 resets the environment variable. But if we're using
105 // nspluginwrapper, that means it'll spawn its subprocess without the
106 // environment variable! So set it again.
107 setenv("GDK_NATIVE_WINDOWS", "1", 1);
110 ui::SetDefaultX11ErrorHandlers();
111 #endif
113 PatchNPNFunctions();
115 // Preload the library to avoid loading, unloading then reloading
116 preloaded_plugin_module_ = base::LoadNativeLibrary(plugin_path, NULL);
118 scoped_refptr<webkit::npapi::PluginLib> plugin(
119 webkit::npapi::PluginLib::CreatePluginLib(plugin_path));
120 if (plugin.get()) {
121 plugin->NP_Initialize();
122 // For OOP plugins the plugin dll will be unloaded during process shutdown
123 // time.
124 plugin->set_defer_unload(true);
127 GetContentClient()->plugin()->PluginProcessStarted(
128 plugin.get() ? plugin->plugin_info().name : string16());
130 GetContentClient()->AddNPAPIPlugins(
131 webkit::npapi::PluginList::Singleton());
133 // Certain plugins, such as flash, steal the unhandled exception filter
134 // thus we never get crash reports when they fault. This call fixes it.
135 message_loop()->set_exception_restoration(true);
136 channel()->AddFilter(new EnsureTerminateMessageFilter());
139 PluginThread::~PluginThread() {
140 if (preloaded_plugin_module_) {
141 base::UnloadNativeLibrary(preloaded_plugin_module_);
142 preloaded_plugin_module_ = NULL;
144 NPChannelBase::CleanupChannels();
145 webkit::npapi::PluginLib::UnloadAllPlugins();
147 if (webkit_glue::ShouldForcefullyTerminatePluginProcess())
148 base::KillProcess(base::GetCurrentProcessHandle(), 0, /* wait= */ false);
150 lazy_tls.Pointer()->Set(NULL);
153 PluginThread* PluginThread::current() {
154 return lazy_tls.Pointer()->Get();
157 bool PluginThread::OnControlMessageReceived(const IPC::Message& msg) {
158 bool handled = true;
159 IPC_BEGIN_MESSAGE_MAP(PluginThread, msg)
160 IPC_MESSAGE_HANDLER(PluginProcessMsg_CreateChannel, OnCreateChannel)
161 IPC_MESSAGE_HANDLER(PluginProcessMsg_NotifyRenderersOfPendingShutdown,
162 OnNotifyRenderersOfPendingShutdown)
163 IPC_MESSAGE_UNHANDLED(handled = false)
164 IPC_END_MESSAGE_MAP()
165 return handled;
168 void PluginThread::OnCreateChannel(int renderer_id,
169 bool incognito) {
170 scoped_refptr<PluginChannel> channel(PluginChannel::GetPluginChannel(
171 renderer_id, ChildProcess::current()->io_message_loop_proxy()));
172 IPC::ChannelHandle channel_handle;
173 if (channel.get()) {
174 channel_handle.name = channel->channel_handle().name;
175 #if defined(OS_POSIX)
176 // On POSIX, pass the renderer-side FD.
177 channel_handle.socket =
178 base::FileDescriptor(channel->TakeRendererFileDescriptor(), true);
179 #endif
180 channel->set_incognito(incognito);
183 Send(new PluginProcessHostMsg_ChannelCreated(channel_handle));
186 void PluginThread::OnNotifyRenderersOfPendingShutdown() {
187 PluginChannel::NotifyRenderersOfPendingShutdown();
190 } // namespace content