Skip to content

Commit

Permalink
Internal code cleanup of IEDriverServer.exe
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jimevans committed Mar 20, 2013
1 parent 6199dbe commit 51a60b5
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 112 deletions.
46 changes: 15 additions & 31 deletions cpp/IEDriver/WebDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
16 changes: 6 additions & 10 deletions cpp/IEDriver/WebDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
18 changes: 9 additions & 9 deletions cpp/IEDriverServer/CommandLineArguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::string>::const_iterator it =
std::wstring CommandLineArguments::GetValue(std::wstring arg_name,
std::wstring default_value) {
std::map<std::wstring, std::wstring>::const_iterator it =
this->args_map_.find(arg_name);
if (it != this->args_map_.end()) {
return it->second;
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions cpp/IEDriverServer/CommandLineArguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ 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:
void ParseArguments(int argc, _TCHAR* argv[]);
int GetSwitchDelimiterLength(std::wstring arg);

bool is_help_requested_;
std::map<std::string, std::string> args_map_;
std::map<std::wstring, std::wstring> args_map_;
};

#endif // COMMANDLINEARGUMENTS_H_
110 changes: 55 additions & 55 deletions cpp/IEDriverServer/IEDriverServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -91,62 +91,62 @@ 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) {
BOOL is_emulated;
HANDLE process_handle = ::GetCurrentProcess();
::IsWow64Process(process_handle, &is_emulated);
if (!is_emulated) {
arch_description = "64-bit";
arch_description = L"64-bit";
}
::CloseHandle(process_handle);
}

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<char> file_name_buffer(MAX_PATH + 1);
::GetModuleFileNameA(NULL, &file_name_buffer[0], MAX_PATH);
std::vector<wchar_t> 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<BYTE> 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<void**>(&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<char*>(value);
return static_cast<wchar_t*>(value);
}

void ShowUsage(void) {
Expand Down Expand Up @@ -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(),
Expand All @@ -212,7 +212,7 @@ int _tmain(int argc, _TCHAR* argv[]) {
return ERR_DLL_LOAD_FAIL;
}

STARTSERVEREXPROC start_server_ex_proc = reinterpret_cast<STARTSERVEREXPROC>(
STARTSERVERPROC start_server_ex_proc = reinterpret_cast<STARTSERVERPROC>(
::GetProcAddress(module_handle, START_SERVER_EX_API_NAME));
STOPSERVERPROC stop_server_proc = reinterpret_cast<STOPSERVERPROC>(
::GetProcAddress(module_handle, STOP_SERVER_API_NAME));
Expand All @@ -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;
}
}

Expand Down
8 changes: 4 additions & 4 deletions cpp/IEDriverServer/IEDriverServer.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,27 @@
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
<UseOfAtl>Static</UseOfAtl>
<UseOfAtl>false</UseOfAtl>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
<UseOfAtl>Static</UseOfAtl>
<UseOfAtl>false</UseOfAtl>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<UseOfAtl>Static</UseOfAtl>
<UseOfAtl>false</UseOfAtl>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<UseOfAtl>Static</UseOfAtl>
<UseOfAtl>false</UseOfAtl>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down

0 comments on commit 51a60b5

Please sign in to comment.