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 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include <mach/mach.h> | 12 #include <mach/mach.h> |
13 | 13 |
14 #include "base/lock.h" | |
15 #include "base/process.h" | 14 #include "base/process.h" |
16 #include "base/process_util.h" | 15 #include "base/process_util.h" |
17 #include "base/singleton.h" | 16 #include "base/singleton.h" |
| 17 #include "base/synchronization/lock.h" |
18 #include "chrome/common/notification_observer.h" | 18 #include "chrome/common/notification_observer.h" |
19 #include "chrome/common/notification_registrar.h" | 19 #include "chrome/common/notification_registrar.h" |
20 | 20 |
21 // On OS X, the mach_port_t of a process is required to collect metrics about | 21 // On OS X, the mach_port_t of a process is required to collect metrics about |
22 // the process. Running |task_for_pid()| is only allowed for privileged code. | 22 // the process. Running |task_for_pid()| is only allowed for privileged code. |
23 // However, a process has port rights to all its subprocesses, so let the | 23 // However, a process has port rights to all its subprocesses, so let the |
24 // browser's child processes send their Mach port to the browser over IPC. | 24 // browser's child processes send their Mach port to the browser over IPC. |
25 // This way, the brower can at least collect metrics of its child processes, | 25 // This way, the brower can at least collect metrics of its child processes, |
26 // which is what it's most interested in anyway. | 26 // which is what it's most interested in anyway. |
27 // | 27 // |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 // nothing if PlaceholderForPid() has not already been called for the given | 63 // nothing if PlaceholderForPid() has not already been called for the given |
64 // |pid|. Callers MUST acquire the lock given by GetLock() before calling | 64 // |pid|. Callers MUST acquire the lock given by GetLock() before calling |
65 // this method (and release the lock afterwards). | 65 // this method (and release the lock afterwards). |
66 void FinalizePid(base::ProcessHandle pid, const MachInfo& mach_info); | 66 void FinalizePid(base::ProcessHandle pid, const MachInfo& mach_info); |
67 | 67 |
68 // Removes all mappings belonging to |pid| from the broker. | 68 // Removes all mappings belonging to |pid| from the broker. |
69 void InvalidatePid(base::ProcessHandle pid); | 69 void InvalidatePid(base::ProcessHandle pid); |
70 | 70 |
71 // The lock that protects this MachBroker object. Clients MUST acquire and | 71 // The lock that protects this MachBroker object. Clients MUST acquire and |
72 // release this lock around calls to PlaceholderForPid() and FinalizePid(). | 72 // release this lock around calls to PlaceholderForPid() and FinalizePid(). |
73 Lock& GetLock(); | 73 base::Lock& GetLock(); |
74 | 74 |
75 // Returns the Mach port name to use when sending or receiving messages. | 75 // Returns the Mach port name to use when sending or receiving messages. |
76 // Does the Right Thing in the browser and in child processes. | 76 // Does the Right Thing in the browser and in child processes. |
77 static std::string GetMachPortName(); | 77 static std::string GetMachPortName(); |
78 | 78 |
79 // Implement |ProcessMetrics::PortProvider|. | 79 // Implement |ProcessMetrics::PortProvider|. |
80 virtual mach_port_t TaskForPid(base::ProcessHandle process) const; | 80 virtual mach_port_t TaskForPid(base::ProcessHandle process) const; |
81 | 81 |
82 // Implement |NotificationObserver|. | 82 // Implement |NotificationObserver|. |
83 virtual void Observe(NotificationType type, | 83 virtual void Observe(NotificationType type, |
84 const NotificationSource& source, | 84 const NotificationSource& source, |
85 const NotificationDetails& details); | 85 const NotificationDetails& details); |
86 private: | 86 private: |
87 // Private constructor. | 87 // Private constructor. |
88 MachBroker(); | 88 MachBroker(); |
89 | 89 |
90 // True if the listener thread has been started. | 90 // True if the listener thread has been started. |
91 bool listener_thread_started_; | 91 bool listener_thread_started_; |
92 | 92 |
93 // Used to register for notifications received by NotificationObserver. | 93 // Used to register for notifications received by NotificationObserver. |
94 // Accessed only on the UI thread. | 94 // Accessed only on the UI thread. |
95 NotificationRegistrar registrar_; | 95 NotificationRegistrar registrar_; |
96 | 96 |
97 // Stores mach info for every process in the broker. | 97 // Stores mach info for every process in the broker. |
98 typedef std::map<base::ProcessHandle, MachInfo> MachMap; | 98 typedef std::map<base::ProcessHandle, MachInfo> MachMap; |
99 MachMap mach_map_; | 99 MachMap mach_map_; |
100 | 100 |
101 // Mutex that guards |mach_map_|. | 101 // Mutex that guards |mach_map_|. |
102 mutable Lock lock_; | 102 mutable base::Lock lock_; |
103 | 103 |
104 friend class MachBrokerTest; | 104 friend class MachBrokerTest; |
105 friend class RegisterNotificationTask; | 105 friend class RegisterNotificationTask; |
106 // Needed in order to make the constructor private. | 106 // Needed in order to make the constructor private. |
107 friend struct DefaultSingletonTraits<MachBroker>; | 107 friend struct DefaultSingletonTraits<MachBroker>; |
108 DISALLOW_COPY_AND_ASSIGN(MachBroker); | 108 DISALLOW_COPY_AND_ASSIGN(MachBroker); |
109 }; | 109 }; |
110 | 110 |
111 #endif // CHROME_BROWSER_MACH_BROKER_H_ | 111 #endif // CHROME_BROWSER_MACH_BROKER_H_ |
112 | 112 |
OLD | NEW |