Index: content/browser/download/download_item_impl.cc |
diff --git a/content/browser/download/download_item_impl.cc b/content/browser/download/download_item_impl.cc |
index 1c3236034f7d023d6e2cd8e25c0eb9a3242bc594..73c3cc9435fd6f40dbc4e7f224812936908a697d 100644 |
--- a/content/browser/download/download_item_impl.cc |
+++ b/content/browser/download/download_item_impl.cc |
@@ -20,7 +20,6 @@ |
#include "content/browser/download/download_file.h" |
#include "content/browser/download/download_file_manager.h" |
#include "content/browser/download/download_id.h" |
-#include "content/browser/download/download_manager.h" |
#include "content/browser/download/download_persistent_store_info.h" |
#include "content/browser/download/download_request_handle.h" |
#include "content/browser/download/download_stats.h" |
@@ -28,7 +27,6 @@ |
#include "content/browser/tab_contents/tab_contents.h" |
#include "content/public/browser/browser_thread.h" |
#include "content/public/browser/content_browser_client.h" |
-#include "content/public/browser/download_manager_delegate.h" |
#include "net/base/net_util.h" |
using content::BrowserThread; |
@@ -118,15 +116,34 @@ class NullDownloadRequestHandle : public DownloadRequestHandleInterface { |
} // namespace |
+// Infrastructure in DownloadItemImpl::Delegate to assert invariant that |
+// delegate always outlives all attached DownloadItemImpls. |
+DownloadItemImpl::Delegate::Delegate() |
+ : count_(0) {} |
+ |
+DownloadItemImpl::Delegate::~Delegate() { |
+ DCHECK_EQ(0, count_); |
+} |
+ |
+void DownloadItemImpl::Delegate::Attach() { |
+ ++count_; |
+} |
+ |
+void DownloadItemImpl::Delegate::Detach() { |
+ DCHECK_LT(0, count_); |
+ --count_; |
+} |
+ |
// 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. |
// Constructor for reading from the history service. |
-DownloadItemImpl::DownloadItemImpl(DownloadManager* download_manager, |
+DownloadItemImpl::DownloadItemImpl(Delegate* delegate, |
+ DownloadId download_id, |
const DownloadPersistentStoreInfo& info) |
- : download_id_(download_manager->GetNextId()), |
+ : download_id_(download_id), |
full_path_(info.path), |
url_chain_(1, info.url), |
referrer_url_(info.referrer_url), |
@@ -138,7 +155,7 @@ DownloadItemImpl::DownloadItemImpl(DownloadManager* download_manager, |
start_time_(info.start_time), |
end_time_(info.end_time), |
db_handle_(info.db_handle), |
- download_manager_(download_manager), |
+ delegate_(delegate), |
is_paused_(false), |
open_when_complete_(false), |
file_externally_removed_(false), |
@@ -150,6 +167,7 @@ DownloadItemImpl::DownloadItemImpl(DownloadManager* download_manager, |
opened_(info.opened), |
open_enabled_(true), |
delegate_delayed_complete_(false) { |
+ delegate_->Attach(); |
if (IsInProgress()) |
state_ = CANCELLED; |
if (IsComplete()) |
@@ -159,7 +177,7 @@ DownloadItemImpl::DownloadItemImpl(DownloadManager* download_manager, |
// Constructing for a regular download: |
DownloadItemImpl::DownloadItemImpl( |
- DownloadManager* download_manager, |
+ Delegate* delegate, |
const DownloadCreateInfo& info, |
DownloadRequestHandleInterface* request_handle, |
bool is_otr) |
@@ -186,7 +204,7 @@ DownloadItemImpl::DownloadItemImpl( |
state_(IN_PROGRESS), |
start_time_(info.start_time), |
db_handle_(DownloadItem::kUninitializedHandle), |
- download_manager_(download_manager), |
+ delegate_(delegate), |
is_paused_(false), |
open_when_complete_(false), |
file_externally_removed_(false), |
@@ -198,11 +216,12 @@ DownloadItemImpl::DownloadItemImpl( |
opened_(false), |
open_enabled_(true), |
delegate_delayed_complete_(false) { |
+ delegate_->Attach(); |
Init(true /* actively downloading */); |
} |
// Constructing for the "Save Page As..." feature: |
-DownloadItemImpl::DownloadItemImpl(DownloadManager* download_manager, |
+DownloadItemImpl::DownloadItemImpl(Delegate* delegate, |
const FilePath& path, |
const GURL& url, |
bool is_otr, |
@@ -220,7 +239,7 @@ DownloadItemImpl::DownloadItemImpl(DownloadManager* download_manager, |
state_(IN_PROGRESS), |
start_time_(base::Time::Now()), |
db_handle_(DownloadItem::kUninitializedHandle), |
- download_manager_(download_manager), |
+ delegate_(delegate), |
is_paused_(false), |
open_when_complete_(false), |
file_externally_removed_(false), |
@@ -232,6 +251,7 @@ DownloadItemImpl::DownloadItemImpl(DownloadManager* download_manager, |
opened_(false), |
open_enabled_(true), |
delegate_delayed_complete_(false) { |
+ delegate_->Attach(); |
Init(true /* actively downloading */); |
} |
@@ -240,7 +260,8 @@ DownloadItemImpl::~DownloadItemImpl() { |
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
TransitionTo(REMOVING); |
- download_manager_->AssertQueueStateConsistent(this); |
+ delegate_->AssertStateConsistent(this); |
+ delegate_->Detach(); |
} |
void DownloadItemImpl::AddObserver(Observer* observer) { |
@@ -273,8 +294,7 @@ bool DownloadItemImpl::CanOpenDownload() { |
} |
bool DownloadItemImpl::ShouldOpenFileBasedOnExtension() { |
- return download_manager_->delegate()->ShouldOpenFileBasedOnExtension( |
- GetUserVerifiedFilePath()); |
+ return delegate_->ShouldOpenFileBasedOnExtension(GetUserVerifiedFilePath()); |
} |
void DownloadItemImpl::OpenDownload() { |
@@ -293,11 +313,11 @@ void DownloadItemImpl::OpenDownload() { |
// don't generally have the proper interface for that to the external |
// program that opens the file. So instead we spawn a check to update |
// the UI if the file has been deleted in parallel with the open. |
- download_manager_->CheckForFileRemoval(this); |
+ delegate_->CheckForFileRemoval(this); |
download_stats::RecordOpen(GetEndTime(), !GetOpened()); |
opened_ = true; |
FOR_EACH_OBSERVER(Observer, observers_, OnDownloadOpened(this)); |
- download_manager_->MarkDownloadOpened(this); |
+ delegate_->DownloadOpened(this); |
// For testing: If download opening is disabled on this item, |
// make the rest of the routine a no-op. |
@@ -325,7 +345,7 @@ void DownloadItemImpl::DangerousDownloadValidated() { |
safety_state_ = DANGEROUS_BUT_VALIDATED; |
UpdateObservers(); |
- download_manager_->MaybeCompleteDownload(this); |
+ delegate_->MaybeCompleteDownload(this); |
} |
void DownloadItemImpl::UpdateSize(int64 bytes_so_far) { |
@@ -376,7 +396,7 @@ void DownloadItemImpl::Cancel(bool user_cancel) { |
TransitionTo(CANCELLED); |
if (user_cancel) |
- download_manager_->DownloadCancelledInternal(this); |
+ delegate_->DownloadCancelled(this); |
} |
void DownloadItemImpl::MarkAsComplete() { |
@@ -409,6 +429,11 @@ void DownloadItemImpl::OnDownloadedFileRemoved() { |
UpdateObservers(); |
} |
+void DownloadItemImpl::MaybeCompleteDownload() { |
+ // TODO(rdsmith): Move logic for this function here. |
+ delegate_->MaybeCompleteDownload(this); |
+} |
+ |
void DownloadItemImpl::Completed() { |
// TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
@@ -418,7 +443,7 @@ void DownloadItemImpl::Completed() { |
DCHECK(all_data_saved_); |
end_time_ = base::Time::Now(); |
TransitionTo(COMPLETE); |
- download_manager_->DownloadCompleted(GetId()); |
+ delegate_->DownloadCompleted(this); |
download_stats::RecordDownloadCompleted(start_tick_, received_bytes_); |
if (auto_opened_) { |
@@ -504,12 +529,12 @@ void DownloadItemImpl::Remove() { |
// TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
- download_manager_->AssertQueueStateConsistent(this); |
+ delegate_->AssertStateConsistent(this); |
Cancel(true); |
- download_manager_->AssertQueueStateConsistent(this); |
+ delegate_->AssertStateConsistent(this); |
TransitionTo(REMOVING); |
- download_manager_->RemoveDownload(db_handle_); |
+ delegate_->DownloadRemoved(this); |
// We have now been deleted. |
} |
@@ -583,16 +608,17 @@ void DownloadItemImpl::OnDownloadCompleting(DownloadFileManager* file_manager) { |
if (NeedsRename()) { |
BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
base::Bind(&DownloadFileManager::RenameCompletingDownloadFile, |
- file_manager, GetGlobalId(), |
+ file_manager, download_id_, |
GetTargetFilePath(), GetSafetyState() == SAFE)); |
return; |
} |
Completed(); |
- BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
- base::Bind(&DownloadFileManager::CompleteDownload, |
- file_manager, GetGlobalId())); |
+ BrowserThread::PostTask( |
+ BrowserThread::FILE, FROM_HERE, |
+ base::Bind(&DownloadFileManager::CompleteDownload, |
+ file_manager, download_id_)); |
} |
void DownloadItemImpl::OnDownloadRenamedToFinalName(const FilePath& full_path) { |
@@ -607,7 +633,7 @@ void DownloadItemImpl::OnDownloadRenamedToFinalName(const FilePath& full_path) { |
Rename(full_path); |
- if (download_manager_->delegate()->ShouldOpenDownload(this)) { |
+ if (delegate_->ShouldOpenDownload(this)) { |
Completed(); |
} else { |
delegate_delayed_complete_ = true; |
@@ -630,11 +656,8 @@ bool DownloadItemImpl::MatchesQuery(const string16& query) const { |
// L"/\x4f60\x597d\x4f60\x597d", |
// "/%E4%BD%A0%E5%A5%BD%E4%BD%A0%E5%A5%BD" |
std::string languages; |
- TabContents* tab = GetTabContents(); |
- if (tab) { |
- languages = content::GetContentClient()->browser()->GetAcceptLangs( |
- tab->browser_context()); |
- } |
+ languages = content::GetContentClient()->browser()->GetAcceptLangs( |
+ BrowserContext()); |
string16 url_formatted(net::FormatUrl(GetURL(), languages)); |
if (base::i18n::StringSearchIgnoringCaseAndAccents(query, url_formatted)) |
return true; |
@@ -706,6 +729,10 @@ TabContents* DownloadItemImpl::GetTabContents() const { |
return NULL; |
} |
+content::BrowserContext* DownloadItemImpl::BrowserContext() const { |
+ return delegate_->BrowserContext(); |
+} |
+ |
FilePath DownloadItemImpl::GetTargetFilePath() const { |
return full_path_.DirName().Append(state_info_.target_name); |
} |
@@ -728,9 +755,10 @@ void DownloadItemImpl::OffThreadCancel(DownloadFileManager* file_manager) { |
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
request_handle_->CancelRequest(); |
- BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
- base::Bind(&DownloadFileManager::CancelDownload, |
- file_manager, GetGlobalId())); |
+ BrowserThread::PostTask( |
+ BrowserThread::FILE, FROM_HERE, |
+ base::Bind(&DownloadFileManager::CancelDownload, |
+ file_manager, download_id_)); |
} |
void DownloadItemImpl::Init(bool active) { |
@@ -865,9 +893,6 @@ base::Time DownloadItemImpl::GetStartTime() const { return start_time_; } |
base::Time DownloadItemImpl::GetEndTime() const { return end_time_; } |
void DownloadItemImpl::SetDbHandle(int64 handle) { db_handle_ = handle; } |
int64 DownloadItemImpl::GetDbHandle() const { return db_handle_; } |
-DownloadManager* DownloadItemImpl::GetDownloadManager() { |
- return download_manager_; |
-} |
bool DownloadItemImpl::IsPaused() const { return is_paused_; } |
bool DownloadItemImpl::GetOpenWhenComplete() const { |
return open_when_complete_; |