| 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/notification_observer.h" | 17 #include "content/public/browser/notification_observer.h" |
| 18 #include "content/public/browser/notification_registrar.h" | 18 #include "content/public/browser/notification_registrar.h" |
| 19 | 19 |
| 20 namespace content { |
| 21 |
| 20 // On OS X, the mach_port_t of a process is required to collect metrics about | 22 // On OS X, the mach_port_t of a process is required to collect metrics about |
| 21 // the process. Running |task_for_pid()| is only allowed for privileged code. | 23 // the process. Running |task_for_pid()| is only allowed for privileged code. |
| 22 // However, a process has port rights to all its subprocesses, so let the | 24 // However, a process has port rights to all its subprocesses, so let the |
| 23 // browser's child processes send their Mach port to the browser over IPC. | 25 // browser's child processes send their Mach port to the browser over IPC. |
| 24 // This way, the brower can at least collect metrics of its child processes, | 26 // This way, the brower can at least collect metrics of its child processes, |
| 25 // which is what it's most interested in anyway. | 27 // which is what it's most interested in anyway. |
| 26 // | 28 // |
| 27 // Mach ports can only be sent over Mach IPC, not over the |socketpair()| that | 29 // Mach ports can only be sent over Mach IPC, not over the |socketpair()| that |
| 28 // the regular IPC system uses. Hence, the child processes open a Mach | 30 // the regular IPC system uses. Hence, the child processes open a Mach |
| 29 // connection shortly after launching and ipc their mach data to the browser | 31 // connection shortly after launching and ipc their mach data to the browser |
| 30 // process. This data is kept in a global |MachBroker| object. | 32 // process. This data is kept in a global |MachBroker| object. |
| 31 // | 33 // |
| 32 // Since this data arrives over a separate channel, it is not available | 34 // Since this data arrives over a separate channel, it is not available |
| 33 // immediately after a child process has been started. | 35 // immediately after a child process has been started. |
| 34 class CONTENT_EXPORT MachBroker : public base::ProcessMetrics::PortProvider, | 36 class CONTENT_EXPORT MachBroker : public base::ProcessMetrics::PortProvider, |
| 35 public content::NotificationObserver { | 37 public NotificationObserver { |
| 36 public: | 38 public: |
| 37 // Returns the global MachBroker. | 39 // Returns the global MachBroker. |
| 38 static MachBroker* GetInstance(); | 40 static MachBroker* GetInstance(); |
| 39 | 41 |
| 40 // Performs any necessary setup that cannot happen in the constructor. | 42 // Performs any necessary setup that cannot happen in the constructor. |
| 41 // Callers MUST acquire the lock given by GetLock() before calling this | 43 // Callers MUST acquire the lock given by GetLock() before calling this |
| 42 // method (and release the lock afterwards). | 44 // method (and release the lock afterwards). |
| 43 void EnsureRunning(); | 45 void EnsureRunning(); |
| 44 | 46 |
| 45 struct MachInfo { | 47 struct MachInfo { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 73 // and FinalizePid(). | 75 // and FinalizePid(). |
| 74 base::Lock& GetLock(); | 76 base::Lock& GetLock(); |
| 75 | 77 |
| 76 // Returns the Mach port name to use when sending or receiving messages. | 78 // Returns the Mach port name to use when sending or receiving messages. |
| 77 // Does the Right Thing in the browser and in child processes. | 79 // Does the Right Thing in the browser and in child processes. |
| 78 static std::string GetMachPortName(); | 80 static std::string GetMachPortName(); |
| 79 | 81 |
| 80 // Implement |ProcessMetrics::PortProvider|. | 82 // Implement |ProcessMetrics::PortProvider|. |
| 81 virtual mach_port_t TaskForPid(base::ProcessHandle process) const OVERRIDE; | 83 virtual mach_port_t TaskForPid(base::ProcessHandle process) const OVERRIDE; |
| 82 | 84 |
| 83 // Implement |content::NotificationObserver|. | 85 // Implement |NotificationObserver|. |
| 84 virtual void Observe(int type, | 86 virtual void Observe(int type, |
| 85 const content::NotificationSource& source, | 87 const NotificationSource& source, |
| 86 const content::NotificationDetails& details) OVERRIDE; | 88 const NotificationDetails& details) OVERRIDE; |
| 87 private: | 89 private: |
| 88 friend class MachBrokerTest; | 90 friend class MachBrokerTest; |
| 89 friend struct DefaultSingletonTraits<MachBroker>; | 91 friend struct DefaultSingletonTraits<MachBroker>; |
| 90 | 92 |
| 91 MachBroker(); | 93 MachBroker(); |
| 92 virtual ~MachBroker(); | 94 virtual ~MachBroker(); |
| 93 | 95 |
| 94 // Callback used to register notifications on the UI thread. | 96 // Callback used to register notifications on the UI thread. |
| 95 void RegisterNotifications(); | 97 void RegisterNotifications(); |
| 96 | 98 |
| 97 // True if the listener thread has been started. | 99 // True if the listener thread has been started. |
| 98 bool listener_thread_started_; | 100 bool listener_thread_started_; |
| 99 | 101 |
| 100 // Used to register for notifications received by NotificationObserver. | 102 // Used to register for notifications received by NotificationObserver. |
| 101 // Accessed only on the UI thread. | 103 // Accessed only on the UI thread. |
| 102 content::NotificationRegistrar registrar_; | 104 NotificationRegistrar registrar_; |
| 103 | 105 |
| 104 // Stores mach info for every process in the broker. | 106 // Stores mach info for every process in the broker. |
| 105 typedef std::map<base::ProcessHandle, MachInfo> MachMap; | 107 typedef std::map<base::ProcessHandle, MachInfo> MachMap; |
| 106 MachMap mach_map_; | 108 MachMap mach_map_; |
| 107 | 109 |
| 108 // Mutex that guards |mach_map_|. | 110 // Mutex that guards |mach_map_|. |
| 109 mutable base::Lock lock_; | 111 mutable base::Lock lock_; |
| 110 | 112 |
| 111 DISALLOW_COPY_AND_ASSIGN(MachBroker); | 113 DISALLOW_COPY_AND_ASSIGN(MachBroker); |
| 112 }; | 114 }; |
| 113 | 115 |
| 116 } // namespace content |
| 117 |
| 114 #endif // CONTENT_BROWSER_MACH_BROKER_MAC_H_ | 118 #endif // CONTENT_BROWSER_MACH_BROKER_MAC_H_ |
| OLD | NEW |