Path: blob/trunk/cpp/iedriver/AsyncScriptExecutor.h
2867 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the "License");5// you may not use this file except in compliance with the License.6// You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing, software11// distributed under the License is distributed on an "AS IS" BASIS,12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13// See the License for the specific language governing permissions and14// limitations under the License.1516#ifndef WEBDRIVER_IE_ASYNCSCRIPTEXECUTOR_H_17#define WEBDRIVER_IE_ASYNCSCRIPTEXECUTOR_H_1819#include <vector>2021#include "IElementManager.h"22#include "messages.h"23#include "json.h"2425namespace webdriver {2627// Structure to be used for comunication between threads28struct AsyncScriptExecutorThreadContext {29HWND hwnd;30LPCTSTR script_source;31int script_argument_count;32HWND main_element_repository_handle;33LPCTSTR serialized_script_args;34};3536class ElementRepository;3738// We use a CWindowImpl (creating a hidden window) here because we39// want to synchronize access to the command handler. For that we40// use SendMessage() most of the time, and SendMessage() requires41// a window handle.42class AsyncScriptExecutor : public CWindowImpl<AsyncScriptExecutor>, public IElementManager {43public:44DECLARE_WND_CLASS(L"WebDriverAsyncAtomWndClass")4546BEGIN_MSG_MAP(Session)47MESSAGE_HANDLER(WM_CREATE, OnCreate)48MESSAGE_HANDLER(WM_CLOSE, OnClose)49MESSAGE_HANDLER(WM_DESTROY, OnDestroy)50MESSAGE_HANDLER(WD_INIT, OnInit)51MESSAGE_HANDLER(WD_ASYNC_SCRIPT_SET_DOCUMENT, OnSetDocument)52MESSAGE_HANDLER(WD_ASYNC_SCRIPT_SET_ELEMENT_ARGUMENT, OnSetElementArgument)53MESSAGE_HANDLER(WD_ASYNC_SCRIPT_IS_EXECUTION_READY, OnIsExecutionReady)54MESSAGE_HANDLER(WD_ASYNC_SCRIPT_EXECUTE, OnExecuteScript)55MESSAGE_HANDLER(WD_ASYNC_SCRIPT_IS_EXECUTION_COMPLETE, OnIsExecutionComplete)56MESSAGE_HANDLER(WD_ASYNC_SCRIPT_DETACH_LISTENTER, OnDetachListener)57MESSAGE_HANDLER(WD_ASYNC_SCRIPT_GET_RESULT, OnGetResult)58MESSAGE_HANDLER(WD_ASYNC_SCRIPT_NOTIFY_ELEMENT_TRANSFERRED, OnNotifyElementTransferred)59MESSAGE_HANDLER(WD_ASYNC_SCRIPT_SET_POLLING_SCRIPT, OnSetPollingScript)60MESSAGE_HANDLER(WD_ASYNC_SCRIPT_GET_REQUIRED_ELEMENT_LIST, OnGetRequiredElementList)61END_MSG_MAP()6263LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);64LRESULT OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);65LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);66LRESULT OnInit(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);67LRESULT OnSetDocument(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);68LRESULT OnSetElementArgument(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);69LRESULT OnIsExecutionReady(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);70LRESULT OnExecuteScript(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);71LRESULT OnIsExecutionComplete(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);72LRESULT OnDetachListener(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);73LRESULT OnGetResult(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);74LRESULT OnNotifyElementTransferred(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);75LRESULT OnSetPollingScript(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);76LRESULT OnGetRequiredElementList(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);7778int GetManagedElement(const std::string& element_id,79ElementHandle* element_wrapper) const;80bool AddManagedElement(IHTMLElement* element,81ElementHandle* element_wrapper);82void RemoveManagedElement(const std::string& element_id);8384static unsigned int WINAPI ThreadProc(LPVOID lpParameter);8586private:87void GetElementIdList(const Json::Value& json_object);88bool WaitForPollingScript(void);89void TransferReturnedElements(void);90void ReplaceTransferredElementResult(std::string original_element_id,91std::string element_id,92Json::Value* result);9394ElementRepository* element_repository_;95HWND main_element_repository_handle_;96CComPtr<IHTMLDocument2> script_host_;97std::vector<CComVariant> script_arguments_;98std::vector<std::string> element_id_list_;99std::wstring script_source_code_;100std::wstring polling_script_source_code_;101Json::Value script_args_;102Json::Value script_result_;103int script_argument_count_;104int script_argument_index_;105int status_code_;106bool is_execution_completed_;107bool is_listener_attached_;108};109110} // namespace webdriver111112#endif // WEBDRIVER_IE_ASYNCSCRIPTEXECUTOR_H_113114115116