PageRenderTime 33ms CodeModel.GetById 13ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

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