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