| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_FRAME_TEST_CHROME_FRAME_TEST_UTILS_H_ | 5 #ifndef CHROME_FRAME_TEST_CHROME_FRAME_TEST_UTILS_H_ |
| 6 #define CHROME_FRAME_TEST_CHROME_FRAME_TEST_UTILS_H_ | 6 #define CHROME_FRAME_TEST_CHROME_FRAME_TEST_UTILS_H_ |
| 7 | 7 |
| 8 #include <atlbase.h> |
| 9 #include <atlcom.h> |
| 10 #include <string> |
| 11 #include <exdisp.h> |
| 12 #include <exdispid.h> |
| 13 #include <mshtml.h> |
| 14 #include <shlguid.h> |
| 15 #include <shobjidl.h> |
| 8 #include <windows.h> | 16 #include <windows.h> |
| 9 | 17 |
| 10 #include "base/basictypes.h" | 18 #include "base/basictypes.h" |
| 19 #include "base/message_loop.h" |
| 11 #include "base/process_util.h" | 20 #include "base/process_util.h" |
| 21 #include "base/scoped_comptr_win.h" |
| 22 #include "base/scoped_variant_win.h" |
| 23 |
| 24 #include "chrome_frame/test_utils.h" |
| 25 // Include without path to make GYP build see it. |
| 26 #include "chrome_tab.h" // NOLINT |
| 12 | 27 |
| 13 namespace chrome_frame_test { | 28 namespace chrome_frame_test { |
| 14 | 29 |
| 15 bool IsTopLevelWindow(HWND window); | 30 bool IsTopLevelWindow(HWND window); |
| 16 int CloseVisibleWindowsOnAllThreads(HANDLE process); | 31 int CloseVisibleWindowsOnAllThreads(HANDLE process); |
| 17 bool ForceSetForegroundWindow(HWND window); | 32 bool ForceSetForegroundWindow(HWND window); |
| 18 bool EnsureProcessInForeground(base::ProcessId process_id); | 33 bool EnsureProcessInForeground(base::ProcessId process_id); |
| 19 | 34 |
| 20 // Iterates through all the characters in the string and simulates | 35 // Iterates through all the characters in the string and simulates |
| 21 // keyboard input. The input goes to the currently active application. | 36 // keyboard input. The input goes to the currently active application. |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 public: | 98 public: |
| 84 LowIntegrityToken(); | 99 LowIntegrityToken(); |
| 85 ~LowIntegrityToken(); | 100 ~LowIntegrityToken(); |
| 86 BOOL Impersonate(); | 101 BOOL Impersonate(); |
| 87 BOOL RevertToSelf(); | 102 BOOL RevertToSelf(); |
| 88 protected: | 103 protected: |
| 89 static bool IsImpersonated(); | 104 static bool IsImpersonated(); |
| 90 bool impersonated_; | 105 bool impersonated_; |
| 91 }; | 106 }; |
| 92 | 107 |
| 108 // MessageLoopForUI wrapper that runs only for a limited time. |
| 109 // We need a UI message loop in the main thread. |
| 110 class TimedMsgLoop { |
| 111 public: |
| 112 void RunFor(int seconds) { |
| 113 QuitAfter(seconds); |
| 114 loop_.MessageLoop::Run(); |
| 115 } |
| 116 |
| 117 void PostDelayedTask( |
| 118 const tracked_objects::Location& from_here, Task* task, int64 delay_ms) { |
| 119 loop_.PostDelayedTask(from_here, task, delay_ms); |
| 120 } |
| 121 |
| 122 void Quit() { |
| 123 loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask); |
| 124 } |
| 125 |
| 126 void QuitAfter(int seconds) { |
| 127 loop_.PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask, 1000 * seconds); |
| 128 } |
| 129 |
| 130 MessageLoopForUI loop_; |
| 131 }; |
| 132 |
| 133 // Launches IE as a COM server and returns the corresponding IWebBrowser2 |
| 134 // interface pointer. |
| 135 // Returns S_OK on success. |
| 136 HRESULT LaunchIEAsComServer(IWebBrowser2** web_browser); |
| 137 |
| 138 #ifndef DISPID_NEWPROCESS |
| 139 #define DISPID_NEWPROCESS 284 |
| 140 #endif // DISPID_NEWPROCESS |
| 141 |
| 142 // This class sets up event sinks to the IWebBrowser interface. Currently it |
| 143 // subscribes to the following events:- |
| 144 // 1. DISPID_BEFORENAVIGATE2 |
| 145 // 2. DISPID_NAVIGATEERROR |
| 146 // 3. DISPID_NAVIGATECOMPLETE2 |
| 147 // 4. DISPID_NEWWINDOW3 |
| 148 // 5. DISPID_DOCUMENTCOMPLETE |
| 149 // Other events can be subscribed to on an if needed basis. |
| 150 class WebBrowserEventSink |
| 151 : public CComObjectRootEx<CComMultiThreadModel>, |
| 152 public IDispEventSimpleImpl<0, WebBrowserEventSink, |
| 153 &DIID_DWebBrowserEvents2> { |
| 154 public: |
| 155 typedef IDispEventSimpleImpl<0, WebBrowserEventSink, |
| 156 &DIID_DWebBrowserEvents2> DispEventsImpl; |
| 157 WebBrowserEventSink() |
| 158 : ALLOW_THIS_IN_INITIALIZER_LIST( |
| 159 onmessage_(this, &WebBrowserEventSink::OnMessageInternal)), |
| 160 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 161 onloaderror_(this, &WebBrowserEventSink::OnLoadErrorInternal)), |
| 162 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 163 onload_(this, &WebBrowserEventSink::OnLoadInternal)) { |
| 164 } |
| 165 |
| 166 ~WebBrowserEventSink() { |
| 167 Uninitialize(); |
| 168 } |
| 169 |
| 170 void Uninitialize(); |
| 171 |
| 172 // Helper function to launch IE and navigate to a URL. |
| 173 // Returns S_OK on success, S_FALSE if the test was not run, other |
| 174 // errors on failure. |
| 175 HRESULT LaunchIEAndNavigate(const std::wstring& navigate_url); |
| 176 |
| 177 virtual HRESULT Navigate(const std::wstring& navigate_url); |
| 178 |
| 179 // Set input focus to chrome frame window. |
| 180 void SetFocusToChrome(); |
| 181 |
| 182 // Send keyboard input to the renderer window hosted in chrome using |
| 183 // SendInput API |
| 184 void SendInputToChrome(const std::string& input_string); |
| 185 |
| 186 BEGIN_COM_MAP(WebBrowserEventSink) |
| 187 END_COM_MAP() |
| 188 |
| 189 BEGIN_SINK_MAP(WebBrowserEventSink) |
| 190 SINK_ENTRY_INFO(0, DIID_DWebBrowserEvents2, DISPID_BEFORENAVIGATE2, |
| 191 OnBeforeNavigate2Internal, &kBeforeNavigate2Info) |
| 192 SINK_ENTRY_INFO(0, DIID_DWebBrowserEvents2, DISPID_DOWNLOADBEGIN, |
| 193 OnDownloadBegin, &kVoidMethodInfo) |
| 194 SINK_ENTRY_INFO(0, DIID_DWebBrowserEvents2, DISPID_NAVIGATECOMPLETE2, |
| 195 OnNavigateComplete2Internal, &kNavigateComplete2Info) |
| 196 SINK_ENTRY_INFO(0, DIID_DWebBrowserEvents2, DISPID_NAVIGATEERROR, |
| 197 OnNavigateError, &kNavigateErrorInfo) |
| 198 SINK_ENTRY_INFO(0, DIID_DWebBrowserEvents2, DISPID_NEWWINDOW3, |
| 199 OnNewWindow3, &kNewWindow3Info) |
| 200 SINK_ENTRY_INFO(0, DIID_DWebBrowserEvents2, DISPID_DOCUMENTCOMPLETE, |
| 201 OnDocumentCompleteInternal, &kDocumentCompleteInfo) |
| 202 END_SINK_MAP() |
| 203 |
| 204 STDMETHOD_(void, OnNavigateError)(IDispatch* dispatch, VARIANT* url, |
| 205 VARIANT* frame_name, VARIANT* status_code, |
| 206 VARIANT* cancel) { |
| 207 DLOG(INFO) << __FUNCTION__; |
| 208 } |
| 209 |
| 210 STDMETHOD(OnBeforeNavigate2)(IDispatch* dispatch, VARIANT* url, VARIANT* |
| 211 flags, VARIANT* target_frame_name, |
| 212 VARIANT* post_data, VARIANT* headers, |
| 213 VARIANT_BOOL* cancel) { |
| 214 return S_OK; |
| 215 } |
| 216 |
| 217 STDMETHOD(OnBeforeNavigate2Internal)(IDispatch* dispatch, VARIANT* url, |
| 218 VARIANT* flags, |
| 219 VARIANT* target_frame_name, |
| 220 VARIANT* post_data, VARIANT* headers, |
| 221 VARIANT_BOOL* cancel); |
| 222 STDMETHOD_(void, OnDownloadBegin)() {} |
| 223 STDMETHOD_(void, OnNavigateComplete2Internal)(IDispatch* dispatch, |
| 224 VARIANT* url); |
| 225 STDMETHOD_(void, OnNavigateComplete2)(IDispatch* dispatch, VARIANT* url) {} |
| 226 STDMETHOD_(void, OnNewWindow3)(IDispatch** dispatch, VARIANT_BOOL* Cancel, |
| 227 DWORD flags, BSTR url_context, BSTR url) {} |
| 228 |
| 229 STDMETHOD_(void, OnDocumentCompleteInternal)(IDispatch* dispatch, |
| 230 VARIANT* url); |
| 231 |
| 232 STDMETHOD_(void, OnDocumentComplete)(IDispatch* dispatch, |
| 233 VARIANT* url) {} |
| 234 #ifdef _DEBUG |
| 235 STDMETHOD(Invoke)(DISPID dispid, REFIID riid, |
| 236 LCID lcid, WORD flags, DISPPARAMS* params, VARIANT* result, |
| 237 EXCEPINFO* except_info, UINT* arg_error) { |
| 238 DLOG(INFO) << __FUNCTION__ << L" disp id :" << dispid; |
| 239 return DispEventsImpl::Invoke(dispid, riid, lcid, flags, params, result, |
| 240 except_info, arg_error); |
| 241 } |
| 242 #endif // _DEBUG |
| 243 |
| 244 // Chrome frame callbacks |
| 245 virtual void OnLoad(const wchar_t* url) {} |
| 246 virtual void OnLoadError(const wchar_t* url) {} |
| 247 virtual void OnMessage(const wchar_t* message) {} |
| 248 |
| 249 IWebBrowser2* web_browser2() { |
| 250 return web_browser2_.get(); |
| 251 } |
| 252 |
| 253 HRESULT SetWebBrowser(IWebBrowser2* web_browser2); |
| 254 |
| 255 protected: |
| 256 // IChromeFrame callbacks |
| 257 HRESULT OnLoadInternal(const VARIANT* param); |
| 258 HRESULT OnLoadErrorInternal(const VARIANT* param); |
| 259 HRESULT OnMessageInternal(const VARIANT* param); |
| 260 |
| 261 void ConnectToChromeFrame(); |
| 262 void DisconnectFromChromeFrame(); |
| 263 HWND GetAttachedChromeRendererWindow(); |
| 264 |
| 265 public: |
| 266 ScopedComPtr<IWebBrowser2> web_browser2_; |
| 267 ScopedComPtr<IChromeFrame> chrome_frame_; |
| 268 DispCallback<WebBrowserEventSink> onmessage_; |
| 269 DispCallback<WebBrowserEventSink> onloaderror_; |
| 270 DispCallback<WebBrowserEventSink> onload_; |
| 271 |
| 272 protected: |
| 273 static _ATL_FUNC_INFO kBeforeNavigate2Info; |
| 274 static _ATL_FUNC_INFO kNavigateComplete2Info; |
| 275 static _ATL_FUNC_INFO kNavigateErrorInfo; |
| 276 static _ATL_FUNC_INFO kNewWindow3Info; |
| 277 static _ATL_FUNC_INFO kVoidMethodInfo; |
| 278 static _ATL_FUNC_INFO kDocumentCompleteInfo; |
| 279 }; |
| 280 |
| 93 } // namespace chrome_frame_test | 281 } // namespace chrome_frame_test |
| 94 | 282 |
| 95 #endif // CHROME_FRAME_TEST_CHROME_FRAME_TEST_UTILS_H_ | 283 #endif // CHROME_FRAME_TEST_CHROME_FRAME_TEST_UTILS_H_ |
| OLD | NEW |