1 // Copyright 2013 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/upload_list.h"
10 #include "base/bind.h"
11 #include "base/file_util.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_split.h"
14 #include "content/public/browser/browser_thread.h"
16 using content::BrowserThread
;
18 UploadList::UploadInfo::UploadInfo(const std::string
& c
, const base::Time
& t
)
21 UploadList::UploadInfo::~UploadInfo() {}
23 UploadList::UploadList(Delegate
* delegate
,
24 const base::FilePath
& upload_log_path
)
25 : delegate_(delegate
),
26 upload_log_path_(upload_log_path
) {}
28 UploadList::~UploadList() {}
30 void UploadList::LoadUploadListAsynchronously() {
31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
32 BrowserThread::PostBlockingPoolTask(
34 base::Bind(&UploadList::LoadUploadListAndInformDelegateOfCompletion
,
38 void UploadList::ClearDelegate() {
39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
43 void UploadList::LoadUploadListAndInformDelegateOfCompletion() {
45 BrowserThread::PostTask(
48 base::Bind(&UploadList::InformDelegateOfCompletion
, this));
51 void UploadList::LoadUploadList() {
52 if (base::PathExists(upload_log_path_
)) {
54 file_util::ReadFileToString(upload_log_path_
, &contents
);
55 std::vector
<std::string
> log_entries
;
56 base::SplitStringAlongWhitespace(contents
, &log_entries
);
57 ParseLogEntries(log_entries
);
61 void UploadList::AppendUploadInfo(const UploadInfo
& info
) {
62 uploads_
.push_back(info
);
65 void UploadList::ParseLogEntries(
66 const std::vector
<std::string
>& log_entries
) {
67 std::vector
<std::string
>::const_reverse_iterator i
;
68 for (i
= log_entries
.rbegin(); i
!= log_entries
.rend(); ++i
) {
69 std::vector
<std::string
> components
;
70 base::SplitString(*i
, ',', &components
);
71 // Skip any blank (or corrupted) lines.
72 if (components
.size() != 2)
74 double seconds_since_epoch
;
75 if (!base::StringToDouble(components
[0], &seconds_since_epoch
))
77 UploadInfo
info(components
[1],
78 base::Time::FromDoubleT(seconds_since_epoch
));
79 uploads_
.push_back(info
);
83 void UploadList::InformDelegateOfCompletion() {
84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
86 delegate_
->OnUploadListAvailable();
89 void UploadList::GetUploads(unsigned int max_count
,
90 std::vector
<UploadInfo
>* uploads
) {
91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
92 std::copy(uploads_
.begin(),
93 uploads_
.begin() + std::min
<size_t>(uploads_
.size(), max_count
),
94 std::back_inserter(*uploads
));