crash_upload_list_win.cc revision ddb351dbec246cf1fab5ec20d2d5520909041de1
1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/crash_upload_list_win.h"
6
7#include "base/stringprintf.h"
8#include "base/string_util.h"
9#include "base/sys_string_conversions.h"
10
11CrashUploadListWin::CrashUploadListWin(Delegate* delegate)
12    : CrashUploadList(delegate) {}
13
14void CrashUploadListWin::LoadCrashList() {
15  std::vector<uint8> buffer(1024);
16  HANDLE event_log = OpenEventLog(NULL, L"Application");
17  if (event_log) {
18    while (true) {
19      DWORD bytes_read;
20      DWORD bytes_needed;
21      BOOL success =
22          ReadEventLog(event_log,
23                       EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ,
24                       0,
25                       &buffer[0],
26                       buffer.size(),
27                       &bytes_read,
28                       &bytes_needed);
29      if (success) {
30        DWORD record_offset = 0;
31        // The ReadEventLog() API docs imply, but do not explicitly state that
32        // partial records will not be returned. Use DCHECK() to affirm this.
33        while (record_offset < bytes_read) {
34          DCHECK(record_offset + sizeof(EVENTLOGRECORD) <= bytes_read);
35          EVENTLOGRECORD* record = (EVENTLOGRECORD*)&buffer[record_offset];
36          DCHECK(record_offset + record->Length <= bytes_read);
37          if (IsPossibleCrashLogRecord(record))
38            ProcessPossibleCrashLogRecord(record);
39          record_offset += record->Length;
40        }
41      } else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
42        // Resize buffer to the required minimum size.
43        buffer.resize(bytes_needed);
44      } else {
45        // Stop on any other error, including the expected case
46        // of ERROR_HANDLE_EOF.
47        DCHECK(GetLastError() == ERROR_HANDLE_EOF);
48        break;
49      }
50    }
51    CloseEventLog(event_log);
52  }
53}
54
55bool CrashUploadListWin::IsPossibleCrashLogRecord(
56    EVENTLOGRECORD* record) const {
57  LPWSTR provider_name = (LPWSTR)((uint8*)record + sizeof(EVENTLOGRECORD));
58  return !wcscmp(L"Chrome", provider_name) &&
59      record->EventType == EVENTLOG_INFORMATION_TYPE &&
60      record->NumStrings >= 1;
61}
62
63void CrashUploadListWin::ProcessPossibleCrashLogRecord(EVENTLOGRECORD* record) {
64  // Add the crash if the message matches the expected pattern.
65  const std::wstring pattern_prefix(L"Id=");
66  const std::wstring pattern_suffix(L".");
67  std::wstring message((LPWSTR)((uint8*)record + record->StringOffset));
68  size_t start_index = message.find(pattern_prefix);
69  if (start_index != std::wstring::npos) {
70    start_index += pattern_prefix.size();
71    size_t end_index = message.find(pattern_suffix, start_index);
72    if (end_index != std::wstring::npos) {
73      std::wstring crash_id =
74          message.substr(start_index, end_index - start_index);
75      crashes().push_back(
76          CrashInfo(base::SysWideToUTF8(crash_id),
77                    base::Time::FromDoubleT(record->TimeGenerated)));
78    }
79  }
80}
81