[email protected] | 3373ad3 | 2012-01-25 11:33:48 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | #ifndef CHROME_FRAME_TEST_UTILS_H_ |
| 6 | #define CHROME_FRAME_TEST_UTILS_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
[email protected] | a1800e8 | 2009-11-19 00:53:23 | [diff] [blame] | 10 | #include <atlbase.h> |
| 11 | #include <atlcom.h> |
| 12 | |
[email protected] | 3373ad3 | 2012-01-25 11:33:48 | [diff] [blame] | 13 | #include "base/string16.h" |
| 14 | |
[email protected] | 864b136 | 2010-08-19 03:49:38 | [diff] [blame] | 15 | class FilePath; |
[email protected] | 66ff735 | 2009-10-15 05:09:50 | [diff] [blame] | 16 | |
[email protected] | ea9ed97d | 2010-01-05 19:16:23 | [diff] [blame] | 17 | extern const wchar_t kChromeFrameDllName[]; |
[email protected] | 43c698f | 2010-05-01 01:38:59 | [diff] [blame] | 18 | extern const wchar_t kChromeLauncherExeName[]; |
[email protected] | ea9ed97d | 2010-01-05 19:16:23 | [diff] [blame] | 19 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 20 | // Helper class used to register different chrome frame DLLs while running |
[email protected] | 39b3349e | 2010-02-24 18:54:12 | [diff] [blame] | 21 | // tests. The default constructor registers the DLL found in the build path. |
[email protected] | 3373ad3 | 2012-01-25 11:33:48 | [diff] [blame] | 22 | // Programs that use this class MUST include a call to the class's |
| 23 | // RegisterAndExitProcessIfDirected method at the top of their main entrypoint. |
| 24 | // |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 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. |
| 27 | // |
| 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 { |
| 31 | public: |
[email protected] | cf5912b | 2011-02-01 22:20:44 | [diff] [blame] | 32 | enum RegistrationType { |
| 33 | PER_USER, |
| 34 | SYSTEM_LEVEL, |
| 35 | }; |
| 36 | |
| 37 | explicit ScopedChromeFrameRegistrar(RegistrationType registration_type); |
| 38 | ScopedChromeFrameRegistrar(const std::wstring& path, |
| 39 | RegistrationType registration_type); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 40 | virtual ~ScopedChromeFrameRegistrar(); |
| 41 | |
| 42 | void RegisterChromeFrameAtPath(const std::wstring& path); |
[email protected] | 43c698f | 2010-05-01 01:38:59 | [diff] [blame] | 43 | void UnegisterChromeFrameAtPath(const std::wstring& path); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 44 | void RegisterReferenceChromeFrameBuild(); |
| 45 | |
| 46 | std::wstring GetChromeFrameDllPath() const; |
| 47 | |
[email protected] | cf5912b | 2011-02-01 22:20:44 | [diff] [blame] | 48 | static void RegisterAtPath(const std::wstring& path, |
| 49 | RegistrationType registration_type); |
| 50 | static void UnregisterAtPath(const std::wstring& path, |
| 51 | RegistrationType registration_type); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 52 | static void RegisterDefaults(); |
[email protected] | bcd840c | 2010-05-27 10:43:00 | [diff] [blame] | 53 | static FilePath GetReferenceChromeFrameDllPath(); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 54 | |
[email protected] | 3373ad3 | 2012-01-25 11:33:48 | [diff] [blame] | 55 | // Registers or unregisters a COM DLL and exits the process if the process's |
| 56 | // command line is: |
| 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(); |
| 62 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 63 | private: |
[email protected] | 3373ad3 | 2012-01-25 11:33:48 | [diff] [blame] | 64 | enum RegistrationOperation { |
| 65 | REGISTER, |
| 66 | UNREGISTER, |
| 67 | }; |
| 68 | |
| 69 | // The string "--call-registration-entrypoint". |
| 70 | static const wchar_t kCallRegistrationEntrypointSwitch[]; |
| 71 | |
| 72 | static void DoRegistration(const string16& path, |
| 73 | RegistrationType registration_type, |
| 74 | RegistrationOperation registration_operation); |
| 75 | |
[email protected] | ea9ed97d | 2010-01-05 19:16:23 | [diff] [blame] | 76 | // Contains the path of the most recently registered Chrome Frame DLL. |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 77 | std::wstring new_chrome_frame_dll_path_; |
| 78 | |
[email protected] | ea9ed97d | 2010-01-05 19:16:23 | [diff] [blame] | 79 | // Contains the path of the Chrome Frame DLL to be registered at destruction. |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 80 | std::wstring original_dll_path_; |
[email protected] | cf5912b | 2011-02-01 22:20:44 | [diff] [blame] | 81 | |
| 82 | // Indicates whether per user or per machine registration is needed. |
| 83 | RegistrationType registration_type_; |
[email protected] | 0ba6f54e | 2011-03-08 23:07:08 | [diff] [blame] | 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_; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 87 | }; |
| 88 | |
[email protected] | 87ba2e60 | 2011-03-01 02:25:11 | [diff] [blame] | 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 |
| 91 | // folder structure. |
| 92 | FilePath GetChromeFrameBuildPath(); |
| 93 | |
[email protected] | a1800e8 | 2009-11-19 00:53:23 | [diff] [blame] | 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. |
| 97 | template <typename T> |
| 98 | class DispCallback |
| 99 | : public IDispEventSimpleImpl<1, DispCallback<T>, &IID_IDispatch> { |
| 100 | public: |
| 101 | typedef HRESULT (T::*Method)(const VARIANT* param); |
| 102 | |
| 103 | DispCallback(T* owner, Method method) : owner_(owner), method_(method) { |
| 104 | } |
| 105 | |
| 106 | BEGIN_SINK_MAP(DispCallback) |
| 107 | SINK_ENTRY_INFO(1, IID_IDispatch, DISPID_VALUE, OnCallback, &g_single_param) |
| 108 | END_SINK_MAP() |
| 109 | |
| 110 | virtual ULONG STDMETHODCALLTYPE AddRef() { |
| 111 | return owner_->AddRef(); |
| 112 | } |
| 113 | virtual ULONG STDMETHODCALLTYPE Release() { |
| 114 | return owner_->Release(); |
| 115 | } |
| 116 | |
| 117 | STDMETHOD(OnCallback)(VARIANT param) { |
| 118 | return (owner_->*method_)(¶m); |
| 119 | } |
| 120 | |
| 121 | IDispatch* ToDispatch() { |
| 122 | return reinterpret_cast<IDispatch*>(this); |
| 123 | } |
| 124 | |
| 125 | T* owner_; |
| 126 | Method method_; |
| 127 | }; |
| 128 | |
[email protected] | 0b5a2f8 | 2009-12-07 21:20:19 | [diff] [blame] | 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); |
| 134 | |
[email protected] | 58d58c7b | 2010-11-05 23:34:03 | [diff] [blame] | 135 | // If the workstation is locked and cannot receive user input. |
| 136 | bool IsWorkstationLocked(); |
[email protected] | a1800e8 | 2009-11-19 00:53:23 | [diff] [blame] | 137 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 138 | #endif // CHROME_FRAME_TEST_UTILS_H_ |