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