|
| 1 | +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 | + |
| 6 | +#include <windows.h> |
| 7 | +#include <shlobj.h> // for SHChangeNotify and IApplicationAssociationRegistration |
| 8 | + |
| 9 | +#include "mozilla/ArrayUtils.h" |
| 10 | +#include "mozilla/RefPtr.h" |
| 11 | +#include "mozilla/UniquePtr.h" |
| 12 | +#include "mozilla/WinHeaderOnlyUtils.h" |
| 13 | +#include "WindowsUserChoice.h" |
| 14 | + |
| 15 | +#include "EventLog.h" |
| 16 | +#include "SetDefaultBrowser.h" |
| 17 | + |
| 18 | +static bool AddMillisecondsToSystemTime(SYSTEMTIME& aSystemTime, |
| 19 | + ULONGLONG aIncrementMS) { |
| 20 | + FILETIME fileTime; |
| 21 | + ULARGE_INTEGER fileTimeInt; |
| 22 | + if (!::SystemTimeToFileTime(&aSystemTime, &fileTime)) { |
| 23 | + return false; |
| 24 | + } |
| 25 | + fileTimeInt.LowPart = fileTime.dwLowDateTime; |
| 26 | + fileTimeInt.HighPart = fileTime.dwHighDateTime; |
| 27 | + |
| 28 | + // FILETIME is in units of 100ns. |
| 29 | + fileTimeInt.QuadPart += aIncrementMS * 1000 * 10; |
| 30 | + |
| 31 | + fileTime.dwLowDateTime = fileTimeInt.LowPart; |
| 32 | + fileTime.dwHighDateTime = fileTimeInt.HighPart; |
| 33 | + SYSTEMTIME tmpSystemTime; |
| 34 | + if (!::FileTimeToSystemTime(&fileTime, &tmpSystemTime)) { |
| 35 | + return false; |
| 36 | + } |
| 37 | + |
| 38 | + aSystemTime = tmpSystemTime; |
| 39 | + return true; |
| 40 | +} |
| 41 | + |
| 42 | +// Compare two SYSTEMTIMEs as FILETIME after clearing everything |
| 43 | +// below minutes. |
| 44 | +static bool CheckEqualMinutes(SYSTEMTIME aSystemTime1, |
| 45 | + SYSTEMTIME aSystemTime2) { |
| 46 | + aSystemTime1.wSecond = 0; |
| 47 | + aSystemTime1.wMilliseconds = 0; |
| 48 | + |
| 49 | + aSystemTime2.wSecond = 0; |
| 50 | + aSystemTime2.wMilliseconds = 0; |
| 51 | + |
| 52 | + FILETIME fileTime1; |
| 53 | + FILETIME fileTime2; |
| 54 | + if (!::SystemTimeToFileTime(&aSystemTime1, &fileTime1) || |
| 55 | + !::SystemTimeToFileTime(&aSystemTime2, &fileTime2)) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + return (fileTime1.dwLowDateTime == fileTime2.dwLowDateTime) && |
| 60 | + (fileTime1.dwHighDateTime == fileTime2.dwHighDateTime); |
| 61 | +} |
| 62 | + |
| 63 | +/* |
| 64 | + * Set an association with a UserChoice key |
| 65 | + * |
| 66 | + * Removes the old key, creates a new one with ProgID and Hash set to |
| 67 | + * enable a new asociation. |
| 68 | + * |
| 69 | + * @param aExt File type or protocol to associate |
| 70 | + * @param aSid Current user's string SID |
| 71 | + * @param aProgID ProgID to use for the asociation |
| 72 | + * |
| 73 | + * @return true if successful, false on error. |
| 74 | + */ |
| 75 | +static bool SetUserChoice(const wchar_t* aExt, const wchar_t* aSid, |
| 76 | + const wchar_t* aProgID) { |
| 77 | + SYSTEMTIME hashTimestamp; |
| 78 | + ::GetSystemTime(&hashTimestamp); |
| 79 | + auto hash = GenerateUserChoiceHash(aExt, aSid, aProgID, hashTimestamp); |
| 80 | + if (!hash) { |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + // The hash changes at the end of each minute, so check that the hash should |
| 85 | + // be the same by the time we're done writing. |
| 86 | + const ULONGLONG kWriteTimingThresholdMilliseconds = 100; |
| 87 | + // Generating the hash could have taken some time, so start from now. |
| 88 | + SYSTEMTIME writeEndTimestamp; |
| 89 | + ::GetSystemTime(&writeEndTimestamp); |
| 90 | + if (!AddMillisecondsToSystemTime(writeEndTimestamp, |
| 91 | + kWriteTimingThresholdMilliseconds)) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + if (!CheckEqualMinutes(hashTimestamp, writeEndTimestamp)) { |
| 95 | + LOG_ERROR_MESSAGE( |
| 96 | + L"Hash is too close to expiration, sleeping until next hash."); |
| 97 | + ::Sleep(kWriteTimingThresholdMilliseconds * 2); |
| 98 | + |
| 99 | + // For consistency, use the current time. |
| 100 | + ::GetSystemTime(&hashTimestamp); |
| 101 | + hash = GenerateUserChoiceHash(aExt, aSid, aProgID, hashTimestamp); |
| 102 | + if (!hash) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + auto assocKeyPath = GetAssociationKeyPath(aExt); |
| 108 | + if (!assocKeyPath) { |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + LSTATUS ls; |
| 113 | + HKEY rawAssocKey; |
| 114 | + ls = ::RegOpenKeyExW(HKEY_CURRENT_USER, assocKeyPath.get(), 0, |
| 115 | + KEY_READ | KEY_WRITE, &rawAssocKey); |
| 116 | + if (ls != ERROR_SUCCESS) { |
| 117 | + LOG_ERROR(HRESULT_FROM_WIN32(ls)); |
| 118 | + return false; |
| 119 | + } |
| 120 | + nsAutoRegKey assocKey(rawAssocKey); |
| 121 | + |
| 122 | + // When Windows creates this key, it is read-only (Deny Set Value), so we need |
| 123 | + // to delete it first. |
| 124 | + // We don't set any similar special permissions. |
| 125 | + ls = ::RegDeleteKeyW(assocKey.get(), L"UserChoice"); |
| 126 | + if (ls != ERROR_SUCCESS) { |
| 127 | + LOG_ERROR(HRESULT_FROM_WIN32(ls)); |
| 128 | + return false; |
| 129 | + } |
| 130 | + |
| 131 | + HKEY rawUserChoiceKey; |
| 132 | + ls = ::RegCreateKeyExW(assocKey.get(), L"UserChoice", 0, nullptr, |
| 133 | + 0 /* options */, KEY_READ | KEY_WRITE, |
| 134 | + 0 /* security attributes */, &rawUserChoiceKey, |
| 135 | + nullptr); |
| 136 | + if (ls != ERROR_SUCCESS) { |
| 137 | + LOG_ERROR(HRESULT_FROM_WIN32(ls)); |
| 138 | + return false; |
| 139 | + } |
| 140 | + nsAutoRegKey userChoiceKey(rawUserChoiceKey); |
| 141 | + |
| 142 | + DWORD progIdByteCount = (::lstrlenW(aProgID) + 1) * sizeof(wchar_t); |
| 143 | + ls = ::RegSetValueExW(userChoiceKey.get(), L"ProgID", 0, REG_SZ, |
| 144 | + reinterpret_cast<const unsigned char*>(aProgID), |
| 145 | + progIdByteCount); |
| 146 | + if (ls != ERROR_SUCCESS) { |
| 147 | + LOG_ERROR(HRESULT_FROM_WIN32(ls)); |
| 148 | + return false; |
| 149 | + } |
| 150 | + |
| 151 | + DWORD hashByteCount = (::lstrlenW(hash.get()) + 1) * sizeof(wchar_t); |
| 152 | + ls = ::RegSetValueExW(userChoiceKey.get(), L"Hash", 0, REG_SZ, |
| 153 | + reinterpret_cast<const unsigned char*>(hash.get()), |
| 154 | + hashByteCount); |
| 155 | + if (ls != ERROR_SUCCESS) { |
| 156 | + LOG_ERROR(HRESULT_FROM_WIN32(ls)); |
| 157 | + return false; |
| 158 | + } |
| 159 | + |
| 160 | + return true; |
| 161 | +} |
| 162 | + |
| 163 | +static bool VerifyUserDefault(const wchar_t* aExt, const wchar_t* aProgID) { |
| 164 | + RefPtr<IApplicationAssociationRegistration> pAAR; |
| 165 | + HRESULT hr = ::CoCreateInstance( |
| 166 | + CLSID_ApplicationAssociationRegistration, nullptr, CLSCTX_INPROC, |
| 167 | + IID_IApplicationAssociationRegistration, getter_AddRefs(pAAR)); |
| 168 | + if (FAILED(hr)) { |
| 169 | + LOG_ERROR(hr); |
| 170 | + return false; |
| 171 | + } |
| 172 | + |
| 173 | + wchar_t* rawRegisteredApp; |
| 174 | + bool isProtocol = aExt[0] != L'.'; |
| 175 | + // Note: Checks AL_USER instead of AL_EFFECTIVE. |
| 176 | + hr = pAAR->QueryCurrentDefault(aExt, |
| 177 | + isProtocol ? AT_URLPROTOCOL : AT_FILEEXTENSION, |
| 178 | + AL_USER, &rawRegisteredApp); |
| 179 | + if (FAILED(hr)) { |
| 180 | + if (hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION)) { |
| 181 | + LOG_ERROR_MESSAGE(L"UserChoice ProgID %s for %s was rejected", aProgID, |
| 182 | + aExt); |
| 183 | + } else { |
| 184 | + LOG_ERROR(hr); |
| 185 | + } |
| 186 | + return false; |
| 187 | + } |
| 188 | + mozilla::UniquePtr<wchar_t, mozilla::CoTaskMemFreeDeleter> registeredApp( |
| 189 | + rawRegisteredApp); |
| 190 | + |
| 191 | + if (::CompareStringOrdinal(registeredApp.get(), -1, aProgID, -1, FALSE) != |
| 192 | + CSTR_EQUAL) { |
| 193 | + LOG_ERROR_MESSAGE( |
| 194 | + L"Default was %s after writing ProgID %s to UserChoice for %s", |
| 195 | + registeredApp.get(), aProgID, aExt); |
| 196 | + return false; |
| 197 | + } |
| 198 | + |
| 199 | + return true; |
| 200 | +} |
| 201 | + |
| 202 | +HRESULT SetDefaultBrowserUserChoice(const wchar_t* aAumi) { |
| 203 | + auto urlProgID = FormatProgID(L"FirefoxURL", aAumi); |
| 204 | + if (!CheckProgIDExists(urlProgID.get())) { |
| 205 | + LOG_ERROR_MESSAGE(L"ProgID %s not found", urlProgID.get()); |
| 206 | + return MOZ_E_NO_PROGID; |
| 207 | + } |
| 208 | + |
| 209 | + auto htmlProgID = FormatProgID(L"FirefoxHTML", aAumi); |
| 210 | + if (!CheckProgIDExists(htmlProgID.get())) { |
| 211 | + LOG_ERROR_MESSAGE(L"ProgID %s not found", htmlProgID.get()); |
| 212 | + return MOZ_E_NO_PROGID; |
| 213 | + } |
| 214 | + |
| 215 | + if (!CheckBrowserUserChoiceHashes()) { |
| 216 | + LOG_ERROR_MESSAGE(L"UserChoice Hash mismatch"); |
| 217 | + return MOZ_E_HASH_CHECK; |
| 218 | + } |
| 219 | + |
| 220 | + auto sid = GetCurrentUserStringSid(); |
| 221 | + if (!sid) { |
| 222 | + return E_FAIL; |
| 223 | + } |
| 224 | + |
| 225 | + bool ok = true; |
| 226 | + bool defaultRejected = false; |
| 227 | + |
| 228 | + struct { |
| 229 | + const wchar_t* ext; |
| 230 | + const wchar_t* progID; |
| 231 | + } associations[] = {{L"https", urlProgID.get()}, |
| 232 | + {L"http", urlProgID.get()}, |
| 233 | + {L".html", htmlProgID.get()}, |
| 234 | + {L".htm", htmlProgID.get()}}; |
| 235 | + for (int i = 0; i < mozilla::ArrayLength(associations); ++i) { |
| 236 | + if (!SetUserChoice(associations[i].ext, sid.get(), |
| 237 | + associations[i].progID)) { |
| 238 | + ok = false; |
| 239 | + break; |
| 240 | + } else if (!VerifyUserDefault(associations[i].ext, |
| 241 | + associations[i].progID)) { |
| 242 | + defaultRejected = true; |
| 243 | + ok = false; |
| 244 | + break; |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + // Notify shell to refresh icons |
| 249 | + ::SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nullptr, nullptr); |
| 250 | + |
| 251 | + if (!ok) { |
| 252 | + LOG_ERROR_MESSAGE(L"Failed setting default with %s", aAumi); |
| 253 | + if (defaultRejected) { |
| 254 | + return MOZ_E_REJECTED; |
| 255 | + } |
| 256 | + return E_FAIL; |
| 257 | + } else { |
| 258 | + return S_OK; |
| 259 | + } |
| 260 | +} |
0 commit comments