[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "content/browser/mach_broker_mac.h" |
| 6 | |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 7 | #include "base/bind.h" |
| 8 | #include "base/bind_helpers.h" |
| 9 | #include "base/command_line.h" |
| 10 | #include "base/logging.h" |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 11 | #include "content/browser/renderer_host/render_process_host_impl.h" |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 12 | #include "content/public/browser/browser_thread.h" |
[email protected] | 348fbaac | 2013-06-11 06:31:51 | [diff] [blame] | 13 | #include "content/public/browser/child_process_data.h" |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 14 | #include "content/public/browser/notification_service.h" |
| 15 | #include "content/public/browser/notification_types.h" |
| 16 | #include "content/public/common/content_switches.h" |
| 17 | |
| 18 | namespace content { |
| 19 | |
| 20 | namespace { |
amistry | 11ca9a5 | 2016-03-04 02:04:49 | [diff] [blame] | 21 | const char kBootstrapName[] = "rohitfork"; |
| 22 | } |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 23 | |
amistry | 11ca9a5 | 2016-03-04 02:04:49 | [diff] [blame] | 24 | // static |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 25 | bool MachBroker::ChildSendTaskPortToParent() { |
amistry | 11ca9a5 | 2016-03-04 02:04:49 | [diff] [blame] | 26 | return base::MachPortBroker::ChildSendTaskPortToParent(kBootstrapName); |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | MachBroker* MachBroker::GetInstance() { |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 30 | return base::Singleton<MachBroker, |
| 31 | base::LeakySingletonTraits<MachBroker>>::get(); |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | base::Lock& MachBroker::GetLock() { |
amistry | 11ca9a5 | 2016-03-04 02:04:49 | [diff] [blame] | 35 | return broker_.GetLock(); |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | void MachBroker::EnsureRunning() { |
amistry | 11ca9a5 | 2016-03-04 02:04:49 | [diff] [blame] | 39 | GetLock().AssertAcquired(); |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 40 | |
rsesek | ca5b918a | 2015-05-14 00:01:15 | [diff] [blame] | 41 | if (initialized_) |
| 42 | return; |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 43 | |
rsesek | ca5b918a | 2015-05-14 00:01:15 | [diff] [blame] | 44 | // Do not attempt to reinitialize in the event of failure. |
| 45 | initialized_ = true; |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 46 | |
rsesek | ca5b918a | 2015-05-14 00:01:15 | [diff] [blame] | 47 | BrowserThread::PostTask( |
| 48 | BrowserThread::UI, FROM_HERE, |
| 49 | base::Bind(&MachBroker::RegisterNotifications, base::Unretained(this))); |
| 50 | |
amistry | 11ca9a5 | 2016-03-04 02:04:49 | [diff] [blame] | 51 | if (!broker_.Init()) { |
rsesek | ca5b918a | 2015-05-14 00:01:15 | [diff] [blame] | 52 | LOG(ERROR) << "Failed to initialize the MachListenerThreadDelegate"; |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
sramajay | 52ac072b | 2014-12-02 17:18:35 | [diff] [blame] | 56 | void MachBroker::AddPlaceholderForPid(base::ProcessHandle pid, |
| 57 | int child_process_id) { |
amistry | 11ca9a5 | 2016-03-04 02:04:49 | [diff] [blame] | 58 | GetLock().AssertAcquired(); |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 59 | |
amistry | 11ca9a5 | 2016-03-04 02:04:49 | [diff] [blame] | 60 | broker_.AddPlaceholderForPid(pid); |
sramajay | 52ac072b | 2014-12-02 17:18:35 | [diff] [blame] | 61 | child_process_id_map_[child_process_id] = pid; |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | mach_port_t MachBroker::TaskForPid(base::ProcessHandle pid) const { |
amistry | 11ca9a5 | 2016-03-04 02:04:49 | [diff] [blame] | 65 | return broker_.TaskForPid(pid); |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void MachBroker::BrowserChildProcessHostDisconnected( |
| 69 | const ChildProcessData& data) { |
sramajay | 52ac072b | 2014-12-02 17:18:35 | [diff] [blame] | 70 | InvalidateChildProcessId(data.id); |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 71 | } |
| 72 | |
wfh | 22e2f4a2 | 2015-04-28 22:39:21 | [diff] [blame] | 73 | void MachBroker::BrowserChildProcessCrashed(const ChildProcessData& data, |
| 74 | int exit_code) { |
sramajay | 52ac072b | 2014-12-02 17:18:35 | [diff] [blame] | 75 | InvalidateChildProcessId(data.id); |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void MachBroker::Observe(int type, |
| 79 | const NotificationSource& source, |
| 80 | const NotificationDetails& details) { |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 81 | switch (type) { |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 82 | case NOTIFICATION_RENDERER_PROCESS_TERMINATED: |
sramajay | 52ac072b | 2014-12-02 17:18:35 | [diff] [blame] | 83 | case NOTIFICATION_RENDERER_PROCESS_CLOSED: { |
| 84 | RenderProcessHost* host = Source<RenderProcessHost>(source).ptr(); |
| 85 | InvalidateChildProcessId(host->GetID()); |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 86 | break; |
sramajay | 52ac072b | 2014-12-02 17:18:35 | [diff] [blame] | 87 | } |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 88 | default: |
| 89 | NOTREACHED() << "Unexpected notification"; |
| 90 | break; |
| 91 | } |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 92 | } |
| 93 | |
rsesek | dba8411 | 2015-09-18 19:22:07 | [diff] [blame] | 94 | // static |
| 95 | std::string MachBroker::GetMachPortName() { |
| 96 | const base::CommandLine* command_line = |
| 97 | base::CommandLine::ForCurrentProcess(); |
| 98 | const bool is_child = command_line->HasSwitch(switches::kProcessType); |
amistry | 11ca9a5 | 2016-03-04 02:04:49 | [diff] [blame] | 99 | return base::MachPortBroker::GetMachPortName(kBootstrapName, is_child); |
rsesek | dba8411 | 2015-09-18 19:22:07 | [diff] [blame] | 100 | } |
| 101 | |
amistry | db227315 | 2016-03-10 23:45:40 | [diff] [blame] | 102 | MachBroker::MachBroker() : initialized_(false), broker_(kBootstrapName) { |
| 103 | broker_.AddObserver(this); |
| 104 | } |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 105 | |
amistry | db227315 | 2016-03-10 23:45:40 | [diff] [blame] | 106 | MachBroker::~MachBroker() { |
| 107 | broker_.RemoveObserver(this); |
| 108 | } |
| 109 | |
| 110 | void MachBroker::OnReceivedTaskPort(base::ProcessHandle process) { |
| 111 | NotifyObservers(process); |
| 112 | } |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 113 | |
sramajay | 52ac072b | 2014-12-02 17:18:35 | [diff] [blame] | 114 | void MachBroker::InvalidateChildProcessId(int child_process_id) { |
amistry | 11ca9a5 | 2016-03-04 02:04:49 | [diff] [blame] | 115 | base::AutoLock lock(GetLock()); |
sramajay | 52ac072b | 2014-12-02 17:18:35 | [diff] [blame] | 116 | MachBroker::ChildProcessIdMap::iterator it = |
| 117 | child_process_id_map_.find(child_process_id); |
| 118 | if (it == child_process_id_map_.end()) |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 119 | return; |
| 120 | |
amistry | 11ca9a5 | 2016-03-04 02:04:49 | [diff] [blame] | 121 | broker_.InvalidatePid(it->second); |
sramajay | 52ac072b | 2014-12-02 17:18:35 | [diff] [blame] | 122 | child_process_id_map_.erase(it); |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 123 | } |
| 124 | |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 125 | void MachBroker::RegisterNotifications() { |
| 126 | registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 127 | NotificationService::AllBrowserContextsAndSources()); |
| 128 | registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 129 | NotificationService::AllBrowserContextsAndSources()); |
| 130 | |
| 131 | // No corresponding StopObservingBrowserChildProcesses, |
| 132 | // we leak this singleton. |
| 133 | BrowserChildProcessObserver::Add(this); |
| 134 | } |
| 135 | |
| 136 | } // namespace content |