| OLD | NEW |
| (Empty) |
| 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/plugin/plugin_thread.h" | |
| 6 | |
| 7 #include "build/build_config.h" | |
| 8 | |
| 9 #if defined(OS_MACOSX) | |
| 10 #include <CoreFoundation/CoreFoundation.h> | |
| 11 #endif | |
| 12 | |
| 13 #include <string> | |
| 14 #include <vector> | |
| 15 | |
| 16 #include "base/bind.h" | |
| 17 #include "base/command_line.h" | |
| 18 #include "base/lazy_instance.h" | |
| 19 #include "base/process/process.h" | |
| 20 #include "base/threading/thread_local.h" | |
| 21 #include "content/child/blink_platform_impl.h" | |
| 22 #include "content/child/child_process.h" | |
| 23 #include "content/child/npapi/plugin_lib.h" | |
| 24 #include "content/common/plugin_process_messages.h" | |
| 25 #include "content/public/common/content_switches.h" | |
| 26 #include "content/public/plugin/content_plugin_client.h" | |
| 27 #include "third_party/WebKit/public/web/WebKit.h" | |
| 28 #include "ipc/ipc_channel_handle.h" | |
| 29 #include "ipc/message_filter.h" | |
| 30 | |
| 31 namespace content { | |
| 32 | |
| 33 namespace { | |
| 34 | |
| 35 class EnsureTerminateMessageFilter : public IPC::MessageFilter { | |
| 36 public: | |
| 37 EnsureTerminateMessageFilter() {} | |
| 38 | |
| 39 protected: | |
| 40 ~EnsureTerminateMessageFilter() override {} | |
| 41 | |
| 42 // IPC::MessageFilter: | |
| 43 void OnChannelError() override { | |
| 44 // How long we wait before forcibly shutting down the process. | |
| 45 const base::TimeDelta kPluginProcessTerminateTimeout = | |
| 46 base::TimeDelta::FromSeconds(3); | |
| 47 // Ensure that we don't wait indefinitely for the plugin to shutdown. | |
| 48 // as the browser does not terminate plugin processes on shutdown. | |
| 49 // We achieve this by posting an exit process task on the IO thread. | |
| 50 base::MessageLoop::current()->PostDelayedTask( | |
| 51 FROM_HERE, | |
| 52 base::Bind(&EnsureTerminateMessageFilter::Terminate, this), | |
| 53 kPluginProcessTerminateTimeout); | |
| 54 } | |
| 55 | |
| 56 private: | |
| 57 void Terminate() { | |
| 58 base::Process::Current().Terminate(0, false); | |
| 59 } | |
| 60 }; | |
| 61 | |
| 62 } // namespace | |
| 63 | |
| 64 static base::LazyInstance<base::ThreadLocalPointer<PluginThread> > lazy_tls = | |
| 65 LAZY_INSTANCE_INITIALIZER; | |
| 66 | |
| 67 PluginThread::PluginThread() | |
| 68 : preloaded_plugin_module_(NULL), | |
| 69 forcefully_terminate_plugin_process_(false) { | |
| 70 base::FilePath plugin_path = | |
| 71 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath( | |
| 72 switches::kPluginPath); | |
| 73 | |
| 74 lazy_tls.Pointer()->Set(this); | |
| 75 | |
| 76 // Preload the library to avoid loading, unloading then reloading | |
| 77 preloaded_plugin_module_ = base::LoadNativeLibrary(plugin_path, NULL); | |
| 78 | |
| 79 scoped_refptr<PluginLib> plugin(PluginLib::CreatePluginLib(plugin_path)); | |
| 80 if (plugin.get()) { | |
| 81 plugin->NP_Initialize(); | |
| 82 // For OOP plugins the plugin dll will be unloaded during process shutdown | |
| 83 // time. | |
| 84 plugin->set_defer_unload(true); | |
| 85 } | |
| 86 | |
| 87 GetContentClient()->plugin()->PluginProcessStarted( | |
| 88 plugin.get() ? plugin->plugin_info().name : base::string16()); | |
| 89 | |
| 90 channel()->AddFilter(new EnsureTerminateMessageFilter()); | |
| 91 | |
| 92 // This is needed because we call some code which uses Blink strings. | |
| 93 blink_platform_impl_.reset(new BlinkPlatformImpl); | |
| 94 blink::initialize(blink_platform_impl_.get()); | |
| 95 } | |
| 96 | |
| 97 PluginThread::~PluginThread() { | |
| 98 } | |
| 99 | |
| 100 void PluginThread::SetForcefullyTerminatePluginProcess() { | |
| 101 forcefully_terminate_plugin_process_ = true; | |
| 102 } | |
| 103 | |
| 104 void PluginThread::Shutdown() { | |
| 105 ChildThreadImpl::Shutdown(); | |
| 106 | |
| 107 if (preloaded_plugin_module_) { | |
| 108 base::UnloadNativeLibrary(preloaded_plugin_module_); | |
| 109 preloaded_plugin_module_ = NULL; | |
| 110 } | |
| 111 NPChannelBase::CleanupChannels(); | |
| 112 PluginLib::UnloadAllPlugins(); | |
| 113 | |
| 114 if (forcefully_terminate_plugin_process_) | |
| 115 base::Process::Current().Terminate(0, /* wait= */ false); | |
| 116 | |
| 117 lazy_tls.Pointer()->Set(NULL); | |
| 118 } | |
| 119 | |
| 120 PluginThread* PluginThread::current() { | |
| 121 return lazy_tls.Pointer()->Get(); | |
| 122 } | |
| 123 | |
| 124 bool PluginThread::OnControlMessageReceived(const IPC::Message& msg) { | |
| 125 bool handled = true; | |
| 126 IPC_BEGIN_MESSAGE_MAP(PluginThread, msg) | |
| 127 IPC_MESSAGE_HANDLER(PluginProcessMsg_CreateChannel, OnCreateChannel) | |
| 128 IPC_MESSAGE_HANDLER(PluginProcessMsg_NotifyRenderersOfPendingShutdown, | |
| 129 OnNotifyRenderersOfPendingShutdown) | |
| 130 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 131 IPC_END_MESSAGE_MAP() | |
| 132 return handled; | |
| 133 } | |
| 134 | |
| 135 void PluginThread::OnCreateChannel(int renderer_id, | |
| 136 bool incognito) { | |
| 137 scoped_refptr<PluginChannel> channel(PluginChannel::GetPluginChannel( | |
| 138 renderer_id, ChildProcess::current()->io_task_runner())); | |
| 139 IPC::ChannelHandle channel_handle; | |
| 140 if (channel.get()) { | |
| 141 channel_handle.name = channel->channel_handle().name; | |
| 142 #if defined(OS_POSIX) | |
| 143 // On POSIX, pass the renderer-side FD. | |
| 144 channel_handle.socket = | |
| 145 base::FileDescriptor(channel->TakeRendererFileDescriptor()); | |
| 146 #endif | |
| 147 channel->set_incognito(incognito); | |
| 148 } | |
| 149 | |
| 150 Send(new PluginProcessHostMsg_ChannelCreated(channel_handle)); | |
| 151 } | |
| 152 | |
| 153 void PluginThread::OnNotifyRenderersOfPendingShutdown() { | |
| 154 PluginChannel::NotifyRenderersOfPendingShutdown(); | |
| 155 } | |
| 156 | |
| 157 } // namespace content | |
| OLD | NEW |