chromeos: bluetooth: tie Proxy lifetime to object, not observer
[chromium-blink-merge.git] / chrome / browser / crash_upload_list.cc
blob30c04f80004629f61c3560c6ada6422da0c3a266
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"
7 #include <iterator>
9 #include "base/bind.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"
15 #if defined(OS_WIN)
16 #include "chrome/browser/crash_upload_list_win.h"
17 #endif
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() {}
28 // static
29 CrashUploadList* CrashUploadList::Create(Delegate* delegate) {
30 #if defined(OS_WIN)
31 return new CrashUploadListWin(delegate);
32 #else
33 return new CrashUploadList(delegate);
34 #endif
37 CrashUploadList::CrashUploadList(Delegate* delegate) : delegate_(delegate) {}
39 CrashUploadList::~CrashUploadList() {}
41 void CrashUploadList::LoadCrashListAsynchronously() {
42 BrowserThread::PostBlockingPoolTask(
43 FROM_HERE,
44 base::Bind(&CrashUploadList::LoadCrashListAndInformDelegateOfCompletion,
45 this));
48 void CrashUploadList::ClearDelegate() {
49 delegate_ = NULL;
53 void CrashUploadList::LoadCrashListAndInformDelegateOfCompletion() {
54 LoadCrashList();
55 BrowserThread::PostTask(
56 BrowserThread::UI,
57 FROM_HERE,
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)) {
66 std::string contents;
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)
82 continue;
83 double seconds_since_epoch;
84 if (!base::StringToDouble(components[0], &seconds_since_epoch))
85 continue;
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));
93 if (delegate_)
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() {
105 return crashes_;