Roll src/third_party/WebKit 605a979:06cb9e9 (svn 202556:202558)
[chromium-blink-merge.git] / components / upload_list / upload_list.h
blob2becc6ad4673d11c16606e2354fb8b24472c490f
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 #ifndef COMPONENTS_UPLOAD_LIST_UPLOAD_LIST_H_
6 #define COMPONENTS_UPLOAD_LIST_UPLOAD_LIST_H_
8 #include <string>
9 #include <vector>
11 #include "base/files/file_path.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/threading/thread_checker.h"
15 #include "base/time/time.h"
17 namespace base {
18 class SequencedTaskRunner;
19 class SequencedWorkerPool;
22 // Loads and parses an upload list text file of the format
23 // time,id[,local_id]
24 // time,id[,local_id]
25 // etc.
26 // where each line represents an upload and "time" is Unix time. Must be used
27 // from the UI thread. The loading and parsing is done on a blocking pool task
28 // runner. A line may or may not contain "local_id".
29 class UploadList : public base::RefCountedThreadSafe<UploadList> {
30 public:
31 struct UploadInfo {
32 UploadInfo(const std::string& id,
33 const base::Time& t,
34 const std::string& local_id);
35 UploadInfo(const std::string& id, const base::Time& t);
36 ~UploadInfo();
38 std::string id;
39 base::Time time;
40 // ID for a locally stored object that may or may not be uploaded.
41 std::string local_id;
44 class Delegate {
45 public:
46 // Invoked when the upload list has been loaded. Will be called on the
47 // UI thread.
48 virtual void OnUploadListAvailable() = 0;
50 protected:
51 virtual ~Delegate() {}
54 // Creates a new upload list with the given callback delegate.
55 UploadList(Delegate* delegate,
56 const base::FilePath& upload_log_path,
57 const scoped_refptr<base::SequencedWorkerPool>& worker_pool);
59 // Starts loading the upload list. OnUploadListAvailable will be called when
60 // loading is complete.
61 void LoadUploadListAsynchronously();
63 // Clears the delegate, so that any outstanding asynchronous load will not
64 // call the delegate on completion.
65 void ClearDelegate();
67 // Populates |uploads| with the |max_count| most recent uploads,
68 // in reverse chronological order.
69 // Must be called only after OnUploadListAvailable has been called.
70 void GetUploads(unsigned int max_count, std::vector<UploadInfo>* uploads);
72 protected:
73 virtual ~UploadList();
75 // Reads the upload log and stores the entries in |uploads_|.
76 virtual void LoadUploadList();
78 // Adds |info| to |uploads_|.
79 void AppendUploadInfo(const UploadInfo& info);
81 // Clear |uploads_|.
82 void ClearUploads();
84 private:
85 friend class base::RefCountedThreadSafe<UploadList>;
86 FRIEND_TEST_ALL_PREFIXES(UploadListTest, ParseLogEntries);
87 FRIEND_TEST_ALL_PREFIXES(UploadListTest, ParseLogEntriesWithLocalId);
89 // Manages the background thread work for LoadUploadListAsynchronously().
90 void LoadUploadListAndInformDelegateOfCompletion(
91 const scoped_refptr<base::SequencedTaskRunner>& task_runner);
93 // Calls the delegate's callback method, if there is a delegate.
94 void InformDelegateOfCompletion();
96 // Parses upload log lines, converting them to UploadInfo entries.
97 void ParseLogEntries(const std::vector<std::string>& log_entries);
99 std::vector<UploadInfo> uploads_;
100 Delegate* delegate_;
101 const base::FilePath upload_log_path_;
103 base::ThreadChecker thread_checker_;
104 scoped_refptr<base::SequencedWorkerPool> worker_pool_;
106 DISALLOW_COPY_AND_ASSIGN(UploadList);
109 #endif // COMPONENTS_UPLOAD_LIST_UPLOAD_LIST_H_