[email protected] | 234e826 | 2012-02-22 21:05:54 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | c8717adf7 | 2011-02-18 21:07:16 | [diff] [blame] | 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.h" |
| 6 | |
[email protected] | 234e826 | 2012-02-22 21:05:54 | [diff] [blame] | 7 | #include <algorithm> |
[email protected] | 72e79c1 | 2012-03-19 05:24:00 | [diff] [blame] | 8 | #include <iterator> |
[email protected] | 4b5b497e | 2011-04-03 02:34:27 | [diff] [blame] | 9 | |
[email protected] | 69eb9112 | 2011-11-22 21:44:22 | [diff] [blame] | 10 | #include "base/bind.h" |
[email protected] | c8717adf7 | 2011-02-18 21:07:16 | [diff] [blame] | 11 | #include "base/file_path.h" |
| 12 | #include "base/file_util.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 13 | #include "base/path_service.h" |
[email protected] | c8717adf7 | 2011-02-18 21:07:16 | [diff] [blame] | 14 | #include "base/string_number_conversions.h" |
| 15 | #include "base/string_split.h" |
[email protected] | b35b1dd | 2011-04-01 15:58:50 | [diff] [blame] | 16 | #if defined(OS_WIN) |
| 17 | #include "chrome/browser/crash_upload_list_win.h" |
| 18 | #endif |
[email protected] | c8717adf7 | 2011-02-18 21:07:16 | [diff] [blame] | 19 | #include "chrome/common/chrome_paths.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 20 | #include "content/public/browser/browser_thread.h" |
[email protected] | c8717adf7 | 2011-02-18 21:07:16 | [diff] [blame] | 21 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 22 | using content::BrowserThread; |
| 23 | |
[email protected] | c8717adf7 | 2011-02-18 21:07:16 | [diff] [blame] | 24 | CrashUploadList::CrashInfo::CrashInfo(const std::string& c, const base::Time& t) |
| 25 | : crash_id(c), crash_time(t) {} |
| 26 | |
| 27 | CrashUploadList::CrashInfo::~CrashInfo() {} |
| 28 | |
[email protected] | b35b1dd | 2011-04-01 15:58:50 | [diff] [blame] | 29 | // static |
| 30 | CrashUploadList* 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] | 234e826 | 2012-02-22 21:05:54 | [diff] [blame] | 38 | // static |
| 39 | const char* CrashUploadList::kReporterLogFilename = "uploads.log"; |
| 40 | |
[email protected] | c8717adf7 | 2011-02-18 21:07:16 | [diff] [blame] | 41 | CrashUploadList::CrashUploadList(Delegate* delegate) : delegate_(delegate) {} |
| 42 | |
| 43 | CrashUploadList::~CrashUploadList() {} |
| 44 | |
| 45 | void CrashUploadList::LoadCrashListAsynchronously() { |
[email protected] | 3189013e | 2012-01-19 04:11:57 | [diff] [blame] | 46 | BrowserThread::PostBlockingPoolTask( |
[email protected] | 69eb9112 | 2011-11-22 21:44:22 | [diff] [blame] | 47 | FROM_HERE, |
| 48 | base::Bind(&CrashUploadList::LoadCrashListAndInformDelegateOfCompletion, |
| 49 | this)); |
[email protected] | c8717adf7 | 2011-02-18 21:07:16 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void CrashUploadList::ClearDelegate() { |
| 53 | delegate_ = NULL; |
| 54 | } |
| 55 | |
[email protected] | b35b1dd | 2011-04-01 15:58:50 | [diff] [blame] | 56 | |
| 57 | void CrashUploadList::LoadCrashListAndInformDelegateOfCompletion() { |
| 58 | LoadCrashList(); |
[email protected] | 69eb9112 | 2011-11-22 21:44:22 | [diff] [blame] | 59 | BrowserThread::PostTask( |
| 60 | BrowserThread::UI, |
| 61 | FROM_HERE, |
| 62 | base::Bind(&CrashUploadList::InformDelegateOfCompletion, this)); |
[email protected] | b35b1dd | 2011-04-01 15:58:50 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void CrashUploadList::LoadCrashList() { |
[email protected] | c8717adf7 | 2011-02-18 21:07:16 | [diff] [blame] | 66 | 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] | b35b1dd | 2011-04-01 15:58:50 | [diff] [blame] | 72 | std::vector<std::string> log_entries; |
| 73 | base::SplitStringAlongWhitespace(contents, &log_entries); |
| 74 | ParseLogEntries(log_entries); |
[email protected] | c8717adf7 | 2011-02-18 21:07:16 | [diff] [blame] | 75 | } |
[email protected] | b35b1dd | 2011-04-01 15:58:50 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void 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] | c8717adf7 | 2011-02-18 21:07:16 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void CrashUploadList::InformDelegateOfCompletion() { |
| 96 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 97 | if (delegate_) |
| 98 | delegate_->OnCrashListAvailable(); |
| 99 | } |
| 100 | |
| 101 | void CrashUploadList::GetUploadedCrashes(unsigned int max_count, |
| 102 | std::vector<CrashInfo>* crashes) { |
[email protected] | b35b1dd | 2011-04-01 15:58:50 | [diff] [blame] | 103 | std::copy(crashes_.begin(), |
| 104 | crashes_.begin() + std::min<size_t>(crashes_.size(), max_count), |
| 105 | std::back_inserter(*crashes)); |
| 106 | } |
| 107 | |
| 108 | std::vector<CrashUploadList::CrashInfo>& CrashUploadList::crashes() { |
| 109 | return crashes_; |
[email protected] | c8717adf7 | 2011-02-18 21:07:16 | [diff] [blame] | 110 | } |