blob: 264cfb9cd55d061bb8c50202a71db50c78a84a90 [file] [log] [blame]
[email protected]234e8262012-02-22 21:05:541// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]c8717adf72011-02-18 21:07:162// 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.h"
6
[email protected]234e8262012-02-22 21:05:547#include <algorithm>
[email protected]72e79c12012-03-19 05:24:008#include <iterator>
[email protected]4b5b497e2011-04-03 02:34:279
[email protected]69eb91122011-11-22 21:44:2210#include "base/bind.h"
[email protected]c8717adf72011-02-18 21:07:1611#include "base/file_path.h"
12#include "base/file_util.h"
[email protected]c38831a12011-10-28 12:44:4913#include "base/path_service.h"
[email protected]c8717adf72011-02-18 21:07:1614#include "base/string_number_conversions.h"
15#include "base/string_split.h"
[email protected]b35b1dd2011-04-01 15:58:5016#if defined(OS_WIN)
17#include "chrome/browser/crash_upload_list_win.h"
18#endif
[email protected]c8717adf72011-02-18 21:07:1619#include "chrome/common/chrome_paths.h"
[email protected]c38831a12011-10-28 12:44:4920#include "content/public/browser/browser_thread.h"
[email protected]c8717adf72011-02-18 21:07:1621
[email protected]631bb742011-11-02 11:29:3922using content::BrowserThread;
23
[email protected]c8717adf72011-02-18 21:07:1624CrashUploadList::CrashInfo::CrashInfo(const std::string& c, const base::Time& t)
25 : crash_id(c), crash_time(t) {}
26
27CrashUploadList::CrashInfo::~CrashInfo() {}
28
[email protected]b35b1dd2011-04-01 15:58:5029// static
30CrashUploadList* CrashUploadList::Create(Delegate* delegate) {
31#if defined(OS_WIN)
32 return new CrashUploadListWin(delegate);
33#else
34 return new CrashUploadList(delegate);
35#endif
36}
37
[email protected]234e8262012-02-22 21:05:5438// static
39const char* CrashUploadList::kReporterLogFilename = "uploads.log";
40
[email protected]c8717adf72011-02-18 21:07:1641CrashUploadList::CrashUploadList(Delegate* delegate) : delegate_(delegate) {}
42
43CrashUploadList::~CrashUploadList() {}
44
45void CrashUploadList::LoadCrashListAsynchronously() {
[email protected]3189013e2012-01-19 04:11:5746 BrowserThread::PostBlockingPoolTask(
[email protected]69eb91122011-11-22 21:44:2247 FROM_HERE,
48 base::Bind(&CrashUploadList::LoadCrashListAndInformDelegateOfCompletion,
49 this));
[email protected]c8717adf72011-02-18 21:07:1650}
51
52void CrashUploadList::ClearDelegate() {
53 delegate_ = NULL;
54}
55
[email protected]b35b1dd2011-04-01 15:58:5056
57void CrashUploadList::LoadCrashListAndInformDelegateOfCompletion() {
58 LoadCrashList();
[email protected]69eb91122011-11-22 21:44:2259 BrowserThread::PostTask(
60 BrowserThread::UI,
61 FROM_HERE,
62 base::Bind(&CrashUploadList::InformDelegateOfCompletion, this));
[email protected]b35b1dd2011-04-01 15:58:5063}
64
65void CrashUploadList::LoadCrashList() {
[email protected]c8717adf72011-02-18 21:07:1666 FilePath crash_dir_path;
67 PathService::Get(chrome::DIR_CRASH_DUMPS, &crash_dir_path);
68 FilePath upload_log_path = crash_dir_path.AppendASCII("uploads.log");
69 if (file_util::PathExists(upload_log_path)) {
70 std::string contents;
71 file_util::ReadFileToString(upload_log_path, &contents);
[email protected]b35b1dd2011-04-01 15:58:5072 std::vector<std::string> log_entries;
73 base::SplitStringAlongWhitespace(contents, &log_entries);
74 ParseLogEntries(log_entries);
[email protected]c8717adf72011-02-18 21:07:1675 }
[email protected]b35b1dd2011-04-01 15:58:5076}
77
78void CrashUploadList::ParseLogEntries(
79 const std::vector<std::string>& log_entries) {
80 std::vector<std::string>::const_reverse_iterator i;
81 for (i = log_entries.rbegin(); i != log_entries.rend(); ++i) {
82 std::vector<std::string> components;
83 base::SplitString(*i, ',', &components);
84 // Skip any blank (or corrupted) lines.
85 if (components.size() != 2)
86 continue;
87 double seconds_since_epoch;
88 if (!base::StringToDouble(components[0], &seconds_since_epoch))
89 continue;
90 CrashInfo info(components[1], base::Time::FromDoubleT(seconds_since_epoch));
91 crashes_.push_back(info);
92 }
[email protected]c8717adf72011-02-18 21:07:1693}
94
95void CrashUploadList::InformDelegateOfCompletion() {
96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
97 if (delegate_)
98 delegate_->OnCrashListAvailable();
99}
100
101void CrashUploadList::GetUploadedCrashes(unsigned int max_count,
102 std::vector<CrashInfo>* crashes) {
[email protected]b35b1dd2011-04-01 15:58:50103 std::copy(crashes_.begin(),
104 crashes_.begin() + std::min<size_t>(crashes_.size(), max_count),
105 std::back_inserter(*crashes));
106}
107
108std::vector<CrashUploadList::CrashInfo>& CrashUploadList::crashes() {
109 return crashes_;
[email protected]c8717adf72011-02-18 21:07:16110}