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.
5 #include "chrome/browser/crash_upload_list.h"
10 #include "base/file_path.h"
11 #include "base/file_util.h"
12 #include "base/path_service.h"
13 #include "base/string_number_conversions.h"
14 #include "base/string_split.h"
16 #include "chrome/browser/crash_upload_list_win.h"
18 #include "chrome/common/chrome_paths.h"
19 #include "content/public/browser/browser_thread.h"
21 using content::BrowserThread
;
23 CrashUploadList::CrashInfo::CrashInfo(const std::string
& c
, const base::Time
& t
)
24 : crash_id(c
), crash_time(t
) {}
26 CrashUploadList::CrashInfo::~CrashInfo() {}
29 CrashUploadList
* CrashUploadList::Create(Delegate
* delegate
) {
31 return new CrashUploadListWin(delegate
);
33 return new CrashUploadList(delegate
);
37 CrashUploadList::CrashUploadList(Delegate
* delegate
) : delegate_(delegate
) {}
39 CrashUploadList::~CrashUploadList() {}
41 void CrashUploadList::LoadCrashListAsynchronously() {
42 BrowserThread::PostBlockingPoolTask(
44 base::Bind(&CrashUploadList::LoadCrashListAndInformDelegateOfCompletion
,
48 void CrashUploadList::ClearDelegate() {
53 void CrashUploadList::LoadCrashListAndInformDelegateOfCompletion() {
55 BrowserThread::PostTask(
58 base::Bind(&CrashUploadList::InformDelegateOfCompletion
, this));
61 void CrashUploadList::LoadCrashList() {
62 FilePath crash_dir_path
;
63 PathService::Get(chrome::DIR_CRASH_DUMPS
, &crash_dir_path
);
64 FilePath upload_log_path
= crash_dir_path
.AppendASCII("uploads.log");
65 if (file_util::PathExists(upload_log_path
)) {
67 file_util::ReadFileToString(upload_log_path
, &contents
);
68 std::vector
<std::string
> log_entries
;
69 base::SplitStringAlongWhitespace(contents
, &log_entries
);
70 ParseLogEntries(log_entries
);
74 void CrashUploadList::ParseLogEntries(
75 const std::vector
<std::string
>& log_entries
) {
76 std::vector
<std::string
>::const_reverse_iterator i
;
77 for (i
= log_entries
.rbegin(); i
!= log_entries
.rend(); ++i
) {
78 std::vector
<std::string
> components
;
79 base::SplitString(*i
, ',', &components
);
80 // Skip any blank (or corrupted) lines.
81 if (components
.size() != 2)
83 double seconds_since_epoch
;
84 if (!base::StringToDouble(components
[0], &seconds_since_epoch
))
86 CrashInfo
info(components
[1], base::Time::FromDoubleT(seconds_since_epoch
));
87 crashes_
.push_back(info
);
91 void CrashUploadList::InformDelegateOfCompletion() {
92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
94 delegate_
->OnCrashListAvailable();
97 void CrashUploadList::GetUploadedCrashes(unsigned int max_count
,
98 std::vector
<CrashInfo
>* crashes
) {
99 std::copy(crashes_
.begin(),
100 crashes_
.begin() + std::min
<size_t>(crashes_
.size(), max_count
),
101 std::back_inserter(*crashes
));
104 std::vector
<CrashUploadList::CrashInfo
>& CrashUploadList::crashes() {