1 // Copyright 2015 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 BASE_PROCESS_PORT_PROVIDER_MAC_H_ 6 #define BASE_PROCESS_PORT_PROVIDER_MAC_H_ 7 8 #include <mach/mach.h> 9 10 #include "base/base_export.h" 11 #include "base/macros.h" 12 #include "base/observer_list.h" 13 #include "base/process/process_handle.h" 14 #include "base/synchronization/lock.h" 15 16 namespace base { 17 18 // Abstract base class that provides a mapping from ProcessHandle (pid_t) to the 19 // Mach task port. This replicates task_for_pid(), which requires root 20 // privileges. 21 class BASE_EXPORT PortProvider { 22 public: 23 PortProvider(); 24 virtual ~PortProvider(); 25 26 class Observer { 27 public: ~Observer()28 virtual ~Observer() {}; 29 // Called by the PortProvider to notify observers that the task port was 30 // received for a given process. 31 // No guarantees are made about the thread on which this notification will 32 // be sent. 33 // Observers must not call AddObserver() or RemoveObserver() in this 34 // callback, as doing so will deadlock. 35 virtual void OnReceivedTaskPort(ProcessHandle process) = 0; 36 }; 37 38 // Returns the mach task port for |process| if possible, or else 39 // |MACH_PORT_NULL|. 40 virtual mach_port_t TaskForPid(ProcessHandle process) const = 0; 41 42 // Observer interface. 43 void AddObserver(Observer* observer); 44 void RemoveObserver(Observer* observer); 45 46 protected: 47 // Called by subclasses to send a notification to observers. 48 void NotifyObservers(ProcessHandle process); 49 50 private: 51 // ObserverList is not thread-safe, so |lock_| ensures consistency of 52 // |observer_list_|. 53 base::Lock lock_; 54 base::ObserverList<Observer> observer_list_; 55 56 DISALLOW_COPY_AND_ASSIGN(PortProvider); 57 }; 58 59 } // namespace base 60 61 #endif // BASE_PROCESS_PORT_PROVIDER_MAC_H_ 62