Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(95)

Side by Side Diff: chrome/plugin/plugin_thread.cc

Issue 155944: Switch the first thread in a child process to be the main thread... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: sync Created 11 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
14 #include "base/command_line.h" 9 #include "base/command_line.h"
15 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
16 #include "base/process_util.h" 11 #include "base/process_util.h"
17 #include "base/thread_local.h" 12 #include "base/thread_local.h"
18 #include "chrome/common/child_process.h" 13 #include "chrome/common/child_process.h"
19 #include "chrome/common/chrome_plugin_lib.h" 14 #include "chrome/common/chrome_plugin_lib.h"
20 #include "chrome/common/chrome_switches.h" 15 #include "chrome/common/chrome_switches.h"
21 #include "chrome/common/notification_service.h"
22 #include "chrome/common/plugin_messages.h" 16 #include "chrome/common/plugin_messages.h"
23 #include "chrome/common/render_messages.h" 17 #include "chrome/common/render_messages.h"
24 #include "chrome/plugin/chrome_plugin_host.h" 18 #include "chrome/plugin/chrome_plugin_host.h"
25 #include "chrome/plugin/npobject_util.h" 19 #include "chrome/plugin/npobject_util.h"
26 #include "chrome/renderer/render_thread.h" 20 #include "chrome/renderer/render_thread.h"
27 #include "net/base/net_errors.h" 21 #include "net/base/net_errors.h"
28 #include "webkit/glue/plugins/plugin_lib.h" 22 #include "webkit/glue/plugins/plugin_lib.h"
29 #include "webkit/glue/webkit_glue.h" 23 #include "webkit/glue/webkit_glue.h"
30 24
31 static base::LazyInstance<base::ThreadLocalPointer<PluginThread> > lazy_tls( 25 static base::LazyInstance<base::ThreadLocalPointer<PluginThread> > lazy_tls(
32 base::LINKER_INITIALIZED); 26 base::LINKER_INITIALIZED);
33 27
34 PluginThread::PluginThread() 28 PluginThread::PluginThread()
35 : ChildThread(base::Thread::Options(MessageLoop::TYPE_UI, 0)), 29 : preloaded_plugin_module_(NULL) {
36 preloaded_plugin_module_(NULL) {
37 plugin_path_ = FilePath::FromWStringHack( 30 plugin_path_ = FilePath::FromWStringHack(
38 CommandLine::ForCurrentProcess()->GetSwitchValue(switches::kPluginPath)); 31 CommandLine::ForCurrentProcess()->GetSwitchValue(switches::kPluginPath));
39 }
40 32
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() {
56 lazy_tls.Pointer()->Set(this); 33 lazy_tls.Pointer()->Set(this);
57 #if defined(OS_LINUX) 34 #if defined(OS_LINUX)
58 { 35 {
59 // XEmbed plugins assume they are hosted in a Gtk application, so we need 36 // XEmbed plugins assume they are hosted in a Gtk application, so we need
60 // to initialize Gtk in the plugin process. 37 // to initialize Gtk in the plugin process.
61 const std::vector<std::string>& args = 38 const std::vector<std::string>& args =
62 CommandLine::ForCurrentProcess()->argv(); 39 CommandLine::ForCurrentProcess()->argv();
63 int argc = args.size(); 40 int argc = args.size();
64 scoped_array<char *> argv(new char *[argc + 1]); 41 scoped_array<char *> argv(new char *[argc + 1]);
65 for (size_t i = 0; i < args.size(); ++i) { 42 for (size_t i = 0; i < args.size(); ++i) {
66 // TODO(piman@google.com): can gtk_init modify argv? Just being safe 43 // TODO(piman@google.com): can gtk_init modify argv? Just being safe
67 // here. 44 // here.
68 argv[i] = strdup(args[i].c_str()); 45 argv[i] = strdup(args[i].c_str());
69 } 46 }
70 argv[argc] = NULL; 47 argv[argc] = NULL;
71 char **argv_pointer = argv.get(); 48 char **argv_pointer = argv.get();
72 gtk_init(&argc, &argv_pointer); 49 gtk_init(&argc, &argv_pointer);
73 for (size_t i = 0; i < args.size(); ++i) { 50 for (size_t i = 0; i < args.size(); ++i) {
74 free(argv[i]); 51 free(argv[i]);
75 } 52 }
76 } 53 }
77 #endif 54 #endif
78 ChildThread::Init();
79 55
80 PatchNPNFunctions(); 56 PatchNPNFunctions();
81 #if defined(OS_WIN)
82 CoInitialize(NULL);
83 #endif
84
85 notification_service_.reset(new NotificationService);
86 57
87 // Preload the library to avoid loading, unloading then reloading 58 // Preload the library to avoid loading, unloading then reloading
88 preloaded_plugin_module_ = base::LoadNativeLibrary(plugin_path_); 59 preloaded_plugin_module_ = base::LoadNativeLibrary(plugin_path_);
89 60
90 ChromePluginLib::Create(plugin_path_, GetCPBrowserFuncsForPlugin()); 61 ChromePluginLib::Create(plugin_path_, GetCPBrowserFuncsForPlugin());
91 62
92 scoped_refptr<NPAPI::PluginLib> plugin = 63 scoped_refptr<NPAPI::PluginLib> plugin =
93 NPAPI::PluginLib::CreatePluginLib(plugin_path_); 64 NPAPI::PluginLib::CreatePluginLib(plugin_path_);
94 if (plugin.get()) { 65 if (plugin.get()) {
95 plugin->NP_Initialize(); 66 plugin->NP_Initialize();
96 } 67 }
97 68
98 // Certain plugins, such as flash, steal the unhandled exception filter 69 // Certain plugins, such as flash, steal the unhandled exception filter
99 // thus we never get crash reports when they fault. This call fixes it. 70 // thus we never get crash reports when they fault. This call fixes it.
100 message_loop()->set_exception_restoration(true); 71 message_loop()->set_exception_restoration(true);
101 } 72 }
102 73
103 void PluginThread::CleanUp() { 74 PluginThread::~PluginThread() {
104 if (preloaded_plugin_module_) { 75 if (preloaded_plugin_module_) {
105 base::UnloadNativeLibrary(preloaded_plugin_module_); 76 base::UnloadNativeLibrary(preloaded_plugin_module_);
106 preloaded_plugin_module_ = NULL; 77 preloaded_plugin_module_ = NULL;
107 } 78 }
108 PluginChannelBase::CleanupChannels(); 79 PluginChannelBase::CleanupChannels();
109 NPAPI::PluginLib::UnloadAllPlugins(); 80 NPAPI::PluginLib::UnloadAllPlugins();
110 ChromePluginLib::UnloadAllPlugins(); 81 ChromePluginLib::UnloadAllPlugins();
111 notification_service_.reset();
112 #if defined(OS_WIN)
113 CoUninitialize();
114 #endif
115 82
116 if (webkit_glue::ShouldForcefullyTerminatePluginProcess()) 83 if (webkit_glue::ShouldForcefullyTerminatePluginProcess())
117 base::KillProcess(base::GetCurrentProcessHandle(), 0, /* wait= */ false); 84 base::KillProcess(base::GetCurrentProcessHandle(), 0, /* wait= */ false);
118 85
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();
123 lazy_tls.Pointer()->Set(NULL); 86 lazy_tls.Pointer()->Set(NULL);
124 } 87 }
125 88
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
126 void PluginThread::OnCreateChannel( 100 void PluginThread::OnCreateChannel(
127 int process_id, 101 int process_id,
128 bool off_the_record) { 102 bool off_the_record) {
129 scoped_refptr<PluginChannel> channel = 103 scoped_refptr<PluginChannel> channel = PluginChannel::GetPluginChannel(
130 PluginChannel::GetPluginChannel(process_id, owner_loop()); 104 process_id, ChildProcess::current()->io_message_loop());
131 IPC::ChannelHandle channel_handle; 105 IPC::ChannelHandle channel_handle;
132 if (channel.get()) { 106 if (channel.get()) {
133 channel_handle.name = channel->channel_name(); 107 channel_handle.name = channel->channel_name();
134 #if defined(OS_POSIX) 108 #if defined(OS_POSIX)
135 // On POSIX, pass the renderer-side FD. Also mark it as auto-close so that 109 // On POSIX, pass the renderer-side FD. Also mark it as auto-close so that
136 // it gets closed after it has been sent. 110 // it gets closed after it has been sent.
137 int renderer_fd = channel->DisownRendererFd(); 111 int renderer_fd = channel->DisownRendererFd();
138 channel_handle.socket = base::FileDescriptor(renderer_fd, true); 112 channel_handle.socket = base::FileDescriptor(renderer_fd, true);
139 #endif 113 #endif
140 channel->set_off_the_record(off_the_record); 114 channel->set_off_the_record(off_the_record);
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 } 192 }
219 193
220 if (!result || net_error != net::OK) 194 if (!result || net_error != net::OK)
221 return false; 195 return false;
222 196
223 *proxy_list = proxy_result; 197 *proxy_list = proxy_result;
224 return true; 198 return true;
225 } 199 }
226 200
227 } // namespace webkit_glue 201 } // namespace webkit_glue
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698