summaryrefslogtreecommitdiffstats
path: root/chrome/browser/download/download_manager.cc
diff options
context:
space:
mode:
authorphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-07-16 21:41:16 +0000
committerphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-07-16 21:41:16 +0000
commit6c69796dfc4e3c4fe1c6c0d7c561d3f17a3e18a9 (patch)
tree18375c7ebd926415dbc1371de37e5945528c6b30 /chrome/browser/download/download_manager.cc
parentaa561d95543b80f81781e2c332b271a044c134c6 (diff)
downloadchromium_src-6c69796dfc4e3c4fe1c6c0d7c561d3f17a3e18a9.zip
chromium_src-6c69796dfc4e3c4fe1c6c0d7c561d3f17a3e18a9.tar.gz
chromium_src-6c69796dfc4e3c4fe1c6c0d7c561d3f17a3e18a9.tar.bz2
Download code cleanup: split big files into smaller one.
It's hard to find things in the download directory. DownloadFile and DownloadFileManager were bundled together. Similarly for DownloadItem and DownloadManager, which was much less intuitive. Some 3-5 line structs were also in one bag with something else. I extracted them to their own small file. TEST=unit_tests, browser_tests, ui_tests BUG=48913 Review URL: http://codereview.chromium.org/2806054 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@52749 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/download/download_manager.cc')
-rw-r--r--chrome/browser/download/download_manager.cc262
1 files changed, 10 insertions, 252 deletions
diff --git a/chrome/browser/download/download_manager.cc b/chrome/browser/download/download_manager.cc
index c43f18e..26d1d54 100644
--- a/chrome/browser/download/download_manager.cc
+++ b/chrome/browser/download/download_manager.cc
@@ -9,48 +9,39 @@
#include "base/callback.h"
#include "base/file_util.h"
#include "base/logging.h"
-#include "base/message_loop.h"
#include "base/path_service.h"
#include "base/rand_util.h"
-#include "base/stl_util-inl.h"
-#include "base/string_util.h"
#include "base/sys_string_conversions.h"
#include "base/task.h"
-#include "base/thread.h"
-#include "base/timer.h"
#include "build/build_config.h"
+#include "chrome/browser/browser.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_thread.h"
-#include "chrome/browser/download/download_file.h"
+#include "chrome/browser/download/download_file_manager.h"
+#include "chrome/browser/download/download_item.h"
#include "chrome/browser/download/download_util.h"
#include "chrome/browser/extensions/crx_installer.h"
#include "chrome/browser/extensions/extension_install_ui.h"
#include "chrome/browser/extensions/extensions_service.h"
+#include "chrome/browser/history/download_types.h"
#include "chrome/browser/net/chrome_url_request_context.h"
#include "chrome/browser/platform_util.h"
-#include "chrome/browser/pref_service.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/renderer_host/render_process_host.h"
#include "chrome/browser/renderer_host/render_view_host.h"
-#include "chrome/browser/renderer_host/resource_dispatcher_host.h"
#include "chrome/browser/tab_contents/infobar_delegate.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/tab_contents/tab_util.h"
-#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
-#include "chrome/common/extensions/extension.h"
-#include "chrome/common/extensions/user_script.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/notification_type.h"
#include "chrome/common/pref_names.h"
#include "googleurl/src/gurl.h"
-#include "grit/chromium_strings.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "net/base/mime_util.h"
#include "net/base/net_util.h"
-#include "net/url_request/url_request_context.h"
#if defined(OS_WIN)
#include "app/win_util.h"
@@ -60,25 +51,6 @@
namespace {
-// Periodically update our observers.
-class DownloadItemUpdateTask : public Task {
- public:
- explicit DownloadItemUpdateTask(DownloadItem* item) : item_(item) {}
- void Run() { if (item_) item_->UpdateObservers(); }
-
- private:
- DownloadItem* item_;
-};
-
-// Update frequency (milliseconds).
-const int kUpdateTimeMs = 1000;
-
-// Our download table ID starts at 1, so we use 0 to represent a download that
-// has started, but has not yet had its data persisted in the table. We use fake
-// database handles in incognito mode starting at -1 and progressively getting
-// more negative.
-const int kUninitializedHandle = 0;
-
// Used to sort download items based on descending start time.
bool CompareStartTime(DownloadItem* first, DownloadItem* second) {
return first->start_time() > second->start_time();
@@ -94,226 +66,12 @@ void DeleteDownloadedFile(const FilePath& path) {
} // namespace
-// DownloadItem implementation -------------------------------------------------
-
-// Constructor for reading from the history service.
-DownloadItem::DownloadItem(const DownloadCreateInfo& info)
- : id_(-1),
- full_path_(info.path),
- 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_(info.received_bytes),
- start_tick_(base::TimeTicks()),
- state_(static_cast<DownloadState>(info.state)),
- start_time_(info.start_time),
- db_handle_(info.db_handle),
- manager_(NULL),
- is_paused_(false),
- open_when_complete_(false),
- safety_state_(SAFE),
- auto_opened_(false),
- original_name_(info.original_name),
- render_process_id_(-1),
- request_id_(-1),
- save_as_(false),
- is_otr_(false),
- is_extension_install_(info.is_extension_install),
- name_finalized_(false),
- is_temporary_(false) {
- if (state_ == IN_PROGRESS)
- state_ = CANCELLED;
- Init(false /* don't start progress timer */);
-}
-
-// Constructor for DownloadItem created via user action in the main thread.
-DownloadItem::DownloadItem(int32 download_id,
- 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),
- full_path_(path),
- path_uniquifier_(path_uniquifier),
- url_(url),
- referrer_url_(referrer_url),
- mime_type_(mime_type),
- original_mime_type_(original_mime_type),
- total_bytes_(download_size),
- received_bytes_(0),
- start_tick_(base::TimeTicks::Now()),
- state_(IN_PROGRESS),
- start_time_(start_time),
- db_handle_(kUninitializedHandle),
- manager_(NULL),
- is_paused_(false),
- open_when_complete_(false),
- safety_state_(is_dangerous ? DANGEROUS : SAFE),
- auto_opened_(false),
- original_name_(original_name),
- render_process_id_(render_process_id),
- request_id_(request_id),
- save_as_(save_as),
- is_otr_(is_otr),
- is_extension_install_(is_extension_install),
- name_finalized_(false),
- is_temporary_(is_temporary) {
- 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();
-}
-
-void DownloadItem::AddObserver(Observer* observer) {
- observers_.AddObserver(observer);
-}
-
-void DownloadItem::RemoveObserver(Observer* observer) {
- observers_.RemoveObserver(observer);
-}
-
-void DownloadItem::UpdateObservers() {
- FOR_EACH_OBSERVER(Observer, observers_, OnDownloadUpdated(this));
-}
-
-void DownloadItem::NotifyObserversDownloadFileCompleted() {
- FOR_EACH_OBSERVER(Observer, observers_, OnDownloadFileCompleted(this));
-}
-
-void DownloadItem::NotifyObserversDownloadOpened() {
- FOR_EACH_OBSERVER(Observer, observers_, OnDownloadOpened(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 (received_bytes_ > total_bytes_)
- total_bytes_ = 0;
-}
-
-// Updates from the download thread may have been posted while this download
-// was being cancelled in the UI thread, so we'll accept them unless we're
-// complete.
-void DownloadItem::Update(int64 bytes_so_far) {
- if (state_ == COMPLETE) {
- NOTREACHED();
- return;
- }
- UpdateSize(bytes_so_far);
- UpdateObservers();
-}
-
-// Triggered by a user action.
-void DownloadItem::Cancel(bool update_history) {
- if (state_ != IN_PROGRESS) {
- // Small downloads might be complete before this method has a chance to run.
- return;
- }
- state_ = CANCELLED;
- UpdateObservers();
- StopProgressTimer();
- if (update_history)
- manager_->DownloadCancelled(id_);
-}
-
-void DownloadItem::Finished(int64 size) {
- state_ = COMPLETE;
- UpdateSize(size);
- StopProgressTimer();
-}
-
-void DownloadItem::Remove(bool delete_on_disk) {
- Cancel(true);
- state_ = REMOVING;
- if (delete_on_disk)
- manager_->DeleteDownload(full_path_);
- manager_->RemoveDownload(db_handle_);
- // We have now been deleted.
-}
-
-void DownloadItem::StartProgressTimer() {
- update_timer_.Start(base::TimeDelta::FromMilliseconds(kUpdateTimeMs), this,
- &DownloadItem::UpdateObservers);
-}
-
-void DownloadItem::StopProgressTimer() {
- update_timer_.Stop();
-}
-
-bool DownloadItem::TimeRemaining(base::TimeDelta* remaining) const {
- if (total_bytes_ <= 0)
- return false; // We never received the content_length for this download.
-
- int64 speed = CurrentSpeed();
- if (speed == 0)
- return false;
-
- *remaining =
- base::TimeDelta::FromSeconds((total_bytes_ - received_bytes_) / speed);
- return true;
-}
-
-int64 DownloadItem::CurrentSpeed() const {
- base::TimeDelta diff = base::TimeTicks::Now() - start_tick_;
- int64 diff_ms = diff.InMilliseconds();
- return diff_ms == 0 ? 0 : received_bytes_ * 1000 / diff_ms;
-}
-
-int DownloadItem::PercentComplete() const {
- int percent = -1;
- if (total_bytes_ > 0)
- percent = static_cast<int>(received_bytes_ * 100.0 / total_bytes_);
- return percent;
-}
-
-void DownloadItem::Rename(const FilePath& full_path) {
- DCHECK(!full_path.empty());
- full_path_ = full_path;
- file_name_ = full_path_.BaseName();
-}
-
-void DownloadItem::TogglePause() {
- DCHECK(state_ == IN_PROGRESS);
- manager_->PauseDownload(id_, !is_paused_);
- is_paused_ = !is_paused_;
- UpdateObservers();
-}
-
-FilePath DownloadItem::GetFileName() const {
- if (safety_state_ == DownloadItem::SAFE)
- return file_name_;
- if (path_uniquifier_ > 0) {
- FilePath name(original_name_);
- download_util::AppendNumberToPath(&name, path_uniquifier_);
- return name;
- }
- return original_name_;
-}
-
-// DownloadManager implementation ----------------------------------------------
+// Our download table ID starts at 1, so we use 0 to represent a download that
+// has started, but has not yet had its data persisted in the table. We use fake
+// database handles in incognito mode starting at -1 and progressively getting
+// more negative.
+// static
+const int DownloadManager::kUninitializedHandle = 0;
// static
void DownloadManager::RegisterUserPrefs(PrefService* prefs) {