| 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 "chrome/browser/mach_broker_mac.h" | 5 #include "content/browser/mach_broker_mac.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/mac/foundation_util.h" | 9 #include "base/mac/foundation_util.h" |
| 10 #include "base/mach_ipc_mac.h" | 10 #include "base/mach_ipc_mac.h" |
| 11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| 12 #include "base/sys_string_conversions.h" | 12 #include "base/sys_string_conversions.h" |
| 13 #include "base/threading/platform_thread.h" | 13 #include "base/threading/platform_thread.h" |
| 14 #include "chrome/browser/extensions/extension_host.h" | |
| 15 #include "chrome/common/chrome_switches.h" | |
| 16 #include "content/browser/browser_thread.h" | 14 #include "content/browser/browser_thread.h" |
| 17 #include "content/browser/renderer_host/render_process_host.h" | 15 #include "content/browser/renderer_host/render_process_host.h" |
| 18 #include "content/common/child_process_info.h" | 16 #include "content/common/child_process_info.h" |
| 17 #include "content/common/content_switches.h" |
| 19 #include "content/common/notification_service.h" | 18 #include "content/common/notification_service.h" |
| 20 | 19 |
| 21 namespace { | 20 namespace { |
| 22 // Prints a string representation of a Mach error code. | 21 // Prints a string representation of a Mach error code. |
| 23 std::string MachErrorCode(kern_return_t err) { | 22 std::string MachErrorCode(kern_return_t err) { |
| 24 return StringPrintf("0x%x %s", err, mach_error_string(err)); | 23 return StringPrintf("0x%x %s", err, mach_error_string(err)); |
| 25 } | 24 } |
| 26 } // namespace | 25 } // namespace |
| 27 | 26 |
| 28 // Required because notifications happen on the UI thread. | 27 // Required because notifications happen on the UI thread. |
| 29 class RegisterNotificationTask : public Task { | 28 class RegisterNotificationTask : public Task { |
| 30 public: | 29 public: |
| 31 RegisterNotificationTask( | 30 RegisterNotificationTask( |
| 32 MachBroker* broker) | 31 MachBroker* broker) |
| 33 : broker_(broker) { } | 32 : broker_(broker) { } |
| 34 | 33 |
| 35 virtual void Run() { | 34 virtual void Run() { |
| 36 broker_->registrar_.Add(broker_, | 35 broker_->registrar_.Add(broker_, |
| 37 NotificationType::RENDERER_PROCESS_CLOSED, | 36 NotificationType::RENDERER_PROCESS_CLOSED, |
| 38 NotificationService::AllSources()); | 37 NotificationService::AllSources()); |
| 39 broker_->registrar_.Add(broker_, | 38 broker_->registrar_.Add(broker_, |
| 40 NotificationType::RENDERER_PROCESS_TERMINATED, | 39 NotificationType::RENDERER_PROCESS_TERMINATED, |
| 41 NotificationService::AllSources()); | 40 NotificationService::AllSources()); |
| 42 broker_->registrar_.Add(broker_, | 41 broker_->registrar_.Add(broker_, |
| 43 NotificationType::CHILD_PROCESS_CRASHED, | 42 NotificationType::CHILD_PROCESS_CRASHED, |
| 44 NotificationService::AllSources()); | 43 NotificationService::AllSources()); |
| 45 broker_->registrar_.Add(broker_, | 44 broker_->registrar_.Add(broker_, |
| 46 NotificationType::CHILD_PROCESS_HOST_DISCONNECTED, | 45 NotificationType::CHILD_PROCESS_HOST_DISCONNECTED, |
| 47 NotificationService::AllSources()); | 46 NotificationService::AllSources()); |
| 48 broker_->registrar_.Add(broker_, | |
| 49 NotificationType::EXTENSION_PROCESS_TERMINATED, | |
| 50 NotificationService::AllSources()); | |
| 51 } | 47 } |
| 52 | 48 |
| 53 private: | 49 private: |
| 54 MachBroker* broker_; | 50 MachBroker* broker_; |
| 55 DISALLOW_COPY_AND_ASSIGN(RegisterNotificationTask); | 51 DISALLOW_COPY_AND_ASSIGN(RegisterNotificationTask); |
| 56 }; | 52 }; |
| 57 | 53 |
| 58 class MachListenerThreadDelegate : public base::PlatformThread::Delegate { | 54 class MachListenerThreadDelegate : public base::PlatformThread::Delegate { |
| 59 public: | 55 public: |
| 60 MachListenerThreadDelegate(MachBroker* broker) : broker_(broker) { | 56 MachListenerThreadDelegate(MachBroker* broker) : broker_(broker) { |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 const NotificationDetails& details) { | 200 const NotificationDetails& details) { |
| 205 // TODO(rohitrao): These notifications do not always carry the proper PIDs, | 201 // TODO(rohitrao): These notifications do not always carry the proper PIDs, |
| 206 // especially when the renderer is already gone or has crashed. Find a better | 202 // especially when the renderer is already gone or has crashed. Find a better |
| 207 // way to listen for child process deaths. http://crbug.com/55734 | 203 // way to listen for child process deaths. http://crbug.com/55734 |
| 208 base::ProcessHandle handle = 0; | 204 base::ProcessHandle handle = 0; |
| 209 switch (type.value) { | 205 switch (type.value) { |
| 210 case NotificationType::RENDERER_PROCESS_CLOSED: | 206 case NotificationType::RENDERER_PROCESS_CLOSED: |
| 211 case NotificationType::RENDERER_PROCESS_TERMINATED: | 207 case NotificationType::RENDERER_PROCESS_TERMINATED: |
| 212 handle = Source<RenderProcessHost>(source)->GetHandle(); | 208 handle = Source<RenderProcessHost>(source)->GetHandle(); |
| 213 break; | 209 break; |
| 214 case NotificationType::EXTENSION_PROCESS_TERMINATED: | |
| 215 handle = | |
| 216 Details<ExtensionHost>(details)->render_process_host()->GetHandle(); | |
| 217 break; | |
| 218 case NotificationType::CHILD_PROCESS_CRASHED: | 210 case NotificationType::CHILD_PROCESS_CRASHED: |
| 219 case NotificationType::CHILD_PROCESS_HOST_DISCONNECTED: | 211 case NotificationType::CHILD_PROCESS_HOST_DISCONNECTED: |
| 220 handle = Details<ChildProcessInfo>(details)->handle(); | 212 handle = Details<ChildProcessInfo>(details)->handle(); |
| 221 break; | 213 break; |
| 222 default: | 214 default: |
| 223 NOTREACHED() << "Unexpected notification"; | 215 NOTREACHED() << "Unexpected notification"; |
| 224 break; | 216 break; |
| 225 } | 217 } |
| 226 InvalidatePid(handle); | 218 InvalidatePid(handle); |
| 227 } | 219 } |
| 228 | 220 |
| 229 // static | 221 // static |
| 230 std::string MachBroker::GetMachPortName() { | 222 std::string MachBroker::GetMachPortName() { |
| 231 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | 223 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 232 const bool is_child = command_line->HasSwitch(switches::kProcessType); | 224 const bool is_child = command_line->HasSwitch(switches::kProcessType); |
| 233 | 225 |
| 234 // In non-browser (child) processes, use the parent's pid. | 226 // In non-browser (child) processes, use the parent's pid. |
| 235 const pid_t pid = is_child ? getppid() : getpid(); | 227 const pid_t pid = is_child ? getppid() : getpid(); |
| 236 return StringPrintf("%s.rohitfork.%d", base::mac::BaseBundleID(), pid); | 228 return StringPrintf("%s.rohitfork.%d", base::mac::BaseBundleID(), pid); |
| 237 } | 229 } |
| OLD | NEW |