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_controller.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::DownloadDestinationController { | |
| 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 // Indicate that an error has occurred on the download. | 74 // Indicate that an error has occurred on the download. |
| 65 virtual void Interrupt(content::DownloadInterruptReason reason); | 75 virtual void Interrupt(content::DownloadInterruptReason reason); |
| 66 | 76 |
| 67 // Mark the item as having been persisted. | 77 // Mark the item as having been persisted. |
| 68 virtual void SetIsPersisted(); | 78 virtual void SetIsPersisted(); |
| 69 | 79 |
| 70 // Set the item's DB handle. | 80 // Set the item's DB handle. |
| 71 virtual void SetDbHandle(int64 handle); | 81 virtual void SetDbHandle(int64 handle); |
| 72 | 82 |
| 73 // Cancels the off-thread aspects of the download. | 83 // Cancels the off-thread aspects of the download. |
| 74 // TODO(rdsmith): This should be private and only called from | 84 // TODO(rdsmith): This should be private and only called from |
| 75 // DownloadItem::Cancel/Interrupt; it isn't now because we can't | 85 // DownloadItem::Cancel/Interrupt; it isn't now because we can't |
| 76 // call those functions from | 86 // call those functions from |
| 77 // DownloadManager::FileSelectionCancelled() without doing some | 87 // DownloadManager::FileSelectionCancelled() without doing some |
| 78 // rewrites of the DownloadManager queues. | 88 // rewrites of the DownloadManager queues. |
| 79 virtual void OffThreadCancel(DownloadFileManager* file_manager); | 89 virtual void OffThreadCancel(); |
| 80 | 90 |
| 81 // Called when the downloaded file is removed. | 91 // Called when the downloaded file is removed. |
| 82 virtual void OnDownloadedFileRemoved(); | 92 virtual void OnDownloadedFileRemoved(); |
| 83 | 93 |
| 84 // Called when the download is ready to complete. | 94 // Called when the download is ready to complete. |
| 85 // This may perform final rename if necessary and will eventually call | 95 // This may perform final rename if necessary and will eventually call |
| 86 // DownloadItem::Completed(). | 96 // DownloadItem::Completed(). |
| 87 virtual void OnDownloadCompleting(DownloadFileManager* file_manager); | 97 virtual void OnDownloadCompleting(); |
| 88 | 98 |
| 89 // Called periodically from the download thread, or from the UI thread | 99 // Called periodically from the download thread, or from the UI thread |
| 90 // for saving packages. | 100 // for saving packages. |
| 91 // |bytes_so_far| is the number of bytes received so far. | 101 // |bytes_so_far| is the number of bytes received so far. |
| 92 // |hash_state| is the current hash state. | 102 // |hash_state| is the current hash state. |
| 93 virtual void UpdateProgress(int64 bytes_so_far, | 103 virtual void UpdateProgress(int64 bytes_so_far, |
| 94 int64 bytes_per_sec, | 104 int64 bytes_per_sec, |
| 95 const std::string& hash_state); | 105 const std::string& hash_state); |
| 96 | 106 |
| 97 // Called by SavePackage to display progress when the DownloadItem | 107 // Called by SavePackage to display progress when the DownloadItem |
| 98 // should be considered complete. | 108 // should be considered complete. |
| 99 virtual void MarkAsComplete(); | 109 virtual void MarkAsComplete(); |
| 100 | 110 |
| 101 // Called when all data has been saved. Only has display effects. | 111 // Called when all data has been saved. Only has display effects. |
| 102 virtual void OnAllDataSaved(int64 size, const std::string& final_hash); | 112 virtual void OnAllDataSaved(const std::string& final_hash); |
| 103 | 113 |
| 104 // Called by SavePackage to set the total number of bytes on the item. | 114 // Called by SavePackage to set the total number of bytes on the item. |
| 105 virtual void SetTotalBytes(int64 total_bytes); | 115 virtual void SetTotalBytes(int64 total_bytes); |
| 106 | 116 |
| 117 // Provide a weak pointer reference to a DownloadDestinationController | |
| 118 // for use by download destinations. | |
| 119 base::WeakPtr<content::DownloadDestinationController> | |
| 120 DestinationControllerAsWeakPtr(); | |
| 121 | |
| 107 // Overridden from DownloadItem. | 122 // Overridden from DownloadItem. |
| 108 virtual void AddObserver(DownloadItem::Observer* observer) OVERRIDE; | 123 virtual void AddObserver(DownloadItem::Observer* observer) OVERRIDE; |
| 109 virtual void RemoveObserver(DownloadItem::Observer* observer) OVERRIDE; | 124 virtual void RemoveObserver(DownloadItem::Observer* observer) OVERRIDE; |
| 110 virtual void UpdateObservers() OVERRIDE; | 125 virtual void UpdateObservers() OVERRIDE; |
| 111 virtual bool CanShowInFolder() OVERRIDE; | 126 virtual bool CanShowInFolder() OVERRIDE; |
| 112 virtual bool CanOpenDownload() OVERRIDE; | 127 virtual bool CanOpenDownload() OVERRIDE; |
| 113 virtual bool ShouldOpenFileBasedOnExtension() OVERRIDE; | 128 virtual bool ShouldOpenFileBasedOnExtension() OVERRIDE; |
| 114 virtual void OpenDownload() OVERRIDE; | 129 virtual void OpenDownload() OVERRIDE; |
| 115 virtual void ShowDownloadInShell() OVERRIDE; | 130 virtual void ShowDownloadInShell() OVERRIDE; |
| 116 virtual void DangerousDownloadValidated() OVERRIDE; | 131 virtual void DangerousDownloadValidated() OVERRIDE; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 133 virtual const FilePath& GetFullPath() const OVERRIDE; | 148 virtual const FilePath& GetFullPath() const OVERRIDE; |
| 134 virtual const FilePath& GetTargetFilePath() const OVERRIDE; | 149 virtual const FilePath& GetTargetFilePath() const OVERRIDE; |
| 135 virtual TargetDisposition GetTargetDisposition() const OVERRIDE; | 150 virtual TargetDisposition GetTargetDisposition() const OVERRIDE; |
| 136 virtual void OnTargetPathDetermined( | 151 virtual void OnTargetPathDetermined( |
| 137 const FilePath& target_path, | 152 const FilePath& target_path, |
| 138 TargetDisposition disposition, | 153 TargetDisposition disposition, |
| 139 content::DownloadDangerType danger_type) OVERRIDE; | 154 content::DownloadDangerType danger_type) OVERRIDE; |
| 140 virtual void OnTargetPathSelected(const FilePath& target_path) OVERRIDE; | 155 virtual void OnTargetPathSelected(const FilePath& target_path) OVERRIDE; |
| 141 virtual void OnContentCheckCompleted( | 156 virtual void OnContentCheckCompleted( |
| 142 content::DownloadDangerType danger_type) OVERRIDE; | 157 content::DownloadDangerType danger_type) OVERRIDE; |
| 143 virtual void OnIntermediatePathDetermined(DownloadFileManager* file_manager, | 158 virtual void OnIntermediatePathDetermined(const FilePath& path) OVERRIDE; |
| 144 const FilePath& path) OVERRIDE; | |
| 145 virtual const GURL& GetURL() const OVERRIDE; | 159 virtual const GURL& GetURL() const OVERRIDE; |
| 146 virtual const std::vector<GURL>& GetUrlChain() const OVERRIDE; | 160 virtual const std::vector<GURL>& GetUrlChain() const OVERRIDE; |
| 147 virtual const GURL& GetOriginalUrl() const OVERRIDE; | 161 virtual const GURL& GetOriginalUrl() const OVERRIDE; |
| 148 virtual const GURL& GetReferrerUrl() const OVERRIDE; | 162 virtual const GURL& GetReferrerUrl() const OVERRIDE; |
| 149 virtual std::string GetSuggestedFilename() const OVERRIDE; | 163 virtual std::string GetSuggestedFilename() const OVERRIDE; |
| 150 virtual std::string GetContentDisposition() const OVERRIDE; | 164 virtual std::string GetContentDisposition() const OVERRIDE; |
| 151 virtual std::string GetMimeType() const OVERRIDE; | 165 virtual std::string GetMimeType() const OVERRIDE; |
| 152 virtual std::string GetOriginalMimeType() const OVERRIDE; | 166 virtual std::string GetOriginalMimeType() const OVERRIDE; |
| 153 virtual std::string GetReferrerCharset() const OVERRIDE; | 167 virtual std::string GetReferrerCharset() const OVERRIDE; |
| 154 virtual std::string GetRemoteAddress() const OVERRIDE; | 168 virtual std::string GetRemoteAddress() const OVERRIDE; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 189 virtual FilePath GetFileNameToReportUser() const OVERRIDE; | 203 virtual FilePath GetFileNameToReportUser() const OVERRIDE; |
| 190 virtual void SetDisplayName(const FilePath& name) OVERRIDE; | 204 virtual void SetDisplayName(const FilePath& name) OVERRIDE; |
| 191 virtual FilePath GetUserVerifiedFilePath() const OVERRIDE; | 205 virtual FilePath GetUserVerifiedFilePath() const OVERRIDE; |
| 192 virtual std::string DebugString(bool verbose) const OVERRIDE; | 206 virtual std::string DebugString(bool verbose) const OVERRIDE; |
| 193 virtual void MockDownloadOpenForTesting() OVERRIDE; | 207 virtual void MockDownloadOpenForTesting() OVERRIDE; |
| 194 virtual ExternalData* GetExternalData(const void* key) OVERRIDE; | 208 virtual ExternalData* GetExternalData(const void* key) OVERRIDE; |
| 195 virtual const ExternalData* GetExternalData(const void* key) const OVERRIDE; | 209 virtual const ExternalData* GetExternalData(const void* key) const OVERRIDE; |
| 196 virtual void SetExternalData(const void* key, ExternalData* data) OVERRIDE; | 210 virtual void SetExternalData(const void* key, ExternalData* data) OVERRIDE; |
| 197 | 211 |
| 198 private: | 212 private: |
| 213 // DownloadDestinationController | |
| 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 downlcasting. | |
|
benjhayden
2012/07/25 15:19:16
I have the same problem! You start typing some wor
Randy Smith (Not in Mondays)
2012/07/30 01:07:23
I couldn't figure out what you were talking about
| |
| 222 friend class base::WeakPtr<content::DownloadDestinationController>; | |
| 223 | |
| 199 // Construction common to all constructors. |active| should be true for new | 224 // Construction common to all constructors. |active| should be true for new |
| 200 // downloads and false for downloads from the history. | 225 // downloads and false for downloads from the history. |
| 201 // |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 |
| 202 // this is. | 227 // this is. |
| 203 void Init(bool active, download_net_logs::DownloadType download_type); | 228 void Init(bool active, download_net_logs::DownloadType download_type); |
| 204 | 229 |
| 205 // Returns true if the download still needs to be renamed to | 230 // Returns true if the download still needs to be renamed to |
| 206 // GetTargetFilePath(). | 231 // GetTargetFilePath(). |
| 207 bool NeedsRename() const; | 232 bool NeedsRename() const; |
| 208 | 233 |
| 209 // 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 |
| 210 // internal cleanup, file rename, and potentially auto-open. (Dangerous | 235 // internal cleanup, file rename, and potentially auto-open. (Dangerous |
| 211 // downloads still may block on user acceptance after this point.) | 236 // downloads still may block on user acceptance after this point.) |
| 212 void MaybeCompleteDownload(); | 237 void MaybeCompleteDownload(); |
| 213 | 238 |
| 214 // Internal helper for maintaining consistent received and total sizes, and | |
| 215 // setting the final hash. | |
| 216 // Should only be called from |OnAllDataSaved|. | |
| 217 void ProgressComplete(int64 bytes_so_far, | |
| 218 const std::string& final_hash); | |
| 219 | |
| 220 // Called when the entire download operation (including renaming etc) | 239 // Called when the entire download operation (including renaming etc) |
| 221 // is completed. | 240 // is completed. |
| 222 void Completed(); | 241 void Completed(); |
| 223 | 242 |
| 224 // Call to transition state; all state transitions should go through this. | 243 // Call to transition state; all state transitions should go through this. |
| 225 void TransitionTo(DownloadState new_state); | 244 void TransitionTo(DownloadState new_state); |
| 226 | 245 |
| 227 // Set the |danger_type_| and invoke obserers if necessary. | 246 // Set the |danger_type_| and invoke obserers if necessary. |
| 228 void SetDangerType(content::DownloadDangerType danger_type); | 247 void SetDangerType(content::DownloadDangerType danger_type); |
| 229 | 248 |
| 230 // Set the |current_path_| to |new_path|. | 249 // Set the |current_path_| to |new_path|. |
| 231 void SetFullPath(const FilePath& new_path); | 250 void SetFullPath(const FilePath& new_path); |
| 232 | 251 |
| 233 // 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. |
| 234 void OnDownloadRenamedToFinalName(DownloadFileManager* file_manager, | 253 void OnDownloadRenamedToFinalName(content::DownloadInterruptReason reason, |
| 235 content::DownloadInterruptReason reason, | |
| 236 const FilePath& full_path); | 254 const FilePath& full_path); |
| 237 | 255 |
| 238 // Callback invoked when the download has been renamed to its intermediate | 256 // Callback invoked when the download has been renamed to its intermediate |
| 239 // name. | 257 // name. |
| 240 void OnDownloadRenamedToIntermediateName( | 258 void OnDownloadRenamedToIntermediateName( |
| 241 content::DownloadInterruptReason reason, const FilePath& full_path); | 259 content::DownloadInterruptReason reason, const FilePath& full_path); |
| 242 | 260 |
| 261 // Callback from file thread when we initialize the DownloadFile. | |
| 262 void OnDownloadFileInitialized( | |
| 263 content::DownloadInterruptReason result); | |
| 264 | |
| 243 // Callback from file thread when we release the DownloadFile. | 265 // Callback from file thread when we release the DownloadFile. |
| 244 void OnDownloadFileReleased(); | 266 void OnDownloadFileReleased(); |
| 245 | 267 |
| 246 // The handle to the request information. Used for operations outside the | 268 // The handle to the request information. Used for operations outside the |
| 247 // download system. | 269 // download system. |
| 248 scoped_ptr<DownloadRequestHandleInterface> request_handle_; | 270 scoped_ptr<DownloadRequestHandleInterface> request_handle_; |
| 249 | 271 |
| 250 // Download ID assigned by DownloadResourceHandler. | 272 // Download ID assigned by DownloadResourceHandler. |
| 251 content::DownloadId download_id_; | 273 content::DownloadId download_id_; |
| 252 | 274 |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 398 // Do we actually open downloads when requested? For testing purposes only. | 420 // Do we actually open downloads when requested? For testing purposes only. |
| 399 bool open_enabled_; | 421 bool open_enabled_; |
| 400 | 422 |
| 401 // Did the delegate delay calling Complete on this download? | 423 // Did the delegate delay calling Complete on this download? |
| 402 bool delegate_delayed_complete_; | 424 bool delegate_delayed_complete_; |
| 403 | 425 |
| 404 // External Data storage. All objects in the store | 426 // External Data storage. All objects in the store |
| 405 // are owned by the DownloadItemImpl. | 427 // are owned by the DownloadItemImpl. |
| 406 std::map<const void*, ExternalData*> external_data_map_; | 428 std::map<const void*, ExternalData*> external_data_map_; |
| 407 | 429 |
| 430 // DownloadFile associated with this download. Note that this | |
| 431 // pointer may only be used or destroyed on the FILE thread. | |
|
benjhayden
2012/07/25 15:19:16
Then maybe it shouldn't be scoped_ptr? scoped_ptr
Randy Smith (Not in Mondays)
2012/07/30 01:07:23
Well, I want to strongly indicate the ownership re
| |
| 432 scoped_ptr<content::DownloadFile> download_file_; | |
| 433 | |
| 408 // Net log to use for this download. | 434 // Net log to use for this download. |
| 409 const net::BoundNetLog bound_net_log_; | 435 const net::BoundNetLog bound_net_log_; |
| 410 | 436 |
| 411 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_; | 437 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_; |
| 412 | 438 |
| 413 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); | 439 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); |
| 414 }; | 440 }; |
| 415 | 441 |
| 416 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ | 442 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ |
| OLD | NEW |