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 |
119 // Overridden from DownloadItem. | 126 // Overridden from DownloadItem. |
120 virtual void AddObserver(DownloadItem::Observer* observer) OVERRIDE; | 127 virtual void AddObserver(DownloadItem::Observer* observer) OVERRIDE; |
121 virtual void RemoveObserver(DownloadItem::Observer* observer) OVERRIDE; | 128 virtual void RemoveObserver(DownloadItem::Observer* observer) OVERRIDE; |
122 virtual void UpdateObservers() OVERRIDE; | 129 virtual void UpdateObservers() OVERRIDE; |
123 virtual bool CanShowInFolder() OVERRIDE; | 130 virtual bool CanShowInFolder() OVERRIDE; |
124 virtual bool CanOpenDownload() OVERRIDE; | 131 virtual bool CanOpenDownload() OVERRIDE; |
125 virtual bool ShouldOpenFileBasedOnExtension() OVERRIDE; | 132 virtual bool ShouldOpenFileBasedOnExtension() OVERRIDE; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
190 GetPersistentStoreInfo() const OVERRIDE; | 197 GetPersistentStoreInfo() const OVERRIDE; |
191 virtual content::BrowserContext* GetBrowserContext() const OVERRIDE; | 198 virtual content::BrowserContext* GetBrowserContext() const OVERRIDE; |
192 virtual content::WebContents* GetWebContents() const OVERRIDE; | 199 virtual content::WebContents* GetWebContents() const OVERRIDE; |
193 virtual FilePath GetFileNameToReportUser() const OVERRIDE; | 200 virtual FilePath GetFileNameToReportUser() const OVERRIDE; |
194 virtual void SetDisplayName(const FilePath& name) OVERRIDE; | 201 virtual void SetDisplayName(const FilePath& name) OVERRIDE; |
195 virtual FilePath GetUserVerifiedFilePath() const OVERRIDE; | 202 virtual FilePath GetUserVerifiedFilePath() const OVERRIDE; |
196 virtual std::string DebugString(bool verbose) const OVERRIDE; | 203 virtual std::string DebugString(bool verbose) const OVERRIDE; |
197 virtual void MockDownloadOpenForTesting() OVERRIDE; | 204 virtual void MockDownloadOpenForTesting() OVERRIDE; |
198 | 205 |
199 private: | 206 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 | |
200 // Construction common to all constructors. |active| should be true for new | 218 // Construction common to all constructors. |active| should be true for new |
201 // downloads and false for downloads from the history. | 219 // downloads and false for downloads from the history. |
202 // |download_type| indicates to the net log system what kind of download | 220 // |download_type| indicates to the net log system what kind of download |
203 // this is. | 221 // this is. |
204 void Init(bool active, download_net_logs::DownloadType download_type); | 222 void Init(bool active, download_net_logs::DownloadType download_type); |
205 | 223 |
206 // Returns true if the download still needs to be renamed to | 224 // Returns true if the download still needs to be renamed to |
207 // GetTargetFilePath(). | 225 // GetTargetFilePath(). |
208 bool NeedsRename() const; | 226 bool NeedsRename() const; |
209 | 227 |
210 // If all pre-requisites have been met, complete download processing, i.e. do | 228 // If all pre-requisites have been met, complete download processing, i.e. do |
211 // internal cleanup, file rename, and potentially auto-open. (Dangerous | 229 // internal cleanup, file rename, and potentially auto-open. (Dangerous |
212 // downloads still may block on user acceptance after this point.) | 230 // downloads still may block on user acceptance after this point.) |
213 void MaybeCompleteDownload(); | 231 void MaybeCompleteDownload(); |
214 | 232 |
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) | 233 // Called when the entire download operation (including renaming etc) |
222 // is completed. | 234 // is completed. |
223 void Completed(); | 235 void Completed(); |
224 | 236 |
225 // Call to transition state; all state transitions should go through this. | 237 // Call to transition state; all state transitions should go through this. |
226 void TransitionTo(DownloadState new_state); | 238 void TransitionTo(DownloadState new_state); |
227 | 239 |
228 // Set the |danger_type_| and invoke obserers if necessary. | 240 // Set the |danger_type_| and invoke obserers if necessary. |
229 void SetDangerType(content::DownloadDangerType danger_type); | 241 void SetDangerType(content::DownloadDangerType danger_type); |
230 | 242 |
231 // Set the |current_path_| to |new_path|. | 243 // Set the |current_path_| to |new_path|. |
232 void SetFullPath(const FilePath& new_path); | 244 void SetFullPath(const FilePath& new_path); |
233 | 245 |
234 // Callback invoked when the download has been renamed to its final name. | 246 // Callback invoked when the download has been renamed to its final name. |
235 void OnDownloadRenamedToFinalName(content::DownloadInterruptReason reason, | 247 void OnDownloadRenamedToFinalName(content::DownloadInterruptReason reason, |
236 const FilePath& full_path); | 248 const FilePath& full_path); |
237 | 249 |
238 // Callback invoked when the download has been renamed to its intermediate | 250 // Callback invoked when the download has been renamed to its intermediate |
239 // name. | 251 // name. |
240 void OnDownloadRenamedToIntermediateName( | 252 void OnDownloadRenamedToIntermediateName( |
241 content::DownloadInterruptReason reason, const FilePath& full_path); | 253 content::DownloadInterruptReason reason, const FilePath& full_path); |
242 | 254 |
255 // Callback from file thread when we initialize the DownloadFile. | |
256 void OnDownloadFileInitialized( | |
257 content::DownloadInterruptReason result); | |
258 | |
243 // Callback from file thread when we release the DownloadFile. | 259 // Callback from file thread when we release the DownloadFile. |
244 void OnDownloadFileReleased(); | 260 void OnDownloadFileReleased(); |
245 | 261 |
262 // Will be false for save package downloads retrieved from the history. | |
263 // TODO(rdsmith): Replace with a generalized enum for "download source". | |
264 const bool is_save_package_download_; | |
benjhayden
2012/08/22 19:07:31
How would you feel about exposing this to Download
Randy Smith (Not in Mondays)
2012/08/23 16:55:58
I think I'll skip answering the psychological ques
| |
265 | |
246 // The handle to the request information. Used for operations outside the | 266 // The handle to the request information. Used for operations outside the |
247 // download system. | 267 // download system. |
248 scoped_ptr<DownloadRequestHandleInterface> request_handle_; | 268 scoped_ptr<DownloadRequestHandleInterface> request_handle_; |
249 | 269 |
250 // Download ID assigned by DownloadResourceHandler. | 270 // Download ID assigned by DownloadResourceHandler. |
251 content::DownloadId download_id_; | 271 content::DownloadId download_id_; |
252 | 272 |
253 // Display name for the download. If this is empty, then the display name is | 273 // Display name for the download. If this is empty, then the display name is |
254 // considered to be |target_path_.BaseName()|. | 274 // considered to be |target_path_.BaseName()|. |
255 FilePath display_name_; | 275 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 | 411 // when the user closes the shelf before the item has been opened but should |
392 // be treated as though the user opened it. | 412 // be treated as though the user opened it. |
393 bool opened_; | 413 bool opened_; |
394 | 414 |
395 // Do we actually open downloads when requested? For testing purposes only. | 415 // Do we actually open downloads when requested? For testing purposes only. |
396 bool open_enabled_; | 416 bool open_enabled_; |
397 | 417 |
398 // Did the delegate delay calling Complete on this download? | 418 // Did the delegate delay calling Complete on this download? |
399 bool delegate_delayed_complete_; | 419 bool delegate_delayed_complete_; |
400 | 420 |
421 // DownloadFile associated with this download. Note that this | |
422 // pointer may only be used or destroyed on the FILE thread. | |
423 // This pointer will be non-null only while the DownloadItem is in | |
424 // the IN_PROGRESS state. | |
425 scoped_ptr<content::DownloadFile> download_file_; | |
426 | |
401 // Net log to use for this download. | 427 // Net log to use for this download. |
402 const net::BoundNetLog bound_net_log_; | 428 const net::BoundNetLog bound_net_log_; |
403 | 429 |
404 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_; | 430 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_; |
405 | 431 |
406 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); | 432 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); |
407 }; | 433 }; |
408 | 434 |
409 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ | 435 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ |
OLD | NEW |