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
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_MACH_BROKER_MAC_H_
6 #define CONTENT_BROWSER_MACH_BROKER_MAC_H_
13 #include "base/mac/dispatch_source_mach.h"
14 #include "base/mac/scoped_mach_port.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/singleton.h"
17 #include "base/process/process_handle.h"
18 #include "base/process/process_metrics.h"
19 #include "base/synchronization/lock.h"
20 #include "content/public/browser/browser_child_process_observer.h"
21 #include "content/public/browser/notification_observer.h"
22 #include "content/public/browser/notification_registrar.h"
26 // On OS X, the mach_port_t of a process is required to collect metrics about
27 // the process. Running |task_for_pid()| is only allowed for privileged code.
28 // However, a process has port rights to all its subprocesses, so let the
29 // browser's child processes send their Mach port to the browser over IPC.
30 // This way, the brower can at least collect metrics of its child processes,
31 // which is what it's most interested in anyway.
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.
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::ProcessMetrics::PortProvider
,
41 public BrowserChildProcessObserver
,
42 public NotificationObserver
{
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();
50 // Returns the global MachBroker.
51 static MachBroker
* GetInstance();
53 // The lock that protects this MachBroker object. Clients MUST acquire and
54 // release this lock around calls to EnsureRunning(), PlaceholderForPid(),
56 base::Lock
& GetLock();
58 // Performs any necessary setup that cannot happen in the constructor.
59 // Callers MUST acquire the lock given by GetLock() before calling this
60 // method (and release the lock afterwards).
63 // 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
65 // MUST acquire the lock given by GetLock() before calling this method (and
66 // release the lock afterwards).
67 void AddPlaceholderForPid(base::ProcessHandle pid
, int child_process_id
);
69 // Implement |ProcessMetrics::PortProvider|.
70 mach_port_t
TaskForPid(base::ProcessHandle process
) const override
;
72 // Implement |BrowserChildProcessObserver|.
73 void BrowserChildProcessHostDisconnected(
74 const ChildProcessData
& data
) override
;
75 void BrowserChildProcessCrashed(const ChildProcessData
& data
,
76 int exit_code
) override
;
78 // Implement |NotificationObserver|.
79 void Observe(int type
,
80 const NotificationSource
& source
,
81 const NotificationDetails
& details
) override
;
83 // Returns the Mach port name to use when sending or receiving messages.
84 // Does the Right Thing in the browser and in child processes.
85 static std::string
GetMachPortName();
88 friend class MachBrokerTest
;
89 friend struct base::DefaultSingletonTraits
<MachBroker
>;
92 ~MachBroker() override
;
94 // Performs any initialization work.
97 // Message handler that is invoked on |dispatch_source_| when an
98 // incoming message needs to be received.
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
);
107 // Removes all mappings belonging to |child_process_id| from the broker.
108 void InvalidateChildProcessId(int child_process_id
);
110 // Callback used to register notifications on the UI thread.
111 void RegisterNotifications();
113 // Whether or not the class has been initialized.
116 // Used to register for notifications received by NotificationObserver.
117 // Accessed only on the UI thread.
118 NotificationRegistrar registrar_
;
120 // The Mach port on which the server listens.
121 base::mac::ScopedMachReceiveRight server_port_
;
123 // The dispatch source and queue on which Mach messages will be received.
124 scoped_ptr
<base::DispatchSourceMach
> dispatch_source_
;
126 // Stores mach info for every process in the broker.
127 typedef std::map
<base::ProcessHandle
, mach_port_t
> MachMap
;
130 // Stores the Child process unique id (RenderProcessHost ID) for every
132 typedef std::map
<int, base::ProcessHandle
> ChildProcessIdMap
;
133 ChildProcessIdMap child_process_id_map_
;
135 // Mutex that guards |mach_map_| and |child_process_id_map_|.
136 mutable base::Lock lock_
;
138 DISALLOW_COPY_AND_ASSIGN(MachBroker
);
141 } // namespace content
143 #endif // CONTENT_BROWSER_MACH_BROKER_MAC_H_