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