OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 MOJO_EDK_SYSTEM_MACH_PORT_RELAY_H_ |
| 6 #define MOJO_EDK_SYSTEM_MACH_PORT_RELAY_H_ |
| 7 |
| 8 #include <set> |
| 9 |
| 10 #include "base/macros.h" |
| 11 #include "base/process/port_provider_mac.h" |
| 12 #include "base/synchronization/lock.h" |
| 13 #include "mojo/edk/embedder/scoped_platform_handle.h" |
| 14 #include "mojo/edk/system/channel.h" |
| 15 |
| 16 namespace mojo { |
| 17 namespace edk { |
| 18 |
| 19 // The MachPortRelay is used by a privileged process, usually the root process, |
| 20 // to manipulate Mach ports in a child process. Ports can be added to and |
| 21 // extracted from a child process that has registered itself with the |
| 22 // |base::PortProvider| used by this class. |
| 23 class MachPortRelay : public base::PortProvider::Observer { |
| 24 public: |
| 25 class Observer { |
| 26 public: |
| 27 // Called by the MachPortRelay to notify observers that a new process is |
| 28 // ready for Mach ports to be sent/received. There are no guarantees about |
| 29 // the thread this is called on, including the presence of a MessageLoop. |
| 30 // Implementations must not call AddObserver() or RemoveObserver() during |
| 31 // this function, as doing so will deadlock. |
| 32 virtual void OnProcessReady(base::ProcessHandle process) = 0; |
| 33 }; |
| 34 |
| 35 // Used by a child process to receive Mach ports from a sender (privileged) |
| 36 // process. Each Mach port in |handles| is interpreted as an intermediate Mach |
| 37 // port. It replaces each Mach port with the final Mach port received from the |
| 38 // intermediate port. This method takes ownership of the intermediate Mach |
| 39 // port and gives ownership of the final Mach port to the caller. Any handles |
| 40 // that are not Mach ports will remain unchanged, and the number and ordering |
| 41 // of handles is preserved. |
| 42 // Returns |false| on failure and there is no guarantee about whether a Mach |
| 43 // port is intermediate or final. |
| 44 // |
| 45 // See SendPortsToProcess() for the definition of intermediate and final Mach |
| 46 // ports. |
| 47 static bool ReceivePorts(PlatformHandleVector* handles); |
| 48 |
| 49 explicit MachPortRelay(base::PortProvider* port_provider); |
| 50 ~MachPortRelay() override; |
| 51 |
| 52 // Sends the Mach ports attached to |message| to |process|. |
| 53 // For each Mach port attached to |message|, a new Mach port, the intermediate |
| 54 // port, is created in |process|. The message's Mach port is then sent over |
| 55 // this intermediate port and the message is modified to refer to the name of |
| 56 // the intermediate port. The Mach port received over the intermediate port in |
| 57 // the child is referred to as the final Mach port. |
| 58 // Returns |false| on failure and |message| may contain a mix of actual Mach |
| 59 // ports and names. |
| 60 bool SendPortsToProcess(Channel::Message* message, |
| 61 base::ProcessHandle process); |
| 62 |
| 63 // Extracts the Mach ports attached to |message| from |process|. |
| 64 // Any Mach ports attached to |message| are names and not actual Mach ports |
| 65 // that are valid in this process. For each of those Mach port names, a send |
| 66 // right is extracted from |process| and the port name is replaced with the |
| 67 // send right. |
| 68 // Returns |false| on failure and |message| may contain a mix of actual Mach |
| 69 // ports and names. |
| 70 bool ExtractPortRights(Channel::Message* message, |
| 71 base::ProcessHandle process); |
| 72 |
| 73 // Observer interface. |
| 74 void AddObserver(Observer* observer); |
| 75 void RemoveObserver(Observer* observer); |
| 76 |
| 77 base::PortProvider* port_provider() const { return port_provider_; } |
| 78 |
| 79 private: |
| 80 // base::PortProvider::Observer implementation. |
| 81 void OnReceivedTaskPort(base::ProcessHandle process) override; |
| 82 |
| 83 base::PortProvider* const port_provider_; |
| 84 |
| 85 base::Lock observers_lock_; |
| 86 std::set<Observer*> observers_; |
| 87 |
| 88 DISALLOW_COPY_AND_ASSIGN(MachPortRelay); |
| 89 }; |
| 90 |
| 91 } // namespace edk |
| 92 } // namespace mojo |
| 93 |
| 94 #endif // MOJO_EDK_SYSTEM_MACH_PORT_RELAY_H_ |
OLD | NEW |