This source file includes following definitions.
- DeleteDownloadedFile
- DeleteDownloadedFileDone
- DownloadFileDetach
- DownloadFileCancel
- IsDownloadResumptionEnabled
- weak_ptr_factory_
- weak_ptr_factory_
- weak_ptr_factory_
- AddObserver
- RemoveObserver
- UpdateObservers
- ValidateDangerousDownload
- StealDangerousDownload
- Pause
- Resume
- Cancel
- Remove
- OpenDownload
- ShowDownloadInShell
- GetId
- GetState
- GetLastReason
- IsPaused
- IsTemporary
- CanResume
- IsDone
- GetURL
- GetUrlChain
- GetOriginalUrl
- GetReferrerUrl
- GetTabUrl
- GetTabReferrerUrl
- GetSuggestedFilename
- GetContentDisposition
- GetMimeType
- GetOriginalMimeType
- GetRemoteAddress
- HasUserGesture
- GetTransitionType
- GetLastModifiedTime
- GetETag
- IsSavePackageDownload
- GetFullPath
- GetTargetFilePath
- GetForcedFilePath
- GetFileNameToReportUser
- GetTargetDisposition
- GetHash
- GetHashState
- GetFileExternallyRemoved
- DeleteFile
- IsDangerous
- GetDangerType
- TimeRemaining
- CurrentSpeed
- PercentComplete
- AllDataSaved
- GetTotalBytes
- GetReceivedBytes
- GetStartTime
- GetEndTime
- CanShowInFolder
- CanOpenDownload
- ShouldOpenFileBasedOnExtension
- GetOpenWhenComplete
- GetAutoOpened
- GetOpened
- GetBrowserContext
- GetWebContents
- OnContentCheckCompleted
- SetOpenWhenComplete
- SetIsTemporary
- SetOpened
- SetDisplayName
- DebugString
- GetResumeMode
- MergeOriginInfoOnResume
- NotifyRemoved
- OnDownloadedFileRemoved
- DestinationObserverAsWeakPtr
- GetBoundNetLog
- SetTotalBytes
- OnAllDataSaved
- MarkAsComplete
- DestinationUpdate
- DestinationError
- DestinationCompleted
- Init
- Start
- OnDownloadFileInitialized
- OnDownloadTargetDetermined
- OnDownloadRenamedToIntermediateName
- MaybeCompleteDownload
- OnDownloadCompleting
- OnDownloadRenamedToFinalName
- DelayedDownloadOpened
- Completed
- OnResumeRequestStarted
- Interrupt
- ReleaseDownloadFile
- IsDownloadReadyForCompletion
- TransitionTo
- SetDangerType
- SetFullPath
- AutoResumeIfValid
- ResumeInterruptedDownload
- InternalToExternalState
- ExternalToInternalState
- DebugDownloadStateString
- DebugResumeModeString
#include "content/browser/download/download_item_impl.h"
#include <vector>
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/format_macros.h"
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/stl_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "content/browser/download/download_create_info.h"
#include "content/browser/download/download_file.h"
#include "content/browser/download/download_interrupt_reasons_impl.h"
#include "content/browser/download/download_item_impl_delegate.h"
#include "content/browser/download/download_request_handle.h"
#include "content/browser/download/download_stats.h"
#include "content/browser/renderer_host/render_view_host_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/download_danger_type.h"
#include "content/public/browser/download_interrupt_reasons.h"
#include "content/public/browser/download_url_parameters.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/referrer.h"
#include "net/base/net_util.h"
namespace content {
namespace {
bool DeleteDownloadedFile(const base::FilePath& path) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
if (base::DirectoryExists(path))
return true;
return base::DeleteFile(path, false);
}
void DeleteDownloadedFileDone(
base::WeakPtr<DownloadItemImpl> item,
const base::Callback<void(bool)>& callback,
bool success) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (success && item.get())
item->OnDownloadedFileRemoved();
callback.Run(success);
}
static base::FilePath DownloadFileDetach(
scoped_ptr<DownloadFile> download_file) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
base::FilePath full_path = download_file->FullPath();
download_file->Detach();
return full_path;
}
static void DownloadFileCancel(scoped_ptr<DownloadFile> download_file) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
download_file->Cancel();
}
bool IsDownloadResumptionEnabled() {
return CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEnableDownloadResumption);
}
}
const uint32 DownloadItem::kInvalidId = 0;
const char DownloadItem::kEmptyFileHash[] = "";
const int DownloadItemImpl::kMaxAutoResumeAttempts = 5;
DownloadItemImpl::DownloadItemImpl(DownloadItemImplDelegate* delegate,
uint32 download_id,
const base::FilePath& current_path,
const base::FilePath& target_path,
const std::vector<GURL>& url_chain,
const GURL& referrer_url,
const base::Time& start_time,
const base::Time& end_time,
const std::string& etag,
const std::string& last_modified,
int64 received_bytes,
int64 total_bytes,
DownloadItem::DownloadState state,
DownloadDangerType danger_type,
DownloadInterruptReason interrupt_reason,
bool opened,
const net::BoundNetLog& bound_net_log)
: is_save_package_download_(false),
download_id_(download_id),
current_path_(current_path),
target_path_(target_path),
target_disposition_(TARGET_DISPOSITION_OVERWRITE),
url_chain_(url_chain),
referrer_url_(referrer_url),
transition_type_(PAGE_TRANSITION_LINK),
has_user_gesture_(false),
total_bytes_(total_bytes),
received_bytes_(received_bytes),
bytes_per_sec_(0),
last_modified_time_(last_modified),
etag_(etag),
last_reason_(interrupt_reason),
start_tick_(base::TimeTicks()),
state_(ExternalToInternalState(state)),
danger_type_(danger_type),
start_time_(start_time),
end_time_(end_time),
delegate_(delegate),
is_paused_(false),
auto_resume_count_(0),
open_when_complete_(false),
file_externally_removed_(false),
auto_opened_(false),
is_temporary_(false),
all_data_saved_(state == COMPLETE),
destination_error_(content::DOWNLOAD_INTERRUPT_REASON_NONE),
opened_(opened),
delegate_delayed_complete_(false),
bound_net_log_(bound_net_log),
weak_ptr_factory_(this) {
delegate_->Attach();
DCHECK_NE(IN_PROGRESS_INTERNAL, state_);
Init(false , SRC_HISTORY_IMPORT);
}
DownloadItemImpl::DownloadItemImpl(
DownloadItemImplDelegate* delegate,
uint32 download_id,
const DownloadCreateInfo& info,
const net::BoundNetLog& bound_net_log)
: is_save_package_download_(false),
download_id_(download_id),
target_disposition_(
(info.save_info->prompt_for_save_location) ?
TARGET_DISPOSITION_PROMPT : TARGET_DISPOSITION_OVERWRITE),
url_chain_(info.url_chain),
referrer_url_(info.referrer_url),
tab_url_(info.tab_url),
tab_referrer_url_(info.tab_referrer_url),
suggested_filename_(base::UTF16ToUTF8(info.save_info->suggested_name)),
forced_file_path_(info.save_info->file_path),
transition_type_(info.transition_type),
has_user_gesture_(info.has_user_gesture),
content_disposition_(info.content_disposition),
mime_type_(info.mime_type),
original_mime_type_(info.original_mime_type),
remote_address_(info.remote_address),
total_bytes_(info.total_bytes),
received_bytes_(0),
bytes_per_sec_(0),
last_modified_time_(info.last_modified),
etag_(info.etag),
last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE),
start_tick_(base::TimeTicks::Now()),
state_(IN_PROGRESS_INTERNAL),
danger_type_(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS),
start_time_(info.start_time),
delegate_(delegate),
is_paused_(false),
auto_resume_count_(0),
open_when_complete_(false),
file_externally_removed_(false),
auto_opened_(false),
is_temporary_(!info.save_info->file_path.empty()),
all_data_saved_(false),
destination_error_(content::DOWNLOAD_INTERRUPT_REASON_NONE),
opened_(false),
delegate_delayed_complete_(false),
bound_net_log_(bound_net_log),
weak_ptr_factory_(this) {
delegate_->Attach();
Init(true , SRC_ACTIVE_DOWNLOAD);
bound_net_log_.AddEvent(
net::NetLog::TYPE_DOWNLOAD_URL_REQUEST,
info.request_bound_net_log.source().ToEventParametersCallback());
info.request_bound_net_log.AddEvent(
net::NetLog::TYPE_DOWNLOAD_STARTED,
bound_net_log_.source().ToEventParametersCallback());
}
DownloadItemImpl::DownloadItemImpl(
DownloadItemImplDelegate* delegate,
uint32 download_id,
const base::FilePath& path,
const GURL& url,
const std::string& mime_type,
scoped_ptr<DownloadRequestHandleInterface> request_handle,
const net::BoundNetLog& bound_net_log)
: is_save_package_download_(true),
request_handle_(request_handle.Pass()),
download_id_(download_id),
current_path_(path),
target_path_(path),
target_disposition_(TARGET_DISPOSITION_OVERWRITE),
url_chain_(1, url),
referrer_url_(GURL()),
transition_type_(PAGE_TRANSITION_LINK),
has_user_gesture_(false),
mime_type_(mime_type),
original_mime_type_(mime_type),
total_bytes_(0),
received_bytes_(0),
bytes_per_sec_(0),
last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE),
start_tick_(base::TimeTicks::Now()),
state_(IN_PROGRESS_INTERNAL),
danger_type_(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS),
start_time_(base::Time::Now()),
delegate_(delegate),
is_paused_(false),
auto_resume_count_(0),
open_when_complete_(false),
file_externally_removed_(false),
auto_opened_(false),
is_temporary_(false),
all_data_saved_(false),
destination_error_(content::DOWNLOAD_INTERRUPT_REASON_NONE),
opened_(false),
delegate_delayed_complete_(false),
bound_net_log_(bound_net_log),
weak_ptr_factory_(this) {
delegate_->Attach();
Init(true , SRC_SAVE_PAGE_AS);
}
DownloadItemImpl::~DownloadItemImpl() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!download_file_.get());
FOR_EACH_OBSERVER(Observer, observers_, OnDownloadDestroyed(this));
delegate_->AssertStateConsistent(this);
delegate_->Detach();
}
void DownloadItemImpl::AddObserver(Observer* observer) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
observers_.AddObserver(observer);
}
void DownloadItemImpl::RemoveObserver(Observer* observer) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
observers_.RemoveObserver(observer);
}
void DownloadItemImpl::UpdateObservers() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
FOR_EACH_OBSERVER(Observer, observers_, OnDownloadUpdated(this));
}
void DownloadItemImpl::ValidateDangerousDownload() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!IsDone());
DCHECK(IsDangerous());
VLOG(20) << __FUNCTION__ << " download=" << DebugString(true);
if (IsDone() || !IsDangerous())
return;
RecordDangerousDownloadAccept(GetDangerType(),
GetTargetFilePath());
danger_type_ = DOWNLOAD_DANGER_TYPE_USER_VALIDATED;
bound_net_log_.AddEvent(
net::NetLog::TYPE_DOWNLOAD_ITEM_SAFETY_STATE_UPDATED,
base::Bind(&ItemCheckedNetLogCallback, GetDangerType()));
UpdateObservers();
MaybeCompleteDownload();
}
void DownloadItemImpl::StealDangerousDownload(
const AcquireFileCallback& callback) {
VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true);
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(IsDangerous());
if (download_file_) {
BrowserThread::PostTaskAndReplyWithResult(
BrowserThread::FILE,
FROM_HERE,
base::Bind(&DownloadFileDetach, base::Passed(&download_file_)),
callback);
} else {
callback.Run(current_path_);
}
current_path_.clear();
Remove();
}
void DownloadItemImpl::Pause() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (state_ != IN_PROGRESS_INTERNAL || is_paused_)
return;
request_handle_->PauseRequest();
is_paused_ = true;
UpdateObservers();
}
void DownloadItemImpl::Resume() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
switch (state_) {
case IN_PROGRESS_INTERNAL:
if (!is_paused_)
return;
request_handle_->ResumeRequest();
is_paused_ = false;
UpdateObservers();
return;
case COMPLETING_INTERNAL:
case COMPLETE_INTERNAL:
case CANCELLED_INTERNAL:
case RESUMING_INTERNAL:
return;
case INTERRUPTED_INTERNAL:
auto_resume_count_ = 0;
ResumeInterruptedDownload();
return;
case MAX_DOWNLOAD_INTERNAL_STATE:
NOTREACHED();
}
}
void DownloadItemImpl::Cancel(bool user_cancel) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true);
if (state_ != IN_PROGRESS_INTERNAL &&
state_ != INTERRUPTED_INTERNAL &&
state_ != RESUMING_INTERNAL) {
return;
}
if (IsDangerous()) {
RecordDangerousDownloadDiscard(
user_cancel ? DOWNLOAD_DISCARD_DUE_TO_USER_ACTION
: DOWNLOAD_DISCARD_DUE_TO_SHUTDOWN,
GetDangerType(),
GetTargetFilePath());
}
last_reason_ = user_cancel ? DOWNLOAD_INTERRUPT_REASON_USER_CANCELED
: DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN;
RecordDownloadCount(CANCELLED_COUNT);
if (!is_save_package_download_ && download_file_)
ReleaseDownloadFile(true);
if (state_ == IN_PROGRESS_INTERNAL) {
request_handle_->CancelRequest();
}
if ((state_ == INTERRUPTED_INTERNAL || state_ == RESUMING_INTERNAL) &&
!current_path_.empty()) {
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE,
base::Bind(base::IgnoreResult(&DeleteDownloadedFile), current_path_));
current_path_.clear();
}
TransitionTo(CANCELLED_INTERNAL, UPDATE_OBSERVERS);
}
void DownloadItemImpl::Remove() {
VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true);
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
delegate_->AssertStateConsistent(this);
Cancel(true);
delegate_->AssertStateConsistent(this);
NotifyRemoved();
delegate_->DownloadRemoved(this);
}
void DownloadItemImpl::OpenDownload() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (!IsDone()) {
if (!IsTemporary())
open_when_complete_ = !open_when_complete_;
return;
}
if (state_ != COMPLETE_INTERNAL || file_externally_removed_)
return;
delegate_->CheckForFileRemoval(this);
RecordOpen(GetEndTime(), !GetOpened());
opened_ = true;
FOR_EACH_OBSERVER(Observer, observers_, OnDownloadOpened(this));
delegate_->OpenDownload(this);
}
void DownloadItemImpl::ShowDownloadInShell() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
delegate_->ShowDownloadInShell(this);
}
uint32 DownloadItemImpl::GetId() const {
return download_id_;
}
DownloadItem::DownloadState DownloadItemImpl::GetState() const {
return InternalToExternalState(state_);
}
DownloadInterruptReason DownloadItemImpl::GetLastReason() const {
return last_reason_;
}
bool DownloadItemImpl::IsPaused() const {
return is_paused_;
}
bool DownloadItemImpl::IsTemporary() const {
return is_temporary_;
}
bool DownloadItemImpl::CanResume() const {
if ((GetState() == IN_PROGRESS) && IsPaused())
return true;
if (state_ != INTERRUPTED_INTERNAL)
return false;
if (!GetWebContents())
return false;
ResumeMode resume_mode = GetResumeMode();
return IsDownloadResumptionEnabled() &&
(resume_mode == RESUME_MODE_USER_RESTART ||
resume_mode == RESUME_MODE_USER_CONTINUE);
}
bool DownloadItemImpl::IsDone() const {
switch (state_) {
case IN_PROGRESS_INTERNAL:
case COMPLETING_INTERNAL:
return false;
case COMPLETE_INTERNAL:
case CANCELLED_INTERNAL:
return true;
case INTERRUPTED_INTERNAL:
return !CanResume();
case RESUMING_INTERNAL:
return false;
case MAX_DOWNLOAD_INTERNAL_STATE:
break;
}
NOTREACHED();
return true;
}
const GURL& DownloadItemImpl::GetURL() const {
return url_chain_.empty() ? GURL::EmptyGURL() : url_chain_.back();
}
const std::vector<GURL>& DownloadItemImpl::GetUrlChain() const {
return url_chain_;
}
const GURL& DownloadItemImpl::GetOriginalUrl() const {
return url_chain_.empty() ? GURL::EmptyGURL() : url_chain_.front();
}
const GURL& DownloadItemImpl::GetReferrerUrl() const {
return referrer_url_;
}
const GURL& DownloadItemImpl::GetTabUrl() const {
return tab_url_;
}
const GURL& DownloadItemImpl::GetTabReferrerUrl() const {
return tab_referrer_url_;
}
std::string DownloadItemImpl::GetSuggestedFilename() const {
return suggested_filename_;
}
std::string DownloadItemImpl::GetContentDisposition() const {
return content_disposition_;
}
std::string DownloadItemImpl::GetMimeType() const {
return mime_type_;
}
std::string DownloadItemImpl::GetOriginalMimeType() const {
return original_mime_type_;
}
std::string DownloadItemImpl::GetRemoteAddress() const {
return remote_address_;
}
bool DownloadItemImpl::HasUserGesture() const {
return has_user_gesture_;
};
PageTransition DownloadItemImpl::GetTransitionType() const {
return transition_type_;
};
const std::string& DownloadItemImpl::GetLastModifiedTime() const {
return last_modified_time_;
}
const std::string& DownloadItemImpl::GetETag() const {
return etag_;
}
bool DownloadItemImpl::IsSavePackageDownload() const {
return is_save_package_download_;
}
const base::FilePath& DownloadItemImpl::GetFullPath() const {
return current_path_;
}
const base::FilePath& DownloadItemImpl::GetTargetFilePath() const {
return target_path_;
}
const base::FilePath& DownloadItemImpl::GetForcedFilePath() const {
return forced_file_path_;
}
base::FilePath DownloadItemImpl::GetFileNameToReportUser() const {
if (!display_name_.empty())
return display_name_;
return target_path_.BaseName();
}
DownloadItem::TargetDisposition DownloadItemImpl::GetTargetDisposition() const {
return target_disposition_;
}
const std::string& DownloadItemImpl::GetHash() const {
return hash_;
}
const std::string& DownloadItemImpl::GetHashState() const {
return hash_state_;
}
bool DownloadItemImpl::GetFileExternallyRemoved() const {
return file_externally_removed_;
}
void DownloadItemImpl::DeleteFile(const base::Callback<void(bool)>& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (GetState() != DownloadItem::COMPLETE) {
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&DeleteDownloadedFileDone,
base::WeakPtr<DownloadItemImpl>(), callback, false));
return;
}
if (current_path_.empty() || file_externally_removed_) {
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&DeleteDownloadedFileDone,
base::WeakPtr<DownloadItemImpl>(), callback, true));
return;
}
BrowserThread::PostTaskAndReplyWithResult(
BrowserThread::FILE, FROM_HERE,
base::Bind(&DeleteDownloadedFile, current_path_),
base::Bind(&DeleteDownloadedFileDone,
weak_ptr_factory_.GetWeakPtr(), callback));
}
bool DownloadItemImpl::IsDangerous() const {
#if defined(OS_WIN)
return (danger_type_ == DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE ||
danger_type_ == DOWNLOAD_DANGER_TYPE_DANGEROUS_URL ||
danger_type_ == DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT ||
danger_type_ == DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT ||
danger_type_ == DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST ||
danger_type_ == DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED);
#else
return (danger_type_ == DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE ||
danger_type_ == DOWNLOAD_DANGER_TYPE_DANGEROUS_URL);
#endif
}
DownloadDangerType DownloadItemImpl::GetDangerType() const {
return danger_type_;
}
bool DownloadItemImpl::TimeRemaining(base::TimeDelta* remaining) const {
if (total_bytes_ <= 0)
return false;
int64 speed = CurrentSpeed();
if (speed == 0)
return false;
*remaining = base::TimeDelta::FromSeconds(
(total_bytes_ - received_bytes_) / speed);
return true;
}
int64 DownloadItemImpl::CurrentSpeed() const {
if (is_paused_)
return 0;
return bytes_per_sec_;
}
int DownloadItemImpl::PercentComplete() const {
if (delegate_delayed_complete_ || total_bytes_ <= 0)
return -1;
return static_cast<int>(received_bytes_ * 100.0 / total_bytes_);
}
bool DownloadItemImpl::AllDataSaved() const {
return all_data_saved_;
}
int64 DownloadItemImpl::GetTotalBytes() const {
return total_bytes_;
}
int64 DownloadItemImpl::GetReceivedBytes() const {
return received_bytes_;
}
base::Time DownloadItemImpl::GetStartTime() const {
return start_time_;
}
base::Time DownloadItemImpl::GetEndTime() const {
return end_time_;
}
bool DownloadItemImpl::CanShowInFolder() {
return CanOpenDownload() && !GetFullPath().empty();
}
bool DownloadItemImpl::CanOpenDownload() {
const bool is_complete = GetState() == DownloadItem::COMPLETE;
return (!IsDone() || is_complete) && !IsTemporary() &&
!file_externally_removed_;
}
bool DownloadItemImpl::ShouldOpenFileBasedOnExtension() {
return delegate_->ShouldOpenFileBasedOnExtension(GetTargetFilePath());
}
bool DownloadItemImpl::GetOpenWhenComplete() const {
return open_when_complete_;
}
bool DownloadItemImpl::GetAutoOpened() {
return auto_opened_;
}
bool DownloadItemImpl::GetOpened() const {
return opened_;
}
BrowserContext* DownloadItemImpl::GetBrowserContext() const {
return delegate_->GetBrowserContext();
}
WebContents* DownloadItemImpl::GetWebContents() const {
if (request_handle_)
return request_handle_->GetWebContents();
return NULL;
}
void DownloadItemImpl::OnContentCheckCompleted(DownloadDangerType danger_type) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(AllDataSaved());
VLOG(20) << __FUNCTION__ << " danger_type=" << danger_type
<< " download=" << DebugString(true);
SetDangerType(danger_type);
UpdateObservers();
}
void DownloadItemImpl::SetOpenWhenComplete(bool open) {
open_when_complete_ = open;
}
void DownloadItemImpl::SetIsTemporary(bool temporary) {
is_temporary_ = temporary;
}
void DownloadItemImpl::SetOpened(bool opened) {
opened_ = opened;
}
void DownloadItemImpl::SetDisplayName(const base::FilePath& name) {
display_name_ = name;
}
std::string DownloadItemImpl::DebugString(bool verbose) const {
std::string description =
base::StringPrintf("{ id = %d"
" state = %s",
download_id_,
DebugDownloadStateString(state_));
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).is_valid() ? (*iter).spec() : "<invalid>";
++iter;
for ( ; verbose && (iter != last); ++iter) {
url_list += " ->\n\t";
const GURL& next_url = *iter;
url_list += next_url.is_valid() ? next_url.spec() : "<invalid>";
}
}
if (verbose) {
description += base::StringPrintf(
" total = %" PRId64
" received = %" PRId64
" reason = %s"
" paused = %c"
" resume_mode = %s"
" auto_resume_count = %d"
" danger = %d"
" all_data_saved = %c"
" last_modified = '%s'"
" etag = '%s'"
" has_download_file = %s"
" url_chain = \n\t\"%s\"\n\t"
" full_path = \"%" PRFilePath "\"\n\t"
" target_path = \"%" PRFilePath "\"",
GetTotalBytes(),
GetReceivedBytes(),
DownloadInterruptReasonToString(last_reason_).c_str(),
IsPaused() ? 'T' : 'F',
DebugResumeModeString(GetResumeMode()),
auto_resume_count_,
GetDangerType(),
AllDataSaved() ? 'T' : 'F',
GetLastModifiedTime().c_str(),
GetETag().c_str(),
download_file_.get() ? "true" : "false",
url_list.c_str(),
GetFullPath().value().c_str(),
GetTargetFilePath().value().c_str());
} else {
description += base::StringPrintf(" url = \"%s\"", url_list.c_str());
}
description += " }";
return description;
}
DownloadItemImpl::ResumeMode DownloadItemImpl::GetResumeMode() const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
const bool force_restart =
(current_path_.empty() || (etag_.empty() && last_modified_time_.empty()));
const bool force_user =
(auto_resume_count_ >= kMaxAutoResumeAttempts || is_paused_);
ResumeMode mode = RESUME_MODE_INVALID;
switch(last_reason_) {
case DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR:
case DOWNLOAD_INTERRUPT_REASON_NETWORK_TIMEOUT:
if (force_restart && force_user)
mode = RESUME_MODE_USER_RESTART;
else if (force_restart)
mode = RESUME_MODE_IMMEDIATE_RESTART;
else if (force_user)
mode = RESUME_MODE_USER_CONTINUE;
else
mode = RESUME_MODE_IMMEDIATE_CONTINUE;
break;
case DOWNLOAD_INTERRUPT_REASON_SERVER_PRECONDITION:
case DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE:
case DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT:
if (force_user)
mode = RESUME_MODE_USER_RESTART;
else
mode = RESUME_MODE_IMMEDIATE_RESTART;
break;
case DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED:
case DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED:
case DOWNLOAD_INTERRUPT_REASON_NETWORK_SERVER_DOWN:
case DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST:
case DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED:
case DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN:
case DOWNLOAD_INTERRUPT_REASON_CRASH:
if (force_restart)
mode = RESUME_MODE_USER_RESTART;
else
mode = RESUME_MODE_USER_CONTINUE;
break;
case DOWNLOAD_INTERRUPT_REASON_FILE_FAILED:
case DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED:
case DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE:
case DOWNLOAD_INTERRUPT_REASON_FILE_NAME_TOO_LONG:
case DOWNLOAD_INTERRUPT_REASON_FILE_TOO_LARGE:
mode = RESUME_MODE_USER_RESTART;
break;
case DOWNLOAD_INTERRUPT_REASON_NONE:
case DOWNLOAD_INTERRUPT_REASON_FILE_VIRUS_INFECTED:
case DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT:
case DOWNLOAD_INTERRUPT_REASON_USER_CANCELED:
case DOWNLOAD_INTERRUPT_REASON_FILE_BLOCKED:
case DOWNLOAD_INTERRUPT_REASON_FILE_SECURITY_CHECK_FAILED:
mode = RESUME_MODE_INVALID;
break;
}
return mode;
}
void DownloadItemImpl::MergeOriginInfoOnResume(
const DownloadCreateInfo& new_create_info) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK_EQ(RESUMING_INTERNAL, state_);
DCHECK(!new_create_info.url_chain.empty());
std::vector<GURL>::const_iterator chain_iter =
new_create_info.url_chain.begin();
if (*chain_iter == url_chain_.back())
++chain_iter;
int origin_state = 0;
if (chain_iter != new_create_info.url_chain.end())
origin_state |= ORIGIN_STATE_ON_RESUMPTION_ADDITIONAL_REDIRECTS;
if (etag_ != new_create_info.etag ||
last_modified_time_ != new_create_info.last_modified)
origin_state |= ORIGIN_STATE_ON_RESUMPTION_VALIDATORS_CHANGED;
if (content_disposition_ != new_create_info.content_disposition)
origin_state |= ORIGIN_STATE_ON_RESUMPTION_CONTENT_DISPOSITION_CHANGED;
RecordOriginStateOnResumption(new_create_info.save_info->offset != 0,
origin_state);
url_chain_.insert(
url_chain_.end(), chain_iter, new_create_info.url_chain.end());
etag_ = new_create_info.etag;
last_modified_time_ = new_create_info.last_modified;
content_disposition_ = new_create_info.content_disposition;
}
void DownloadItemImpl::NotifyRemoved() {
FOR_EACH_OBSERVER(Observer, observers_, OnDownloadRemoved(this));
}
void DownloadItemImpl::OnDownloadedFileRemoved() {
file_externally_removed_ = true;
VLOG(20) << __FUNCTION__ << " download=" << DebugString(true);
UpdateObservers();
}
base::WeakPtr<DownloadDestinationObserver>
DownloadItemImpl::DestinationObserverAsWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
const net::BoundNetLog& DownloadItemImpl::GetBoundNetLog() const {
return bound_net_log_;
}
void DownloadItemImpl::SetTotalBytes(int64 total_bytes) {
total_bytes_ = total_bytes;
}
void DownloadItemImpl::OnAllDataSaved(const std::string& final_hash) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK_EQ(IN_PROGRESS_INTERNAL, state_);
DCHECK(!all_data_saved_);
all_data_saved_ = true;
VLOG(20) << __FUNCTION__ << " download=" << DebugString(true);
hash_ = final_hash;
hash_state_ = "";
UpdateObservers();
}
void DownloadItemImpl::MarkAsComplete() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(all_data_saved_);
end_time_ = base::Time::Now();
TransitionTo(COMPLETE_INTERNAL, UPDATE_OBSERVERS);
}
void DownloadItemImpl::DestinationUpdate(int64 bytes_so_far,
int64 bytes_per_sec,
const std::string& hash_state) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
VLOG(20) << __FUNCTION__ << " so_far=" << bytes_so_far
<< " per_sec=" << bytes_per_sec << " download=" << DebugString(true);
if (GetState() != IN_PROGRESS) {
return;
}
bytes_per_sec_ = bytes_per_sec;
hash_state_ = hash_state;
received_bytes_ = bytes_so_far;
if (received_bytes_ > total_bytes_)
total_bytes_ = 0;
if (bound_net_log_.IsLogging()) {
bound_net_log_.AddEvent(
net::NetLog::TYPE_DOWNLOAD_ITEM_UPDATED,
net::NetLog::Int64Callback("bytes_so_far", received_bytes_));
}
UpdateObservers();
}
void DownloadItemImpl::DestinationError(DownloadInterruptReason reason) {
if (current_path_.empty() || target_path_.empty())
destination_error_ = reason;
else
Interrupt(reason);
}
void DownloadItemImpl::DestinationCompleted(const std::string& final_hash) {
VLOG(20) << __FUNCTION__ << " download=" << DebugString(true);
if (GetState() != IN_PROGRESS)
return;
OnAllDataSaved(final_hash);
MaybeCompleteDownload();
}
void DownloadItemImpl::Init(bool active,
DownloadType download_type) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (active)
RecordDownloadCount(START_COUNT);
std::string file_name;
if (download_type == SRC_HISTORY_IMPORT) {
file_name = target_path_.AsUTF8Unsafe();
} else {
file_name = forced_file_path_.AsUTF8Unsafe();
if (file_name.empty())
file_name = suggested_filename_;
if (file_name.empty())
file_name = GetURL().ExtractFileName();
}
base::Callback<base::Value*(net::NetLog::LogLevel)> active_data = base::Bind(
&ItemActivatedNetLogCallback, this, download_type, &file_name);
if (active) {
bound_net_log_.BeginEvent(
net::NetLog::TYPE_DOWNLOAD_ITEM_ACTIVE, active_data);
} else {
bound_net_log_.AddEvent(
net::NetLog::TYPE_DOWNLOAD_ITEM_ACTIVE, active_data);
}
VLOG(20) << __FUNCTION__ << "() " << DebugString(true);
}
void DownloadItemImpl::Start(
scoped_ptr<DownloadFile> file,
scoped_ptr<DownloadRequestHandleInterface> req_handle) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!download_file_.get());
DCHECK(file.get());
DCHECK(req_handle.get());
download_file_ = file.Pass();
request_handle_ = req_handle.Pass();
if (GetState() == CANCELLED) {
ReleaseDownloadFile(true);
request_handle_->CancelRequest();
return;
}
TransitionTo(IN_PROGRESS_INTERNAL, UPDATE_OBSERVERS);
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE,
base::Bind(&DownloadFile::Initialize,
base::Unretained(download_file_.get()),
base::Bind(&DownloadItemImpl::OnDownloadFileInitialized,
weak_ptr_factory_.GetWeakPtr())));
}
void DownloadItemImpl::OnDownloadFileInitialized(
DownloadInterruptReason result) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (result != DOWNLOAD_INTERRUPT_REASON_NONE) {
Interrupt(result);
return;
}
delegate_->DetermineDownloadTarget(
this, base::Bind(&DownloadItemImpl::OnDownloadTargetDetermined,
weak_ptr_factory_.GetWeakPtr()));
}
void DownloadItemImpl::OnDownloadTargetDetermined(
const base::FilePath& target_path,
TargetDisposition disposition,
DownloadDangerType danger_type,
const base::FilePath& intermediate_path) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (target_path.empty()) {
Cancel(true);
return;
}
VLOG(20) << __FUNCTION__ << " " << target_path.value() << " " << disposition
<< " " << danger_type << " " << DebugString(true);
target_path_ = target_path;
target_disposition_ = disposition;
SetDangerType(danger_type);
DCHECK(intermediate_path.DirName() == target_path.DirName());
if (intermediate_path == current_path_) {
OnDownloadRenamedToIntermediateName(DOWNLOAD_INTERRUPT_REASON_NONE,
intermediate_path);
return;
}
DCHECK(!is_save_package_download_);
DCHECK(download_file_.get());
DownloadFile::RenameCompletionCallback callback =
base::Bind(&DownloadItemImpl::OnDownloadRenamedToIntermediateName,
weak_ptr_factory_.GetWeakPtr());
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE,
base::Bind(&DownloadFile::RenameAndUniquify,
base::Unretained(download_file_.get()),
intermediate_path, callback));
}
void DownloadItemImpl::OnDownloadRenamedToIntermediateName(
DownloadInterruptReason reason,
const base::FilePath& full_path) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
VLOG(20) << __FUNCTION__ << " download=" << DebugString(true);
if (DOWNLOAD_INTERRUPT_REASON_NONE != destination_error_) {
if (reason == DOWNLOAD_INTERRUPT_REASON_NONE)
SetFullPath(full_path);
Interrupt(destination_error_);
destination_error_ = DOWNLOAD_INTERRUPT_REASON_NONE;
} else if (DOWNLOAD_INTERRUPT_REASON_NONE != reason) {
Interrupt(reason);
DCHECK(current_path_.empty());
} else {
SetFullPath(full_path);
UpdateObservers();
MaybeCompleteDownload();
}
}
void DownloadItemImpl::MaybeCompleteDownload() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!is_save_package_download_);
if (!IsDownloadReadyForCompletion(
base::Bind(&DownloadItemImpl::MaybeCompleteDownload,
weak_ptr_factory_.GetWeakPtr())))
return;
DCHECK_EQ(IN_PROGRESS_INTERNAL, state_);
DCHECK(!IsDangerous());
DCHECK(all_data_saved_);
OnDownloadCompleting();
}
void DownloadItemImpl::OnDownloadCompleting() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (state_ != IN_PROGRESS_INTERNAL)
return;
VLOG(20) << __FUNCTION__ << "()"
<< " " << DebugString(true);
DCHECK(!GetTargetFilePath().empty());
DCHECK(!IsDangerous());
if (is_save_package_download_) {
Completed();
return;
}
DCHECK(download_file_.get());
DownloadFile::RenameCompletionCallback callback =
base::Bind(&DownloadItemImpl::OnDownloadRenamedToFinalName,
weak_ptr_factory_.GetWeakPtr());
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE,
base::Bind(&DownloadFile::RenameAndAnnotate,
base::Unretained(download_file_.get()),
GetTargetFilePath(), callback));
}
void DownloadItemImpl::OnDownloadRenamedToFinalName(
DownloadInterruptReason reason,
const base::FilePath& full_path) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!is_save_package_download_);
if (state_ != IN_PROGRESS_INTERNAL)
return;
VLOG(20) << __FUNCTION__ << "()"
<< " full_path = \"" << full_path.value() << "\""
<< " " << DebugString(false);
if (DOWNLOAD_INTERRUPT_REASON_NONE != reason) {
Interrupt(reason);
DCHECK(current_path_.empty());
return;
}
DCHECK(target_path_ == full_path);
if (full_path != current_path_) {
DCHECK(!full_path.empty());
SetFullPath(full_path);
}
DCHECK(download_file_.get());
ReleaseDownloadFile(false);
TransitionTo(COMPLETING_INTERNAL, DONT_UPDATE_OBSERVERS);
if (delegate_->ShouldOpenDownload(
this, base::Bind(&DownloadItemImpl::DelayedDownloadOpened,
weak_ptr_factory_.GetWeakPtr()))) {
Completed();
} else {
delegate_delayed_complete_ = true;
UpdateObservers();
}
}
void DownloadItemImpl::DelayedDownloadOpened(bool auto_opened) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
auto_opened_ = auto_opened;
Completed();
}
void DownloadItemImpl::Completed() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
VLOG(20) << __FUNCTION__ << "() " << DebugString(false);
DCHECK(all_data_saved_);
end_time_ = base::Time::Now();
TransitionTo(COMPLETE_INTERNAL, UPDATE_OBSERVERS);
RecordDownloadCompleted(start_tick_, received_bytes_);
if (auto_opened_) {
} else if (GetOpenWhenComplete() ||
ShouldOpenFileBasedOnExtension() ||
IsTemporary()) {
if (!IsTemporary())
OpenDownload();
auto_opened_ = true;
UpdateObservers();
}
}
void DownloadItemImpl::OnResumeRequestStarted(
DownloadItem* item,
DownloadInterruptReason interrupt_reason) {
if (item) {
DCHECK_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, interrupt_reason);
DCHECK_EQ(static_cast<DownloadItem*>(this), item);
return;
}
DCHECK_NE(DOWNLOAD_INTERRUPT_REASON_NONE, interrupt_reason);
Interrupt(interrupt_reason);
}
void DownloadItemImpl::Interrupt(DownloadInterruptReason reason) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK_NE(DOWNLOAD_INTERRUPT_REASON_NONE, reason);
if (state_ != IN_PROGRESS_INTERNAL && state_ != RESUMING_INTERNAL)
return;
last_reason_ = reason;
ResumeMode resume_mode = GetResumeMode();
if (state_ == IN_PROGRESS_INTERNAL) {
ReleaseDownloadFile(resume_mode == RESUME_MODE_IMMEDIATE_RESTART ||
resume_mode == RESUME_MODE_USER_RESTART ||
resume_mode == RESUME_MODE_INVALID ||
!IsDownloadResumptionEnabled());
request_handle_->CancelRequest();
} else {
DCHECK(!download_file_.get());
}
all_data_saved_ = false;
TransitionTo(INTERRUPTED_INTERNAL, DONT_UPDATE_OBSERVERS);
RecordDownloadInterrupted(reason, received_bytes_, total_bytes_);
if (!GetWebContents())
RecordDownloadCount(INTERRUPTED_WITHOUT_WEBCONTENTS);
AutoResumeIfValid();
UpdateObservers();
}
void DownloadItemImpl::ReleaseDownloadFile(bool destroy_file) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (destroy_file) {
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE,
base::Bind(&DownloadFileCancel, base::Passed(&download_file_)));
current_path_.clear();
} else {
BrowserThread::PostTask(
BrowserThread::FILE,
FROM_HERE,
base::Bind(base::IgnoreResult(&DownloadFileDetach),
base::Passed(&download_file_)));
}
weak_ptr_factory_.InvalidateWeakPtrs();
}
bool DownloadItemImpl::IsDownloadReadyForCompletion(
const base::Closure& state_change_notification) {
if (!AllDataSaved())
return false;
if (IsDangerous())
return false;
if (state_ != IN_PROGRESS_INTERNAL)
return false;
if (GetTargetFilePath().empty())
return false;
if (target_path_.DirName() != current_path_.DirName())
return false;
if (!delegate_->ShouldCompleteDownload(this, state_change_notification))
return false;
return true;
}
void DownloadItemImpl::TransitionTo(DownloadInternalState new_state,
ShouldUpdateObservers notify_action) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (state_ == new_state)
return;
DownloadInternalState old_state = state_;
state_ = new_state;
switch (state_) {
case COMPLETING_INTERNAL:
bound_net_log_.AddEvent(
net::NetLog::TYPE_DOWNLOAD_ITEM_COMPLETING,
base::Bind(&ItemCompletingNetLogCallback, received_bytes_, &hash_));
break;
case COMPLETE_INTERNAL:
bound_net_log_.AddEvent(
net::NetLog::TYPE_DOWNLOAD_ITEM_FINISHED,
base::Bind(&ItemFinishedNetLogCallback, auto_opened_));
break;
case INTERRUPTED_INTERNAL:
bound_net_log_.AddEvent(
net::NetLog::TYPE_DOWNLOAD_ITEM_INTERRUPTED,
base::Bind(&ItemInterruptedNetLogCallback, last_reason_,
received_bytes_, &hash_state_));
break;
case IN_PROGRESS_INTERNAL:
if (old_state == INTERRUPTED_INTERNAL) {
bound_net_log_.AddEvent(
net::NetLog::TYPE_DOWNLOAD_ITEM_RESUMED,
base::Bind(&ItemResumingNetLogCallback,
false, last_reason_, received_bytes_, &hash_state_));
}
break;
case CANCELLED_INTERNAL:
bound_net_log_.AddEvent(
net::NetLog::TYPE_DOWNLOAD_ITEM_CANCELED,
base::Bind(&ItemCanceledNetLogCallback, received_bytes_,
&hash_state_));
break;
default:
break;
}
VLOG(20) << " " << __FUNCTION__ << "()" << " this = " << DebugString(true)
<< " " << InternalToExternalState(old_state)
<< " " << InternalToExternalState(state_);
bool is_done = (state_ != IN_PROGRESS_INTERNAL &&
state_ != COMPLETING_INTERNAL);
bool was_done = (old_state != IN_PROGRESS_INTERNAL &&
old_state != COMPLETING_INTERNAL);
if (is_done && !was_done)
bound_net_log_.EndEvent(net::NetLog::TYPE_DOWNLOAD_ITEM_ACTIVE);
if (was_done && !is_done) {
std::string file_name(target_path_.BaseName().AsUTF8Unsafe());
bound_net_log_.BeginEvent(net::NetLog::TYPE_DOWNLOAD_ITEM_ACTIVE,
base::Bind(&ItemActivatedNetLogCallback,
this, SRC_ACTIVE_DOWNLOAD,
&file_name));
}
if (notify_action == UPDATE_OBSERVERS)
UpdateObservers();
}
void DownloadItemImpl::SetDangerType(DownloadDangerType danger_type) {
if (danger_type != danger_type_) {
bound_net_log_.AddEvent(
net::NetLog::TYPE_DOWNLOAD_ITEM_SAFETY_STATE_UPDATED,
base::Bind(&ItemCheckedNetLogCallback, danger_type));
}
if ((danger_type_ == DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS ||
danger_type_ == DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE ||
danger_type_ == DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT ||
danger_type_ == DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT) &&
(danger_type == DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST ||
danger_type == DOWNLOAD_DANGER_TYPE_DANGEROUS_URL ||
danger_type == DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT ||
danger_type == DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED)) {
RecordMaliciousDownloadClassified(danger_type);
}
danger_type_ = danger_type;
}
void DownloadItemImpl::SetFullPath(const base::FilePath& new_path) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
VLOG(20) << __FUNCTION__ << "()"
<< " new_path = \"" << new_path.value() << "\""
<< " " << DebugString(true);
DCHECK(!new_path.empty());
bound_net_log_.AddEvent(
net::NetLog::TYPE_DOWNLOAD_ITEM_RENAMED,
base::Bind(&ItemRenamedNetLogCallback, ¤t_path_, &new_path));
current_path_ = new_path;
}
void DownloadItemImpl::AutoResumeIfValid() {
DVLOG(20) << __FUNCTION__ << "() " << DebugString(true);
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
ResumeMode mode = GetResumeMode();
if (mode != RESUME_MODE_IMMEDIATE_RESTART &&
mode != RESUME_MODE_IMMEDIATE_CONTINUE) {
return;
}
auto_resume_count_++;
ResumeInterruptedDownload();
}
void DownloadItemImpl::ResumeInterruptedDownload() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
if (!command_line.HasSwitch(switches::kEnableDownloadResumption))
return;
if (state_ != INTERRUPTED_INTERNAL)
return;
if (!GetWebContents())
return;
ResumeMode mode = GetResumeMode();
if (mode == RESUME_MODE_IMMEDIATE_RESTART ||
mode == RESUME_MODE_USER_RESTART) {
received_bytes_ = 0;
hash_state_ = "";
last_modified_time_ = "";
etag_ = "";
}
scoped_ptr<DownloadUrlParameters> download_params(
DownloadUrlParameters::FromWebContents(GetWebContents(),
GetOriginalUrl()));
download_params->set_file_path(GetFullPath());
download_params->set_offset(GetReceivedBytes());
download_params->set_hash_state(GetHashState());
download_params->set_last_modified(GetLastModifiedTime());
download_params->set_etag(GetETag());
download_params->set_callback(
base::Bind(&DownloadItemImpl::OnResumeRequestStarted,
weak_ptr_factory_.GetWeakPtr()));
delegate_->ResumeInterruptedDownload(download_params.Pass(), GetId());
is_paused_ = false;
TransitionTo(RESUMING_INTERNAL, DONT_UPDATE_OBSERVERS);
}
DownloadItem::DownloadState DownloadItemImpl::InternalToExternalState(
DownloadInternalState internal_state) {
switch (internal_state) {
case IN_PROGRESS_INTERNAL:
return IN_PROGRESS;
case COMPLETING_INTERNAL:
return IN_PROGRESS;
case COMPLETE_INTERNAL:
return COMPLETE;
case CANCELLED_INTERNAL:
return CANCELLED;
case INTERRUPTED_INTERNAL:
return INTERRUPTED;
case RESUMING_INTERNAL:
return INTERRUPTED;
case MAX_DOWNLOAD_INTERNAL_STATE:
break;
}
NOTREACHED();
return MAX_DOWNLOAD_STATE;
}
DownloadItemImpl::DownloadInternalState
DownloadItemImpl::ExternalToInternalState(
DownloadState external_state) {
switch (external_state) {
case IN_PROGRESS:
return IN_PROGRESS_INTERNAL;
case COMPLETE:
return COMPLETE_INTERNAL;
case CANCELLED:
return CANCELLED_INTERNAL;
case INTERRUPTED:
return INTERRUPTED_INTERNAL;
default:
NOTREACHED();
}
return MAX_DOWNLOAD_INTERNAL_STATE;
}
const char* DownloadItemImpl::DebugDownloadStateString(
DownloadInternalState state) {
switch (state) {
case IN_PROGRESS_INTERNAL:
return "IN_PROGRESS";
case COMPLETING_INTERNAL:
return "COMPLETING";
case COMPLETE_INTERNAL:
return "COMPLETE";
case CANCELLED_INTERNAL:
return "CANCELLED";
case INTERRUPTED_INTERNAL:
return "INTERRUPTED";
case RESUMING_INTERNAL:
return "RESUMING";
case MAX_DOWNLOAD_INTERNAL_STATE:
break;
};
NOTREACHED() << "Unknown download state " << state;
return "unknown";
}
const char* DownloadItemImpl::DebugResumeModeString(ResumeMode mode) {
switch (mode) {
case RESUME_MODE_INVALID:
return "INVALID";
case RESUME_MODE_IMMEDIATE_CONTINUE:
return "IMMEDIATE_CONTINUE";
case RESUME_MODE_IMMEDIATE_RESTART:
return "IMMEDIATE_RESTART";
case RESUME_MODE_USER_CONTINUE:
return "USER_CONTINUE";
case RESUME_MODE_USER_RESTART:
return "USER_RESTART";
}
NOTREACHED() << "Unknown resume mode " << mode;
return "unknown";
}
}