blob: cd236c6d098eb8703f118bb8068848d75d31e6ed [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
5#include "chrome/browser/upload_list.h"
6
7#include <algorithm>
8#include <iterator>
9
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"
15
16using content::BrowserThread;
17
[email protected]25021cfb2014-03-25 17:20:3518UploadList::UploadInfo::UploadInfo(const std::string& id,
19 const base::Time& t,
20 const std::string& local_id)
21 : id(id), time(t), local_id(local_id) {}
22
23UploadList::UploadInfo::UploadInfo(const std::string& id, const base::Time& t)
24 : id(id), time(t) {}
[email protected]9e94ab772013-07-23 08:00:5725
26UploadList::UploadInfo::~UploadInfo() {}
27
28UploadList::UploadList(Delegate* delegate,
29 const base::FilePath& upload_log_path)
30 : delegate_(delegate),
31 upload_log_path_(upload_log_path) {}
32
33UploadList::~UploadList() {}
34
35void UploadList::LoadUploadListAsynchronously() {
36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
37 BrowserThread::PostBlockingPoolTask(
38 FROM_HERE,
39 base::Bind(&UploadList::LoadUploadListAndInformDelegateOfCompletion,
40 this));
41}
42
43void UploadList::ClearDelegate() {
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
45 delegate_ = NULL;
46}
47
48void UploadList::LoadUploadListAndInformDelegateOfCompletion() {
49 LoadUploadList();
50 BrowserThread::PostTask(
51 BrowserThread::UI,
52 FROM_HERE,
53 base::Bind(&UploadList::InformDelegateOfCompletion, this));
54}
55
56void UploadList::LoadUploadList() {
57 if (base::PathExists(upload_log_path_)) {
58 std::string contents;
[email protected]82f84b92013-08-30 18:23:5059 base::ReadFileToString(upload_log_path_, &contents);
[email protected]9e94ab772013-07-23 08:00:5760 std::vector<std::string> log_entries;
61 base::SplitStringAlongWhitespace(contents, &log_entries);
[email protected]1aa80102013-10-23 09:23:1562 ClearUploads();
[email protected]9e94ab772013-07-23 08:00:5763 ParseLogEntries(log_entries);
64 }
65}
66
67void UploadList::AppendUploadInfo(const UploadInfo& info) {
68 uploads_.push_back(info);
69}
70
[email protected]1aa80102013-10-23 09:23:1571void UploadList::ClearUploads() {
72 uploads_.clear();
73}
74
[email protected]9e94ab772013-07-23 08:00:5775void 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.
[email protected]25021cfb2014-03-25 17:20:3582 if (components.size() != 2 && components.size() != 3)
[email protected]9e94ab772013-07-23 08:00:5783 continue;
[email protected]25021cfb2014-03-25 17:20:3584 base::Time upload_time;
[email protected]9e94ab772013-07-23 08:00:5785 double seconds_since_epoch;
[email protected]25021cfb2014-03-25 17:20:3586 if (!components[0].empty()) {
87 if (!base::StringToDouble(components[0], &seconds_since_epoch))
88 continue;
89 upload_time = base::Time::FromDoubleT(seconds_since_epoch);
90 }
91 UploadInfo info(components[1], upload_time);
92 if (components.size() == 3)
93 info.local_id = components[2];
[email protected]9e94ab772013-07-23 08:00:5794 uploads_.push_back(info);
95 }
96}
97
98void UploadList::InformDelegateOfCompletion() {
99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
100 if (delegate_)
101 delegate_->OnUploadListAvailable();
102}
103
104void 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));
110}