Skip to content

Commit

Permalink
Cross-thread enable IE add and retrieve managed element methods
Browse files Browse the repository at this point in the history
Prepare for the retrieval and addition of elements to the known element
repository in a cross-thread manner. This involves marshaling the COM
interfaces for the elements using CoMarshalInterThreadInterfaceInStream
and CoGetInterfaceAndReleaseStream (yes, these are the names of real
Windows API calls).
  • Loading branch information
jimevans committed Feb 9, 2018
1 parent 1254ba2 commit f462f9d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
10 changes: 8 additions & 2 deletions cpp/iedriver/IECommandExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,9 @@ LRESULT IECommandExecutor::OnGetManagedElement(UINT uMsg,
ElementHandle element_handle;
int status_code = this->GetManagedElement(info->element_id, &element_handle);
if (status_code == WD_SUCCESS) {
info->element = element_handle->element();
::CoMarshalInterThreadInterfaceInStream(IID_IHTMLElement,
element_handle->element(),
&info->element_stream);
}
return status_code;
}
Expand All @@ -460,7 +462,11 @@ LRESULT IECommandExecutor::OnAddManagedElement(UINT uMsg,
BOOL& bHandled) {
ElementInfo* info = reinterpret_cast<ElementInfo*>(lParam);
ElementHandle element_handle;
this->AddManagedElement(info->element, &element_handle);
CComPtr<IHTMLElement> element;
::CoGetInterfaceAndReleaseStream(info->element_stream,
IID_IHTMLElement,
reinterpret_cast<void**>(&element));
this->AddManagedElement(element, &element_handle);
info->element_id = element_handle->element_id();
return WD_SUCCESS;
}
Expand Down
2 changes: 1 addition & 1 deletion cpp/iedriver/IECommandExecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace webdriver {

struct ElementInfo {
std::string element_id;
IHTMLElement* element;
LPSTREAM element_stream;
};

// Forward declaration of classes.
Expand Down
8 changes: 6 additions & 2 deletions cpp/iedriver/Script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,11 @@ int Script::AddArgument(HWND element_repository_handle, const Json::Value& arg)
reinterpret_cast<LPARAM>(&info)));

if (status_code == WD_SUCCESS) {
ElementHandle wrapped_element(new Element(info.element, info.element_id));
CComPtr<IHTMLElement> element;
::CoGetInterfaceAndReleaseStream(info.element_stream,
IID_IHTMLElement,
reinterpret_cast<void**>(&element));
ElementHandle wrapped_element(new Element(element, info.element_id));
bool is_element_valid = wrapped_element->IsAttachedToDom();
if (is_element_valid) {
is_element_valid = wrapped_element->IsDocumentFocused(this->script_engine_host_);
Expand All @@ -692,7 +696,7 @@ int Script::AddArgument(HWND element_repository_handle, const Json::Value& arg)
}

if (is_element_valid) {
this->AddArgument(info.element);
this->AddArgument(element);
} else {
status_code = EOBSOLETEELEMENT;
}
Expand Down
9 changes: 7 additions & 2 deletions cpp/iedriver/VariantUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,13 @@ int VariantUtilities::ConvertVariantToJsonValue(HWND element_repository_handle,
ElementInfo info;
CComPtr<IHTMLElement> node;
variant_value.pdispVal->QueryInterface<IHTMLElement>(&node);
info.element = node;
status_code = static_cast<int>(::SendMessage(element_repository_handle, WD_ADD_MANAGED_ELEMENT, NULL, reinterpret_cast<LPARAM>(&info)));
::CoMarshalInterThreadInterfaceInStream(IID_IHTMLElement,
node,
&info.element_stream);
status_code = static_cast<int>(::SendMessage(element_repository_handle,
WD_ADD_MANAGED_ELEMENT,
NULL,
reinterpret_cast<LPARAM>(&info)));
Json::Value element_value(Json::objectValue);
element_value["element-6066-11e4-a52e-4f735466cecf"] = info.element_id;
*value = element_value;
Expand Down

0 comments on commit f462f9d

Please sign in to comment.