| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_MACH_BROKER_H_ | 5 #ifndef CHROME_BROWSER_MACH_BROKER_H_ |
| 6 #define CHROME_BROWSER_MACH_BROKER_H_ | 6 #define CHROME_BROWSER_MACH_BROKER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <string> |
| 10 | 11 |
| 11 #include <mach/mach.h> | 12 #include <mach/mach.h> |
| 12 | 13 |
| 13 #include "base/lock.h" | 14 #include "base/lock.h" |
| 14 #include "base/process.h" | 15 #include "base/process.h" |
| 15 #include "base/process_util.h" | 16 #include "base/process_util.h" |
| 16 #include "base/singleton.h" | 17 #include "base/singleton.h" |
| 17 #include "chrome/common/notification_observer.h" | 18 #include "chrome/common/notification_observer.h" |
| 18 #include "chrome/common/notification_registrar.h" | 19 #include "chrome/common/notification_registrar.h" |
| 19 | 20 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 30 // process. This data is kept in a global |MachBroker| object. | 31 // process. This data is kept in a global |MachBroker| object. |
| 31 // | 32 // |
| 32 // Since this data arrives over a separate channel, it is not available | 33 // Since this data arrives over a separate channel, it is not available |
| 33 // immediately after a child process has been started. | 34 // immediately after a child process has been started. |
| 34 class MachBroker : public base::ProcessMetrics::PortProvider, | 35 class MachBroker : public base::ProcessMetrics::PortProvider, |
| 35 public NotificationObserver { | 36 public NotificationObserver { |
| 36 public: | 37 public: |
| 37 // Returns the global MachBroker. | 38 // Returns the global MachBroker. |
| 38 static MachBroker* instance(); | 39 static MachBroker* instance(); |
| 39 | 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 |
| 40 struct MachInfo { | 45 struct MachInfo { |
| 41 MachInfo() : mach_task_(MACH_PORT_NULL) {} | 46 MachInfo() : mach_task_(MACH_PORT_NULL) {} |
| 42 | 47 |
| 43 MachInfo& SetTask(mach_port_t task) { | 48 MachInfo& SetTask(mach_port_t task) { |
| 44 mach_task_ = task; | 49 mach_task_ = task; |
| 45 return *this; | 50 return *this; |
| 46 } | 51 } |
| 47 | 52 |
| 48 mach_port_t mach_task_; | 53 mach_port_t mach_task_; |
| 49 }; | 54 }; |
| 50 | 55 |
| 51 // Adds mach info for a given pid. | 56 // Adds a placeholder to the map for the given pid with MACH_PORT_NULL. |
| 52 void RegisterPid(base::ProcessHandle pid, const MachInfo& mach_info); | 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); |
| 53 | 67 |
| 54 // Removes all mappings belonging to |pid| from the broker. | 68 // Removes all mappings belonging to |pid| from the broker. |
| 55 void Invalidate(base::ProcessHandle pid); | 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(); |
| 56 | 78 |
| 57 // Implement |ProcessMetrics::PortProvider|. | 79 // Implement |ProcessMetrics::PortProvider|. |
| 58 virtual mach_port_t TaskForPid(base::ProcessHandle process) const; | 80 virtual mach_port_t TaskForPid(base::ProcessHandle process) const; |
| 59 | 81 |
| 60 // Implement |NotificationObserver|. | 82 // Implement |NotificationObserver|. |
| 61 virtual void Observe(NotificationType type, | 83 virtual void Observe(NotificationType type, |
| 62 const NotificationSource& source, | 84 const NotificationSource& source, |
| 63 const NotificationDetails& details); | 85 const NotificationDetails& details); |
| 64 private: | 86 private: |
| 65 // Private constructor. | 87 // Private constructor. |
| 66 MachBroker(); | 88 MachBroker(); |
| 67 | 89 |
| 90 // True if the listener thread has been started. |
| 91 bool listener_thread_started_; |
| 92 |
| 68 // Used to register for notifications received by NotificationObserver. | 93 // Used to register for notifications received by NotificationObserver. |
| 69 // Accessed only on the UI thread. | 94 // Accessed only on the UI thread. |
| 70 NotificationRegistrar registrar_; | 95 NotificationRegistrar registrar_; |
| 71 | 96 |
| 72 friend struct DefaultSingletonTraits<MachBroker>; | |
| 73 friend class MachBrokerTest; | |
| 74 | |
| 75 // Stores mach info for every process in the broker. | 97 // Stores mach info for every process in the broker. |
| 76 typedef std::map<base::ProcessHandle, MachInfo> MachMap; | 98 typedef std::map<base::ProcessHandle, MachInfo> MachMap; |
| 77 MachMap mach_map_; | 99 MachMap mach_map_; |
| 78 | 100 |
| 79 // Mutex that guards |mach_map_|. | 101 // Mutex that guards |mach_map_|. |
| 80 mutable Lock lock_; | 102 mutable Lock lock_; |
| 81 | 103 |
| 104 friend class MachBrokerTest; |
| 82 friend class RegisterNotificationTask; | 105 friend class RegisterNotificationTask; |
| 106 // Needed in order to make the constructor private. |
| 107 friend struct DefaultSingletonTraits<MachBroker>; |
| 83 DISALLOW_COPY_AND_ASSIGN(MachBroker); | 108 DISALLOW_COPY_AND_ASSIGN(MachBroker); |
| 84 }; | 109 }; |
| 85 | 110 |
| 86 #endif // CHROME_BROWSER_MACH_BROKER_H_ | 111 #endif // CHROME_BROWSER_MACH_BROKER_H_ |
| 87 | 112 |
| OLD | NEW |