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
3 // found in the LICENSE file.
5 #include "chrome_frame/test_utils.h"
13 #include "base/command_line.h"
14 #include "base/file_util.h"
15 #include "base/files/file_path.h"
16 #include "base/logging.h"
17 #include "base/path_service.h"
18 #include "base/process_util.h"
19 #include "base/stringprintf.h"
20 #include "base/utf_string_conversions.h"
21 #include "base/win/scoped_handle.h"
22 #include "chrome/common/chrome_paths.h"
23 #include "chrome/common/chrome_switches.h"
24 #include "chrome_frame/test/chrome_frame_test_utils.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 const wchar_t kChromeFrameDllName
[] = L
"npchrome_frame.dll";
28 const wchar_t kChromeLauncherExeName
[] = L
"chrome_launcher.exe";
29 // How long to wait for DLLs to register or unregister themselves before killing
31 const int64 kDllRegistrationTimeoutMs
= 30 * 1000;
33 base::FilePath
GetChromeFrameBuildPath() {
34 base::FilePath build_path
;
35 PathService::Get(chrome::DIR_APP
, &build_path
);
37 base::FilePath dll_path
= build_path
.Append(kChromeFrameDllName
);
39 if (!file_util::PathExists(dll_path
)) {
40 // Well, dang.. try looking in the current directory.
41 dll_path
= build_path
.Append(kChromeFrameDllName
);
44 if (!file_util::PathExists(dll_path
)) {
45 // No luck, return something empty.
46 dll_path
= base::FilePath();
52 const wchar_t ScopedChromeFrameRegistrar::kCallRegistrationEntrypointSwitch
[] =
53 L
"--call-registration-entrypoint";
55 bool ScopedChromeFrameRegistrar::register_chrome_path_provider_
= false;
58 void ScopedChromeFrameRegistrar::RegisterDefaults() {
59 if (!register_chrome_path_provider_
) {
60 chrome::RegisterPathProvider();
61 register_chrome_path_provider_
= true;
65 // Registers or unregisters the DLL at |path| by calling out to the current
66 // executable with --call-registration-entrypoint. Loading the DLL into the
67 // test process is problematic for component=shared_library builds since
68 // singletons in base.dll aren't designed to handle multiple initialization.
69 // Use of rundll32.exe is problematic since it does not return useful error
72 void ScopedChromeFrameRegistrar::DoRegistration(
74 RegistrationType registration_type
,
75 RegistrationOperation registration_operation
) {
76 static const char* const kEntrypoints
[] = {
78 "DllUnregisterServer",
79 "DllRegisterUserServer",
80 "DllUnregisterUserServer",
83 DCHECK(!path
.empty());
84 DCHECK(registration_type
== PER_USER
|| registration_type
== SYSTEM_LEVEL
);
85 DCHECK(registration_operation
== REGISTER
||
86 registration_operation
== UNREGISTER
);
88 int entrypoint_index
= 0;
89 base::LaunchOptions launch_options
;
90 base::ProcessHandle process_handle
= INVALID_HANDLE_VALUE
;
93 if (registration_type
== PER_USER
)
94 entrypoint_index
+= 2;
95 if (registration_operation
== UNREGISTER
)
96 entrypoint_index
+= 1;
97 string16
registration_command(ASCIIToUTF16("\""));
98 registration_command
+=
99 CommandLine::ForCurrentProcess()->GetProgram().value();
100 registration_command
+= ASCIIToUTF16("\" ");
101 registration_command
+= kCallRegistrationEntrypointSwitch
;
102 registration_command
+= ASCIIToUTF16(" \"");
103 registration_command
+= path
;
104 registration_command
+= ASCIIToUTF16("\" ");
105 registration_command
+= ASCIIToUTF16(kEntrypoints
[entrypoint_index
]);
106 launch_options
.wait
= true;
107 if (!base::LaunchProcess(registration_command
, launch_options
,
110 << "Failed to register or unregister DLL with command: "
111 << registration_command
;
113 base::win::ScopedHandle
rundll32(process_handle
);
114 if (!base::WaitForExitCodeWithTimeout(
115 process_handle
, &exit_code
,
116 base::TimeDelta::FromMilliseconds(kDllRegistrationTimeoutMs
))) {
117 LOG(ERROR
) << "Timeout waiting to register or unregister DLL with "
118 "command: " << registration_command
;
119 base::KillProcess(process_handle
, 0, false);
120 NOTREACHED() << "Aborting test due to registration failure.";
123 if (exit_code
!= 0) {
124 if (registration_operation
== REGISTER
) {
126 << "DLL registration failed (exit code: 0x" << std::hex
<< exit_code
127 << ", command: " << registration_command
128 << "). Make sure you are running as Admin.";
132 << "DLL unregistration failed (exit code: 0x" << std::hex
<< exit_code
133 << ", command: " << registration_command
<< ").";
139 void ScopedChromeFrameRegistrar::RegisterAtPath(
140 const std::wstring
& path
, RegistrationType registration_type
) {
141 DoRegistration(path
, registration_type
, REGISTER
);
145 void ScopedChromeFrameRegistrar::UnregisterAtPath(
146 const std::wstring
& path
, RegistrationType registration_type
) {
147 DoRegistration(path
, registration_type
, UNREGISTER
);
150 base::FilePath
ScopedChromeFrameRegistrar::GetReferenceChromeFrameDllPath() {
151 base::FilePath reference_build_dir
;
152 PathService::Get(chrome::DIR_APP
, &reference_build_dir
);
154 reference_build_dir
= reference_build_dir
.DirName();
155 reference_build_dir
= reference_build_dir
.DirName();
157 reference_build_dir
= reference_build_dir
.AppendASCII("chrome_frame");
158 reference_build_dir
= reference_build_dir
.AppendASCII("tools");
159 reference_build_dir
= reference_build_dir
.AppendASCII("test");
160 reference_build_dir
= reference_build_dir
.AppendASCII("reference_build");
161 reference_build_dir
= reference_build_dir
.AppendASCII("chrome");
162 reference_build_dir
= reference_build_dir
.AppendASCII("servers");
163 reference_build_dir
= reference_build_dir
.Append(kChromeFrameDllName
);
164 return reference_build_dir
;
168 void ScopedChromeFrameRegistrar::RegisterAndExitProcessIfDirected() {
169 // This method is invoked before any Chromium helpers have been initialized.
170 // Take pains to use only Win32 and CRT functions.
172 const wchar_t* const* argv
= ::CommandLineToArgvW(::GetCommandLine(), &argc
);
173 if (argc
< 2 || ::lstrcmp(argv
[1], kCallRegistrationEntrypointSwitch
) != 0)
176 printf("Usage: %S %S <path to dll> <entrypoint>\n", argv
[0],
177 kCallRegistrationEntrypointSwitch
);
181 // The only way to leave from here on down is ExitProcess.
182 const wchar_t* dll_path
= argv
[2];
183 const wchar_t* wide_entrypoint
= argv
[3];
184 char entrypoint
[256];
185 HRESULT exit_code
= 0;
186 int entrypoint_len
= lstrlen(wide_entrypoint
);
187 if (entrypoint_len
<= 0 || entrypoint_len
>= arraysize(entrypoint
)) {
188 exit_code
= HRESULT_FROM_WIN32(ERROR_PROC_NOT_FOUND
);
190 // Convert wide to narrow. Since the entrypoint must be a narrow string
191 // anyway, it is safe to truncate each character like this.
192 std::copy(wide_entrypoint
, wide_entrypoint
+ entrypoint_len
+ 1,
194 HMODULE dll_module
= ::LoadLibrary(dll_path
);
195 if (dll_module
== NULL
) {
196 exit_code
= HRESULT_FROM_WIN32(::GetLastError());
198 typedef HRESULT (STDAPICALLTYPE
*RegisterFp
)();
199 RegisterFp register_func
=
200 reinterpret_cast<RegisterFp
>(::GetProcAddress(dll_module
,
202 if (register_func
== NULL
) {
203 exit_code
= HRESULT_FROM_WIN32(::GetLastError());
205 exit_code
= register_func();
207 ::FreeLibrary(dll_module
);
211 ::ExitProcess(exit_code
);
216 ScopedChromeFrameRegistrar::ScopedChromeFrameRegistrar(
217 const std::wstring
& path
, RegistrationType registration_type
)
218 : registration_type_(registration_type
) {
219 if (!register_chrome_path_provider_
) {
220 // Register paths needed by the ScopedChromeFrameRegistrar.
221 chrome::RegisterPathProvider();
222 register_chrome_path_provider_
= true;
224 original_dll_path_
= path
;
225 RegisterChromeFrameAtPath(original_dll_path_
);
228 ScopedChromeFrameRegistrar::ScopedChromeFrameRegistrar(
229 RegistrationType registration_type
)
230 : registration_type_(registration_type
) {
231 if (!register_chrome_path_provider_
) {
232 // Register paths needed by the ScopedChromeFrameRegistrar.
233 chrome::RegisterPathProvider();
234 register_chrome_path_provider_
= true;
236 original_dll_path_
= GetChromeFrameBuildPath().value();
237 RegisterChromeFrameAtPath(original_dll_path_
);
240 ScopedChromeFrameRegistrar::~ScopedChromeFrameRegistrar() {
241 if (base::FilePath(original_dll_path_
) !=
242 base::FilePath(new_chrome_frame_dll_path_
)) {
243 RegisterChromeFrameAtPath(original_dll_path_
);
244 } else if (registration_type_
== PER_USER
) {
245 UnregisterAtPath(new_chrome_frame_dll_path_
, registration_type_
);
246 HWND chrome_frame_helper_window
=
247 FindWindow(L
"ChromeFrameHelperWindowClass", NULL
);
248 if (IsWindow(chrome_frame_helper_window
)) {
249 PostMessage(chrome_frame_helper_window
, WM_CLOSE
, 0, 0);
251 base::KillProcesses(L
"chrome_frame_helper.exe", 0, NULL
);
256 void ScopedChromeFrameRegistrar::RegisterChromeFrameAtPath(
257 const std::wstring
& path
) {
258 RegisterAtPath(path
, registration_type_
);
259 new_chrome_frame_dll_path_
= path
;
262 void ScopedChromeFrameRegistrar::RegisterReferenceChromeFrameBuild() {
263 RegisterChromeFrameAtPath(GetReferenceChromeFrameDllPath().value());
266 std::wstring
ScopedChromeFrameRegistrar::GetChromeFrameDllPath() const {
267 return new_chrome_frame_dll_path_
;
270 bool IsWorkstationLocked() {
271 bool is_locked
= true;
272 HDESK input_desk
= ::OpenInputDesktop(0, 0, GENERIC_READ
);
274 wchar_t name
[256] = {0};
276 if (::GetUserObjectInformation(input_desk
,
281 is_locked
= lstrcmpi(name
, L
"default") != 0;
283 ::CloseDesktop(input_desk
);