Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Side by Side Diff: content/browser/mach_broker_mac.h

Issue 1755973002: Move non-content specific parts of content::MachBroker into base::MachPortBroker. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove libbsm from content build rules. Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/browser/BUILD.gn ('k') | content/browser/mach_broker_mac.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CONTENT_BROWSER_MACH_BROKER_MAC_H_ 5 #ifndef CONTENT_BROWSER_MACH_BROKER_MAC_H_
6 #define CONTENT_BROWSER_MACH_BROKER_MAC_H_ 6 #define CONTENT_BROWSER_MACH_BROKER_MAC_H_
7 7
8 #include <mach/mach.h>
9
10 #include <map> 8 #include <map>
11 #include <string> 9 #include <string>
12 10
13 #include "base/mac/dispatch_source_mach.h" 11 #include "base/mac/mach_port_broker.h"
14 #include "base/mac/scoped_mach_port.h"
15 #include "base/macros.h" 12 #include "base/macros.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/singleton.h" 13 #include "base/memory/singleton.h"
18 #include "base/process/port_provider_mac.h" 14 #include "base/process/port_provider_mac.h"
19 #include "base/process/process_handle.h" 15 #include "base/process/process_handle.h"
20 #include "base/synchronization/lock.h" 16 #include "base/synchronization/lock.h"
21 #include "content/public/browser/browser_child_process_observer.h" 17 #include "content/public/browser/browser_child_process_observer.h"
22 #include "content/public/browser/notification_observer.h" 18 #include "content/public/browser/notification_observer.h"
23 #include "content/public/browser/notification_registrar.h" 19 #include "content/public/browser/notification_registrar.h"
24 20
25 namespace content { 21 namespace content {
26 22
27 // On OS X, the task port of a process is required to collect metrics about the 23 // A global |MachBroker| singleton is used by content embedders to provide
28 // process, and to insert Mach ports into the process. Running |task_for_pid()| 24 // access to mach task ports for content child processes.
29 // is only allowed for privileged code. However, a process has port rights to
30 // all its subprocesses, so let the browser's child processes send their Mach
31 // port to the browser over IPC.
32 //
33 // Mach ports can only be sent over Mach IPC, not over the |socketpair()| that
34 // the regular IPC system uses. Hence, the child processes open a Mach
35 // connection shortly after launching and ipc their mach data to the browser
36 // process. This data is kept in a global |MachBroker| object.
37 //
38 // Since this data arrives over a separate channel, it is not available
39 // immediately after a child process has been started.
40 class CONTENT_EXPORT MachBroker : public base::PortProvider, 25 class CONTENT_EXPORT MachBroker : public base::PortProvider,
41 public BrowserChildProcessObserver, 26 public BrowserChildProcessObserver,
42 public NotificationObserver { 27 public NotificationObserver {
43 public: 28 public:
44 // For use in child processes. This will send the task port of the current 29 // 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 30 // 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 31 // the parent process. Returns true if the message was sent successfully
47 // and false if otherwise. 32 // and false if otherwise.
48 static bool ChildSendTaskPortToParent(); 33 static bool ChildSendTaskPortToParent();
49 34
50 // Returns the global MachBroker. 35 // Returns the global MachBroker.
51 static MachBroker* GetInstance(); 36 static MachBroker* GetInstance();
52 37
53 // The lock that protects this MachBroker object. Clients MUST acquire and 38 // The lock that protects this MachBroker object. Clients MUST acquire and
54 // release this lock around calls to EnsureRunning(), PlaceholderForPid(), 39 // release this lock around calls to EnsureRunning() and PlaceholderForPid().
55 // and FinalizePid().
56 base::Lock& GetLock(); 40 base::Lock& GetLock();
57 41
58 // Performs any necessary setup that cannot happen in the constructor. 42 // Performs any necessary setup that cannot happen in the constructor.
59 // Callers MUST acquire the lock given by GetLock() before calling this 43 // Callers MUST acquire the lock given by GetLock() before calling this
60 // method (and release the lock afterwards). 44 // method (and release the lock afterwards).
61 void EnsureRunning(); 45 void EnsureRunning();
62 46
63 // Adds a placeholder to the map for the given pid with MACH_PORT_NULL. 47 // Adds a placeholder to the map for the given pid with MACH_PORT_NULL.
64 // Callers are expected to later update the port with FinalizePid(). Callers 48 // Callers MUST acquire the lock given by GetLock() before calling this method
65 // MUST acquire the lock given by GetLock() before calling this method (and 49 // (and release the lock afterwards).
66 // release the lock afterwards).
67 void AddPlaceholderForPid(base::ProcessHandle pid, int child_process_id); 50 void AddPlaceholderForPid(base::ProcessHandle pid, int child_process_id);
68 51
69 // Implement |base::PortProvider|. 52 // Implement |base::PortProvider|.
70 mach_port_t TaskForPid(base::ProcessHandle process) const override; 53 mach_port_t TaskForPid(base::ProcessHandle process) const override;
71 54
72 // Implement |BrowserChildProcessObserver|. 55 // Implement |BrowserChildProcessObserver|.
73 void BrowserChildProcessHostDisconnected( 56 void BrowserChildProcessHostDisconnected(
74 const ChildProcessData& data) override; 57 const ChildProcessData& data) override;
75 void BrowserChildProcessCrashed(const ChildProcessData& data, 58 void BrowserChildProcessCrashed(const ChildProcessData& data,
76 int exit_code) override; 59 int exit_code) override;
77 60
78 // Implement |NotificationObserver|. 61 // Implement |NotificationObserver|.
79 void Observe(int type, 62 void Observe(int type,
80 const NotificationSource& source, 63 const NotificationSource& source,
81 const NotificationDetails& details) override; 64 const NotificationDetails& details) override;
82 65
83 // Returns the Mach port name to use when sending or receiving messages. 66 // Returns the Mach port name to use when sending or receiving messages.
84 // Does the Right Thing in the browser and in child processes. 67 // Does the Right Thing in the browser and in child processes.
85 static std::string GetMachPortName(); 68 static std::string GetMachPortName();
86 69
87 private: 70 private:
88 friend class MachBrokerTest; 71 friend class MachBrokerTest;
89 friend struct base::DefaultSingletonTraits<MachBroker>; 72 friend struct base::DefaultSingletonTraits<MachBroker>;
90 73
91 MachBroker(); 74 MachBroker();
92 ~MachBroker() override; 75 ~MachBroker() override;
93 76
94 // Performs any initialization work.
95 bool Init();
96
97 // Message handler that is invoked on |dispatch_source_| when an
98 // incoming message needs to be received.
99 void HandleRequest();
100
101 // Updates the mapping for |pid| to include the given |mach_info|. Does
102 // nothing if PlaceholderForPid() has not already been called for the given
103 // |pid|. Callers MUST acquire the lock given by GetLock() before calling
104 // this method (and release the lock afterwards).
105 void FinalizePid(base::ProcessHandle pid, mach_port_t task_port);
106
107 // Removes all mappings belonging to |child_process_id| from the broker. 77 // Removes all mappings belonging to |child_process_id| from the broker.
108 void InvalidateChildProcessId(int child_process_id); 78 void InvalidateChildProcessId(int child_process_id);
109 79
110 // Callback used to register notifications on the UI thread. 80 // Callback used to register notifications on the UI thread.
111 void RegisterNotifications(); 81 void RegisterNotifications();
112 82
113 // Whether or not the class has been initialized. 83 // Whether or not the class has been initialized.
114 bool initialized_; 84 bool initialized_;
115 85
116 // Used to register for notifications received by NotificationObserver. 86 // Used to register for notifications received by NotificationObserver.
117 // Accessed only on the UI thread. 87 // Accessed only on the UI thread.
118 NotificationRegistrar registrar_; 88 NotificationRegistrar registrar_;
119 89
120 // The Mach port on which the server listens.
121 base::mac::ScopedMachReceiveRight server_port_;
122
123 // The dispatch source and queue on which Mach messages will be received.
124 scoped_ptr<base::DispatchSourceMach> dispatch_source_;
125
126 // Stores mach info for every process in the broker.
127 typedef std::map<base::ProcessHandle, mach_port_t> MachMap;
128 MachMap mach_map_;
129
130 // Stores the Child process unique id (RenderProcessHost ID) for every 90 // Stores the Child process unique id (RenderProcessHost ID) for every
131 // process. 91 // process. Protected by base::MachPortBroker::GetLock().
132 typedef std::map<int, base::ProcessHandle> ChildProcessIdMap; 92 typedef std::map<int, base::ProcessHandle> ChildProcessIdMap;
133 ChildProcessIdMap child_process_id_map_; 93 ChildProcessIdMap child_process_id_map_;
134 94
135 // Mutex that guards |mach_map_| and |child_process_id_map_|. 95 // Underlying port broker that receives and manages mach ports.
136 mutable base::Lock lock_; 96 base::MachPortBroker broker_;
137 97
138 DISALLOW_COPY_AND_ASSIGN(MachBroker); 98 DISALLOW_COPY_AND_ASSIGN(MachBroker);
139 }; 99 };
140 100
141 } // namespace content 101 } // namespace content
142 102
143 #endif // CONTENT_BROWSER_MACH_BROKER_MAC_H_ 103 #endif // CONTENT_BROWSER_MACH_BROKER_MAC_H_
OLDNEW
« no previous file with comments | « content/browser/BUILD.gn ('k') | content/browser/mach_broker_mac.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698