Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: installer/src/installer-lib/process.h

Issue 6003395731128320: Only take into account processes that have our plugin loaded (Closed)
Patch Set: Do not fake C++ iterators Created March 28, 2014, 2:26 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 /** 1 /**
2 * \file process.h 2 * \file process.h
3 */ 3 */
4 4
5 #ifndef PROCESS_H 5 #ifndef PROCESS_H
6 #define PROCESS_H 6 #define PROCESS_H
7 7
8 #include <vector> 8 #include <vector>
9 #include <set> 9 #include <set>
10 #include <algorithm> 10 #include <algorithm>
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 * Copy assignment declared private and not defined. 73 * Copy assignment declared private and not defined.
74 * 74 *
75 * \par Implementation 75 * \par Implementation
76 * Add "= delete" for C++11. 76 * Add "= delete" for C++11.
77 */ 77 */
78 Windows_Handle operator=( const Windows_Handle & ) ; 78 Windows_Handle operator=( const Windows_Handle & ) ;
79 79
80 }; 80 };
81 81
82 //------------------------------------------------------- 82 //-------------------------------------------------------
83 // exe_name_set: case-insensitive wide-string set 83 // file_name_set: case-insensitive wide-string set
84 //------------------------------------------------------- 84 //-------------------------------------------------------
85 int wcscmpi( const wchar_t * s1, const wchar_t * s2 ) ; 85 int wcscmpi( const wchar_t * s1, const wchar_t * s2 ) ;
86 86
87 struct exe_name 87 struct file_name
88 { 88 {
89 /** 89 /**
90 * Pointer to wide-character string, which is supposed to be null-terminated. 90 * Pointer to wide-character string, which is supposed to be null-terminated.
91 */ 91 */
92 const wchar_t * name ; 92 const wchar_t * name ;
93 93
94 exe_name( const wchar_t * name ) : name( name ) {} ; 94 file_name( const wchar_t * name ) : name( name ) {} ;
95 } ; 95 } ;
96 96
97 template <> 97 template <>
98 struct std::less< exe_name > 98 struct std::less< file_name >
99 : std::binary_function< exe_name, exe_name, bool > 99 : std::binary_function< file_name, file_name, bool >
100 { 100 {
101 bool operator()( const exe_name & a, const exe_name & b ) const 101 bool operator()( const file_name & a, const file_name & b ) const
102 { 102 {
103 return wcscmpi( a.name, b.name ) < 0 ; 103 return wcscmpi( a.name, b.name ) < 0 ;
104 } 104 }
105 } ; 105 } ;
106 106
107 struct exe_name_set 107 struct file_name_set
108 : public std::set< exe_name > 108 : public std::set< file_name >
109 { 109 {
110 exe_name_set( const wchar_t * exe_name_list[], size_t n_exe_names ) 110 file_name_set( const wchar_t * file_name_list[], size_t n_file_names )
111 { 111 {
112 for ( unsigned int j = 0 ; j < n_exe_names ; ++ j ) 112 for ( unsigned int j = 0 ; j < n_file_names ; ++ j )
113 { 113 {
114 insert( exe_name( exe_name_list[ j ] ) ) ; 114 insert( file_name( file_name_list[ j ] ) ) ;
115 } 115 }
116 } 116 }
117 } ; 117 } ;
118 118
119 //------------------------------------------------------- 119 //-------------------------------------------------------
120 //------------------------------------------------------- 120 //-------------------------------------------------------
121 /** 121 /**
122 * Filter by process name. Comparison is case-insensitive. 122 * Filter by process name. Comparison is case-insensitive. With ABP module loade d
123 */ 123 */
124 class process_by_any_exe_name_CI 124 class process_by_any_exe_with_any_module
125 : public std::unary_function< PROCESSENTRY32W, bool > 125 : public std::binary_function< PROCESSENTRY32W, file_name_set, bool >
126 { 126 {
127 const exe_name_set & names ; 127 const file_name_set & processNames ;
128 const file_name_set & moduleNames;
128 public: 129 public:
129 bool operator()( const PROCESSENTRY32W & ) ; 130 bool operator()( const PROCESSENTRY32W & ) ;
130 process_by_any_exe_name_CI( const exe_name_set & names ) 131 process_by_any_exe_with_any_module( const file_name_set & names, const file_na me_set & moduleNames )
131 : names( names ) 132 : processNames( names ), moduleNames( moduleNames )
132 {} 133 {}
133 } ; 134 } ;
134 135
136
135 //------------------------------------------------------- 137 //-------------------------------------------------------
136 // Process utility functions. 138 // Process utility functions.
137 //------------------------------------------------------- 139 //-------------------------------------------------------
138 /** 140 /**
139 * A promiscuous filter admits everything. 141 * A promiscuous filter admits everything.
140 */ 142 */
141 struct every_process 143 struct every_process
142 : public std::unary_function< PROCESSENTRY32W, bool > 144 : public std::unary_function< PROCESSENTRY32W, bool >
143 { 145 {
144 bool operator()( const PROCESSENTRY32W & ) { return true ; } ; 146 bool operator()( const PROCESSENTRY32W & ) { return true ; } ;
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 Snapshot() ; 259 Snapshot() ;
258 260
259 /** 261 /**
260 * Reconstruct the current instance with a new system snapshot. 262 * Reconstruct the current instance with a new system snapshot.
261 */ 263 */
262 void refresh() ; 264 void refresh() ;
263 265
264 /** 266 /**
265 * Return a pointer to the first process in the snapshot. 267 * Return a pointer to the first process in the snapshot.
266 */ 268 */
267 PROCESSENTRY32W * begin() ; 269 PROCESSENTRY32W * first() ;
268
269 /**
270 * The end pointer is an alias for the null pointer.
271 */
272 inline PROCESSENTRY32W * end() const { return 0 ; }
273 270
274 /** 271 /**
275 * Return a pointer to the next process in the snapshot. 272 * Return a pointer to the next process in the snapshot.
276 * begin() must have been called first. 273 * begin() must have been called first.
277 */ 274 */
278 PROCESSENTRY32W * next() ; 275 PROCESSENTRY32W * next() ;
276 } ;
277
278 class ModulesSnapshot
279 {
280 /**
281 * Handle to the process snapshot.
282 */
283 Windows_Handle handle;
279 284
280 /** 285 /**
281 * Type definition for pointer to underlying structure. 286 * Buffer for reading a single module entry out of the snapshot.
282 */ 287 */
283 typedef PROCESSENTRY32W * Pointer ; 288 MODULEENTRY32W module;
284 } ; 289
290 /**
291 * Copy constructor declared private and not defined.
292 *
293 * \par Implementation
294 * Add "= delete" for C++11.
295 */
296 ModulesSnapshot(const ModulesSnapshot&);
297
298 /**
299 * Copy assignment declared private and not defined.
300 *
301 * \par Implementation
302 * Add "= delete" for C++11.
303 */
304 ModulesSnapshot operator=(const ModulesSnapshot&);
305
306
307 public:
308 /**
309 * Default constructor takes the snapshot.
310 */
311 ModulesSnapshot(DWORD processId);
312
313 /**
314 * Return a pointer to the first process in the snapshot.
315 */
316 MODULEENTRY32W* first();
317
318 /**
319 * Return a pointer to the next process in the snapshot.
320 * begin() must have been called first.
321 */
322 MODULEENTRY32W* next();
323 };
324
285 325
286 //------------------------------------------------------- 326 //-------------------------------------------------------
287 // initialize_process_list 327 // initialize_process_list
288 //------------------------------------------------------- 328 //-------------------------------------------------------
289 /** 329 /**
290 * \tparam T The type into which a PROCESSENTRY32W struture is extracted. 330 * \tparam T The type into which a PROCESSENTRY32W struture is extracted.
291 * \tparam Admittance Function type for argument 'admit' 331 * \tparam Admittance Function type for argument 'admit'
292 * \tparam Extractor Function type for argument 'extract' 332 * \tparam Extractor Function type for argument 'extract'
293 * \param admit A unary predicate function class that determines what's included 333 * \param admit A unary predicate function class that determines what's included
294 * A process appears in the list only if the predicate returns true. 334 * A process appears in the list only if the predicate returns true.
295 * The use of this predicate is analogous to that in std::copy_if. 335 * The use of this predicate is analogous to that in std::copy_if.
296 * \param convert A conversion function that takes a PROCESSENTRY32W as input ar gument and returns an element of type T. 336 * \param convert A conversion function that takes a PROCESSENTRY32W as input ar gument and returns an element of type T.
297 */ 337 */
298 template< class T, class Admittance, class Extractor > 338 template<class T, class Admittance, class Extractor>
299 void initialize_process_list( std::vector< T > & v, Snapshot & snap, Admittance admit = Admittance(), Extractor extract = Extractor() ) 339 void initialize_process_list(std::vector<T>& v, Snapshot& snap, Admittance admit = Admittance(), Extractor extract = Extractor())
300 { 340 {
301 Snapshot::Pointer p = snap.begin() ; 341 PROCESSENTRY32W* p = snap.first();
302 while ( p != snap.end() ) 342 while (p != NULL)
303 { 343 {
304 if ( admit( * p ) ) 344 if (admit(*p ))
305 { 345 {
306 /* 346 /*
307 * We don't have C++11 emplace_back, which can construct the element in p lace. 347 * We don't have C++11 emplace_back, which can construct the element in p lace.
308 * Instead, we copy the return value of the converter. 348 * Instead, we copy the return value of the converter.
309 */ 349 */
310 v.push_back( extract( * p ) ); 350 v.push_back(extract(*p));
311 } 351 }
312 p = snap.next() ; 352 p = snap.next();
313 } 353 }
314 } ; 354 };
315 355
316 //------------------------------------------------------- 356 //-------------------------------------------------------
317 // initialize_process_set 357 // initialize_process_set
318 //------------------------------------------------------- 358 //-------------------------------------------------------
319 /** 359 /**
320 * \tparam T The type into which a PROCESSENTRY32W struture is extracted. 360 * \tparam T The type into which a PROCESSENTRY32W struture is extracted.
321 * \tparam Admittance Function type for argument 'admit' 361 * \tparam Admittance Function type for argument 'admit'
322 * \tparam Extractor Function type for argument 'extract' 362 * \tparam Extractor Function type for argument 'extract'
323 * \param admit A unary predicate function class that determines what's included 363 * \param admit A unary predicate function class that determines what's included
324 * A process appears in the list only if the predicate returns true. 364 * A process appears in the list only if the predicate returns true.
325 * The use of this predicate is analogous to that in std::copy_if. 365 * The use of this predicate is analogous to that in std::copy_if.
326 * \param convert A conversion function that takes a PROCESSENTRY32W as input ar gument and returns an element of type T. 366 * \param convert A conversion function that takes a PROCESSENTRY32W as input ar gument and returns an element of type T.
327 */ 367 */
328 template< class T, class Admittance, class Extractor > 368 template<class T, class Admittance, class Extractor>
329 void initialize_process_set( std::set< T > & set, Snapshot & snap, Admittance ad mit = Admittance(), Extractor extract = Extractor() ) 369 void initialize_process_set(std::set< T > & set, Snapshot& snap, Admittance admi t = Admittance(), Extractor extract = Extractor())
330 { 370 {
331 Snapshot::Pointer p = snap.begin() ; 371 PROCESSENTRY32W* p = snap.first();
332 while ( p != snap.end() ) 372 while (p != NULL)
333 { 373 {
334 if ( admit( * p ) ) 374 if (admit(*p))
335 { 375 {
336 set.insert( extract( * p ) ); 376 set.insert(extract(*p));
337 } 377 }
338 p = snap.next() ; 378 p = snap.next();
339 } 379 }
340 } ; 380 };
341 381
342 //------------------------------------------------------- 382 //-------------------------------------------------------
343 // enumerate_windows 383 // enumerate_windows
344 //------------------------------------------------------- 384 //-------------------------------------------------------
345 385
346 /** 386 /**
347 * States of a window enumeration. 387 * States of a window enumeration.
348 */ 388 */
349 typedef enum 389 typedef enum
350 { 390 {
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
458 * Set of process identifiers matching one of the executable names. 498 * Set of process identifiers matching one of the executable names.
459 */ 499 */
460 std::set< DWORD > pid_set ; 500 std::set< DWORD > pid_set ;
461 501
462 /** 502 /**
463 * Set of executable names by which to filter. 503 * Set of executable names by which to filter.
464 * 504 *
465 * The argument of the filter constructor is a set by reference. 505 * The argument of the filter constructor is a set by reference.
466 * Since it does not make a copy for itself, we define it as a class member to provide its allocation. 506 * Since it does not make a copy for itself, we define it as a class member to provide its allocation.
467 */ 507 */
468 exe_name_set exe_names ; 508 file_name_set file_names ;
509 /**
510 * Set of module (DLL) names by which to filter.
511 */
512 file_name_set module_names ;
469 513
470 /** 514 process_by_any_exe_with_any_module filter ;
471 * Filter function object matches on any of the exe names specified in the con structor.
472 */
473 process_by_any_exe_name_CI filter ;
474 515
475 /** 516 /**
476 * Copy function object copies just the process ID. 517 * Copy function object copies just the process ID.
477 */ 518 */
478 copy_PID copy ; 519 copy_PID copy ;
479 520
480 /** 521 /**
481 * Snapshot of running processes. 522 * Snapshot of running processes.
482 */ 523 */
483 Snapshot & snapshot ; 524 Snapshot & snapshot ;
(...skipping 30 matching lines...) Expand all
514 if ( ! b ) 555 if ( ! b )
515 { 556 {
516 // Assert the process that created the window is not in our pid_set 557 // Assert the process that created the window is not in our pid_set
517 return true ; 558 return true ;
518 } 559 }
519 return f( window ) ; 560 return f( window ) ;
520 } 561 }
521 } ; 562 } ;
522 563
523 public: 564 public:
524 Process_Closer( Snapshot & snapshot, const wchar_t * exe_name_list[], size_t n _exe_names ) 565 Process_Closer( Snapshot & snapshot, const wchar_t * file_name_list[], size_t n_file_names, const wchar_t * module_name_list[], size_t n_module_names )
525 : snapshot( snapshot ), exe_names( exe_name_list, n_exe_names ), filter( exe _names ) 566 : snapshot( snapshot ), file_names( file_name_list, n_file_names ), module_n ames(module_name_list, n_module_names), filter( file_names, module_names )
526 { 567 {
527 update() ; 568 update() ;
528 } 569 }
529 570
530 /** 571 /**
531 * Refresh our state to match the snapshot state. 572 * Refresh our state to match the snapshot state.
532 */ 573 */
533 void refresh() 574 void refresh()
534 { 575 {
535 pid_set.clear() ; 576 pid_set.clear() ;
(...skipping 12 matching lines...) Expand all
548 } 589 }
549 590
550 /* 591 /*
551 * Shut down every process in the pid_set. 592 * Shut down every process in the pid_set.
552 */ 593 */
553 bool shut_down() ; 594 bool shut_down() ;
554 595
555 } ; 596 } ;
556 597
557 #endif 598 #endif
OLDNEW
« no previous file with comments | « installer/src/custom-action/close_application.cpp ('k') | installer/src/installer-lib/process.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld