xref: /aosp_15_r20/external/google-breakpad/src/client/windows/sender/crash_report_sender.h (revision 9712c20fc9bbfbac4935993a2ca0b3958c5adad2)
1 // Copyright 2006 Google LLC
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 //     * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //     * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 //     * Neither the name of Google LLC nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #ifndef CLIENT_WINDOWS_SENDER_CRASH_REPORT_SENDER_H__
30 #define CLIENT_WINDOWS_SENDER_CRASH_REPORT_SENDER_H__
31 
32 // CrashReportSender is a "static" class which provides an API to upload
33 // crash reports via HTTP(S).  A crash report is formatted as a multipart POST
34 // request, which contains a set of caller-supplied string key/value pairs,
35 // and a minidump file to upload.
36 //
37 // To use this library in your project, you will need to link against
38 // wininet.lib.
39 
40 #pragma warning( push )
41 // Disable exception handler warnings.
42 #pragma warning( disable : 4530 )
43 
44 #include <map>
45 #include <string>
46 
47 namespace google_breakpad {
48 
49 using std::wstring;
50 using std::map;
51 
52 typedef enum {
53   RESULT_FAILED = 0,  // Failed to communicate with the server; try later.
54   RESULT_REJECTED,    // Successfully sent the crash report, but the
55                       // server rejected it; don't resend this report.
56   RESULT_SUCCEEDED,   // The server accepted the crash report.
57   RESULT_THROTTLED    // No attempt was made to send the crash report, because
58                       // we exceeded the maximum reports per day.
59 } ReportResult;
60 
61 class CrashReportSender {
62  public:
63   // Initializes a CrashReportSender instance.
64   // If checkpoint_file is non-empty, breakpad will persist crash report
65   // state to this file.  A checkpoint file is required for
66   // set_max_reports_per_day() to function properly.
67   explicit CrashReportSender(const wstring& checkpoint_file);
~CrashReportSender()68   ~CrashReportSender() {}
69 
70   // Sets the maximum number of crash reports that will be sent in a 24-hour
71   // period.  This uses the state persisted to the checkpoint file.
72   // The default value of -1 means that there is no limit on reports sent.
set_max_reports_per_day(int reports)73   void set_max_reports_per_day(int reports) {
74     max_reports_per_day_ = reports;
75   }
76 
max_reports_per_day()77   int max_reports_per_day() const { return max_reports_per_day_; }
78 
79   // Sends the specified files, along with the map of
80   // name value pairs, as a multipart POST request to the given URL.
81   // Parameter names must contain only printable ASCII characters,
82   // and may not contain a quote (") character.
83   // Only HTTP(S) URLs are currently supported.  The return value indicates
84   // the result of the operation (see above for possible results).
85   // If report_code is non-NULL and the report is sent successfully (that is,
86   // the return value is RESULT_SUCCEEDED), a code uniquely identifying the
87   // report will be returned in report_code.
88   // (Otherwise, report_code will be unchanged.)
89   ReportResult SendCrashReport(const wstring& url,
90                                const map<wstring, wstring>& parameters,
91                                const map<wstring, wstring>& files,
92                                wstring* report_code);
93 
94  private:
95   // Reads persistent state from a checkpoint file.
96   void ReadCheckpoint(FILE* fd);
97 
98   // Called when a new report has been sent, to update the checkpoint state.
99   void ReportSent(int today);
100 
101   // Returns today's date (UTC) formatted as YYYYMMDD.
102   int GetCurrentDate() const;
103 
104   // Opens the checkpoint file with the specified mode.
105   // Returns zero on success, or an error code on failure.
106   int OpenCheckpointFile(const wchar_t* mode, FILE** fd);
107 
108   wstring checkpoint_file_;
109   int max_reports_per_day_;
110   // The last date on which we sent a report, expressed as YYYYMMDD.
111   int last_sent_date_;
112   // Number of reports sent on last_sent_date_
113   int reports_sent_;
114 
115   // Disallow copy constructor and operator=
116   explicit CrashReportSender(const CrashReportSender&);
117   void operator=(const CrashReportSender&);
118 };
119 
120 }  // namespace google_breakpad
121 
122 #pragma warning( pop )
123 
124 #endif  // CLIENT_WINDOWS_SENDER_CRASH_REPORT_SENDER_H__
125