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 "components/upload_list/upload_list.h"
10 #include "base/bind.h"
11 #include "base/files/file_util.h"
12 #include "base/location.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_split.h"
15 #include "base/strings/string_util.h"
16 #include "base/thread_task_runner_handle.h"
17 #include "base/threading/sequenced_worker_pool.h"
19 UploadList::UploadInfo::UploadInfo(const std::string
& id
,
21 const std::string
& local_id
)
22 : id(id
), time(t
), local_id(local_id
) {}
24 UploadList::UploadInfo::UploadInfo(const std::string
& id
, const base::Time
& t
)
27 UploadList::UploadInfo::~UploadInfo() {}
29 UploadList::UploadList(
31 const base::FilePath
& upload_log_path
,
32 const scoped_refptr
<base::SequencedWorkerPool
>& worker_pool
)
33 : delegate_(delegate
),
34 upload_log_path_(upload_log_path
),
35 worker_pool_(worker_pool
) {}
37 UploadList::~UploadList() {}
39 void UploadList::LoadUploadListAsynchronously() {
40 DCHECK(thread_checker_
.CalledOnValidThread());
41 worker_pool_
->PostTask(
43 base::Bind(&UploadList::LoadUploadListAndInformDelegateOfCompletion
,
44 this, base::ThreadTaskRunnerHandle::Get()));
47 void UploadList::ClearDelegate() {
48 DCHECK(thread_checker_
.CalledOnValidThread());
52 void UploadList::LoadUploadListAndInformDelegateOfCompletion(
53 const scoped_refptr
<base::SequencedTaskRunner
>& task_runner
) {
55 task_runner
->PostTask(
57 base::Bind(&UploadList::InformDelegateOfCompletion
, this));
60 void UploadList::LoadUploadList() {
61 if (base::PathExists(upload_log_path_
)) {
63 base::ReadFileToString(upload_log_path_
, &contents
);
64 std::vector
<std::string
> log_entries
= base::SplitString(
65 contents
, base::kWhitespaceASCII
, base::KEEP_WHITESPACE
,
66 base::SPLIT_WANT_NONEMPTY
);
68 ParseLogEntries(log_entries
);
72 void UploadList::AppendUploadInfo(const UploadInfo
& info
) {
73 uploads_
.push_back(info
);
76 void UploadList::ClearUploads() {
80 void UploadList::ParseLogEntries(
81 const std::vector
<std::string
>& log_entries
) {
82 std::vector
<std::string
>::const_reverse_iterator i
;
83 for (i
= log_entries
.rbegin(); i
!= log_entries
.rend(); ++i
) {
84 std::vector
<std::string
> components
= base::SplitString(
85 *i
, ",", base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
86 // Skip any blank (or corrupted) lines.
87 if (components
.size() != 2 && components
.size() != 3)
89 base::Time upload_time
;
90 double seconds_since_epoch
;
91 if (!components
[0].empty()) {
92 if (!base::StringToDouble(components
[0], &seconds_since_epoch
))
94 upload_time
= base::Time::FromDoubleT(seconds_since_epoch
);
96 UploadInfo
info(components
[1], upload_time
);
97 if (components
.size() == 3)
98 info
.local_id
= components
[2];
99 uploads_
.push_back(info
);
103 void UploadList::InformDelegateOfCompletion() {
104 DCHECK(thread_checker_
.CalledOnValidThread());
106 delegate_
->OnUploadListAvailable();
109 void UploadList::GetUploads(unsigned int max_count
,
110 std::vector
<UploadInfo
>* uploads
) {
111 DCHECK(thread_checker_
.CalledOnValidThread());
112 std::copy(uploads_
.begin(),
113 uploads_
.begin() + std::min
<size_t>(uploads_
.size(), max_count
),
114 std::back_inserter(*uploads
));