/thirdparty/breakpad/client/windows/sender/crash_report_sender.cc

http://github.com/tomahawk-player/tomahawk · C++ · 142 lines · 92 code · 17 blank · 33 comment · 23 complexity · fd4da8d7b3657346843570ac366574e7 MD5 · raw 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. // Disable exception handler warnings.
  30. #pragma warning( disable : 4530 )
  31. #include <errno.h>
  32. #include "client/windows/sender/crash_report_sender.h"
  33. #include "common/windows/http_upload.h"
  34. #if _MSC_VER < 1400 // MSVC 2005/8
  35. // Older MSVC doesn't have fscanf_s, but they are compatible as long as
  36. // we don't use the string conversions (%s/%c/%S/%C).
  37. #define fscanf_s fscanf
  38. #endif
  39. namespace google_breakpad {
  40. static const char kCheckpointSignature[] = "GBP1\n";
  41. CrashReportSender::CrashReportSender(const wstring &checkpoint_file)
  42. : checkpoint_file_(checkpoint_file),
  43. max_reports_per_day_(-1),
  44. last_sent_date_(-1),
  45. reports_sent_(0) {
  46. FILE *fd;
  47. if (OpenCheckpointFile(L"r", &fd) == 0) {
  48. ReadCheckpoint(fd);
  49. fclose(fd);
  50. }
  51. }
  52. ReportResult CrashReportSender::SendCrashReport(
  53. const wstring &url, const map<wstring, wstring> &parameters,
  54. const wstring &dump_file_name, wstring *report_code) {
  55. int today = GetCurrentDate();
  56. if (today == last_sent_date_ &&
  57. max_reports_per_day_ != -1 &&
  58. reports_sent_ >= max_reports_per_day_) {
  59. return RESULT_THROTTLED;
  60. }
  61. int http_response = 0;
  62. bool result = HTTPUpload::SendRequest(
  63. url, parameters, dump_file_name, L"upload_file_minidump", NULL, report_code,
  64. &http_response);
  65. if (result) {
  66. ReportSent(today);
  67. return RESULT_SUCCEEDED;
  68. } else if (http_response >= 400 && http_response < 500) {
  69. return RESULT_REJECTED;
  70. } else {
  71. return RESULT_FAILED;
  72. }
  73. }
  74. void CrashReportSender::ReadCheckpoint(FILE *fd) {
  75. char buf[128];
  76. if (!fgets(buf, sizeof(buf), fd) ||
  77. strcmp(buf, kCheckpointSignature) != 0) {
  78. return;
  79. }
  80. if (fscanf_s(fd, "%d\n", &last_sent_date_) != 1) {
  81. last_sent_date_ = -1;
  82. return;
  83. }
  84. if (fscanf_s(fd, "%d\n", &reports_sent_) != 1) {
  85. reports_sent_ = 0;
  86. return;
  87. }
  88. }
  89. void CrashReportSender::ReportSent(int today) {
  90. // Update the report stats
  91. if (today != last_sent_date_) {
  92. last_sent_date_ = today;
  93. reports_sent_ = 0;
  94. }
  95. ++reports_sent_;
  96. // Update the checkpoint file
  97. FILE *fd;
  98. if (OpenCheckpointFile(L"w", &fd) == 0) {
  99. fputs(kCheckpointSignature, fd);
  100. fprintf(fd, "%d\n", last_sent_date_);
  101. fprintf(fd, "%d\n", reports_sent_);
  102. fclose(fd);
  103. }
  104. }
  105. int CrashReportSender::GetCurrentDate() const {
  106. SYSTEMTIME system_time;
  107. GetSystemTime(&system_time);
  108. return (system_time.wYear * 10000) + (system_time.wMonth * 100) +
  109. system_time.wDay;
  110. }
  111. int CrashReportSender::OpenCheckpointFile(const wchar_t *mode, FILE **fd) {
  112. if (checkpoint_file_.empty()) {
  113. return ENOENT;
  114. }
  115. #if _MSC_VER >= 1400 // MSVC 2005/8
  116. return _wfopen_s(fd, checkpoint_file_.c_str(), mode);
  117. #else
  118. *fd = _wfopen(checkpoint_file_.c_str(), mode);
  119. if (*fd == NULL) {
  120. return errno;
  121. }
  122. return 0;
  123. #endif
  124. }
  125. } // namespace google_breakpad