Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #include "base/win/startup_information.h" |
michael@0 | 6 | |
michael@0 | 7 | #include "base/logging.h" |
michael@0 | 8 | #include "base/win/windows_version.h" |
michael@0 | 9 | |
michael@0 | 10 | namespace { |
michael@0 | 11 | |
michael@0 | 12 | typedef BOOL (WINAPI *InitializeProcThreadAttributeListFunction)( |
michael@0 | 13 | LPPROC_THREAD_ATTRIBUTE_LIST attribute_list, |
michael@0 | 14 | DWORD attribute_count, |
michael@0 | 15 | DWORD flags, |
michael@0 | 16 | PSIZE_T size); |
michael@0 | 17 | static InitializeProcThreadAttributeListFunction |
michael@0 | 18 | initialize_proc_thread_attribute_list; |
michael@0 | 19 | |
michael@0 | 20 | typedef BOOL (WINAPI *UpdateProcThreadAttributeFunction)( |
michael@0 | 21 | LPPROC_THREAD_ATTRIBUTE_LIST attribute_list, |
michael@0 | 22 | DWORD flags, |
michael@0 | 23 | DWORD_PTR attribute, |
michael@0 | 24 | PVOID value, |
michael@0 | 25 | SIZE_T size, |
michael@0 | 26 | PVOID previous_value, |
michael@0 | 27 | PSIZE_T return_size); |
michael@0 | 28 | static UpdateProcThreadAttributeFunction update_proc_thread_attribute_list; |
michael@0 | 29 | |
michael@0 | 30 | typedef VOID (WINAPI *DeleteProcThreadAttributeListFunction)( |
michael@0 | 31 | LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList); |
michael@0 | 32 | static DeleteProcThreadAttributeListFunction delete_proc_thread_attribute_list; |
michael@0 | 33 | |
michael@0 | 34 | } // namespace |
michael@0 | 35 | |
michael@0 | 36 | namespace base { |
michael@0 | 37 | namespace win { |
michael@0 | 38 | |
michael@0 | 39 | StartupInformation::StartupInformation() { |
michael@0 | 40 | memset(&startup_info_, 0, sizeof(startup_info_)); |
michael@0 | 41 | |
michael@0 | 42 | // Pre Windows Vista doesn't support STARTUPINFOEX. |
michael@0 | 43 | if (base::win::GetVersion() < base::win::VERSION_VISTA) { |
michael@0 | 44 | startup_info_.StartupInfo.cb = sizeof(STARTUPINFO); |
michael@0 | 45 | return; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | startup_info_.StartupInfo.cb = sizeof(startup_info_); |
michael@0 | 49 | |
michael@0 | 50 | // Load the attribute API functions. |
michael@0 | 51 | if (!initialize_proc_thread_attribute_list || |
michael@0 | 52 | !update_proc_thread_attribute_list || |
michael@0 | 53 | !delete_proc_thread_attribute_list) { |
michael@0 | 54 | HMODULE module = ::GetModuleHandleW(L"kernel32.dll"); |
michael@0 | 55 | initialize_proc_thread_attribute_list = |
michael@0 | 56 | reinterpret_cast<InitializeProcThreadAttributeListFunction>( |
michael@0 | 57 | ::GetProcAddress(module, "InitializeProcThreadAttributeList")); |
michael@0 | 58 | update_proc_thread_attribute_list = |
michael@0 | 59 | reinterpret_cast<UpdateProcThreadAttributeFunction>( |
michael@0 | 60 | ::GetProcAddress(module, "UpdateProcThreadAttribute")); |
michael@0 | 61 | delete_proc_thread_attribute_list = |
michael@0 | 62 | reinterpret_cast<DeleteProcThreadAttributeListFunction>( |
michael@0 | 63 | ::GetProcAddress(module, "DeleteProcThreadAttributeList")); |
michael@0 | 64 | } |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | StartupInformation::~StartupInformation() { |
michael@0 | 68 | if (startup_info_.lpAttributeList) { |
michael@0 | 69 | delete_proc_thread_attribute_list(startup_info_.lpAttributeList); |
michael@0 | 70 | delete [] reinterpret_cast<BYTE*>(startup_info_.lpAttributeList); |
michael@0 | 71 | } |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | bool StartupInformation::InitializeProcThreadAttributeList( |
michael@0 | 75 | DWORD attribute_count) { |
michael@0 | 76 | if (startup_info_.StartupInfo.cb != sizeof(startup_info_) || |
michael@0 | 77 | startup_info_.lpAttributeList) |
michael@0 | 78 | return false; |
michael@0 | 79 | |
michael@0 | 80 | SIZE_T size = 0; |
michael@0 | 81 | initialize_proc_thread_attribute_list(NULL, attribute_count, 0, &size); |
michael@0 | 82 | if (size == 0) |
michael@0 | 83 | return false; |
michael@0 | 84 | |
michael@0 | 85 | startup_info_.lpAttributeList = |
michael@0 | 86 | reinterpret_cast<LPPROC_THREAD_ATTRIBUTE_LIST>(new BYTE[size]); |
michael@0 | 87 | if (!initialize_proc_thread_attribute_list(startup_info_.lpAttributeList, |
michael@0 | 88 | attribute_count, 0, &size)) { |
michael@0 | 89 | delete [] reinterpret_cast<BYTE*>(startup_info_.lpAttributeList); |
michael@0 | 90 | startup_info_.lpAttributeList = NULL; |
michael@0 | 91 | return false; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | return true; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | bool StartupInformation::UpdateProcThreadAttribute( |
michael@0 | 98 | DWORD_PTR attribute, |
michael@0 | 99 | void* value, |
michael@0 | 100 | size_t size) { |
michael@0 | 101 | if (!startup_info_.lpAttributeList) |
michael@0 | 102 | return false; |
michael@0 | 103 | return !!update_proc_thread_attribute_list(startup_info_.lpAttributeList, 0, |
michael@0 | 104 | attribute, value, size, NULL, NULL); |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | } // namespace win |
michael@0 | 108 | } // namespace base |
michael@0 | 109 |