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 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 virtual std::string DebugString(bool verbose) const OVERRIDE; | 142 virtual std::string DebugString(bool verbose) const OVERRIDE; |
136 virtual void MockDownloadOpenForTesting() OVERRIDE; | 143 virtual void MockDownloadOpenForTesting() OVERRIDE; |
137 | 144 |
138 // All remaining public interfaces virtual to allow for DownloadItemImpl | 145 // All remaining public interfaces virtual to allow for DownloadItemImpl |
139 // mocks. | 146 // mocks. |
140 | 147 |
141 // Main entry points for regular downloads, in order ------------------------- | 148 // Main entry points for regular downloads, in order ------------------------- |
142 | 149 |
143 // TODO(rdsmith): Fold the process that uses these fully into | 150 // TODO(rdsmith): Fold the process that uses these fully into |
144 // DownloadItemImpl and pass callbacks to the delegate so that all of | 151 // DownloadItemImpl and pass callbacks to the delegate so that all of |
145 // these can be made private | 152 // these other than Start() can be made private. |
146 | 153 |
147 // Called when the target path has been determined. |target_path| is the | 154 // Start the download |
148 // suggested target path. |disposition| indicates how the target path should | 155 virtual void Start(scoped_ptr<content::DownloadFile> download_file); |
149 // be used (see TargetDisposition). |danger_type| is the danger level of | |
150 // |target_path| as determined by the caller. |intermediate_path| is the path | |
151 // to use to store the download until OnDownloadCompleting() is called. | |
152 virtual void OnDownloadTargetDetermined( | |
153 const FilePath& target_path, | |
154 TargetDisposition disposition, | |
155 content::DownloadDangerType danger_type, | |
156 const FilePath& intermediate_path); | |
157 | 156 |
158 // If all pre-requisites have been met, complete download processing, i.e. do | 157 // If all pre-requisites have been met, complete download processing, i.e. do |
159 // internal cleanup, file rename, and potentially auto-open. (Dangerous | 158 // internal cleanup, file rename, and potentially auto-open. (Dangerous |
160 // downloads still may block on user acceptance after this point.) | 159 // downloads still may block on user acceptance after this point.) |
161 virtual void MaybeCompleteDownload(); | 160 virtual void MaybeCompleteDownload(); |
162 | 161 |
163 // Needed because of interwining with DownloadManagerImpl -------------------- | 162 // Needed because of interwining with DownloadManagerImpl -------------------- |
164 | 163 |
165 // TODO(rdsmith): Unwind DownloadManagerImpl and DownloadItemImpl, | 164 // TODO(rdsmith): Unwind DownloadManagerImpl and DownloadItemImpl, |
166 // removing these from the public interface. | 165 // removing these from the public interface. |
167 | 166 |
168 // Notify observers that this item is being removed by the user. | 167 // Notify observers that this item is being removed by the user. |
169 virtual void NotifyRemoved(); | 168 virtual void NotifyRemoved(); |
170 | 169 |
171 virtual void OnDownloadedFileRemoved(); | 170 virtual void OnDownloadedFileRemoved(); |
172 | 171 |
173 // Cancels the off-thread aspects of the download (such as | |
174 // the corresponding URLRequest). | |
175 // TODO(rdsmith): This should be private and only called from | |
176 // DownloadItem::Cancel/Interrupt; it isn't now because we can't | |
177 // call those functions from | |
178 // DownloadManager::FileSelectionCancelled() without doing some | |
179 // rewrites of the DownloadManager queues. | |
180 virtual void OffThreadCancel(); | |
181 | |
182 // Indicate that an error has occurred on the download. | 172 // Indicate that an error has occurred on the download. |
183 virtual void Interrupt(content::DownloadInterruptReason reason); | 173 virtual void Interrupt(content::DownloadInterruptReason reason); |
184 | 174 |
| 175 // Provide a weak pointer reference to a DownloadDestinationObserver |
| 176 // for use by download destinations. |
| 177 base::WeakPtr<content::DownloadDestinationObserver> |
| 178 DestinationObserverAsWeakPtr(); |
| 179 |
| 180 // For dispatching on whether we're dealing with a SavePackage download. |
| 181 virtual bool IsSavePackageDownload() const; |
| 182 |
185 // DownloadItemImpl routines only needed by SavePackage ---------------------- | 183 // DownloadItemImpl routines only needed by SavePackage ---------------------- |
186 | 184 |
187 // Called by SavePackage to set the total number of bytes on the item. | 185 // Called by SavePackage to set the total number of bytes on the item. |
188 virtual void SetTotalBytes(int64 total_bytes); | 186 virtual void SetTotalBytes(int64 total_bytes); |
189 | 187 |
190 // Indicate progress in saving data to its destination. | 188 // Indicate progress in saving data to its destination. |
191 // |bytes_so_far| is the number of bytes received so far. | 189 // |bytes_so_far| is the number of bytes received so far. |
192 // |hash_state| is the current hash state. | 190 // |hash_state| is the current hash state. |
193 virtual void UpdateProgress(int64 bytes_so_far, | 191 virtual void UpdateProgress(int64 bytes_so_far, |
194 int64 bytes_per_sec, | 192 int64 bytes_per_sec, |
195 const std::string& hash_state); | 193 const std::string& hash_state); |
196 | 194 |
197 virtual void OnAllDataSaved(int64 size, const std::string& final_hash); | 195 virtual void OnAllDataSaved(const std::string& final_hash); |
198 | 196 |
199 // Called by SavePackage to display progress when the DownloadItem | 197 // Called by SavePackage to display progress when the DownloadItem |
200 // should be considered complete. | 198 // should be considered complete. |
201 virtual void MarkAsComplete(); | 199 virtual void MarkAsComplete(); |
202 | 200 |
203 // Interactions with persistence system -------------------------------------- | 201 // Interactions with persistence system -------------------------------------- |
204 | 202 |
205 // TODO(benjhayden): Remove when DownloadHistory becomes an observer. | 203 // TODO(benjhayden): Remove when DownloadHistory becomes an observer. |
206 virtual void SetIsPersisted(); | 204 virtual void SetIsPersisted(); |
207 virtual void SetDbHandle(int64 handle); | 205 virtual void SetDbHandle(int64 handle); |
(...skipping 22 matching lines...) Expand all Loading... |
230 // Only incoming transition IN_PROGRESS-> | 228 // Only incoming transition IN_PROGRESS-> |
231 CANCELLED_INTERNAL, | 229 CANCELLED_INTERNAL, |
232 | 230 |
233 // An error has interrupted the download. | 231 // An error has interrupted the download. |
234 // Only incoming transition IN_PROGRESS-> | 232 // Only incoming transition IN_PROGRESS-> |
235 INTERRUPTED_INTERNAL, | 233 INTERRUPTED_INTERNAL, |
236 | 234 |
237 MAX_DOWNLOAD_INTERNAL_STATE, | 235 MAX_DOWNLOAD_INTERNAL_STATE, |
238 }; | 236 }; |
239 | 237 |
| 238 // DownloadDestinationObserver |
| 239 virtual void DestinationUpdate(int64 bytes_so_far, |
| 240 int64 bytes_per_sec, |
| 241 const std::string& hash_state) OVERRIDE; |
| 242 virtual void DestinationError( |
| 243 content::DownloadInterruptReason reason) OVERRIDE; |
| 244 virtual void DestinationCompleted(const std::string& final_hash) OVERRIDE; |
| 245 |
240 // Normal progression of a download ------------------------------------------ | 246 // Normal progression of a download ------------------------------------------ |
241 | 247 |
242 // These are listed in approximately chronological order. There are also | 248 // These are listed in approximately chronological order. There are also |
243 // public methods involved in normal download progression; see | 249 // public methods involved in normal download progression; see |
244 // the implementation ordering in download_item_impl.cc. | 250 // the implementation ordering in download_item_impl.cc. |
245 | 251 |
246 // Construction common to all constructors. |active| should be true for new | 252 // Construction common to all constructors. |active| should be true for new |
247 // downloads and false for downloads from the history. | 253 // downloads and false for downloads from the history. |
248 // |download_type| indicates to the net log system what kind of download | 254 // |download_type| indicates to the net log system what kind of download |
249 // this is. | 255 // this is. |
250 void Init(bool active, download_net_logs::DownloadType download_type); | 256 void Init(bool active, download_net_logs::DownloadType download_type); |
251 | 257 |
| 258 // Called when the target path has been determined. |target_path| is the |
| 259 // suggested target path. |disposition| indicates how the target path should |
| 260 // be used (see TargetDisposition). |danger_type| is the danger level of |
| 261 // |target_path| as determined by the caller. |intermediate_path| is the path |
| 262 // to use to store the download until OnDownloadCompleting() is called. |
| 263 virtual void OnDownloadTargetDetermined( |
| 264 const FilePath& target_path, |
| 265 TargetDisposition disposition, |
| 266 content::DownloadDangerType danger_type, |
| 267 const FilePath& intermediate_path); |
| 268 |
| 269 // Callback from file thread when we initialize the DownloadFile. |
| 270 void OnDownloadFileInitialized( |
| 271 content::DownloadInterruptReason result); |
| 272 |
252 void OnDownloadRenamedToIntermediateName( | 273 void OnDownloadRenamedToIntermediateName( |
253 content::DownloadInterruptReason reason, const FilePath& full_path); | 274 content::DownloadInterruptReason reason, const FilePath& full_path); |
254 | 275 |
255 // Called when the download is ready to complete. | 276 // Called when the download is ready to complete. |
256 // This may perform final rename if necessary and will eventually call | 277 // This may perform final rename if necessary and will eventually call |
257 // DownloadItem::Completed(). | 278 // DownloadItem::Completed(). |
258 virtual void OnDownloadCompleting(); | 279 virtual void OnDownloadCompleting(); |
259 | 280 |
260 // Called after the delegate has given the go-ahead to actually complete | 281 // Called after the delegate has given the go-ahead to actually complete |
261 // the download. | 282 // the download. |
262 void ReadyForDownloadCompletionDone(); | 283 void ReadyForDownloadCompletionDone(); |
263 | 284 |
264 void OnDownloadRenamedToFinalName(content::DownloadInterruptReason reason, | 285 void OnDownloadRenamedToFinalName(content::DownloadInterruptReason reason, |
265 const FilePath& full_path); | 286 const FilePath& full_path); |
266 | 287 |
| 288 void ReleaseDownloadFile(); |
| 289 |
267 void OnDownloadFileReleased(); | 290 void OnDownloadFileReleased(); |
268 | 291 |
269 // Called when the entire download operation (including renaming etc) | 292 // Called when the entire download operation (including renaming etc) |
270 // is completed. | 293 // is completed. |
271 void Completed(); | 294 void Completed(); |
272 | 295 |
273 // Helper routines ----------------------------------------------------------- | 296 // Helper routines ----------------------------------------------------------- |
274 | 297 |
| 298 // Cancel the DownloadFile if we have it. |
| 299 void CancelDownloadFile(); |
| 300 |
275 // Check if a download is ready for completion. | 301 // Check if a download is ready for completion. |
276 bool IsDownloadReadyForCompletion(); | 302 bool IsDownloadReadyForCompletion(); |
277 | 303 |
278 // Returns true if the download still needs to be renamed to | 304 // Returns true if the download still needs to be renamed to |
279 // GetTargetFilePath(). | 305 // GetTargetFilePath(). |
280 bool NeedsRename() const; | 306 bool NeedsRename() const; |
281 | 307 |
282 // Internal helper for maintaining consistent received and total sizes, and | |
283 // setting the final hash. | |
284 // Should only be called from |OnAllDataSaved|. | |
285 void ProgressComplete(int64 bytes_so_far, | |
286 const std::string& final_hash); | |
287 | |
288 // Call to transition state; all state transitions should go through this. | 308 // Call to transition state; all state transitions should go through this. |
289 void TransitionTo(DownloadInternalState new_state); | 309 void TransitionTo(DownloadInternalState new_state); |
290 | 310 |
291 // Set the |danger_type_| and invoke obserers if necessary. | 311 // Set the |danger_type_| and invoke obserers if necessary. |
292 void SetDangerType(content::DownloadDangerType danger_type); | 312 void SetDangerType(content::DownloadDangerType danger_type); |
293 | 313 |
294 void SetFullPath(const FilePath& new_path); | 314 void SetFullPath(const FilePath& new_path); |
295 | 315 |
296 // Mapping between internal and external states. | 316 // Mapping between internal and external states. |
297 static DownloadState InternalToExternalState( | 317 static DownloadState InternalToExternalState( |
298 DownloadInternalState internal_state); | 318 DownloadInternalState internal_state); |
299 static DownloadInternalState ExternalToInternalState( | 319 static DownloadInternalState ExternalToInternalState( |
300 DownloadState external_state); | 320 DownloadState external_state); |
301 | 321 |
302 // Debugging routines -------------------------------------------------------- | 322 // Debugging routines -------------------------------------------------------- |
303 static const char* DebugDownloadStateString(DownloadInternalState state); | 323 static const char* DebugDownloadStateString(DownloadInternalState state); |
304 | 324 |
| 325 // Will be false for save package downloads retrieved from the history. |
| 326 // TODO(rdsmith): Replace with a generalized enum for "download source". |
| 327 const bool is_save_package_download_; |
| 328 |
305 // The handle to the request information. Used for operations outside the | 329 // The handle to the request information. Used for operations outside the |
306 // download system. | 330 // download system. |
307 scoped_ptr<DownloadRequestHandleInterface> request_handle_; | 331 scoped_ptr<DownloadRequestHandleInterface> request_handle_; |
308 | 332 |
309 // Download ID assigned by DownloadResourceHandler. | 333 // Download ID assigned by DownloadResourceHandler. |
310 content::DownloadId download_id_; | 334 content::DownloadId download_id_; |
311 | 335 |
312 // Display name for the download. If this is empty, then the display name is | 336 // Display name for the download. If this is empty, then the display name is |
313 // considered to be |target_path_.BaseName()|. | 337 // considered to be |target_path_.BaseName()|. |
314 FilePath display_name_; | 338 FilePath display_name_; |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
450 // when the user closes the shelf before the item has been opened but should | 474 // when the user closes the shelf before the item has been opened but should |
451 // be treated as though the user opened it. | 475 // be treated as though the user opened it. |
452 bool opened_; | 476 bool opened_; |
453 | 477 |
454 // Do we actually open downloads when requested? For testing purposes only. | 478 // Do we actually open downloads when requested? For testing purposes only. |
455 bool open_enabled_; | 479 bool open_enabled_; |
456 | 480 |
457 // Did the delegate delay calling Complete on this download? | 481 // Did the delegate delay calling Complete on this download? |
458 bool delegate_delayed_complete_; | 482 bool delegate_delayed_complete_; |
459 | 483 |
| 484 // DownloadFile associated with this download. Note that this |
| 485 // pointer may only be used or destroyed on the FILE thread. |
| 486 // This pointer will be non-null only while the DownloadItem is in |
| 487 // the IN_PROGRESS state. |
| 488 scoped_ptr<content::DownloadFile> download_file_; |
| 489 |
460 // Net log to use for this download. | 490 // Net log to use for this download. |
461 const net::BoundNetLog bound_net_log_; | 491 const net::BoundNetLog bound_net_log_; |
462 | 492 |
463 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_; | 493 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_; |
464 | 494 |
465 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); | 495 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); |
466 }; | 496 }; |
467 | 497 |
468 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ | 498 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ |
OLD | NEW |