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