Add callback in UserScriptLoader to notify users when scripts are loaded.
[chromium-blink-merge.git] / content / browser / mach_broker_mac.h
blobfb9f3efbf17b4dca105e4d683993ad368a72d4f7
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 <mach/mach.h>
10 #include <map>
11 #include <string>
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, int child_process_id);
66 // Implement |ProcessMetrics::PortProvider|.
67 mach_port_t TaskForPid(base::ProcessHandle process) const override;
69 // Implement |BrowserChildProcessObserver|.
70 void BrowserChildProcessHostDisconnected(
71 const ChildProcessData& data) override;
72 void BrowserChildProcessCrashed(const ChildProcessData& data,
73 int exit_code) override;
75 // Implement |NotificationObserver|.
76 void Observe(int type,
77 const NotificationSource& source,
78 const NotificationDetails& details) override;
80 private:
81 friend class MachBrokerTest;
82 friend class MachListenerThreadDelegate;
83 friend struct DefaultSingletonTraits<MachBroker>;
85 MachBroker();
86 ~MachBroker() override;
88 // Updates the mapping for |pid| to include the given |mach_info|. Does
89 // nothing if PlaceholderForPid() has not already been called for the given
90 // |pid|. Callers MUST acquire the lock given by GetLock() before calling
91 // this method (and release the lock afterwards).
92 void FinalizePid(base::ProcessHandle pid, mach_port_t task_port);
94 // Removes all mappings belonging to |child_process_id| from the broker.
95 void InvalidateChildProcessId(int child_process_id);
97 // Returns the Mach port name to use when sending or receiving messages.
98 // Does the Right Thing in the browser and in child processes.
99 static std::string GetMachPortName();
100 // Callback used to register notifications on the UI thread.
101 void RegisterNotifications();
103 // True if the listener thread has been started.
104 bool listener_thread_started_;
106 // Used to register for notifications received by NotificationObserver.
107 // Accessed only on the UI thread.
108 NotificationRegistrar registrar_;
110 // Stores mach info for every process in the broker.
111 typedef std::map<base::ProcessHandle, mach_port_t> MachMap;
112 MachMap mach_map_;
114 // Stores the Child process unique id (RenderProcessHost ID) for every
115 // process.
116 typedef std::map<int, base::ProcessHandle> ChildProcessIdMap;
117 ChildProcessIdMap child_process_id_map_;
119 // Mutex that guards |mach_map_| and |child_process_id_map_|.
120 mutable base::Lock lock_;
122 DISALLOW_COPY_AND_ASSIGN(MachBroker);
125 } // namespace content
127 #endif // CONTENT_BROWSER_MACH_BROKER_MAC_H_