This source file includes following definitions.
- package_
- Start
- UpdateSize
- Update
- Cancel
- Finish
- PercentComplete
- Rename
- SetSaveId
- SetTotalBytes
#include "content/browser/download/save_item.h"
#include "base/logging.h"
#include "base/strings/string_util.h"
#include "content/browser/download/save_file.h"
#include "content/browser/download/save_file_manager.h"
#include "content/browser/download/save_package.h"
namespace content {
SaveItem::SaveItem(const GURL& url,
const Referrer& referrer,
SavePackage* package,
SaveFileCreateInfo::SaveFileSource save_source)
: save_id_(-1),
url_(url),
referrer_(referrer),
total_bytes_(0),
received_bytes_(0),
state_(WAIT_START),
has_final_name_(false),
is_success_(false),
save_source_(save_source),
package_(package) {
DCHECK(package);
}
SaveItem::~SaveItem() {
}
void SaveItem::Start() {
DCHECK(state_ == WAIT_START);
state_ = IN_PROGRESS;
}
void SaveItem::UpdateSize(int64 bytes_so_far) {
received_bytes_ = bytes_so_far;
if (received_bytes_ >= total_bytes_)
total_bytes_ = 0;
}
void SaveItem::Update(int64 bytes_so_far) {
if (state_ != IN_PROGRESS) {
NOTREACHED();
return;
}
UpdateSize(bytes_so_far);
}
void SaveItem::Cancel() {
if (state_ != IN_PROGRESS) {
return;
}
state_ = CANCELED;
is_success_ = false;
Finish(received_bytes_, false);
package_->SaveCanceled(this);
}
void SaveItem::Finish(int64 size, bool is_success) {
DCHECK(has_final_name() || (save_id_ == -1 && !is_success_) ||
(save_id_ != -1 && !is_success_ && !size));
state_ = COMPLETE;
is_success_ = is_success;
UpdateSize(size);
}
int SaveItem::PercentComplete() const {
switch (state_) {
case COMPLETE:
case CANCELED:
return 100;
case WAIT_START:
return 0;
case IN_PROGRESS: {
int percent = 0;
if (total_bytes_ > 0)
percent = static_cast<int>(received_bytes_ * 100.0 / total_bytes_);
return percent;
}
default: {
NOTREACHED();
return -1;
}
}
}
void SaveItem::Rename(const base::FilePath& full_path) {
DCHECK(!full_path.empty() && !has_final_name());
full_path_ = full_path;
file_name_ = full_path_.BaseName();
has_final_name_ = true;
}
void SaveItem::SetSaveId(int32 save_id) {
DCHECK_EQ(-1, save_id_);
save_id_ = save_id;
}
void SaveItem::SetTotalBytes(int64 total_bytes) {
DCHECK_EQ(0, total_bytes_);
total_bytes_ = total_bytes;
}
}