blob: e99ce0b575bd7b8470bc8093f1178c7e88d17b60 [file] [log] [blame]
[email protected]9e94ab772013-07-23 08:00:571// 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.
4
sdefresne44eb1f22015-08-06 08:51:555#include "components/upload_list/upload_list.h"
[email protected]9e94ab772013-07-23 08:00:576
7#include <algorithm>
8#include <iterator>
9
10#include "base/bind.h"
thestig18dfb7a52014-08-26 10:44:0411#include "base/files/file_util.h"
sdefresne44eb1f22015-08-06 08:51:5512#include "base/location.h"
[email protected]9e94ab772013-07-23 08:00:5713#include "base/strings/string_number_conversions.h"
14#include "base/strings/string_split.h"
brettw83dc1612015-08-12 07:31:1815#include "base/strings/string_util.h"
sdefresne44eb1f22015-08-06 08:51:5516#include "base/thread_task_runner_handle.h"
17#include "base/threading/sequenced_worker_pool.h"
[email protected]9e94ab772013-07-23 08:00:5718
[email protected]25021cfb2014-03-25 17:20:3519UploadList::UploadInfo::UploadInfo(const std::string& id,
20 const base::Time& t,
21 const std::string& local_id)
22 : id(id), time(t), local_id(local_id) {}
23
24UploadList::UploadInfo::UploadInfo(const std::string& id, const base::Time& t)
25 : id(id), time(t) {}
[email protected]9e94ab772013-07-23 08:00:5726
27UploadList::UploadInfo::~UploadInfo() {}
28
sdefresne44eb1f22015-08-06 08:51:5529UploadList::UploadList(
30 Delegate* delegate,
31 const base::FilePath& upload_log_path,
32 const scoped_refptr<base::SequencedWorkerPool>& worker_pool)
[email protected]9e94ab772013-07-23 08:00:5733 : delegate_(delegate),
sdefresne44eb1f22015-08-06 08:51:5534 upload_log_path_(upload_log_path),
35 worker_pool_(worker_pool) {}
[email protected]9e94ab772013-07-23 08:00:5736
37UploadList::~UploadList() {}
38
39void UploadList::LoadUploadListAsynchronously() {
sdefresne44eb1f22015-08-06 08:51:5540 DCHECK(thread_checker_.CalledOnValidThread());
41 worker_pool_->PostTask(
[email protected]9e94ab772013-07-23 08:00:5742 FROM_HERE,
43 base::Bind(&UploadList::LoadUploadListAndInformDelegateOfCompletion,
sdefresne44eb1f22015-08-06 08:51:5544 this, base::ThreadTaskRunnerHandle::Get()));
[email protected]9e94ab772013-07-23 08:00:5745}
46
47void UploadList::ClearDelegate() {
sdefresne44eb1f22015-08-06 08:51:5548 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]9e94ab772013-07-23 08:00:5749 delegate_ = NULL;
50}
51
sdefresne44eb1f22015-08-06 08:51:5552void UploadList::LoadUploadListAndInformDelegateOfCompletion(
53 const scoped_refptr<base::SequencedTaskRunner>& task_runner) {
[email protected]9e94ab772013-07-23 08:00:5754 LoadUploadList();
sdefresne44eb1f22015-08-06 08:51:5555 task_runner->PostTask(
[email protected]9e94ab772013-07-23 08:00:5756 FROM_HERE,
57 base::Bind(&UploadList::InformDelegateOfCompletion, this));
58}
59
60void UploadList::LoadUploadList() {
61 if (base::PathExists(upload_log_path_)) {
62 std::string contents;
[email protected]82f84b92013-08-30 18:23:5063 base::ReadFileToString(upload_log_path_, &contents);
brettw83dc1612015-08-12 07:31:1864 std::vector<std::string> log_entries = base::SplitString(
65 contents, base::kWhitespaceASCII, base::KEEP_WHITESPACE,
66 base::SPLIT_WANT_NONEMPTY);
[email protected]1aa80102013-10-23 09:23:1567 ClearUploads();
[email protected]9e94ab772013-07-23 08:00:5768 ParseLogEntries(log_entries);
69 }
70}
71
72void UploadList::AppendUploadInfo(const UploadInfo& info) {
73 uploads_.push_back(info);
74}
75
[email protected]1aa80102013-10-23 09:23:1576void UploadList::ClearUploads() {
77 uploads_.clear();
78}
79
[email protected]9e94ab772013-07-23 08:00:5780void 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) {
brettwc6f82b12015-07-21 21:37:3884 std::vector<std::string> components = base::SplitString(
85 *i, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
[email protected]9e94ab772013-07-23 08:00:5786 // Skip any blank (or corrupted) lines.
[email protected]25021cfb2014-03-25 17:20:3587 if (components.size() != 2 && components.size() != 3)
[email protected]9e94ab772013-07-23 08:00:5788 continue;
[email protected]25021cfb2014-03-25 17:20:3589 base::Time upload_time;
[email protected]9e94ab772013-07-23 08:00:5790 double seconds_since_epoch;
[email protected]25021cfb2014-03-25 17:20:3591 if (!components[0].empty()) {
92 if (!base::StringToDouble(components[0], &seconds_since_epoch))
93 continue;
94 upload_time = base::Time::FromDoubleT(seconds_since_epoch);
95 }
96 UploadInfo info(components[1], upload_time);
97 if (components.size() == 3)
98 info.local_id = components[2];
[email protected]9e94ab772013-07-23 08:00:5799 uploads_.push_back(info);
100 }
101}
102
103void UploadList::InformDelegateOfCompletion() {
sdefresne44eb1f22015-08-06 08:51:55104 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]9e94ab772013-07-23 08:00:57105 if (delegate_)
106 delegate_->OnUploadListAvailable();
107}
108
109void UploadList::GetUploads(unsigned int max_count,
110 std::vector<UploadInfo>* uploads) {
sdefresne44eb1f22015-08-06 08:51:55111 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]9e94ab772013-07-23 08:00:57112 std::copy(uploads_.begin(),
113 uploads_.begin() + std::min<size_t>(uploads_.size(), max_count),
114 std::back_inserter(*uploads));
115}