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