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 // Called when the download is ready to complete. | 157 // Called when the download is ready to complete. |
159 // This may perform final rename if necessary and will eventually call | 158 // This may perform final rename if necessary and will eventually call |
160 // DownloadItem::Completed(). | 159 // DownloadItem::Completed(). |
161 virtual void OnDownloadCompleting(); | 160 virtual void OnDownloadCompleting(); |
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 // If all pre-requisites have been met, complete download processing, i.e. do | 276 // If all pre-requisites have been met, complete download processing, i.e. do |
256 // internal cleanup, file rename, and potentially auto-open. (Dangerous | 277 // internal cleanup, file rename, and potentially auto-open. (Dangerous |
257 // downloads still may block on user acceptance after this point.) | 278 // downloads still may block on user acceptance after this point.) |
258 void MaybeCompleteDownload(); | 279 void MaybeCompleteDownload(); |
259 | 280 |
260 void OnDownloadRenamedToFinalName(content::DownloadInterruptReason reason, | 281 void OnDownloadRenamedToFinalName(content::DownloadInterruptReason reason, |
261 const FilePath& full_path); | 282 const FilePath& full_path); |
262 | 283 |
| 284 void ReleaseDownloadFile(); |
| 285 |
263 void OnDownloadFileReleased(); | 286 void OnDownloadFileReleased(); |
264 | 287 |
265 // Called when the entire download operation (including renaming etc) | 288 // Called when the entire download operation (including renaming etc) |
266 // is completed. | 289 // is completed. |
267 void Completed(); | 290 void Completed(); |
268 | 291 |
269 // Helper routines ----------------------------------------------------------- | 292 // Helper routines ----------------------------------------------------------- |
270 | 293 |
| 294 // Cancel the DownloadFile if we have it. |
| 295 void CancelDownloadFile(); |
| 296 |
271 // Returns true if the download still needs to be renamed to | 297 // Returns true if the download still needs to be renamed to |
272 // GetTargetFilePath(). | 298 // GetTargetFilePath(). |
273 bool NeedsRename() const; | 299 bool NeedsRename() const; |
274 | 300 |
275 // Internal helper for maintaining consistent received and total sizes, and | |
276 // setting the final hash. | |
277 // Should only be called from |OnAllDataSaved|. | |
278 void ProgressComplete(int64 bytes_so_far, | |
279 const std::string& final_hash); | |
280 | |
281 // Call to transition state; all state transitions should go through this. | 301 // Call to transition state; all state transitions should go through this. |
282 void TransitionTo(DownloadInternalState new_state); | 302 void TransitionTo(DownloadInternalState new_state); |
283 | 303 |
284 // Set the |danger_type_| and invoke obserers if necessary. | 304 // Set the |danger_type_| and invoke obserers if necessary. |
285 void SetDangerType(content::DownloadDangerType danger_type); | 305 void SetDangerType(content::DownloadDangerType danger_type); |
286 | 306 |
287 void SetFullPath(const FilePath& new_path); | 307 void SetFullPath(const FilePath& new_path); |
288 | 308 |
289 // Mapping between internal and external states. | 309 // Mapping between internal and external states. |
290 static DownloadState InternalToExternalState( | 310 static DownloadState InternalToExternalState( |
291 DownloadInternalState internal_state); | 311 DownloadInternalState internal_state); |
292 static DownloadInternalState ExternalToInternalState( | 312 static DownloadInternalState ExternalToInternalState( |
293 DownloadState external_state); | 313 DownloadState external_state); |
294 | 314 |
295 // Debugging routines -------------------------------------------------------- | 315 // Debugging routines -------------------------------------------------------- |
296 static const char* DebugDownloadStateString(DownloadInternalState state); | 316 static const char* DebugDownloadStateString(DownloadInternalState state); |
297 | 317 |
| 318 // Will be false for save package downloads retrieved from the history. |
| 319 // TODO(rdsmith): Replace with a generalized enum for "download source". |
| 320 const bool is_save_package_download_; |
| 321 |
298 // The handle to the request information. Used for operations outside the | 322 // The handle to the request information. Used for operations outside the |
299 // download system. | 323 // download system. |
300 scoped_ptr<DownloadRequestHandleInterface> request_handle_; | 324 scoped_ptr<DownloadRequestHandleInterface> request_handle_; |
301 | 325 |
302 // Download ID assigned by DownloadResourceHandler. | 326 // Download ID assigned by DownloadResourceHandler. |
303 content::DownloadId download_id_; | 327 content::DownloadId download_id_; |
304 | 328 |
305 // Display name for the download. If this is empty, then the display name is | 329 // Display name for the download. If this is empty, then the display name is |
306 // considered to be |target_path_.BaseName()|. | 330 // considered to be |target_path_.BaseName()|. |
307 FilePath display_name_; | 331 FilePath display_name_; |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
443 // when the user closes the shelf before the item has been opened but should | 467 // when the user closes the shelf before the item has been opened but should |
444 // be treated as though the user opened it. | 468 // be treated as though the user opened it. |
445 bool opened_; | 469 bool opened_; |
446 | 470 |
447 // Do we actually open downloads when requested? For testing purposes only. | 471 // Do we actually open downloads when requested? For testing purposes only. |
448 bool open_enabled_; | 472 bool open_enabled_; |
449 | 473 |
450 // Did the delegate delay calling Complete on this download? | 474 // Did the delegate delay calling Complete on this download? |
451 bool delegate_delayed_complete_; | 475 bool delegate_delayed_complete_; |
452 | 476 |
| 477 // DownloadFile associated with this download. Note that this |
| 478 // pointer may only be used or destroyed on the FILE thread. |
| 479 // This pointer will be non-null only while the DownloadItem is in |
| 480 // the IN_PROGRESS state. |
| 481 scoped_ptr<content::DownloadFile> download_file_; |
| 482 |
453 // Net log to use for this download. | 483 // Net log to use for this download. |
454 const net::BoundNetLog bound_net_log_; | 484 const net::BoundNetLog bound_net_log_; |
455 | 485 |
456 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_; | 486 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_; |
457 | 487 |
458 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); | 488 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); |
459 }; | 489 }; |
460 | 490 |
461 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ | 491 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ |
OLD | NEW |