port_provider_mac.h 1.8 KB

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