Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / download / save_item.h
blobd7cbb4f0329ccb29fed9ba970690ec8c1faff064
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_
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"
14 namespace content {
15 class SavePackage;
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
28 SaveItem(const GURL& url,
29 const Referrer& referrer,
30 SavePackage* package,
31 SaveFileCreateInfo::SaveFileSource save_source);
33 ~SaveItem();
35 void Start();
37 // Received a new chunk of data.
38 void Update(int64 bytes_so_far);
40 // Cancel saving item.
41 void Cancel();
43 // Saving operation completed.
44 void Finish(int64 size, bool is_success);
46 // Rough percent complete, -1 means we don't know (since we didn't receive a
47 // total size).
48 int PercentComplete() const;
50 // Update path for SaveItem, the actual file is renamed on the file thread.
51 void Rename(const base::FilePath& full_path);
53 void SetSaveId(int32 save_id);
55 void SetTotalBytes(int64 total_bytes);
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_;
71 SavePackage* package() const { return package_; }
73 private:
74 // Internal helper for maintaining consistent received and total sizes.
75 void UpdateSize(int64 size);
77 // Request ID assigned by the ResourceDispatcherHost.
78 int32 save_id_;
80 // Full path to the save item file.
81 base::FilePath full_path_;
83 // Short display version of the file.
84 base::FilePath file_name_;
86 // The URL for this save item.
87 GURL url_;
88 Referrer referrer_;
90 // Total bytes expected.
91 int64 total_bytes_;
93 // Current received bytes.
94 int64 received_bytes_;
96 // The current state of this save item.
97 SaveState state_;
99 // Specifies if this name is a final or not.
100 bool has_final_name_;
102 // Flag indicates whether SaveItem has error while in saving process.
103 bool is_success_;
105 SaveFileCreateInfo::SaveFileSource save_source_;
107 // Our owning object.
108 SavePackage* package_;
110 DISALLOW_COPY_AND_ASSIGN(SaveItem);
113 } // namespace content
115 #endif // CONTENT_BROWSER_DOWNLOAD_SAVE_ITEM_H_