OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 CHROME_BROWSER_MACH_BROKER_H_ | 5 #ifndef CHROME_BROWSER_MACH_BROKER_H_ |
6 #define CHROME_BROWSER_MACH_BROKER_H_ | 6 #define CHROME_BROWSER_MACH_BROKER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include <mach/mach.h> | 10 #include <mach/mach.h> |
11 | 11 |
12 #include "base/lock.h" | 12 #include "base/lock.h" |
13 #include "base/process.h" | 13 #include "base/process.h" |
14 #include "base/process_util.h" | 14 #include "base/process_util.h" |
15 #include "base/singleton.h" | 15 #include "base/singleton.h" |
| 16 #include "chrome/common/notification_registrar.h" |
16 | 17 |
17 // On OS X, the mach_port_t of a process is required to collect metrics about | 18 // On OS X, the mach_port_t of a process is required to collect metrics about |
18 // the process. Running |task_for_pid()| is only allowed for privileged code. | 19 // the process. Running |task_for_pid()| is only allowed for privileged code. |
19 // However, a process has port rights to all its subprocesses, so let the | 20 // However, a process has port rights to all its subprocesses, so let the |
20 // browser's child processes send their Mach port to the browser over IPC. | 21 // browser's child processes send their Mach port to the browser over IPC. |
21 // This way, the brower can at least collect metrics of its child processes, | 22 // This way, the brower can at least collect metrics of its child processes, |
22 // which is what it's most interested in anyway. | 23 // which is what it's most interested in anyway. |
23 // | 24 // |
24 // Mach ports can only be sent over Mach IPC, not over the |socketpair()| that | 25 // Mach ports can only be sent over Mach IPC, not over the |socketpair()| that |
25 // the regular IPC system uses. Hence, the child processes open a Mach | 26 // the regular IPC system uses. Hence, the child processes open a Mach |
26 // connection shortly after launching and ipc their mach data to the browser | 27 // connection shortly after launching and ipc their mach data to the browser |
27 // process. This data is kept in a global |MachBroker| object. | 28 // process. This data is kept in a global |MachBroker| object. |
28 // | 29 // |
29 // Since this data arrives over a separate channel, it is not available | 30 // Since this data arrives over a separate channel, it is not available |
30 // immediately after a child process has been started. | 31 // immediately after a child process has been started. |
31 class MachBroker : public base::ProcessMetrics::PortProvider { | 32 class MachBroker : public base::ProcessMetrics::PortProvider, |
| 33 public NotificationObserver { |
32 public: | 34 public: |
33 // Returns the global MachBroker. | 35 // Returns the global MachBroker. |
34 static MachBroker* instance(); | 36 static MachBroker* instance(); |
35 | 37 |
36 struct MachInfo { | 38 struct MachInfo { |
37 MachInfo() : mach_task_(MACH_PORT_NULL) {} | 39 MachInfo() : mach_task_(MACH_PORT_NULL) {} |
38 | 40 |
39 MachInfo& SetTask(mach_port_t task) { | 41 MachInfo& SetTask(mach_port_t task) { |
40 mach_task_ = task; | 42 mach_task_ = task; |
41 return *this; | 43 return *this; |
42 } | 44 } |
43 | 45 |
44 mach_port_t mach_task_; | 46 mach_port_t mach_task_; |
45 }; | 47 }; |
46 | 48 |
47 // Adds mach info for a given pid. | 49 // Adds mach info for a given pid. |
48 void RegisterPid(base::ProcessHandle pid, const MachInfo& mach_info); | 50 void RegisterPid(base::ProcessHandle pid, const MachInfo& mach_info); |
49 | 51 |
50 // Removes all mappings belonging to |pid| from the broker. | 52 // Removes all mappings belonging to |pid| from the broker. |
51 void Invalidate(base::ProcessHandle pid); | 53 void Invalidate(base::ProcessHandle pid); |
52 | 54 |
53 // Implement |ProcessMetrics::PortProvider|. | 55 // Implement |ProcessMetrics::PortProvider|. |
54 virtual mach_port_t TaskForPid(base::ProcessHandle process) const; | 56 virtual mach_port_t TaskForPid(base::ProcessHandle process) const; |
55 | 57 |
| 58 // Implement |NotificationObserver|. |
| 59 virtual void Observe(NotificationType type, |
| 60 const NotificationSource& source, |
| 61 const NotificationDetails& details); |
56 private: | 62 private: |
57 // Private constructor. | 63 // Private constructor. |
58 MachBroker() {} | 64 MachBroker(); |
| 65 |
| 66 // Used to register for notifications received by NotificationObserver. |
| 67 // Accessed only on the UI thread. |
| 68 NotificationRegistrar registrar_; |
| 69 |
59 friend struct DefaultSingletonTraits<MachBroker>; | 70 friend struct DefaultSingletonTraits<MachBroker>; |
60 friend class MachBrokerTest; | 71 friend class MachBrokerTest; |
61 | 72 |
62 // Stores mach info for every process in the broker. | 73 // Stores mach info for every process in the broker. |
63 typedef std::map<base::ProcessHandle, MachInfo> MachMap; | 74 typedef std::map<base::ProcessHandle, MachInfo> MachMap; |
64 MachMap mach_map_; | 75 MachMap mach_map_; |
65 | 76 |
66 // Mutex that guards |mach_map_|. | 77 // Mutex that guards |mach_map_|. |
67 mutable Lock lock_; | 78 mutable Lock lock_; |
68 | 79 |
| 80 friend class RegisterNotificationTask; |
69 DISALLOW_COPY_AND_ASSIGN(MachBroker); | 81 DISALLOW_COPY_AND_ASSIGN(MachBroker); |
70 }; | 82 }; |
71 | 83 |
72 #endif // CHROME_BROWSER_MACH_BROKER_H_ | 84 #endif // CHROME_BROWSER_MACH_BROKER_H_ |
73 | 85 |
OLD | NEW |