From 51a60b5e813ee8517d443b8c32db95cfea67863f Mon Sep 17 00:00:00 2001 From: Jim Evans Date: Wed, 20 Mar 2013 10:59:48 -0400 Subject: [PATCH] Internal code cleanup of IEDriverServer.exe This commit removes the ATL dependency from IEDriverServer.exe. It does not remove the dependency on ATL from the internal DLL used by the executable, only the executable shell itself. It also removes obsolete APIs from the internally-used IEDriver.dll. Since IEDriverServer.exe is now the only client of that DLL, and indeed, since the DLL is no longer produced as a standalone binary, the state-management APIs provided by the DLL are no longer needed. --- cpp/IEDriver/WebDriver.cpp | 46 +++----- cpp/IEDriver/WebDriver.h | 16 ++- cpp/IEDriverServer/CommandLineArguments.cpp | 18 ++-- cpp/IEDriverServer/CommandLineArguments.h | 6 +- cpp/IEDriverServer/IEDriverServer.cpp | 110 ++++++++++---------- cpp/IEDriverServer/IEDriverServer.vcxproj | 8 +- 6 files changed, 92 insertions(+), 112 deletions(-) diff --git a/cpp/IEDriver/WebDriver.cpp b/cpp/IEDriver/WebDriver.cpp index 523abed38d412..3ea7bf2c03a7a 100644 --- a/cpp/IEDriver/WebDriver.cpp +++ b/cpp/IEDriver/WebDriver.cpp @@ -13,17 +13,21 @@ #include "WebDriver.h" -webdriver::Server* StartServer(int port) { - return StartServerEx(port, "", "", "", ""); -} - -webdriver::Server* StartServerEx(int port, - const std::string& host, - const std::string& log_level, - const std::string& log_file, - const std::string& version) { +webdriver::Server* StartServer(int port, + const std::wstring& host, + const std::wstring& log_level, + const std::wstring& log_file, + const std::wstring& version) { if (server == NULL) { - server = new webdriver::IEServer(port, host, log_level, log_file, version); + std::string converted_host = CW2A(host.c_str(), CP_UTF8); + std::string converted_log_level = CW2A(log_level.c_str(), CP_UTF8); + std::string converted_log_file = CW2A(log_file.c_str(), CP_UTF8); + std::string converted_version = CW2A(version.c_str(), CP_UTF8); + server = new webdriver::IEServer(port, + converted_host, + converted_log_level, + converted_log_file, + converted_version); if (!server->Start()) { delete server; server = NULL; @@ -32,30 +36,10 @@ webdriver::Server* StartServerEx(int port, return server; } -void StopServer(webdriver::Server* myserver) { +void StopServer() { if (server) { server->Stop(); delete server; server = NULL; } } - -int GetServerSessionCount() { - int session_count(0); - if (server != NULL) { - session_count = server->session_count(); - } - return session_count; -} - -int GetServerPort() { - int server_port(0); - if (server != NULL) { - server_port = server->port(); - } - return server_port; -} - -bool ServerIsRunning() { - return server != NULL; -} \ No newline at end of file diff --git a/cpp/IEDriver/WebDriver.h b/cpp/IEDriver/WebDriver.h index 954d05cbec20c..2ddb6457f1a20 100644 --- a/cpp/IEDriver/WebDriver.h +++ b/cpp/IEDriver/WebDriver.h @@ -24,16 +24,12 @@ extern "C" { webdriver::Server* server = NULL; -EXPORT webdriver::Server* StartServer(int port); -EXPORT webdriver::Server* StartServerEx(int port, - const std::string& host, - const std::string& log_level, - const std::string& log_file, - const std::string& version); -EXPORT void StopServer(webdriver::Server* myserver); -EXPORT int GetServerSessionCount(); -EXPORT bool ServerIsRunning(); -EXPORT int GetServerPort(); +EXPORT webdriver::Server* StartServer(int port, + const std::wstring& host, + const std::wstring& log_level, + const std::wstring& log_file, + const std::wstring& version); +EXPORT void StopServer(void); #ifdef __cplusplus } diff --git a/cpp/IEDriverServer/CommandLineArguments.cpp b/cpp/IEDriverServer/CommandLineArguments.cpp index c5eed6df95cee..d1439b20bf284 100644 --- a/cpp/IEDriverServer/CommandLineArguments.cpp +++ b/cpp/IEDriverServer/CommandLineArguments.cpp @@ -20,9 +20,9 @@ CommandLineArguments::CommandLineArguments(int argc, _TCHAR* argv[]) { CommandLineArguments::~CommandLineArguments(void) { } -std::string CommandLineArguments::GetValue(std::string arg_name, - std::string default_value) { - std::map::const_iterator it = +std::wstring CommandLineArguments::GetValue(std::wstring arg_name, + std::wstring default_value) { + std::map::const_iterator it = this->args_map_.find(arg_name); if (it != this->args_map_.end()) { return it->second; @@ -38,19 +38,19 @@ void CommandLineArguments::ParseArguments(int argc, _TCHAR* argv[]) { int switch_delimiter_length = GetSwitchDelimiterLength(raw_arg); std::wstring arg = raw_arg.substr(switch_delimiter_length); size_t equal_pos = arg.find(L"="); - std::string arg_name = ""; - std::string arg_value = ""; + std::wstring arg_name = L""; + std::wstring arg_value = L""; if (equal_pos != std::string::npos && equal_pos > 0) { - arg_name = CW2A(arg.substr(0, equal_pos).c_str(), CP_UTF8); - arg_value = CW2A(arg.substr(equal_pos + 1).c_str(), CP_UTF8); + arg_name = arg.substr(0, equal_pos); + arg_value = arg.substr(equal_pos + 1); } else { - arg_name = CW2A(arg.c_str(), CP_UTF8); + arg_name = arg.c_str(); } // coerce all argument names to lowercase, making argument names // case-insensitive. std::transform(arg_name.begin(), arg_name.end(), arg_name.begin(), tolower); - if (arg_name == "?" || arg_name == "h" || arg_name == "help") { + if (arg_name == L"?" || arg_name == L"h" || arg_name == L"help") { this->is_help_requested_ = true; } this->args_map_[arg_name] = arg_value; diff --git a/cpp/IEDriverServer/CommandLineArguments.h b/cpp/IEDriverServer/CommandLineArguments.h index d1b3a34315021..6a30d0f4d65f7 100644 --- a/cpp/IEDriverServer/CommandLineArguments.h +++ b/cpp/IEDriverServer/CommandLineArguments.h @@ -26,8 +26,8 @@ class CommandLineArguments { CommandLineArguments(int arg_count, _TCHAR* arg_array[]); virtual ~CommandLineArguments(void); - std::string GetValue(std::string arg_name, - std::string default_value); + std::wstring GetValue(std::wstring arg_name, + std::wstring default_value); bool is_help_requested (void) const { return this->is_help_requested_; } private: @@ -35,7 +35,7 @@ class CommandLineArguments { int GetSwitchDelimiterLength(std::wstring arg); bool is_help_requested_; - std::map args_map_; + std::map args_map_; }; #endif // COMMANDLINEARGUMENTS_H_ \ No newline at end of file diff --git a/cpp/IEDriverServer/IEDriverServer.cpp b/cpp/IEDriverServer/IEDriverServer.cpp index 0a2d4ff5e8ae1..5d69628b7ebf4 100644 --- a/cpp/IEDriverServer/IEDriverServer.cpp +++ b/cpp/IEDriverServer/IEDriverServer.cpp @@ -22,7 +22,7 @@ // TODO(JimEvans): Change the prototypes of these functions in the // IEDriver project to match the prototype specified here. -typedef void* (__cdecl *STARTSERVEREXPROC)(int, const std::string&, const std::string&, const std::string&, const std::string&); +typedef void* (__cdecl *STARTSERVERPROC)(int, const std::wstring&, const std::wstring&, const std::wstring&, const std::wstring&); typedef void (__cdecl *STOPSERVERPROC)(void); #define ERR_DLL_EXTRACT_FAIL 1 @@ -32,16 +32,16 @@ typedef void (__cdecl *STOPSERVERPROC)(void); #define RESOURCE_TYPE L"BINARY" #define TEMP_FILE_PREFIX L"IEDriver" -#define START_SERVER_EX_API_NAME "StartServerEx" +#define START_SERVER_EX_API_NAME "StartServer" #define STOP_SERVER_API_NAME "StopServer" -#define PORT_COMMAND_LINE_ARG "port" -#define HOST_COMMAND_LINE_ARG "host" -#define LOGLEVEL_COMMAND_LINE_ARG "log-level" -#define LOGFILE_COMMAND_LINE_ARG "log-file" -#define SILENT_COMMAND_LINE_ARG "silent" -#define EXTRACTPATH_COMMAND_LINE_ARG "extract-path" -#define BOOLEAN_COMMAND_LINE_ARG_MISSING_VALUE "value-not-specified" +#define PORT_COMMAND_LINE_ARG L"port" +#define HOST_COMMAND_LINE_ARG L"host" +#define LOGLEVEL_COMMAND_LINE_ARG L"log-level" +#define LOGFILE_COMMAND_LINE_ARG L"log-file" +#define SILENT_COMMAND_LINE_ARG L"silent" +#define EXTRACTPATH_COMMAND_LINE_ARG L"extract-path" +#define BOOLEAN_COMMAND_LINE_ARG_MISSING_VALUE L"value-not-specified" bool ExtractResource(unsigned short resource_id, const std::wstring& output_file_name) { @@ -91,8 +91,8 @@ bool ExtractResource(unsigned short resource_id, return success; } -std::string GetProcessArchitectureDescription() { - std::string arch_description = "32-bit"; +std::wstring GetProcessArchitectureDescription() { + std::wstring arch_description = L"32-bit"; SYSTEM_INFO system_info; ::GetNativeSystemInfo(&system_info); if (system_info.wProcessorArchitecture != 0) { @@ -100,7 +100,7 @@ std::string GetProcessArchitectureDescription() { HANDLE process_handle = ::GetCurrentProcess(); ::IsWow64Process(process_handle, &is_emulated); if (!is_emulated) { - arch_description = "64-bit"; + arch_description = L"64-bit"; } ::CloseHandle(process_handle); } @@ -108,45 +108,45 @@ std::string GetProcessArchitectureDescription() { return arch_description; } -std::string GetExecutableVersion() { +std::wstring GetExecutableVersion() { struct LANGANDCODEPAGE { WORD language; WORD code_page; } *lang_info; // get the filename of the executable containing the version resource - std::vector file_name_buffer(MAX_PATH + 1); - ::GetModuleFileNameA(NULL, &file_name_buffer[0], MAX_PATH); + std::vector file_name_buffer(MAX_PATH + 1); + ::GetModuleFileNameW(NULL, &file_name_buffer[0], MAX_PATH); DWORD dummy; - DWORD length = ::GetFileVersionInfoSizeA(&file_name_buffer[0], + DWORD length = ::GetFileVersionInfoSizeW(&file_name_buffer[0], &dummy); std::vector version_buffer(length); - ::GetFileVersionInfoA(&file_name_buffer[0], + ::GetFileVersionInfoW(&file_name_buffer[0], dummy, length, &version_buffer[0]); UINT page_count; - BOOL query_result = ::VerQueryValueA(&version_buffer[0], - "\\VarFileInfo\\Translation", + BOOL query_result = ::VerQueryValueW(&version_buffer[0], + L"\\VarFileInfo\\Translation", reinterpret_cast(&lang_info), &page_count); - char sub_block[MAX_PATH]; - _snprintf_s(sub_block, + wchar_t sub_block[MAX_PATH]; + _snwprintf_s(sub_block, MAX_PATH, MAX_PATH, - "\\StringFileInfo\\%04x%04x\\FileVersion", + L"\\StringFileInfo\\%04x%04x\\FileVersion", lang_info->language, lang_info->code_page); LPVOID value = NULL; UINT size; - query_result = ::VerQueryValueA(&version_buffer[0], + query_result = ::VerQueryValueW(&version_buffer[0], sub_block, &value, &size); - return static_cast(value); + return static_cast(value); } void ShowUsage(void) { @@ -188,9 +188,9 @@ int _tmain(int argc, _TCHAR* argv[]) { std::wstring extraction_path(&temp_path_buffer[0]); - std::string extraction_path_arg = args.GetValue(EXTRACTPATH_COMMAND_LINE_ARG, ""); + std::wstring extraction_path_arg = args.GetValue(EXTRACTPATH_COMMAND_LINE_ARG, L""); if (extraction_path_arg.size() != 0) { - extraction_path = CA2W(extraction_path_arg.c_str(), CP_UTF8); + extraction_path = extraction_path_arg; } unsigned int error_code = ::GetTempFileName(extraction_path.c_str(), @@ -212,7 +212,7 @@ int _tmain(int argc, _TCHAR* argv[]) { return ERR_DLL_LOAD_FAIL; } - STARTSERVEREXPROC start_server_ex_proc = reinterpret_cast( + STARTSERVERPROC start_server_ex_proc = reinterpret_cast( ::GetProcAddress(module_handle, START_SERVER_EX_API_NAME)); STOPSERVERPROC stop_server_proc = reinterpret_cast( ::GetProcAddress(module_handle, STOP_SERVER_API_NAME)); @@ -222,52 +222,52 @@ int _tmain(int argc, _TCHAR* argv[]) { return ERR_FUNCTION_NOT_FOUND; } - int port = atoi(args.GetValue(PORT_COMMAND_LINE_ARG, "5555").c_str()); - std::string host_address = args.GetValue(HOST_COMMAND_LINE_ARG, ""); - std::string log_level = args.GetValue(LOGLEVEL_COMMAND_LINE_ARG, ""); - std::string log_file = args.GetValue(LOGFILE_COMMAND_LINE_ARG, ""); + int port = _wtoi(args.GetValue(PORT_COMMAND_LINE_ARG, L"5555").c_str()); + std::wstring host_address = args.GetValue(HOST_COMMAND_LINE_ARG, L""); + std::wstring log_level = args.GetValue(LOGLEVEL_COMMAND_LINE_ARG, L""); + std::wstring log_file = args.GetValue(LOGFILE_COMMAND_LINE_ARG, L""); bool silent = args.GetValue(SILENT_COMMAND_LINE_ARG, BOOLEAN_COMMAND_LINE_ARG_MISSING_VALUE).size() == 0; - std::string executable_version = GetExecutableVersion(); + std::wstring executable_version = GetExecutableVersion(); void* server_value = start_server_ex_proc(port, host_address, log_level, log_file, executable_version); if (server_value == NULL) { - std::cout << L"Failed to start the server with: " - << L"port = '" << port << "', " - << L"host = '" << host_address << "', " - << L"log level = '" << log_level << "', " - << L"log file = '" << log_file << "'"; + std::wcout << L"Failed to start the server with: " + << L"port = '" << port << "', " + << L"host = '" << host_address << "', " + << L"log level = '" << log_level << "', " + << L"log file = '" << log_file << "'"; return ERR_SERVER_START; } if (!silent) { - std::cout << "Started InternetExplorerDriver server" - << " (" << GetProcessArchitectureDescription() << ")" - << std::endl; - std::cout << executable_version - << std::endl; - std::cout << "Listening on port " << port << std::endl; + std::wcout << L"Started InternetExplorerDriver server" + << L" (" << GetProcessArchitectureDescription() << L")" + << std::endl; + std::wcout << executable_version + << std::endl; + std::wcout << L"Listening on port " << port << std::endl; if (host_address.size() > 0) { - std::cout << "Bound to network adapter with IP address " - << host_address - << std::endl; + std::wcout << L"Bound to network adapter with IP address " + << host_address + << std::endl; } if (log_level.size() > 0) { - std::cout << "Log level is set to " - << log_level - << std::endl; + std::wcout << L"Log level is set to " + << log_level + << std::endl; } if (log_file.size() > 0) { - std::cout << "Log file is set to " - << log_file - << std::endl; + std::wcout << L"Log file is set to " + << log_file + << std::endl; } if (extraction_path_arg.size() > 0) { - std::cout << "Library extracted to " - << extraction_path_arg - << std::endl; + std::wcout << L"Library extracted to " + << extraction_path_arg + << std::endl; } } diff --git a/cpp/IEDriverServer/IEDriverServer.vcxproj b/cpp/IEDriverServer/IEDriverServer.vcxproj index 4c81d01f72b99..95ef6ef78997a 100644 --- a/cpp/IEDriverServer/IEDriverServer.vcxproj +++ b/cpp/IEDriverServer/IEDriverServer.vcxproj @@ -28,27 +28,27 @@ Application true Unicode - Static + false Application true Unicode - Static + false Application false true Unicode - Static + false Application false true Unicode - Static + false