Chromium Code Reviews| 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 BASE_MAC_MACH_PORT_BROKER_H_ | |
| 6 #define BASE_MAC_MACH_PORT_BROKER_H_ | |
| 7 | |
| 8 #include <mach/mach.h> | |
| 9 | |
| 10 #include <map> | |
| 11 #include <string> | |
| 12 | |
| 13 #include "base/base_export.h" | |
| 14 #include "base/mac/dispatch_source_mach.h" | |
| 15 #include "base/mac/scoped_mach_port.h" | |
| 16 #include "base/macros.h" | |
| 17 #include "base/memory/scoped_ptr.h" | |
| 18 #include "base/process/port_provider_mac.h" | |
| 19 #include "base/process/process_handle.h" | |
| 20 #include "base/synchronization/lock.h" | |
| 21 | |
| 22 namespace content { | |
| 23 class MachBrokerTest; | |
|
erikchen
2016/03/02 06:56:11
A test in content/ needs access to private members
Anand Mistry (off Chromium)
2016/03/03 01:57:39
I kinda worked around this. I figured the existing
| |
| 24 }; | |
| 25 | |
| 26 namespace base { | |
| 27 | |
| 28 // On OS X, the task port of a process is required to collect metrics about the | |
| 29 // process, and to insert Mach ports into the process. Running |task_for_pid()| | |
| 30 // is only allowed for privileged code. However, a process has port rights to | |
| 31 // all its subprocesses, so let the child processes send their Mach port to the | |
| 32 // parent over IPC. | |
| 33 // | |
| 34 // Mach ports can only be sent over Mach IPC, not over the |socketpair()| that | |
| 35 // the regular IPC system uses. Hence, the child processes opens a Mach | |
| 36 // connection shortly after launching and ipc their mach data to the parent | |
| 37 // process. A single |MachPortBroker| with a given name is expected to exist in | |
| 38 // the parent process. | |
| 39 // | |
| 40 // Since this data arrives over a separate channel, it is not available | |
| 41 // immediately after a child process has been started. | |
| 42 class BASE_EXPORT MachPortBroker : public base::PortProvider { | |
| 43 public: | |
| 44 // For use in child processes. This will send the task port of the current | |
| 45 // process over Mach IPC to the port registered by name (via this class) in | |
| 46 // the parent process. Returns true if the message was sent successfully | |
| 47 // and false if otherwise. | |
| 48 static bool ChildSendTaskPortToParent(const std::string& name); | |
| 49 | |
| 50 // Returns the Mach port name to use when sending or receiving messages. | |
| 51 // Does the Right Thing in the browser and in child processes. | |
| 52 static std::string GetMachPortName(const std::string& name, bool is_child); | |
| 53 | |
| 54 MachPortBroker(const std::string& name); | |
| 55 ~MachPortBroker() override; | |
| 56 | |
| 57 // Performs any initialization work. | |
| 58 bool Init(); | |
| 59 | |
| 60 // Adds a placeholder to the map for the given pid with MACH_PORT_NULL. | |
| 61 // Callers are expected to later update the port with FinalizePid(). Callers | |
| 62 // MUST acquire the lock given by GetLock() before calling this method (and | |
| 63 // release the lock afterwards). | |
| 64 void AddPlaceholderForPid(base::ProcessHandle pid); | |
| 65 | |
| 66 // Removes |pid| from the task port map. Callers MUST acquire the lock given | |
| 67 // by GetLock() before calling this method (and release the lock afterwards). | |
| 68 void InvalidatePid(base::ProcessHandle pid); | |
| 69 | |
| 70 // The lock that protects this MachPortBroker object. Callers MUST acquire | |
| 71 // and release this lock around calls to AddPlaceholderForPid(), | |
| 72 // InvalidatePid(), and FinalizePid(); | |
| 73 base::Lock& GetLock() { return lock_; } | |
| 74 | |
| 75 // Implement |base::PortProvider|. | |
| 76 mach_port_t TaskForPid(base::ProcessHandle process) const override; | |
| 77 | |
| 78 private: | |
| 79 friend class content::MachBrokerTest; | |
| 80 friend class MachPortBrokerTest; | |
| 81 | |
| 82 // Message handler that is invoked on |dispatch_source_| when an | |
| 83 // incoming message needs to be received. | |
| 84 void HandleRequest(); | |
| 85 | |
| 86 // Updates the mapping for |pid| to include the given |mach_info|. Does | |
| 87 // nothing if PlaceholderForPid() has not already been called for the given | |
| 88 // |pid|. Callers MUST acquire the lock given by GetLock() before calling | |
| 89 // this method (and release the lock afterwards). | |
| 90 void FinalizePid(base::ProcessHandle pid, mach_port_t task_port); | |
| 91 | |
| 92 // Name used to identify a particular port broker. | |
| 93 const std::string name_; | |
| 94 | |
| 95 // The Mach port on which the server listens. | |
| 96 base::mac::ScopedMachReceiveRight server_port_; | |
| 97 | |
| 98 // The dispatch source and queue on which Mach messages will be received. | |
| 99 scoped_ptr<base::DispatchSourceMach> dispatch_source_; | |
| 100 | |
| 101 // Stores mach info for every process in the broker. | |
| 102 typedef std::map<base::ProcessHandle, mach_port_t> MachMap; | |
| 103 MachMap mach_map_; | |
| 104 | |
| 105 // Mutex that guards |mach_map_|. | |
| 106 mutable base::Lock lock_; | |
| 107 | |
| 108 DISALLOW_COPY_AND_ASSIGN(MachPortBroker); | |
| 109 }; | |
| 110 | |
| 111 } // namespace base | |
| 112 | |
| 113 #endif // BASE_MAC_MACH_PORT_BROKER_H_ | |
| OLD | NEW |