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 18 // time,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. 23 class UploadList : public base::RefCountedThreadSafe<UploadList> { 24 public: 25 struct UploadInfo { 26 UploadInfo(const std::string& c, const base::Time& t); 27 ~UploadInfo(); 28 29 std::string id; 30 base::Time time; 31 }; 32 33 class Delegate { 34 public: 35 // Invoked when the upload list has been loaded. Will be called on the 36 // UI thread. 37 virtual void OnUploadListAvailable() = 0; 38 39 protected: 40 virtual ~Delegate() {} 41 }; 42 43 // Creates a new upload list with the given callback delegate. 44 UploadList(Delegate* delegate, const base::FilePath& upload_log_path); 45 46 // Starts loading the upload list. OnUploadListAvailable will be called when 47 // loading is complete. 48 void LoadUploadListAsynchronously(); 49 50 // Clears the delegate, so that any outstanding asynchronous load will not 51 // call the delegate on completion. 52 void ClearDelegate(); 53 54 // Populates |uploads| with the |max_count| most recent uploads, 55 // in reverse chronological order. 56 // Must be called only after OnUploadListAvailable has been called. 57 void GetUploads(unsigned int max_count, std::vector<UploadInfo>* uploads); 58 59 protected: 60 virtual ~UploadList(); 61 62 // Reads the upload log and stores the entries in |uploads_|. 63 virtual void LoadUploadList(); 64 65 // Adds |info| to |uploads_|. 66 void AppendUploadInfo(const UploadInfo& info); 67 68 private: 69 friend class base::RefCountedThreadSafe<UploadList>; 70 FRIEND_TEST_ALL_PREFIXES(UploadListTest, ParseLogEntries); 71 72 // Manages the background thread work for LoadUploadListAsynchronously(). 73 void LoadUploadListAndInformDelegateOfCompletion(); 74 75 // Calls the delegate's callback method, if there is a delegate. 76 void InformDelegateOfCompletion(); 77 78 // Parses upload log lines, converting them to UploadInfo entries. 79 void ParseLogEntries(const std::vector<std::string>& log_entries); 80 81 std::vector<UploadInfo> uploads_; 82 Delegate* delegate_; 83 const base::FilePath upload_log_path_; 84 85 DISALLOW_COPY_AND_ASSIGN(UploadList); 86 }; 87 88 #endif // CHROME_BROWSER_UPLOAD_LIST_H_ 89