 Issue 6003395731128320:
  Only take into account processes that have our plugin loaded  (Closed)
    
  
    Issue 6003395731128320:
  Only take into account processes that have our plugin loaded  (Closed) 
  | Left: | ||
| Right: | 
| OLD | NEW | 
|---|---|
| 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 Loading... | |
| 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 ) ; | 
| 
Wladimir Palant
2014/03/28 07:30:17
I realize that this isn't your code but still - wh
 
Eric
2014/03/28 12:06:00
It's mine.
 
Oleksandr
2014/03/28 12:48:35
That's in TODO: http://codereview.adblockplus.org/
 
Wladimir Palant
2014/03/28 13:55:24
Using an existing comparison string is one part of
 | |
| 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 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 276 * begin() must have been called first. | 278 * begin() must have been called first. | 
| 277 */ | 279 */ | 
| 278 PROCESSENTRY32W * next() ; | 280 PROCESSENTRY32W * next() ; | 
| 279 | 281 | 
| 280 /** | 282 /** | 
| 281 * Type definition for pointer to underlying structure. | 283 * Type definition for pointer to underlying structure. | 
| 282 */ | 284 */ | 
| 283 typedef PROCESSENTRY32W * Pointer ; | 285 typedef PROCESSENTRY32W * Pointer ; | 
| 284 } ; | 286 } ; | 
| 285 | 287 | 
| 288 class ModulesSnapshot | |
| 289 { | |
| 290 /** | |
| 291 * Handle to the process snapshot. | |
| 292 */ | |
| 293 Windows_Handle handle ; | |
| 
Wladimir Palant
2014/03/28 07:30:17
Style nit: no space before the semicolon please (a
 | |
| 294 | |
| 295 /** | |
| 296 * Buffer for reading a single module entry out of the snapshot. | |
| 297 */ | |
| 298 MODULEENTRY32W module; | |
| 299 | |
| 300 /** | |
| 301 * Copy constructor declared private and not defined. | |
| 302 * | |
| 303 * \par Implementation | |
| 304 * Add "= delete" for C++11. | |
| 305 */ | |
| 306 ModulesSnapshot( const ModulesSnapshot & ) ; | |
| 307 | |
| 308 /** | |
| 309 * Copy assignment declared private and not defined. | |
| 310 * | |
| 311 * \par Implementation | |
| 312 * Add "= delete" for C++11. | |
| 313 */ | |
| 314 ModulesSnapshot operator=( const ModulesSnapshot & ) ; | |
| 315 | |
| 316 | |
| 317 public: | |
| 318 /** | |
| 319 * Default constructor takes the snapshot. | |
| 320 */ | |
| 321 ModulesSnapshot(DWORD processId) ; | |
| 322 | |
| 323 /** | |
| 324 * Return a pointer to the first process in the snapshot. | |
| 325 */ | |
| 326 MODULEENTRY32W * begin() ; | |
| 327 | |
| 328 /** | |
| 329 * The end pointer is an alias for the null pointer. | |
| 330 */ | |
| 331 inline MODULEENTRY32W * end() const { return 0 ; } | |
| 332 | |
| 333 /** | |
| 334 * Return a pointer to the next process in the snapshot. | |
| 335 * begin() must have been called first. | |
| 336 */ | |
| 337 MODULEENTRY32W * next() ; | |
| 338 | |
| 339 /** | |
| 340 * Type definition for pointer to underlying structure. | |
| 341 */ | |
| 342 typedef MODULEENTRY32W * Pointer ; | |
| 
Wladimir Palant
2014/03/28 07:30:17
What is that type good for? Faking a generic itera
 
Oleksandr
2014/03/28 12:48:35
Yes.
On 2014/03/28 07:30:17, Wladimir Palant wrote
 | |
| 343 } ; | |
| 344 | |
| 345 | |
| 286 //------------------------------------------------------- | 346 //------------------------------------------------------- | 
| 287 // initialize_process_list | 347 // initialize_process_list | 
| 288 //------------------------------------------------------- | 348 //------------------------------------------------------- | 
| 289 /** | 349 /** | 
| 290 * \tparam T The type into which a PROCESSENTRY32W struture is extracted. | 350 * \tparam T The type into which a PROCESSENTRY32W struture is extracted. | 
| 291 * \tparam Admittance Function type for argument 'admit' | 351 * \tparam Admittance Function type for argument 'admit' | 
| 292 * \tparam Extractor Function type for argument 'extract' | 352 * \tparam Extractor Function type for argument 'extract' | 
| 293 * \param admit A unary predicate function class that determines what's included | 353 * \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. | 354 * 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. | 355 * The use of this predicate is analogous to that in std::copy_if. | 
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 458 * Set of process identifiers matching one of the executable names. | 518 * Set of process identifiers matching one of the executable names. | 
| 459 */ | 519 */ | 
| 460 std::set< DWORD > pid_set ; | 520 std::set< DWORD > pid_set ; | 
| 461 | 521 | 
| 462 /** | 522 /** | 
| 463 * Set of executable names by which to filter. | 523 * Set of executable names by which to filter. | 
| 464 * | 524 * | 
| 465 * The argument of the filter constructor is a set by reference. | 525 * 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. | 526 * Since it does not make a copy for itself, we define it as a class member to provide its allocation. | 
| 467 */ | 527 */ | 
| 468 exe_name_set exe_names ; | 528 file_name_set file_names ; | 
| 529 file_name_set module_names ; | |
| 
Wladimir Palant
2014/03/28 07:30:17
Nit: The comment no longer applies here, I guess m
 
Eric
2014/03/28 12:06:00
Yes. The comment beginning /** is a Doxygen commen
 | |
| 469 | 530 | 
| 470 /** | 531 /** | 
| 471 * Filter function object matches on any of the exe names specified in the con structor. | 532 * Filter function object matches on any of the exe names specified in the con structor. | 
| 472 */ | 533 */ | 
| 473 process_by_any_exe_name_CI filter ; | 534 // process_by_any_file_name_CI filter ; | 
| 
Wladimir Palant
2014/03/28 07:30:17
Please remove this line instead of commenting it.
 | |
| 535 | |
| 536 process_by_any_exe_with_any_module filter ; | |
| 474 | 537 | 
| 475 /** | 538 /** | 
| 476 * Copy function object copies just the process ID. | 539 * Copy function object copies just the process ID. | 
| 477 */ | 540 */ | 
| 478 copy_PID copy ; | 541 copy_PID copy ; | 
| 479 | 542 | 
| 480 /** | 543 /** | 
| 481 * Snapshot of running processes. | 544 * Snapshot of running processes. | 
| 482 */ | 545 */ | 
| 483 Snapshot & snapshot ; | 546 Snapshot & snapshot ; | 
| (...skipping 30 matching lines...) Expand all Loading... | |
| 514 if ( ! b ) | 577 if ( ! b ) | 
| 515 { | 578 { | 
| 516 // Assert the process that created the window is not in our pid_set | 579 // Assert the process that created the window is not in our pid_set | 
| 517 return true ; | 580 return true ; | 
| 518 } | 581 } | 
| 519 return f( window ) ; | 582 return f( window ) ; | 
| 520 } | 583 } | 
| 521 } ; | 584 } ; | 
| 522 | 585 | 
| 523 public: | 586 public: | 
| 524 Process_Closer( Snapshot & snapshot, const wchar_t * exe_name_list[], size_t n _exe_names ) | 587 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 ) | 588 : snapshot( snapshot ), file_names( file_name_list, n_file_names ), module_n ames(module_name_list, n_module_names), filter( file_names, module_names ) | 
| 
Wladimir Palant
2014/03/28 07:30:17
With this signature getting very complex, it is pr
 
