summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorahendrickson@chromium.org <ahendrickson@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-05-11 12:14:13 +0000
committerahendrickson@chromium.org <ahendrickson@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-05-11 12:14:13 +0000
commitbc932eff9ad3a4dcf306ed234d73510e49d33ff2 (patch)
tree763666f71b200d4eba8e708838b8ff183a794d6e
parentc34e9a7afa5eb386f22aa2480156b6bdec8597cd (diff)
downloadchromium_src-bc932eff9ad3a4dcf306ed234d73510e49d33ff2.zip
chromium_src-bc932eff9ad3a4dcf306ed234d73510e49d33ff2.tar.gz
chromium_src-bc932eff9ad3a4dcf306ed234d73510e49d33ff2.tar.bz2
Added DownloadProcessHandle class.
BUG=None TEST=Download tests still pass. Review URL: http://codereview.chromium.org/6932046 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@84967 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/download/download_file.cc23
-rw-r--r--chrome/browser/download/download_file.h9
-rw-r--r--chrome/browser/download/download_file_manager.cc47
-rw-r--r--chrome/browser/download/download_file_manager.h5
-rw-r--r--chrome/browser/download/download_file_unittest.cc5
-rw-r--r--chrome/browser/download/download_item.cc37
-rw-r--r--chrome/browser/download/download_item.h13
-rw-r--r--chrome/browser/download/download_manager.cc53
-rw-r--r--chrome/browser/download/download_manager.h13
-rw-r--r--chrome/browser/download/download_process_handle.cc39
-rw-r--r--chrome/browser/download/download_process_handle.h42
-rw-r--r--chrome/browser/download/download_util.cc21
-rw-r--r--chrome/browser/download/download_util.h6
-rw-r--r--chrome/browser/history/download_create_info.cc18
-rw-r--r--chrome/browser/history/download_create_info.h5
-rw-r--r--chrome/browser/renderer_host/download_resource_handler.cc7
-rw-r--r--chrome/chrome_browser.gypi2
17 files changed, 211 insertions, 134 deletions
diff --git a/chrome/browser/download/download_file.cc b/chrome/browser/download/download_file.cc
index 629ee11..264bae3 100644
--- a/chrome/browser/download/download_file.cc
+++ b/chrome/browser/download/download_file.cc
@@ -21,11 +21,9 @@ DownloadFile::DownloadFile(const DownloadCreateInfo* info,
info->received_bytes,
info->save_info.file_stream),
id_(info->download_id),
- child_id_(info->child_id),
- request_id_(info->request_id),
+ process_handle_(info->process_handle),
download_manager_(download_manager) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
-
}
DownloadFile::~DownloadFile() {
@@ -34,12 +32,7 @@ DownloadFile::~DownloadFile() {
void DownloadFile::CancelDownloadRequest(ResourceDispatcherHost* rdh) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
- BrowserThread::PostTask(
- BrowserThread::IO, FROM_HERE,
- NewRunnableFunction(&download_util::CancelDownloadRequest,
- rdh,
- child_id_,
- request_id_));
+ download_util::CancelDownloadRequest(rdh, process_handle_);
}
DownloadManager* DownloadFile::GetDownloadManager() {
@@ -49,15 +42,15 @@ DownloadManager* DownloadFile::GetDownloadManager() {
std::string DownloadFile::DebugString() const {
return base::StringPrintf("{"
- " full_path_ = " "\"%s\""
" id_ = " "%d"
- " child_id_ = " "%d"
- " request_id_ = " "%d"
+ " child_id = " "%d"
+ " request_id = " "%d"
+ " render_view_id = " "%d"
" Base File = %s"
" }",
- full_path_.value().c_str(),
id_,
- child_id_,
- request_id_,
+ process_handle_.child_id(),
+ process_handle_.request_id(),
+ process_handle_.render_view_id(),
BaseFile::DebugString().c_str());
}
diff --git a/chrome/browser/download/download_file.h b/chrome/browser/download/download_file.h
index 815f643..63060fe 100644
--- a/chrome/browser/download/download_file.h
+++ b/chrome/browser/download/download_file.h
@@ -11,6 +11,7 @@
#include "base/basictypes.h"
#include "base/memory/ref_counted.h"
#include "chrome/browser/download/base_file.h"
+#include "chrome/browser/download/download_process_handle.h"
#include "chrome/browser/download/download_types.h"
struct DownloadCreateInfo;
@@ -40,11 +41,9 @@ class DownloadFile : public BaseFile {
// the DownloadFileManager for its internal record keeping.
int id_;
- // IDs for looking up the tab we are associated with.
- int child_id_;
-
- // Handle for informing the ResourceDispatcherHost of a UI based cancel.
- int request_id_;
+ // The handle to the process information. Used for operations outside the
+ // download system, specifically canceling a download.
+ DownloadProcessHandle process_handle_;
// DownloadManager this download belongs to.
scoped_refptr<DownloadManager> download_manager_;
diff --git a/chrome/browser/download/download_file_manager.cc b/chrome/browser/download/download_file_manager.cc
index 2522a98..faa85ba 100644
--- a/chrome/browser/download/download_file_manager.cc
+++ b/chrome/browser/download/download_file_manager.cc
@@ -11,6 +11,7 @@
#include "base/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/browser/download/download_manager.h"
+#include "chrome/browser/download/download_process_handle.h"
#include "chrome/browser/download/download_util.h"
#include "chrome/browser/history/download_create_info.h"
#include "chrome/browser/net/chrome_url_request_context.h"
@@ -30,21 +31,6 @@ namespace {
// cause it to become unresponsive (in milliseconds).
const int kUpdatePeriodMs = 500;
-DownloadManager* DownloadManagerForRenderViewHost(int render_process_id,
- int render_view_id) {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
-
- TabContents* contents = tab_util::GetTabContentsByID(render_process_id,
- render_view_id);
- if (contents) {
- Profile* profile = contents->profile();
- if (profile)
- return profile->GetDownloadManager();
- }
-
- return NULL;
-}
-
} // namespace
DownloadFileManager::DownloadFileManager(ResourceDispatcherHost* rdh)
@@ -78,12 +64,8 @@ void DownloadFileManager::CreateDownloadFile(DownloadCreateInfo* info,
scoped_ptr<DownloadFile>
download_file(new DownloadFile(info, download_manager));
if (!download_file->Initialize(get_hash)) {
- BrowserThread::PostTask(
- BrowserThread::IO, FROM_HERE,
- NewRunnableFunction(&download_util::CancelDownloadRequest,
- resource_dispatcher_host_,
- info->child_id,
- info->request_id));
+ download_util::CancelDownloadRequest(resource_dispatcher_host_,
+ info->process_handle);
delete info;
return;
}
@@ -97,7 +79,7 @@ void DownloadFileManager::CreateDownloadFile(DownloadCreateInfo* info,
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
NewRunnableMethod(this, &DownloadFileManager::ResumeDownloadRequest,
- info->child_id, info->request_id));
+ info->process_handle));
StartUpdateTimer();
@@ -107,11 +89,14 @@ void DownloadFileManager::CreateDownloadFile(DownloadCreateInfo* info,
&DownloadManager::StartDownload, info));
}
-void DownloadFileManager::ResumeDownloadRequest(int child_id, int request_id) {
+void DownloadFileManager::ResumeDownloadRequest(
+ DownloadProcessHandle process) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
// This balances the pause in DownloadResourceHandler::OnResponseStarted.
- resource_dispatcher_host_->PauseRequest(child_id, request_id, false);
+ resource_dispatcher_host_->PauseRequest(process.child_id(),
+ process.request_id(),
+ false);
}
DownloadFile* DownloadFileManager::GetDownloadFile(int id) {
@@ -158,15 +143,10 @@ void DownloadFileManager::StartDownload(DownloadCreateInfo* info) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(info);
- DownloadManager* manager = DownloadManagerForRenderViewHost(
- info->child_id, info->render_view_id);
+ DownloadManager* manager = info->process_handle.GetDownloadManager();
if (!manager) {
- BrowserThread::PostTask(
- BrowserThread::IO, FROM_HERE,
- NewRunnableFunction(&download_util::CancelDownloadRequest,
- resource_dispatcher_host_,
- info->child_id,
- info->request_id));
+ download_util::CancelDownloadRequest(resource_dispatcher_host_,
+ info->process_handle);
delete info;
return;
}
@@ -178,7 +158,8 @@ void DownloadFileManager::StartDownload(DownloadCreateInfo* info) {
BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
NewRunnableMethod(this, &DownloadFileManager::CreateDownloadFile,
- info, make_scoped_refptr(manager), hash_needed));
+ info,
+ make_scoped_refptr(manager), hash_needed));
}
// We don't forward an update to the UI thread here, since we want to throttle
diff --git a/chrome/browser/download/download_file_manager.h b/chrome/browser/download/download_file_manager.h
index 1d96b5d..6047aee 100644
--- a/chrome/browser/download/download_file_manager.h
+++ b/chrome/browser/download/download_file_manager.h
@@ -47,6 +47,7 @@
#include "base/hash_tables.h"
#include "base/memory/ref_counted.h"
#include "base/timer.h"
+#include "chrome/browser/download/download_process_handle.h"
#include "ui/gfx/native_widget_types.h"
struct DownloadBuffer;
@@ -143,7 +144,9 @@ class DownloadFileManager
// Tells the ResourceDispatcherHost to resume a download request
// that was paused to wait for the on-disk file to be created.
- void ResumeDownloadRequest(int child_id, int request_id);
+ // |process| is passed by value because this is called from other
+ // threads, and this way we don't have to worry about object lifetimes.
+ void ResumeDownloadRequest(DownloadProcessHandle process);
// Called only on the download thread.
DownloadFile* GetDownloadFile(int id);
diff --git a/chrome/browser/download/download_file_unittest.cc b/chrome/browser/download/download_file_unittest.cc
index 7826330..ab2758e 100644
--- a/chrome/browser/download/download_file_unittest.cc
+++ b/chrome/browser/download/download_file_unittest.cc
@@ -8,6 +8,7 @@
#include "base/string_number_conversions.h"
#include "chrome/browser/download/download_file.h"
#include "chrome/browser/download/download_manager.h"
+#include "chrome/browser/download/download_process_handle.h"
#include "chrome/browser/download/download_status_updater.h"
#include "chrome/browser/download/download_util.h"
#include "chrome/browser/download/mock_download_manager.h"
@@ -57,8 +58,8 @@ class DownloadFileTest : public testing::Test {
virtual void CreateDownloadFile(scoped_ptr<DownloadFile>* file, int offset) {
DownloadCreateInfo info;
info.download_id = kDummyDownloadId + offset;
- info.child_id = kDummyChildId;
- info.request_id = kDummyRequestId - offset;
+ info.process_handle =
+ DownloadProcessHandle(kDummyChildId, -1, kDummyRequestId - offset);
info.save_info.file_stream = file_stream_;
file->reset(new DownloadFile(&info, download_manager_));
}
diff --git a/chrome/browser/download/download_item.cc b/chrome/browser/download/download_item.cc
index c94d3b8..59126a8 100644
--- a/chrome/browser/download/download_item.cc
+++ b/chrome/browser/download/download_item.cc
@@ -135,8 +135,6 @@ DownloadItem::DownloadItem(DownloadManager* download_manager,
danger_type_(NOT_DANGEROUS),
auto_opened_(false),
target_name_(info.original_name),
- render_process_id_(-1),
- request_id_(-1),
save_as_(false),
is_otr_(false),
is_extension_install_(info.is_extension_install),
@@ -177,8 +175,7 @@ DownloadItem::DownloadItem(DownloadManager* download_manager,
info.is_dangerous_url)),
auto_opened_(false),
target_name_(info.original_name),
- render_process_id_(info.child_id),
- request_id_(info.request_id),
+ process_handle_(info.process_handle),
save_as_(info.prompt_user_for_save_location),
is_otr_(is_otr),
is_extension_install_(info.is_extension_install),
@@ -213,8 +210,6 @@ DownloadItem::DownloadItem(DownloadManager* download_manager,
safety_state_(SAFE),
danger_type_(NOT_DANGEROUS),
auto_opened_(false),
- render_process_id_(-1),
- request_id_(-1),
save_as_(false),
is_otr_(is_otr),
is_extension_install_(false),
@@ -616,28 +611,42 @@ std::string DownloadItem::DebugString(bool verbose) const {
std::string description = base::StringPrintf(
"{ id_ = %d state = %s", id_, DebugDownloadStateString(state()));
+ // Construct a string of the URL chain.
+ std::string url_list("<none>");
+ if (!url_chain_.empty()) {
+ std::vector<GURL>::const_iterator iter = url_chain_.begin();
+ std::vector<GURL>::const_iterator last = url_chain_.end();
+ url_list = (*iter).spec();
+ ++iter;
+ for ( ; verbose && (iter != last); ++iter) {
+ url_list += " -> ";
+ const GURL& next_url = *iter;
+ url_list += next_url.spec();
+ }
+ }
+
if (verbose) {
description += base::StringPrintf(
" db_handle = %" PRId64
" total_bytes = %" PRId64
- " is_paused = %c"
- " is_extension_install = %c"
- " is_otr = %c"
- " safety_state = %s"
- " url = \"%s\""
+ " is_paused = " "%c"
+ " is_extension_install = " "%c"
+ " is_otr = " "%c"
+ " safety_state = " "%s"
+ " url_chain = " "\"%s\""
" target_name_ = \"%" PRFilePath "\""
- " full_path = \"%" PRFilePath "\" }",
+ " full_path = \"%" PRFilePath "\"",
db_handle(),
total_bytes(),
is_paused() ? 'T' : 'F',
is_extension_install() ? 'T' : 'F',
is_otr() ? 'T' : 'F',
DebugSafetyStateString(safety_state()),
- url().spec().c_str(),
+ url_list.c_str(),
target_name_.value().c_str(),
full_path().value().c_str());
} else {
- description += " url = \"" + url().spec() + "\" }";
+ description += base::StringPrintf(" url = \"%s\"", url_list.c_str());
}
return description;
}
diff --git a/chrome/browser/download/download_item.h b/chrome/browser/download/download_item.h
index aafab61..3a6bbd7 100644
--- a/chrome/browser/download/download_item.h
+++ b/chrome/browser/download/download_item.h
@@ -25,6 +25,7 @@
#include "base/observer_list.h"
#include "base/time.h"
#include "base/timer.h"
+#include "chrome/browser/download/download_process_handle.h"
#include "googleurl/src/gurl.h"
class DownloadFileManager;
@@ -257,8 +258,6 @@ class DownloadItem {
bool is_paused() const { return is_paused_; }
bool open_when_complete() const { return open_when_complete_; }
void set_open_when_complete(bool open) { open_when_complete_ = open; }
- int render_process_id() const { return render_process_id_; }
- int request_id() const { return request_id_; }
SafetyState safety_state() const { return safety_state_; }
void set_safety_state(SafetyState safety_state) {
safety_state_ = safety_state;
@@ -273,6 +272,10 @@ class DownloadItem {
void set_opened(bool opened) { opened_ = opened; }
bool opened() const { return opened_; }
+ const DownloadProcessHandle& process_handle() const {
+ return process_handle_;
+ }
+
// Returns the final target file path for the download.
FilePath GetTargetFilePath() const;
@@ -382,9 +385,9 @@ class DownloadItem {
// This stores their final target name.
FilePath target_name_;
- // For canceling or pausing requests.
- int render_process_id_;
- int request_id_;
+ // The handle to the process information. Used for operations outside the
+ // download system.
+ DownloadProcessHandle process_handle_;
// True if the item was downloaded as a result of 'save as...'
bool save_as_;
diff --git a/chrome/browser/download/download_manager.cc b/chrome/browser/download/download_manager.cc
index fb80f8f..fb9fb7b 100644
--- a/chrome/browser/download/download_manager.cc
+++ b/chrome/browser/download/download_manager.cc
@@ -22,6 +22,7 @@
#include "chrome/browser/download/download_history.h"
#include "chrome/browser/download/download_item.h"
#include "chrome/browser/download/download_prefs.h"
+#include "chrome/browser/download/download_process_handle.h"
#include "chrome/browser/download/download_safe_browsing_client.h"
#include "chrome/browser/download/download_status_updater.h"
#include "chrome/browser/download/download_util.h"
@@ -429,8 +430,7 @@ void DownloadManager::OnPathExistenceAvailable(DownloadCreateInfo* info) {
if (!select_file_dialog_.get())
select_file_dialog_ = SelectFileDialog::Create(this);
- TabContents* contents = tab_util::GetTabContentsByID(info->child_id,
- info->render_view_id);
+ TabContents* contents = info->process_handle.GetTabContents();
SelectFileDialog::FileTypeInfo file_type_info;
FilePath::StringType extension = info->suggested_path.Extension();
if (!extension.empty()) {
@@ -702,22 +702,15 @@ void DownloadManager::DownloadCancelled(int32 download_id) {
download_history_->UpdateEntry(download);
}
- DownloadCancelledInternal(download_id,
- download->render_process_id(),
- download->request_id());
+ DownloadCancelledInternal(download_id, download->process_handle());
}
-void DownloadManager::DownloadCancelledInternal(int download_id,
- int render_process_id,
- int request_id) {
+void DownloadManager::DownloadCancelledInternal(
+ int download_id, DownloadProcessHandle process_handle) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
// Cancel the network request. RDH is guaranteed to outlive the IO thread.
- BrowserThread::PostTask(
- BrowserThread::IO, FROM_HERE,
- NewRunnableFunction(&download_util::CancelDownloadRequest,
- g_browser_process->resource_dispatcher_host(),
- render_process_id,
- request_id));
+ download_util::CancelDownloadRequest(
+ g_browser_process->resource_dispatcher_host(), process_handle);
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE,
@@ -775,8 +768,7 @@ void DownloadManager::PauseDownload(int32 download_id, bool pause) {
NewRunnableMethod(this,
&DownloadManager::PauseDownloadRequest,
g_browser_process->resource_dispatcher_host(),
- download->render_process_id(),
- download->request_id(),
+ download->process_handle(),
pause));
}
@@ -786,11 +778,12 @@ void DownloadManager::UpdateAppIcon() {
}
void DownloadManager::PauseDownloadRequest(ResourceDispatcherHost* rdh,
- int render_process_id,
- int request_id,
+ DownloadProcessHandle process_handle,
bool pause) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
- rdh->PauseRequest(render_process_id, request_id, pause);
+ rdh->PauseRequest(process_handle.child_id(),
+ process_handle.request_id(),
+ pause);
}
void DownloadManager::RemoveDownload(int64 download_handle) {
@@ -983,9 +976,7 @@ void DownloadManager::FileSelectionCanceled(void* params) {
// The user didn't pick a place to save the file, so need to cancel the
// download that's already in progress to the temporary location.
DownloadCreateInfo* info = reinterpret_cast<DownloadCreateInfo*>(params);
- DownloadCancelledInternal(info->download_id,
- info->child_id,
- info->request_id);
+ DownloadCancelledInternal(info->download_id, info->process_handle);
}
void DownloadManager::DangerousDownloadValidated(DownloadItem* download) {
@@ -1045,7 +1036,7 @@ void DownloadManager::OnCreateDownloadEntryComplete(
// Show in the appropriate browser UI.
// This includes buttons to save or cancel, for a dangerous download.
- ShowDownloadInBrowser(info, download);
+ ShowDownloadInBrowser(&info.process_handle, download);
// Inform interested objects about the new download.
NotifyModelChanged();
@@ -1069,13 +1060,15 @@ void DownloadManager::OnCreateDownloadEntryComplete(
}
}
-void DownloadManager::ShowDownloadInBrowser(const DownloadCreateInfo& info,
- DownloadItem* download) {
+void DownloadManager::ShowDownloadInBrowser(
+ DownloadProcessHandle* process_handle, DownloadItem* download) {
+ if (!process_handle)
+ return;
+
// The 'contents' may no longer exist if the user closed the tab before we
// get this start completion event. If it does, tell the origin TabContents
// to display its download shelf.
- TabContents* contents = tab_util::GetTabContentsByID(info.child_id,
- info.render_view_id);
+ TabContents* contents = process_handle->GetTabContents();
// If the contents no longer exists, we start the download in the last active
// browser. This is not ideal but better than fully hiding the download from
@@ -1086,8 +1079,10 @@ void DownloadManager::ShowDownloadInBrowser(const DownloadCreateInfo& info,
contents = last_active->GetSelectedTabContents();
}
- if (contents)
- contents->OnStartDownload(download);
+ if (!contents)
+ return;
+
+ contents->OnStartDownload(download);
}
// Clears the last download path, used to initialize "save as" dialogs.
diff --git a/chrome/browser/download/download_manager.h b/chrome/browser/download/download_manager.h
index f213a74..5cc4e0e 100644
--- a/chrome/browser/download/download_manager.h
+++ b/chrome/browser/download/download_manager.h
@@ -42,6 +42,7 @@
#include "base/scoped_ptr.h"
#include "base/time.h"
#include "chrome/browser/download/download_status_updater_delegate.h"
+#include "chrome/browser/download/download_process_handle.h"
#include "chrome/browser/ui/shell_dialogs.h"
#include "content/browser/browser_thread.h"
@@ -194,7 +195,7 @@ class DownloadManager
DownloadCreateInfo info, int64 db_handle);
// Display a new download in the appropriate browser UI.
- void ShowDownloadInBrowser(const DownloadCreateInfo& info,
+ void ShowDownloadInBrowser(DownloadProcessHandle* process_handle,
DownloadItem* download);
// The number of in progress (including paused) downloads.
@@ -285,9 +286,10 @@ class DownloadManager
void AttachDownloadItem(DownloadCreateInfo* info);
// Download cancel helper function.
+ // |process_handle| is passed by value because it is ultimately passed to
+ // other threads, and this way we don't have to worry about object lifetimes.
void DownloadCancelledInternal(int download_id,
- int render_process_id,
- int request_id);
+ DownloadProcessHandle process_handle);
// All data has been downloaded.
// |hash| is sha256 hash for the downloaded file. It is empty when the hash
@@ -302,9 +304,10 @@ class DownloadManager
// Makes the ResourceDispatcherHost pause/un-pause a download request.
// Called on the IO thread.
+ // |process_handle| is passed by value because this is called from other
+ // threads, and this way we don't have to worry about object lifetimes.
void PauseDownloadRequest(ResourceDispatcherHost* rdh,
- int render_process_id,
- int request_id,
+ DownloadProcessHandle process_handle,
bool pause);
// Inform observers that the model has changed.
diff --git a/chrome/browser/download/download_process_handle.cc b/chrome/browser/download/download_process_handle.cc
new file mode 100644
index 0000000..f4c7f32
--- /dev/null
+++ b/chrome/browser/download/download_process_handle.cc
@@ -0,0 +1,39 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/download/download_process_handle.h"
+
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/tab_contents/tab_util.h"
+#include "content/browser/browser_thread.h"
+#include "content/browser/tab_contents/tab_contents.h"
+
+DownloadProcessHandle::DownloadProcessHandle()
+ : child_id_(-1), render_view_id_(-1), request_id_(-1) {
+}
+
+DownloadProcessHandle::DownloadProcessHandle(int child_id,
+ int render_view_id,
+ int request_id)
+ : child_id_(child_id),
+ render_view_id_(render_view_id),
+ request_id_(request_id) {
+}
+
+TabContents* DownloadProcessHandle::GetTabContents() {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ return tab_util::GetTabContentsByID(child_id_, render_view_id_);
+}
+
+DownloadManager* DownloadProcessHandle::GetDownloadManager() {
+ TabContents* contents = GetTabContents();
+ if (!contents)
+ return NULL;
+
+ Profile* profile = contents->profile();
+ if (!profile)
+ return NULL;
+
+ return profile->GetDownloadManager();
+}
diff --git a/chrome/browser/download/download_process_handle.h b/chrome/browser/download/download_process_handle.h
new file mode 100644
index 0000000..b722d61
--- /dev/null
+++ b/chrome/browser/download/download_process_handle.h
@@ -0,0 +1,42 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_PROCESS_HANDLE_H_
+#define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_PROCESS_HANDLE_H_
+#pragma once
+
+class DownloadManager;
+class ResourceDispatcherHost;
+class TabContents;
+
+// A handle used by the download system for operations on external
+// objects associated with the download (e.g. URLRequest, TabContents,
+// DownloadManager).
+// This class needs to be copyable, so we can pass it across threads and not
+// worry about lifetime or const-ness.
+class DownloadProcessHandle {
+ public:
+ DownloadProcessHandle();
+ DownloadProcessHandle(int child_id, int render_view_id, int request_id);
+
+ // These functions must be called on the UI thread.
+ TabContents* GetTabContents();
+ DownloadManager* GetDownloadManager();
+
+ int child_id() const { return child_id_; }
+ int render_view_id() const { return render_view_id_; }
+ int request_id() const { return request_id_; }
+
+ private:
+ // The ID of the child process that started the download.
+ int child_id_;
+
+ // The ID of the render view that started the download.
+ int render_view_id_;
+
+ // The ID associated with the request used for the download.
+ int request_id_;
+};
+
+#endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_PROCESS_HANDLE_H_
diff --git a/chrome/browser/download/download_util.cc b/chrome/browser/download/download_util.cc
index 90fe73d..f68d676 100644
--- a/chrome/browser/download/download_util.cc
+++ b/chrome/browser/download/download_util.cc
@@ -809,13 +809,24 @@ void DownloadUrl(
*context);
}
-void CancelDownloadRequest(ResourceDispatcherHost* rdh,
- int render_process_id,
- int request_id) {
+static void CancelDownloadRequestOnIOThread(
+ ResourceDispatcherHost* rdh, DownloadProcessHandle process_handle) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
// |rdh| may be NULL in unit tests.
- if (rdh)
- rdh->CancelRequest(render_process_id, request_id, false);
+ if (!rdh)
+ return;
+
+ rdh->CancelRequest(process_handle.child_id(),
+ process_handle.request_id(),
+ false);
+}
+
+void CancelDownloadRequest(ResourceDispatcherHost* rdh,
+ DownloadProcessHandle process_handle) {
+ BrowserThread::PostTask(
+ BrowserThread::IO, FROM_HERE,
+ NewRunnableFunction(&download_util::CancelDownloadRequestOnIOThread,
+ rdh, process_handle));
}
void NotifyDownloadInitiated(int render_process_id, int render_view_id) {
diff --git a/chrome/browser/download/download_util.h b/chrome/browser/download/download_util.h
index d933d22..376fc18 100644
--- a/chrome/browser/download/download_util.h
+++ b/chrome/browser/download/download_util.h
@@ -13,6 +13,7 @@
#include "base/basictypes.h"
#include "base/file_path.h"
#include "base/string16.h"
+#include "chrome/browser/download/download_process_handle.h"
#include "ui/gfx/native_widget_types.h"
#if defined(TOOLKIT_VIEWS)
@@ -242,9 +243,10 @@ void DownloadUrl(const GURL& url,
// Tells the resource dispatcher host to cancel a download request.
// Must be called on the IO thread.
+// |process_handle| is passed by value because it is ultimately passed to
+// other threads, and this way we don't have to worry about object lifetimes.
void CancelDownloadRequest(ResourceDispatcherHost* rdh,
- int render_process_id,
- int request_id);
+ DownloadProcessHandle process_handle);
// Sends a notification on downloads being initiated
// Must be called on the UI thread.
diff --git a/chrome/browser/history/download_create_info.cc b/chrome/browser/history/download_create_info.cc
index a7ece9a..12b23a8 100644
--- a/chrome/browser/history/download_create_info.cc
+++ b/chrome/browser/history/download_create_info.cc
@@ -26,9 +26,6 @@ DownloadCreateInfo::DownloadCreateInfo(const FilePath& path,
state(state),
download_id(download_id),
has_user_gesture(has_user_gesture),
- child_id(-1),
- render_view_id(-1),
- request_id(-1),
db_handle(0),
prompt_user_for_save_location(false),
is_dangerous_file(false),
@@ -43,9 +40,6 @@ DownloadCreateInfo::DownloadCreateInfo()
state(-1),
download_id(-1),
has_user_gesture(false),
- child_id(-1),
- render_view_id(-1),
- request_id(-1),
db_handle(0),
prompt_user_for_save_location(false),
is_dangerous_file(false),
@@ -62,24 +56,24 @@ bool DownloadCreateInfo::IsDangerous() {
std::string DownloadCreateInfo::DebugString() const {
return base::StringPrintf("{"
- " url_ = \"%s\""
+ " download_id = %d"
+ " url = \"%s\""
" path = \"%" PRFilePath "\""
" received_bytes = %" PRId64
" total_bytes = %" PRId64
" child_id = %d"
" render_view_id = %d"
" request_id = %d"
- " download_id = %d"
" prompt_user_for_save_location = %c"
" }",
+ download_id,
url().spec().c_str(),
path.value().c_str(),
received_bytes,
total_bytes,
- child_id,
- render_view_id,
- request_id,
- download_id,
+ process_handle.child_id(),
+ process_handle.render_view_id(),
+ process_handle.request_id(),
prompt_user_for_save_location ? 'T' : 'F');
}
diff --git a/chrome/browser/history/download_create_info.h b/chrome/browser/history/download_create_info.h
index 361183e..77e966b 100644
--- a/chrome/browser/history/download_create_info.h
+++ b/chrome/browser/history/download_create_info.h
@@ -15,6 +15,7 @@
#include "base/file_path.h"
#include "base/time.h"
#include "chrome/browser/download/download_file.h"
+#include "chrome/browser/download/download_process_handle.h"
#include "googleurl/src/gurl.h"
// Used for informing the download database of a new download, where we don't
@@ -57,9 +58,7 @@ struct DownloadCreateInfo {
int32 state;
int32 download_id;
bool has_user_gesture;
- int child_id;
- int render_view_id;
- int request_id;
+ DownloadProcessHandle process_handle;
int64 db_handle;
std::string content_disposition;
std::string mime_type;
diff --git a/chrome/browser/renderer_host/download_resource_handler.cc b/chrome/browser/renderer_host/download_resource_handler.cc
index 7b7e02f..bbcab26 100644
--- a/chrome/browser/renderer_host/download_resource_handler.cc
+++ b/chrome/browser/renderer_host/download_resource_handler.cc
@@ -12,6 +12,7 @@
#include "base/stringprintf.h"
#include "chrome/browser/download/download_item.h"
#include "chrome/browser/download/download_file_manager.h"
+#include "chrome/browser/download/download_process_handle.h"
#include "chrome/browser/download/download_util.h"
#include "chrome/browser/history/download_create_info.h"
#include "content/browser/browser_thread.h"
@@ -88,9 +89,9 @@ bool DownloadResourceHandler::OnResponseStarted(int request_id,
info->state = DownloadItem::IN_PROGRESS;
info->download_id = download_id_;
info->has_user_gesture = request_info->has_user_gesture();
- info->child_id = global_id_.child_id;
- info->render_view_id = render_view_id_;
- info->request_id = global_id_.request_id;
+ info->process_handle = DownloadProcessHandle(global_id_.child_id,
+ render_view_id_,
+ global_id_.request_id);
info->content_disposition = content_disposition_;
info->mime_type = response->response_head.mime_type;
// TODO(ahendrickson) -- Get the last modified time and etag, so we can
diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi
index d0091a8..a17ec89 100644
--- a/chrome/chrome_browser.gypi
+++ b/chrome/chrome_browser.gypi
@@ -792,6 +792,8 @@
'browser/download/download_manager.h',
'browser/download/download_prefs.cc',
'browser/download/download_prefs.h',
+ 'browser/download/download_process_handle.cc',
+ 'browser/download/download_process_handle.h',
'browser/download/download_request_infobar_delegate.cc',
'browser/download/download_request_infobar_delegate.h',
'browser/download/download_request_limiter.cc',