OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // Each download is represented by a DownloadItem, and all DownloadItems | 5 // Each download is represented by a DownloadItem, and all DownloadItems |
6 // are owned by the DownloadManager which maintains a global list of all | 6 // are owned by the DownloadManager which maintains a global list of all |
7 // downloads. DownloadItems are created when a user initiates a download, | 7 // downloads. DownloadItems are created when a user initiates a download, |
8 // and exist for the duration of the browser life time. | 8 // and exist for the duration of the browser life time. |
9 // | 9 // |
10 // Download observers: | 10 // Download observers: |
11 // DownloadItem::Observer: | 11 // DownloadItem::Observer: |
12 // - allows observers to receive notifications about one download from start | 12 // - allows observers to receive notifications about one download from start |
13 // to completion | 13 // to completion |
14 // Use AddObserver() / RemoveObserver() on the appropriate download object to | 14 // Use AddObserver() / RemoveObserver() on the appropriate download object to |
15 // receive state updates. | 15 // receive state updates. |
16 | 16 |
17 #ifndef CONTENT_PUBLIC_BROWSER_DOWNLOAD_ITEM_H_ | 17 #ifndef CONTENT_PUBLIC_BROWSER_DOWNLOAD_ITEM_H_ |
18 #define CONTENT_PUBLIC_BROWSER_DOWNLOAD_ITEM_H_ | 18 #define CONTENT_PUBLIC_BROWSER_DOWNLOAD_ITEM_H_ |
19 | 19 |
20 #include <map> | 20 #include <map> |
21 #include <string> | 21 #include <string> |
22 #include <vector> | 22 #include <vector> |
23 | 23 |
24 #include "base/file_path.h" | 24 #include "base/file_path.h" |
25 #include "base/string16.h" | 25 #include "base/string16.h" |
| 26 #include "base/supports_user_data.h" |
26 #include "content/public/browser/download_danger_type.h" | 27 #include "content/public/browser/download_danger_type.h" |
27 #include "content/public/browser/download_interrupt_reasons.h" | 28 #include "content/public/browser/download_interrupt_reasons.h" |
28 #include "content/public/common/page_transition_types.h" | 29 #include "content/public/common/page_transition_types.h" |
29 | 30 |
30 class FilePath; | 31 class FilePath; |
31 class GURL; | 32 class GURL; |
32 struct DownloadCreateInfo; | 33 struct DownloadCreateInfo; |
33 | 34 |
34 namespace base { | 35 namespace base { |
35 class Time; | 36 class Time; |
36 class TimeDelta; | 37 class TimeDelta; |
37 } | 38 } |
38 | 39 |
39 namespace content { | 40 namespace content { |
40 | 41 |
41 class BrowserContext; | 42 class BrowserContext; |
42 class DownloadId; | 43 class DownloadId; |
43 class DownloadManager; | 44 class DownloadManager; |
44 class WebContents; | 45 class WebContents; |
45 struct DownloadPersistentStoreInfo; | 46 struct DownloadPersistentStoreInfo; |
46 | 47 |
47 // One DownloadItem per download. This is the model class that stores all the | 48 // One DownloadItem per download. This is the model class that stores all the |
48 // state for a download. Multiple views, such as a tab's download shelf and the | 49 // state for a download. Multiple views, such as a tab's download shelf and the |
49 // Destination tab's download view, may refer to a given DownloadItem. | 50 // Destination tab's download view, may refer to a given DownloadItem. |
50 // | 51 // |
51 // This is intended to be used only on the UI thread. | 52 // This is intended to be used only on the UI thread. |
52 class CONTENT_EXPORT DownloadItem { | 53 class CONTENT_EXPORT DownloadItem : public base::SupportsUserData { |
53 public: | 54 public: |
54 enum DownloadState { | 55 enum DownloadState { |
55 // Download is actively progressing. | 56 // Download is actively progressing. |
56 IN_PROGRESS = 0, | 57 IN_PROGRESS = 0, |
57 | 58 |
58 // Download is completely finished. | 59 // Download is completely finished. |
59 COMPLETE, | 60 COMPLETE, |
60 | 61 |
61 // Download has been cancelled. | 62 // Download has been cancelled. |
62 CANCELLED, | 63 CANCELLED, |
63 | 64 |
64 // This state indicates that the download item is about to be destroyed, | |
65 // and observers seeing this state should release all references. | |
66 REMOVING, | |
67 | |
68 // This state indicates that the download has been interrupted. | 65 // This state indicates that the download has been interrupted. |
69 INTERRUPTED, | 66 INTERRUPTED, |
70 | 67 |
71 // Maximum value. | 68 // Maximum value. |
72 MAX_DOWNLOAD_STATE | 69 MAX_DOWNLOAD_STATE |
73 }; | 70 }; |
74 | 71 |
75 enum SafetyState { | 72 enum SafetyState { |
76 SAFE = 0, | 73 SAFE = 0, |
77 DANGEROUS, | 74 DANGEROUS, |
(...skipping 17 matching lines...) Expand all Loading... |
95 // A fake download table ID which represents a download that has started, | 92 // A fake download table ID which represents a download that has started, |
96 // but is not yet in the table. | 93 // but is not yet in the table. |
97 static const int kUninitializedHandle; | 94 static const int kUninitializedHandle; |
98 | 95 |
99 static const char kEmptyFileHash[]; | 96 static const char kEmptyFileHash[]; |
100 | 97 |
101 // Interface that observers of a particular download must implement in order | 98 // Interface that observers of a particular download must implement in order |
102 // to receive updates to the download's status. | 99 // to receive updates to the download's status. |
103 class CONTENT_EXPORT Observer { | 100 class CONTENT_EXPORT Observer { |
104 public: | 101 public: |
105 virtual void OnDownloadUpdated(DownloadItem* download) = 0; | 102 virtual void OnDownloadUpdated(DownloadItem* download) {} |
106 | 103 |
107 // Called when a downloaded file has been opened. | 104 // Called when a downloaded file has been opened. |
108 virtual void OnDownloadOpened(DownloadItem* download) = 0; | 105 virtual void OnDownloadOpened(DownloadItem* download) {} |
| 106 |
| 107 // Called when the user removes a download. |
| 108 virtual void OnDownloadRemoved(DownloadItem* download) {} |
| 109 |
| 110 // Called when the download is being destroyed. This happens after |
| 111 // every OnDownloadRemoved() as well as when the DownloadManager is going |
| 112 // down. |
| 113 virtual void OnDownloadDestroyed(DownloadItem* download) {} |
109 | 114 |
110 protected: | 115 protected: |
111 virtual ~Observer() {} | 116 virtual ~Observer() {} |
112 }; | 117 }; |
113 | 118 |
114 // Interface for data that can be stored associated with (and owned | |
115 // by) an object of this class via GetExternalData/SetExternalData. | |
116 class ExternalData { | |
117 public: | |
118 virtual ~ExternalData() {}; | |
119 }; | |
120 | |
121 virtual ~DownloadItem() {} | 119 virtual ~DownloadItem() {} |
122 | 120 |
123 virtual void AddObserver(DownloadItem::Observer* observer) = 0; | 121 virtual void AddObserver(DownloadItem::Observer* observer) = 0; |
124 virtual void RemoveObserver(DownloadItem::Observer* observer) = 0; | 122 virtual void RemoveObserver(DownloadItem::Observer* observer) = 0; |
125 | 123 |
126 // Notifies our observers periodically. | 124 // Notifies our observers periodically. |
127 virtual void UpdateObservers() = 0; | 125 virtual void UpdateObservers() = 0; |
128 | 126 |
129 // Returns true if it is OK to open a folder which this file is inside. | 127 // Returns true if it is OK to open a folder which this file is inside. |
130 virtual bool CanShowInFolder() = 0; | 128 virtual bool CanShowInFolder() = 0; |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 virtual const FilePath& GetTargetFilePath() const = 0; | 216 virtual const FilePath& GetTargetFilePath() const = 0; |
219 | 217 |
220 // Get the target disposition. | 218 // Get the target disposition. |
221 virtual TargetDisposition GetTargetDisposition() const = 0; | 219 virtual TargetDisposition GetTargetDisposition() const = 0; |
222 | 220 |
223 // Called if a check of the download contents was performed and the results of | 221 // Called if a check of the download contents was performed and the results of |
224 // the test are available. This should only be called after AllDataSaved() is | 222 // the test are available. This should only be called after AllDataSaved() is |
225 // true. | 223 // true. |
226 virtual void OnContentCheckCompleted(DownloadDangerType danger_type) = 0; | 224 virtual void OnContentCheckCompleted(DownloadDangerType danger_type) = 0; |
227 | 225 |
228 virtual bool IsPersisted() const = 0; | |
229 | |
230 // Accessors | 226 // Accessors |
231 virtual const std::string& GetHash() const = 0; | 227 virtual const std::string& GetHash() const = 0; |
232 virtual DownloadState GetState() const = 0; | 228 virtual DownloadState GetState() const = 0; |
233 virtual const GURL& GetURL() const = 0; | 229 virtual const GURL& GetURL() const = 0; |
234 virtual const std::vector<GURL>& GetUrlChain() const = 0; | 230 virtual const std::vector<GURL>& GetUrlChain() const = 0; |
235 virtual const GURL& GetOriginalUrl() const = 0; | 231 virtual const GURL& GetOriginalUrl() const = 0; |
236 virtual const GURL& GetReferrerUrl() const = 0; | 232 virtual const GURL& GetReferrerUrl() const = 0; |
237 virtual std::string GetSuggestedFilename() const = 0; | 233 virtual std::string GetSuggestedFilename() const = 0; |
238 virtual std::string GetContentDisposition() const = 0; | 234 virtual std::string GetContentDisposition() const = 0; |
239 virtual std::string GetMimeType() const = 0; | 235 virtual std::string GetMimeType() const = 0; |
240 virtual std::string GetOriginalMimeType() const = 0; | 236 virtual std::string GetOriginalMimeType() const = 0; |
241 virtual std::string GetReferrerCharset() const = 0; | 237 virtual std::string GetReferrerCharset() const = 0; |
242 virtual std::string GetRemoteAddress() const = 0; | 238 virtual std::string GetRemoteAddress() const = 0; |
243 virtual int64 GetTotalBytes() const = 0; | 239 virtual int64 GetTotalBytes() const = 0; |
244 virtual int64 GetReceivedBytes() const = 0; | 240 virtual int64 GetReceivedBytes() const = 0; |
245 virtual const std::string& GetHashState() const = 0; | 241 virtual const std::string& GetHashState() const = 0; |
246 virtual int32 GetId() const = 0; | 242 virtual int32 GetId() const = 0; |
247 virtual DownloadId GetGlobalId() const = 0; | 243 virtual DownloadId GetGlobalId() const = 0; |
248 virtual base::Time GetStartTime() const = 0; | 244 virtual base::Time GetStartTime() const = 0; |
249 virtual base::Time GetEndTime() const = 0; | 245 virtual base::Time GetEndTime() const = 0; |
250 virtual int64 GetDbHandle() const = 0; | |
251 virtual bool IsPaused() const = 0; | 246 virtual bool IsPaused() const = 0; |
252 virtual bool GetOpenWhenComplete() const = 0; | 247 virtual bool GetOpenWhenComplete() const = 0; |
253 virtual void SetOpenWhenComplete(bool open) = 0; | 248 virtual void SetOpenWhenComplete(bool open) = 0; |
254 virtual bool GetFileExternallyRemoved() const = 0; | 249 virtual bool GetFileExternallyRemoved() const = 0; |
255 virtual SafetyState GetSafetyState() const = 0; | 250 virtual SafetyState GetSafetyState() const = 0; |
256 // Why |safety_state_| is not SAFE. | 251 // Why |safety_state_| is not SAFE. |
257 virtual DownloadDangerType GetDangerType() const = 0; | 252 virtual DownloadDangerType GetDangerType() const = 0; |
258 virtual bool IsDangerous() const = 0; | 253 virtual bool IsDangerous() const = 0; |
259 | 254 |
260 virtual bool GetAutoOpened() = 0; | 255 virtual bool GetAutoOpened() = 0; |
261 virtual FilePath GetTargetName() const = 0; | 256 virtual FilePath GetTargetName() const = 0; |
262 virtual const FilePath& GetForcedFilePath() const = 0; | 257 virtual const FilePath& GetForcedFilePath() const = 0; |
263 virtual bool HasUserGesture() const = 0; | 258 virtual bool HasUserGesture() const = 0; |
264 virtual PageTransition GetTransitionType() const = 0; | 259 virtual PageTransition GetTransitionType() const = 0; |
265 virtual bool IsOtr() const = 0; | 260 virtual bool IsOtr() const = 0; |
266 virtual bool IsTemporary() const = 0; | 261 virtual bool IsTemporary() const = 0; |
267 virtual void SetIsTemporary(bool temporary) = 0; | 262 virtual void SetIsTemporary(bool temporary) = 0; |
268 virtual void SetOpened(bool opened) = 0; | 263 virtual void SetOpened(bool opened) = 0; |
269 virtual bool GetOpened() const = 0; | 264 virtual bool GetOpened() const = 0; |
270 | 265 |
271 virtual const std::string& GetLastModifiedTime() const = 0; | 266 virtual const std::string& GetLastModifiedTime() const = 0; |
272 virtual const std::string& GetETag() const = 0; | 267 virtual const std::string& GetETag() const = 0; |
273 | 268 |
274 virtual DownloadInterruptReason GetLastReason() const = 0; | 269 virtual DownloadInterruptReason GetLastReason() const = 0; |
275 virtual DownloadPersistentStoreInfo GetPersistentStoreInfo() const = 0; | |
276 virtual BrowserContext* GetBrowserContext() const = 0; | 270 virtual BrowserContext* GetBrowserContext() const = 0; |
277 virtual WebContents* GetWebContents() const = 0; | 271 virtual WebContents* GetWebContents() const = 0; |
278 | 272 |
279 // Returns the file-name that should be reported to the user. If a display | 273 // Returns the file-name that should be reported to the user. If a display |
280 // name has been explicitly set using SetDisplayName(), this function returns | 274 // name has been explicitly set using SetDisplayName(), this function returns |
281 // that display name. Otherwise returns the final target filename. | 275 // that display name. Otherwise returns the final target filename. |
282 virtual FilePath GetFileNameToReportUser() const = 0; | 276 virtual FilePath GetFileNameToReportUser() const = 0; |
283 | 277 |
284 // Set a display name for the download that will be independent of the target | 278 // Set a display name for the download that will be independent of the target |
285 // filename. If |name| is not empty, then GetFileNameToReportUser() will | 279 // filename. If |name| is not empty, then GetFileNameToReportUser() will |
286 // return |name|. Has no effect on the final target filename. | 280 // return |name|. Has no effect on the final target filename. |
287 virtual void SetDisplayName(const FilePath& name) = 0; | 281 virtual void SetDisplayName(const FilePath& name) = 0; |
288 | 282 |
289 // Returns the user-verified target file path for the download. | 283 // Returns the user-verified target file path for the download. |
290 // This returns the same path as GetTargetFilePath() for safe downloads | 284 // This returns the same path as GetTargetFilePath() for safe downloads |
291 // but does not for dangerous downloads until the name is verified. | 285 // but does not for dangerous downloads until the name is verified. |
292 virtual FilePath GetUserVerifiedFilePath() const = 0; | 286 virtual FilePath GetUserVerifiedFilePath() const = 0; |
293 | 287 |
294 // Manage data owned by other subsystems associated with the | |
295 // DownloadItem. By custom, key is the address of a | |
296 // static char subsystem_specific_string[] = ".."; defined | |
297 // in the subsystem, but the only requirement of this interface | |
298 // is that the key be unique over all data stored with this | |
299 // DownloadItem. | |
300 // | |
301 // Note that SetExternalData takes ownership of the | |
302 // passed object; it will be destroyed when the DownloadItem is. | |
303 // If an object is already held by the DownloadItem associated with | |
304 // the passed key, it will be destroyed if overwriten by a new pointer | |
305 // (overwrites by the same pointer are ignored). | |
306 virtual ExternalData* GetExternalData(const void* key) = 0; | |
307 virtual const ExternalData* GetExternalData(const void* key) const = 0; | |
308 virtual void SetExternalData(const void* key, ExternalData* data) = 0; | |
309 | |
310 virtual std::string DebugString(bool verbose) const = 0; | 288 virtual std::string DebugString(bool verbose) const = 0; |
311 | 289 |
312 virtual void MockDownloadOpenForTesting() = 0; | 290 virtual void MockDownloadOpenForTesting() = 0; |
313 }; | 291 }; |
314 | 292 |
315 } // namespace content | 293 } // namespace content |
316 | 294 |
317 #endif // CONTENT_PUBLIC_BROWSER_DOWNLOAD_ITEM_H_ | 295 #endif // CONTENT_PUBLIC_BROWSER_DOWNLOAD_ITEM_H_ |
OLD | NEW |