| OLD | NEW |
| (Empty) |
| 1 // Copyright 2009 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 // | |
| 16 // Provides a class (ProcessMonitor) to watch for Win32 process names and fire | |
| 17 // callback events (via the ProcessMonitorCallbackInterface) when processes are | |
| 18 // created/exited. | |
| 19 | |
| 20 #ifndef OMAHA_TOOLS_SRC_GOOPDUMP_PROCESS_MONITOR_H__ | |
| 21 #define OMAHA_TOOLS_SRC_GOOPDUMP_PROCESS_MONITOR_H__ | |
| 22 | |
| 23 #include <windows.h> | |
| 24 #include <atlstr.h> | |
| 25 #include <map> | |
| 26 #include <vector> | |
| 27 | |
| 28 #include "omaha/common/scoped_any.h" | |
| 29 #include "omaha/common/synchronized.h" | |
| 30 | |
| 31 namespace omaha { | |
| 32 | |
| 33 // Interface for users of the ProcessMonitor class to receive events when a | |
| 34 // process matching their desired pattern is created or exits. | |
| 35 class ProcessMonitorCallbackInterface { | |
| 36 public: | |
| 37 ProcessMonitorCallbackInterface() {} | |
| 38 virtual ~ProcessMonitorCallbackInterface() {} | |
| 39 | |
| 40 // Called when a new process is found that matches the pattern. The pattern | |
| 41 // that matched is passed in as process_pattern. | |
| 42 virtual void OnProcessAdded(DWORD process_id, | |
| 43 const CString& process_pattern) = 0; | |
| 44 | |
| 45 // Called when a process that was previously found has exited. | |
| 46 virtual void OnProcessRemoved(DWORD process_id) = 0; | |
| 47 | |
| 48 private: | |
| 49 DISALLOW_EVIL_CONSTRUCTORS(ProcessMonitorCallbackInterface); | |
| 50 }; | |
| 51 | |
| 52 // This class creates a thread to monitor running processes for particular | |
| 53 // process names. Fires events via ProcessMonitorCallbackInterface when a new | |
| 54 // process is detected that matches a name pattern and also when those processes | |
| 55 // exit. | |
| 56 // This class uses polling to look for the processes since the only way to get | |
| 57 // event notification of process creation is to create a driver. | |
| 58 class ProcessMonitor { | |
| 59 public: | |
| 60 typedef std::map<HANDLE, DWORD> MapHandleToDword; | |
| 61 typedef MapHandleToDword::iterator MapHandleToDwordIterator; | |
| 62 | |
| 63 ProcessMonitor(); | |
| 64 ~ProcessMonitor(); | |
| 65 | |
| 66 // Starts the monitoring process to look for processes that match | |
| 67 // process_name_pattern and fire events via the callback. | |
| 68 HRESULT Start(ProcessMonitorCallbackInterface* callback, | |
| 69 const TCHAR* process_name_pattern); | |
| 70 | |
| 71 // Starts the monitoring process with multiple patterns. | |
| 72 HRESULT StartWithPatterns(ProcessMonitorCallbackInterface* callback, | |
| 73 const std::vector<CString>& process_name_patterns); | |
| 74 | |
| 75 // Stops the monitoring process and cleans up. | |
| 76 HRESULT Stop(); | |
| 77 | |
| 78 private: | |
| 79 // Thread procedure for monitoring the processes in the background. | |
| 80 static DWORD WINAPI MonitorThreadProc(void* param); | |
| 81 DWORD MonitorProc(); | |
| 82 | |
| 83 // Called when a process matching the process is found. | |
| 84 void OnProcessAdded(DWORD process_id, const CString& process_pattern); | |
| 85 | |
| 86 // Called when a previously added process exits. | |
| 87 void OnProcessRemoved(DWORD process_id); | |
| 88 | |
| 89 // Walks the active process list to look for matches against the pattern. If | |
| 90 // a process is found to match that's not in the list, it's added to the map | |
| 91 // and OnProcessAdded() is called. | |
| 92 bool UpdateProcessList(MapHandleToDword* map_handle_pid); | |
| 93 | |
| 94 // Walks through the map and calls CloseHandle() on each handle in the list. | |
| 95 void CleanupHandleMap(MapHandleToDword* map_handle_pid); | |
| 96 | |
| 97 bool is_running_; // Set to true while the process monitoring is running. | |
| 98 ProcessMonitorCallbackInterface* callback_; // Event callback interface. | |
| 99 std::vector<CString> process_name_patterns_; // List of patterns to match. | |
| 100 CriticalSection lock_; // Protects is_running_ flag. | |
| 101 scoped_handle event_thread_exit_; // Signal to exit monitor_thread_. | |
| 102 scoped_handle monitor_thread_; // Handle to the monitoring thread. | |
| 103 | |
| 104 DISALLOW_EVIL_CONSTRUCTORS(ProcessMonitor); | |
| 105 }; | |
| 106 | |
| 107 } // namespace omaha | |
| 108 | |
| 109 #endif // OMAHA_TOOLS_SRC_GOOPDUMP_PROCESS_MONITOR_H__ | |
| 110 | |
| OLD | NEW |