[WebSocket] Send (request|response)_headers_text to the inspector.
[chromium-blink-merge.git] / content / browser / mach_broker_mac.h
blob51038bfa010831beed3b735850649fa2a081fab3
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_
8 #include <map>
9 #include <string>
11 #include <mach/mach.h>
13 #include "base/memory/singleton.h"
14 #include "base/process/process_handle.h"
15 #include "base/process/process_metrics.h"
16 #include "base/synchronization/lock.h"
17 #include "content/public/browser/browser_child_process_observer.h"
18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h"
21 namespace content {
23 // On OS X, the mach_port_t of a process is required to collect metrics about
24 // the process. Running |task_for_pid()| is only allowed for privileged code.
25 // However, a process has port rights to all its subprocesses, so let the
26 // browser's child processes send their Mach port to the browser over IPC.
27 // This way, the brower can at least collect metrics of its child processes,
28 // which is what it's most interested in anyway.
30 // Mach ports can only be sent over Mach IPC, not over the |socketpair()| that
31 // the regular IPC system uses. Hence, the child processes open a Mach
32 // connection shortly after launching and ipc their mach data to the browser
33 // process. This data is kept in a global |MachBroker| object.
35 // Since this data arrives over a separate channel, it is not available
36 // immediately after a child process has been started.
37 class CONTENT_EXPORT MachBroker : public base::ProcessMetrics::PortProvider,
38 public BrowserChildProcessObserver,
39 public NotificationObserver {
40 public:
41 // For use in child processes. This will send the task port of the current
42 // process over Mach IPC to the port registered by name (via this class) in
43 // the parent process. Returns true if the message was sent successfully
44 // and false if otherwise.
45 static bool ChildSendTaskPortToParent();
47 // Returns the global MachBroker.
48 static MachBroker* GetInstance();
50 // The lock that protects this MachBroker object. Clients MUST acquire and
51 // release this lock around calls to EnsureRunning(), PlaceholderForPid(),
52 // and FinalizePid().
53 base::Lock& GetLock();
55 // Performs any necessary setup that cannot happen in the constructor.
56 // Callers MUST acquire the lock given by GetLock() before calling this
57 // method (and release the lock afterwards).
58 void EnsureRunning();
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);
66 // Implement |ProcessMetrics::PortProvider|.
67 virtual mach_port_t TaskForPid(base::ProcessHandle process) const OVERRIDE;
69 // Implement |BrowserChildProcessObserver|.
70 virtual void BrowserChildProcessHostDisconnected(
71 const ChildProcessData& data) OVERRIDE;
72 virtual void BrowserChildProcessCrashed(
73 const ChildProcessData& data) OVERRIDE;
75 // Implement |NotificationObserver|.
76 virtual void Observe(int type,
77 const NotificationSource& source,
78 const NotificationDetails& details) OVERRIDE;
79 private:
80 friend class MachBrokerTest;
81 friend class MachListenerThreadDelegate;
82 friend struct DefaultSingletonTraits<MachBroker>;
84 MachBroker();
85 virtual ~MachBroker();
87 // Updates the mapping for |pid| to include the given |mach_info|. Does
88 // nothing if PlaceholderForPid() has not already been called for the given
89 // |pid|. Callers MUST acquire the lock given by GetLock() before calling
90 // this method (and release the lock afterwards).
91 void FinalizePid(base::ProcessHandle pid, mach_port_t task_port);
93 // Removes all mappings belonging to |pid| from the broker.
94 void InvalidatePid(base::ProcessHandle pid);
96 // Returns the Mach port name to use when sending or receiving messages.
97 // Does the Right Thing in the browser and in child processes.
98 static std::string GetMachPortName();
99 // Callback used to register notifications on the UI thread.
100 void RegisterNotifications();
102 // True if the listener thread has been started.
103 bool listener_thread_started_;
105 // Used to register for notifications received by NotificationObserver.
106 // Accessed only on the UI thread.
107 NotificationRegistrar registrar_;
109 // Stores mach info for every process in the broker.
110 typedef std::map<base::ProcessHandle, mach_port_t> MachMap;
111 MachMap mach_map_;
113 // Mutex that guards |mach_map_|.
114 mutable base::Lock lock_;
116 DISALLOW_COPY_AND_ASSIGN(MachBroker);
119 } // namespace content
121 #endif // CONTENT_BROWSER_MACH_BROKER_MAC_H_