| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #include "chrome/browser/download/download_item.h" | 5 #include "chrome/browser/download/download_item.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
| 13 #include "base/timer.h" | 13 #include "base/timer.h" |
| 14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 15 #include "net/base/net_util.h" | 15 #include "net/base/net_util.h" |
| 16 #include "chrome/browser/browser_thread.h" | 16 #include "chrome/browser/browser_thread.h" |
| 17 #include "chrome/browser/download/download_extensions.h" | 17 #include "chrome/browser/download/download_extensions.h" |
| 18 #include "chrome/browser/download/download_file_manager.h" | 18 #include "chrome/browser/download/download_file_manager.h" |
| 19 #include "chrome/browser/download/download_history.h" | 19 #include "chrome/browser/download/download_history.h" |
| 20 #include "chrome/browser/download/download_manager.h" | 20 #include "chrome/browser/download/download_manager.h" |
| 21 #include "chrome/browser/download/download_prefs.h" | 21 #include "chrome/browser/download/download_prefs.h" |
| 22 #include "chrome/browser/download/download_util.h" | 22 #include "chrome/browser/download/download_util.h" |
| 23 #include "chrome/browser/history/download_create_info.h" | 23 #include "chrome/browser/history/download_create_info.h" |
| 24 #include "chrome/browser/platform_util.h" | 24 #include "chrome/browser/platform_util.h" |
| 25 #include "chrome/browser/prefs/pref_service.h" | 25 #include "chrome/browser/prefs/pref_service.h" |
| 26 #include "chrome/browser/profiles/profile.h" | 26 #include "chrome/browser/profiles/profile.h" |
| 27 #include "chrome/common/extensions/extension.h" | 27 #include "chrome/common/extensions/extension.h" |
| 28 #include "chrome/common/pref_names.h" | 28 #include "chrome/common/pref_names.h" |
| 29 | 29 |
| 30 // A DownloadItem normally goes through the following states: |
| 31 // * Created (when download starts) |
| 32 // * Made visible to consumers (e.g. Javascript) after the |
| 33 // destination file has been determined. |
| 34 // * Entered into the history database. |
| 35 // * Made visible in the download shelf. |
| 36 // * All data is received. Note that the actual data download occurs |
| 37 // in parallel with the above steps, but until those steps are |
| 38 // complete, completion of the data download will be ignored. |
| 39 // * Download file is renamed to its final name, and possibly |
| 40 // auto-opened. |
| 41 // TODO(rdsmith): This progress should be reflected in |
| 42 // DownloadItem::DownloadState and a state transition table/state diagram. |
| 43 // |
| 44 // TODO(rdsmith): This description should be updated to reflect the cancel |
| 45 // pathways. |
| 46 |
| 30 namespace { | 47 namespace { |
| 31 | 48 |
| 32 // Update frequency (milliseconds). | 49 // Update frequency (milliseconds). |
| 33 const int kUpdateTimeMs = 1000; | 50 const int kUpdateTimeMs = 1000; |
| 34 | 51 |
| 35 void DeleteDownloadedFile(const FilePath& path) { | 52 void DeleteDownloadedFile(const FilePath& path) { |
| 36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 37 | 54 |
| 38 // Make sure we only delete files. | 55 // Make sure we only delete files. |
| 39 if (!file_util::DirectoryExists(path)) | 56 if (!file_util::DirectoryExists(path)) |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 // Small downloads might be complete before this method has a chance to run. | 306 // Small downloads might be complete before this method has a chance to run. |
| 290 return; | 307 return; |
| 291 } | 308 } |
| 292 state_ = CANCELLED; | 309 state_ = CANCELLED; |
| 293 UpdateObservers(); | 310 UpdateObservers(); |
| 294 StopProgressTimer(); | 311 StopProgressTimer(); |
| 295 if (update_history) | 312 if (update_history) |
| 296 download_manager_->DownloadCancelled(id_); | 313 download_manager_->DownloadCancelled(id_); |
| 297 } | 314 } |
| 298 | 315 |
| 299 void DownloadItem::OnAllDataSaved(int64 size) { | 316 void DownloadItem::OnReadyToFinish(int64 size) { |
| 300 state_ = COMPLETE; | 317 state_ = COMPLETE; |
| 301 UpdateSize(size); | 318 UpdateSize(size); |
| 302 StopProgressTimer(); | 319 StopProgressTimer(); |
| 303 } | 320 } |
| 304 | 321 |
| 305 void DownloadItem::Finished() { | 322 void DownloadItem::Finished() { |
| 306 // Handle chrome extensions explicitly and skip the shell execute. | 323 // Handle chrome extensions explicitly and skip the shell execute. |
| 307 if (is_extension_install()) { | 324 if (is_extension_install()) { |
| 308 download_util::OpenChromeExtension(download_manager_->profile(), | 325 download_util::OpenChromeExtension(download_manager_->profile(), |
| 309 download_manager_, | 326 download_manager_, |
| 310 *this); | 327 *this); |
| 311 auto_opened_ = true; | 328 auto_opened_ = true; |
| 312 } else if (open_when_complete() || | 329 } else if (open_when_complete() || |
| 313 download_manager_->ShouldOpenFileBasedOnExtension( | 330 download_manager_->ShouldOpenFileBasedOnExtension( |
| 314 GetUserVerifiedFilePath()) || | 331 GetUserVerifiedFilePath()) || |
| 315 is_temporary()) { | 332 is_temporary()) { |
| 316 // If the download is temporary, like in drag-and-drop, do not open it but | 333 // If the download is temporary, like in drag-and-drop, do not open it but |
| 317 // we still need to set it auto-opened so that it can be removed from the | 334 // we still need to set it auto-opened so that it can be removed from the |
| 318 // download shelf. | 335 // download shelf. |
| 319 if (!is_temporary()) | 336 if (!is_temporary()) |
| 320 OpenDownload(); | 337 OpenDownload(); |
| 321 auto_opened_ = true; | 338 auto_opened_ = true; |
| 322 } | 339 } |
| 323 | 340 |
| 324 // Notify our observers that we are complete (the call to OnAllDataSaved() | 341 // Notify our observers that we are complete (the call to OnReadyToFinish() |
| 325 // set the state to complete but did not notify). | 342 // set the state to complete but did not notify). |
| 326 UpdateObservers(); | 343 UpdateObservers(); |
| 327 | 344 |
| 328 // The download file is meant to be completed if both the filename is | 345 // The download file is meant to be completed if both the filename is |
| 329 // finalized and the file data is downloaded. The ordering of these two | 346 // finalized and the file data is downloaded. The ordering of these two |
| 330 // actions is indeterministic. Thus, if the filename is not finalized yet, | 347 // actions is indeterministic. Thus, if the filename is not finalized yet, |
| 331 // delay the notification. | 348 // delay the notification. |
| 332 if (name_finalized()) { | 349 if (name_finalized()) { |
| 333 NotifyObserversDownloadFileCompleted(); | 350 NotifyObserversDownloadFileCompleted(); |
| 334 download_manager_->RemoveFromActiveList(id()); | 351 download_manager_->RemoveFromActiveList(id()); |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 547 target_name_.value().c_str(), | 564 target_name_.value().c_str(), |
| 548 full_path().value().c_str()); | 565 full_path().value().c_str()); |
| 549 } else { | 566 } else { |
| 550 description += base::StringPrintf(" url = \"%s\"", url().spec().c_str()); | 567 description += base::StringPrintf(" url = \"%s\"", url().spec().c_str()); |
| 551 } | 568 } |
| 552 | 569 |
| 553 description += " }"; | 570 description += " }"; |
| 554 | 571 |
| 555 return description; | 572 return description; |
| 556 } | 573 } |
| OLD | NEW |