| 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 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 // 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 |
| 32 // 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 |
| 33 // process. This data is kept in a global |MachBroker| object. | 33 // process. This data is kept in a global |MachBroker| object. |
| 34 // | 34 // |
| 35 // 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 |
| 36 // immediately after a child process has been started. | 36 // immediately after a child process has been started. |
| 37 class CONTENT_EXPORT MachBroker : public base::ProcessMetrics::PortProvider, | 37 class CONTENT_EXPORT MachBroker : public base::ProcessMetrics::PortProvider, |
| 38 public BrowserChildProcessObserver, | 38 public BrowserChildProcessObserver, |
| 39 public NotificationObserver { | 39 public NotificationObserver { |
| 40 public: | 40 public: |
| 41 // For use in child processes. This will send the task port of the current |
| 42 // process over Mach IPC to the port registered by name (via this class) in |
| 43 // the parent process. Returns true if the message was sent successfully |
| 44 // and false if otherwise. |
| 45 static bool ChildSendTaskPortToParent(); |
| 46 |
| 41 // Returns the global MachBroker. | 47 // Returns the global MachBroker. |
| 42 static MachBroker* GetInstance(); | 48 static MachBroker* GetInstance(); |
| 43 | 49 |
| 50 // The lock that protects this MachBroker object. Clients MUST acquire and |
| 51 // release this lock around calls to EnsureRunning(), PlaceholderForPid(), |
| 52 // and FinalizePid(). |
| 53 base::Lock& GetLock(); |
| 54 |
| 44 // Performs any necessary setup that cannot happen in the constructor. | 55 // Performs any necessary setup that cannot happen in the constructor. |
| 45 // Callers MUST acquire the lock given by GetLock() before calling this | 56 // Callers MUST acquire the lock given by GetLock() before calling this |
| 46 // method (and release the lock afterwards). | 57 // method (and release the lock afterwards). |
| 47 void EnsureRunning(); | 58 void EnsureRunning(); |
| 48 | 59 |
| 49 struct MachInfo { | |
| 50 MachInfo() : mach_task_(MACH_PORT_NULL) {} | |
| 51 | |
| 52 MachInfo& SetTask(mach_port_t task) { | |
| 53 mach_task_ = task; | |
| 54 return *this; | |
| 55 } | |
| 56 | |
| 57 mach_port_t mach_task_; | |
| 58 }; | |
| 59 | |
| 60 // Adds a placeholder to the map for the given pid with MACH_PORT_NULL. | 60 // Adds a placeholder to the map for the given pid with MACH_PORT_NULL. |
| 61 // Callers are expected to later update the port with FinalizePid(). Callers | 61 // Callers are expected to later update the port with FinalizePid(). Callers |
| 62 // MUST acquire the lock given by GetLock() before calling this method (and | 62 // MUST acquire the lock given by GetLock() before calling this method (and |
| 63 // release the lock afterwards). | 63 // release the lock afterwards). |
| 64 void AddPlaceholderForPid(base::ProcessHandle pid); | 64 void AddPlaceholderForPid(base::ProcessHandle pid); |
| 65 | 65 |
| 66 // Updates the mapping for |pid| to include the given |mach_info|. Does | |
| 67 // nothing if PlaceholderForPid() has not already been called for the given | |
| 68 // |pid|. Callers MUST acquire the lock given by GetLock() before calling | |
| 69 // this method (and release the lock afterwards). | |
| 70 void FinalizePid(base::ProcessHandle pid, const MachInfo& mach_info); | |
| 71 | |
| 72 // Removes all mappings belonging to |pid| from the broker. | |
| 73 void InvalidatePid(base::ProcessHandle pid); | |
| 74 | |
| 75 // The lock that protects this MachBroker object. Clients MUST acquire and | |
| 76 // release this lock around calls to EnsureRunning(), PlaceholderForPid(), | |
| 77 // and FinalizePid(). | |
| 78 base::Lock& GetLock(); | |
| 79 | |
| 80 // Returns the Mach port name to use when sending or receiving messages. | |
| 81 // Does the Right Thing in the browser and in child processes. | |
| 82 static std::string GetMachPortName(); | |
| 83 | |
| 84 // Implement |ProcessMetrics::PortProvider|. | 66 // Implement |ProcessMetrics::PortProvider|. |
| 85 virtual mach_port_t TaskForPid(base::ProcessHandle process) const OVERRIDE; | 67 virtual mach_port_t TaskForPid(base::ProcessHandle process) const OVERRIDE; |
| 86 | 68 |
| 87 // Implement |BrowserChildProcessObserver|. | 69 // Implement |BrowserChildProcessObserver|. |
| 88 virtual void BrowserChildProcessHostDisconnected( | 70 virtual void BrowserChildProcessHostDisconnected( |
| 89 const ChildProcessData& data) OVERRIDE; | 71 const ChildProcessData& data) OVERRIDE; |
| 90 virtual void BrowserChildProcessCrashed( | 72 virtual void BrowserChildProcessCrashed( |
| 91 const ChildProcessData& data) OVERRIDE; | 73 const ChildProcessData& data) OVERRIDE; |
| 92 | 74 |
| 93 // Implement |NotificationObserver|. | 75 // Implement |NotificationObserver|. |
| 94 virtual void Observe(int type, | 76 virtual void Observe(int type, |
| 95 const NotificationSource& source, | 77 const NotificationSource& source, |
| 96 const NotificationDetails& details) OVERRIDE; | 78 const NotificationDetails& details) OVERRIDE; |
| 97 private: | 79 private: |
| 98 friend class MachBrokerTest; | 80 friend class MachBrokerTest; |
| 81 friend class MachListenerThreadDelegate; |
| 99 friend struct DefaultSingletonTraits<MachBroker>; | 82 friend struct DefaultSingletonTraits<MachBroker>; |
| 100 | 83 |
| 101 MachBroker(); | 84 MachBroker(); |
| 102 virtual ~MachBroker(); | 85 virtual ~MachBroker(); |
| 103 | 86 |
| 87 // Updates the mapping for |pid| to include the given |mach_info|. Does |
| 88 // nothing if PlaceholderForPid() has not already been called for the given |
| 89 // |pid|. Callers MUST acquire the lock given by GetLock() before calling |
| 90 // this method (and release the lock afterwards). |
| 91 void FinalizePid(base::ProcessHandle pid, mach_port_t task_port); |
| 92 |
| 93 // Removes all mappings belonging to |pid| from the broker. |
| 94 void InvalidatePid(base::ProcessHandle pid); |
| 95 |
| 96 // Returns the Mach port name to use when sending or receiving messages. |
| 97 // Does the Right Thing in the browser and in child processes. |
| 98 static std::string GetMachPortName(); |
| 104 // Callback used to register notifications on the UI thread. | 99 // Callback used to register notifications on the UI thread. |
| 105 void RegisterNotifications(); | 100 void RegisterNotifications(); |
| 106 | 101 |
| 107 // True if the listener thread has been started. | 102 // True if the listener thread has been started. |
| 108 bool listener_thread_started_; | 103 bool listener_thread_started_; |
| 109 | 104 |
| 110 // Used to register for notifications received by NotificationObserver. | 105 // Used to register for notifications received by NotificationObserver. |
| 111 // Accessed only on the UI thread. | 106 // Accessed only on the UI thread. |
| 112 NotificationRegistrar registrar_; | 107 NotificationRegistrar registrar_; |
| 113 | 108 |
| 114 // Stores mach info for every process in the broker. | 109 // Stores mach info for every process in the broker. |
| 115 typedef std::map<base::ProcessHandle, MachInfo> MachMap; | 110 typedef std::map<base::ProcessHandle, mach_port_t> MachMap; |
| 116 MachMap mach_map_; | 111 MachMap mach_map_; |
| 117 | 112 |
| 118 // Mutex that guards |mach_map_|. | 113 // Mutex that guards |mach_map_|. |
| 119 mutable base::Lock lock_; | 114 mutable base::Lock lock_; |
| 120 | 115 |
| 121 DISALLOW_COPY_AND_ASSIGN(MachBroker); | 116 DISALLOW_COPY_AND_ASSIGN(MachBroker); |
| 122 }; | 117 }; |
| 123 | 118 |
| 124 } // namespace content | 119 } // namespace content |
| 125 | 120 |
| 126 #endif // CONTENT_BROWSER_MACH_BROKER_MAC_H_ | 121 #endif // CONTENT_BROWSER_MACH_BROKER_MAC_H_ |
| OLD | NEW |