| 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, mach_port_t* host) | |
| 40 : task_(task), host_(host) { | |
| 41 // TODO(thakis): Move some place central | |
| 42 const std::string kMachChannelPrefix = "com.Google.Chrome"; | |
| 43 std::string channel = kMachChannelPrefix + channel_name; | |
| 44 | |
| 45 // This creates our named server port -- needs to happen on the current | |
| 46 // thread. | |
| 47 printf("Creating receive port %s\n", channel.c_str()); | |
| 48 port_.reset(new ReceivePort(channel.c_str())); | |
| 49 } | |
| 50 | |
| 51 virtual void Run() { | |
| 52 // TODO(thakis): Move some place central | |
| 53 const int kMachPortMessageID = 57; | |
| 54 | |
| 55 const int kMachPortMessageReceiveWaitMs = 1000; | |
| 56 | |
| 57 | |
| 58 | |
| 59 //ReceivePort receivePort(channel_name.c_str()); | |
| 60 | |
| 61 // TODO(thakis): time histogram between creation and port reception? | |
| 62 MachReceiveMessage message; | |
| 63 kern_return_t result = port_->WaitForMessage( | |
| 64 &message, kMachPortMessageReceiveWaitMs); | |
| 65 if (result == KERN_SUCCESS) { | |
| 66 CHECK(kMachPortMessageID == message.GetMessageID()); | |
| 67 CHECK(2 == message.GetDescriptorCount()); | |
| 68 | |
| 69 // TODO(thakis): Constants for the indices? | |
| 70 *task_ = message.GetTranslatedPort(0); | |
| 71 *host_ = message.GetTranslatedPort(1); | |
| 72 printf("yay\n"); | |
| 73 } else { | |
| 74 // TODO(thakis): Log somewhere? | |
| 75 printf("nay\n"); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 private: | |
| 80 scoped_ptr<ReceivePort> port_; | |
| 81 mach_port_t* task_; | |
| 82 mach_port_t* host_; | |
| 83 }; | |
| 84 | |
| 85 class MachTask2 : public Task { | |
| 86 public: | |
| 87 MachTask2(mach_port_t task, mach_port_t host, base::ProcessHandle pid) | |
| 88 : task_(task), host_(host), pid_(pid) {} | |
| 89 | |
| 90 virtual void Run() { | |
| 91 MachBroker::instance()->RegisterPid( | |
| 92 pid_, | |
| 93 MachBroker::MachInfo().SetTask(task_).SetHost(host_)); | |
| 94 } | |
| 95 private: | |
| 96 mach_port_t task_; | |
| 97 mach_port_t host_; | |
| 98 base::ProcessHandle pid_; | |
| 99 }; | |
| 100 | |
| 101 class MachThread : public base::Thread { | |
| 102 public: | |
| 103 MachThread() : base::Thread("MachThread"), task_(0), host_(0) {} | |
| 104 | |
| 105 void DoIt(const std::string& channel_name) { | |
| 106 DCHECK(message_loop()); | |
| 107 message_loop()->PostTask(FROM_HERE, | |
| 108 new MachTask(channel_name, &task_, &host_)); | |
| 109 } | |
| 110 | |
| 111 void DoIt2(base::ProcessHandle pid) { | |
| 112 DCHECK(message_loop()); | |
| 113 message_loop()->PostTask(FROM_HERE, | |
| 114 new MachTask2(task_, host_, pid)); | |
| 115 } | |
| 116 | |
| 117 private: | |
| 118 mach_port_t task_; | |
| 119 mach_port_t host_; | |
| 120 }; | |
| 121 #endif | |
| 122 | |
| 123 // Having the functionality of ChildProcessLauncher be in an internal | 30 // Having the functionality of ChildProcessLauncher be in an internal |
| 124 // ref counted object allows us to automatically terminate the process when the | 31 // ref counted object allows us to automatically terminate the process when the |
| 125 // parent class destructs, while still holding on to state that we need. | 32 // parent class destructs, while still holding on to state that we need. |
| 126 class ChildProcessLauncher::Context | 33 class ChildProcessLauncher::Context |
| 127 : public base::RefCountedThreadSafe<ChildProcessLauncher::Context> { | 34 : public base::RefCountedThreadSafe<ChildProcessLauncher::Context> { |
| 128 public: | 35 public: |
| 129 Context() | 36 Context() |
| 130 : starting_(true) | 37 : starting_(true) |
| 131 #if defined(OS_LINUX) | 38 #if defined(OS_LINUX) |
| 132 , zygote_(false) | 39 , zygote_(false) |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 } | 153 } |
| 247 if (is_renderer) { | 154 if (is_renderer) { |
| 248 const int sandbox_fd = | 155 const int sandbox_fd = |
| 249 Singleton<RenderSandboxHostLinux>()->GetRendererSocket(); | 156 Singleton<RenderSandboxHostLinux>()->GetRendererSocket(); |
| 250 fds_to_map.push_back(std::make_pair( | 157 fds_to_map.push_back(std::make_pair( |
| 251 sandbox_fd, | 158 sandbox_fd, |
| 252 kSandboxIPCChannel + base::GlobalDescriptors::kBaseDescriptor)); | 159 kSandboxIPCChannel + base::GlobalDescriptors::kBaseDescriptor)); |
| 253 } | 160 } |
| 254 #endif // defined(OS_LINUX) | 161 #endif // defined(OS_LINUX) |
| 255 | 162 |
| 256 #if defined(OS_MACOSX) | |
| 257 // TODO(thakis): Possibly somewhere else? | |
| 258 // (then again, the fds duping stuff is here too, so maybe it's ok) | |
| 259 | |
| 260 MachThread mach_thread; | |
| 261 CHECK(mach_thread.Start()); | |
| 262 mach_thread.DoIt( | |
| 263 cmd_line->GetSwitchValueASCII(switches::kProcessChannelID)); | |
| 264 #endif | |
| 265 | |
| 266 | |
| 267 // Actually launch the app. | 163 // Actually launch the app. |
| 268 if (!base::LaunchApp(cmd_line->argv(), env, fds_to_map, false, &handle)) | 164 if (!base::LaunchApp(cmd_line->argv(), env, fds_to_map, false, &handle)) |
| 269 handle = base::kNullProcessHandle; | 165 handle = base::kNullProcessHandle; |
| 270 | |
| 271 #if defined(OS_MACOSX) | |
| 272 // TODO(thakis): Check |handle| first. | |
| 273 mach_thread.DoIt2(handle); | |
| 274 #endif | |
| 275 } | 166 } |
| 276 #endif | 167 #endif |
| 277 | 168 |
| 278 ChromeThread::PostTask( | 169 ChromeThread::PostTask( |
| 279 client_thread_id_, FROM_HERE, | 170 client_thread_id_, FROM_HERE, |
| 280 NewRunnableMethod( | 171 NewRunnableMethod( |
| 281 this, | 172 this, |
| 282 &ChildProcessLauncher::Context::Notify, | 173 &ChildProcessLauncher::Context::Notify, |
| 283 #if defined(OS_LINUX) | 174 #if defined(OS_LINUX) |
| 284 zygote, | 175 zygote, |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 412 if (child_exited) | 303 if (child_exited) |
| 413 context_->process_.Close(); | 304 context_->process_.Close(); |
| 414 | 305 |
| 415 return did_crash; | 306 return did_crash; |
| 416 } | 307 } |
| 417 | 308 |
| 418 void ChildProcessLauncher::SetProcessBackgrounded(bool background) { | 309 void ChildProcessLauncher::SetProcessBackgrounded(bool background) { |
| 419 DCHECK(!context_->starting_); | 310 DCHECK(!context_->starting_); |
| 420 context_->process_.SetProcessBackgrounded(background); | 311 context_->process_.SetProcessBackgrounded(background); |
| 421 } | 312 } |
| OLD | NEW |