blob: f07bfbed9b1d94da2f43ed15d7eada38b8f2615e [file] [log] [blame]
[email protected]3373ad32012-01-25 11:33:481// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]f7817822009-09-24 05:11:582// 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]a1800e82009-11-19 00:53:2310#include <atlbase.h>
11#include <atlcom.h>
12
[email protected]3373ad32012-01-25 11:33:4813#include "base/string16.h"
14
[email protected]864b1362010-08-19 03:49:3815class FilePath;
[email protected]66ff7352009-10-15 05:09:5016
[email protected]ea9ed97d2010-01-05 19:16:2317extern const wchar_t kChromeFrameDllName[];
[email protected]43c698f2010-05-01 01:38:5918extern const wchar_t kChromeLauncherExeName[];
[email protected]ea9ed97d2010-01-05 19:16:2319
[email protected]f7817822009-09-24 05:11:5820// Helper class used to register different chrome frame DLLs while running
[email protected]39b3349e2010-02-24 18:54:1221// tests. The default constructor registers the DLL found in the build path.
[email protected]3373ad32012-01-25 11:33:4822// 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]f7817822009-09-24 05:11:5825// 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.
30class ScopedChromeFrameRegistrar {
31 public:
[email protected]cf5912b2011-02-01 22:20:4432 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]f7817822009-09-24 05:11:5840 virtual ~ScopedChromeFrameRegistrar();
41
42 void RegisterChromeFrameAtPath(const std::wstring& path);
[email protected]43c698f2010-05-01 01:38:5943 void UnegisterChromeFrameAtPath(const std::wstring& path);
[email protected]f7817822009-09-24 05:11:5844 void RegisterReferenceChromeFrameBuild();
45
46 std::wstring GetChromeFrameDllPath() const;
47
[email protected]cf5912b2011-02-01 22:20:4448 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]f7817822009-09-24 05:11:5852 static void RegisterDefaults();
[email protected]bcd840c2010-05-27 10:43:0053 static FilePath GetReferenceChromeFrameDllPath();
[email protected]f7817822009-09-24 05:11:5854
[email protected]3373ad32012-01-25 11:33:4855 // 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]f7817822009-09-24 05:11:5863 private:
[email protected]3373ad32012-01-25 11:33:4864 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]ea9ed97d2010-01-05 19:16:2376 // Contains the path of the most recently registered Chrome Frame DLL.
[email protected]f7817822009-09-24 05:11:5877 std::wstring new_chrome_frame_dll_path_;
78
[email protected]ea9ed97d2010-01-05 19:16:2379 // Contains the path of the Chrome Frame DLL to be registered at destruction.
[email protected]f7817822009-09-24 05:11:5880 std::wstring original_dll_path_;
[email protected]cf5912b2011-02-01 22:20:4481
82 // Indicates whether per user or per machine registration is needed.
83 RegistrationType registration_type_;
[email protected]0ba6f54e2011-03-08 23:07:0884 // 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]f7817822009-09-24 05:11:5887};
88
[email protected]87ba2e602011-03-01 02:25:1189// 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.
92FilePath GetChromeFrameBuildPath();
93
[email protected]a1800e82009-11-19 00:53:2394// Callback description for onload, onloaderror, onmessage
95static _ATL_FUNC_INFO g_single_param = {CC_STDCALL, VT_EMPTY, 1, {VT_VARIANT}};
96// Simple class that forwards the callbacks.
97template <typename T>
98class 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_)(&param);
119 }
120
121 IDispatch* ToDispatch() {
122 return reinterpret_cast<IDispatch*>(this);
123 }
124
125 T* owner_;
126 Method method_;
127};
128
[email protected]0b5a2f82009-12-07 21:20:19129// 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.
132bool KillAllNamedProcessesWithArgument(const std::wstring& process_name,
133 const std::wstring& argument);
134
[email protected]58d58c7b2010-11-05 23:34:03135// If the workstation is locked and cannot receive user input.
136bool IsWorkstationLocked();
[email protected]a1800e82009-11-19 00:53:23137
[email protected]f7817822009-09-24 05:11:58138#endif // CHROME_FRAME_TEST_UTILS_H_