- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / upload_list.cc
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
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
16 using content::BrowserThread;
17
18 UploadList::UploadInfo::UploadInfo(const std::string& c, const base::Time& t)
19     : id(c), time(t) {}
20
21 UploadList::UploadInfo::~UploadInfo() {}
22
23 UploadList::UploadList(Delegate* delegate,
24                        const base::FilePath& upload_log_path)
25     : delegate_(delegate),
26       upload_log_path_(upload_log_path) {}
27
28 UploadList::~UploadList() {}
29
30 void UploadList::LoadUploadListAsynchronously() {
31   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
32   BrowserThread::PostBlockingPoolTask(
33       FROM_HERE,
34       base::Bind(&UploadList::LoadUploadListAndInformDelegateOfCompletion,
35                  this));
36 }
37
38 void UploadList::ClearDelegate() {
39   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
40   delegate_ = NULL;
41 }
42
43 void UploadList::LoadUploadListAndInformDelegateOfCompletion() {
44   LoadUploadList();
45   BrowserThread::PostTask(
46       BrowserThread::UI,
47       FROM_HERE,
48       base::Bind(&UploadList::InformDelegateOfCompletion, this));
49 }
50
51 void UploadList::LoadUploadList() {
52   if (base::PathExists(upload_log_path_)) {
53     std::string contents;
54     base::ReadFileToString(upload_log_path_, &contents);
55     std::vector<std::string> log_entries;
56     base::SplitStringAlongWhitespace(contents, &log_entries);
57     ClearUploads();
58     ParseLogEntries(log_entries);
59   }
60 }
61
62 void UploadList::AppendUploadInfo(const UploadInfo& info) {
63   uploads_.push_back(info);
64 }
65
66 void UploadList::ClearUploads() {
67   uploads_.clear();
68 }
69
70 void UploadList::ParseLogEntries(
71     const std::vector<std::string>& log_entries) {
72   std::vector<std::string>::const_reverse_iterator i;
73   for (i = log_entries.rbegin(); i != log_entries.rend(); ++i) {
74     std::vector<std::string> components;
75     base::SplitString(*i, ',', &components);
76     // Skip any blank (or corrupted) lines.
77     if (components.size() != 2)
78       continue;
79     double seconds_since_epoch;
80     if (!base::StringToDouble(components[0], &seconds_since_epoch))
81       continue;
82     UploadInfo info(components[1],
83                     base::Time::FromDoubleT(seconds_since_epoch));
84     uploads_.push_back(info);
85   }
86 }
87
88 void UploadList::InformDelegateOfCompletion() {
89   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
90   if (delegate_)
91     delegate_->OnUploadListAvailable();
92 }
93
94 void UploadList::GetUploads(unsigned int max_count,
95                             std::vector<UploadInfo>* uploads) {
96   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
97   std::copy(uploads_.begin(),
98             uploads_.begin() + std::min<size_t>(uploads_.size(), max_count),
99             std::back_inserter(*uploads));
100 }