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/files/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
& id
,
20 const std::string
& local_id
)
21 : id(id
), time(t
), local_id(local_id
) {}
23 UploadList::UploadInfo::UploadInfo(const std::string
& id
, const base::Time
& t
)
26 UploadList::UploadInfo::~UploadInfo() {}
28 UploadList::UploadList(Delegate
* delegate
,
29 const base::FilePath
& upload_log_path
)
30 : delegate_(delegate
),
31 upload_log_path_(upload_log_path
) {}
33 UploadList::~UploadList() {}
35 void UploadList::LoadUploadListAsynchronously() {
36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
37 BrowserThread::PostBlockingPoolTask(
39 base::Bind(&UploadList::LoadUploadListAndInformDelegateOfCompletion
,
43 void UploadList::ClearDelegate() {
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
48 void UploadList::LoadUploadListAndInformDelegateOfCompletion() {
50 BrowserThread::PostTask(
53 base::Bind(&UploadList::InformDelegateOfCompletion
, this));
56 void UploadList::LoadUploadList() {
57 if (base::PathExists(upload_log_path_
)) {
59 base::ReadFileToString(upload_log_path_
, &contents
);
60 std::vector
<std::string
> log_entries
;
61 base::SplitStringAlongWhitespace(contents
, &log_entries
);
63 ParseLogEntries(log_entries
);
67 void UploadList::AppendUploadInfo(const UploadInfo
& info
) {
68 uploads_
.push_back(info
);
71 void UploadList::ClearUploads() {
75 void UploadList::ParseLogEntries(
76 const std::vector
<std::string
>& log_entries
) {
77 std::vector
<std::string
>::const_reverse_iterator i
;
78 for (i
= log_entries
.rbegin(); i
!= log_entries
.rend(); ++i
) {
79 std::vector
<std::string
> components
;
80 base::SplitString(*i
, ',', &components
);
81 // Skip any blank (or corrupted) lines.
82 if (components
.size() != 2 && components
.size() != 3)
84 base::Time upload_time
;
85 double seconds_since_epoch
;
86 if (!components
[0].empty()) {
87 if (!base::StringToDouble(components
[0], &seconds_since_epoch
))
89 upload_time
= base::Time::FromDoubleT(seconds_since_epoch
);
91 UploadInfo
info(components
[1], upload_time
);
92 if (components
.size() == 3)
93 info
.local_id
= components
[2];
94 uploads_
.push_back(info
);
98 void UploadList::InformDelegateOfCompletion() {
99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
101 delegate_
->OnUploadListAvailable();
104 void UploadList::GetUploads(unsigned int max_count
,
105 std::vector
<UploadInfo
>* uploads
) {
106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
107 std::copy(uploads_
.begin(),
108 uploads_
.begin() + std::min
<size_t>(uploads_
.size(), max_count
),
109 std::back_inserter(*uploads
));