| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/plugin/plugin_thread.h" | 5 #include "chrome/plugin/plugin_thread.h" |
| 6 | 6 |
| 7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
| 8 | 8 |
| 9 #if defined(OS_WIN) |
| 10 #include <windows.h> |
| 11 #include <objbase.h> |
| 12 #endif |
| 13 |
| 9 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 10 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
| 11 #include "base/process_util.h" | 16 #include "base/process_util.h" |
| 12 #include "base/thread_local.h" | 17 #include "base/thread_local.h" |
| 13 #include "chrome/common/child_process.h" | 18 #include "chrome/common/child_process.h" |
| 14 #include "chrome/common/chrome_plugin_lib.h" | 19 #include "chrome/common/chrome_plugin_lib.h" |
| 15 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
| 21 #include "chrome/common/notification_service.h" |
| 16 #include "chrome/common/plugin_messages.h" | 22 #include "chrome/common/plugin_messages.h" |
| 17 #include "chrome/common/render_messages.h" | 23 #include "chrome/common/render_messages.h" |
| 18 #include "chrome/plugin/chrome_plugin_host.h" | 24 #include "chrome/plugin/chrome_plugin_host.h" |
| 19 #include "chrome/plugin/npobject_util.h" | 25 #include "chrome/plugin/npobject_util.h" |
| 20 #include "chrome/renderer/render_thread.h" | 26 #include "chrome/renderer/render_thread.h" |
| 21 #include "net/base/net_errors.h" | 27 #include "net/base/net_errors.h" |
| 22 #include "webkit/glue/plugins/plugin_lib.h" | 28 #include "webkit/glue/plugins/plugin_lib.h" |
| 23 #include "webkit/glue/webkit_glue.h" | 29 #include "webkit/glue/webkit_glue.h" |
| 24 | 30 |
| 25 static base::LazyInstance<base::ThreadLocalPointer<PluginThread> > lazy_tls( | 31 static base::LazyInstance<base::ThreadLocalPointer<PluginThread> > lazy_tls( |
| 26 base::LINKER_INITIALIZED); | 32 base::LINKER_INITIALIZED); |
| 27 | 33 |
| 28 PluginThread::PluginThread() | 34 PluginThread::PluginThread() |
| 29 : preloaded_plugin_module_(NULL) { | 35 : ChildThread(base::Thread::Options(MessageLoop::TYPE_UI, 0)), |
| 36 preloaded_plugin_module_(NULL) { |
| 30 plugin_path_ = FilePath::FromWStringHack( | 37 plugin_path_ = FilePath::FromWStringHack( |
| 31 CommandLine::ForCurrentProcess()->GetSwitchValue(switches::kPluginPath)); | 38 CommandLine::ForCurrentProcess()->GetSwitchValue(switches::kPluginPath)); |
| 39 } |
| 32 | 40 |
| 41 PluginThread::~PluginThread() { |
| 42 } |
| 43 |
| 44 PluginThread* PluginThread::current() { |
| 45 return lazy_tls.Pointer()->Get(); |
| 46 } |
| 47 |
| 48 void PluginThread::OnControlMessageReceived(const IPC::Message& msg) { |
| 49 IPC_BEGIN_MESSAGE_MAP(PluginThread, msg) |
| 50 IPC_MESSAGE_HANDLER(PluginProcessMsg_CreateChannel, OnCreateChannel) |
| 51 IPC_MESSAGE_HANDLER(PluginProcessMsg_PluginMessage, OnPluginMessage) |
| 52 IPC_END_MESSAGE_MAP() |
| 53 } |
| 54 |
| 55 void PluginThread::Init() { |
| 33 lazy_tls.Pointer()->Set(this); | 56 lazy_tls.Pointer()->Set(this); |
| 34 #if defined(OS_LINUX) | 57 #if defined(OS_LINUX) |
| 35 { | 58 { |
| 36 // XEmbed plugins assume they are hosted in a Gtk application, so we need | 59 // XEmbed plugins assume they are hosted in a Gtk application, so we need |
| 37 // to initialize Gtk in the plugin process. | 60 // to initialize Gtk in the plugin process. |
| 38 const std::vector<std::string>& args = | 61 const std::vector<std::string>& args = |
| 39 CommandLine::ForCurrentProcess()->argv(); | 62 CommandLine::ForCurrentProcess()->argv(); |
| 40 int argc = args.size(); | 63 int argc = args.size(); |
| 41 scoped_array<char *> argv(new char *[argc + 1]); | 64 scoped_array<char *> argv(new char *[argc + 1]); |
| 42 for (size_t i = 0; i < args.size(); ++i) { | 65 for (size_t i = 0; i < args.size(); ++i) { |
| 43 // TODO(piman@google.com): can gtk_init modify argv? Just being safe | 66 // TODO(piman@google.com): can gtk_init modify argv? Just being safe |
| 44 // here. | 67 // here. |
| 45 argv[i] = strdup(args[i].c_str()); | 68 argv[i] = strdup(args[i].c_str()); |
| 46 } | 69 } |
| 47 argv[argc] = NULL; | 70 argv[argc] = NULL; |
| 48 char **argv_pointer = argv.get(); | 71 char **argv_pointer = argv.get(); |
| 49 gtk_init(&argc, &argv_pointer); | 72 gtk_init(&argc, &argv_pointer); |
| 50 for (size_t i = 0; i < args.size(); ++i) { | 73 for (size_t i = 0; i < args.size(); ++i) { |
| 51 free(argv[i]); | 74 free(argv[i]); |
| 52 } | 75 } |
| 53 } | 76 } |
| 54 #endif | 77 #endif |
| 78 ChildThread::Init(); |
| 55 | 79 |
| 56 PatchNPNFunctions(); | 80 PatchNPNFunctions(); |
| 81 #if defined(OS_WIN) |
| 82 CoInitialize(NULL); |
| 83 #endif |
| 84 |
| 85 notification_service_.reset(new NotificationService); |
| 57 | 86 |
| 58 // Preload the library to avoid loading, unloading then reloading | 87 // Preload the library to avoid loading, unloading then reloading |
| 59 preloaded_plugin_module_ = base::LoadNativeLibrary(plugin_path_); | 88 preloaded_plugin_module_ = base::LoadNativeLibrary(plugin_path_); |
| 60 | 89 |
| 61 ChromePluginLib::Create(plugin_path_, GetCPBrowserFuncsForPlugin()); | 90 ChromePluginLib::Create(plugin_path_, GetCPBrowserFuncsForPlugin()); |
| 62 | 91 |
| 63 scoped_refptr<NPAPI::PluginLib> plugin = | 92 scoped_refptr<NPAPI::PluginLib> plugin = |
| 64 NPAPI::PluginLib::CreatePluginLib(plugin_path_); | 93 NPAPI::PluginLib::CreatePluginLib(plugin_path_); |
| 65 if (plugin.get()) { | 94 if (plugin.get()) { |
| 66 plugin->NP_Initialize(); | 95 plugin->NP_Initialize(); |
| 67 } | 96 } |
| 68 | 97 |
| 69 // Certain plugins, such as flash, steal the unhandled exception filter | 98 // Certain plugins, such as flash, steal the unhandled exception filter |
| 70 // thus we never get crash reports when they fault. This call fixes it. | 99 // thus we never get crash reports when they fault. This call fixes it. |
| 71 message_loop()->set_exception_restoration(true); | 100 message_loop()->set_exception_restoration(true); |
| 72 } | 101 } |
| 73 | 102 |
| 74 PluginThread::~PluginThread() { | 103 void PluginThread::CleanUp() { |
| 75 if (preloaded_plugin_module_) { | 104 if (preloaded_plugin_module_) { |
| 76 base::UnloadNativeLibrary(preloaded_plugin_module_); | 105 base::UnloadNativeLibrary(preloaded_plugin_module_); |
| 77 preloaded_plugin_module_ = NULL; | 106 preloaded_plugin_module_ = NULL; |
| 78 } | 107 } |
| 79 PluginChannelBase::CleanupChannels(); | 108 PluginChannelBase::CleanupChannels(); |
| 80 NPAPI::PluginLib::UnloadAllPlugins(); | 109 NPAPI::PluginLib::UnloadAllPlugins(); |
| 81 ChromePluginLib::UnloadAllPlugins(); | 110 ChromePluginLib::UnloadAllPlugins(); |
| 111 notification_service_.reset(); |
| 112 #if defined(OS_WIN) |
| 113 CoUninitialize(); |
| 114 #endif |
| 82 | 115 |
| 83 if (webkit_glue::ShouldForcefullyTerminatePluginProcess()) | 116 if (webkit_glue::ShouldForcefullyTerminatePluginProcess()) |
| 84 base::KillProcess(base::GetCurrentProcessHandle(), 0, /* wait= */ false); | 117 base::KillProcess(base::GetCurrentProcessHandle(), 0, /* wait= */ false); |
| 85 | 118 |
| 119 // Call this last because it deletes the ResourceDispatcher, which is used |
| 120 // in some of the above cleanup. |
| 121 // See http://code.google.com/p/chromium/issues/detail?id=8980 |
| 122 ChildThread::CleanUp(); |
| 86 lazy_tls.Pointer()->Set(NULL); | 123 lazy_tls.Pointer()->Set(NULL); |
| 87 } | 124 } |
| 88 | 125 |
| 89 PluginThread* PluginThread::current() { | |
| 90 return lazy_tls.Pointer()->Get(); | |
| 91 } | |
| 92 | |
| 93 void PluginThread::OnControlMessageReceived(const IPC::Message& msg) { | |
| 94 IPC_BEGIN_MESSAGE_MAP(PluginThread, msg) | |
| 95 IPC_MESSAGE_HANDLER(PluginProcessMsg_CreateChannel, OnCreateChannel) | |
| 96 IPC_MESSAGE_HANDLER(PluginProcessMsg_PluginMessage, OnPluginMessage) | |
| 97 IPC_END_MESSAGE_MAP() | |
| 98 } | |
| 99 | |
| 100 void PluginThread::OnCreateChannel( | 126 void PluginThread::OnCreateChannel( |
| 101 int process_id, | 127 int process_id, |
| 102 bool off_the_record) { | 128 bool off_the_record) { |
| 103 scoped_refptr<PluginChannel> channel = PluginChannel::GetPluginChannel( | 129 scoped_refptr<PluginChannel> channel = |
| 104 process_id, ChildProcess::current()->io_message_loop()); | 130 PluginChannel::GetPluginChannel(process_id, owner_loop()); |
| 105 IPC::ChannelHandle channel_handle; | 131 IPC::ChannelHandle channel_handle; |
| 106 if (channel.get()) { | 132 if (channel.get()) { |
| 107 channel_handle.name = channel->channel_name(); | 133 channel_handle.name = channel->channel_name(); |
| 108 #if defined(OS_POSIX) | 134 #if defined(OS_POSIX) |
| 109 // On POSIX, pass the renderer-side FD. Also mark it as auto-close so that | 135 // On POSIX, pass the renderer-side FD. Also mark it as auto-close so that |
| 110 // it gets closed after it has been sent. | 136 // it gets closed after it has been sent. |
| 111 int renderer_fd = channel->DisownRendererFd(); | 137 int renderer_fd = channel->DisownRendererFd(); |
| 112 channel_handle.socket = base::FileDescriptor(renderer_fd, true); | 138 channel_handle.socket = base::FileDescriptor(renderer_fd, true); |
| 113 #endif | 139 #endif |
| 114 channel->set_off_the_record(off_the_record); | 140 channel->set_off_the_record(off_the_record); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 } | 218 } |
| 193 | 219 |
| 194 if (!result || net_error != net::OK) | 220 if (!result || net_error != net::OK) |
| 195 return false; | 221 return false; |
| 196 | 222 |
| 197 *proxy_list = proxy_result; | 223 *proxy_list = proxy_result; |
| 198 return true; | 224 return true; |
| 199 } | 225 } |
| 200 | 226 |
| 201 } // namespace webkit_glue | 227 } // namespace webkit_glue |
| OLD | NEW |