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