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