[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome_frame/test/chrome_frame_test_utils.h" |
| 6 | |
[email protected] | 6e58a95 | 2011-01-12 19:28:50 | [diff] [blame] | 7 | #include <atlapp.h> |
| 8 | #include <atlmisc.h> |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 9 | #include <iepmapi.h> |
[email protected] | 42d7c02 | 2009-11-20 22:44:58 | [diff] [blame] | 10 | #include <sddl.h> |
[email protected] | 874d5dd6 | 2011-04-19 07:03:18 | [diff] [blame] | 11 | #include <shlobj.h> |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 12 | |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 13 | #include "base/command_line.h" |
[email protected] | 59aa617b | 2010-09-02 22:02:09 | [diff] [blame] | 14 | #include "base/file_path.h" |
[email protected] | 874d5dd6 | 2011-04-19 07:03:18 | [diff] [blame] | 15 | #include "base/file_util.h" |
[email protected] | d8b83b21 | 2010-04-07 18:27:52 | [diff] [blame] | 16 | #include "base/file_version_info.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 17 | #include "base/memory/scoped_ptr.h" |
[email protected] | d23ca37 | 2010-02-16 20:36:09 | [diff] [blame] | 18 | #include "base/path_service.h" |
[email protected] | d9f9219 | 2010-06-23 14:51:32 | [diff] [blame] | 19 | #include "base/process_util.h" |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 20 | #include "base/string_util.h" |
[email protected] | d3451d83 | 2010-10-01 11:17:37 | [diff] [blame] | 21 | #include "base/stringprintf.h" |
[email protected] | d55194ca | 2010-03-11 18:25:45 | [diff] [blame] | 22 | #include "base/utf_string_conversions.h" |
[email protected] | 2d650398 | 2010-10-17 04:41:54 | [diff] [blame] | 23 | #include "base/win/registry.h" |
[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 24 | #include "base/win/scoped_handle.h" |
[email protected] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 25 | #include "base/win/windows_version.h" |
[email protected] | bc73b4e5 | 2010-03-26 04:16:20 | [diff] [blame] | 26 | #include "chrome/common/chrome_paths.h" |
| 27 | #include "chrome/common/chrome_paths_internal.h" |
[email protected] | 8ee65ba | 2011-04-12 20:53:23 | [diff] [blame] | 28 | #include "chrome/common/chrome_switches.h" |
[email protected] | 7bc272f | 2009-12-09 01:09:28 | [diff] [blame] | 29 | #include "chrome_frame/utils.h" |
[email protected] | 7dd06a0 | 2010-12-09 02:56:24 | [diff] [blame] | 30 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 31 | #include "ui/base/clipboard/clipboard.h" |
| 32 | #include "ui/base/clipboard/scoped_clipboard_writer.h" |
[email protected] | bc73b4e5 | 2010-03-26 04:16:20 | [diff] [blame] | 33 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 34 | namespace chrome_frame_test { |
| 35 | |
[email protected] | 2014db00 | 2010-09-28 13:46:10 | [diff] [blame] | 36 | const wchar_t kCrashServicePipeName[] = L"\\\\.\\pipe\\ChromeCrashServices"; |
| 37 | |
| 38 | const DWORD kCrashServicePipeDesiredAccess = FILE_READ_DATA | |
| 39 | FILE_WRITE_DATA | |
| 40 | FILE_WRITE_ATTRIBUTES; |
| 41 | |
| 42 | const DWORD kCrashServicePipeFlagsAndAttributes = SECURITY_IDENTIFICATION | |
| 43 | SECURITY_SQOS_PRESENT; |
| 44 | const int kCrashServiceStartupTimeoutMs = 500; |
| 45 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 46 | const wchar_t kIEImageName[] = L"iexplore.exe"; |
| 47 | const wchar_t kIEBrokerImageName[] = L"ieuser.exe"; |
[email protected] | bcd840c | 2010-05-27 10:43:00 | [diff] [blame] | 48 | const char kChromeImageName[] = "chrome.exe"; |
[email protected] | d8b83b21 | 2010-04-07 18:27:52 | [diff] [blame] | 49 | const wchar_t kIEProfileName[] = L"iexplore"; |
[email protected] | 128f9d9 | 2010-04-12 15:58:52 | [diff] [blame] | 50 | const wchar_t kChromeLauncher[] = L"chrome_launcher.exe"; |
[email protected] | ba443b7 | 2010-05-14 23:34:38 | [diff] [blame] | 51 | const int kChromeFrameLongNavigationTimeoutInSeconds = 10; |
[email protected] | 0cc270a | 2011-02-03 21:08:00 | [diff] [blame] | 52 | const int kChromeFrameVeryLongNavigationTimeoutInSeconds = 30; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 53 | |
[email protected] | 7dd06a0 | 2010-12-09 02:56:24 | [diff] [blame] | 54 | const wchar_t TempRegKeyOverride::kTempTestKeyPath[] = |
| 55 | L"Software\\Chromium\\TempTestKeys"; |
| 56 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 57 | // Callback function for EnumThreadWindows. |
| 58 | BOOL CALLBACK CloseWindowsThreadCallback(HWND hwnd, LPARAM param) { |
| 59 | int& count = *reinterpret_cast<int*>(param); |
| 60 | if (IsWindowVisible(hwnd)) { |
| 61 | if (IsWindowEnabled(hwnd)) { |
| 62 | DWORD results = 0; |
| 63 | if (!::SendMessageTimeout(hwnd, WM_SYSCOMMAND, SC_CLOSE, 0, SMTO_BLOCK, |
| 64 | 10000, &results)) { |
[email protected] | d3451d83 | 2010-10-01 11:17:37 | [diff] [blame] | 65 | LOG(WARNING) << "Window hung: " << base::StringPrintf(L"%08X", hwnd); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 66 | } |
| 67 | count++; |
| 68 | } else { |
| 69 | DLOG(WARNING) << "Skipping disabled window: " |
[email protected] | d3451d83 | 2010-10-01 11:17:37 | [diff] [blame] | 70 | << base::StringPrintf(L"%08X", hwnd); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | return TRUE; // continue enumeration |
| 74 | } |
| 75 | |
| 76 | // Attempts to close all non-child, visible windows on the given thread. |
| 77 | // The return value is the number of visible windows a close request was |
| 78 | // sent to. |
| 79 | int CloseVisibleTopLevelWindowsOnThread(DWORD thread_id) { |
| 80 | int window_close_attempts = 0; |
| 81 | EnumThreadWindows(thread_id, CloseWindowsThreadCallback, |
| 82 | reinterpret_cast<LPARAM>(&window_close_attempts)); |
| 83 | return window_close_attempts; |
| 84 | } |
| 85 | |
| 86 | // Enumerates the threads of a process and attempts to close visible non-child |
| 87 | // windows on all threads of the process. |
| 88 | // The return value is the number of visible windows a close request was |
| 89 | // sent to. |
| 90 | int CloseVisibleWindowsOnAllThreads(HANDLE process) { |
| 91 | DWORD process_id = ::GetProcessId(process); |
| 92 | if (process_id == 0) { |
| 93 | NOTREACHED(); |
| 94 | return 0; |
| 95 | } |
| 96 | |
[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 97 | base::win::ScopedHandle snapshot( |
| 98 | CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0)); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 99 | if (!snapshot.IsValid()) { |
| 100 | NOTREACHED(); |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | int window_close_attempts = 0; |
| 105 | THREADENTRY32 te = { sizeof(THREADENTRY32) }; |
| 106 | if (Thread32First(snapshot, &te)) { |
| 107 | do { |
| 108 | if (RTL_CONTAINS_FIELD(&te, te.dwSize, th32OwnerProcessID) && |
| 109 | te.th32OwnerProcessID == process_id) { |
| 110 | window_close_attempts += |
| 111 | CloseVisibleTopLevelWindowsOnThread(te.th32ThreadID); |
| 112 | } |
| 113 | te.dwSize = sizeof(te); |
| 114 | } while (Thread32Next(snapshot, &te)); |
| 115 | } |
| 116 | |
| 117 | return window_close_attempts; |
| 118 | } |
| 119 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 120 | std::wstring GetExecutableAppPath(const std::wstring& file) { |
| 121 | std::wstring kAppPathsKey = |
| 122 | L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\"; |
| 123 | |
| 124 | std::wstring app_path; |
[email protected] | 2d650398 | 2010-10-17 04:41:54 | [diff] [blame] | 125 | base::win::RegKey key(HKEY_LOCAL_MACHINE, (kAppPathsKey + file).c_str(), |
| 126 | KEY_READ); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 127 | if (key.Handle()) { |
| 128 | key.ReadValue(NULL, &app_path); |
| 129 | } |
| 130 | |
| 131 | return app_path; |
| 132 | } |
| 133 | |
| 134 | std::wstring FormatCommandForApp(const std::wstring& exe_name, |
| 135 | const std::wstring& argument) { |
[email protected] | d3451d83 | 2010-10-01 11:17:37 | [diff] [blame] | 136 | std::wstring reg_path( |
| 137 | base::StringPrintf(L"Applications\\%ls\\shell\\open\\command", |
| 138 | exe_name.c_str())); |
[email protected] | 2d650398 | 2010-10-17 04:41:54 | [diff] [blame] | 139 | base::win::RegKey key(HKEY_CLASSES_ROOT, reg_path.c_str(), KEY_READ); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 140 | |
| 141 | std::wstring command; |
| 142 | if (key.Handle()) { |
| 143 | key.ReadValue(NULL, &command); |
| 144 | int found = command.find(L"%1"); |
| 145 | if (found >= 0) { |
| 146 | command.replace(found, 2, argument); |
| 147 | } |
| 148 | } |
| 149 | return command; |
| 150 | } |
| 151 | |
| 152 | base::ProcessHandle LaunchExecutable(const std::wstring& executable, |
| 153 | const std::wstring& argument) { |
| 154 | base::ProcessHandle process = NULL; |
| 155 | std::wstring path = GetExecutableAppPath(executable); |
| 156 | if (path.empty()) { |
| 157 | path = FormatCommandForApp(executable, argument); |
| 158 | if (path.empty()) { |
[email protected] | 7ec1f4e | 2010-05-19 18:43:21 | [diff] [blame] | 159 | LOG(ERROR) << "Failed to find executable: " << executable; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 160 | } else { |
[email protected] | 51343d5a | 2009-10-26 22:39:33 | [diff] [blame] | 161 | CommandLine cmdline = CommandLine::FromString(path); |
[email protected] | e599218 | 2011-07-15 16:47:02 | [diff] [blame^] | 162 | if (!base::LaunchProcess(cmdline, base::LaunchOptions(), &process)) { |
[email protected] | adda869 | 2011-07-14 20:55:30 | [diff] [blame] | 163 | LOG(ERROR) << "LaunchProcess failed: " << ::GetLastError(); |
[email protected] | 7ec1f4e | 2010-05-19 18:43:21 | [diff] [blame] | 164 | } |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 165 | } |
| 166 | } else { |
[email protected] | d1540c8 | 2009-10-27 00:18:32 | [diff] [blame] | 167 | CommandLine cmdline((FilePath(path))); |
[email protected] | 0445eb4 | 2010-08-13 22:10:30 | [diff] [blame] | 168 | cmdline.AppendArgNative(argument); |
[email protected] | e599218 | 2011-07-15 16:47:02 | [diff] [blame^] | 169 | if (!base::LaunchProcess(cmdline, base::LaunchOptions(), &process)) { |
[email protected] | adda869 | 2011-07-14 20:55:30 | [diff] [blame] | 170 | LOG(ERROR) << "LaunchProcess failed: " << ::GetLastError(); |
[email protected] | 7ec1f4e | 2010-05-19 18:43:21 | [diff] [blame] | 171 | } |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 172 | } |
| 173 | return process; |
| 174 | } |
| 175 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 176 | base::ProcessHandle LaunchChrome(const std::wstring& url) { |
[email protected] | bcd840c | 2010-05-27 10:43:00 | [diff] [blame] | 177 | FilePath path; |
[email protected] | d23ca37 | 2010-02-16 20:36:09 | [diff] [blame] | 178 | PathService::Get(base::DIR_MODULE, &path); |
[email protected] | bcd840c | 2010-05-27 10:43:00 | [diff] [blame] | 179 | path = path.AppendASCII(kChromeImageName); |
[email protected] | d23ca37 | 2010-02-16 20:36:09 | [diff] [blame] | 180 | |
[email protected] | bcd840c | 2010-05-27 10:43:00 | [diff] [blame] | 181 | CommandLine cmd(path); |
[email protected] | 0445eb4 | 2010-08-13 22:10:30 | [diff] [blame] | 182 | cmd.AppendSwitch(switches::kNoFirstRun); |
| 183 | cmd.AppendArgNative(url); |
[email protected] | d23ca37 | 2010-02-16 20:36:09 | [diff] [blame] | 184 | |
| 185 | base::ProcessHandle process = NULL; |
[email protected] | e599218 | 2011-07-15 16:47:02 | [diff] [blame^] | 186 | base::LaunchProcess(cmd, base::LaunchOptions(), &process); |
[email protected] | d23ca37 | 2010-02-16 20:36:09 | [diff] [blame] | 187 | return process; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 188 | } |
| 189 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 190 | base::ProcessHandle LaunchIEOnVista(const std::wstring& url) { |
| 191 | typedef HRESULT (WINAPI* IELaunchURLPtr)( |
| 192 | const wchar_t* url, |
| 193 | PROCESS_INFORMATION *pi, |
| 194 | VOID *info); |
| 195 | |
| 196 | IELaunchURLPtr launch; |
| 197 | PROCESS_INFORMATION pi = {0}; |
| 198 | IELAUNCHURLINFO info = {sizeof info, 0}; |
| 199 | HMODULE h = LoadLibrary(L"ieframe.dll"); |
[email protected] | 7ec1f4e | 2010-05-19 18:43:21 | [diff] [blame] | 200 | if (!h) { |
| 201 | LOG(ERROR) << "Failed to load ieframe.dll: " << ::GetLastError(); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 202 | return NULL; |
[email protected] | 7ec1f4e | 2010-05-19 18:43:21 | [diff] [blame] | 203 | } |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 204 | launch = reinterpret_cast<IELaunchURLPtr>(GetProcAddress(h, "IELaunchURL")); |
[email protected] | 7ec1f4e | 2010-05-19 18:43:21 | [diff] [blame] | 205 | CHECK(launch); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 206 | HRESULT hr = launch(url.c_str(), &pi, &info); |
| 207 | FreeLibrary(h); |
[email protected] | 7ec1f4e | 2010-05-19 18:43:21 | [diff] [blame] | 208 | if (SUCCEEDED(hr)) { |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 209 | CloseHandle(pi.hThread); |
[email protected] | 7ec1f4e | 2010-05-19 18:43:21 | [diff] [blame] | 210 | } else { |
[email protected] | d3451d83 | 2010-10-01 11:17:37 | [diff] [blame] | 211 | LOG(ERROR) << base::StringPrintf("IELaunchURL failed: 0x%08X", hr); |
[email protected] | 7ec1f4e | 2010-05-19 18:43:21 | [diff] [blame] | 212 | } |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 213 | return pi.hProcess; |
| 214 | } |
| 215 | |
| 216 | base::ProcessHandle LaunchIE(const std::wstring& url) { |
[email protected] | 874d5dd6 | 2011-04-19 07:03:18 | [diff] [blame] | 217 | if (GetInstalledIEVersion() >= IE_8) { |
| 218 | chrome_frame_test::ClearIESessionHistory(); |
| 219 | } |
| 220 | |
[email protected] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 221 | if (base::win::GetVersion() >= base::win::VERSION_VISTA) { |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 222 | return LaunchIEOnVista(url); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 223 | } |
[email protected] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 224 | return LaunchExecutable(kIEImageName, url); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | int CloseAllIEWindows() { |
| 228 | int ret = 0; |
| 229 | |
[email protected] | 8ee65ba | 2011-04-12 20:53:23 | [diff] [blame] | 230 | base::win::ScopedComPtr<IShellWindows> windows; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 231 | HRESULT hr = ::CoCreateInstance(__uuidof(ShellWindows), NULL, CLSCTX_ALL, |
| 232 | IID_IShellWindows, reinterpret_cast<void**>(windows.Receive())); |
| 233 | DCHECK(SUCCEEDED(hr)); |
| 234 | |
| 235 | if (SUCCEEDED(hr)) { |
| 236 | long count = 0; // NOLINT |
| 237 | windows->get_Count(&count); |
| 238 | VARIANT i = { VT_I4 }; |
| 239 | for (i.lVal = 0; i.lVal < count; ++i.lVal) { |
[email protected] | 8ee65ba | 2011-04-12 20:53:23 | [diff] [blame] | 240 | base::win::ScopedComPtr<IDispatch> folder; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 241 | windows->Item(i, folder.Receive()); |
| 242 | if (folder != NULL) { |
[email protected] | 8ee65ba | 2011-04-12 20:53:23 | [diff] [blame] | 243 | base::win::ScopedComPtr<IWebBrowser2> browser; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 244 | if (SUCCEEDED(browser.QueryFrom(folder))) { |
[email protected] | 59aa617b | 2010-09-02 22:02:09 | [diff] [blame] | 245 | bool is_ie = true; |
| 246 | HWND window = NULL; |
| 247 | // Check the class of the browser window to make sure we only close |
| 248 | // IE windows. |
| 249 | if (browser->get_HWND(reinterpret_cast<SHANDLE_PTR*>(window))) { |
| 250 | wchar_t class_name[MAX_PATH]; |
| 251 | if (::GetClassName(window, class_name, arraysize(class_name))) { |
| 252 | is_ie = _wcsicmp(class_name, L"IEFrame") == 0; |
| 253 | } |
| 254 | } |
| 255 | if (is_ie) { |
| 256 | browser->Quit(); |
| 257 | ++ret; |
| 258 | } |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | return ret; |
| 265 | } |
| 266 | |
[email protected] | 42d7c02 | 2009-11-20 22:44:58 | [diff] [blame] | 267 | |
| 268 | LowIntegrityToken::LowIntegrityToken() : impersonated_(false) { |
| 269 | } |
| 270 | |
| 271 | LowIntegrityToken::~LowIntegrityToken() { |
| 272 | RevertToSelf(); |
| 273 | } |
| 274 | |
| 275 | BOOL LowIntegrityToken::RevertToSelf() { |
| 276 | BOOL ok = TRUE; |
| 277 | if (impersonated_) { |
| 278 | DCHECK(IsImpersonated()); |
| 279 | ok = ::RevertToSelf(); |
| 280 | if (ok) |
| 281 | impersonated_ = false; |
| 282 | } |
| 283 | |
| 284 | return ok; |
| 285 | } |
| 286 | |
| 287 | BOOL LowIntegrityToken::Impersonate() { |
| 288 | DCHECK(!impersonated_); |
| 289 | DCHECK(!IsImpersonated()); |
| 290 | HANDLE process_token_handle = NULL; |
| 291 | BOOL ok = ::OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE, |
| 292 | &process_token_handle); |
| 293 | if (!ok) { |
| 294 | DLOG(ERROR) << "::OpenProcessToken failed: " << GetLastError(); |
| 295 | return ok; |
| 296 | } |
| 297 | |
[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 298 | base::win::ScopedHandle process_token(process_token_handle); |
[email protected] | 42d7c02 | 2009-11-20 22:44:58 | [diff] [blame] | 299 | // Create impersonation low integrity token. |
| 300 | HANDLE impersonation_token_handle = NULL; |
| 301 | ok = ::DuplicateTokenEx(process_token, |
| 302 | TOKEN_QUERY | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, NULL, |
| 303 | SecurityImpersonation, TokenImpersonation, &impersonation_token_handle); |
| 304 | if (!ok) { |
| 305 | DLOG(ERROR) << "::DuplicateTokenEx failed: " << GetLastError(); |
| 306 | return ok; |
| 307 | } |
| 308 | |
[email protected] | 7ec1f4e | 2010-05-19 18:43:21 | [diff] [blame] | 309 | // TODO(stoyan): sandbox/src/restricted_token_utils.cc has |
| 310 | // SetTokenIntegrityLevel function already. |
[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 311 | base::win::ScopedHandle impersonation_token(impersonation_token_handle); |
[email protected] | 42d7c02 | 2009-11-20 22:44:58 | [diff] [blame] | 312 | PSID integrity_sid = NULL; |
| 313 | TOKEN_MANDATORY_LABEL tml = {0}; |
| 314 | ok = ::ConvertStringSidToSid(SDDL_ML_LOW, &integrity_sid); |
| 315 | if (!ok) { |
| 316 | DLOG(ERROR) << "::ConvertStringSidToSid failed: " << GetLastError(); |
| 317 | return ok; |
| 318 | } |
| 319 | |
| 320 | tml.Label.Attributes = SE_GROUP_INTEGRITY | SE_GROUP_INTEGRITY_ENABLED; |
| 321 | tml.Label.Sid = integrity_sid; |
| 322 | ok = ::SetTokenInformation(impersonation_token, TokenIntegrityLevel, |
| 323 | &tml, sizeof(tml) + ::GetLengthSid(integrity_sid)); |
| 324 | ::LocalFree(integrity_sid); |
| 325 | if (!ok) { |
| 326 | DLOG(ERROR) << "::SetTokenInformation failed: " << GetLastError(); |
| 327 | return ok; |
| 328 | } |
| 329 | |
| 330 | // Switch current thread to low integrity. |
| 331 | ok = ::ImpersonateLoggedOnUser(impersonation_token); |
| 332 | if (ok) { |
| 333 | impersonated_ = true; |
| 334 | } else { |
| 335 | DLOG(ERROR) << "::ImpersonateLoggedOnUser failed: " << GetLastError(); |
| 336 | } |
| 337 | |
| 338 | return ok; |
| 339 | } |
| 340 | |
| 341 | bool LowIntegrityToken::IsImpersonated() { |
| 342 | HANDLE token = NULL; |
| 343 | if (!::OpenThreadToken(::GetCurrentThread(), 0, false, &token) && |
| 344 | ::GetLastError() != ERROR_NO_TOKEN) { |
| 345 | return true; |
| 346 | } |
| 347 | |
| 348 | if (token) |
| 349 | ::CloseHandle(token); |
| 350 | |
| 351 | return false; |
| 352 | } |
| 353 | |
[email protected] | 7bc272f | 2009-12-09 01:09:28 | [diff] [blame] | 354 | HRESULT LaunchIEAsComServer(IWebBrowser2** web_browser) { |
| 355 | if (!web_browser) |
| 356 | return E_INVALIDARG; |
| 357 | |
[email protected] | 874d5dd6 | 2011-04-19 07:03:18 | [diff] [blame] | 358 | if (GetInstalledIEVersion() >= IE_8) { |
| 359 | chrome_frame_test::ClearIESessionHistory(); |
| 360 | } |
| 361 | |
[email protected] | 7aa8d97 | 2010-02-17 04:24:49 | [diff] [blame] | 362 | AllowSetForegroundWindow(ASFW_ANY); |
| 363 | |
[email protected] | 7bc272f | 2009-12-09 01:09:28 | [diff] [blame] | 364 | HRESULT hr = S_OK; |
| 365 | DWORD cocreate_flags = CLSCTX_LOCAL_SERVER; |
| 366 | chrome_frame_test::LowIntegrityToken token; |
| 367 | // Vista has a bug which manifests itself when a medium integrity process |
| 368 | // launches a COM server like IE which runs in protected mode due to UAC. |
| 369 | // This causes the IWebBrowser2 interface which is returned to be useless, |
| 370 | // i.e it does not receive any events, etc. Our workaround for this is |
| 371 | // to impersonate a low integrity token and then launch IE. |
[email protected] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 372 | if (base::win::GetVersion() == base::win::VERSION_VISTA && |
[email protected] | 6448b3ab | 2010-07-12 22:07:03 | [diff] [blame] | 373 | GetInstalledIEVersion() == IE_7) { |
[email protected] | 7bc272f | 2009-12-09 01:09:28 | [diff] [blame] | 374 | // Create medium integrity browser that will launch IE broker. |
[email protected] | 8ee65ba | 2011-04-12 20:53:23 | [diff] [blame] | 375 | base::win::ScopedComPtr<IWebBrowser2> medium_integrity_browser; |
[email protected] | 7bc272f | 2009-12-09 01:09:28 | [diff] [blame] | 376 | hr = medium_integrity_browser.CreateInstance(CLSID_InternetExplorer, NULL, |
| 377 | CLSCTX_LOCAL_SERVER); |
| 378 | if (FAILED(hr)) |
| 379 | return hr; |
| 380 | medium_integrity_browser->Quit(); |
| 381 | // Broker remains alive. |
| 382 | if (!token.Impersonate()) { |
| 383 | hr = HRESULT_FROM_WIN32(GetLastError()); |
| 384 | return hr; |
| 385 | } |
| 386 | |
| 387 | cocreate_flags |= CLSCTX_ENABLE_CLOAKING; |
| 388 | } |
| 389 | |
| 390 | hr = ::CoCreateInstance(CLSID_InternetExplorer, NULL, |
| 391 | cocreate_flags, IID_IWebBrowser2, |
| 392 | reinterpret_cast<void**>(web_browser)); |
| 393 | // ~LowIntegrityToken() will switch integrity back to medium. |
| 394 | return hr; |
| 395 | } |
| 396 | |
[email protected] | 38f57abb | 2010-12-01 19:58:30 | [diff] [blame] | 397 | // TODO([email protected]) Could share this code with chrome_frame_plugin.h |
[email protected] | bc73b4e5 | 2010-03-26 04:16:20 | [diff] [blame] | 398 | FilePath GetProfilePath(const std::wstring& profile_name) { |
| 399 | FilePath profile_path; |
| 400 | chrome::GetChromeFrameUserDataDirectory(&profile_path); |
| 401 | return profile_path.Append(profile_name); |
| 402 | } |
| 403 | |
[email protected] | 1da3517 | 2010-04-22 02:30:03 | [diff] [blame] | 404 | std::wstring GetExeVersion(const std::wstring& exe_path) { |
| 405 | scoped_ptr<FileVersionInfo> ie_version_info( |
| 406 | FileVersionInfo::CreateFileVersionInfo(FilePath(exe_path))); |
| 407 | return ie_version_info->product_version(); |
| 408 | } |
| 409 | |
| 410 | IEVersion GetInstalledIEVersion() { |
| 411 | std::wstring path = chrome_frame_test::GetExecutableAppPath(kIEImageName); |
| 412 | std::wstring version = GetExeVersion(path); |
| 413 | |
| 414 | switch (version[0]) { |
| 415 | case '6': |
| 416 | return IE_6; |
| 417 | case '7': |
| 418 | return IE_7; |
| 419 | case '8': |
| 420 | return IE_8; |
[email protected] | d8e1351 | 2010-09-22 17:02:58 | [diff] [blame] | 421 | case '9': |
| 422 | return IE_9; |
[email protected] | 1da3517 | 2010-04-22 02:30:03 | [diff] [blame] | 423 | default: |
| 424 | break; |
| 425 | } |
| 426 | |
| 427 | return IE_UNSUPPORTED; |
| 428 | } |
| 429 | |
[email protected] | d8b83b21 | 2010-04-07 18:27:52 | [diff] [blame] | 430 | FilePath GetProfilePathForIE() { |
| 431 | FilePath profile_path; |
| 432 | // Browsers without IDeleteBrowsingHistory in non-priv mode |
| 433 | // have their profiles moved into "Temporary Internet Files". |
| 434 | // The code below basically retrieves the version of IE and computes |
| 435 | // the profile directory accordingly. |
[email protected] | d9ca776c | 2011-01-31 21:44:26 | [diff] [blame] | 436 | if (GetInstalledIEVersion() <= IE_7) { |
[email protected] | d8b83b21 | 2010-04-07 18:27:52 | [diff] [blame] | 437 | profile_path = GetIETemporaryFilesFolder(); |
| 438 | profile_path = profile_path.Append(L"Google Chrome Frame"); |
[email protected] | 38f57abb | 2010-12-01 19:58:30 | [diff] [blame] | 439 | } else { |
| 440 | profile_path = GetProfilePath(kIEProfileName); |
[email protected] | d8b83b21 | 2010-04-07 18:27:52 | [diff] [blame] | 441 | } |
| 442 | return profile_path; |
| 443 | } |
| 444 | |
[email protected] | 6448b3ab | 2010-07-12 22:07:03 | [diff] [blame] | 445 | FilePath GetTestDataFolder() { |
| 446 | FilePath test_dir; |
| 447 | PathService::Get(base::DIR_SOURCE_ROOT, &test_dir); |
| 448 | test_dir = test_dir.Append(FILE_PATH_LITERAL("chrome_frame")) |
| 449 | .Append(FILE_PATH_LITERAL("test")) |
| 450 | .Append(FILE_PATH_LITERAL("data")); |
| 451 | return test_dir; |
| 452 | } |
| 453 | |
[email protected] | 5b811ad | 2010-11-24 15:24:05 | [diff] [blame] | 454 | FilePath GetSeleniumTestFolder() { |
| 455 | FilePath test_dir; |
| 456 | PathService::Get(base::DIR_SOURCE_ROOT, &test_dir); |
| 457 | test_dir = test_dir.Append(FILE_PATH_LITERAL("data")) |
| 458 | .Append(FILE_PATH_LITERAL("selenium_core")); |
| 459 | return test_dir; |
| 460 | } |
| 461 | |
[email protected] | 6448b3ab | 2010-07-12 22:07:03 | [diff] [blame] | 462 | std::wstring GetPathFromUrl(const std::wstring& url) { |
| 463 | string16 url16 = WideToUTF16(url); |
| 464 | GURL gurl = GURL(url16); |
| 465 | if (gurl.has_query()) { |
| 466 | GURL::Replacements replacements; |
| 467 | replacements.ClearQuery(); |
| 468 | gurl = gurl.ReplaceComponents(replacements); |
| 469 | } |
| 470 | return UTF8ToWide(gurl.PathForRequest()); |
| 471 | } |
| 472 | |
| 473 | std::wstring GetPathAndQueryFromUrl(const std::wstring& url) { |
| 474 | string16 url16 = WideToUTF16(url); |
| 475 | GURL gurl = GURL(url16); |
| 476 | return UTF8ToWide(gurl.PathForRequest()); |
| 477 | } |
| 478 | |
[email protected] | fe30d76 | 2010-11-30 17:37:19 | [diff] [blame] | 479 | std::wstring GetClipboardText() { |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 480 | ui::Clipboard clipboard; |
[email protected] | fe30d76 | 2010-11-30 17:37:19 | [diff] [blame] | 481 | string16 text16; |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 482 | clipboard.ReadText(ui::Clipboard::BUFFER_STANDARD, &text16); |
[email protected] | fe30d76 | 2010-11-30 17:37:19 | [diff] [blame] | 483 | return UTF16ToWide(text16); |
| 484 | } |
| 485 | |
| 486 | void SetClipboardText(const std::wstring& text) { |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 487 | ui::Clipboard clipboard; |
[email protected] | fe30d76 | 2010-11-30 17:37:19 | [diff] [blame] | 488 | { |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 489 | ui::ScopedClipboardWriter clipboard_writer(&clipboard); |
[email protected] | fe30d76 | 2010-11-30 17:37:19 | [diff] [blame] | 490 | clipboard_writer.WriteText(WideToUTF16(text)); |
| 491 | } |
| 492 | } |
| 493 | |
[email protected] | 6448b3ab | 2010-07-12 22:07:03 | [diff] [blame] | 494 | bool AddCFMetaTag(std::string* html_data) { |
| 495 | if (!html_data) { |
| 496 | NOTREACHED(); |
| 497 | return false; |
| 498 | } |
[email protected] | 59aa617b | 2010-09-02 22:02:09 | [diff] [blame] | 499 | std::string lower = StringToLowerASCII(*html_data); |
| 500 | size_t head = lower.find("<head>"); |
[email protected] | 6448b3ab | 2010-07-12 22:07:03 | [diff] [blame] | 501 | if (head == std::string::npos) { |
| 502 | // Add missing head section. |
[email protected] | 59aa617b | 2010-09-02 22:02:09 | [diff] [blame] | 503 | size_t html = lower.find("<html>"); |
[email protected] | 6448b3ab | 2010-07-12 22:07:03 | [diff] [blame] | 504 | if (html != std::string::npos) { |
[email protected] | 59aa617b | 2010-09-02 22:02:09 | [diff] [blame] | 505 | head = html + strlen("<html>"); |
| 506 | html_data->insert(head, "<head></head>"); |
| 507 | } else { |
| 508 | DLOG(ERROR) << "Meta tag will not be injected " |
| 509 | << "because the html tag could not be found"; |
[email protected] | 6448b3ab | 2010-07-12 22:07:03 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | if (head != std::string::npos) { |
| 513 | html_data->insert( |
| 514 | head + strlen("<head>"), |
| 515 | "<meta http-equiv=\"x-ua-compatible\" content=\"chrome=1\" />"); |
| 516 | } |
| 517 | return head != std::string::npos; |
| 518 | } |
| 519 | |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 520 | CloseIeAtEndOfScope::~CloseIeAtEndOfScope() { |
| 521 | int closed = CloseAllIEWindows(); |
| 522 | DLOG_IF(ERROR, closed != 0) << "Closed " << closed << " windows forcefully"; |
| 523 | } |
| 524 | |
[email protected] | 2014db00 | 2010-09-28 13:46:10 | [diff] [blame] | 525 | // Attempt to connect to a running crash_service instance. Success occurs if we |
| 526 | // can actually connect to the service's pipe or we receive ERROR_PIPE_BUSY. |
| 527 | // Waits up to |timeout_ms| for success. |timeout_ms| may be 0, meaning only try |
| 528 | // once, or negative, meaning wait forever. |
| 529 | bool DetectRunningCrashService(int timeout_ms) { |
| 530 | // Wait for the crash_service.exe to be ready for clients. |
| 531 | base::Time start = base::Time::Now(); |
[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 532 | base::win::ScopedHandle new_pipe; |
[email protected] | 2014db00 | 2010-09-28 13:46:10 | [diff] [blame] | 533 | |
| 534 | while (true) { |
| 535 | new_pipe.Set(::CreateFile(kCrashServicePipeName, |
| 536 | kCrashServicePipeDesiredAccess, |
| 537 | 0, // dwShareMode |
| 538 | NULL, // lpSecurityAttributes |
| 539 | OPEN_EXISTING, |
| 540 | kCrashServicePipeFlagsAndAttributes, |
| 541 | NULL)); // hTemplateFile |
| 542 | |
| 543 | if (new_pipe.IsValid()) { |
| 544 | return true; |
| 545 | } |
| 546 | |
| 547 | switch (::GetLastError()) { |
| 548 | case ERROR_PIPE_BUSY: |
| 549 | // OK, it exists, let's assume that clients will eventually be able to |
| 550 | // connect to it. |
| 551 | return true; |
| 552 | case ERROR_FILE_NOT_FOUND: |
| 553 | // Wait a bit longer |
| 554 | break; |
| 555 | default: |
[email protected] | 6e58a95 | 2011-01-12 19:28:50 | [diff] [blame] | 556 | DPLOG(WARNING) << "Unexpected error while checking crash_service.exe's " |
| 557 | << "pipe."; |
[email protected] | 2014db00 | 2010-09-28 13:46:10 | [diff] [blame] | 558 | // Go ahead and wait in case it clears up. |
| 559 | break; |
| 560 | } |
| 561 | |
| 562 | if (timeout_ms == 0) { |
| 563 | return false; |
| 564 | } else if (timeout_ms > 0) { |
| 565 | base::TimeDelta duration = base::Time::Now() - start; |
| 566 | if (duration.InMilliseconds() > timeout_ms) { |
| 567 | return false; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | Sleep(10); |
| 572 | } |
| 573 | } |
| 574 | |
[email protected] | d9f9219 | 2010-06-23 14:51:32 | [diff] [blame] | 575 | base::ProcessHandle StartCrashService() { |
[email protected] | 2014db00 | 2010-09-28 13:46:10 | [diff] [blame] | 576 | if (DetectRunningCrashService(kCrashServiceStartupTimeoutMs)) { |
[email protected] | 2b9a9f16 | 2010-10-19 20:30:45 | [diff] [blame] | 577 | DVLOG(1) << "crash_service.exe is already running. We will use the " |
| 578 | "existing process and leave it running after tests complete."; |
[email protected] | 2014db00 | 2010-09-28 13:46:10 | [diff] [blame] | 579 | return NULL; |
| 580 | } |
| 581 | |
[email protected] | d9f9219 | 2010-06-23 14:51:32 | [diff] [blame] | 582 | FilePath exe_dir; |
| 583 | if (!PathService::Get(base::DIR_EXE, &exe_dir)) { |
| 584 | DCHECK(false); |
| 585 | return NULL; |
| 586 | } |
| 587 | |
| 588 | base::ProcessHandle crash_service = NULL; |
| 589 | |
[email protected] | 2b9a9f16 | 2010-10-19 20:30:45 | [diff] [blame] | 590 | DVLOG(1) << "Starting crash_service.exe so you know if a test crashes!"; |
[email protected] | 2014db00 | 2010-09-28 13:46:10 | [diff] [blame] | 591 | |
[email protected] | d9f9219 | 2010-06-23 14:51:32 | [diff] [blame] | 592 | FilePath crash_service_path = exe_dir.AppendASCII("crash_service.exe"); |
[email protected] | e599218 | 2011-07-15 16:47:02 | [diff] [blame^] | 593 | if (!base::LaunchProcess(crash_service_path.value(), base::LaunchOptions(), |
| 594 | &crash_service)) { |
[email protected] | d9f9219 | 2010-06-23 14:51:32 | [diff] [blame] | 595 | DLOG(ERROR) << "Couldn't start crash_service.exe"; |
| 596 | return NULL; |
| 597 | } |
| 598 | |
[email protected] | 2014db00 | 2010-09-28 13:46:10 | [diff] [blame] | 599 | base::Time start = base::Time::Now(); |
| 600 | |
| 601 | if (DetectRunningCrashService(kCrashServiceStartupTimeoutMs)) { |
[email protected] | 2b9a9f16 | 2010-10-19 20:30:45 | [diff] [blame] | 602 | DVLOG(1) << "crash_service.exe is ready for clients in " |
| 603 | << (base::Time::Now() - start).InMilliseconds() << " ms."; |
[email protected] | 2014db00 | 2010-09-28 13:46:10 | [diff] [blame] | 604 | return crash_service; |
| 605 | } else { |
| 606 | DLOG(ERROR) << "crash_service.exe failed to accept client connections " |
[email protected] | 2b9a9f16 | 2010-10-19 20:30:45 | [diff] [blame] | 607 | "within " << kCrashServiceStartupTimeoutMs << " ms. " |
| 608 | "Terminating it now."; |
[email protected] | 2014db00 | 2010-09-28 13:46:10 | [diff] [blame] | 609 | |
| 610 | // First check to see if it's even still running just to minimize the |
| 611 | // likelihood of spurious error messages from KillProcess. |
| 612 | if (WAIT_OBJECT_0 != ::WaitForSingleObject(crash_service, 0)) { |
| 613 | base::KillProcess(crash_service, 0, false); |
| 614 | } |
| 615 | return NULL; |
| 616 | } |
[email protected] | d9f9219 | 2010-06-23 14:51:32 | [diff] [blame] | 617 | } |
| 618 | |
[email protected] | 7dd06a0 | 2010-12-09 02:56:24 | [diff] [blame] | 619 | TempRegKeyOverride::TempRegKeyOverride(HKEY override, const wchar_t* temp_name) |
| 620 | : override_(override), temp_name_(temp_name) { |
| 621 | DCHECK(temp_name && lstrlenW(temp_name)); |
| 622 | std::wstring key_path(kTempTestKeyPath); |
| 623 | key_path += L"\\" + temp_name_; |
[email protected] | e06f4d5 | 2011-01-19 07:28:46 | [diff] [blame] | 624 | EXPECT_EQ(ERROR_SUCCESS, temp_key_.Create(HKEY_CURRENT_USER, key_path.c_str(), |
| 625 | KEY_ALL_ACCESS)); |
[email protected] | 7dd06a0 | 2010-12-09 02:56:24 | [diff] [blame] | 626 | EXPECT_EQ(ERROR_SUCCESS, |
| 627 | ::RegOverridePredefKey(override_, temp_key_.Handle())); |
| 628 | } |
| 629 | |
| 630 | TempRegKeyOverride::~TempRegKeyOverride() { |
| 631 | ::RegOverridePredefKey(override_, NULL); |
| 632 | // The temp key will be deleted via a call to DeleteAllTempKeys(). |
| 633 | } |
| 634 | |
| 635 | // static |
| 636 | void TempRegKeyOverride::DeleteAllTempKeys() { |
| 637 | base::win::RegKey key; |
[email protected] | e06f4d5 | 2011-01-19 07:28:46 | [diff] [blame] | 638 | if (key.Open(HKEY_CURRENT_USER, L"", KEY_ALL_ACCESS) == ERROR_SUCCESS) { |
[email protected] | 7dd06a0 | 2010-12-09 02:56:24 | [diff] [blame] | 639 | key.DeleteKey(kTempTestKeyPath); |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | ScopedVirtualizeHklmAndHkcu::ScopedVirtualizeHklmAndHkcu() { |
| 644 | TempRegKeyOverride::DeleteAllTempKeys(); |
| 645 | hklm_.reset(new TempRegKeyOverride(HKEY_LOCAL_MACHINE, L"hklm_fake")); |
| 646 | hkcu_.reset(new TempRegKeyOverride(HKEY_CURRENT_USER, L"hkcu_fake")); |
| 647 | } |
| 648 | |
| 649 | ScopedVirtualizeHklmAndHkcu::~ScopedVirtualizeHklmAndHkcu() { |
| 650 | hkcu_.reset(NULL); |
| 651 | hklm_.reset(NULL); |
| 652 | TempRegKeyOverride::DeleteAllTempKeys(); |
| 653 | } |
| 654 | |
[email protected] | 3c8a5c5 | 2010-12-22 00:07:15 | [diff] [blame] | 655 | bool KillProcesses(const std::wstring& executable_name, int exit_code, |
| 656 | bool wait) { |
| 657 | bool result = true; |
| 658 | base::NamedProcessIterator iter(executable_name, NULL); |
| 659 | while (const base::ProcessEntry* entry = iter.NextProcessEntry()) { |
| 660 | result &= base::KillProcessById(entry->pid(), exit_code, wait); |
| 661 | } |
| 662 | return result; |
| 663 | } |
| 664 | |
[email protected] | cf5912b | 2011-02-01 22:20:44 | [diff] [blame] | 665 | ScopedChromeFrameRegistrar::RegistrationType GetTestBedType() { |
| 666 | if (GetConfigBool(false, L"PerUserTestBed")) { |
| 667 | return ScopedChromeFrameRegistrar::PER_USER; |
| 668 | } else { |
| 669 | return ScopedChromeFrameRegistrar::SYSTEM_LEVEL; |
| 670 | } |
| 671 | } |
| 672 | |
[email protected] | 874d5dd6 | 2011-04-19 07:03:18 | [diff] [blame] | 673 | void ClearIESessionHistory() { |
| 674 | wchar_t local_app_data_path[MAX_PATH + 1] = {0}; |
| 675 | SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, |
| 676 | local_app_data_path); |
| 677 | |
| 678 | std::wstring session_history_path = local_app_data_path; |
| 679 | session_history_path += L"\\Microsoft\\Internet Explorer\\Recovery"; |
| 680 | file_util::Delete(session_history_path, true); |
| 681 | } |
| 682 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 683 | } // namespace chrome_frame_test |