OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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_DOWNLOAD_ITEM_IMPL_H_ | |
6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ | |
7 #pragma once | |
8 | |
9 #include <string> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/file_path.h" | |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/observer_list.h" | |
15 #include "base/time.h" | |
16 #include "base/timer.h" | |
17 #include "content/browser/download/download_item.h" | |
18 #include "content/browser/download/download_id.h" | |
19 #include "content/browser/download/download_request_handle.h" | |
20 #include "content/browser/download/download_state_info.h" | |
21 #include "content/browser/download/interrupt_reasons.h" | |
22 #include "content/common/content_export.h" | |
23 #include "googleurl/src/gurl.h" | |
24 #include "net/base/net_errors.h" | |
25 | |
26 class DownloadFileManager; | |
27 class DownloadId; | |
28 class DownloadManager; | |
29 class TabContents; | |
30 | |
31 struct DownloadCreateInfo; | |
32 struct DownloadPersistentStoreInfo; | |
33 | |
34 // See download_item.h for usage. | |
35 class DownloadItemImpl : public DownloadItem { | |
36 public: | |
37 // Constructing from persistent store: | |
38 DownloadItemImpl(DownloadManager* download_manager, | |
39 const DownloadPersistentStoreInfo& info); | |
40 | |
41 // Constructing for a regular download. | |
42 // Takes ownership of the object pointed to by |request_handle|. | |
43 DownloadItemImpl(DownloadManager* download_manager, | |
44 const DownloadCreateInfo& info, | |
45 DownloadRequestHandleInterface* request_handle, | |
46 bool is_otr); | |
47 | |
48 // Constructing for the "Save Page As..." feature: | |
49 DownloadItemImpl(DownloadManager* download_manager, | |
50 const FilePath& path, | |
51 const GURL& url, | |
52 bool is_otr, | |
53 DownloadId download_id); | |
54 | |
55 virtual ~DownloadItemImpl(); | |
56 | |
57 // Overridden from DownloadItem. | |
58 virtual void AddObserver(DownloadItem::Observer* observer) OVERRIDE; | |
59 virtual void RemoveObserver(DownloadItem::Observer* observer) OVERRIDE; | |
60 virtual void UpdateObservers() OVERRIDE; | |
61 virtual bool CanShowInFolder() OVERRIDE; | |
62 virtual bool CanOpenDownload() OVERRIDE; | |
63 virtual bool ShouldOpenFileBasedOnExtension() OVERRIDE; | |
64 virtual void OpenDownload() OVERRIDE; | |
65 virtual void ShowDownloadInShell() OVERRIDE; | |
66 virtual void DangerousDownloadValidated() OVERRIDE; | |
67 virtual void Update(int64 bytes_so_far) OVERRIDE; | |
68 virtual void Cancel(bool user_cancel) OVERRIDE; | |
69 virtual void MarkAsComplete() OVERRIDE; | |
70 virtual void DelayedDownloadOpened() OVERRIDE; | |
71 virtual void OnAllDataSaved( | |
72 int64 size, const std::string& final_hash) OVERRIDE; | |
73 virtual void OnDownloadedFileRemoved() OVERRIDE; | |
74 virtual void Interrupted(int64 size, InterruptReason reason) OVERRIDE; | |
75 virtual void Delete(DeleteReason reason) OVERRIDE; | |
76 virtual void Remove() OVERRIDE; | |
77 virtual bool TimeRemaining(base::TimeDelta* remaining) const OVERRIDE; | |
78 virtual int64 CurrentSpeed() const OVERRIDE; | |
79 virtual int PercentComplete() const OVERRIDE; | |
80 virtual void OnPathDetermined(const FilePath& path) OVERRIDE; | |
81 virtual bool AllDataSaved() const OVERRIDE; | |
82 virtual void SetFileCheckResults(const DownloadStateInfo& state) OVERRIDE; | |
83 virtual void Rename(const FilePath& full_path) OVERRIDE; | |
84 virtual void TogglePause() OVERRIDE; | |
85 virtual void OnDownloadCompleting(DownloadFileManager* file_manager) OVERRIDE; | |
86 virtual void OnDownloadRenamedToFinalName(const FilePath& full_path) OVERRIDE; | |
87 virtual bool MatchesQuery(const string16& query) const OVERRIDE; | |
88 virtual bool IsPartialDownload() const OVERRIDE; | |
89 virtual bool IsInProgress() const OVERRIDE; | |
90 virtual bool IsCancelled() const OVERRIDE; | |
91 virtual bool IsInterrupted() const OVERRIDE; | |
92 virtual bool IsComplete() const OVERRIDE; | |
93 virtual DownloadState GetState() const OVERRIDE; | |
94 virtual const FilePath& GetFullPath() const OVERRIDE; | |
95 virtual void SetPathUniquifier(int uniquifier) OVERRIDE; | |
96 virtual const GURL& GetURL() const OVERRIDE; | |
97 virtual const std::vector<GURL>& GetUrlChain() const OVERRIDE; | |
98 virtual const GURL& GetOriginalUrl() const OVERRIDE; | |
99 virtual const GURL& GetReferrerUrl() const OVERRIDE; | |
100 virtual std::string GetSuggestedFilename() const OVERRIDE; | |
101 virtual std::string GetContentDisposition() const OVERRIDE; | |
102 virtual std::string GetMimeType() const OVERRIDE; | |
103 virtual std::string GetOriginalMimeType() const OVERRIDE; | |
104 virtual std::string GetReferrerCharset() const OVERRIDE; | |
105 virtual int64 GetTotalBytes() const OVERRIDE; | |
106 virtual void SetTotalBytes(int64 total_bytes) OVERRIDE; | |
107 virtual const std::string& GetHash() const OVERRIDE; | |
108 virtual int64 GetReceivedBytes() const OVERRIDE; | |
109 virtual int32 GetId() const OVERRIDE; | |
110 virtual DownloadId GetGlobalId() const OVERRIDE; | |
111 virtual base::Time GetStartTime() const OVERRIDE; | |
112 virtual base::Time GetEndTime() const OVERRIDE; | |
113 virtual void SetDbHandle(int64 handle) OVERRIDE; | |
114 virtual int64 GetDbHandle() const OVERRIDE; | |
115 virtual DownloadManager* GetDownloadManager() OVERRIDE; | |
116 virtual bool IsPaused() const OVERRIDE; | |
117 virtual bool GetOpenWhenComplete() const OVERRIDE; | |
118 virtual void SetOpenWhenComplete(bool open) OVERRIDE; | |
119 virtual bool GetFileExternallyRemoved() const OVERRIDE; | |
120 virtual SafetyState GetSafetyState() const OVERRIDE; | |
121 virtual DangerType GetDangerType() const OVERRIDE; | |
122 virtual bool IsDangerous() const OVERRIDE; | |
123 virtual void MarkFileDangerous() OVERRIDE; | |
124 virtual void MarkUrlDangerous() OVERRIDE; | |
125 virtual bool GetAutoOpened() OVERRIDE; | |
126 virtual const FilePath& GetTargetName() const OVERRIDE; | |
127 virtual bool PromptUserForSaveLocation() const OVERRIDE; | |
128 virtual bool IsOtr() const OVERRIDE; | |
129 virtual const FilePath& GetSuggestedPath() const OVERRIDE; | |
130 virtual bool IsTemporary() const OVERRIDE; | |
131 virtual void SetOpened(bool opened) OVERRIDE; | |
132 virtual bool GetOpened() const OVERRIDE; | |
133 virtual InterruptReason GetLastReason() const OVERRIDE; | |
134 virtual DownloadPersistentStoreInfo GetPersistentStoreInfo() const OVERRIDE; | |
135 virtual DownloadStateInfo GetStateInfo() const OVERRIDE; | |
136 virtual TabContents* GetTabContents() const OVERRIDE; | |
137 virtual FilePath GetTargetFilePath() const OVERRIDE; | |
138 virtual FilePath GetFileNameToReportUser() const OVERRIDE; | |
139 virtual FilePath GetUserVerifiedFilePath() const OVERRIDE; | |
140 virtual bool NeedsRename() const OVERRIDE; | |
141 virtual void OffThreadCancel(DownloadFileManager* file_manager) OVERRIDE; | |
142 virtual std::string DebugString(bool verbose) const OVERRIDE; | |
143 virtual void MockDownloadOpenForTesting() OVERRIDE; | |
144 | |
145 private: | |
146 // Construction common to all constructors. |active| should be true for new | |
147 // downloads and false for downloads from the history. | |
148 void Init(bool active); | |
149 | |
150 // Internal helper for maintaining consistent received and total sizes. | |
151 void UpdateSize(int64 size); | |
152 | |
153 // Called when the entire download operation (including renaming etc) | |
154 // is completed. | |
155 void Completed(); | |
156 | |
157 // Call to transition state; all state transitions should go through this. | |
158 void TransitionTo(DownloadState new_state); | |
159 | |
160 // Called when safety_state_ should be recomputed from is_dangerous_file | |
161 // and is_dangerous_url. | |
162 void UpdateSafetyState(); | |
163 | |
164 // Helper function to recompute |state_info_.target_name| when | |
165 // it may have changed. (If it's non-null it should be left alone, | |
166 // otherwise updated from |full_path_|.) | |
167 void UpdateTarget(); | |
168 | |
169 // State information used by the download manager. | |
170 DownloadStateInfo state_info_; | |
171 | |
172 // The handle to the request information. Used for operations outside the | |
173 // download system. | |
174 scoped_ptr<DownloadRequestHandleInterface> request_handle_; | |
175 | |
176 // Download ID assigned by DownloadResourceHandler. | |
177 DownloadId download_id_; | |
178 | |
179 // Full path to the downloaded or downloading file. | |
180 FilePath full_path_; | |
181 | |
182 // A number that should be appended to the path to make it unique, or 0 if the | |
183 // path should be used as is. | |
184 int path_uniquifier_; | |
185 | |
186 // The chain of redirects that leading up to and including the final URL. | |
187 std::vector<GURL> url_chain_; | |
188 | |
189 // The URL of the page that initiated the download. | |
190 GURL referrer_url_; | |
191 | |
192 // Suggested filename in 'download' attribute of an anchor. Details: | |
193 // http://www.whatwg.org/specs/web-apps/current-work/#downloading-hyperlinks | |
194 std::string suggested_filename_; | |
195 | |
196 // Information from the request. | |
197 // Content-disposition field from the header. | |
198 std::string content_disposition_; | |
199 | |
200 // Mime-type from the header. Subject to change. | |
201 std::string mime_type_; | |
202 | |
203 // The value of the content type header sent with the downloaded item. It | |
204 // may be different from |mime_type_|, which may be set based on heuristics | |
205 // which may look at the file extension and first few bytes of the file. | |
206 std::string original_mime_type_; | |
207 | |
208 // The charset of the referring page where the download request comes from. | |
209 // It's used to construct a suggested filename. | |
210 std::string referrer_charset_; | |
211 | |
212 // Total bytes expected | |
213 int64 total_bytes_; | |
214 | |
215 // Current received bytes | |
216 int64 received_bytes_; | |
217 | |
218 // Sha256 hash of the content. This might be empty either because | |
219 // the download isn't done yet or because the hash isn't needed | |
220 // (ChromeDownloadManagerDelegate::GenerateFileHash() returned false). | |
221 std::string hash_; | |
222 | |
223 // Last reason. | |
224 InterruptReason last_reason_; | |
225 | |
226 // Start time for calculating remaining time | |
227 base::TimeTicks start_tick_; | |
228 | |
229 // The current state of this download | |
230 DownloadState state_; | |
231 | |
232 // The views of this item in the download shelf and download tab | |
233 ObserverList<Observer> observers_; | |
234 | |
235 // Time the download was started | |
236 base::Time start_time_; | |
237 | |
238 // Time the download completed | |
239 base::Time end_time_; | |
240 | |
241 // Our persistent store handle | |
242 int64 db_handle_; | |
243 | |
244 // Our owning object | |
245 DownloadManager* download_manager_; | |
246 | |
247 // In progress downloads may be paused by the user, we note it here | |
248 bool is_paused_; | |
249 | |
250 // A flag for indicating if the download should be opened at completion. | |
251 bool open_when_complete_; | |
252 | |
253 // A flag for indicating if the downloaded file is externally removed. | |
254 bool file_externally_removed_; | |
255 | |
256 // Indicates if the download is considered potentially safe or dangerous | |
257 // (executable files are typically considered dangerous). | |
258 SafetyState safety_state_; | |
259 | |
260 // True if the download was auto-opened. We set this rather than using | |
261 // an observer as it's frequently possible for the download to be auto opened | |
262 // before the observer is added. | |
263 bool auto_opened_; | |
264 | |
265 // True if the download was initiated in an incognito window. | |
266 bool is_otr_; | |
267 | |
268 // True if the item was downloaded temporarily. | |
269 bool is_temporary_; | |
270 | |
271 // True if we've saved all the data for the download. | |
272 bool all_data_saved_; | |
273 | |
274 // Did the user open the item either directly or indirectly (such as by | |
275 // setting always open files of this type)? The shelf also sets this field | |
276 // when the user closes the shelf before the item has been opened but should | |
277 // be treated as though the user opened it. | |
278 bool opened_; | |
279 | |
280 // Do we actual open downloads when requested? For testing purposes | |
281 // only. | |
282 bool open_enabled_; | |
283 | |
284 // Did the delegate delay calling Complete on this download? | |
285 bool delegate_delayed_complete_; | |
286 | |
287 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); | |
288 }; | |
289 | |
290 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_ | |
cbentzel
2011/11/16 12:28:44
This should be CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_I
benjhayden
2011/11/18 18:59:34
Done.
| |
OLD | NEW |