| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "content/browser/mach_broker_mac.h" | 5 #include "content/browser/mach_broker_mac.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/mac/foundation_util.h" | 11 #include "base/mac/foundation_util.h" |
| 12 #include "base/mach_ipc_mac.h" | 12 #include "base/mach_ipc_mac.h" |
| 13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 14 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
| 15 #include "base/sys_string_conversions.h" | 15 #include "base/sys_string_conversions.h" |
| 16 #include "base/threading/platform_thread.h" | 16 #include "base/threading/platform_thread.h" |
| 17 #include "content/browser/renderer_host/render_process_host_impl.h" | 17 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 18 #include "content/public/browser/child_process_data.h" | 18 #include "content/public/browser/child_process_data.h" |
| 19 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
| 21 #include "content/public/browser/notification_types.h" | 21 #include "content/public/browser/notification_types.h" |
| 22 #include "content/public/common/content_switches.h" | 22 #include "content/public/common/content_switches.h" |
| 23 | 23 |
| 24 using content::BrowserThread; | 24 namespace content { |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 |
| 27 // Prints a string representation of a Mach error code. | 28 // Prints a string representation of a Mach error code. |
| 28 std::string MachErrorCode(kern_return_t err) { | 29 std::string MachErrorCode(kern_return_t err) { |
| 29 return base::StringPrintf("0x%x %s", err, mach_error_string(err)); | 30 return base::StringPrintf("0x%x %s", err, mach_error_string(err)); |
| 30 } | 31 } |
| 32 |
| 31 } // namespace | 33 } // namespace |
| 32 | 34 |
| 33 class MachListenerThreadDelegate : public base::PlatformThread::Delegate { | 35 class MachListenerThreadDelegate : public base::PlatformThread::Delegate { |
| 34 public: | 36 public: |
| 35 MachListenerThreadDelegate(MachBroker* broker) : broker_(broker) { | 37 MachListenerThreadDelegate(MachBroker* broker) : broker_(broker) { |
| 36 DCHECK(broker_); | 38 DCHECK(broker_); |
| 37 std::string port_name = MachBroker::GetMachPortName(); | 39 std::string port_name = MachBroker::GetMachPortName(); |
| 38 | 40 |
| 39 // Create the receive port in the constructor, not in ThreadMain(). It is | 41 // Create the receive port in the constructor, not in ThreadMain(). It is |
| 40 // important to create and register the receive port before starting the | 42 // important to create and register the receive port before starting the |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 // Returns the mach task belonging to |pid|. | 168 // Returns the mach task belonging to |pid|. |
| 167 mach_port_t MachBroker::TaskForPid(base::ProcessHandle pid) const { | 169 mach_port_t MachBroker::TaskForPid(base::ProcessHandle pid) const { |
| 168 base::AutoLock lock(lock_); | 170 base::AutoLock lock(lock_); |
| 169 MachBroker::MachMap::const_iterator it = mach_map_.find(pid); | 171 MachBroker::MachMap::const_iterator it = mach_map_.find(pid); |
| 170 if (it == mach_map_.end()) | 172 if (it == mach_map_.end()) |
| 171 return MACH_PORT_NULL; | 173 return MACH_PORT_NULL; |
| 172 return it->second.mach_task_; | 174 return it->second.mach_task_; |
| 173 } | 175 } |
| 174 | 176 |
| 175 void MachBroker::Observe(int type, | 177 void MachBroker::Observe(int type, |
| 176 const content::NotificationSource& source, | 178 const NotificationSource& source, |
| 177 const content::NotificationDetails& details) { | 179 const NotificationDetails& details) { |
| 178 // TODO(rohitrao): These notifications do not always carry the proper PIDs, | 180 // TODO(rohitrao): These notifications do not always carry the proper PIDs, |
| 179 // especially when the renderer is already gone or has crashed. Find a better | 181 // especially when the renderer is already gone or has crashed. Find a better |
| 180 // way to listen for child process deaths. http://crbug.com/55734 | 182 // way to listen for child process deaths. http://crbug.com/55734 |
| 181 base::ProcessHandle handle = 0; | 183 base::ProcessHandle handle = 0; |
| 182 switch (type) { | 184 switch (type) { |
| 183 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: | 185 case NOTIFICATION_RENDERER_PROCESS_CLOSED: |
| 184 handle = | 186 handle = |
| 185 content::Details<content::RenderProcessHost::RendererClosedDetails>( | 187 Details<RenderProcessHost::RendererClosedDetails>( |
| 186 details)->handle; | 188 details)->handle; |
| 187 break; | 189 break; |
| 188 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: | 190 case NOTIFICATION_RENDERER_PROCESS_TERMINATED: |
| 189 handle = content::Source<content::RenderProcessHost>(source)-> | 191 handle = Source<RenderProcessHost>(source)->GetHandle(); |
| 190 GetHandle(); | |
| 191 break; | 192 break; |
| 192 case content::NOTIFICATION_CHILD_PROCESS_CRASHED: | 193 case NOTIFICATION_CHILD_PROCESS_CRASHED: |
| 193 case content::NOTIFICATION_CHILD_PROCESS_HOST_DISCONNECTED: | 194 case NOTIFICATION_CHILD_PROCESS_HOST_DISCONNECTED: |
| 194 handle = content::Details<content::ChildProcessData>(details)->handle; | 195 handle = Details<ChildProcessData>(details)->handle; |
| 195 break; | 196 break; |
| 196 default: | 197 default: |
| 197 NOTREACHED() << "Unexpected notification"; | 198 NOTREACHED() << "Unexpected notification"; |
| 198 break; | 199 break; |
| 199 } | 200 } |
| 200 InvalidatePid(handle); | 201 InvalidatePid(handle); |
| 201 } | 202 } |
| 202 | 203 |
| 203 // static | 204 // static |
| 204 std::string MachBroker::GetMachPortName() { | 205 std::string MachBroker::GetMachPortName() { |
| 205 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | 206 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 206 const bool is_child = command_line->HasSwitch(switches::kProcessType); | 207 const bool is_child = command_line->HasSwitch(switches::kProcessType); |
| 207 | 208 |
| 208 // In non-browser (child) processes, use the parent's pid. | 209 // In non-browser (child) processes, use the parent's pid. |
| 209 const pid_t pid = is_child ? getppid() : getpid(); | 210 const pid_t pid = is_child ? getppid() : getpid(); |
| 210 return base::StringPrintf("%s.rohitfork.%d", base::mac::BaseBundleID(), pid); | 211 return base::StringPrintf("%s.rohitfork.%d", base::mac::BaseBundleID(), pid); |
| 211 } | 212 } |
| 212 | 213 |
| 213 MachBroker::MachBroker() : listener_thread_started_(false) { | 214 MachBroker::MachBroker() : listener_thread_started_(false) { |
| 214 } | 215 } |
| 215 | 216 |
| 216 MachBroker::~MachBroker() {} | 217 MachBroker::~MachBroker() {} |
| 217 | 218 |
| 218 void MachBroker::RegisterNotifications() { | 219 void MachBroker::RegisterNotifications() { |
| 219 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 220 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 220 content::NotificationService::AllBrowserContextsAndSources()); | 221 NotificationService::AllBrowserContextsAndSources()); |
| 221 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 222 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 222 content::NotificationService::AllBrowserContextsAndSources()); | 223 NotificationService::AllBrowserContextsAndSources()); |
| 223 registrar_.Add(this, content::NOTIFICATION_CHILD_PROCESS_CRASHED, | 224 registrar_.Add(this, NOTIFICATION_CHILD_PROCESS_CRASHED, |
| 224 content::NotificationService::AllBrowserContextsAndSources()); | 225 NotificationService::AllBrowserContextsAndSources()); |
| 225 registrar_.Add(this, content::NOTIFICATION_CHILD_PROCESS_HOST_DISCONNECTED, | 226 registrar_.Add(this, NOTIFICATION_CHILD_PROCESS_HOST_DISCONNECTED, |
| 226 content::NotificationService::AllBrowserContextsAndSources()); | 227 NotificationService::AllBrowserContextsAndSources()); |
| 227 } | 228 } |
| 229 |
| 230 } // namespace content |
| OLD | NEW |