• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Chromium Authors
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/memory/scoped_refptr.h"
12 #include "base/observer_list_threadsafe.h"
13 #include "base/process/process_handle.h"
14 
15 namespace base {
16 
17 // Abstract base class that provides a mapping from ProcessHandle (pid_t) to the
18 // Mach task port. This replicates task_for_pid(), which requires root
19 // privileges.
20 class BASE_EXPORT PortProvider {
21  public:
22   PortProvider();
23 
24   PortProvider(const PortProvider&) = delete;
25   PortProvider& operator=(const PortProvider&) = delete;
26 
27   virtual ~PortProvider();
28 
29   class Observer {
30    public:
~Observer()31     virtual ~Observer() {}
32     // Called by the PortProvider to notify observers that the task port was
33     // received for a given process.
34     // This notification is guaranteed to be sent on the same task runner where
35     // the observer was added.
36     virtual void OnReceivedTaskPort(ProcessHandle process) = 0;
37   };
38 
39   // Returns the mach task port for |process| if possible, or else
40   // |MACH_PORT_NULL|.
41   virtual mach_port_t TaskForPid(ProcessHandle process) const = 0;
42 
43   // Observer interface.
44   void AddObserver(Observer* observer);
45   void RemoveObserver(Observer* observer);
46 
47  protected:
48   // Called by subclasses to send a notification to observers.
49   void NotifyObservers(ProcessHandle process);
50 
51  private:
52   scoped_refptr<base::ObserverListThreadSafe<Observer>> observer_list_;
53 };
54 
55 // Port provider that returns the calling process's task port, ignoring its
56 // argument.
57 class BASE_EXPORT SelfPortProvider : public base::PortProvider {
58   mach_port_t TaskForPid(base::ProcessHandle process) const override;
59 };
60 
61 }  // namespace base
62 
63 #endif  // BASE_PROCESS_PORT_PROVIDER_MAC_H_
64