Planeshift

crash_report_sender.h

Go to the documentation of this file.
00001 // Copyright (c) 2006, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 
00030 #ifndef CLIENT_WINDOWS_SENDER_CRASH_REPORT_SENDER_H__
00031 #define CLIENT_WINDOWS_SENDER_CRASH_REPORT_SENDER_H__
00032 
00033 // CrashReportSender is a "static" class which provides an API to upload
00034 // crash reports via HTTP(S).  A crash report is formatted as a multipart POST
00035 // request, which contains a set of caller-supplied string key/value pairs,
00036 // and a minidump file to upload.
00037 //
00038 // To use this library in your project, you will need to link against
00039 // wininet.lib.
00040 
00041 #pragma warning( push )
00042 // Disable exception handler warnings.
00043 #pragma warning( disable : 4530 ) 
00044 
00045 #include <map>
00046 #include <string>
00047 
00048 namespace google_breakpad {
00049 
00050 using std::wstring;
00051 using std::map;
00052 
00053 typedef enum {
00054   RESULT_FAILED = 0,  // Failed to communicate with the server; try later.
00055   RESULT_REJECTED,    // Successfully sent the crash report, but the
00056                       // server rejected it; don't resend this report.
00057   RESULT_SUCCEEDED,   // The server accepted the crash report.
00058   RESULT_THROTTLED    // No attempt was made to send the crash report, because
00059                       // we exceeded the maximum reports per day.
00060 } ReportResult;
00061 
00062 class CrashReportSender {
00063  public:
00064   // Initializes a CrashReportSender instance.
00065   // If checkpoint_file is non-empty, breakpad will persist crash report
00066   // state to this file.  A checkpoint file is required for
00067   // set_max_reports_per_day() to function properly.
00068   explicit CrashReportSender(const wstring &checkpoint_file);
00069   ~CrashReportSender() {}
00070 
00071   // Sets the maximum number of crash reports that will be sent in a 24-hour
00072   // period.  This uses the state persisted to the checkpoint file.
00073   // The default value of -1 means that there is no limit on reports sent.
00074   void set_max_reports_per_day(int reports) {
00075     max_reports_per_day_ = reports;
00076   }
00077 
00078   int max_reports_per_day() const { return max_reports_per_day_; }
00079 
00080   // Sends the specified minidump file, along with the map of
00081   // name value pairs, as a multipart POST request to the given URL.
00082   // Parameter names must contain only printable ASCII characters,
00083   // and may not contain a quote (") character.
00084   // Only HTTP(S) URLs are currently supported.  The return value indicates
00085   // the result of the operation (see above for possible results).
00086   // If report_code is non-NULL and the report is sent successfully (that is,
00087   // the return value is RESULT_SUCCEEDED), a code uniquely identifying the
00088   // report will be returned in report_code.
00089   // (Otherwise, report_code will be unchanged.)
00090   ReportResult SendCrashReport(const wstring &url,
00091                                const map<wstring, wstring> &parameters,
00092                                const wstring &dump_file_name,
00093                                wstring *report_code);
00094 
00095  private:
00096   // Reads persistent state from a checkpoint file.
00097   void ReadCheckpoint(FILE *fd);
00098 
00099   // Called when a new report has been sent, to update the checkpoint state.
00100   void ReportSent(int today);
00101 
00102   // Returns today's date (UTC) formatted as YYYYMMDD.
00103   int GetCurrentDate() const;
00104 
00105   // Opens the checkpoint file with the specified mode.
00106   // Returns zero on success, or an error code on failure.
00107   int OpenCheckpointFile(const wchar_t *mode, FILE **fd);
00108 
00109   wstring checkpoint_file_;
00110   int max_reports_per_day_;
00111   // The last date on which we sent a report, expressed as YYYYMMDD.
00112   int last_sent_date_;
00113   // Number of reports sent on last_sent_date_
00114   int reports_sent_;
00115 
00116   // Disallow copy constructor and operator=
00117   explicit CrashReportSender(const CrashReportSender &);
00118   void operator=(const CrashReportSender &);
00119 };
00120 
00121 }  // namespace google_breakpad
00122 
00123 #pragma warning( pop )
00124 
00125 #endif  // CLIENT_WINDOWS_SENDER_CRASH_REPORT_SENDER_H__