1// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/download/save_item.h"
6
7#include "base/file_util.h"
8#include "base/logging.h"
9#include "base/string_util.h"
10#include "chrome/browser/download/save_file.h"
11#include "chrome/browser/download/save_file_manager.h"
12#include "chrome/browser/download/save_package.h"
13
14// Constructor for SaveItem when creating each saving job.
15SaveItem::SaveItem(const GURL& url,
16                   const GURL& referrer,
17                   SavePackage* package,
18                   SaveFileCreateInfo::SaveFileSource save_source)
19  : save_id_(-1),
20    url_(url),
21    referrer_(referrer),
22    total_bytes_(0),
23    received_bytes_(0),
24    state_(WAIT_START),
25    has_final_name_(false),
26    is_success_(false),
27    save_source_(save_source),
28    package_(package) {
29  DCHECK(package);
30}
31
32SaveItem::~SaveItem() {
33}
34
35// Set start state for save item.
36void SaveItem::Start() {
37  DCHECK(state_ == WAIT_START);
38  state_ = IN_PROGRESS;
39}
40
41// If we've received more data than we were expecting (bad server info?),
42// revert to 'unknown size mode'.
43void SaveItem::UpdateSize(int64 bytes_so_far) {
44  received_bytes_ = bytes_so_far;
45  if (received_bytes_ >= total_bytes_)
46    total_bytes_ = 0;
47}
48
49// Updates from the file thread may have been posted while this saving job
50// was being canceled in the UI thread, so we'll accept them unless we're
51// complete.
52void SaveItem::Update(int64 bytes_so_far) {
53  if (state_ != IN_PROGRESS) {
54    NOTREACHED();
55    return;
56  }
57  UpdateSize(bytes_so_far);
58}
59
60// Cancel this saving item job. If the job is not in progress, ignore
61// this command. The SavePackage will each in-progress SaveItem's cancel
62// when canceling whole saving page job.
63void SaveItem::Cancel() {
64  // If item is in WAIT_START mode, which means no request has been sent.
65  // So we need not to cancel it.
66  if (state_ != IN_PROGRESS) {
67    // Small downloads might be complete before method has a chance to run.
68    return;
69  }
70  state_ = CANCELED;
71  is_success_ = false;
72  Finish(received_bytes_, false);
73  package_->SaveCanceled(this);
74}
75
76// Set finish state for a save item
77void SaveItem::Finish(int64 size, bool is_success) {
78  // When this function is called, the SaveItem should be one of following
79  // three situations.
80  // a) The data of this SaveItem is finished saving. So it should have
81  // generated final name.
82  // b) Error happened before the start of saving process. So no |save_id_| is
83  // generated for this SaveItem and the |is_success_| should be false.
84  // c) Error happened in the start of saving process, the SaveItem has a save
85  // id, |is_success_| should be false, and the |size| should be 0.
86  DCHECK(has_final_name() || (save_id_ == -1 && !is_success_) ||
87         (save_id_ != -1 && !is_success_ && !size));
88  state_ = COMPLETE;
89  is_success_ = is_success;
90  UpdateSize(size);
91}
92
93// Calculate the percentage of the save item
94int SaveItem::PercentComplete() const {
95  switch (state_) {
96    case COMPLETE:
97    case CANCELED:
98      return 100;
99    case WAIT_START:
100      return 0;
101    case IN_PROGRESS: {
102      int percent = 0;
103      if (total_bytes_ > 0)
104        percent = static_cast<int>(received_bytes_ * 100.0 / total_bytes_);
105      return percent;
106    }
107    default: {
108      NOTREACHED();
109      return -1;
110    }
111  }
112}
113
114// Rename the save item with new path.
115void SaveItem::Rename(const FilePath& full_path) {
116  DCHECK(!full_path.empty() && !has_final_name());
117  full_path_ = full_path;
118  file_name_ = full_path_.BaseName();
119  has_final_name_ = true;
120}
121
122void SaveItem::SetSaveId(int32 save_id) {
123  DCHECK(save_id_ == -1);
124  save_id_ = save_id;
125}
126
127void SaveItem::SetTotalBytes(int64 total_bytes) {
128  DCHECK(total_bytes_ == 0);
129  total_bytes_ = total_bytes;
130}
131