1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #ifndef CRASHREPORTER_H__
6 #define CRASHREPORTER_H__
7 
8 #ifdef _MSC_VER
9 #  pragma warning(push)
10 // Disable exception handler warnings.
11 #  pragma warning(disable : 4530)
12 #endif
13 
14 #include <string>
15 #include <map>
16 #include <vector>
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <iostream>
20 #include <fstream>
21 
22 #define MAX_COMMENT_LENGTH 10000
23 
24 #if defined(XP_WIN)
25 
26 #  include <windows.h>
27 
28 #  define UI_SNPRINTF _snprintf
29 #  define UI_DIR_SEPARATOR "\\"
30 
31 std::string WideToUTF8(const std::wstring& wide, bool* success = 0);
32 
33 #else
34 
35 #  define UI_SNPRINTF snprintf
36 #  define UI_DIR_SEPARATOR "/"
37 
38 #endif
39 
40 #include "json/json.h"
41 
42 #define UI_CRASH_REPORTER_FILENAME "crashreporter"
43 #define UI_MINIDUMP_ANALYZER_FILENAME "minidump-analyzer"
44 #define UI_PING_SENDER_FILENAME "pingsender"
45 
46 typedef std::map<std::string, std::string> StringTable;
47 
48 #define ST_CRASHREPORTERTITLE "CrashReporterTitle"
49 #define ST_CRASHREPORTERVENDORTITLE "CrashReporterVendorTitle"
50 #define ST_CRASHREPORTERERROR "CrashReporterErrorText"
51 #define ST_CRASHREPORTERPRODUCTERROR "CrashReporterProductErrorText2"
52 #define ST_CRASHREPORTERHEADER "CrashReporterSorry"
53 #define ST_CRASHREPORTERDESCRIPTION "CrashReporterDescriptionText2"
54 #define ST_CRASHREPORTERDEFAULT "CrashReporterDefault"
55 #define ST_VIEWREPORT "Details"
56 #define ST_VIEWREPORTTITLE "ViewReportTitle"
57 #define ST_COMMENTGRAYTEXT "CommentGrayText"
58 #define ST_EXTRAREPORTINFO "ExtraReportInfo"
59 #define ST_CHECKSUBMIT "CheckSendReport"
60 #define ST_CHECKURL "CheckIncludeURL"
61 #define ST_REPORTPRESUBMIT "ReportPreSubmit2"
62 #define ST_REPORTDURINGSUBMIT "ReportDuringSubmit2"
63 #define ST_REPORTSUBMITSUCCESS "ReportSubmitSuccess"
64 #define ST_SUBMITFAILED "ReportSubmitFailed"
65 #define ST_QUIT "Quit2"
66 #define ST_RESTART "Restart"
67 #define ST_OK "Ok"
68 #define ST_CLOSE "Close"
69 
70 #define ST_ERROR_BADARGUMENTS "ErrorBadArguments"
71 #define ST_ERROR_EXTRAFILEEXISTS "ErrorExtraFileExists"
72 #define ST_ERROR_EXTRAFILEREAD "ErrorExtraFileRead"
73 #define ST_ERROR_EXTRAFILEMOVE "ErrorExtraFileMove"
74 #define ST_ERROR_DUMPFILEEXISTS "ErrorDumpFileExists"
75 #define ST_ERROR_DUMPFILEMOVE "ErrorDumpFileMove"
76 #define ST_ERROR_NOPRODUCTNAME "ErrorNoProductName"
77 #define ST_ERROR_NOSERVERURL "ErrorNoServerURL"
78 #define ST_ERROR_NOSETTINGSPATH "ErrorNoSettingsPath"
79 #define ST_ERROR_CREATEDUMPDIR "ErrorCreateDumpDir"
80 #define ST_ERROR_ENDOFLIFE "ErrorEndOfLife"
81 
82 //=============================================================================
83 // implemented in crashreporter.cpp and ping.cpp
84 //=============================================================================
85 
86 namespace CrashReporter {
87 extern StringTable gStrings;
88 extern std::string gSettingsPath;
89 extern std::string gEventsPath;
90 extern int gArgc;
91 extern char** gArgv;
92 extern bool gAutoSubmit;
93 
94 void UIError(const std::string& message);
95 
96 // The UI finished sending the report
97 void SendCompleted(bool success, const std::string& serverResponse);
98 
99 bool ReadStrings(std::istream& in, StringTable& strings, bool unescape);
100 bool ReadStringsFromFile(const std::string& path, StringTable& strings,
101                          bool unescape);
102 void LogMessage(const std::string& message);
103 void DeleteDump();
104 
105 // Telemetry ping
106 bool SendCrashPing(Json::Value& extra, const std::string& hash,
107                    std::string& pingUuid, const std::string& pingDir);
108 
109 static const unsigned int kSaveCount = 10;
110 }  // namespace CrashReporter
111 
112 //=============================================================================
113 // implemented in the platform-specific files
114 //=============================================================================
115 
116 bool UIInit();
117 void UIShutdown();
118 
119 // Run the UI for when the app was launched without a dump file
120 void UIShowDefaultUI();
121 
122 // Run the UI for when the app was launched with a dump file
123 // Return true if the user sent (or tried to send) the crash report,
124 // false if they chose not to, and it should be deleted.
125 bool UIShowCrashUI(const StringTable& files, const Json::Value& queryParameters,
126                    const std::string& sendURL,
127                    const std::vector<std::string>& restartArgs);
128 
129 void UIError_impl(const std::string& message);
130 
131 bool UIGetIniPath(std::string& path);
132 bool UIGetSettingsPath(const std::string& vendor, const std::string& product,
133                        std::string& settingsPath);
134 bool UIEnsurePathExists(const std::string& path);
135 bool UIFileExists(const std::string& path);
136 bool UIMoveFile(const std::string& oldfile, const std::string& newfile);
137 bool UIDeleteFile(const std::string& oldfile);
138 std::ifstream* UIOpenRead(const std::string& filename,
139                           std::ios_base::openmode mode);
140 std::ofstream* UIOpenWrite(const std::string& filename,
141                            std::ios_base::openmode mode);
142 void UIPruneSavedDumps(const std::string& directory);
143 
144 // Run the program specified by exename, passing it the parameters in arg.
145 // If wait is true, wait for the program to terminate execution before
146 // returning. Returns true if the program was launched correctly, false
147 // otherwise.
148 bool UIRunProgram(const std::string& exename,
149                   const std::vector<std::string>& args, bool wait = false);
150 
151 // Read the environment variable specified by name
152 std::string UIGetEnv(const std::string& name);
153 
154 #ifdef _MSC_VER
155 #  pragma warning(pop)
156 #endif
157 
158 #endif
159