ErrorHandler.cpp (2164B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ 6 7 #include "ErrorHandler.h" 8 9 #include "mozilla/CmdLineAndEnvUtils.h" 10 #include "mozilla/UniquePtr.h" 11 12 #if defined(MOZ_LAUNCHER_PROCESS) 13 # include "mozilla/LauncherRegistryInfo.h" 14 #endif // defined(MOZ_LAUNCHER_PROCESS) 15 16 namespace { 17 18 constexpr wchar_t kEventSourceName[] = L"" MOZ_APP_DISPLAYNAME " Launcher"; 19 20 struct EventSourceDeleter { 21 using pointer = HANDLE; 22 23 void operator()(pointer aEvtSrc) { ::DeregisterEventSource(aEvtSrc); } 24 }; 25 26 using EventLog = mozilla::UniquePtr<HANDLE, EventSourceDeleter>; 27 28 struct SerializedEventData { 29 HRESULT mHr; 30 uint32_t mLine; 31 char mFile[1]; 32 }; 33 34 } // anonymous namespace 35 36 static void PostErrorToLog(const mozilla::LauncherError& aError) { 37 // This is very bare-bones; just enough to spit out an HRESULT to the 38 // Application event log. 39 EventLog log(::RegisterEventSourceW(nullptr, kEventSourceName)); 40 41 if (!log) { 42 return; 43 } 44 45 size_t fileLen = strlen(aError.mFile); 46 size_t dataLen = sizeof(HRESULT) + sizeof(uint32_t) + fileLen; 47 auto evtDataBuf = mozilla::MakeUnique<char[]>(dataLen); 48 SerializedEventData& evtData = 49 *reinterpret_cast<SerializedEventData*>(evtDataBuf.get()); 50 evtData.mHr = aError.mError.AsHResult(); 51 evtData.mLine = aError.mLine; 52 // Since this is binary data, we're not concerning ourselves with null 53 // terminators. 54 memcpy(evtData.mFile, aError.mFile, fileLen); 55 56 ::ReportEventW(log.get(), EVENTLOG_ERROR_TYPE, 0, aError.mError.AsHResult(), 57 nullptr, 0, dataLen, nullptr, evtDataBuf.get()); 58 } 59 60 namespace mozilla { 61 62 void HandleLauncherError(const LauncherError& aError, 63 const char* aProcessType) { 64 #if defined(MOZ_LAUNCHER_PROCESS) 65 LauncherRegistryInfo regInfo; 66 (void)regInfo.DisableDueToFailure(); 67 #endif // defined(MOZ_LAUNCHER_PROCESS) 68 69 PostErrorToLog(aError); 70 } 71 72 } // namespace mozilla