Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2698)

Unified Diff: chrome/browser/download/download_item.cc

Issue 3029025: Download code cleanup: (Closed)
Patch Set: rebase'n'final Created 10 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/download/download_item.h ('k') | chrome/browser/download/download_manager.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/download/download_item.cc
diff --git a/chrome/browser/download/download_item.cc b/chrome/browser/download/download_item.cc
index f4a6cb19e790093f3f1d4a02b51bb1cd9d0b08ec..f67b1a19b7f484f27ccca2d9565e451e34bd4ba3 100644
--- a/chrome/browser/download/download_item.cc
+++ b/chrome/browser/download/download_item.cc
@@ -9,6 +9,7 @@
#include "chrome/browser/download/download_manager.h"
#include "chrome/browser/download/download_util.h"
#include "chrome/browser/history/download_types.h"
+#include "chrome/common/extensions/extension.h"
namespace {
@@ -18,7 +19,8 @@ const int kUpdateTimeMs = 1000;
} // namespace
// Constructor for reading from the history service.
-DownloadItem::DownloadItem(const DownloadCreateInfo& info)
+DownloadItem::DownloadItem(DownloadManager* download_manager,
+ const DownloadCreateInfo& info)
: id_(-1),
full_path_(info.path),
url_(info.url),
@@ -31,7 +33,7 @@ DownloadItem::DownloadItem(const DownloadCreateInfo& info)
state_(static_cast<DownloadState>(info.state)),
start_time_(info.start_time),
db_handle_(info.db_handle),
- manager_(NULL),
+ download_manager_(download_manager),
is_paused_(false),
open_when_complete_(false),
safety_state_(SAFE),
@@ -50,60 +52,75 @@ DownloadItem::DownloadItem(const DownloadCreateInfo& info)
Init(false /* don't start progress timer */);
}
-// Constructor for DownloadItem created via user action in the main thread.
-DownloadItem::DownloadItem(int32 download_id,
+// Constructing for a regular download:
+DownloadItem::DownloadItem(DownloadManager* download_manager,
+ const DownloadCreateInfo& info,
+ bool is_otr)
+ : id_(info.download_id),
+ full_path_(info.path),
+ path_uniquifier_(info.path_uniquifier),
+ url_(info.url),
+ referrer_url_(info.referrer_url),
+ mime_type_(info.mime_type),
+ original_mime_type_(info.original_mime_type),
+ total_bytes_(info.total_bytes),
+ received_bytes_(0),
+ start_tick_(base::TimeTicks::Now()),
+ state_(IN_PROGRESS),
+ start_time_(info.start_time),
+ db_handle_(DownloadManager::kUninitializedHandle),
+ download_manager_(download_manager),
+ is_paused_(false),
+ open_when_complete_(false),
+ safety_state_(info.is_dangerous ? DANGEROUS : SAFE),
+ auto_opened_(false),
+ original_name_(info.original_name),
+ render_process_id_(info.child_id),
+ request_id_(info.request_id),
+ save_as_(info.prompt_user_for_save_location),
+ is_otr_(is_otr),
+ is_extension_install_(info.is_extension_install),
+ name_finalized_(false),
+ is_temporary_(!info.save_info.file_path.empty()),
+ need_final_rename_(false) {
+ Init(true /* start progress timer */);
+}
+
+// Constructing for the "Save Page As..." feature:
+DownloadItem::DownloadItem(DownloadManager* download_manager,
const FilePath& path,
- int path_uniquifier,
const GURL& url,
- const GURL& referrer_url,
- const std::string& mime_type,
- const std::string& original_mime_type,
- const FilePath& original_name,
- const base::Time start_time,
- int64 download_size,
- int render_process_id,
- int request_id,
- bool is_dangerous,
- bool save_as,
- bool is_otr,
- bool is_extension_install,
- bool is_temporary)
- : id_(download_id),
+ bool is_otr)
+ : id_(1),
full_path_(path),
- path_uniquifier_(path_uniquifier),
+ path_uniquifier_(0),
url_(url),
- referrer_url_(referrer_url),
- mime_type_(mime_type),
- original_mime_type_(original_mime_type),
- total_bytes_(download_size),
+ referrer_url_(GURL()),
+ mime_type_(std::string()),
+ original_mime_type_(std::string()),
+ total_bytes_(0),
received_bytes_(0),
start_tick_(base::TimeTicks::Now()),
state_(IN_PROGRESS),
- start_time_(start_time),
+ start_time_(base::Time::Now()),
db_handle_(DownloadManager::kUninitializedHandle),
- manager_(NULL),
+ download_manager_(download_manager),
is_paused_(false),
open_when_complete_(false),
- safety_state_(is_dangerous ? DANGEROUS : SAFE),
+ safety_state_(SAFE),
auto_opened_(false),
- original_name_(original_name),
- render_process_id_(render_process_id),
- request_id_(request_id),
- save_as_(save_as),
+ original_name_(FilePath()),
+ render_process_id_(-1),
+ request_id_(-1),
+ save_as_(false),
is_otr_(is_otr),
- is_extension_install_(is_extension_install),
+ is_extension_install_(false),
name_finalized_(false),
- is_temporary_(is_temporary),
+ is_temporary_(false),
need_final_rename_(false) {
Init(true /* start progress timer */);
}
-void DownloadItem::Init(bool start_timer) {
- file_name_ = full_path_.BaseName();
- if (start_timer)
- StartProgressTimer();
-}
-
DownloadItem::~DownloadItem() {
state_ = REMOVING;
UpdateObservers();
@@ -125,14 +142,45 @@ void DownloadItem::NotifyObserversDownloadFileCompleted() {
FOR_EACH_OBSERVER(Observer, observers_, OnDownloadFileCompleted(this));
}
-void DownloadItem::NotifyObserversDownloadOpened() {
- FOR_EACH_OBSERVER(Observer, observers_, OnDownloadOpened(this));
+bool DownloadItem::CanOpenDownload() {
+ FilePath file_to_use = full_path();
+ if (!original_name().value().empty())
+ file_to_use = original_name();
+
+ return !Extension::IsExtension(file_to_use) &&
+ !download_manager_->IsExecutableFile(file_to_use);
+}
+
+bool DownloadItem::ShouldOpenFileBasedOnExtension() {
+ return download_manager_->ShouldOpenFileBasedOnExtension(full_path());
+}
+
+void DownloadItem::OpenFilesBasedOnExtension(bool open) {
+ return download_manager_->OpenFilesBasedOnExtension(full_path(), open);
+}
+
+void DownloadItem::OpenDownload() {
+ if (state() == DownloadItem::IN_PROGRESS) {
+ open_when_complete_ = !open_when_complete_;
+ } else if (state() == DownloadItem::COMPLETE) {
+ FOR_EACH_OBSERVER(Observer, observers_, OnDownloadOpened(this));
+ download_manager_->OpenDownload(this, NULL);
+ }
+}
+
+void DownloadItem::ShowDownloadInShell() {
+ download_manager_->ShowDownloadInShell(this);
+}
+
+void DownloadItem::DangerousDownloadValidated() {
+ download_manager_->DangerousDownloadValidated(this);
}
-// If we've received more data than we were expecting (bad server info?), revert
-// to 'unknown size mode'.
void DownloadItem::UpdateSize(int64 bytes_so_far) {
received_bytes_ = bytes_so_far;
+
+ // If we've received more data than we were expecting (bad server info?),
+ // revert to 'unknown size mode'.
if (received_bytes_ > total_bytes_)
total_bytes_ = 0;
}
@@ -168,7 +216,7 @@ void DownloadItem::Cancel(bool update_history) {
UpdateObservers();
StopProgressTimer();
if (update_history)
- manager_->DownloadCancelled(id_);
+ download_manager_->DownloadCancelled(id_);
}
void DownloadItem::Finished(int64 size) {
@@ -181,8 +229,8 @@ void DownloadItem::Remove(bool delete_on_disk) {
Cancel(true);
state_ = REMOVING;
if (delete_on_disk)
- manager_->DeleteDownload(full_path_);
- manager_->RemoveDownload(db_handle_);
+ download_manager_->DeleteDownload(full_path_);
+ download_manager_->RemoveDownload(db_handle_);
// We have now been deleted.
}
@@ -220,7 +268,7 @@ void DownloadItem::Rename(const FilePath& full_path) {
void DownloadItem::TogglePause() {
DCHECK(state_ == IN_PROGRESS);
- manager_->PauseDownload(id_, !is_paused_);
+ download_manager_->PauseDownload(id_, !is_paused_);
is_paused_ = !is_paused_;
UpdateObservers();
}
@@ -246,3 +294,9 @@ FilePath DownloadItem::GetFileName() const {
}
return original_name_;
}
+
+void DownloadItem::Init(bool start_timer) {
+ file_name_ = full_path_.BaseName();
+ if (start_timer)
+ StartProgressTimer();
+}
« no previous file with comments | « chrome/browser/download/download_item.h ('k') | chrome/browser/download/download_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698