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_
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"
18 class SequencedTaskRunner
;
19 class SequencedWorkerPool
;
22 // Loads and parses an upload list text file of the format
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
> {
32 UploadInfo(const std::string
& id
,
34 const std::string
& local_id
);
35 UploadInfo(const std::string
& id
, const base::Time
& t
);
40 // ID for a locally stored object that may or may not be uploaded.
46 // Invoked when the upload list has been loaded. Will be called on the
48 virtual void OnUploadListAvailable() = 0;
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.
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
);
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
);
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_
;
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_