| 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" |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 | 165 |
| 166 // Returns the mach task belonging to |pid|. | 166 // Returns the mach task belonging to |pid|. |
| 167 mach_port_t MachBroker::TaskForPid(base::ProcessHandle pid) const { | 167 mach_port_t MachBroker::TaskForPid(base::ProcessHandle pid) const { |
| 168 base::AutoLock lock(lock_); | 168 base::AutoLock lock(lock_); |
| 169 MachBroker::MachMap::const_iterator it = mach_map_.find(pid); | 169 MachBroker::MachMap::const_iterator it = mach_map_.find(pid); |
| 170 if (it == mach_map_.end()) | 170 if (it == mach_map_.end()) |
| 171 return MACH_PORT_NULL; | 171 return MACH_PORT_NULL; |
| 172 return it->second.mach_task_; | 172 return it->second.mach_task_; |
| 173 } | 173 } |
| 174 | 174 |
| 175 void MachBroker::BrowserChildProcessHostDisconnected( |
| 176 const ChildProcessData& data) { |
| 177 InvalidatePid(data.handle); |
| 178 } |
| 179 |
| 180 void MachBroker::BrowserChildProcessCrashed(const ChildProcessData& data) { |
| 181 InvalidatePid(data.handle); |
| 182 } |
| 183 |
| 175 void MachBroker::Observe(int type, | 184 void MachBroker::Observe(int type, |
| 176 const NotificationSource& source, | 185 const NotificationSource& source, |
| 177 const NotificationDetails& details) { | 186 const NotificationDetails& details) { |
| 178 // TODO(rohitrao): These notifications do not always carry the proper PIDs, | 187 // 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 | 188 // 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 | 189 // way to listen for child process deaths. http://crbug.com/55734 |
| 181 base::ProcessHandle handle = 0; | 190 base::ProcessHandle handle = 0; |
| 182 switch (type) { | 191 switch (type) { |
| 183 case NOTIFICATION_RENDERER_PROCESS_CLOSED: | 192 case NOTIFICATION_RENDERER_PROCESS_CLOSED: |
| 184 handle = Details<RenderProcessHost::RendererClosedDetails>( | 193 handle = Details<RenderProcessHost::RendererClosedDetails>( |
| 185 details)->handle; | 194 details)->handle; |
| 186 break; | 195 break; |
| 187 case NOTIFICATION_RENDERER_PROCESS_TERMINATED: | 196 case NOTIFICATION_RENDERER_PROCESS_TERMINATED: |
| 188 handle = Source<RenderProcessHost>(source)->GetHandle(); | 197 handle = Source<RenderProcessHost>(source)->GetHandle(); |
| 189 break; | 198 break; |
| 190 case NOTIFICATION_CHILD_PROCESS_CRASHED: | |
| 191 case NOTIFICATION_CHILD_PROCESS_HOST_DISCONNECTED: | |
| 192 handle = Details<ChildProcessData>(details)->handle; | |
| 193 break; | |
| 194 default: | 199 default: |
| 195 NOTREACHED() << "Unexpected notification"; | 200 NOTREACHED() << "Unexpected notification"; |
| 196 break; | 201 break; |
| 197 } | 202 } |
| 198 InvalidatePid(handle); | 203 InvalidatePid(handle); |
| 199 } | 204 } |
| 200 | 205 |
| 201 // static | 206 // static |
| 202 std::string MachBroker::GetMachPortName() { | 207 std::string MachBroker::GetMachPortName() { |
| 203 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | 208 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 204 const bool is_child = command_line->HasSwitch(switches::kProcessType); | 209 const bool is_child = command_line->HasSwitch(switches::kProcessType); |
| 205 | 210 |
| 206 // In non-browser (child) processes, use the parent's pid. | 211 // In non-browser (child) processes, use the parent's pid. |
| 207 const pid_t pid = is_child ? getppid() : getpid(); | 212 const pid_t pid = is_child ? getppid() : getpid(); |
| 208 return base::StringPrintf("%s.rohitfork.%d", base::mac::BaseBundleID(), pid); | 213 return base::StringPrintf("%s.rohitfork.%d", base::mac::BaseBundleID(), pid); |
| 209 } | 214 } |
| 210 | 215 |
| 211 MachBroker::MachBroker() : listener_thread_started_(false) { | 216 MachBroker::MachBroker() : listener_thread_started_(false) { |
| 212 } | 217 } |
| 213 | 218 |
| 214 MachBroker::~MachBroker() {} | 219 MachBroker::~MachBroker() {} |
| 215 | 220 |
| 216 void MachBroker::RegisterNotifications() { | 221 void MachBroker::RegisterNotifications() { |
| 217 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_CLOSED, | 222 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 218 NotificationService::AllBrowserContextsAndSources()); | 223 NotificationService::AllBrowserContextsAndSources()); |
| 219 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 224 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 220 NotificationService::AllBrowserContextsAndSources()); | 225 NotificationService::AllBrowserContextsAndSources()); |
| 221 registrar_.Add(this, NOTIFICATION_CHILD_PROCESS_CRASHED, | 226 |
| 222 NotificationService::AllBrowserContextsAndSources()); | 227 // No corresponding StopObservingBrowserChildProcesses, |
| 223 registrar_.Add(this, NOTIFICATION_CHILD_PROCESS_HOST_DISCONNECTED, | 228 // we leak this singleton. |
| 224 NotificationService::AllBrowserContextsAndSources()); | 229 BrowserChildProcessObserver::Add(this); |
| 225 } | 230 } |
| 226 | 231 |
| 227 } // namespace content | 232 } // namespace content |
| OLD | NEW |