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 // Prints a string representation of a Mach error code. | 27 // Prints a string representation of a Mach error code. |
28 std::string MachErrorCode(kern_return_t err) { | 28 std::string MachErrorCode(kern_return_t err) { |
29 return base::StringPrintf("0x%x %s", err, mach_error_string(err)); | 29 return base::StringPrintf("0x%x %s", err, mach_error_string(err)); |
30 } | 30 } |
31 } // namespace | 31 } // namespace |
32 | 32 |
33 class MachListenerThreadDelegate : public base::PlatformThread::Delegate { | 33 class MachListenerThreadDelegate : public base::PlatformThread::Delegate { |
34 public: | 34 public: |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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::Observe(int type, | 175 void MachBroker::Observe(int type, |
176 const content::NotificationSource& source, | 176 const NotificationSource& source, |
177 const content::NotificationDetails& details) { | 177 const NotificationDetails& details) { |
178 // TODO(rohitrao): These notifications do not always carry the proper PIDs, | 178 // 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 | 179 // 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 | 180 // way to listen for child process deaths. http://crbug.com/55734 |
181 base::ProcessHandle handle = 0; | 181 base::ProcessHandle handle = 0; |
182 switch (type) { | 182 switch (type) { |
183 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: | 183 case NOTIFICATION_RENDERER_PROCESS_CLOSED: |
184 handle = | 184 handle = Details<RenderProcessHost::RendererClosedDetails>( |
185 content::Details<content::RenderProcessHost::RendererClosedDetails>( | 185 details)->handle; |
186 details)->handle; | |
187 break; | 186 break; |
188 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: | 187 case NOTIFICATION_RENDERER_PROCESS_TERMINATED: |
189 handle = content::Source<content::RenderProcessHost>(source)-> | 188 handle = Source<RenderProcessHost>(source)->GetHandle(); |
190 GetHandle(); | |
191 break; | 189 break; |
192 case content::NOTIFICATION_CHILD_PROCESS_CRASHED: | 190 case NOTIFICATION_CHILD_PROCESS_CRASHED: |
193 case content::NOTIFICATION_CHILD_PROCESS_HOST_DISCONNECTED: | 191 case NOTIFICATION_CHILD_PROCESS_HOST_DISCONNECTED: |
194 handle = content::Details<content::ChildProcessData>(details)->handle; | 192 handle = Details<ChildProcessData>(details)->handle; |
195 break; | 193 break; |
196 default: | 194 default: |
197 NOTREACHED() << "Unexpected notification"; | 195 NOTREACHED() << "Unexpected notification"; |
198 break; | 196 break; |
199 } | 197 } |
200 InvalidatePid(handle); | 198 InvalidatePid(handle); |
201 } | 199 } |
202 | 200 |
203 // static | 201 // static |
204 std::string MachBroker::GetMachPortName() { | 202 std::string MachBroker::GetMachPortName() { |
205 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | 203 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
206 const bool is_child = command_line->HasSwitch(switches::kProcessType); | 204 const bool is_child = command_line->HasSwitch(switches::kProcessType); |
207 | 205 |
208 // In non-browser (child) processes, use the parent's pid. | 206 // In non-browser (child) processes, use the parent's pid. |
209 const pid_t pid = is_child ? getppid() : getpid(); | 207 const pid_t pid = is_child ? getppid() : getpid(); |
210 return base::StringPrintf("%s.rohitfork.%d", base::mac::BaseBundleID(), pid); | 208 return base::StringPrintf("%s.rohitfork.%d", base::mac::BaseBundleID(), pid); |
211 } | 209 } |
212 | 210 |
213 MachBroker::MachBroker() : listener_thread_started_(false) { | 211 MachBroker::MachBroker() : listener_thread_started_(false) { |
214 } | 212 } |
215 | 213 |
216 MachBroker::~MachBroker() {} | 214 MachBroker::~MachBroker() {} |
217 | 215 |
218 void MachBroker::RegisterNotifications() { | 216 void MachBroker::RegisterNotifications() { |
219 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 217 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_CLOSED, |
220 content::NotificationService::AllBrowserContextsAndSources()); | 218 NotificationService::AllBrowserContextsAndSources()); |
221 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 219 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
222 content::NotificationService::AllBrowserContextsAndSources()); | 220 NotificationService::AllBrowserContextsAndSources()); |
223 registrar_.Add(this, content::NOTIFICATION_CHILD_PROCESS_CRASHED, | 221 registrar_.Add(this, NOTIFICATION_CHILD_PROCESS_CRASHED, |
224 content::NotificationService::AllBrowserContextsAndSources()); | 222 NotificationService::AllBrowserContextsAndSources()); |
225 registrar_.Add(this, content::NOTIFICATION_CHILD_PROCESS_HOST_DISCONNECTED, | 223 registrar_.Add(this, NOTIFICATION_CHILD_PROCESS_HOST_DISCONNECTED, |
226 content::NotificationService::AllBrowserContextsAndSources()); | 224 NotificationService::AllBrowserContextsAndSources()); |
227 } | 225 } |
| 226 |
| 227 } // namespace content |
OLD | NEW |