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 20 matching lines...) Expand all Loading... |
228 // Only incoming transition IN_PROGRESS-> | 226 // Only incoming transition IN_PROGRESS-> |
229 CANCELLED_INTERNAL, | 227 CANCELLED_INTERNAL, |
230 | 228 |
231 // An error has interrupted the download. | 229 // An error has interrupted the download. |
232 // Only incoming transition IN_PROGRESS-> | 230 // Only incoming transition IN_PROGRESS-> |
233 INTERRUPTED_INTERNAL, | 231 INTERRUPTED_INTERNAL, |
234 | 232 |
235 MAX_DOWNLOAD_INTERNAL_STATE, | 233 MAX_DOWNLOAD_INTERNAL_STATE, |
236 }; | 234 }; |
237 | 235 |
| 236 // DownloadDestinationObserver |
| 237 virtual void DestinationUpdate(int64 bytes_so_far, |
| 238 int64 bytes_per_sec, |
| 239 const std::string& hash_state) OVERRIDE; |
| 240 virtual void DestinationError( |
| 241 content::DownloadInterruptReason reason) OVERRIDE; |
| 242 virtual void DestinationCompleted(const std::string& final_hash) OVERRIDE; |
| 243 |
238 // Normal progression of a download ------------------------------------------ | 244 // Normal progression of a download ------------------------------------------ |
239 | 245 |
240 // These are listed in approximately chronological order. There are also | 246 // These are listed in approximately chronological order. There are also |
241 // public methods involved in normal download progression; see | 247 // public methods involved in normal download progression; see |
242 // the implementation ordering in download_item_impl.cc. | 248 // the implementation ordering in download_item_impl.cc. |
243 | 249 |
244 // Construction common to all constructors. |active| should be true for new | 250 // Construction common to all constructors. |active| should be true for new |
245 // downloads and false for downloads from the history. | 251 // downloads and false for downloads from the history. |
246 // |download_type| indicates to the net log system what kind of download | 252 // |download_type| indicates to the net log system what kind of download |
247 // this is. | 253 // this is. |
248 void Init(bool active, download_net_logs::DownloadType download_type); | 254 void Init(bool active, download_net_logs::DownloadType download_type); |
249 | 255 |
| 256 // Called when the target path has been determined. |target_path| is the |
| 257 // suggested target path. |disposition| indicates how the target path should |
| 258 // be used (see TargetDisposition). |danger_type| is the danger level of |
| 259 // |target_path| as determined by the caller. |intermediate_path| is the path |
| 260 // to use to store the download until OnDownloadCompleting() is called. |
| 261 virtual void OnDownloadTargetDetermined( |
| 262 const FilePath& target_path, |
| 263 TargetDisposition disposition, |
| 264 content::DownloadDangerType danger_type, |
| 265 const FilePath& intermediate_path); |
| 266 |
| 267 // Callback from file thread when we initialize the DownloadFile. |
| 268 void OnDownloadFileInitialized( |
| 269 content::DownloadInterruptReason result); |
| 270 |
250 void OnDownloadRenamedToIntermediateName( | 271 void OnDownloadRenamedToIntermediateName( |
251 content::DownloadInterruptReason reason, const FilePath& full_path); | 272 content::DownloadInterruptReason reason, const FilePath& full_path); |
252 | 273 |
253 // If all pre-requisites have been met, complete download processing, i.e. do | 274 // If all pre-requisites have been met, complete download processing, i.e. do |
254 // internal cleanup, file rename, and potentially auto-open. (Dangerous | 275 // internal cleanup, file rename, and potentially auto-open. (Dangerous |
255 // downloads still may block on user acceptance after this point.) | 276 // downloads still may block on user acceptance after this point.) |
256 void MaybeCompleteDownload(); | 277 void MaybeCompleteDownload(); |
257 | 278 |
258 void OnDownloadRenamedToFinalName(content::DownloadInterruptReason reason, | 279 void OnDownloadRenamedToFinalName(content::DownloadInterruptReason reason, |
259 const FilePath& full_path); | 280 const FilePath& full_path); |
260 | 281 |
| 282 void ReleaseDownloadFile(); |
| 283 |
261 void OnDownloadFileReleased(); | 284 void OnDownloadFileReleased(); |
262 | 285 |
263 // Called when the entire download operation (including renaming etc) | 286 // Called when the entire download operation (including renaming etc) |
264 // is completed. | 287 // is completed. |
265 void Completed(); | 288 void Completed(); |
266 | 289 |
267 // Helper routines ----------------------------------------------------------- | 290 // Helper routines ----------------------------------------------------------- |
268 | 291 |
269 // Returns true if the download still needs to be renamed to | 292 // Returns true if the download still needs to be renamed to |
270 // GetTargetFilePath(). | 293 // GetTargetFilePath(). |
271 bool NeedsRename() const; | 294 bool NeedsRename() const; |
272 | 295 |
273 // Internal helper for maintaining consistent received and total sizes, and | |
274 // setting the final hash. | |
275 // Should only be called from |OnAllDataSaved|. | |
276 void ProgressComplete(int64 bytes_so_far, | |
277 const std::string& final_hash); | |
278 | |
279 // Call to transition state; all state transitions should go through this. | 296 // Call to transition state; all state transitions should go through this. |
280 void TransitionTo(DownloadInternalState new_state); | 297 void TransitionTo(DownloadInternalState new_state); |
281 | 298 |
282 // Set the |danger_type_| and invoke obserers if necessary. | 299 // Set the |danger_type_| and invoke obserers if necessary. |
283 void SetDangerType(content::DownloadDangerType danger_type); | 300 void SetDangerType(content::DownloadDangerType danger_type); |
284 | 301 |
285 void SetFullPath(const FilePath& new_path); | 302 void SetFullPath(const FilePath& new_path); |
286 | 303 |
287 // Mapping between internal and external states. | 304 // Mapping between internal and external states. |
288 static DownloadState InternalToExternalState( | 305 static DownloadState InternalToExternalState( |
289 DownloadInternalState internal_state); | 306 DownloadInternalState internal_state); |
290 static DownloadInternalState ExternalToInternalState( | 307 static DownloadInternalState ExternalToInternalState( |
291 DownloadState external_state); | 308 DownloadState external_state); |
292 | 309 |
293 // Debugging routines -------------------------------------------------------- | 310 // Debugging routines -------------------------------------------------------- |
294 static const char* DebugDownloadStateString(DownloadInternalState state); | 311 static const char* DebugDownloadStateString(DownloadInternalState state); |
295 | 312 |
| 313 // Will be false for save package downloads retrieved from the history. |
| 314 // TODO(rdsmith): Replace with a generalized enum for "download source". |
| 315 const bool is_save_package_download_; |
| 316 |
296 // The handle to the request information. Used for operations outside the | 317 // The handle to the request information. Used for operations outside the |
297 // download system. | 318 // download system. |
298 scoped_ptr<DownloadRequestHandleInterface> request_handle_; | 319 scoped_ptr<DownloadRequestHandleInterface> request_handle_; |
299 | 320 |
300 // Download ID assigned by DownloadResourceHandler. | 321 // Download ID assigned by DownloadResourceHandler. |
301 content::DownloadId download_id_; | 322 content::DownloadId download_id_; |
302 | 323 |
303 // Display name for the download. If this is empty, then the display name is | 324 // Display name for the download. If this is empty, then the display name is |
304 // considered to be |target_path_.BaseName()|. | 325 // considered to be |target_path_.BaseName()|. |
305 FilePath display_name_; | 326 FilePath display_name_; |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
441 // when the user closes the shelf before the item has been opened but should | 462 // when the user closes the shelf before the item has been opened but should |
442 // be treated as though the user opened it. | 463 // be treated as though the user opened it. |
443 bool opened_; | 464 bool opened_; |
444 | 465 |
445 // Do we actually open downloads when requested? For testing purposes only. | 466 // Do we actually open downloads when requested? For testing purposes only. |
446 bool open_enabled_; | 467 bool open_enabled_; |
447 | 468 |
448 // Did the delegate delay calling Complete on this download? | 469 // Did the delegate delay calling Complete on this download? |
449 bool delegate_delayed_complete_; | 470 bool delegate_delayed_complete_; |
450 | 471 |
| 472 // DownloadFile associated with this download. Note that this |
| 473 // pointer may only be used or destroyed on the FILE thread. |
| 474 // This pointer will be non-null only while the DownloadItem is in |
| 475 // the IN_PROGRESS state. |
| 476 scoped_ptr<content::DownloadFile> download_file_; |
| 477 |
451 // Net log to use for this download. | 478 // Net log to use for this download. |
452 const net::BoundNetLog bound_net_log_; | 479 const net::BoundNetLog bound_net_log_; |
453 | 480 |
454 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_; | 481 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_; |
455 | 482 |
456 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); | 483 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); |
457 }; | 484 }; |
458 | 485 |
459 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ | 486 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ |
OLD | NEW |