mach_broker_mac.h revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2009 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 CHROME_BROWSER_MACH_BROKER_H_
6#define CHROME_BROWSER_MACH_BROKER_H_
7#pragma once
8
9#include <map>
10#include <string>
11
12#include <mach/mach.h>
13
14#include "base/lock.h"
15#include "base/process.h"
16#include "base/process_util.h"
17#include "base/singleton.h"
18#include "chrome/common/notification_observer.h"
19#include "chrome/common/notification_registrar.h"
20
21// On OS X, the mach_port_t of a process is required to collect metrics about
22// the process. Running |task_for_pid()| is only allowed for privileged code.
23// However, a process has port rights to all its subprocesses, so let the
24// browser's child processes send their Mach port to the browser over IPC.
25// This way, the brower can at least collect metrics of its child processes,
26// which is what it's most interested in anyway.
27//
28// Mach ports can only be sent over Mach IPC, not over the |socketpair()| that
29// the regular IPC system uses. Hence, the child processes open a Mach
30// connection shortly after launching and ipc their mach data to the browser
31// process. This data is kept in a global |MachBroker| object.
32//
33// Since this data arrives over a separate channel, it is not available
34// immediately after a child process has been started.
35class MachBroker : public base::ProcessMetrics::PortProvider,
36                   public NotificationObserver {
37 public:
38  // Returns the global MachBroker.
39  static MachBroker* GetInstance();
40
41  // Performs any necessary setup that cannot happen in the constructor.
42  // Clients MUST call this method before fork()ing any children.
43  void PrepareForFork();
44
45  struct MachInfo {
46    MachInfo() : mach_task_(MACH_PORT_NULL) {}
47
48    MachInfo& SetTask(mach_port_t task) {
49      mach_task_ = task;
50      return *this;
51    }
52
53    mach_port_t mach_task_;
54  };
55
56  // Adds a placeholder to the map for the given pid with MACH_PORT_NULL.
57  // Callers are expected to later update the port with FinalizePid().  Callers
58  // MUST acquire the lock given by GetLock() before calling this method (and
59  // release the lock afterwards).
60  void AddPlaceholderForPid(base::ProcessHandle pid);
61
62  // Updates the mapping for |pid| to include the given |mach_info|.  Does
63  // nothing if PlaceholderForPid() has not already been called for the given
64  // |pid|.  Callers MUST acquire the lock given by GetLock() before calling
65  // this method (and release the lock afterwards).
66  void FinalizePid(base::ProcessHandle pid, const MachInfo& mach_info);
67
68  // Removes all mappings belonging to |pid| from the broker.
69  void InvalidatePid(base::ProcessHandle pid);
70
71  // The lock that protects this MachBroker object.  Clients MUST acquire and
72  // release this lock around calls to PlaceholderForPid() and FinalizePid().
73  Lock& GetLock();
74
75  // Returns the Mach port name to use when sending or receiving messages.
76  // Does the Right Thing in the browser and in child processes.
77  static std::string GetMachPortName();
78
79  // Implement |ProcessMetrics::PortProvider|.
80  virtual mach_port_t TaskForPid(base::ProcessHandle process) const;
81
82  // Implement |NotificationObserver|.
83  virtual void Observe(NotificationType type,
84                       const NotificationSource& source,
85                       const NotificationDetails& details);
86 private:
87  // Private constructor.
88  MachBroker();
89
90  // True if the listener thread has been started.
91  bool listener_thread_started_;
92
93  // Used to register for notifications received by NotificationObserver.
94  // Accessed only on the UI thread.
95  NotificationRegistrar registrar_;
96
97  // Stores mach info for every process in the broker.
98  typedef std::map<base::ProcessHandle, MachInfo> MachMap;
99  MachMap mach_map_;
100
101  // Mutex that guards |mach_map_|.
102  mutable Lock lock_;
103
104  friend class MachBrokerTest;
105  friend class RegisterNotificationTask;
106  // Needed in order to make the constructor private.
107  friend struct DefaultSingletonTraits<MachBroker>;
108  DISALLOW_COPY_AND_ASSIGN(MachBroker);
109};
110
111#endif  // CHROME_BROWSER_MACH_BROKER_H_
112
113