Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "chrome/browser/install_verification/win/module_list.h" | 5 #include "chrome/browser/install_verification/win/module_list.h" |
| 6 | 6 |
| 7 #include <Psapi.h> | 7 #include <Psapi.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "chrome/browser/install_verification/win/module_info.h" | 12 #include "chrome/browser/install_verification/win/module_info.h" |
| 13 | 13 |
| 14 namespace { | |
| 15 | |
| 16 void CheckFreeLibrary(HMODULE module) { | |
| 17 BOOL result = ::FreeLibrary(module); | |
| 18 DCHECK(result); | |
| 19 } | |
| 20 | |
| 21 } // namespace | |
| 22 | |
| 23 ModuleList::~ModuleList() { | 14 ModuleList::~ModuleList() { |
| 24 std::for_each(modules_.begin(), modules_.end(), &CheckFreeLibrary); | 15 for (const auto& module : modules_) { |
| 16 BOOL result = ::FreeLibrary(std::get<HMODULE>(module)); | |
| 17 DCHECK(result) << std::get<std::wstring>(module); | |
| 18 } | |
| 25 } | 19 } |
| 26 | 20 |
| 27 scoped_ptr<ModuleList> ModuleList::FromLoadedModuleSnapshot( | 21 scoped_ptr<ModuleList> ModuleList::FromLoadedModuleSnapshot( |
| 28 const std::vector<HMODULE>& snapshot) { | 22 const std::vector<HMODULE>& snapshot) { |
| 29 scoped_ptr<ModuleList> instance(new ModuleList); | 23 scoped_ptr<ModuleList> instance(new ModuleList); |
| 30 | 24 |
| 25 wchar_t module_name[MAX_PATH] = {0}; | |
|
robertshield
2016/04/19 00:54:23
nit: {}
| |
| 26 | |
| 31 for (size_t i = 0; i < snapshot.size(); ++i) { | 27 for (size_t i = 0; i < snapshot.size(); ++i) { |
| 32 HMODULE module = NULL; | 28 HMODULE module = NULL; |
| 33 // ::GetModuleHandleEx add-ref's the module if successful. | 29 // ::GetModuleHandleEx add-ref's the module if successful. |
| 34 if (::GetModuleHandleEx( | 30 if (::GetModuleHandleEx( |
| 35 GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, | 31 GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, |
| 36 reinterpret_cast<LPCWSTR>(snapshot[i]), | 32 reinterpret_cast<LPCWSTR>(snapshot[i]), |
| 37 &module)) { | 33 &module)) { |
| 38 instance->modules_.push_back(module); | 34 if (::GetModuleFileNameW(module, module_name, MAX_PATH)) { |
| 35 instance->modules_.push_back(std::make_tuple(module, module_name)); | |
| 36 } else { | |
| 37 instance->modules_.push_back(std::make_tuple(module, std::wstring())); | |
| 38 } | |
| 39 } | 39 } |
| 40 } | 40 } |
| 41 | 41 |
| 42 return instance; | 42 return instance; |
| 43 } | 43 } |
| 44 | 44 |
| 45 void ModuleList::GetModuleInfoSet(std::set<ModuleInfo>* module_info_set) { | 45 void ModuleList::GetModuleInfoSet(std::set<ModuleInfo>* module_info_set) { |
| 46 HANDLE current_process = ::GetCurrentProcess(); | 46 HANDLE current_process = ::GetCurrentProcess(); |
| 47 for (size_t i = 0; i < modules_.size(); ++i) { | 47 for (size_t i = 0; i < modules_.size(); ++i) { |
| 48 wchar_t filename[MAX_PATH]; | 48 wchar_t filename[MAX_PATH]; |
|
robertshield
2016/04/19 00:54:23
looks like this should have an = {} on the end?
| |
| 49 // Simply ignore modules where GetModuleFileName or GetModuleInformation | 49 // Simply ignore modules where GetModuleFileName or GetModuleInformation |
| 50 // failed, they might have been unloaded. | 50 // failed, they might have been unloaded. |
| 51 if (::GetModuleFileName(modules_[i], filename, MAX_PATH) && | 51 if (::GetModuleFileName(std::get<HMODULE>(modules_[i]), filename, |
| 52 MAX_PATH) && | |
|
robertshield
2016/04/19 00:54:22
Many of the entries in modules_ will already have
Will Harris
2016/04/19 00:59:05
yes I found that later, I think I should change ho
| |
| 52 filename[0]) { | 53 filename[0]) { |
| 53 MODULEINFO sys_module_info = {}; | 54 MODULEINFO sys_module_info = {}; |
| 54 if (::GetModuleInformation( | 55 if (::GetModuleInformation(current_process, |
| 55 current_process, modules_[i], | 56 std::get<HMODULE>(modules_[i]), |
| 56 &sys_module_info, sizeof(sys_module_info))) { | 57 &sys_module_info, sizeof(sys_module_info))) { |
| 57 module_info_set->insert(ModuleInfo( | 58 module_info_set->insert(ModuleInfo( |
| 58 filename, | 59 filename, |
| 59 reinterpret_cast<uintptr_t>(sys_module_info.lpBaseOfDll), | 60 reinterpret_cast<uintptr_t>(sys_module_info.lpBaseOfDll), |
| 60 sys_module_info.SizeOfImage)); | 61 sys_module_info.SizeOfImage)); |
| 61 } | 62 } |
| 62 } | 63 } |
| 63 } | 64 } |
| 64 } | 65 } |
| 65 | 66 |
| 66 ModuleList::ModuleList() {} | 67 ModuleList::ModuleList() {} |
| OLD | NEW |