Repository navigation
Expand file tree
/
Copy pathDocumentHost.cpp
More file actions
387 lines (334 loc) · 13 KB
/
Copy pathDocumentHost.cpp
File metadata and controls
387 lines (334 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "DocumentHost.h"
#include "errorcodes.h"
#include "logging.h"
#include "BrowserCookie.h"
#include "BrowserFactory.h"
#include "CookieManager.h"
#include "messages.h"
#include "RegistryUtilities.h"
#include "Script.h"
#include "StringUtilities.h"
namespace webdriver {
DocumentHost::DocumentHost(HWND hwnd, HWND executor_handle) {
LOG(TRACE) << "Entering DocumentHost::DocumentHost";
// NOTE: COM should be initialized on this thread, so we
// could use CoCreateGuid() and StringFromGUID2() instead.
UUID guid;
RPC_WSTR guid_string = NULL;
RPC_STATUS status = ::UuidCreate(&guid);
if (status != RPC_S_OK) {
// If we encounter an error, not bloody much we can do about it.
// Just log it and continue.
LOG(WARN) << "UuidCreate returned a status other then RPC_S_OK: " << status;
}
status = ::UuidToString(&guid, &guid_string);
if (status != RPC_S_OK) {
// If we encounter an error, not bloody much we can do about it.
// Just log it and continue.
LOG(WARN) << "UuidToString returned a status other then RPC_S_OK: " << status;
}
// RPC_WSTR is currently typedef'd in RpcDce.h (pulled in by rpc.h)
// as unsigned short*. It needs to be typedef'd as wchar_t*
wchar_t* cast_guid_string = reinterpret_cast<wchar_t*>(guid_string);
this->browser_id_ = StringUtilities::ToString(cast_guid_string);
::RpcStringFree(&guid_string);
this->window_handle_ = hwnd;
this->executor_handle_ = executor_handle;
this->script_executor_handle_ = NULL;
this->is_closing_ = false;
this->wait_required_ = false;
this->focused_frame_window_ = NULL;
this->cookie_manager_ = new CookieManager();
if (this->window_handle_ != NULL) {
this->cookie_manager_->Initialize(this->window_handle_);
}
}
DocumentHost::~DocumentHost(void) {
delete this->cookie_manager_;
}
std::string DocumentHost::GetCurrentUrl() {
LOG(TRACE) << "Entering DocumentHost::GetCurrentUrl";
CComPtr<IHTMLDocument2> doc;
this->GetDocument(&doc);
if (!doc) {
LOG(WARN) << "Unable to get document object, DocumentHost::GetDocument returned NULL. "
<< "Attempting to get URL from IWebBrowser2 object";
return this->GetBrowserUrl();
}
CComBSTR url;
HRESULT hr = doc->get_URL(&url);
if (FAILED(hr)) {
LOGHR(WARN, hr) << "Unable to get current URL, call to IHTMLDocument2::get_URL failed";
return "";
}
std::wstring converted_url(url, ::SysStringLen(url));
std::string current_url = StringUtilities::ToString(converted_url);
return current_url;
}
std::string DocumentHost::GetPageSource() {
LOG(TRACE) << "Entering DocumentHost::GetPageSource";
CComPtr<IHTMLDocument2> doc;
this->GetDocument(&doc);
if (!doc) {
LOG(WARN) << "Unable to get document object, DocumentHost::GetDocument did not return a valid IHTMLDocument2 pointer";
return "";
}
CComPtr<IHTMLDocument3> doc3;
HRESULT hr = doc->QueryInterface<IHTMLDocument3>(&doc3);
if (FAILED(hr) || !doc3) {
LOG(WARN) << "Unable to get document object, QueryInterface to IHTMLDocument3 failed";
return "";
}
CComPtr<IHTMLElement> document_element;
hr = doc3->get_documentElement(&document_element);
if (FAILED(hr) || !document_element) {
LOGHR(WARN, hr) << "Unable to get document element from page, call to IHTMLDocument3::get_documentElement failed";
return "";
}
CComBSTR html;
hr = document_element->get_outerHTML(&html);
if (FAILED(hr)) {
LOGHR(WARN, hr) << "Have document element but cannot read source, call to IHTMLElement::get_outerHTML failed";
return "";
}
std::wstring converted_html = html;
std::string page_source = StringUtilities::ToString(converted_html);
return page_source;
}
void DocumentHost::Restore(void) {
if (this->IsFullScreen()) {
this->SetFullScreen(false);
}
HWND window_handle = this->GetTopLevelWindowHandle();
if (::IsZoomed(window_handle) || ::IsIconic(window_handle)) {
::ShowWindow(window_handle, SW_RESTORE);
}
}
int DocumentHost::SetFocusedFrameByElement(IHTMLElement* frame_element) {
LOG(TRACE) << "Entering DocumentHost::SetFocusedFrameByElement";
HRESULT hr = S_OK;
if (!frame_element) {
this->focused_frame_window_ = NULL;
return WD_SUCCESS;
}
CComPtr<IHTMLWindow2> interim_result;
CComPtr<IHTMLObjectElement4> object_element;
hr = frame_element->QueryInterface<IHTMLObjectElement4>(&object_element);
if (SUCCEEDED(hr) && object_element) {
CComPtr<IDispatch> object_disp;
object_element->get_contentDocument(&object_disp);
if (!object_disp) {
LOG(WARN) << "Cannot get IDispatch interface from IHTMLObjectElement4 element";
return ENOSUCHFRAME;
}
CComPtr<IHTMLDocument2> object_doc;
object_disp->QueryInterface<IHTMLDocument2>(&object_doc);
if (!object_doc) {
LOG(WARN) << "Cannot get IHTMLDocument2 document from IDispatch reference";
return ENOSUCHFRAME;
}
hr = object_doc->get_parentWindow(&interim_result);
if (FAILED(hr)) {
LOGHR(WARN, hr) << "Cannot get parentWindow from IHTMLDocument2, call to IHTMLDocument2::get_parentWindow failed";
return ENOSUCHFRAME;
}
} else {
CComPtr<IHTMLFrameBase2> frame_base;
frame_element->QueryInterface<IHTMLFrameBase2>(&frame_base);
if (!frame_base) {
LOG(WARN) << "IHTMLElement is not a FRAME or IFRAME element";
return ENOSUCHFRAME;
}
hr = frame_base->get_contentWindow(&interim_result);
if (FAILED(hr)) {
LOGHR(WARN, hr) << "Cannot get contentWindow from IHTMLFrameBase2, call to IHTMLFrameBase2::get_contentWindow failed";
return ENOSUCHFRAME;
}
}
this->focused_frame_window_ = interim_result;
return WD_SUCCESS;
}
int DocumentHost::SetFocusedFrameByName(const std::string& frame_name) {
LOG(TRACE) << "Entering DocumentHost::SetFocusedFrameByName";
CComVariant frame_identifier = StringUtilities::ToWString(frame_name).c_str();
return this->SetFocusedFrameByIdentifier(frame_identifier);
}
int DocumentHost::SetFocusedFrameByIndex(const int frame_index) {
LOG(TRACE) << "Entering DocumentHost::SetFocusedFrameByIndex";
CComVariant frame_identifier;
frame_identifier.vt = VT_I4;
frame_identifier.lVal = frame_index;
return this->SetFocusedFrameByIdentifier(frame_identifier);
}
void DocumentHost::SetFocusedFrameToParent() {
LOG(TRACE) << "Entering DocumentHost::SetFocusedFrameToParent";
// Three possible outcomes.
// Outcome 1: Already at top-level browsing context. No-op.
if (this->focused_frame_window_ != NULL) {
CComPtr<IHTMLWindow2> parent_window;
HRESULT hr = this->focused_frame_window_->get_parent(&parent_window);
if (FAILED(hr)) {
LOGHR(WARN, hr) << "IHTMLWindow2::get_parent call failed.";
}
CComPtr<IHTMLWindow2> top_window;
hr = this->focused_frame_window_->get_top(&top_window);
if (FAILED(hr)) {
LOGHR(WARN, hr) << "IHTMLWindow2::get_top call failed.";
}
if (top_window.IsEqualObject(parent_window)) {
// Outcome 2: Focus is on a frame one level deep, making the
// parent the top-level browsing context. Set focused frame
// pointer to NULL.
this->focused_frame_window_ = NULL;
} else {
// Outcome 3: Focus is on a frame more than one level deep.
// Set focused frame pointer to parent frame.
this->focused_frame_window_ = parent_window;
}
}
}
int DocumentHost::SetFocusedFrameByIdentifier(VARIANT frame_identifier) {
LOG(TRACE) << "Entering DocumentHost::SetFocusedFrameByIdentifier";
CComPtr<IHTMLDocument2> doc;
this->GetDocument(&doc);
CComPtr<IHTMLFramesCollection2> frames;
HRESULT hr = doc->get_frames(&frames);
if (!frames) {
LOG(WARN) << "No frames in document are set, IHTMLDocument2::get_frames returned NULL";
return ENOSUCHFRAME;
}
long length = 0;
frames->get_length(&length);
if (!length) {
LOG(WARN) << "No frames in document are found IHTMLFramesCollection2::get_length returned 0";
return ENOSUCHFRAME;
}
// Find the frame
CComVariant frame_holder;
hr = frames->item(&frame_identifier, &frame_holder);
if (FAILED(hr)) {
LOGHR(WARN, hr) << "Error retrieving frame holder, call to IHTMLFramesCollection2::item failed";
return ENOSUCHFRAME;
}
CComPtr<IHTMLWindow2> interim_result;
frame_holder.pdispVal->QueryInterface<IHTMLWindow2>(&interim_result);
if (!interim_result) {
LOG(WARN) << "Error retrieving frame, IDispatch cannot be cast to IHTMLWindow2";
return ENOSUCHFRAME;
}
this->focused_frame_window_ = interim_result;
return WD_SUCCESS;
}
void DocumentHost::PostQuitMessage() {
LOG(TRACE) << "Entering DocumentHost::PostQuitMessage";
LPSTR message_payload = new CHAR[this->browser_id_.size() + 1];
strcpy_s(message_payload, this->browser_id_.size() + 1, this->browser_id_.c_str());
::PostMessage(this->executor_handle(),
WD_BROWSER_QUIT,
NULL,
reinterpret_cast<LPARAM>(message_payload));
}
HWND DocumentHost::FindContentWindowHandle(HWND top_level_window_handle) {
LOG(TRACE) << "Entering DocumentHost::FindContentWindowHandle";
ProcessWindowInfo process_window_info;
process_window_info.pBrowser = NULL;
process_window_info.hwndBrowser = NULL;
DWORD process_id;
::GetWindowThreadProcessId(top_level_window_handle, &process_id);
process_window_info.dwProcessId = process_id;
::EnumChildWindows(top_level_window_handle,
&BrowserFactory::FindChildWindowForProcess,
reinterpret_cast<LPARAM>(&process_window_info));
return process_window_info.hwndBrowser;
}
int DocumentHost::GetDocumentMode(IHTMLDocument2* doc) {
LOG(TRACE) << "Entering DocumentHost::GetDocumentMode";
CComPtr<IHTMLDocument6> mode_doc;
doc->QueryInterface<IHTMLDocument6>(&mode_doc);
if (!mode_doc) {
LOG(DEBUG) << "QueryInterface for IHTMLDocument6 fails, so document mode must be 7 or less.";
return 5;
}
CComVariant mode;
HRESULT hr = mode_doc->get_documentMode(&mode);
if (FAILED(hr)) {
LOGHR(WARN, hr) << "get_documentMode failed.";
return 5;
}
int document_mode = static_cast<int>(mode.fltVal);
return document_mode;
}
bool DocumentHost::IsStandardsMode(IHTMLDocument2* doc) {
LOG(TRACE) << "Entering DocumentHost::IsStandardsMode";
CComPtr<IHTMLDocument5> compatibility_mode_doc;
doc->QueryInterface<IHTMLDocument5>(&compatibility_mode_doc);
if (!compatibility_mode_doc) {
LOG(WARN) << "Unable to cast document to IHTMLDocument5. IE6 or greater is required.";
return false;
}
CComBSTR compatibility_mode;
HRESULT hr = compatibility_mode_doc->get_compatMode(&compatibility_mode);
if (FAILED(hr)) {
LOGHR(WARN, hr) << "Failed calling get_compatMode.";
return false;
}
// Compatibility mode should be "BackCompat" for quirks mode, and
// "CSS1Compat" for standards mode. Check for "BackCompat" because
// that's less likely to change.
return compatibility_mode != L"BackCompat";
}
bool DocumentHost::GetDocumentDimensions(IHTMLDocument2* doc, LocationInfo* info) {
LOG(TRACE) << "Entering DocumentHost::GetDocumentDimensions";
CComVariant document_height;
CComVariant document_width;
// In non-standards-compliant mode, the BODY element represents the canvas.
// In standards-compliant mode, the HTML element represents the canvas.
CComPtr<IHTMLElement> canvas_element;
if (!IsStandardsMode(doc)) {
doc->get_body(&canvas_element);
if (!canvas_element) {
LOG(WARN) << "Unable to get canvas element from document in compatibility mode";
return false;
}
} else {
CComPtr<IHTMLDocument3> document_element_doc;
doc->QueryInterface<IHTMLDocument3>(&document_element_doc);
if (!document_element_doc) {
LOG(WARN) << "Unable to get IHTMLDocument3 handle from document.";
return false;
}
// The root node should be the HTML element.
document_element_doc->get_documentElement(&canvas_element);
if (!canvas_element) {
LOG(WARN) << "Could not retrieve document element.";
return false;
}
CComPtr<IHTMLHtmlElement> html_element;
canvas_element->QueryInterface<IHTMLHtmlElement>(&html_element);
if (!html_element) {
LOG(WARN) << "Document element is not the HTML element.";
return false;
}
}
canvas_element->getAttribute(CComBSTR("scrollHeight"), 0, &document_height);
canvas_element->getAttribute(CComBSTR("scrollWidth"), 0, &document_width);
info->height = document_height.lVal;
info->width = document_width.lVal;
return true;
}
} // namespace webdriver