/reporting/crashsender/ErrorReportSender.h

http://crashrpt.googlecode.com/ · C Header · 234 lines · 106 code · 61 blank · 67 comment · 0 complexity · 02ed32250aa01108b69522dfe5131868 MD5 · raw file

  1. /*************************************************************************************
  2. This file is a part of CrashRpt library.
  3. Copyright (c) 2003-2013 The CrashRpt project authors. All Rights Reserved.
  4. Use of this source code is governed by a BSD-style license
  5. that can be found in the License.txt file in the root of the source
  6. tree. All contributing project authors may
  7. be found in the Authors.txt file in the root of the source tree.
  8. ***************************************************************************************/
  9. #pragma once
  10. #include "AssyncNotification.h"
  11. #include "MailMsg.h"
  12. #include "smtpclient.h"
  13. #include "HttpRequestSender.h"
  14. #include "tinyxml.h"
  15. #include "CrashInfoReader.h"
  16. #include "VideoRec.h"
  17. // Action type
  18. enum ActionType
  19. {
  20. COLLECT_CRASH_INFO = 0x01, // Crash info should be collected.
  21. COMPRESS_REPORT = 0x02, // Error report files should be packed into ZIP archive.
  22. RESTART_APP = 0x04, // Crashed app should be restarted.
  23. SEND_REPORT = 0x08, // Report should be sent.
  24. SEND_RECENT_REPORTS = 0x10 // Recent crash reports should be sent.
  25. };
  26. // Mail client launch confirmation status
  27. enum eMailClientConfirm
  28. {
  29. NOT_CONFIRMED_YET, // User didn't confirm yet
  30. ALLOWED, // User allowed mail client launch
  31. NOT_ALLOWED // User didn't allow mail client launch
  32. };
  33. // Messages sent to GUI buy the sender
  34. #define WM_NEXT_ITEM_HINT (WM_USER+1023)
  35. #define WM_ITEM_STATUS_CHANGED (WM_USER+1024)
  36. #define WM_DELIVERY_COMPLETE (WM_USER+1025)
  37. // The main class that collects crash report files, packs them
  38. // into a ZIP archive and sends the error report.
  39. class CErrorReportSender
  40. {
  41. public:
  42. // Constructor.
  43. CErrorReportSender();
  44. // Destructor.
  45. virtual ~CErrorReportSender();
  46. // Returns singleton of this class.
  47. static CErrorReportSender* GetInstance();
  48. // Performs initialization.
  49. BOOL Init(LPCTSTR szFileMappingName);
  50. // Cleans up all temp files and does other finalizing work.
  51. BOOL Finalize();
  52. // Returns pointer to object containing crash information.
  53. CCrashInfoReader* GetCrashInfo();
  54. // Returns last error message.
  55. CString GetErrorMsg();
  56. // Set the window that will receive notifications from this object.
  57. void SetNotificationWindow(HWND hWnd);
  58. // Compresses and sends the report(s).
  59. BOOL Run();
  60. // Blocks until an assync operation finishes.
  61. void WaitForCompletion();
  62. // Cancels the assync operation.
  63. void Cancel();
  64. // Returns error report sending status.
  65. int GetStatus();
  66. // Gets current operation status.
  67. void GetCurOpStatus(int& nProgressPct, std::vector<CString>& msg_log);
  68. // Unblocks waiting worker thread.
  69. void FeedbackReady(int code);
  70. // Returns current error report's index.
  71. int GetCurReport();
  72. // Returns path to log file.
  73. CString GetLogFilePath();
  74. // Returns a localized string from lang file.
  75. CString GetLangStr(LPCTSTR szSection, LPCTSTR szName);
  76. // Allows to specify file name for exporting error report.
  77. void SetExportFlag(BOOL bExport, CString sExportFile);
  78. // Exports crash report to disc as a ZIP archive.
  79. void ExportReport(LPCTSTR szOutFileName);
  80. // Returns TRUE if currently sending error report(s).
  81. BOOL IsSendingNow();
  82. // Returns TRUE if there were errors.
  83. BOOL HasErrors();
  84. // This method finds and terminates all instances of CrashSender.exe process.
  85. static int TerminateAllCrashSenderProcesses();
  86. private:
  87. // Creates log file
  88. BOOL InitLog();
  89. // This method performs an action or several actions.
  90. BOOL DoWork(int Action);
  91. // Worker thread proc.
  92. static DWORD WINAPI WorkerThread(LPVOID lpParam);
  93. // Runs an action or several actions in assync mode.
  94. BOOL DoWorkAssync(int Action);
  95. // Collects crash report files.
  96. BOOL CollectCrashFiles();
  97. // Includes a single file to crash report
  98. BOOL CollectSingleFile(ERIFileItem* pfi);
  99. // Includes all files matching search pattern to crash report
  100. BOOL CollectFilesBySearchTemplate(ERIFileItem* pfi, std::vector<ERIFileItem>& file_list);
  101. // Calculates MD5 hash for a file.
  102. int CalcFileMD5Hash(CString sFileName, CString& sMD5Hash);
  103. // Takes desktop screenshot.
  104. BOOL TakeDesktopScreenshot();
  105. // This method enters the video recording loop.
  106. BOOL RecordVideo();
  107. // Writes video to a webm file
  108. BOOL EncodeVideo();
  109. // Creates crash dump file.
  110. BOOL CreateMiniDump();
  111. // This method is used to have the current process be able to call MiniDumpWriteDump.
  112. BOOL SetDumpPrivileges();
  113. // Creates crash description XML file.
  114. BOOL CreateCrashDescriptionXML(CErrorReportInfo& eri);
  115. // Adds an element to XML file.
  116. void AddElemToXML(CString sName, CString sValue, TiXmlNode* root);
  117. // Minidump callback.
  118. static BOOL CALLBACK MiniDumpCallback(PVOID CallbackParam, PMINIDUMP_CALLBACK_INPUT CallbackInput,
  119. PMINIDUMP_CALLBACK_OUTPUT CallbackOutput);
  120. // Minidump callback.
  121. BOOL OnMinidumpProgress(const PMINIDUMP_CALLBACK_INPUT CallbackInput,
  122. PMINIDUMP_CALLBACK_OUTPUT CallbackOutput);
  123. // Restarts the application.
  124. BOOL RestartApp();
  125. // Dumps registry key to the XML file.
  126. int DumpRegKey(CString sRegKey, CString sDestFile, CString& sErrorMsg);
  127. // Used internally for dumping a registry key.
  128. int DumpRegKey(HKEY hKeyParent, CString sSubKey, TiXmlElement* elem);
  129. // Packs error report files to ZIP archive.
  130. BOOL CompressReportFiles(CErrorReportInfo* eri);
  131. // Unblocks parent process.
  132. void UnblockParentProcess();
  133. // Sends error report.
  134. BOOL SendReport();
  135. // Sends error report over HTTP.
  136. BOOL SendOverHTTP();
  137. // Encodes attachment file with Base-64 encoding.
  138. int Base64EncodeAttachment(CString sFileName, std::string& sEncodedFileData);
  139. // Formats Email text.
  140. CString FormatEmailText();
  141. // Sends error report over SMTP.
  142. BOOL SendOverSMTP();
  143. // Sends error report over Simple MAPI.
  144. BOOL SendOverSMAPI();
  145. // Sends all recently queued error reports in turn.
  146. BOOL SendRecentReports();
  147. // Send the next queued report.
  148. BOOL SendNextReport(int nReport);
  149. // Internal variables
  150. static CErrorReportSender* m_pInstance; // Singleton
  151. CCrashInfoReader m_CrashInfo; // Contains crash information.
  152. CVideoRecorder m_VideoRec; // Video recorder.
  153. CString m_sErrorMsg; // Last error message.
  154. HWND m_hWndNotify; // Notification window.
  155. int m_nStatus; // Error report sending status.
  156. int m_nCurReport; // Index of current error report.
  157. HANDLE m_hThread; // Handle to the worker thread.
  158. int m_SendAttempt; // Number of current sending attempt.
  159. AssyncNotification m_Assync; // Used for communication with the main thread.
  160. CEmailMessage m_EmailMsg; // Email message to send.
  161. CSmtpClient m_SmtpClient; // Used to send report over SMTP.
  162. CHttpRequestSender m_HttpSender; // Used to send report over HTTP.
  163. CMailMsg m_MapiSender; // Used to send report over SMAPI.
  164. CString m_sZipName; // Name of the ZIP archive to send.
  165. int m_Action; // Current assynchronous action.
  166. BOOL m_bExport; // If TRUE than export should be performed.
  167. CString m_sExportFileName; // File name for exporting.
  168. eMailClientConfirm m_MailClientConfirm; // Mail program confirmation result.
  169. BOOL m_bSendingNow; // TRUE if in progress of sending reports.
  170. BOOL m_bErrors; // TRUE if there were errors.
  171. CString m_sCrashLogFile; // Log file.
  172. };