File: mach_port_relay.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (94 lines) | stat: -rw-r--r-- 3,900 bytes parent folder | download
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// 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_EDK_SYSTEM_MACH_PORT_RELAY_H_
#define MOJO_EDK_SYSTEM_MACH_PORT_RELAY_H_

#include <set>

#include "base/macros.h"
#include "base/process/port_provider_mac.h"
#include "base/synchronization/lock.h"
#include "mojo/edk/embedder/scoped_platform_handle.h"
#include "mojo/edk/system/channel.h"

namespace mojo {
namespace edk {

// The MachPortRelay is used by a privileged process, usually the root process,
// to manipulate Mach ports in a child process. Ports can be added to and
// extracted from a child process that has registered itself with the
// |base::PortProvider| used by this class.
class MachPortRelay : public base::PortProvider::Observer {
 public:
  class Observer {
   public:
    // Called by the MachPortRelay to notify observers that a new process is
    // ready for Mach ports to be sent/received. There are no guarantees about
    // the thread this is called on, including the presence of a MessageLoop.
    // Implementations must not call AddObserver() or RemoveObserver() during
    // this function, as doing so will deadlock.
    virtual void OnProcessReady(base::ProcessHandle process) = 0;
  };

  // Used by a child process to receive Mach ports from a sender (privileged)
  // process. Each Mach port in |handles| is interpreted as an intermediate Mach
  // port. It replaces each Mach port with the final Mach port received from the
  // intermediate port. This method takes ownership of the intermediate Mach
  // port and gives ownership of the final Mach port to the caller. Any handles
  // that are not Mach ports will remain unchanged, and the number and ordering
  // of handles is preserved.
  // Returns |false| on failure and there is no guarantee about whether a Mach
  // port is intermediate or final.
  //
  // See SendPortsToProcess() for the definition of intermediate and final Mach
  // ports.
  static bool ReceivePorts(PlatformHandleVector* handles);

  explicit MachPortRelay(base::PortProvider* port_provider);
  ~MachPortRelay() override;

  // Sends the Mach ports attached to |message| to |process|.
  // For each Mach port attached to |message|, a new Mach port, the intermediate
  // port, is created in |process|. The message's Mach port is then sent over
  // this intermediate port and the message is modified to refer to the name of
  // the intermediate port. The Mach port received over the intermediate port in
  // the child is referred to as the final Mach port.
  // Returns |false| on failure and |message| may contain a mix of actual Mach
  // ports and names.
  bool SendPortsToProcess(Channel::Message* message,
                          base::ProcessHandle process);

  // Extracts the Mach ports attached to |message| from |process|.
  // Any Mach ports attached to |message| are names and not actual Mach ports
  // that are valid in this process. For each of those Mach port names, a send
  // right is extracted from |process| and the port name is replaced with the
  // send right.
  // Returns |false| on failure and |message| may contain a mix of actual Mach
  // ports and names.
  bool ExtractPortRights(Channel::Message* message,
                         base::ProcessHandle process);

  // Observer interface.
  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

  base::PortProvider* port_provider() const { return port_provider_; }

 private:
  // base::PortProvider::Observer implementation.
  void OnReceivedTaskPort(base::ProcessHandle process) override;

  base::PortProvider* const port_provider_;

  base::Lock observers_lock_;
  std::set<Observer*> observers_;

  DISALLOW_COPY_AND_ASSIGN(MachPortRelay);
};

}  // namespace edk
}  // namespace mojo

#endif  // MOJO_EDK_SYSTEM_MACH_PORT_RELAY_H_