1 // Copyright (c) 2012 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 CONTENT_BROWSER_DOWNLOAD_SAVE_ITEM_H_ 6 #define CONTENT_BROWSER_DOWNLOAD_SAVE_ITEM_H_ 7 8 #include "base/basictypes.h" 9 #include "base/files/file_path.h" 10 #include "content/browser/download/save_types.h" 11 #include "content/public/common/referrer.h" 12 #include "url/gurl.h" 13 14 namespace content { 15 class SavePackage; 16 17 // One SaveItem per save file. This is the model class that stores all the 18 // state for one save file. 19 class SaveItem { 20 public: 21 enum SaveState { 22 WAIT_START, 23 IN_PROGRESS, 24 COMPLETE, 25 CANCELED 26 }; 27 28 SaveItem(const GURL& url, 29 const Referrer& referrer, 30 SavePackage* package, 31 SaveFileCreateInfo::SaveFileSource save_source); 32 33 ~SaveItem(); 34 35 void Start(); 36 37 // Received a new chunk of data. 38 void Update(int64 bytes_so_far); 39 40 // Cancel saving item. 41 void Cancel(); 42 43 // Saving operation completed. 44 void Finish(int64 size, bool is_success); 45 46 // Rough percent complete, -1 means we don't know (since we didn't receive a 47 // total size). 48 int PercentComplete() const; 49 50 // Update path for SaveItem, the actual file is renamed on the file thread. 51 void Rename(const base::FilePath& full_path); 52 53 void SetSaveId(int32 save_id); 54 55 void SetTotalBytes(int64 total_bytes); 56 57 // Accessors. 58 SaveState state() const { return state_; } 59 const base::FilePath& full_path() const { return full_path_; } 60 const base::FilePath& file_name() const { return file_name_; } 61 const GURL& url() const { return url_; } 62 const Referrer& referrer() const { return referrer_; } 63 int64 total_bytes() const { return total_bytes_; } 64 int64 received_bytes() const { return received_bytes_; } 65 int32 save_id() const { return save_id_; } 66 bool has_final_name() const { return has_final_name_; } 67 bool success() const { return is_success_; } 68 SaveFileCreateInfo::SaveFileSource save_source() const { 69 return save_source_; 70 } 71 SavePackage* package() const { return package_; } 72 73 private: 74 // Internal helper for maintaining consistent received and total sizes. 75 void UpdateSize(int64 size); 76 77 // Request ID assigned by the ResourceDispatcherHost. 78 int32 save_id_; 79 80 // Full path to the save item file. 81 base::FilePath full_path_; 82 83 // Short display version of the file. 84 base::FilePath file_name_; 85 86 // The URL for this save item. 87 GURL url_; 88 Referrer referrer_; 89 90 // Total bytes expected. 91 int64 total_bytes_; 92 93 // Current received bytes. 94 int64 received_bytes_; 95 96 // The current state of this save item. 97 SaveState state_; 98 99 // Specifies if this name is a final or not. 100 bool has_final_name_; 101 102 // Flag indicates whether SaveItem has error while in saving process. 103 bool is_success_; 104 105 SaveFileCreateInfo::SaveFileSource save_source_; 106 107 // Our owning object. 108 SavePackage* package_; 109 110 DISALLOW_COPY_AND_ASSIGN(SaveItem); 111 }; 112 113 } // namespace content 114 115 #endif // CONTENT_BROWSER_DOWNLOAD_SAVE_ITEM_H_ 116