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 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 can be made private |
146 | 153 |
154 // Start the download | |
155 virtual void Start(scoped_ptr<content::DownloadFile> download_file); | |
156 | |
147 // Called when the target path has been determined. |target_path| is the | 157 // Called when the target path has been determined. |target_path| is the |
148 // suggested target path. |disposition| indicates how the target path should | 158 // suggested target path. |disposition| indicates how the target path should |
149 // be used (see TargetDisposition). |danger_type| is the danger level of | 159 // be used (see TargetDisposition). |danger_type| is the danger level of |
150 // |target_path| as determined by the caller. |intermediate_path| is the path | 160 // |target_path| as determined by the caller. |intermediate_path| is the path |
151 // to use to store the download until OnDownloadCompleting() is called. | 161 // to use to store the download until OnDownloadCompleting() is called. |
152 virtual void OnDownloadTargetDetermined( | 162 virtual void OnDownloadTargetDetermined( |
153 const FilePath& target_path, | 163 const FilePath& target_path, |
154 TargetDisposition disposition, | 164 TargetDisposition disposition, |
155 content::DownloadDangerType danger_type, | 165 content::DownloadDangerType danger_type, |
156 const FilePath& intermediate_path); | 166 const FilePath& intermediate_path); |
157 | 167 |
158 // Called when the download is ready to complete. | 168 // Called when the download is ready to complete. |
159 // This may perform final rename if necessary and will eventually call | 169 // This may perform final rename if necessary and will eventually call |
160 // DownloadItem::Completed(). | 170 // DownloadItem::Completed(). |
161 virtual void OnDownloadCompleting(); | 171 virtual void OnDownloadCompleting(); |
162 | 172 |
163 // Needed because of interwining with DownloadManagerImpl -------------------- | 173 // Needed because of interwining with DownloadManagerImpl -------------------- |
164 | 174 |
165 // TODO(rdsmith): Unwind DownloadManagerImpl and DownloadItemImpl, | 175 // TODO(rdsmith): Unwind DownloadManagerImpl and DownloadItemImpl, |
166 // removing these from the public interface. | 176 // removing these from the public interface. |
167 | 177 |
168 // Notify observers that this item is being removed by the user. | 178 // Notify observers that this item is being removed by the user. |
169 virtual void NotifyRemoved(); | 179 virtual void NotifyRemoved(); |
170 | 180 |
171 virtual void OnDownloadedFileRemoved(); | 181 virtual void OnDownloadedFileRemoved(); |
172 | 182 |
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. | 183 // Indicate that an error has occurred on the download. |
183 virtual void Interrupt(content::DownloadInterruptReason reason); | 184 virtual void Interrupt(content::DownloadInterruptReason reason); |
184 | 185 |
186 // Provide a weak pointer reference to a DownloadDestinationObserver | |
187 // for use by download destinations. | |
188 base::WeakPtr<content::DownloadDestinationObserver> | |
189 DestinationObserverAsWeakPtr(); | |
190 | |
191 // For dispatching on whether we're dealing with a SavePackage download. | |
192 virtual bool IsSavePackageDownload() const; | |
193 | |
185 // DownloadItemImpl routines only needed by SavePackage ---------------------- | 194 // DownloadItemImpl routines only needed by SavePackage ---------------------- |
186 | 195 |
187 // Called by SavePackage to set the total number of bytes on the item. | 196 // Called by SavePackage to set the total number of bytes on the item. |
188 virtual void SetTotalBytes(int64 total_bytes); | 197 virtual void SetTotalBytes(int64 total_bytes); |
189 | 198 |
190 // Indicate progress in saving data to its destination. | 199 // Indicate progress in saving data to its destination. |
191 // |bytes_so_far| is the number of bytes received so far. | 200 // |bytes_so_far| is the number of bytes received so far. |
192 // |hash_state| is the current hash state. | 201 // |hash_state| is the current hash state. |
193 virtual void UpdateProgress(int64 bytes_so_far, | 202 virtual void UpdateProgress(int64 bytes_so_far, |
194 int64 bytes_per_sec, | 203 int64 bytes_per_sec, |
195 const std::string& hash_state); | 204 const std::string& hash_state); |
196 | 205 |
197 virtual void OnAllDataSaved(int64 size, const std::string& final_hash); | 206 virtual void OnAllDataSaved(const std::string& final_hash); |
198 | 207 |
199 // Called by SavePackage to display progress when the DownloadItem | 208 // Called by SavePackage to display progress when the DownloadItem |
200 // should be considered complete. | 209 // should be considered complete. |
201 virtual void MarkAsComplete(); | 210 virtual void MarkAsComplete(); |
202 | 211 |
203 // Interactions with persistence system -------------------------------------- | 212 // Interactions with persistence system -------------------------------------- |
204 | 213 |
205 // TODO(benjhayden): Remove when DownloadHistory becomes an observer. | 214 // TODO(benjhayden): Remove when DownloadHistory becomes an observer. |
206 virtual void SetIsPersisted(); | 215 virtual void SetIsPersisted(); |
207 virtual void SetDbHandle(int64 handle); | 216 virtual void SetDbHandle(int64 handle); |
(...skipping 20 matching lines...) Expand all Loading... | |
228 // Only incoming transition IN_PROGRESS-> | 237 // Only incoming transition IN_PROGRESS-> |
229 CANCELLED_INTERNAL, | 238 CANCELLED_INTERNAL, |
230 | 239 |
231 // An error has interrupted the download. | 240 // An error has interrupted the download. |
232 // Only incoming transition IN_PROGRESS-> | 241 // Only incoming transition IN_PROGRESS-> |
233 INTERRUPTED_INTERNAL, | 242 INTERRUPTED_INTERNAL, |
234 | 243 |
235 MAX_DOWNLOAD_INTERNAL_STATE, | 244 MAX_DOWNLOAD_INTERNAL_STATE, |
236 }; | 245 }; |
237 | 246 |
247 // DownloadDestinationObserver | |
248 virtual void DestinationUpdate(int64 bytes_so_far, | |
249 int64 bytes_per_sec, | |
250 const std::string& hash_state) OVERRIDE; | |
251 virtual void DestinationError( | |
252 content::DownloadInterruptReason reason) OVERRIDE; | |
253 virtual void DestinationCompleted(const std::string& final_hash) OVERRIDE; | |
254 | |
238 // Normal progression of a download ------------------------------------------ | 255 // Normal progression of a download ------------------------------------------ |
239 | 256 |
240 // These are listed in approximately chronological order. There are also | 257 // These are listed in approximately chronological order. There are also |
241 // public methods involved in normal download progression; see | 258 // public methods involved in normal download progression; see |
242 // the implementation ordering in download_item_impl.cc. | 259 // the implementation ordering in download_item_impl.cc. |
243 | 260 |
244 // Construction common to all constructors. |active| should be true for new | 261 // Construction common to all constructors. |active| should be true for new |
245 // downloads and false for downloads from the history. | 262 // downloads and false for downloads from the history. |
246 // |download_type| indicates to the net log system what kind of download | 263 // |download_type| indicates to the net log system what kind of download |
247 // this is. | 264 // this is. |
248 void Init(bool active, download_net_logs::DownloadType download_type); | 265 void Init(bool active, download_net_logs::DownloadType download_type); |
249 | 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. | |
benjhayden
2012/09/24 17:43:27
So, the scoped_ptr will never delete the DownloadF
Randy Smith (Not in Mondays)
2012/09/26 21:01:05
Hmmm. I'm not certain what the right thing to do
| |
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 |