port_provider_mac.h 1.8 KB

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