Oleksandr
2014/03/28 12:48:35
This won't work since we need to be able to pass a
 
Eric
2014/03/28 13:15:01
It should work for that. Zero is a valid template
 
Oleksandr
2014/03/28 13:49:27
Hm. Care to elaborate on this? The C++ specs seems
 
Eric
2014/03/28 15:13:19
Oops. I was wrong about that; you're right. The ti
 | |
| 526 { | 589 { | 
| 527 update() ; | 590 update() ; | 
| 528 } | 591 } | 
| 529 | 592 | 
| 530 /** | 593 /** | 
| 531 * Refresh our state to match the snapshot state. | 594 * Refresh our state to match the snapshot state. | 
| 532 */ | 595 */ | 
| 533 void refresh() | 596 void refresh() | 
| 534 { | 597 { | 
| 535 pid_set.clear() ; | 598 pid_set.clear() ; | 
| (...skipping 12 matching lines...) Expand all Loading... | |
| 548 } | 611 } | 
| 549 | 612 | 
| 550 /* | 613 /* | 
| 551 * Shut down every process in the pid_set. | 614 * Shut down every process in the pid_set. | 
| 552 */ | 615 */ | 
| 553 bool shut_down() ; | 616 bool shut_down() ; | 
| 554 | 617 | 
| 555 } ; | 618 } ; | 
| 556 | 619 | 
| 557 #endif | 620 #endif | 
| OLD | NEW |