OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef CONTENT_BROWSER_MACH_BROKER_MAC_H_ | 5 #ifndef CONTENT_BROWSER_MACH_BROKER_MAC_H_ |
6 #define CONTENT_BROWSER_MACH_BROKER_MAC_H_ | 6 #define CONTENT_BROWSER_MACH_BROKER_MAC_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include <mach/mach.h> | 11 #include <mach/mach.h> |
12 | 12 |
13 #include "base/memory/singleton.h" | 13 #include "base/memory/singleton.h" |
14 #include "base/process.h" | 14 #include "base/process.h" |
15 #include "base/process_util.h" | 15 #include "base/process_util.h" |
16 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
| 17 #include "content/public/browser/browser_child_process_observer.h" |
17 #include "content/public/browser/notification_observer.h" | 18 #include "content/public/browser/notification_observer.h" |
18 #include "content/public/browser/notification_registrar.h" | 19 #include "content/public/browser/notification_registrar.h" |
19 | 20 |
20 namespace content { | 21 namespace content { |
21 | 22 |
22 // On OS X, the mach_port_t of a process is required to collect metrics about | 23 // On OS X, the mach_port_t of a process is required to collect metrics about |
23 // the process. Running |task_for_pid()| is only allowed for privileged code. | 24 // the process. Running |task_for_pid()| is only allowed for privileged code. |
24 // However, a process has port rights to all its subprocesses, so let the | 25 // However, a process has port rights to all its subprocesses, so let the |
25 // browser's child processes send their Mach port to the browser over IPC. | 26 // browser's child processes send their Mach port to the browser over IPC. |
26 // This way, the brower can at least collect metrics of its child processes, | 27 // This way, the brower can at least collect metrics of its child processes, |
27 // which is what it's most interested in anyway. | 28 // which is what it's most interested in anyway. |
28 // | 29 // |
29 // Mach ports can only be sent over Mach IPC, not over the |socketpair()| that | 30 // Mach ports can only be sent over Mach IPC, not over the |socketpair()| that |
30 // the regular IPC system uses. Hence, the child processes open a Mach | 31 // the regular IPC system uses. Hence, the child processes open a Mach |
31 // connection shortly after launching and ipc their mach data to the browser | 32 // connection shortly after launching and ipc their mach data to the browser |
32 // process. This data is kept in a global |MachBroker| object. | 33 // process. This data is kept in a global |MachBroker| object. |
33 // | 34 // |
34 // Since this data arrives over a separate channel, it is not available | 35 // Since this data arrives over a separate channel, it is not available |
35 // immediately after a child process has been started. | 36 // immediately after a child process has been started. |
36 class CONTENT_EXPORT MachBroker : public base::ProcessMetrics::PortProvider, | 37 class CONTENT_EXPORT MachBroker : public base::ProcessMetrics::PortProvider, |
| 38 public BrowserChildProcessObserver, |
37 public NotificationObserver { | 39 public NotificationObserver { |
38 public: | 40 public: |
39 // Returns the global MachBroker. | 41 // Returns the global MachBroker. |
40 static MachBroker* GetInstance(); | 42 static MachBroker* GetInstance(); |
41 | 43 |
42 // Performs any necessary setup that cannot happen in the constructor. | 44 // Performs any necessary setup that cannot happen in the constructor. |
43 // Callers MUST acquire the lock given by GetLock() before calling this | 45 // Callers MUST acquire the lock given by GetLock() before calling this |
44 // method (and release the lock afterwards). | 46 // method (and release the lock afterwards). |
45 void EnsureRunning(); | 47 void EnsureRunning(); |
46 | 48 |
(...skipping 28 matching lines...) Expand all Loading... |
75 // and FinalizePid(). | 77 // and FinalizePid(). |
76 base::Lock& GetLock(); | 78 base::Lock& GetLock(); |
77 | 79 |
78 // Returns the Mach port name to use when sending or receiving messages. | 80 // Returns the Mach port name to use when sending or receiving messages. |
79 // Does the Right Thing in the browser and in child processes. | 81 // Does the Right Thing in the browser and in child processes. |
80 static std::string GetMachPortName(); | 82 static std::string GetMachPortName(); |
81 | 83 |
82 // Implement |ProcessMetrics::PortProvider|. | 84 // Implement |ProcessMetrics::PortProvider|. |
83 virtual mach_port_t TaskForPid(base::ProcessHandle process) const OVERRIDE; | 85 virtual mach_port_t TaskForPid(base::ProcessHandle process) const OVERRIDE; |
84 | 86 |
| 87 // Implement |BrowserChildProcessObserver|. |
| 88 virtual void BrowserChildProcessHostDisconnected( |
| 89 const ChildProcessData& data) OVERRIDE; |
| 90 virtual void BrowserChildProcessCrashed( |
| 91 const ChildProcessData& data) OVERRIDE; |
| 92 |
85 // Implement |NotificationObserver|. | 93 // Implement |NotificationObserver|. |
86 virtual void Observe(int type, | 94 virtual void Observe(int type, |
87 const NotificationSource& source, | 95 const NotificationSource& source, |
88 const NotificationDetails& details) OVERRIDE; | 96 const NotificationDetails& details) OVERRIDE; |
89 private: | 97 private: |
90 friend class MachBrokerTest; | 98 friend class MachBrokerTest; |
91 friend struct DefaultSingletonTraits<MachBroker>; | 99 friend struct DefaultSingletonTraits<MachBroker>; |
92 | 100 |
93 MachBroker(); | 101 MachBroker(); |
94 virtual ~MachBroker(); | 102 virtual ~MachBroker(); |
(...skipping 14 matching lines...) Expand all Loading... |
109 | 117 |
110 // Mutex that guards |mach_map_|. | 118 // Mutex that guards |mach_map_|. |
111 mutable base::Lock lock_; | 119 mutable base::Lock lock_; |
112 | 120 |
113 DISALLOW_COPY_AND_ASSIGN(MachBroker); | 121 DISALLOW_COPY_AND_ASSIGN(MachBroker); |
114 }; | 122 }; |
115 | 123 |
116 } // namespace content | 124 } // namespace content |
117 | 125 |
118 #endif // CONTENT_BROWSER_MACH_BROKER_MAC_H_ | 126 #endif // CONTENT_BROWSER_MACH_BROKER_MAC_H_ |
OLD | NEW |