1// Copyright (c) 2010 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#ifndef CHROME_BROWSER_DOWNLOAD_SAVE_ITEM_H__
6#define CHROME_BROWSER_DOWNLOAD_SAVE_ITEM_H__
7#pragma once
8
9#include "base/basictypes.h"
10#include "base/file_path.h"
11#include "chrome/browser/download/save_types.h"
12#include "googleurl/src/gurl.h"
13
14class SavePackage;
15
16// One SaveItem per save file. This is the model class that stores all the
17// state for one save file.
18class SaveItem {
19 public:
20  enum SaveState {
21    WAIT_START,
22    IN_PROGRESS,
23    COMPLETE,
24    CANCELED
25  };
26
27  SaveItem(const GURL& url,
28           const GURL& referrer,
29           SavePackage* package,
30           SaveFileCreateInfo::SaveFileSource save_source);
31
32  ~SaveItem();
33
34  void Start();
35
36  // Received a new chunk of data.
37  void Update(int64 bytes_so_far);
38
39  // Cancel saving item.
40  void Cancel();
41
42  // Saving operation completed.
43  void Finish(int64 size, bool is_success);
44
45  // Rough percent complete, -1 means we don't know (since we didn't receive a
46  // total size).
47  int PercentComplete() const;
48
49  // Update path for SaveItem, the actual file is renamed on the file thread.
50  void Rename(const FilePath& full_path);
51
52  void SetSaveId(int32 save_id);
53
54  void SetTotalBytes(int64 total_bytes);
55
56  // Accessors.
57  SaveState state() const { return state_; }
58  const FilePath& full_path() const { return full_path_; }
59  const FilePath& file_name() const { return file_name_; }
60  const GURL& url() const { return url_; }
61  const GURL& referrer() const { return referrer_; }
62  int64 total_bytes() const { return total_bytes_; }
63  int64 received_bytes() const { return received_bytes_; }
64  int32 save_id() const { return save_id_; }
65  bool has_final_name() const { return has_final_name_; }
66  bool success() const { return is_success_; }
67  SaveFileCreateInfo::SaveFileSource save_source() const {
68    return save_source_;
69  }
70  SavePackage* package() const { return package_; }
71
72 private:
73  // Internal helper for maintaining consistent received and total sizes.
74  void UpdateSize(int64 size);
75
76  // Request ID assigned by the ResourceDispatcherHost.
77  int32 save_id_;
78
79  // Full path to the save item file.
80  FilePath full_path_;
81
82  // Short display version of the file.
83  FilePath file_name_;
84
85  // The URL for this save item.
86  GURL url_;
87  GURL referrer_;
88
89  // Total bytes expected.
90  int64 total_bytes_;
91
92  // Current received bytes.
93  int64 received_bytes_;
94
95  // The current state of this save item.
96  SaveState state_;
97
98  // Specifies if this name is a final or not.
99  bool has_final_name_;
100
101  // Flag indicates whether SaveItem has error while in saving process.
102  bool is_success_;
103
104  SaveFileCreateInfo::SaveFileSource save_source_;
105
106  // Our owning object.
107  SavePackage* package_;
108
109  DISALLOW_COPY_AND_ASSIGN(SaveItem);
110};
111
112#endif  // CHROME_BROWSER_DOWNLOAD_SAVE_ITEM_H__
113