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/callback_forward.h" |
24 #include "base/files/file_path.h" | 25 #include "base/files/file_path.h" |
25 #include "base/string16.h" | 26 #include "base/string16.h" |
26 #include "base/supports_user_data.h" | 27 #include "base/supports_user_data.h" |
27 #include "content/public/browser/download_danger_type.h" | 28 #include "content/public/browser/download_danger_type.h" |
28 #include "content/public/browser/download_interrupt_reasons.h" | 29 #include "content/public/browser/download_interrupt_reasons.h" |
29 #include "content/public/common/page_transition_types.h" | 30 #include "content/public/common/page_transition_types.h" |
30 | 31 |
31 class GURL; | 32 class GURL; |
32 | 33 |
33 namespace base { | 34 namespace base { |
(...skipping 26 matching lines...) Expand all Loading... |
60 // Download has been cancelled. | 61 // Download has been cancelled. |
61 CANCELLED, | 62 CANCELLED, |
62 | 63 |
63 // This state indicates that the download has been interrupted. | 64 // This state indicates that the download has been interrupted. |
64 INTERRUPTED, | 65 INTERRUPTED, |
65 | 66 |
66 // Maximum value. | 67 // Maximum value. |
67 MAX_DOWNLOAD_STATE | 68 MAX_DOWNLOAD_STATE |
68 }; | 69 }; |
69 | 70 |
70 // Reason for deleting the download. Passed to Delete(). | |
71 enum DeleteReason { | |
72 DELETE_DUE_TO_BROWSER_SHUTDOWN = 0, | |
73 DELETE_DUE_TO_USER_DISCARD | |
74 }; | |
75 | |
76 // How the final target path should be used. | 71 // How the final target path should be used. |
77 enum TargetDisposition { | 72 enum TargetDisposition { |
78 TARGET_DISPOSITION_OVERWRITE, // Overwrite if the target already exists. | 73 TARGET_DISPOSITION_OVERWRITE, // Overwrite if the target already exists. |
79 TARGET_DISPOSITION_PROMPT // Prompt the user for the actual | 74 TARGET_DISPOSITION_PROMPT // Prompt the user for the actual |
80 // target. Implies | 75 // target. Implies |
81 // TARGET_DISPOSITION_OVERWRITE. | 76 // TARGET_DISPOSITION_OVERWRITE. |
82 }; | 77 }; |
83 | 78 |
| 79 // Callback used with AcquireFileAndDeleteDownload(). |
| 80 typedef base::Callback<void(const base::FilePath&)> AcquireFileCallback; |
| 81 |
84 static const char kEmptyFileHash[]; | 82 static const char kEmptyFileHash[]; |
85 | 83 |
86 // Interface that observers of a particular download must implement in order | 84 // Interface that observers of a particular download must implement in order |
87 // to receive updates to the download's status. | 85 // to receive updates to the download's status. |
88 class CONTENT_EXPORT Observer { | 86 class CONTENT_EXPORT Observer { |
89 public: | 87 public: |
90 virtual void OnDownloadUpdated(DownloadItem* download) {} | 88 virtual void OnDownloadUpdated(DownloadItem* download) {} |
91 virtual void OnDownloadOpened(DownloadItem* download) {} | 89 virtual void OnDownloadOpened(DownloadItem* download) {} |
92 virtual void OnDownloadRemoved(DownloadItem* download) {} | 90 virtual void OnDownloadRemoved(DownloadItem* download) {} |
93 | 91 |
(...skipping 10 matching lines...) Expand all Loading... |
104 | 102 |
105 // Observation --------------------------------------------------------------- | 103 // Observation --------------------------------------------------------------- |
106 | 104 |
107 virtual void AddObserver(DownloadItem::Observer* observer) = 0; | 105 virtual void AddObserver(DownloadItem::Observer* observer) = 0; |
108 virtual void RemoveObserver(DownloadItem::Observer* observer) = 0; | 106 virtual void RemoveObserver(DownloadItem::Observer* observer) = 0; |
109 virtual void UpdateObservers() = 0; | 107 virtual void UpdateObservers() = 0; |
110 | 108 |
111 // User Actions -------------------------------------------------------------- | 109 // User Actions -------------------------------------------------------------- |
112 | 110 |
113 // Called when the user has validated the download of a dangerous file. | 111 // Called when the user has validated the download of a dangerous file. |
114 virtual void DangerousDownloadValidated() = 0; | 112 virtual void ValidateDangerousDownload() = 0; |
| 113 |
| 114 // Called to acquire a dangerous download and remove the DownloadItem from |
| 115 // views and history. |callback| will be invoked on the UI thread with the |
| 116 // path to the downloaded file. The caller is responsible for cleanup. |
| 117 // Note: It is important for |callback| to be valid since the downloaded file |
| 118 // will not be cleaned up if the callback fails. |
| 119 virtual void StealDangerousDownload(const AcquireFileCallback& callback) = 0; |
115 | 120 |
116 // Pause a download. Will have no effect if the download is already | 121 // Pause a download. Will have no effect if the download is already |
117 // paused. | 122 // paused. |
118 virtual void Pause() = 0; | 123 virtual void Pause() = 0; |
119 | 124 |
120 // Resume a download that has been paused or interrupted. Will have no effect | 125 // Resume a download that has been paused or interrupted. Will have no effect |
121 // if the download is neither. | 126 // if the download is neither. |
122 virtual void Resume() = 0; | 127 virtual void Resume() = 0; |
123 | 128 |
124 // Cancel the download operation. We need to distinguish between cancels at | 129 // Cancel the download operation. We need to distinguish between cancels at |
125 // exit (DownloadManager destructor) from user interface initiated cancels | 130 // exit (DownloadManager destructor) from user interface initiated cancels |
126 // because at exit, the history system may not exist, and any updates to it | 131 // because at exit, the history system may not exist, and any updates to it |
127 // require AddRef'ing the DownloadManager in the destructor which results in | 132 // require AddRef'ing the DownloadManager in the destructor which results in |
128 // a DCHECK failure. Set |user_cancel| to false when canceling from at | 133 // a DCHECK failure. Set |user_cancel| to false when canceling from at |
129 // exit to prevent this crash. This may result in a difference between the | 134 // exit to prevent this crash. This may result in a difference between the |
130 // downloaded file's size on disk, and what the history system's last record | 135 // downloaded file's size on disk, and what the history system's last record |
131 // of it is. At worst, we'll end up re-downloading a small portion of the file | 136 // of it is. At worst, we'll end up re-downloading a small portion of the file |
132 // when resuming a download (assuming the server supports byte ranges). | 137 // when resuming a download (assuming the server supports byte ranges). |
133 virtual void Cancel(bool user_cancel) = 0; | 138 virtual void Cancel(bool user_cancel) = 0; |
134 | 139 |
135 // Deletes the file from disk and removes the download from the views and | 140 // Removes the download from the views and history. If the download was |
136 // history. | 141 // in-progress or interrupted, then the intermediate file will also be |
137 virtual void Delete(DeleteReason reason) = 0; | 142 // deleted. |
138 | |
139 // Removes the download from the views and history. | |
140 virtual void Remove() = 0; | 143 virtual void Remove() = 0; |
141 | 144 |
142 // Open the file associated with this download. If the download is | 145 // Open the file associated with this download. If the download is |
143 // still in progress, marks the download to be opened when it is complete. | 146 // still in progress, marks the download to be opened when it is complete. |
144 virtual void OpenDownload() = 0; | 147 virtual void OpenDownload() = 0; |
145 | 148 |
146 // Show the download via the OS shell. | 149 // Show the download via the OS shell. |
147 virtual void ShowDownloadInShell() = 0; | 150 virtual void ShowDownloadInShell() = 0; |
148 | 151 |
149 // State accessors ----------------------------------------------------------- | 152 // State accessors ----------------------------------------------------------- |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 virtual const std::string& GetHash() const = 0; | 237 virtual const std::string& GetHash() const = 0; |
235 | 238 |
236 // Intermediate hash state, for persisting partial downloads. | 239 // Intermediate hash state, for persisting partial downloads. |
237 virtual const std::string& GetHashState() const = 0; | 240 virtual const std::string& GetHashState() const = 0; |
238 | 241 |
239 // True if the file associated with the download has been removed by | 242 // True if the file associated with the download has been removed by |
240 // external action. | 243 // external action. |
241 virtual bool GetFileExternallyRemoved() const = 0; | 244 virtual bool GetFileExternallyRemoved() const = 0; |
242 | 245 |
243 // True if the file that will be written by the download is dangerous | 246 // True if the file that will be written by the download is dangerous |
244 // and we will require a call to DangerousDownloadValidated() to complete. | 247 // and we will require a call to ValidateDangerousDownload() to complete. |
245 // False if the download is safe or that function has been called. | 248 // False if the download is safe or that function has been called. |
246 virtual bool IsDangerous() const = 0; | 249 virtual bool IsDangerous() const = 0; |
247 | 250 |
248 // Why |safety_state_| is not SAFE. | 251 // Why |safety_state_| is not SAFE. |
249 virtual DownloadDangerType GetDangerType() const = 0; | 252 virtual DownloadDangerType GetDangerType() const = 0; |
250 | 253 |
251 // Progress State accessors ----------------------------------------------- | 254 // Progress State accessors ----------------------------------------------- |
252 | 255 |
253 // Simple calculation of the amount of time remaining to completion. Fills | 256 // Simple calculation of the amount of time remaining to completion. Fills |
254 // |*remaining| with the amount of time remaining if successful. Fails and | 257 // |*remaining| with the amount of time remaining if successful. Fails and |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 // return |name|. Has no effect on the final target filename. | 323 // return |name|. Has no effect on the final target filename. |
321 virtual void SetDisplayName(const base::FilePath& name) = 0; | 324 virtual void SetDisplayName(const base::FilePath& name) = 0; |
322 | 325 |
323 // Debug/testing ------------------------------------------------------------- | 326 // Debug/testing ------------------------------------------------------------- |
324 virtual std::string DebugString(bool verbose) const = 0; | 327 virtual std::string DebugString(bool verbose) const = 0; |
325 }; | 328 }; |
326 | 329 |
327 } // namespace content | 330 } // namespace content |
328 | 331 |
329 #endif // CONTENT_PUBLIC_BROWSER_DOWNLOAD_ITEM_H_ | 332 #endif // CONTENT_PUBLIC_BROWSER_DOWNLOAD_ITEM_H_ |
OLD | NEW |