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"
10 #include <CoreFoundation/CoreFoundation.h>
16 #include "base/bind.h"
17 #include "base/command_line.h"
18 #include "base/lazy_instance.h"
19 #include "base/process/kill.h"
20 #include "base/process/process_handle.h"
21 #include "base/threading/thread_local.h"
22 #include "content/child/blink_platform_impl.h"
23 #include "content/child/child_process.h"
24 #include "content/child/npapi/npobject_util.h"
25 #include "content/child/npapi/plugin_lib.h"
26 #include "content/common/plugin_process_messages.h"
27 #include "content/public/common/content_switches.h"
28 #include "content/public/plugin/content_plugin_client.h"
29 #include "third_party/WebKit/public/web/WebKit.h"
30 #include "ipc/ipc_channel_handle.h"
31 #include "ipc/message_filter.h"
37 class EnsureTerminateMessageFilter
: public IPC::MessageFilter
{
39 EnsureTerminateMessageFilter() {}
42 virtual ~EnsureTerminateMessageFilter() {}
44 // IPC::MessageFilter:
45 virtual void OnChannelError() OVERRIDE
{
46 // How long we wait before forcibly shutting down the process.
47 const base::TimeDelta kPluginProcessTerminateTimeout
=
48 base::TimeDelta::FromSeconds(3);
49 // Ensure that we don't wait indefinitely for the plugin to shutdown.
50 // as the browser does not terminate plugin processes on shutdown.
51 // We achieve this by posting an exit process task on the IO thread.
52 base::MessageLoop::current()->PostDelayedTask(
54 base::Bind(&EnsureTerminateMessageFilter::Terminate
, this),
55 kPluginProcessTerminateTimeout
);
60 base::KillProcess(base::GetCurrentProcessHandle(), 0, false);
66 static base::LazyInstance
<base::ThreadLocalPointer
<PluginThread
> > lazy_tls
=
67 LAZY_INSTANCE_INITIALIZER
;
69 PluginThread::PluginThread()
70 : preloaded_plugin_module_(NULL
),
71 forcefully_terminate_plugin_process_(false) {
72 base::FilePath plugin_path
=
73 CommandLine::ForCurrentProcess()->GetSwitchValuePath(
74 switches::kPluginPath
);
76 lazy_tls
.Pointer()->Set(this);
80 // Preload the library to avoid loading, unloading then reloading
81 preloaded_plugin_module_
= base::LoadNativeLibrary(plugin_path
, NULL
);
83 scoped_refptr
<PluginLib
> plugin(PluginLib::CreatePluginLib(plugin_path
));
85 plugin
->NP_Initialize();
86 // For OOP plugins the plugin dll will be unloaded during process shutdown
88 plugin
->set_defer_unload(true);
91 GetContentClient()->plugin()->PluginProcessStarted(
92 plugin
.get() ? plugin
->plugin_info().name
: base::string16());
94 channel()->AddFilter(new EnsureTerminateMessageFilter());
96 // This is needed because we call some code which uses WebKit strings.
97 webkit_platform_support_
.reset(new BlinkPlatformImpl
);
98 blink::initialize(webkit_platform_support_
.get());
101 PluginThread::~PluginThread() {
104 void PluginThread::SetForcefullyTerminatePluginProcess() {
105 forcefully_terminate_plugin_process_
= true;
108 void PluginThread::Shutdown() {
109 ChildThread::Shutdown();
111 if (preloaded_plugin_module_
) {
112 base::UnloadNativeLibrary(preloaded_plugin_module_
);
113 preloaded_plugin_module_
= NULL
;
115 NPChannelBase::CleanupChannels();
116 PluginLib::UnloadAllPlugins();
118 if (forcefully_terminate_plugin_process_
)
119 base::KillProcess(base::GetCurrentProcessHandle(), 0, /* wait= */ false);
121 lazy_tls
.Pointer()->Set(NULL
);
124 PluginThread
* PluginThread::current() {
125 return lazy_tls
.Pointer()->Get();
128 bool PluginThread::OnControlMessageReceived(const IPC::Message
& msg
) {
130 IPC_BEGIN_MESSAGE_MAP(PluginThread
, msg
)
131 IPC_MESSAGE_HANDLER(PluginProcessMsg_CreateChannel
, OnCreateChannel
)
132 IPC_MESSAGE_HANDLER(PluginProcessMsg_NotifyRenderersOfPendingShutdown
,
133 OnNotifyRenderersOfPendingShutdown
)
134 IPC_MESSAGE_UNHANDLED(handled
= false)
135 IPC_END_MESSAGE_MAP()
139 void PluginThread::OnCreateChannel(int renderer_id
,
141 scoped_refptr
<PluginChannel
> channel(PluginChannel::GetPluginChannel(
142 renderer_id
, ChildProcess::current()->io_message_loop_proxy()));
143 IPC::ChannelHandle channel_handle
;
145 channel_handle
.name
= channel
->channel_handle().name
;
146 #if defined(OS_POSIX)
147 // On POSIX, pass the renderer-side FD.
148 channel_handle
.socket
=
149 base::FileDescriptor(channel
->TakeRendererFileDescriptor(), true);
151 channel
->set_incognito(incognito
);
154 Send(new PluginProcessHostMsg_ChannelCreated(channel_handle
));
157 void PluginThread::OnNotifyRenderersOfPendingShutdown() {
158 PluginChannel::NotifyRenderersOfPendingShutdown();
161 } // namespace content