blob: 2f40297f2863761e9fce75a742cdb54ec0ac8aa4 [file] [log] [blame]
erikchencd3e0e7b2015-10-01 22:53:511// 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"
erikchen7c556432015-11-11 22:07:2711#include "base/macros.h"
12#include "base/observer_list.h"
erikchencd3e0e7b2015-10-01 22:53:5113#include "base/process/process_handle.h"
erikchen7c556432015-11-11 22:07:2714#include "base/synchronization/lock.h"
erikchencd3e0e7b2015-10-01 22:53:5115
16namespace 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.
21class BASE_EXPORT PortProvider {
22 public:
erikchen7c556432015-11-11 22:07:2723 PortProvider();
24 virtual ~PortProvider();
25
26 class Observer {
27 public:
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 };
erikchencd3e0e7b2015-10-01 22:53:5137
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;
erikchen7c556432015-11-11 22:07:2741
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);
erikchencd3e0e7b2015-10-01 22:53:5157};
58
59} // namespace base
60
61#endif // BASE_PROCESS_PORT_PROVIDER_MAC_H_