Skip to content

Commit

Permalink
Updating IE driver C++ code to use Visual Studio 2015.
Browse files Browse the repository at this point in the history
Also cleaning up deprecation warnings and compile errors.
  • Loading branch information
jimevans committed Jul 30, 2015
1 parent c81d9e2 commit 24e05d2
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 77 deletions.
64 changes: 12 additions & 52 deletions cpp/iedriver/BrowserFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
#include <sddl.h>
#include <shlguid.h>
#include <shlobj.h>
#include <VersionHelpers.h>
#include <WinInet.h>
#include "FileUtilities.h"
#include "logging.h"
#include "psapi.h"
#include "RegistryUtilities.h"
Expand All @@ -34,7 +36,6 @@ BrowserFactory::BrowserFactory(void) {
// Must be done in the constructor. Do not move to Initialize().
this->GetExecutableLocation();
this->GetIEVersion();
this->GetOSVersion();
}

BrowserFactory::~BrowserFactory(void) {
Expand Down Expand Up @@ -63,7 +64,7 @@ void BrowserFactory::Initialize(BrowserFactorySettings settings) {
void BrowserFactory::ClearCache() {
LOG(TRACE) << "Entering BrowserFactory::ClearCache";
if (this->clear_cache_) {
if (this->windows_major_version_ >= 6) {
if (::IsWindowsVistaOrGreater()) {
LOG(DEBUG) << "Clearing cache with low mandatory integrity level as required on Windows Vista or later.";
this->InvokeClearCacheUtility(true);
}
Expand Down Expand Up @@ -634,7 +635,7 @@ IWebBrowser2* BrowserFactory::CreateBrowser() {

IWebBrowser2* browser = NULL;
DWORD context = CLSCTX_LOCAL_SERVER;
if (this->ie_major_version_ == 7 && this->windows_major_version_ >= 6) {
if (this->ie_major_version_ == 7 && ::IsWindowsVistaOrGreater()) {
// ONLY for IE 7 on Windows Vista. XP and below do not have Protected Mode;
// Windows 7 shipped with IE8.
context = context | CLSCTX_ENABLE_CLOAKING;
Expand Down Expand Up @@ -972,15 +973,9 @@ void BrowserFactory::GetExecutableLocation() {
void BrowserFactory::GetIEVersion() {
LOG(TRACE) << "Entering BrowserFactory::GetIEVersion";

struct LANGANDCODEPAGE {
WORD language;
WORD code_page;
} *lpTranslate;

DWORD dummy = 0;
DWORD length = ::GetFileVersionInfoSize(this->ie_executable_location_.c_str(),
&dummy);
if (length == 0) {
std::string ie_version = FileUtilities::GetFileVersion(this->ie_executable_location_);

if (ie_version.size() == 0) {
// 64-bit Windows 8 has a bug where it does not return the executable location properly
this->ie_major_version_ = -1;
LOG(WARN) << "Couldn't find IE version for executable "
Expand All @@ -989,60 +984,25 @@ void BrowserFactory::GetIEVersion() {
<< this->ie_major_version_;
return;
}
std::vector<BYTE> version_buffer(length);
::GetFileVersionInfo(this->ie_executable_location_.c_str(),
0, /* ignored */
length,
&version_buffer[0]);

UINT page_count;
BOOL query_result = ::VerQueryValue(&version_buffer[0],
FILE_LANGUAGE_INFO,
reinterpret_cast<void**>(&lpTranslate),
&page_count);

wchar_t sub_block[MAX_PATH];
_snwprintf_s(sub_block,
MAX_PATH,
MAX_PATH,
FILE_VERSION_INFO,
lpTranslate->language,
lpTranslate->code_page);
LPVOID value = NULL;
UINT size;
query_result = ::VerQueryValue(&version_buffer[0],
sub_block,
&value,
&size);
std::wstring ie_version;
ie_version.assign(static_cast<wchar_t*>(value));
std::wstringstream version_stream(ie_version);
version_stream >> this->ie_major_version_;
}

void BrowserFactory::GetOSVersion() {
LOG(TRACE) << "Entering BrowserFactory::GetOSVersion";

OSVERSIONINFO osVersion;
osVersion.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
::GetVersionEx(&osVersion);
this->windows_major_version_ = osVersion.dwMajorVersion;
this->windows_minor_version_ = osVersion.dwMinorVersion;
std::stringstream version_stream(ie_version);
version_stream >> this->ie_major_version_;
}

bool BrowserFactory::ProtectedModeSettingsAreValid() {
LOG(TRACE) << "Entering BrowserFactory::ProtectedModeSettingsAreValid";

bool settings_are_valid = true;
LOG(DEBUG) << "Detected IE version: " << this->ie_major_version_
<< ", detected Windows version: " << this->windows_major_version_;
<< ", Windows version supports Protected Mode: "
<< ::IsWindowsVistaOrGreater() ? "true" : "false";
// Only need to check Protected Mode settings on IE 7 or higher
// and on Windows Vista or higher. Otherwise, Protected Mode
// doesn't come into play, and are valid.
// Documentation of registry settings can be found at the following
// Microsoft KnowledgeBase article:
// http://support.microsoft.com/kb/182569
if (this->ie_major_version_ >= 7 && this->windows_major_version_ >= 6) {
if (this->ie_major_version_ >= 7 && ::IsWindowsVistaOrGreater()) {
HKEY key_handle;
if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_CURRENT_USER,
IE_SECURITY_ZONES_REGISTRY_KEY,
Expand Down
8 changes: 0 additions & 8 deletions cpp/iedriver/BrowserFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
#define ALERT_WINDOW_CLASS "#32770"
#define HTML_DIALOG_WINDOW_CLASS "Internet Explorer_TridentDlgFrame"

#define FILE_LANGUAGE_INFO L"\\VarFileInfo\\Translation"
#define FILE_VERSION_INFO L"\\StringFileInfo\\%04x%04x\\FileVersion"

#define IE_CLSID_REGISTRY_KEY L"SOFTWARE\\Classes\\InternetExplorer.Application\\CLSID"
#define IE_SECURITY_ZONES_REGISTRY_KEY L"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones"
#define IE_TABPROCGROWTH_REGISTRY_KEY L"Software\\Microsoft\\Internet Explorer\\Main"
Expand Down Expand Up @@ -126,8 +123,6 @@ class BrowserFactory {
}

int browser_version(void) const { return this->ie_major_version_; }
int windows_major_version(void) const { return this->windows_major_version_; }
int windows_minor_version(void) const { return this->windows_minor_version_; }

static BOOL CALLBACK FindChildWindowForProcess(HWND hwnd, LPARAM arg);
static BOOL CALLBACK FindDialogWindowForProcess(HWND hwnd, LPARAM arg);
Expand All @@ -150,7 +145,6 @@ class BrowserFactory {

void GetExecutableLocation(void);
void GetIEVersion(void);
void GetOSVersion(void);
bool ProtectedModeSettingsAreValid(void);
int GetZoneProtectedModeSetting(const HKEY key_handle,
const std::wstring& zone_subkey_name);
Expand All @@ -176,8 +170,6 @@ class BrowserFactory {
int browser_attach_timeout_;

int ie_major_version_;
int windows_major_version_;
int windows_minor_version_;
std::wstring ie_executable_location_;
};

Expand Down
79 changes: 79 additions & 0 deletions cpp/iedriver/FileUtilities.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// 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 "FileUtilities.h"

#define FILE_LANGUAGE_INFO L"\\VarFileInfo\\Translation"
#define FILE_VERSION_INFO L"\\StringFileInfo\\%04x%04x\\FileVersion"

namespace webdriver {

FileUtilities::FileUtilities(void) {
}

FileUtilities::~FileUtilities(void) {
}

std::string FileUtilities::GetFileVersion(const std::string& file_name) {
return GetFileVersion(StringUtilities::ToWString(file_name));
}

std::string FileUtilities::GetFileVersion(const std::wstring& file_name) {
struct LANGANDCODEPAGE {
WORD language;
WORD code_page;
} *language_info;

DWORD dummy = 0;
DWORD length = ::GetFileVersionInfoSize(file_name.c_str(), &dummy);
if (length == 0) {
return "";
}

std::vector<char> version_buffer(length);
::GetFileVersionInfo(file_name.c_str(),
0, /* ignored */
length,
&version_buffer[0]);

UINT page_count;
BOOL query_result = ::VerQueryValue(&version_buffer[0],
FILE_LANGUAGE_INFO,
reinterpret_cast<void**>(&language_info),
&page_count);

std::vector<wchar_t> sub_block(MAX_PATH);
_snwprintf_s(&sub_block[0],
MAX_PATH,
MAX_PATH,
FILE_VERSION_INFO,
language_info->language,
language_info->code_page);
std::wstring sub_block_string = &sub_block[0];

void* value = NULL;
unsigned int size;
query_result = ::VerQueryValue(&version_buffer[0],
sub_block_string.c_str(),
&value,
&size);
std::wstring wide_version;
wide_version.assign(static_cast<wchar_t*>(value));
std::string version = StringUtilities::ToString(wide_version);
return version;
}

} // namespace webdriver
36 changes: 36 additions & 0 deletions cpp/iedriver/FileUtilities.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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.

#ifndef WEBDRIVER_IE_FILEUTILITIES_H_
#define WEBDRIVER_IE_FILEUTILITIES_H_

#include <string>


namespace webdriver {

class FileUtilities {
private:
FileUtilities();
~FileUtilities();
public:
static std::string GetFileVersion(const std::string& file_name);
static std::string GetFileVersion(const std::wstring& file_name);
};

} // namespace webdriver

#endif // WEBDRIVER_IE_FILEUTILITIES_H_
2 changes: 2 additions & 0 deletions cpp/iedriver/IEDriver.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@
<ClCompile Include="Element.cpp" />
<ClCompile Include="ElementFinder.cpp" />
<ClCompile Include="ElementRepository.cpp" />
<ClCompile Include="FileUtilities.cpp" />
<ClCompile Include="HookProcessor.cpp" />
<ClCompile Include="HtmlDialog.cpp" />
<ClCompile Include="IECommandExecutor.cpp" />
Expand Down Expand Up @@ -277,6 +278,7 @@
<ClInclude Include="Element.h" />
<ClInclude Include="ElementFinder.h" />
<ClInclude Include="ElementRepository.h" />
<ClInclude Include="FileUtilities.h" />
<ClInclude Include="HookProcessor.h" />
<ClInclude Include="HtmlDialog.h" />
<ClInclude Include="IECommandExecutor.h" />
Expand Down
6 changes: 6 additions & 0 deletions cpp/iedriver/IEDriver.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@
<ClCompile Include="BrowserCookie.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="FileUtilities.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Browser.h">
Expand Down Expand Up @@ -379,6 +382,9 @@
<ClInclude Include="CommandHandlers\SetAlertCredentialsCommandHandler.h">
<Filter>Header Files\CommandHandlers</Filter>
</ClInclude>
<ClInclude Include="FileUtilities.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="IEDriver.def">
Expand Down
12 changes: 3 additions & 9 deletions cpp/iedriver/IEServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include "IEServer.h"
#include "IESession.h"
#include <VersionHelpers.h>
#include "FileUtilities.h"
#include "logging.h"

namespace webdriver {
Expand Down Expand Up @@ -51,15 +53,7 @@ std::string IEServer::GetStatus() {
::ZeroMemory(&system_info, sizeof(SYSTEM_INFO));
::GetNativeSystemInfo(&system_info);

OSVERSIONINFO os_version_info;
::ZeroMemory(&os_version_info, sizeof(OSVERSIONINFO));
os_version_info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
::GetVersionEx(&os_version_info);

std::string major_version = std::to_string(static_cast<long long>(os_version_info.dwMajorVersion));
std::string minor_version = std::to_string(static_cast<long long>(os_version_info.dwMinorVersion));
std::string build_version = std::to_string(static_cast<long long>(os_version_info.dwBuildNumber));
std::string os_version = major_version + "." + minor_version + "." + build_version;
std::string os_version = FileUtilities::GetFileVersion("kernel32.dll");

std::string arch = "x86";
if (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) {
Expand Down
2 changes: 1 addition & 1 deletion cpp/iedriver/InputManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ bool InputManager::SetFocusToBrowser(BrowserHandle browser_wrapper) {
}
::SetForegroundWindow(browser_wrapper->GetTopLevelWindowHandle());
if (current_thread_id != thread_id) {
::SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, (PVOID)lock_timeout, SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);
::SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, reinterpret_cast<void*>(lock_timeout), SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);
::AttachThreadInput(current_thread_id, thread_id, FALSE);
}
}
Expand Down
2 changes: 1 addition & 1 deletion cpp/webdriver-interactions/webdriver-interactions.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<WarningLevel>Level3</WarningLevel>
<DebugInformationFormat>EditAndContinue</DebugInformationFormat>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
</ClCompile>
<PostBuildEvent>
<Command>
Expand Down
2 changes: 2 additions & 0 deletions cpp/webdriver-server/webdriver-server.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeaderFile>precompile.h</PrecompiledHeaderFile>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
Expand All @@ -104,6 +105,7 @@
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeaderFile>precompile.h</PrecompiledHeaderFile>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
Expand Down
Loading

0 comments on commit 24e05d2

Please sign in to comment.