[email protected] | 9e94ab77 | 2013-07-23 08:00:57 | [diff] [blame] | 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. |
| 4 | |
sdefresne | 44eb1f2 | 2015-08-06 08:51:55 | [diff] [blame] | 5 | #include "components/upload_list/upload_list.h" |
[email protected] | 9e94ab77 | 2013-07-23 08:00:57 | [diff] [blame] | 6 | |
| 7 | #include <algorithm> |
| 8 | #include <iterator> |
rsesek | 62217519 | 2016-03-28 15:41:38 | [diff] [blame^] | 9 | #include <utility> |
[email protected] | 9e94ab77 | 2013-07-23 08:00:57 | [diff] [blame] | 10 | |
| 11 | #include "base/bind.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 12 | #include "base/files/file_util.h" |
sdefresne | 44eb1f2 | 2015-08-06 08:51:55 | [diff] [blame] | 13 | #include "base/location.h" |
[email protected] | 9e94ab77 | 2013-07-23 08:00:57 | [diff] [blame] | 14 | #include "base/strings/string_number_conversions.h" |
| 15 | #include "base/strings/string_split.h" |
brettw | 83dc161 | 2015-08-12 07:31:18 | [diff] [blame] | 16 | #include "base/strings/string_util.h" |
sdefresne | 44eb1f2 | 2015-08-06 08:51:55 | [diff] [blame] | 17 | #include "base/thread_task_runner_handle.h" |
| 18 | #include "base/threading/sequenced_worker_pool.h" |
[email protected] | 9e94ab77 | 2013-07-23 08:00:57 | [diff] [blame] | 19 | |
grunell | b452aa3 | 2015-10-23 07:21:09 | [diff] [blame] | 20 | UploadList::UploadInfo::UploadInfo(const std::string& upload_id, |
| 21 | const base::Time& upload_time, |
| 22 | const std::string& local_id, |
| 23 | const base::Time& capture_time) |
| 24 | : upload_id(upload_id), upload_time(upload_time), |
| 25 | local_id(local_id), capture_time(capture_time) {} |
[email protected] | 25021cfb | 2014-03-25 17:20:35 | [diff] [blame] | 26 | |
grunell | b452aa3 | 2015-10-23 07:21:09 | [diff] [blame] | 27 | UploadList::UploadInfo::UploadInfo(const std::string& upload_id, |
| 28 | const base::Time& upload_time) |
| 29 | : upload_id(upload_id), upload_time(upload_time) {} |
[email protected] | 9e94ab77 | 2013-07-23 08:00:57 | [diff] [blame] | 30 | |
| 31 | UploadList::UploadInfo::~UploadInfo() {} |
| 32 | |
sdefresne | 44eb1f2 | 2015-08-06 08:51:55 | [diff] [blame] | 33 | UploadList::UploadList( |
| 34 | Delegate* delegate, |
| 35 | const base::FilePath& upload_log_path, |
| 36 | const scoped_refptr<base::SequencedWorkerPool>& worker_pool) |
[email protected] | 9e94ab77 | 2013-07-23 08:00:57 | [diff] [blame] | 37 | : delegate_(delegate), |
sdefresne | 44eb1f2 | 2015-08-06 08:51:55 | [diff] [blame] | 38 | upload_log_path_(upload_log_path), |
| 39 | worker_pool_(worker_pool) {} |
[email protected] | 9e94ab77 | 2013-07-23 08:00:57 | [diff] [blame] | 40 | |
| 41 | UploadList::~UploadList() {} |
| 42 | |
| 43 | void UploadList::LoadUploadListAsynchronously() { |
sdefresne | 44eb1f2 | 2015-08-06 08:51:55 | [diff] [blame] | 44 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 45 | worker_pool_->PostTask( |
[email protected] | 9e94ab77 | 2013-07-23 08:00:57 | [diff] [blame] | 46 | FROM_HERE, |
rsesek | 62217519 | 2016-03-28 15:41:38 | [diff] [blame^] | 47 | base::Bind(&UploadList::PerformLoadAndNotifyDelegate, |
sdefresne | 44eb1f2 | 2015-08-06 08:51:55 | [diff] [blame] | 48 | this, base::ThreadTaskRunnerHandle::Get())); |
[email protected] | 9e94ab77 | 2013-07-23 08:00:57 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void UploadList::ClearDelegate() { |
sdefresne | 44eb1f2 | 2015-08-06 08:51:55 | [diff] [blame] | 52 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 9e94ab77 | 2013-07-23 08:00:57 | [diff] [blame] | 53 | delegate_ = NULL; |
| 54 | } |
| 55 | |
rsesek | 62217519 | 2016-03-28 15:41:38 | [diff] [blame^] | 56 | void UploadList::PerformLoadAndNotifyDelegate( |
sdefresne | 44eb1f2 | 2015-08-06 08:51:55 | [diff] [blame] | 57 | const scoped_refptr<base::SequencedTaskRunner>& task_runner) { |
rsesek | 62217519 | 2016-03-28 15:41:38 | [diff] [blame^] | 58 | std::vector<UploadInfo> uploads; |
| 59 | LoadUploadList(&uploads); |
sdefresne | 44eb1f2 | 2015-08-06 08:51:55 | [diff] [blame] | 60 | task_runner->PostTask( |
[email protected] | 9e94ab77 | 2013-07-23 08:00:57 | [diff] [blame] | 61 | FROM_HERE, |
rsesek | 62217519 | 2016-03-28 15:41:38 | [diff] [blame^] | 62 | base::Bind(&UploadList::SetUploadsAndNotifyDelegate, this, |
| 63 | std::move(uploads))); |
[email protected] | 9e94ab77 | 2013-07-23 08:00:57 | [diff] [blame] | 64 | } |
| 65 | |
rsesek | 62217519 | 2016-03-28 15:41:38 | [diff] [blame^] | 66 | void UploadList::LoadUploadList(std::vector<UploadInfo>* uploads) { |
[email protected] | 9e94ab77 | 2013-07-23 08:00:57 | [diff] [blame] | 67 | if (base::PathExists(upload_log_path_)) { |
| 68 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 69 | base::ReadFileToString(upload_log_path_, &contents); |
brettw | 83dc161 | 2015-08-12 07:31:18 | [diff] [blame] | 70 | std::vector<std::string> log_entries = base::SplitString( |
| 71 | contents, base::kWhitespaceASCII, base::KEEP_WHITESPACE, |
| 72 | base::SPLIT_WANT_NONEMPTY); |
rsesek | 62217519 | 2016-03-28 15:41:38 | [diff] [blame^] | 73 | ParseLogEntries(log_entries, uploads); |
[email protected] | 9e94ab77 | 2013-07-23 08:00:57 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
[email protected] | 9e94ab77 | 2013-07-23 08:00:57 | [diff] [blame] | 77 | void UploadList::ParseLogEntries( |
rsesek | 62217519 | 2016-03-28 15:41:38 | [diff] [blame^] | 78 | const std::vector<std::string>& log_entries, |
| 79 | std::vector<UploadInfo>* uploads) { |
[email protected] | 9e94ab77 | 2013-07-23 08:00:57 | [diff] [blame] | 80 | std::vector<std::string>::const_reverse_iterator i; |
| 81 | for (i = log_entries.rbegin(); i != log_entries.rend(); ++i) { |
brettw | c6f82b1 | 2015-07-21 21:37:38 | [diff] [blame] | 82 | std::vector<std::string> components = base::SplitString( |
| 83 | *i, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
[email protected] | 9e94ab77 | 2013-07-23 08:00:57 | [diff] [blame] | 84 | // Skip any blank (or corrupted) lines. |
grunell | b452aa3 | 2015-10-23 07:21:09 | [diff] [blame] | 85 | if (components.size() < 2 || components.size() > 4) |
[email protected] | 9e94ab77 | 2013-07-23 08:00:57 | [diff] [blame] | 86 | continue; |
[email protected] | 25021cfb | 2014-03-25 17:20:35 | [diff] [blame] | 87 | base::Time upload_time; |
[email protected] | 9e94ab77 | 2013-07-23 08:00:57 | [diff] [blame] | 88 | double seconds_since_epoch; |
[email protected] | 25021cfb | 2014-03-25 17:20:35 | [diff] [blame] | 89 | if (!components[0].empty()) { |
| 90 | if (!base::StringToDouble(components[0], &seconds_since_epoch)) |
| 91 | continue; |
| 92 | upload_time = base::Time::FromDoubleT(seconds_since_epoch); |
| 93 | } |
| 94 | UploadInfo info(components[1], upload_time); |
grunell | b452aa3 | 2015-10-23 07:21:09 | [diff] [blame] | 95 | |
| 96 | // Add local ID if present. |
| 97 | if (components.size() > 2) |
[email protected] | 25021cfb | 2014-03-25 17:20:35 | [diff] [blame] | 98 | info.local_id = components[2]; |
grunell | b452aa3 | 2015-10-23 07:21:09 | [diff] [blame] | 99 | |
| 100 | // Add capture time if present. |
| 101 | if (components.size() > 3 && |
| 102 | !components[3].empty() && |
| 103 | base::StringToDouble(components[3], &seconds_since_epoch)) { |
| 104 | info.capture_time = base::Time::FromDoubleT(seconds_since_epoch); |
| 105 | } |
| 106 | |
rsesek | 62217519 | 2016-03-28 15:41:38 | [diff] [blame^] | 107 | uploads->push_back(info); |
[email protected] | 9e94ab77 | 2013-07-23 08:00:57 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
rsesek | 62217519 | 2016-03-28 15:41:38 | [diff] [blame^] | 111 | void UploadList::SetUploadsAndNotifyDelegate(std::vector<UploadInfo> uploads) { |
sdefresne | 44eb1f2 | 2015-08-06 08:51:55 | [diff] [blame] | 112 | DCHECK(thread_checker_.CalledOnValidThread()); |
rsesek | 62217519 | 2016-03-28 15:41:38 | [diff] [blame^] | 113 | uploads_ = std::move(uploads); |
[email protected] | 9e94ab77 | 2013-07-23 08:00:57 | [diff] [blame] | 114 | if (delegate_) |
| 115 | delegate_->OnUploadListAvailable(); |
| 116 | } |
| 117 | |
rsesek | 62217519 | 2016-03-28 15:41:38 | [diff] [blame^] | 118 | void UploadList::GetUploads(size_t max_count, |
[email protected] | 9e94ab77 | 2013-07-23 08:00:57 | [diff] [blame] | 119 | std::vector<UploadInfo>* uploads) { |
sdefresne | 44eb1f2 | 2015-08-06 08:51:55 | [diff] [blame] | 120 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 9e94ab77 | 2013-07-23 08:00:57 | [diff] [blame] | 121 | std::copy(uploads_.begin(), |
rsesek | 62217519 | 2016-03-28 15:41:38 | [diff] [blame^] | 122 | uploads_.begin() + std::min(uploads_.size(), max_count), |
[email protected] | 9e94ab77 | 2013-07-23 08:00:57 | [diff] [blame] | 123 | std::back_inserter(*uploads)); |
| 124 | } |