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 #ifndef CHROME_FRAME_TEST_UTILS_H_
6 #define CHROME_FRAME_TEST_UTILS_H_
13 #include "base/string16.h"
17 extern const wchar_t kChromeFrameDllName
[];
18 extern const wchar_t kChromeLauncherExeName
[];
20 // Helper class used to register different chrome frame DLLs while running
21 // tests. The default constructor registers the DLL found in the build path.
22 // Programs that use this class MUST include a call to the class's
23 // RegisterAndExitProcessIfDirected method at the top of their main entrypoint.
25 // At destruction, again registers the DLL found in the build path if another
26 // DLL has since been registered. Triggers GTEST asserts on failure.
28 // TODO(robertshield): Ideally, make this class restore the originally
29 // registered chrome frame DLL (e.g. by looking in HKCR) on destruction.
30 class ScopedChromeFrameRegistrar
{
32 enum RegistrationType
{
37 explicit ScopedChromeFrameRegistrar(RegistrationType registration_type
);
38 ScopedChromeFrameRegistrar(const std::wstring
& path
,
39 RegistrationType registration_type
);
40 virtual ~ScopedChromeFrameRegistrar();
42 void RegisterChromeFrameAtPath(const std::wstring
& path
);
43 void UnegisterChromeFrameAtPath(const std::wstring
& path
);
44 void RegisterReferenceChromeFrameBuild();
46 std::wstring
GetChromeFrameDllPath() const;
48 static void RegisterAtPath(const std::wstring
& path
,
49 RegistrationType registration_type
);
50 static void UnregisterAtPath(const std::wstring
& path
,
51 RegistrationType registration_type
);
52 static void RegisterDefaults();
53 static FilePath
GetReferenceChromeFrameDllPath();
55 // Registers or unregisters a COM DLL and exits the process if the process's
57 // this.exe --call-registration-entrypoint path_to_dll entrypoint
58 // Otherwise simply returns. This method should be invoked at the start of the
59 // entrypoint in each executable that uses ScopedChromeFrameRegistrar to
60 // register or unregister DLLs.
61 static void RegisterAndExitProcessIfDirected();
64 enum RegistrationOperation
{
69 // The string "--call-registration-entrypoint".
70 static const wchar_t kCallRegistrationEntrypointSwitch
[];
72 static void DoRegistration(const string16
& path
,
73 RegistrationType registration_type
,
74 RegistrationOperation registration_operation
);
76 // Contains the path of the most recently registered Chrome Frame DLL.
77 std::wstring new_chrome_frame_dll_path_
;
79 // Contains the path of the Chrome Frame DLL to be registered at destruction.
80 std::wstring original_dll_path_
;
82 // Indicates whether per user or per machine registration is needed.
83 RegistrationType registration_type_
;
84 // We need to register the chrome path provider only once per process. This
85 // flag keeps track of that.
86 static bool register_chrome_path_provider_
;
89 // Returns the path to the Chrome Frame DLL in the build directory. Assumes
90 // that the test executable is running from the build folder or a similar
92 FilePath
GetChromeFrameBuildPath();
94 // Callback description for onload, onloaderror, onmessage
95 static _ATL_FUNC_INFO g_single_param
= {CC_STDCALL
, VT_EMPTY
, 1, {VT_VARIANT
}};
96 // Simple class that forwards the callbacks.
99 : public IDispEventSimpleImpl
<1, DispCallback
<T
>, &IID_IDispatch
> {
101 typedef HRESULT (T::*Method
)(const VARIANT
* param
);
103 DispCallback(T
* owner
, Method method
) : owner_(owner
), method_(method
) {
106 BEGIN_SINK_MAP(DispCallback
)
107 SINK_ENTRY_INFO(1, IID_IDispatch
, DISPID_VALUE
, OnCallback
, &g_single_param
)
110 virtual ULONG STDMETHODCALLTYPE
AddRef() {
111 return owner_
->AddRef();
113 virtual ULONG STDMETHODCALLTYPE
Release() {
114 return owner_
->Release();
117 STDMETHOD(OnCallback
)(VARIANT param
) {
118 return (owner_
->*method_
)(¶m
);
121 IDispatch
* ToDispatch() {
122 return reinterpret_cast<IDispatch
*>(this);
129 // Kills all running processes named |process_name| that have the string
130 // |argument| on their command line. Useful for killing all Chrome Frame
131 // instances of Chrome that all have --chrome-frame in their command line.
132 bool KillAllNamedProcessesWithArgument(const std::wstring
& process_name
,
133 const std::wstring
& argument
);
135 // If the workstation is locked and cannot receive user input.
136 bool IsWorkstationLocked();
138 #endif // CHROME_FRAME_TEST_UTILS_H_