| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // This file declares util functions for setup project. | 5 // This file declares util functions for setup project. |
| 6 | 6 |
| 7 #include "chrome/installer/setup/setup_util.h" | 7 #include "chrome/installer/setup/setup_util.h" |
| 8 | 8 |
| 9 #include <windows.h> | 9 #include <windows.h> |
| 10 | 10 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 } | 81 } |
| 82 | 82 |
| 83 return (version_found ? max_version.release() : NULL); | 83 return (version_found ? max_version.release() : NULL); |
| 84 } | 84 } |
| 85 | 85 |
| 86 bool DeleteFileFromTempProcess(const FilePath& path, | 86 bool DeleteFileFromTempProcess(const FilePath& path, |
| 87 uint32 delay_before_delete_ms) { | 87 uint32 delay_before_delete_ms) { |
| 88 static const wchar_t kRunDll32Path[] = | 88 static const wchar_t kRunDll32Path[] = |
| 89 L"%SystemRoot%\\System32\\rundll32.exe"; | 89 L"%SystemRoot%\\System32\\rundll32.exe"; |
| 90 wchar_t rundll32[MAX_PATH]; | 90 wchar_t rundll32[MAX_PATH]; |
| 91 DWORD size = ExpandEnvironmentStrings(kRunDll32Path, rundll32, | 91 DWORD size = |
| 92 arraysize(rundll32)); | 92 ExpandEnvironmentStrings(kRunDll32Path, rundll32, arraysize(rundll32)); |
| 93 if (!size || size >= MAX_PATH) | 93 if (!size || size >= MAX_PATH) |
| 94 return false; | 94 return false; |
| 95 | 95 |
| 96 STARTUPINFO startup = { sizeof(STARTUPINFO) }; | 96 STARTUPINFO startup = { sizeof(STARTUPINFO) }; |
| 97 PROCESS_INFORMATION pi = {0}; | 97 PROCESS_INFORMATION pi = {0}; |
| 98 BOOL ok = ::CreateProcess(NULL, rundll32, NULL, NULL, FALSE, CREATE_SUSPENDED, | 98 BOOL ok = ::CreateProcess(NULL, rundll32, NULL, NULL, FALSE, CREATE_SUSPENDED, |
| 99 NULL, NULL, &startup, &pi); | 99 NULL, NULL, &startup, &pi); |
| 100 if (ok) { | 100 if (ok) { |
| 101 // We use the main thread of the new process to run: | 101 // We use the main thread of the new process to run: |
| 102 // Sleep(delay_before_delete_ms); | 102 // Sleep(delay_before_delete_ms); |
| 103 // DeleteFile(path); | 103 // DeleteFile(path); |
| 104 // ExitProcess(0); | 104 // ExitProcess(0); |
| 105 // This runs before the main routine of the process runs, so it doesn't | 105 // This runs before the main routine of the process runs, so it doesn't |
| 106 // matter much which executable we choose except that we don't want to | 106 // matter much which executable we choose except that we don't want to |
| 107 // use e.g. a console app that causes a window to be created. | 107 // use e.g. a console app that causes a window to be created. |
| 108 size = (path.value().length() + 1) * sizeof(path.value()[0]); | 108 size = (path.value().length() + 1) * sizeof(path.value()[0]); |
| 109 void* mem = ::VirtualAllocEx(pi.hProcess, NULL, size, MEM_COMMIT, | 109 void* mem = ::VirtualAllocEx(pi.hProcess, NULL, size, MEM_COMMIT, |
| 110 PAGE_READWRITE); | 110 PAGE_READWRITE); |
| 111 if (mem) { | 111 if (mem) { |
| 112 SIZE_T written = 0; | 112 SIZE_T written = 0; |
| 113 ::WriteProcessMemory(pi.hProcess, mem, path.value().c_str(), | 113 ::WriteProcessMemory( |
| 114 pi.hProcess, mem, path.value().c_str(), |
| 114 (path.value().size() + 1) * sizeof(path.value()[0]), &written); | 115 (path.value().size() + 1) * sizeof(path.value()[0]), &written); |
| 115 HMODULE kernel32 = ::GetModuleHandle(L"kernel32.dll"); | 116 HMODULE kernel32 = ::GetModuleHandle(L"kernel32.dll"); |
| 116 PAPCFUNC sleep = reinterpret_cast<PAPCFUNC>( | 117 PAPCFUNC sleep = reinterpret_cast<PAPCFUNC>( |
| 117 ::GetProcAddress(kernel32, "Sleep")); | 118 ::GetProcAddress(kernel32, "Sleep")); |
| 118 PAPCFUNC delete_file = reinterpret_cast<PAPCFUNC>( | 119 PAPCFUNC delete_file = reinterpret_cast<PAPCFUNC>( |
| 119 ::GetProcAddress(kernel32, "DeleteFileW")); | 120 ::GetProcAddress(kernel32, "DeleteFileW")); |
| 120 PAPCFUNC exit_process = reinterpret_cast<PAPCFUNC>( | 121 PAPCFUNC exit_process = reinterpret_cast<PAPCFUNC>( |
| 121 ::GetProcAddress(kernel32, "ExitProcess")); | 122 ::GetProcAddress(kernel32, "ExitProcess")); |
| 122 if (!sleep || !delete_file || !exit_process) { | 123 if (!sleep || !delete_file || !exit_process) { |
| 123 NOTREACHED(); | 124 NOTREACHED(); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 142 | 143 |
| 143 string16 GetActiveSetupPath(BrowserDistribution* dist) { | 144 string16 GetActiveSetupPath(BrowserDistribution* dist) { |
| 144 static const wchar_t kInstalledComponentsPath[] = | 145 static const wchar_t kInstalledComponentsPath[] = |
| 145 L"Software\\Microsoft\\Active Setup\\Installed Components\\"; | 146 L"Software\\Microsoft\\Active Setup\\Installed Components\\"; |
| 146 return kInstalledComponentsPath + dist->GetAppGuid(); | 147 return kInstalledComponentsPath + dist->GetAppGuid(); |
| 147 } | 148 } |
| 148 | 149 |
| 149 ScopedTokenPrivilege::ScopedTokenPrivilege(const wchar_t* privilege_name) | 150 ScopedTokenPrivilege::ScopedTokenPrivilege(const wchar_t* privilege_name) |
| 150 : is_enabled_(false) { | 151 : is_enabled_(false) { |
| 151 if (!::OpenProcessToken(::GetCurrentProcess(), | 152 if (!::OpenProcessToken(::GetCurrentProcess(), |
| 152 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, token_.Receive())) { | 153 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, |
| 154 token_.Receive())) { |
| 153 return; | 155 return; |
| 154 } | 156 } |
| 155 | 157 |
| 156 LUID privilege_luid; | 158 LUID privilege_luid; |
| 157 if (!::LookupPrivilegeValue(NULL, privilege_name, &privilege_luid)) { | 159 if (!::LookupPrivilegeValue(NULL, privilege_name, &privilege_luid)) { |
| 158 token_.Close(); | 160 token_.Close(); |
| 159 return; | 161 return; |
| 160 } | 162 } |
| 161 | 163 |
| 162 // Adjust the token's privileges to enable |privilege_name|. If this privilege | 164 // Adjust the token's privileges to enable |privilege_name|. If this privilege |
| 163 // was already enabled, |previous_privileges_|.PrivilegeCount will be set to 0 | 165 // was already enabled, |previous_privileges_|.PrivilegeCount will be set to 0 |
| 164 // and we then know not to disable this privilege upon destruction. | 166 // and we then know not to disable this privilege upon destruction. |
| 165 TOKEN_PRIVILEGES tp; | 167 TOKEN_PRIVILEGES tp; |
| 166 tp.PrivilegeCount = 1; | 168 tp.PrivilegeCount = 1; |
| 167 tp.Privileges[0].Luid = privilege_luid; | 169 tp.Privileges[0].Luid = privilege_luid; |
| 168 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; | 170 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 169 DWORD return_length; | 171 DWORD return_length; |
| 170 if (!::AdjustTokenPrivileges(token_, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), | 172 if (!::AdjustTokenPrivileges(token_, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), |
| 171 &previous_privileges_, &return_length)) { | 173 &previous_privileges_, &return_length)) { |
| 172 token_.Close(); | 174 token_.Close(); |
| 173 } else { | 175 return; |
| 174 is_enabled_ = true; | |
| 175 } | 176 } |
| 177 |
| 178 is_enabled_ = true; |
| 176 } | 179 } |
| 177 | 180 |
| 178 ScopedTokenPrivilege::~ScopedTokenPrivilege() { | 181 ScopedTokenPrivilege::~ScopedTokenPrivilege() { |
| 179 if (is_enabled_ && previous_privileges_.PrivilegeCount != 0) { | 182 if (is_enabled_ && previous_privileges_.PrivilegeCount != 0) { |
| 180 ::AdjustTokenPrivileges(token_, FALSE, &previous_privileges_, | 183 ::AdjustTokenPrivileges(token_, FALSE, &previous_privileges_, |
| 181 sizeof(TOKEN_PRIVILEGES), NULL, NULL); | 184 sizeof(TOKEN_PRIVILEGES), NULL, NULL); |
| 182 } | 185 } |
| 183 } | 186 } |
| 184 | 187 |
| 185 } // namespace installer | 188 } // namespace installer |
| OLD | NEW |