Home | History | Annotate | Download | only in browser
      1 // Copyright (c) 2011 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/memory/singleton.h"
     15 #include "base/process.h"
     16 #include "base/process_util.h"
     17 #include "base/synchronization/lock.h"
     18 #include "content/common/notification_observer.h"
     19 #include "content/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.
     35 class 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   base::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   ~MachBroker();
     90 
     91   // True if the listener thread has been started.
     92   bool listener_thread_started_;
     93 
     94   // Used to register for notifications received by NotificationObserver.
     95   // Accessed only on the UI thread.
     96   NotificationRegistrar registrar_;
     97 
     98   // Stores mach info for every process in the broker.
     99   typedef std::map<base::ProcessHandle, MachInfo> MachMap;
    100   MachMap mach_map_;
    101 
    102   // Mutex that guards |mach_map_|.
    103   mutable base::Lock lock_;
    104 
    105   friend class MachBrokerTest;
    106   friend class RegisterNotificationTask;
    107   // Needed in order to make the constructor private.
    108   friend struct DefaultSingletonTraits<MachBroker>;
    109   DISALLOW_COPY_AND_ASSIGN(MachBroker);
    110 };
    111 
    112 #endif  // CHROME_BROWSER_MACH_BROKER_H_
    113 
    114