summaryrefslogtreecommitdiffstats
path: root/mojo/shell/runner/host/mach_broker.h
blob: 19b4b8bb98e092450651c24d6a4eaa6075a8bb97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MOJO_SHELL_RUNNER_HOST_MACH_BROKER_H_
#define MOJO_SHELL_RUNNER_HOST_MACH_BROKER_H_

#include "base/mac/mach_port_broker.h"

namespace base {
template <typename T> struct DefaultSingletonTraits;
}

namespace mojo {
namespace shell {

// A global singleton |MachBroker| is used by the shell to provide access to
// Mach task ports for shell out-of-process applications.
class MachBroker {
 public:
  // Sends the task port of the current process to the parent over Mach IPC.
  // For use in child processes.
  static void SendTaskPortToParent();

  // Returns the global |MachBroker|. For use in the shell.
  static MachBroker* GetInstance();

  // Registers |pid| with a MACH_PORT_NULL task port in the port provider. A
  // child's pid must be registered before the broker will accept a task port
  // from that child.
  // Callers MUST acquire the lock given by GetLock() before calling this method
  // (and release the lock afterwards).
  void ExpectPid(base::ProcessHandle pid);

  // Removes |pid| from the port provider.
  // Callers MUST acquire the lock given by GetLock() before calling this method
  // (and release the lock afterwards).
  void RemovePid(base::ProcessHandle pid);

  base::Lock& GetLock() { return broker_.GetLock(); }
  base::PortProvider* port_provider() { return &broker_; }

 private:
  MachBroker();
  ~MachBroker();
  friend struct base::DefaultSingletonTraits<MachBroker>;

  base::MachPortBroker broker_;
};

}  // namespace shell
}  // namespace mojo

#endif  // MOJO_SHELL_RUNNER_HOST_MACH_BROKER_H_