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 base::ReadFileToString(upload_log_path_
, &contents
);
55 std::vector
<std::string
> log_entries
;
56 base::SplitStringAlongWhitespace(contents
, &log_entries
);
58 ParseLogEntries(log_entries
);
62 void UploadList::AppendUploadInfo(const UploadInfo
& info
) {
63 uploads_
.push_back(info
);
66 void UploadList::ClearUploads() {
70 void UploadList::ParseLogEntries(
71 const std::vector
<std::string
>& log_entries
) {
72 std::vector
<std::string
>::const_reverse_iterator i
;
73 for (i
= log_entries
.rbegin(); i
!= log_entries
.rend(); ++i
) {
74 std::vector
<std::string
> components
;
75 base::SplitString(*i
, ',', &components
);
76 // Skip any blank (or corrupted) lines.
77 if (components
.size() != 2)
79 double seconds_since_epoch
;
80 if (!base::StringToDouble(components
[0], &seconds_since_epoch
))
82 UploadInfo
info(components
[1],
83 base::Time::FromDoubleT(seconds_since_epoch
));
84 uploads_
.push_back(info
);
88 void UploadList::InformDelegateOfCompletion() {
89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
91 delegate_
->OnUploadListAvailable();
94 void UploadList::GetUploads(unsigned int max_count
,
95 std::vector
<UploadInfo
>* uploads
) {
96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
97 std::copy(uploads_
.begin(),
98 uploads_
.begin() + std::min
<size_t>(uploads_
.size(), max_count
),
99 std::back_inserter(*uploads
));