Skip to content
This repository has been archived by the owner on Jan 4, 2019. It is now read-only.

Commit

Permalink
Merge pull request #5 from brave/muon-upgrade
Browse files Browse the repository at this point in the history
Part 1 of fix for brave/brave-browser#1782
  • Loading branch information
bsclifton committed Oct 25, 2018
2 parents ecc2d6a + deb72e1 commit a636cea
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/Setup/Setup.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@
<LinkIncremental>true</LinkIncremental>
<OutDir>$(ProjectDir)bin\$(Configuration)\</OutDir>
<IntDir>$(ProjectDir)obj\$(Configuration)\</IntDir>
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);$(ProjectDir)/wtl90</IncludePath>
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);$(ProjectDir)/wtl90;$(WindowsSDK_IncludePath)\um</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);$(ProjectDir)/wtl90</IncludePath>
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);$(ProjectDir)/wtl90;$(WindowsSDK_IncludePath)\um</IncludePath>
<OutDir>$(ProjectDir)bin\$(Configuration)\</OutDir>
<IntDir>$(ProjectDir)obj\$(Configuration)\</IntDir>
</PropertyGroup>
Expand Down
93 changes: 88 additions & 5 deletions src/StubExecutable/StubExecutable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,79 @@ wchar_t* FindOwnExecutableName()
return ret;
}

// Credit to https://stackoverflow.com/a/35717/3193009
LONG GetStringRegKey(HKEY hKey, const std::wstring &strValueName, std::wstring &strValue)
{
WCHAR szBuffer[512];
DWORD dwBufferSize = sizeof(szBuffer);
ULONG nError;
nError = RegQueryValueExW(hKey, strValueName.c_str(), 0, NULL, (LPBYTE)szBuffer, &dwBufferSize);
if (ERROR_SUCCESS == nError)
{
strValue = szBuffer;
}
return nError;
}

// Credit to https://stackoverflow.com/a/6218957/3193009
BOOL FileExists(LPCTSTR szPath)
{
DWORD dwAttrib = GetFileAttributes(szPath);

return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}

std::wstring GetBraveCorePath(HKEY hKey, LPCTSTR lpSubKey)
{
HKEY hBraveKey;
std::wstring fullPath;
if (ERROR_SUCCESS == RegOpenKeyExW(hKey,
lpSubKey, 0, KEY_READ, &hBraveKey))
{
if (ERROR_SUCCESS == GetStringRegKey(hBraveKey, L"path", fullPath))
{
size_t path_index = fullPath.find(L"Update\\BraveUpdate.exe");
if (path_index != std::string::npos)
{
fullPath = fullPath.substr(0, path_index).append(L"Brave-Browser\\Application");
std::wstring exePath(fullPath + L"\\brave.exe");
if (FileExists(exePath.c_str()))
{
return fullPath;
}
}
}
RegCloseKey(hBraveKey);
}

return std::wstring(L"");
}

std::wstring FindBraveCoreInstall()
{
std::wstring fullPath;

// 1) Check for user-specific install
// ex: `C:\Users\bsclifton\AppData\Local\BraveSoftware\Brave-Browser\Application\brave.exe`
fullPath.assign(GetBraveCorePath(HKEY_CURRENT_USER, L"Software\\BraveSoftware\\Update"));
if (fullPath.length() > 0) {
return fullPath;
}

// 2) Check for multi-user install (install would prompt w/ UAC)
// 64-bit; ex: `C:\Program Files (x86)\BraveSoftware\Brave-Browser\Application\brave.exe`
fullPath.assign(GetBraveCorePath(HKEY_LOCAL_MACHINE, L"SOFTWARE\\WOW6432Node\\BraveSoftware\\Update"));
if (fullPath.length() > 0) {
return fullPath;
}

// 32-bit; ex: `C:\Program Files\BraveSoftware\Brave-Browser\Application\brave.exe`
fullPath.assign(GetBraveCorePath(HKEY_LOCAL_MACHINE, L"SOFTWARE\\BraveSoftware\\Update"));

return fullPath;
}

std::wstring FindLatestAppDir()
{
std::wstring ourDir;
Expand Down Expand Up @@ -90,11 +163,21 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
std::wstring appName;
appName.assign(FindOwnExecutableName());

std::wstring workingDir(FindLatestAppDir());
std::wstring fullPath(workingDir + L"\\" + appName);
std::wstring fullPath;
std::wstring workingDir;
// Brave Software specific logic
std::wstring braveCore(FindBraveCoreInstall());
if (braveCore.length() > 0) {
// Logic specifically for launching detected brave-core
workingDir.assign(braveCore);
fullPath = workingDir + L"\\brave.exe";
} else {
// Original stub installer logic
std::wstring appName;
appName.assign(FindOwnExecutableName());
workingDir.assign(FindLatestAppDir());
fullPath = workingDir + L"\\" + appName;
}

STARTUPINFO si = { 0 };
PROCESS_INFORMATION pi = { 0 };
Expand Down

0 comments on commit a636cea

Please sign in to comment.