| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/crash_upload_list.h" | 5 #include "chrome/browser/crash_upload_list.h" |
| 6 | 6 |
| 7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
| 8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| 11 #include "base/string_split.h" | 11 #include "base/string_split.h" |
| 12 #if defined(OS_WIN) |
| 13 #include "chrome/browser/crash_upload_list_win.h" |
| 14 #endif |
| 12 #include "chrome/common/chrome_paths.h" | 15 #include "chrome/common/chrome_paths.h" |
| 13 #include "content/browser/browser_thread.h" | 16 #include "content/browser/browser_thread.h" |
| 14 | 17 |
| 15 CrashUploadList::CrashInfo::CrashInfo(const std::string& c, const base::Time& t) | 18 CrashUploadList::CrashInfo::CrashInfo(const std::string& c, const base::Time& t) |
| 16 : crash_id(c), crash_time(t) {} | 19 : crash_id(c), crash_time(t) {} |
| 17 | 20 |
| 18 CrashUploadList::CrashInfo::~CrashInfo() {} | 21 CrashUploadList::CrashInfo::~CrashInfo() {} |
| 19 | 22 |
| 23 // static |
| 24 CrashUploadList* CrashUploadList::Create(Delegate* delegate) { |
| 25 #if defined(OS_WIN) |
| 26 return new CrashUploadListWin(delegate); |
| 27 #else |
| 28 return new CrashUploadList(delegate); |
| 29 #endif |
| 30 } |
| 31 |
| 20 CrashUploadList::CrashUploadList(Delegate* delegate) : delegate_(delegate) {} | 32 CrashUploadList::CrashUploadList(Delegate* delegate) : delegate_(delegate) {} |
| 21 | 33 |
| 22 CrashUploadList::~CrashUploadList() {} | 34 CrashUploadList::~CrashUploadList() {} |
| 23 | 35 |
| 24 void CrashUploadList::LoadCrashListAsynchronously() { | 36 void CrashUploadList::LoadCrashListAsynchronously() { |
| 25 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 37 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 26 NewRunnableMethod(this, &CrashUploadList::LoadUploadLog)); | 38 NewRunnableMethod(this, |
| 39 &CrashUploadList::LoadCrashListAndInformDelegateOfCompletion)); |
| 27 } | 40 } |
| 28 | 41 |
| 29 void CrashUploadList::ClearDelegate() { | 42 void CrashUploadList::ClearDelegate() { |
| 30 delegate_ = NULL; | 43 delegate_ = NULL; |
| 31 } | 44 } |
| 32 | 45 |
| 33 void CrashUploadList::LoadUploadLog() { | 46 |
| 47 void CrashUploadList::LoadCrashListAndInformDelegateOfCompletion() { |
| 48 LoadCrashList(); |
| 49 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 50 NewRunnableMethod(this, &CrashUploadList::InformDelegateOfCompletion)); |
| 51 } |
| 52 |
| 53 void CrashUploadList::LoadCrashList() { |
| 34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 35 FilePath crash_dir_path; | 55 FilePath crash_dir_path; |
| 36 PathService::Get(chrome::DIR_CRASH_DUMPS, &crash_dir_path); | 56 PathService::Get(chrome::DIR_CRASH_DUMPS, &crash_dir_path); |
| 37 FilePath upload_log_path = crash_dir_path.AppendASCII("uploads.log"); | 57 FilePath upload_log_path = crash_dir_path.AppendASCII("uploads.log"); |
| 38 if (file_util::PathExists(upload_log_path)) { | 58 if (file_util::PathExists(upload_log_path)) { |
| 39 std::string contents; | 59 std::string contents; |
| 40 file_util::ReadFileToString(upload_log_path, &contents); | 60 file_util::ReadFileToString(upload_log_path, &contents); |
| 41 base::SplitStringAlongWhitespace(contents, &log_entries_); | 61 std::vector<std::string> log_entries; |
| 62 base::SplitStringAlongWhitespace(contents, &log_entries); |
| 63 ParseLogEntries(log_entries); |
| 42 } | 64 } |
| 43 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 44 NewRunnableMethod(this, &CrashUploadList::InformDelegateOfCompletion)); | |
| 45 } | 65 } |
| 46 | 66 |
| 47 void CrashUploadList::InformDelegateOfCompletion() { | 67 void CrashUploadList::ParseLogEntries( |
| 48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 68 const std::vector<std::string>& log_entries) { |
| 49 if (delegate_) | 69 std::vector<std::string>::const_reverse_iterator i; |
| 50 delegate_->OnCrashListAvailable(); | 70 for (i = log_entries.rbegin(); i != log_entries.rend(); ++i) { |
| 51 } | |
| 52 | |
| 53 void CrashUploadList::GetUploadedCrashes(unsigned int max_count, | |
| 54 std::vector<CrashInfo>* crashes) { | |
| 55 std::vector<std::string>::reverse_iterator i; | |
| 56 for (i = log_entries_.rbegin(); i != log_entries_.rend(); ++i) { | |
| 57 std::vector<std::string> components; | 71 std::vector<std::string> components; |
| 58 base::SplitString(*i, ',', &components); | 72 base::SplitString(*i, ',', &components); |
| 59 // Skip any blank (or corrupted) lines. | 73 // Skip any blank (or corrupted) lines. |
| 60 if (components.size() != 2) | 74 if (components.size() != 2) |
| 61 continue; | 75 continue; |
| 62 double seconds_since_epoch; | 76 double seconds_since_epoch; |
| 63 if (!base::StringToDouble(components[0], &seconds_since_epoch)) | 77 if (!base::StringToDouble(components[0], &seconds_since_epoch)) |
| 64 continue; | 78 continue; |
| 65 CrashInfo info(components[1], base::Time::FromDoubleT(seconds_since_epoch)); | 79 CrashInfo info(components[1], base::Time::FromDoubleT(seconds_since_epoch)); |
| 66 crashes->push_back(info); | 80 crashes_.push_back(info); |
| 67 } | 81 } |
| 68 } | 82 } |
| 83 |
| 84 void CrashUploadList::InformDelegateOfCompletion() { |
| 85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 86 if (delegate_) |
| 87 delegate_->OnCrashListAvailable(); |
| 88 } |
| 89 |
| 90 void CrashUploadList::GetUploadedCrashes(unsigned int max_count, |
| 91 std::vector<CrashInfo>* crashes) { |
| 92 std::copy(crashes_.begin(), |
| 93 crashes_.begin() + std::min<size_t>(crashes_.size(), max_count), |
| 94 std::back_inserter(*crashes)); |
| 95 } |
| 96 |
| 97 std::vector<CrashUploadList::CrashInfo>& CrashUploadList::crashes() { |
| 98 return crashes_; |
| 99 } |
| OLD | NEW |