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 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ | 5 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ |
6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ | 6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "base/observer_list.h" | 14 #include "base/observer_list.h" |
15 #include "base/time.h" | 15 #include "base/time.h" |
16 #include "base/timer.h" | 16 #include "base/timer.h" |
17 #include "content/browser/download/download_net_log_parameters.h" | 17 #include "content/browser/download/download_net_log_parameters.h" |
18 #include "content/browser/download/download_request_handle.h" | 18 #include "content/browser/download/download_request_handle.h" |
19 #include "content/common/content_export.h" | 19 #include "content/common/content_export.h" |
| 20 #include "content/public/browser/download_destination_observer.h" |
20 #include "content/public/browser/download_id.h" | 21 #include "content/public/browser/download_id.h" |
21 #include "content/public/browser/download_item.h" | 22 #include "content/public/browser/download_item.h" |
22 #include "googleurl/src/gurl.h" | 23 #include "googleurl/src/gurl.h" |
23 #include "net/base/net_errors.h" | 24 #include "net/base/net_errors.h" |
24 #include "net/base/net_log.h" | 25 #include "net/base/net_log.h" |
25 | 26 |
26 class DownloadItemImplDelegate; | 27 class DownloadItemImplDelegate; |
27 | 28 |
| 29 namespace content { |
| 30 class DownloadFile; |
| 31 } |
| 32 |
28 // See download_item.h for usage. | 33 // See download_item.h for usage. |
29 class CONTENT_EXPORT DownloadItemImpl : public content::DownloadItem { | 34 class CONTENT_EXPORT DownloadItemImpl |
| 35 : public content::DownloadItem, |
| 36 public content::DownloadDestinationObserver { |
30 public: | 37 public: |
31 // Note that it is the responsibility of the caller to ensure that a | 38 // Note that it is the responsibility of the caller to ensure that a |
32 // DownloadItemImplDelegate passed to a DownloadItemImpl constructor | 39 // DownloadItemImplDelegate passed to a DownloadItemImpl constructor |
33 // outlives the DownloadItemImpl. | 40 // outlives the DownloadItemImpl. |
34 | 41 |
35 // Constructing from persistent store: | 42 // Constructing from persistent store: |
36 // |bound_net_log| is constructed externally for our use. | 43 // |bound_net_log| is constructed externally for our use. |
37 DownloadItemImpl(DownloadItemImplDelegate* delegate, | 44 DownloadItemImpl(DownloadItemImplDelegate* delegate, |
38 content::DownloadId download_id, | 45 content::DownloadId download_id, |
39 const content::DownloadPersistentStoreInfo& info, | 46 const content::DownloadPersistentStoreInfo& info, |
(...skipping 12 matching lines...) Expand all Loading... |
52 const FilePath& path, | 59 const FilePath& path, |
53 const GURL& url, | 60 const GURL& url, |
54 content::DownloadId download_id, | 61 content::DownloadId download_id, |
55 const std::string& mime_type, | 62 const std::string& mime_type, |
56 const net::BoundNetLog& bound_net_log); | 63 const net::BoundNetLog& bound_net_log); |
57 | 64 |
58 virtual ~DownloadItemImpl(); | 65 virtual ~DownloadItemImpl(); |
59 | 66 |
60 // Implementation functions (not part of the DownloadItem interface). | 67 // Implementation functions (not part of the DownloadItem interface). |
61 | 68 |
| 69 // Start the download |
| 70 virtual void Start(scoped_ptr<content::DownloadFile> download_file); |
| 71 |
62 // Called when the target path has been determined. |target_path| is the | 72 // Called when the target path has been determined. |target_path| is the |
63 // suggested target path. |disposition| indicates how the target path should | 73 // suggested target path. |disposition| indicates how the target path should |
64 // be used (see TargetDisposition). |danger_type| is the danger level of | 74 // be used (see TargetDisposition). |danger_type| is the danger level of |
65 // |target_path| as determined by the caller. |intermediate_path| is the path | 75 // |target_path| as determined by the caller. |intermediate_path| is the path |
66 // to use to store the download until OnDownloadCompleting() is called. | 76 // to use to store the download until OnDownloadCompleting() is called. |
67 virtual void OnDownloadTargetDetermined( | 77 virtual void OnDownloadTargetDetermined( |
68 const FilePath& target_path, | 78 const FilePath& target_path, |
69 TargetDisposition disposition, | 79 TargetDisposition disposition, |
70 content::DownloadDangerType danger_type, | 80 content::DownloadDangerType danger_type, |
71 const FilePath& intermediate_path); | 81 const FilePath& intermediate_path); |
72 | 82 |
73 // Indicate that an error has occurred on the download. | 83 // Indicate that an error has occurred on the download. |
74 virtual void Interrupt(content::DownloadInterruptReason reason); | 84 virtual void Interrupt(content::DownloadInterruptReason reason); |
75 | 85 |
76 // Mark the item as having been persisted. | 86 // Mark the item as having been persisted. |
77 virtual void SetIsPersisted(); | 87 virtual void SetIsPersisted(); |
78 | 88 |
79 // Set the item's DB handle. | 89 // Set the item's DB handle. |
80 virtual void SetDbHandle(int64 handle); | 90 virtual void SetDbHandle(int64 handle); |
81 | 91 |
82 // Cancels the off-thread aspects of the download. | |
83 // TODO(rdsmith): This should be private and only called from | |
84 // DownloadItem::Cancel/Interrupt; it isn't now because we can't | |
85 // call those functions from | |
86 // DownloadManager::FileSelectionCancelled() without doing some | |
87 // rewrites of the DownloadManager queues. | |
88 virtual void OffThreadCancel(); | |
89 | |
90 // Called when the downloaded file is removed. | 92 // Called when the downloaded file is removed. |
91 virtual void OnDownloadedFileRemoved(); | 93 virtual void OnDownloadedFileRemoved(); |
92 | 94 |
93 // Called when the download is ready to complete. | 95 // Called when the download is ready to complete. |
94 // This may perform final rename if necessary and will eventually call | 96 // This may perform final rename if necessary and will eventually call |
95 // DownloadItem::Completed(). | 97 // DownloadItem::Completed(). |
96 virtual void OnDownloadCompleting(); | 98 virtual void OnDownloadCompleting(); |
97 | 99 |
98 // Called periodically from the download thread, or from the UI thread | 100 // Called periodically from the download thread, or from the UI thread |
99 // for saving packages. | 101 // for saving packages. |
100 // |bytes_so_far| is the number of bytes received so far. | 102 // |bytes_so_far| is the number of bytes received so far. |
101 // |hash_state| is the current hash state. | 103 // |hash_state| is the current hash state. |
102 virtual void UpdateProgress(int64 bytes_so_far, | 104 virtual void UpdateProgress(int64 bytes_so_far, |
103 int64 bytes_per_sec, | 105 int64 bytes_per_sec, |
104 const std::string& hash_state); | 106 const std::string& hash_state); |
105 | 107 |
106 // Called by SavePackage to display progress when the DownloadItem | 108 // Called by SavePackage to display progress when the DownloadItem |
107 // should be considered complete. | 109 // should be considered complete. |
108 virtual void MarkAsComplete(); | 110 virtual void MarkAsComplete(); |
109 | 111 |
110 // Called when all data has been saved. Only has display effects. | 112 // Called when all data has been saved. Only has display effects. |
111 virtual void OnAllDataSaved(int64 size, const std::string& final_hash); | 113 virtual void OnAllDataSaved(const std::string& final_hash); |
112 | 114 |
113 // Called by SavePackage to set the total number of bytes on the item. | 115 // Called by SavePackage to set the total number of bytes on the item. |
114 virtual void SetTotalBytes(int64 total_bytes); | 116 virtual void SetTotalBytes(int64 total_bytes); |
115 | 117 |
| 118 // Provide a weak pointer reference to a DownloadDestinationObserver |
| 119 // for use by download destinations. |
| 120 base::WeakPtr<content::DownloadDestinationObserver> |
| 121 DestinationObserverAsWeakPtr(); |
| 122 |
116 // Notify observers that this item is being removed by the user. | 123 // Notify observers that this item is being removed by the user. |
117 virtual void NotifyRemoved(); | 124 virtual void NotifyRemoved(); |
118 | 125 |
| 126 // For dispatching on whether we're dealing with a SavePackage download. |
| 127 virtual bool IsSavePackageDownload() const; |
| 128 |
119 // Overridden from DownloadItem. | 129 // Overridden from DownloadItem. |
120 virtual void AddObserver(DownloadItem::Observer* observer) OVERRIDE; | 130 virtual void AddObserver(DownloadItem::Observer* observer) OVERRIDE; |
121 virtual void RemoveObserver(DownloadItem::Observer* observer) OVERRIDE; | 131 virtual void RemoveObserver(DownloadItem::Observer* observer) OVERRIDE; |
122 virtual void UpdateObservers() OVERRIDE; | 132 virtual void UpdateObservers() OVERRIDE; |
123 virtual bool CanShowInFolder() OVERRIDE; | 133 virtual bool CanShowInFolder() OVERRIDE; |
124 virtual bool CanOpenDownload() OVERRIDE; | 134 virtual bool CanOpenDownload() OVERRIDE; |
125 virtual bool ShouldOpenFileBasedOnExtension() OVERRIDE; | 135 virtual bool ShouldOpenFileBasedOnExtension() OVERRIDE; |
126 virtual void OpenDownload() OVERRIDE; | 136 virtual void OpenDownload() OVERRIDE; |
127 virtual void ShowDownloadInShell() OVERRIDE; | 137 virtual void ShowDownloadInShell() OVERRIDE; |
128 virtual void DangerousDownloadValidated() OVERRIDE; | 138 virtual void DangerousDownloadValidated() OVERRIDE; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 GetPersistentStoreInfo() const OVERRIDE; | 200 GetPersistentStoreInfo() const OVERRIDE; |
191 virtual content::BrowserContext* GetBrowserContext() const OVERRIDE; | 201 virtual content::BrowserContext* GetBrowserContext() const OVERRIDE; |
192 virtual content::WebContents* GetWebContents() const OVERRIDE; | 202 virtual content::WebContents* GetWebContents() const OVERRIDE; |
193 virtual FilePath GetFileNameToReportUser() const OVERRIDE; | 203 virtual FilePath GetFileNameToReportUser() const OVERRIDE; |
194 virtual void SetDisplayName(const FilePath& name) OVERRIDE; | 204 virtual void SetDisplayName(const FilePath& name) OVERRIDE; |
195 virtual FilePath GetUserVerifiedFilePath() const OVERRIDE; | 205 virtual FilePath GetUserVerifiedFilePath() const OVERRIDE; |
196 virtual std::string DebugString(bool verbose) const OVERRIDE; | 206 virtual std::string DebugString(bool verbose) const OVERRIDE; |
197 virtual void MockDownloadOpenForTesting() OVERRIDE; | 207 virtual void MockDownloadOpenForTesting() OVERRIDE; |
198 | 208 |
199 private: | 209 private: |
| 210 // DownloadDestinationObserver |
| 211 virtual void DestinationUpdate(int64 bytes_so_far, |
| 212 int64 bytes_per_sec, |
| 213 const std::string& hash_state) OVERRIDE; |
| 214 virtual void DestinationError( |
| 215 content::DownloadInterruptReason reason) OVERRIDE; |
| 216 virtual void DestinationCompleted(const std::string& final_hash) OVERRIDE; |
| 217 |
| 218 // For weak pointer downcasting. |
| 219 friend class base::WeakPtr<content::DownloadDestinationObserver>; |
| 220 |
200 // Construction common to all constructors. |active| should be true for new | 221 // Construction common to all constructors. |active| should be true for new |
201 // downloads and false for downloads from the history. | 222 // downloads and false for downloads from the history. |
202 // |download_type| indicates to the net log system what kind of download | 223 // |download_type| indicates to the net log system what kind of download |
203 // this is. | 224 // this is. |
204 void Init(bool active, download_net_logs::DownloadType download_type); | 225 void Init(bool active, download_net_logs::DownloadType download_type); |
205 | 226 |
206 // Returns true if the download still needs to be renamed to | 227 // Returns true if the download still needs to be renamed to |
207 // GetTargetFilePath(). | 228 // GetTargetFilePath(). |
208 bool NeedsRename() const; | 229 bool NeedsRename() const; |
209 | 230 |
210 // If all pre-requisites have been met, complete download processing, i.e. do | 231 // If all pre-requisites have been met, complete download processing, i.e. do |
211 // internal cleanup, file rename, and potentially auto-open. (Dangerous | 232 // internal cleanup, file rename, and potentially auto-open. (Dangerous |
212 // downloads still may block on user acceptance after this point.) | 233 // downloads still may block on user acceptance after this point.) |
213 void MaybeCompleteDownload(); | 234 void MaybeCompleteDownload(); |
214 | 235 |
215 // Internal helper for maintaining consistent received and total sizes, and | |
216 // setting the final hash. | |
217 // Should only be called from |OnAllDataSaved|. | |
218 void ProgressComplete(int64 bytes_so_far, | |
219 const std::string& final_hash); | |
220 | |
221 // Called when the entire download operation (including renaming etc) | 236 // Called when the entire download operation (including renaming etc) |
222 // is completed. | 237 // is completed. |
223 void Completed(); | 238 void Completed(); |
224 | 239 |
225 // Call to transition state; all state transitions should go through this. | 240 // Call to transition state; all state transitions should go through this. |
226 void TransitionTo(DownloadState new_state); | 241 void TransitionTo(DownloadState new_state); |
227 | 242 |
228 // Set the |danger_type_| and invoke obserers if necessary. | 243 // Set the |danger_type_| and invoke obserers if necessary. |
229 void SetDangerType(content::DownloadDangerType danger_type); | 244 void SetDangerType(content::DownloadDangerType danger_type); |
230 | 245 |
231 // Set the |current_path_| to |new_path|. | 246 // Set the |current_path_| to |new_path|. |
232 void SetFullPath(const FilePath& new_path); | 247 void SetFullPath(const FilePath& new_path); |
233 | 248 |
234 // Callback invoked when the download has been renamed to its final name. | 249 // Callback invoked when the download has been renamed to its final name. |
235 void OnDownloadRenamedToFinalName(content::DownloadInterruptReason reason, | 250 void OnDownloadRenamedToFinalName(content::DownloadInterruptReason reason, |
236 const FilePath& full_path); | 251 const FilePath& full_path); |
237 | 252 |
238 // Callback invoked when the download has been renamed to its intermediate | 253 // Callback invoked when the download has been renamed to its intermediate |
239 // name. | 254 // name. |
240 void OnDownloadRenamedToIntermediateName( | 255 void OnDownloadRenamedToIntermediateName( |
241 content::DownloadInterruptReason reason, const FilePath& full_path); | 256 content::DownloadInterruptReason reason, const FilePath& full_path); |
242 | 257 |
| 258 // Callback from file thread when we initialize the DownloadFile. |
| 259 void OnDownloadFileInitialized( |
| 260 content::DownloadInterruptReason result); |
| 261 |
243 // Callback from file thread when we release the DownloadFile. | 262 // Callback from file thread when we release the DownloadFile. |
244 void OnDownloadFileReleased(); | 263 void OnDownloadFileReleased(); |
245 | 264 |
| 265 // Will be false for save package downloads retrieved from the history. |
| 266 // TODO(rdsmith): Replace with a generalized enum for "download source". |
| 267 const bool is_save_package_download_; |
| 268 |
246 // The handle to the request information. Used for operations outside the | 269 // The handle to the request information. Used for operations outside the |
247 // download system. | 270 // download system. |
248 scoped_ptr<DownloadRequestHandleInterface> request_handle_; | 271 scoped_ptr<DownloadRequestHandleInterface> request_handle_; |
249 | 272 |
250 // Download ID assigned by DownloadResourceHandler. | 273 // Download ID assigned by DownloadResourceHandler. |
251 content::DownloadId download_id_; | 274 content::DownloadId download_id_; |
252 | 275 |
253 // Display name for the download. If this is empty, then the display name is | 276 // Display name for the download. If this is empty, then the display name is |
254 // considered to be |target_path_.BaseName()|. | 277 // considered to be |target_path_.BaseName()|. |
255 FilePath display_name_; | 278 FilePath display_name_; |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 // when the user closes the shelf before the item has been opened but should | 414 // when the user closes the shelf before the item has been opened but should |
392 // be treated as though the user opened it. | 415 // be treated as though the user opened it. |
393 bool opened_; | 416 bool opened_; |
394 | 417 |
395 // Do we actually open downloads when requested? For testing purposes only. | 418 // Do we actually open downloads when requested? For testing purposes only. |
396 bool open_enabled_; | 419 bool open_enabled_; |
397 | 420 |
398 // Did the delegate delay calling Complete on this download? | 421 // Did the delegate delay calling Complete on this download? |
399 bool delegate_delayed_complete_; | 422 bool delegate_delayed_complete_; |
400 | 423 |
| 424 // DownloadFile associated with this download. Note that this |
| 425 // pointer may only be used or destroyed on the FILE thread. |
| 426 // This pointer will be non-null only while the DownloadItem is in |
| 427 // the IN_PROGRESS state. |
| 428 scoped_ptr<content::DownloadFile> download_file_; |
| 429 |
401 // Net log to use for this download. | 430 // Net log to use for this download. |
402 const net::BoundNetLog bound_net_log_; | 431 const net::BoundNetLog bound_net_log_; |
403 | 432 |
404 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_; | 433 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_; |
405 | 434 |
406 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); | 435 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); |
407 }; | 436 }; |
408 | 437 |
409 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ | 438 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ |
OLD | NEW |