OLD | NEW |
---|---|
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/browser/child_process_launcher.h" | 5 #include "chrome/browser/child_process_launcher.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/scoped_ptr.h" | 9 #include "base/scoped_ptr.h" |
10 #include "base/thread.h" | 10 #include "base/thread.h" |
11 #include "chrome/browser/chrome_thread.h" | 11 #include "chrome/browser/chrome_thread.h" |
12 #include "chrome/common/chrome_descriptors.h" | 12 #include "chrome/common/chrome_descriptors.h" |
13 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
14 #include "chrome/common/process_watcher.h" | 14 #include "chrome/common/process_watcher.h" |
15 #include "chrome/common/result_codes.h" | 15 #include "chrome/common/result_codes.h" |
16 | 16 |
17 #if defined(OS_WIN) | 17 #if defined(OS_WIN) |
18 #include "chrome/browser/sandbox_policy.h" | 18 #include "chrome/browser/sandbox_policy.h" |
19 #elif defined(OS_LINUX) | 19 #elif defined(OS_LINUX) |
20 #include "base/singleton.h" | 20 #include "base/singleton.h" |
21 #include "chrome/browser/crash_handler_host_linux.h" | 21 #include "chrome/browser/crash_handler_host_linux.h" |
22 #include "chrome/browser/zygote_host_linux.h" | 22 #include "chrome/browser/zygote_host_linux.h" |
23 #include "chrome/browser/renderer_host/render_sandbox_host_linux.h" | 23 #include "chrome/browser/renderer_host/render_sandbox_host_linux.h" |
24 #endif | 24 #endif |
25 | 25 |
26 #if defined(OS_POSIX) | 26 #if defined(OS_POSIX) |
27 #include "base/global_descriptors_posix.h" | 27 #include "base/global_descriptors_posix.h" |
28 #endif | 28 #endif |
29 | 29 |
30 #if defined(OS_MACOSX) | |
31 #include "ipc/ipc_switches.h" | |
32 #include "chrome/browser/mach_broker_mac.h" | |
33 #include "chrome/common/mach_ipc_mac.h" | |
34 #endif | |
35 | |
36 #if defined(OS_MACOSX) | |
37 class MachTask : public Task { | |
38 public: | |
39 MachTask(std::string channel_name, mach_port_t* task) | |
40 : task_(task) { | |
41 // TODO(thakis): Move some place central | |
Mark Mentovai
2010/01/11 20:22:46
Wanna do this now?
| |
42 const std::string kMachChannelPrefix = "com.Google.Chrome"; | |
43 std::string channel = kMachChannelPrefix + channel_name; | |
Mark Mentovai
2010/01/11 20:22:46
Unless channel_name begins with a dot, this will g
| |
44 | |
45 MachBroker::instance(); // make sure registrations are registered on the ri ght thread. | |
Mark Mentovai
2010/01/11 20:22:46
80
| |
46 | |
47 // This creates our named server port -- needs to happen on the current | |
48 // thread. | |
49 printf("Creating receive port %s\n", channel.c_str()); | |
Mark Mentovai
2010/01/11 20:22:46
Debugging turdlet?
(I leave my debugging turdlets
| |
50 port_.reset(new ReceivePort(channel.c_str())); | |
51 } | |
52 | |
53 virtual void Run() { | |
54 // TODO(thakis): Move some place central | |
55 const int kMachPortMessageID = 57; | |
Mark Mentovai
2010/01/11 20:22:46
Magic?
| |
56 | |
57 const int kMachPortMessageReceiveWaitMs = 1000; | |
58 | |
59 // TODO(thakis): time histogram between creation and port reception? | |
60 MachReceiveMessage message; | |
61 kern_return_t result = port_->WaitForMessage( | |
62 &message, kMachPortMessageReceiveWaitMs); | |
63 if (result == KERN_SUCCESS) { | |
64 CHECK(kMachPortMessageID == message.GetMessageID()); | |
Mark Mentovai
2010/01/11 20:22:46
CHECK_EQ?
| |
65 CHECK(1 == message.GetDescriptorCount()); | |
Mark Mentovai
2010/01/11 20:22:46
CHECK_EQ?
| |
66 *task_ = message.GetTranslatedPort(0); | |
67 printf("yay\n"); | |
Mark Mentovai
2010/01/11 20:22:46
Debugging turdlet?
| |
68 } else { | |
69 // TODO(thakis): Log somewhere? | |
70 *task_ = 0; | |
71 printf("nay\n"); | |
Mark Mentovai
2010/01/11 20:22:46
LOG?
| |
72 } | |
73 } | |
74 | |
75 private: | |
76 scoped_ptr<ReceivePort> port_; | |
77 mach_port_t* task_; | |
78 }; | |
79 | |
80 class MachTask2 : public Task { | |
Mark Mentovai
2010/01/11 20:22:46
Class-level comments would be nice for this and fo
| |
81 public: | |
82 MachTask2(const mach_port_t& task, base::ProcessHandle pid) // must be refs, might be 0 at call time and only be set later on the other thread | |
Mark Mentovai
2010/01/11 20:22:46
80
| |
83 : task_(task), pid_(pid) {} | |
84 | |
85 virtual void Run() { | |
86 if (task_) | |
Mark Mentovai
2010/01/11 20:22:46
You need {} here because this thing spills over a
| |
87 MachBroker::instance()->RegisterPid( | |
88 pid_, | |
89 MachBroker::MachInfo().SetTask(task_)); | |
90 } | |
91 private: | |
92 const mach_port_t& task_; | |
93 base::ProcessHandle pid_; | |
94 }; | |
95 | |
96 class MachThread : public base::Thread { | |
97 public: | |
98 MachThread() : base::Thread("MachThread"), task_(0) {} | |
Mark Mentovai
2010/01/11 20:22:46
task_(MACH_PORT_NULL) ?
| |
99 | |
100 void DoIt(const std::string& channel_name) { | |
101 DCHECK(message_loop()); | |
102 message_loop()->PostTask(FROM_HERE, | |
103 new MachTask(channel_name, &task_)); | |
104 } | |
105 | |
106 void DoIt2(base::ProcessHandle pid) { | |
Mark Mentovai
2010/01/11 20:22:46
Oh, so is this just to avoid the ban on overloadin
| |
107 DCHECK(message_loop()); | |
108 message_loop()->PostTask(FROM_HERE, | |
109 new MachTask2(task_, pid)); | |
110 } | |
111 | |
112 private: | |
113 mach_port_t task_; | |
114 }; | |
115 #endif | |
116 | |
30 // Having the functionality of ChildProcessLauncher be in an internal | 117 // Having the functionality of ChildProcessLauncher be in an internal |
31 // ref counted object allows us to automatically terminate the process when the | 118 // ref counted object allows us to automatically terminate the process when the |
32 // parent class destructs, while still holding on to state that we need. | 119 // parent class destructs, while still holding on to state that we need. |
33 class ChildProcessLauncher::Context | 120 class ChildProcessLauncher::Context |
34 : public base::RefCountedThreadSafe<ChildProcessLauncher::Context> { | 121 : public base::RefCountedThreadSafe<ChildProcessLauncher::Context> { |
35 public: | 122 public: |
36 Context() | 123 Context() |
37 : starting_(true) | 124 : starting_(true) |
38 #if defined(OS_LINUX) | 125 #if defined(OS_LINUX) |
39 , zygote_(false) | 126 , zygote_(false) |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
153 } | 240 } |
154 if (is_renderer) { | 241 if (is_renderer) { |
155 const int sandbox_fd = | 242 const int sandbox_fd = |
156 Singleton<RenderSandboxHostLinux>()->GetRendererSocket(); | 243 Singleton<RenderSandboxHostLinux>()->GetRendererSocket(); |
157 fds_to_map.push_back(std::make_pair( | 244 fds_to_map.push_back(std::make_pair( |
158 sandbox_fd, | 245 sandbox_fd, |
159 kSandboxIPCChannel + base::GlobalDescriptors::kBaseDescriptor)); | 246 kSandboxIPCChannel + base::GlobalDescriptors::kBaseDescriptor)); |
160 } | 247 } |
161 #endif // defined(OS_LINUX) | 248 #endif // defined(OS_LINUX) |
162 | 249 |
250 #if defined(OS_MACOSX) | |
251 // TODO(thakis): Possibly somewhere else? | |
252 // (then again, the fds duping stuff is here too, so maybe it's ok) | |
253 bool is_renderer = cmd_line->GetSwitchValueASCII(switches::kProcessType) | |
254 == switches::kRendererProcess; | |
255 bool is_extension = cmd_line->GetSwitchValueASCII(switches::kProcessType) == | |
Mark Mentovai
2010/01/11 20:22:46
Fix indent?
This whole section (253-256) should b
| |
256 switches::kExtensionProcess; | |
257 | |
258 for (size_t ii = 0; ii < cmd_line->argv().size(); ++ii) | |
Mark Mentovai
2010/01/11 20:22:46
Debugging turdlets?
| |
259 fprintf(stderr, "%s ", cmd_line->argv()[ii].c_str()); | |
260 fprintf(stderr, "\n"); | |
261 fprintf(stderr, "child type: %s\n", cmd_line->GetSwitchValueASCII(switches::kPro cessType).c_str()); | |
262 MachThread mach_thread; | |
Mark Mentovai
2010/01/11 20:22:46
Fix the indent in here too?
| |
263 if (true || is_renderer || is_extension) { | |
Mark Mentovai
2010/01/11 20:22:46
Remove "true" - debugging turdlet?
| |
264 CHECK(mach_thread.Start()); | |
265 mach_thread.DoIt( | |
266 cmd_line->GetSwitchValueASCII(switches::kProcessChannelID)); | |
267 } | |
268 #endif | |
269 | |
270 | |
163 // Actually launch the app. | 271 // Actually launch the app. |
164 if (!base::LaunchApp(cmd_line->argv(), env, fds_to_map, false, &handle)) | 272 if (!base::LaunchApp(cmd_line->argv(), env, fds_to_map, false, &handle)) |
165 handle = base::kNullProcessHandle; | 273 handle = base::kNullProcessHandle; |
274 | |
275 #if defined(OS_MACOSX) | |
276 if (true || is_renderer || is_extension) { | |
Mark Mentovai
2010/01/11 20:22:46
true
| |
277 // TODO(thakis): Check |handle| first. | |
278 mach_thread.DoIt2(handle); | |
279 } | |
280 #endif | |
166 } | 281 } |
167 #endif | 282 #endif |
168 | 283 |
169 ChromeThread::PostTask( | 284 ChromeThread::PostTask( |
170 client_thread_id_, FROM_HERE, | 285 client_thread_id_, FROM_HERE, |
171 NewRunnableMethod( | 286 NewRunnableMethod( |
172 this, | 287 this, |
173 &ChildProcessLauncher::Context::Notify, | 288 &ChildProcessLauncher::Context::Notify, |
174 #if defined(OS_LINUX) | 289 #if defined(OS_LINUX) |
175 zygote, | 290 zygote, |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
270 | 385 |
271 ChildProcessLauncher::~ChildProcessLauncher() { | 386 ChildProcessLauncher::~ChildProcessLauncher() { |
272 context_->ResetClient(); | 387 context_->ResetClient(); |
273 } | 388 } |
274 | 389 |
275 bool ChildProcessLauncher::IsStarting() { | 390 bool ChildProcessLauncher::IsStarting() { |
276 return context_->starting_; | 391 return context_->starting_; |
277 } | 392 } |
278 | 393 |
279 base::ProcessHandle ChildProcessLauncher::GetHandle() { | 394 base::ProcessHandle ChildProcessLauncher::GetHandle() { |
280 DCHECK(!context_->starting_); | 395 // DCHECK(!context_->starting_); |
Mark Mentovai
2010/01/11 20:22:46
?
| |
281 return context_->process_.handle(); | 396 return context_->process_.handle(); |
282 } | 397 } |
283 | 398 |
284 bool ChildProcessLauncher::DidProcessCrash() { | 399 bool ChildProcessLauncher::DidProcessCrash() { |
285 bool did_crash, child_exited; | 400 bool did_crash, child_exited; |
286 base::ProcessHandle handle = context_->process_.handle(); | 401 base::ProcessHandle handle = context_->process_.handle(); |
287 #if defined(OS_LINUX) | 402 #if defined(OS_LINUX) |
288 if (context_->zygote_) { | 403 if (context_->zygote_) { |
289 did_crash = Singleton<ZygoteHost>()->DidProcessCrash(handle, &child_exited); | 404 did_crash = Singleton<ZygoteHost>()->DidProcessCrash(handle, &child_exited); |
290 } else | 405 } else |
(...skipping 12 matching lines...) Expand all Loading... | |
303 if (child_exited) | 418 if (child_exited) |
304 context_->process_.Close(); | 419 context_->process_.Close(); |
305 | 420 |
306 return did_crash; | 421 return did_crash; |
307 } | 422 } |
308 | 423 |
309 void ChildProcessLauncher::SetProcessBackgrounded(bool background) { | 424 void ChildProcessLauncher::SetProcessBackgrounded(bool background) { |
310 DCHECK(!context_->starting_); | 425 DCHECK(!context_->starting_); |
311 context_->process_.SetProcessBackgrounded(background); | 426 context_->process_.SetProcessBackgrounded(background); |
312 } | 427 } |
OLD | NEW |