summaryrefslogtreecommitdiffstats
path: root/chrome/browser/mach_broker_mac.h
diff options
context:
space:
mode:
authorthakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-12-21 17:44:20 +0000
committerthakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-12-21 17:44:20 +0000
commitb2e8e08818f3542c9043aececfbc913499226183 (patch)
tree84f4bcf67581c8239424420ba831aa8031db382f /chrome/browser/mach_broker_mac.h
parent6b4a530327976d8717dd007cf1403ee453148856 (diff)
downloadchromium_src-b2e8e08818f3542c9043aececfbc913499226183.zip
chromium_src-b2e8e08818f3542c9043aececfbc913499226183.tar.gz
chromium_src-b2e8e08818f3542c9043aececfbc913499226183.tar.bz2
Mac: Create a pid->task_t mapping in the browser process.
Since nothing writes to this map in the browser atm, this does not have any visible effect. BUG=13156,25454 TEST=unittest Review URL: http://codereview.chromium.org/501138 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@35092 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/mach_broker_mac.h')
-rw-r--r--chrome/browser/mach_broker_mac.h73
1 files changed, 73 insertions, 0 deletions
diff --git a/chrome/browser/mach_broker_mac.h b/chrome/browser/mach_broker_mac.h
new file mode 100644
index 0000000..67f4fec
--- /dev/null
+++ b/chrome/browser/mach_broker_mac.h
@@ -0,0 +1,73 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_MACH_BROKER_H_
+#define CHROME_BROWSER_MACH_BROKER_H_
+
+#include <map>
+
+#include <mach/mach.h>
+
+#include "base/lock.h"
+#include "base/process.h"
+#include "base/process_util.h"
+#include "base/singleton.h"
+
+// On OS X, the mach_port_t of a process is required to collect metrics about
+// the process. Running |task_for_pid()| is only allowed for privileged code.
+// However, a process has port rights to all its subprocesses, so let the
+// browser's child processes send their Mach port to the browser over IPC.
+// This way, the brower can at least collect metrics of its child processes,
+// which is what it's most interested in anyway.
+//
+// Mach ports can only be sent over Mach IPC, not over the |socketpair()| that
+// the regular IPC system uses. Hence, the child processes open a Mach
+// connection shortly after launching and ipc their mach data to the browser
+// process. This data is kept in a global |MachBroker| object.
+//
+// Since this data arrives over a separate channel, it is not available
+// immediately after a child process has been started.
+class MachBroker : public base::ProcessMetrics::PortProvider {
+ public:
+ // Returns the global MachBroker.
+ static MachBroker* instance();
+
+ struct MachInfo {
+ MachInfo() : mach_task_(MACH_PORT_NULL) {}
+
+ MachInfo& SetTask(mach_port_t task) {
+ mach_task_ = task;
+ return *this;
+ }
+
+ mach_port_t mach_task_;
+ };
+
+ // Adds mach info for a given pid.
+ void RegisterPid(base::ProcessHandle pid, const MachInfo& mach_info);
+
+ // Removes all mappings belonging to |pid| from the broker.
+ void Invalidate(base::ProcessHandle pid);
+
+ // Implement |ProcessMetrics::PortProvider|.
+ virtual mach_port_t TaskForPid(base::ProcessHandle process) const;
+
+ private:
+ // Private constructor.
+ MachBroker() {}
+ friend struct DefaultSingletonTraits<MachBroker>;
+ friend class MachBrokerTest;
+
+ // Stores mach info for every process in the broker.
+ typedef std::map<base::ProcessHandle, MachInfo> MachMap;
+ MachMap mach_map_;
+
+ // Mutex that guards |mach_map_|.
+ mutable Lock lock_;
+
+ DISALLOW_COPY_AND_ASSIGN(MachBroker);
+};
+
+#endif // CHROME_BROWSER_MACH_BROKER_H_
+