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

Delta Between Two Patch Sets: installer/src/installer-lib/process.h

Issue 6003395731128320: Only take into account processes that have our plugin loaded (Closed)
Left Patch Set: Created March 27, 2014, 3:29 p.m.
Right Patch Set: Simplify Process_Closer constructor Created March 31, 2014, 8:31 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « installer/src/custom-action/close_application.cpp ('k') | installer/src/installer-lib/process.cpp » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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(){};
111 { 111
112 for ( unsigned int j = 0 ; j < n_exe_names ; ++ j ) 112 file_name_set( const wchar_t * file_name_list[], size_t n_file_names )
113 { 113 {
114 insert( exe_name( exe_name_list[ j ] ) ) ; 114 for ( unsigned int j = 0 ; j < n_file_names ; ++ j )
115 } 115 {
116 } 116 insert( file_name( file_name_list[ j ] ) ) ;
117 } ; 117 }
118 118 }
119 //------------------------------------------------------- 119 } ;
120 //------------------------------------------------------- 120
121 /** 121 //-------------------------------------------------------
122 * Filter by process name. Comparison is case-insensitive. 122 //-------------------------------------------------------
123 */ 123 /**
124 class process_by_any_exe_name_CI 124 * Filter by process name. Comparison is case-insensitive. With ABP module loade d
Eric 2014/03/27 16:56:51 Since this filter isn't being used any more, it ca
Wladimir Palant 2014/03/28 07:34:55 I strongly prefer to remove any unused code that's
125 : public std::unary_function< PROCESSENTRY32W, bool > 125 */
126 { 126 class process_by_any_exe_with_any_module
127 const exe_name_set & names ; 127 : public std::binary_function< PROCESSENTRY32W, file_name_set, bool >
128 {
129 const file_name_set & processNames ;
130 const file_name_set & moduleNames;
128 public: 131 public:
129 bool operator()( const PROCESSENTRY32W & ) ; 132 bool operator()( const PROCESSENTRY32W & ) ;
130 process_by_any_exe_name_CI( const exe_name_set & names ) 133 process_by_any_exe_with_any_module( const file_name_set & names, const file_na me_set & moduleNames )
131 : names( names ) 134 : processNames( names ), moduleNames( moduleNames )
132 {}
133 } ;
134
135 //-------------------------------------------------------
136 //-------------------------------------------------------
137 /**
138 * Filter by process name. Comparison is case-insensitive. With ABP module loade d
139 */
140 class process_by_any_exe_name_CI_w_ABP
Eric 2014/03/27 16:52:44 This is not ABP-specific until after we pass it AB
141 : public std::binary_function< PROCESSENTRY32W, exe_name_set, bool >
142 {
143 const exe_name_set & names ;
Eric 2014/03/27 16:52:44 This should be renamed to process_names (or the li
144 const exe_name_set & moduleNames;
145 public:
146 bool operator()( const PROCESSENTRY32W & ) ;
147 process_by_any_exe_name_CI_w_ABP( const exe_name_set & names, const exe_name_s et & moduleNames )
148 : names( names ), moduleNames( moduleNames )
149 {} 135 {}
150 } ; 136 } ;
151 137
152 138
153 //------------------------------------------------------- 139 //-------------------------------------------------------
154 // Process utility functions. 140 // Process utility functions.
155 //------------------------------------------------------- 141 //-------------------------------------------------------
156 /** 142 /**
157 * A promiscuous filter admits everything. 143 * A promiscuous filter admits everything.
158 */ 144 */
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 Snapshot() ; 261 Snapshot() ;
276 262
277 /** 263 /**
278 * Reconstruct the current instance with a new system snapshot. 264 * Reconstruct the current instance with a new system snapshot.
279 */ 265 */
280 void refresh() ; 266 void refresh() ;
281 267
282 /** 268 /**
283 * Return a pointer to the first process in the snapshot. 269 * Return a pointer to the first process in the snapshot.
284 */ 270 */
285 PROCESSENTRY32W * begin() ; 271 PROCESSENTRY32W * first() ;
286
287 /**
288 * The end pointer is an alias for the null pointer.
289 */
290 inline PROCESSENTRY32W * end() const { return 0 ; }
291 272
292 /** 273 /**
293 * Return a pointer to the next process in the snapshot. 274 * Return a pointer to the next process in the snapshot.
294 * begin() must have been called first. 275 * begin() must have been called first.
295 */ 276 */
296 PROCESSENTRY32W * next() ; 277 PROCESSENTRY32W * next() ;
297
298 /**
299 * Type definition for pointer to underlying structure.
300 */
301 typedef PROCESSENTRY32W * Pointer ;
302 } ; 278 } ;
303 279
Eric 2014/03/27 16:52:44 We need some documentation here about CreateToolhe
304 class ModulesSnapshot 280 class ModulesSnapshot
305 { 281 {
306 /** 282 /**
307 * Handle to the process snapshot. 283 * Handle to the process snapshot.
308 */ 284 */
309 Windows_Handle handle ; 285 Windows_Handle handle;
310 286
311 /** 287 /**
312 * Buffer for reading a single module entry out of the snapshot. 288 * Buffer for reading a single module entry out of the snapshot.
313 */ 289 */
314 MODULEENTRY32W module; 290 MODULEENTRY32W module;
315
316 /**
317 * process ID of the process to retrieve modules for.
318 */
319 DWORD processId;
Eric 2014/03/27 16:52:44 Only needed for refresh(), which we don't need.
320 291
321 /** 292 /**
322 * Copy constructor declared private and not defined. 293 * Copy constructor declared private and not defined.
323 * 294 *
324 * \par Implementation 295 * \par Implementation
325 * Add "= delete" for C++11. 296 * Add "= delete" for C++11.
326 */ 297 */
327 ModulesSnapshot( const ModulesSnapshot & ) ; 298 ModulesSnapshot(const ModulesSnapshot&);
328 299
329 /** 300 /**
330 * Copy assignment declared private and not defined. 301 * Copy assignment declared private and not defined.
331 * 302 *
332 * \par Implementation 303 * \par Implementation
333 * Add "= delete" for C++11. 304 * Add "= delete" for C++11.
334 */ 305 */
335 ModulesSnapshot operator=( const ModulesSnapshot & ) ; 306 ModulesSnapshot operator=(const ModulesSnapshot&);
336 307
337 308
338 public: 309 public:
339 /** 310 /**
340 * Default constructor takes the snapshot. 311 * Default constructor takes the snapshot.
341 */ 312 */
342 ModulesSnapshot(DWORD processId) ; 313 ModulesSnapshot(DWORD processId);
343
344 /**
345 * Reconstruct the current instance with a new system snapshot.
346 */
347 void refresh(DWORD processId) ;
Eric 2014/03/27 16:52:44 The way this is being used, I don't believe we nee
348 314
349 /** 315 /**
350 * Return a pointer to the first process in the snapshot. 316 * Return a pointer to the first process in the snapshot.
351 */ 317 */
352 MODULEENTRY32W * begin() ; 318 MODULEENTRY32W* first();
353
354 /**
355 * The end pointer is an alias for the null pointer.
356 */
357 inline MODULEENTRY32W * end() const { return 0 ; }
358 319
359 /** 320 /**
360 * Return a pointer to the next process in the snapshot. 321 * Return a pointer to the next process in the snapshot.
361 * begin() must have been called first. 322 * begin() must have been called first.
362 */ 323 */
363 MODULEENTRY32W * next() ; 324 MODULEENTRY32W* next();
364 325 };
365 /**
366 * Type definition for pointer to underlying structure.
367 */
368 typedef MODULEENTRY32W * Pointer ;
369 } ;
370 326
371 327
372 //------------------------------------------------------- 328 //-------------------------------------------------------
373 // initialize_process_list 329 // initialize_process_list
374 //------------------------------------------------------- 330 //-------------------------------------------------------
375 /** 331 /**
376 * \tparam T The type into which a PROCESSENTRY32W struture is extracted. 332 * \tparam T The type into which a PROCESSENTRY32W struture is extracted.
377 * \tparam Admittance Function type for argument 'admit' 333 * \tparam Admittance Function type for argument 'admit'
378 * \tparam Extractor Function type for argument 'extract' 334 * \tparam Extractor Function type for argument 'extract'
379 * \param admit A unary predicate function class that determines what's included 335 * \param admit A unary predicate function class that determines what's included
380 * A process appears in the list only if the predicate returns true. 336 * A process appears in the list only if the predicate returns true.
381 * The use of this predicate is analogous to that in std::copy_if. 337 * The use of this predicate is analogous to that in std::copy_if.
382 * \param convert A conversion function that takes a PROCESSENTRY32W as input ar gument and returns an element of type T. 338 * \param convert A conversion function that takes a PROCESSENTRY32W as input ar gument and returns an element of type T.
383 */ 339 */
384 template< class T, class Admittance, class Extractor > 340 template<class T, class Admittance, class Extractor>
385 void initialize_process_list( std::vector< T > & v, Snapshot & snap, Admittance admit = Admittance(), Extractor extract = Extractor() ) 341 void initialize_process_list(std::vector<T>& v, Snapshot& snap, Admittance admit = Admittance(), Extractor extract = Extractor())
386 { 342 {
387 Snapshot::Pointer p = snap.begin() ; 343 PROCESSENTRY32W* p = snap.first();
388 while ( p != snap.end() ) 344 while (p != NULL)
389 { 345 {
390 if ( admit( * p ) ) 346 if (admit(*p ))
391 { 347 {
392 /* 348 /*
393 * We don't have C++11 emplace_back, which can construct the element in p lace. 349 * We don't have C++11 emplace_back, which can construct the element in p lace.
394 * Instead, we copy the return value of the converter. 350 * Instead, we copy the return value of the converter.
395 */ 351 */
396 v.push_back( extract( * p ) ); 352 v.push_back(extract(*p));
397 } 353 }
398 p = snap.next() ; 354 p = snap.next();
399 } 355 }
400 } ; 356 };
401 357
402 //------------------------------------------------------- 358 //-------------------------------------------------------
403 // initialize_process_set 359 // initialize_process_set
404 //------------------------------------------------------- 360 //-------------------------------------------------------
405 /** 361 /**
406 * \tparam T The type into which a PROCESSENTRY32W struture is extracted. 362 * \tparam T The type into which a PROCESSENTRY32W struture is extracted.
407 * \tparam Admittance Function type for argument 'admit' 363 * \tparam Admittance Function type for argument 'admit'
408 * \tparam Extractor Function type for argument 'extract' 364 * \tparam Extractor Function type for argument 'extract'
409 * \param admit A unary predicate function class that determines what's included 365 * \param admit A unary predicate function class that determines what's included
410 * A process appears in the list only if the predicate returns true. 366 * A process appears in the list only if the predicate returns true.
411 * The use of this predicate is analogous to that in std::copy_if. 367 * The use of this predicate is analogous to that in std::copy_if.
412 * \param convert A conversion function that takes a PROCESSENTRY32W as input ar gument and returns an element of type T. 368 * \param convert A conversion function that takes a PROCESSENTRY32W as input ar gument and returns an element of type T.
413 */ 369 */
414 template< class T, class Admittance, class Extractor > 370 template<class T, class Admittance, class Extractor>
415 void initialize_process_set( std::set< T > & set, Snapshot & snap, Admittance ad mit = Admittance(), Extractor extract = Extractor() ) 371 void initialize_process_set(std::set< T > & set, Snapshot& snap, Admittance admi t = Admittance(), Extractor extract = Extractor())
416 { 372 {
417 Snapshot::Pointer p = snap.begin() ; 373 PROCESSENTRY32W* p = snap.first();
418 while ( p != snap.end() ) 374 while (p != NULL)
419 { 375 {
420 if ( admit( * p ) ) 376 if (admit(*p))
421 { 377 {
422 set.insert( extract( * p ) ); 378 set.insert(extract(*p));
423 } 379 }
424 p = snap.next() ; 380 p = snap.next();
425 } 381 }
426 } ; 382 };
427 383
428 //------------------------------------------------------- 384 //-------------------------------------------------------
429 // enumerate_windows 385 // enumerate_windows
430 //------------------------------------------------------- 386 //-------------------------------------------------------
431 387
432 /** 388 /**
433 * States of a window enumeration. 389 * States of a window enumeration.
434 */ 390 */
435 typedef enum 391 typedef enum
436 { 392 {
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
544 * Set of process identifiers matching one of the executable names. 500 * Set of process identifiers matching one of the executable names.
545 */ 501 */
546 std::set< DWORD > pid_set ; 502 std::set< DWORD > pid_set ;
547 503
548 /** 504 /**
549 * Set of executable names by which to filter. 505 * Set of executable names by which to filter.
550 * 506 *
551 * The argument of the filter constructor is a set by reference. 507 * The argument of the filter constructor is a set by reference.
552 * Since it does not make a copy for itself, we define it as a class member to provide its allocation. 508 * Since it does not make a copy for itself, we define it as a class member to provide its allocation.
553 */ 509 */
554 exe_name_set exe_names ; 510 file_name_set file_names ;
555 exe_name_set module_names ; 511 /**
Eric 2014/03/27 16:52:44 I'd use dll_names. We configure these with .EXE an
Oleksandr 2014/03/27 23:28:46 I actually think module_names is more consistent,
556 512 * Set of module (DLL) names by which to filter.
557 /** 513 */
558 * Filter function object matches on any of the exe names specified in the con structor. 514 file_name_set module_names ;
559 */ 515
560 // process_by_any_exe_name_CI filter ; 516 process_by_any_exe_with_any_module filter ;
561
562 process_by_any_exe_name_CI_w_ABP filter ;
563 517
564 /** 518 /**
565 * Copy function object copies just the process ID. 519 * Copy function object copies just the process ID.
566 */ 520 */
567 copy_PID copy ; 521 copy_PID copy ;
568 522
569 /** 523 /**
570 * Snapshot of running processes. 524 * Snapshot of running processes.
571 */ 525 */
572 Snapshot & snapshot ; 526 Snapshot & snapshot ;
(...skipping 30 matching lines...) Expand all
603 if ( ! b ) 557 if ( ! b )
604 { 558 {
605 // Assert the process that created the window is not in our pid_set 559 // Assert the process that created the window is not in our pid_set
606 return true ; 560 return true ;
607 } 561 }
608 return f( window ) ; 562 return f( window ) ;
609 } 563 }
610 } ; 564 } ;
611 565
612 public: 566 public:
613 Process_Closer( Snapshot & snapshot, const wchar_t * exe_name_list[], size_t n _exe_names, const wchar_t * module_name_list[], size_t n_module_names ) 567 template <size_t n_file_names, size_t n_module_names>
Eric 2014/03/27 16:52:44 Likewise dll_name_list etc. here.
614 : snapshot( snapshot ), exe_names( exe_name_list, n_exe_names ), module_name s(module_name_list, n_module_names), filter( exe_names, module_names ) 568 Process_Closer(Snapshot & snapshot, const wchar_t* (&file_name_list)[n_file_na mes], const wchar_t* (&module_name_list)[n_module_names])
569 : snapshot(snapshot), file_names(file_name_list, n_file_names), module_names (module_name_list, n_module_names), filter(file_names, module_names)
570 {
571 update() ;
572 }
573 template <size_t n_file_names>
574 Process_Closer(Snapshot & snapshot, const wchar_t * (&file_name_list)[n_file_n ames])
575 : snapshot(snapshot), file_names(file_name_list, n_file_names), module_names (), filter(file_names, module_names)
615 { 576 {
616 update() ; 577 update() ;
617 } 578 }
618 579
619 /** 580 /**
620 * Refresh our state to match the snapshot state. 581 * Refresh our state to match the snapshot state.
621 */ 582 */
622 void refresh() 583 void refresh()
623 { 584 {
624 pid_set.clear() ; 585 pid_set.clear() ;
(...skipping 12 matching lines...) Expand all
637 } 598 }
638 599
639 /* 600 /*
640 * Shut down every process in the pid_set. 601 * Shut down every process in the pid_set.
641 */ 602 */
642 bool shut_down() ; 603 bool shut_down() ;
643 604
644 } ; 605 } ;
645 606
646 #endif 607 #endif
LEFTRIGHT

Powered by Google App Engine
This is Rietveld