From 9227520b8788401c32544f5a706f40bd365c8808 Mon Sep 17 00:00:00 2001 From: "Brian R. Bondy" Date: Wed, 15 May 2013 10:58:09 -0700 Subject: [PATCH] Bug 572162 - Use TaskbarIDs hash as update dir root. r=rstrong --- toolkit/xre/nsXREDirProvider.cpp | 62 ++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/toolkit/xre/nsXREDirProvider.cpp b/toolkit/xre/nsXREDirProvider.cpp index 5ed667ba55b..0ac0bdc837b 100644 --- a/toolkit/xre/nsXREDirProvider.cpp +++ b/toolkit/xre/nsXREDirProvider.cpp @@ -954,6 +954,28 @@ GetRegWindowsAppDataFolder(bool aLocal, nsAString& _retval) return NS_OK; } + +static bool +GetCachedHash(HKEY rootKey, const nsAString ®Path, const nsAString &path, + nsAString &cachedHash) +{ + HKEY baseKey; + if (RegOpenKeyExW(rootKey, regPath.BeginReading(), 0, KEY_READ, &baseKey) != + ERROR_SUCCESS) { + return false; + } + + wchar_t cachedHashRaw[512]; + DWORD bufferSize = sizeof(cachedHashRaw); + LONG result = RegQueryValueExW(baseKey, path.BeginReading(), 0, NULL, + (LPBYTE)cachedHashRaw, &bufferSize); + RegCloseKey(baseKey); + if (result == ERROR_SUCCESS) { + cachedHash.Assign(cachedHashRaw); + } + return ERROR_SUCCESS == result; +} + #endif nsresult @@ -976,6 +998,46 @@ nsXREDirProvider::GetUpdateRootDir(nsIFile* *aResult) NS_ENSURE_SUCCESS(rv, rv); #ifdef XP_WIN + + nsAutoString pathHash; + bool pathHashResult = false; + + nsAutoString appDirPath; + if (gAppData->vendor && !getenv("MOZ_UPDATE_NO_HASH_DIR") && + SUCCEEDED(updRoot->GetPath(appDirPath))) { + + // Figure out where we should check for a cached hash value + wchar_t regPath[1024] = { L'\0' }; + swprintf_s(regPath, mozilla::ArrayLength(regPath), L"SOFTWARE\\%S\\%S\\TaskBarIDs", + gAppData->vendor, MOZ_APP_NAME); + + // If we pre-computed the hash, grab it from the registry. + pathHashResult = GetCachedHash(HKEY_LOCAL_MACHINE, + nsDependentString(regPath), appDirPath, + pathHash); + if (!pathHashResult) { + pathHashResult = GetCachedHash(HKEY_CURRENT_USER, + nsDependentString(regPath), appDirPath, + pathHash); + } + } + + // Get the local app data directory and if a vendor name exists append it. + // If only a product name exists, append it. If neither exist fallback to + // old handling. We don't use the product name on purpose because we want a + // shared update directory for different apps run from the same path (like + // Metro & Desktop). + nsCOMPtr localDir; + if (pathHashResult && (gAppData->vendor || gAppData->name) && + NS_SUCCEEDED(GetUserDataDirectoryHome(getter_AddRefs(localDir), true)) && + NS_SUCCEEDED(localDir->AppendNative(nsDependentCString(gAppData->vendor ? + gAppData->vendor : gAppData->name))) && + NS_SUCCEEDED(localDir->Append(NS_LITERAL_STRING("updates"))) && + NS_SUCCEEDED(localDir->Append(pathHash))) { + NS_ADDREF(*aResult = localDir); + return NS_OK; + } + nsAutoString appPath; rv = updRoot->GetPath(appPath); NS_ENSURE_SUCCESS(rv, rv);