| 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" |
| 8 #include "base/bind_helpers.h" |
| 7 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 8 #include "base/logging.h" | 10 #include "base/logging.h" |
| 9 #include "base/mac/foundation_util.h" | 11 #include "base/mac/foundation_util.h" |
| 10 #include "base/mach_ipc_mac.h" | 12 #include "base/mach_ipc_mac.h" |
| 11 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 12 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
| 13 #include "base/sys_string_conversions.h" | 15 #include "base/sys_string_conversions.h" |
| 14 #include "base/threading/platform_thread.h" | 16 #include "base/threading/platform_thread.h" |
| 15 #include "content/browser/renderer_host/render_process_host_impl.h" | 17 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 16 #include "content/common/child_process_info.h" | 18 #include "content/common/child_process_info.h" |
| 17 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
| 19 #include "content/public/browser/notification_types.h" | 21 #include "content/public/browser/notification_types.h" |
| 20 #include "content/public/common/content_switches.h" | 22 #include "content/public/common/content_switches.h" |
| 21 | 23 |
| 22 using content::BrowserThread; | 24 using content::BrowserThread; |
| 23 | 25 |
| 24 namespace { | 26 namespace { |
| 25 // Prints a string representation of a Mach error code. | 27 // Prints a string representation of a Mach error code. |
| 26 std::string MachErrorCode(kern_return_t err) { | 28 std::string MachErrorCode(kern_return_t err) { |
| 27 return base::StringPrintf("0x%x %s", err, mach_error_string(err)); | 29 return base::StringPrintf("0x%x %s", err, mach_error_string(err)); |
| 28 } | 30 } |
| 29 } // namespace | 31 } // namespace |
| 30 | 32 |
| 31 // Required because notifications happen on the UI thread. | |
| 32 class RegisterNotificationTask : public Task { | |
| 33 public: | |
| 34 RegisterNotificationTask( | |
| 35 MachBroker* broker) | |
| 36 : broker_(broker) { } | |
| 37 | |
| 38 virtual void Run() { | |
| 39 broker_->registrar_.Add(broker_, | |
| 40 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | |
| 41 content::NotificationService::AllBrowserContextsAndSources()); | |
| 42 broker_->registrar_.Add(broker_, | |
| 43 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | |
| 44 content::NotificationService::AllBrowserContextsAndSources()); | |
| 45 broker_->registrar_.Add(broker_, | |
| 46 content::NOTIFICATION_CHILD_PROCESS_CRASHED, | |
| 47 content::NotificationService::AllBrowserContextsAndSources()); | |
| 48 broker_->registrar_.Add(broker_, | |
| 49 content::NOTIFICATION_CHILD_PROCESS_HOST_DISCONNECTED, | |
| 50 content::NotificationService::AllBrowserContextsAndSources()); | |
| 51 } | |
| 52 | |
| 53 private: | |
| 54 MachBroker* broker_; | |
| 55 DISALLOW_COPY_AND_ASSIGN(RegisterNotificationTask); | |
| 56 }; | |
| 57 | |
| 58 class MachListenerThreadDelegate : public base::PlatformThread::Delegate { | 33 class MachListenerThreadDelegate : public base::PlatformThread::Delegate { |
| 59 public: | 34 public: |
| 60 MachListenerThreadDelegate(MachBroker* broker) : broker_(broker) { | 35 MachListenerThreadDelegate(MachBroker* broker) : broker_(broker) { |
| 61 DCHECK(broker_); | 36 DCHECK(broker_); |
| 62 std::string port_name = MachBroker::GetMachPortName(); | 37 std::string port_name = MachBroker::GetMachPortName(); |
| 63 | 38 |
| 64 // 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 |
| 65 // important to create and register the receive port before starting the | 40 // important to create and register the receive port before starting the |
| 66 // thread so that child processes will always have someone who's listening. | 41 // thread so that child processes will always have someone who's listening. |
| 67 receive_port_.reset(new base::ReceivePort(port_name.c_str())); | 42 receive_port_.reset(new base::ReceivePort(port_name.c_str())); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 MachBroker* broker_; // weak | 94 MachBroker* broker_; // weak |
| 120 | 95 |
| 121 DISALLOW_COPY_AND_ASSIGN(MachListenerThreadDelegate); | 96 DISALLOW_COPY_AND_ASSIGN(MachListenerThreadDelegate); |
| 122 }; | 97 }; |
| 123 | 98 |
| 124 // Returns the global MachBroker. | 99 // Returns the global MachBroker. |
| 125 MachBroker* MachBroker::GetInstance() { | 100 MachBroker* MachBroker::GetInstance() { |
| 126 return Singleton<MachBroker, LeakySingletonTraits<MachBroker> >::get(); | 101 return Singleton<MachBroker, LeakySingletonTraits<MachBroker> >::get(); |
| 127 } | 102 } |
| 128 | 103 |
| 129 MachBroker::MachBroker() : listener_thread_started_(false) { | |
| 130 } | |
| 131 | |
| 132 MachBroker::~MachBroker() {} | |
| 133 | |
| 134 void MachBroker::EnsureRunning() { | 104 void MachBroker::EnsureRunning() { |
| 135 lock_.AssertAcquired(); | 105 lock_.AssertAcquired(); |
| 136 | 106 |
| 137 if (!listener_thread_started_) { | 107 if (!listener_thread_started_) { |
| 138 listener_thread_started_ = true; | 108 listener_thread_started_ = true; |
| 139 | 109 |
| 140 BrowserThread::PostTask( | 110 BrowserThread::PostTask( |
| 141 BrowserThread::UI, FROM_HERE, new RegisterNotificationTask(this)); | 111 BrowserThread::UI, FROM_HERE, |
| 112 base::Bind(&MachBroker::RegisterNotifications, base::Unretained(this))); |
| 142 | 113 |
| 143 // Intentional leak. This thread is never joined or reaped. | 114 // Intentional leak. This thread is never joined or reaped. |
| 144 base::PlatformThread::CreateNonJoinable( | 115 base::PlatformThread::CreateNonJoinable( |
| 145 0, new MachListenerThreadDelegate(this)); | 116 0, new MachListenerThreadDelegate(this)); |
| 146 } | 117 } |
| 147 } | 118 } |
| 148 | 119 |
| 149 // Adds a placeholder to the map for the given pid with MACH_PORT_NULL. | 120 // Adds a placeholder to the map for the given pid with MACH_PORT_NULL. |
| 150 void MachBroker::AddPlaceholderForPid(base::ProcessHandle pid) { | 121 void MachBroker::AddPlaceholderForPid(base::ProcessHandle pid) { |
| 151 lock_.AssertAcquired(); | 122 lock_.AssertAcquired(); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 | 202 |
| 232 // static | 203 // static |
| 233 std::string MachBroker::GetMachPortName() { | 204 std::string MachBroker::GetMachPortName() { |
| 234 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | 205 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 235 const bool is_child = command_line->HasSwitch(switches::kProcessType); | 206 const bool is_child = command_line->HasSwitch(switches::kProcessType); |
| 236 | 207 |
| 237 // In non-browser (child) processes, use the parent's pid. | 208 // In non-browser (child) processes, use the parent's pid. |
| 238 const pid_t pid = is_child ? getppid() : getpid(); | 209 const pid_t pid = is_child ? getppid() : getpid(); |
| 239 return base::StringPrintf("%s.rohitfork.%d", base::mac::BaseBundleID(), pid); | 210 return base::StringPrintf("%s.rohitfork.%d", base::mac::BaseBundleID(), pid); |
| 240 } | 211 } |
| 212 |
| 213 MachBroker::MachBroker() : listener_thread_started_(false) { |
| 214 } |
| 215 |
| 216 MachBroker::~MachBroker() {} |
| 217 |
| 218 void MachBroker::RegisterNotifications() { |
| 219 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 220 content::NotificationService::AllBrowserContextsAndSources()); |
| 221 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 222 content::NotificationService::AllBrowserContextsAndSources()); |
| 223 registrar_.Add(this, content::NOTIFICATION_CHILD_PROCESS_CRASHED, |
| 224 content::NotificationService::AllBrowserContextsAndSources()); |
| 225 registrar_.Add(this, content::NOTIFICATION_CHILD_PROCESS_HOST_DISCONNECTED, |
| 226 content::NotificationService::AllBrowserContextsAndSources()); |
| 227 } |
| OLD | NEW |