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 3 // found in the LICENSE file. 4 5 #ifndef CONTENT_BROWSER_MACH_BROKER_MAC_H_ 6 #define CONTENT_BROWSER_MACH_BROKER_MAC_H_ 7 8 #include <map> 9 #include <string> 10 11 #include <mach/mach.h> 12 13 #include "base/memory/singleton.h" 14 #include "base/process/process_handle.h" 15 #include "base/process/process_metrics.h" 16 #include "base/synchronization/lock.h" 17 #include "content/public/browser/browser_child_process_observer.h" 18 #include "content/public/browser/notification_observer.h" 19 #include "content/public/browser/notification_registrar.h" 20 21 namespace content { 22 23 // On OS X, the mach_port_t of a process is required to collect metrics about 24 // the process. Running |task_for_pid()| is only allowed for privileged code. 25 // However, a process has port rights to all its subprocesses, so let the 26 // browser's child processes send their Mach port to the browser over IPC. 27 // This way, the brower can at least collect metrics of its child processes, 28 // which is what it's most interested in anyway. 29 // 30 // Mach ports can only be sent over Mach IPC, not over the |socketpair()| that 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 33 // process. This data is kept in a global |MachBroker| object. 34 // 35 // Since this data arrives over a separate channel, it is not available 36 // immediately after a child process has been started. 37 class CONTENT_EXPORT MachBroker : public base::ProcessMetrics::PortProvider, 38 public BrowserChildProcessObserver, 39 public NotificationObserver { 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 47 // Returns the Mach port name to use when sending or receiving messages. 48 // Does the Right Thing in the browser and in child processes. 49 static std::string GetMachPortName(); 50 51 // Returns the global MachBroker. 52 static MachBroker* GetInstance(); 53 54 // The lock that protects this MachBroker object. Clients MUST acquire and 55 // release this lock around calls to EnsureRunning(), PlaceholderForPid(), 56 // and FinalizePid(). 57 base::Lock& GetLock(); 58 59 // Performs any necessary setup that cannot happen in the constructor. 60 // Callers MUST acquire the lock given by GetLock() before calling this 61 // method (and release the lock afterwards). 62 void EnsureRunning(); 63 64 // Adds a placeholder to the map for the given pid with MACH_PORT_NULL. 65 // Callers are expected to later update the port with FinalizePid(). Callers 66 // MUST acquire the lock given by GetLock() before calling this method (and 67 // release the lock afterwards). 68 void AddPlaceholderForPid(base::ProcessHandle pid); 69 70 // Implement |ProcessMetrics::PortProvider|. 71 virtual mach_port_t TaskForPid(base::ProcessHandle process) const OVERRIDE; 72 73 // Implement |BrowserChildProcessObserver|. 74 virtual void BrowserChildProcessHostDisconnected( 75 const ChildProcessData& data) OVERRIDE; 76 virtual void BrowserChildProcessCrashed( 77 const ChildProcessData& data) OVERRIDE; 78 79 // Implement |NotificationObserver|. 80 virtual void Observe(int type, 81 const NotificationSource& source, 82 const NotificationDetails& details) OVERRIDE; 83 private: 84 friend class MachBrokerTest; 85 friend class MachListenerThreadDelegate; 86 friend struct DefaultSingletonTraits<MachBroker>; 87 88 MachBroker(); 89 virtual ~MachBroker(); 90 91 // Updates the mapping for |pid| to include the given |mach_info|. Does 92 // nothing if PlaceholderForPid() has not already been called for the given 93 // |pid|. Callers MUST acquire the lock given by GetLock() before calling 94 // this method (and release the lock afterwards). 95 void FinalizePid(base::ProcessHandle pid, mach_port_t task_port); 96 97 // Removes all mappings belonging to |pid| from the broker. 98 void InvalidatePid(base::ProcessHandle pid); 99 100 // Callback used to register notifications on the UI thread. 101 void RegisterNotifications(); 102 103 // True if the listener thread has been started. 104 bool listener_thread_started_; 105 106 // Used to register for notifications received by NotificationObserver. 107 // Accessed only on the UI thread. 108 NotificationRegistrar registrar_; 109 110 // Stores mach info for every process in the broker. 111 typedef std::map<base::ProcessHandle, mach_port_t> MachMap; 112 MachMap mach_map_; 113 114 // Mutex that guards |mach_map_|. 115 mutable base::Lock lock_; 116 117 DISALLOW_COPY_AND_ASSIGN(MachBroker); 118 }; 119 120 } // namespace content 121 122 #endif // CONTENT_BROWSER_MACH_BROKER_MAC_H_ 123