download_item_model.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
1// Copyright (c) 2012 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_DOWNLOAD_ITEM_MODEL_H_
6#define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_MODEL_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/compiler_specific.h"
12#include "base/string16.h"
13
14class SavePackage;
15
16namespace content {
17class DownloadItem;
18}
19
20namespace gfx {
21class Font;
22}
23
24// This class is an abstraction for common UI tasks associated with a download.
25class BaseDownloadItemModel {
26 public:
27  explicit BaseDownloadItemModel(content::DownloadItem* download)
28      : download_(download) { }
29  virtual ~BaseDownloadItemModel() { }
30
31  // Cancel the task corresponding to the item.
32  virtual void CancelTask() = 0;
33
34  // Returns a short one-line status string for the download.
35  virtual string16 GetStatusText() const = 0;
36
37  // Returns a string suitable for use as a tooltip. For a regular download, the
38  // tooltip is the filename. For an interrupted download, the string states the
39  // filename and a short description of the reason for interruption. For
40  // example:
41  //    Report.pdf
42  //    Network disconnected
43  // |font| and |max_width| are used to elide the filename and/or interrupt
44  // reason as necessary to keep the width of the tooltip text under
45  // |max_width|. The tooltip will be at most 2 lines.
46  virtual string16 GetTooltipText(const gfx::Font& font,
47                                  int max_width) const = 0;
48
49  // Rough percent complete. Returns -1 if the progress is unknown.
50  virtual int PercentComplete() const = 0;
51
52  // Get the warning text to display for a dangerous download. The |base_width|
53  // is the maximum width of an embedded filename (if there is one). The metrics
54  // for the filename will be based on |font|. Should only be called if
55  // IsDangerous() is true.
56  virtual string16 GetWarningText(const gfx::Font& font,
57                                  int base_width) const = 0;
58
59  // Get the caption text for a button for confirming a dangerous download
60  // warning.
61  virtual string16 GetWarningConfirmButtonText() const = 0;
62
63  // Is this considered a malicious download? Implies IsDangerous().
64  virtual bool IsMalicious() const = 0;
65
66  // Is this considered a dangerous download?
67  virtual bool IsDangerous() const = 0;
68
69  // Get the total number of bytes for this download. Should return 0 if the
70  // total size of the download is not known.
71  virtual int64 GetTotalBytes() const = 0;
72
73  // Get the number of bytes that has completed so far.
74  virtual int64 GetCompletedBytes() const = 0;
75
76  content::DownloadItem* download() { return download_; }
77
78  // Get the status message of the given interrupt |reason|.
79  static string16 InterruptReasonStatusMessage(int reason);
80
81  // Get the description of the given interrupt |reason|.
82  static string16 InterruptReasonMessage(int reason);
83
84 protected:
85  content::DownloadItem* download_;
86};
87
88// Concrete implementation of BaseDownloadItemModel.
89class DownloadItemModel : public BaseDownloadItemModel {
90 public:
91  explicit DownloadItemModel(content::DownloadItem* download);
92  virtual ~DownloadItemModel() { }
93
94  // BaseDownloadItemModel
95  virtual void CancelTask() OVERRIDE;
96  virtual string16 GetStatusText() const OVERRIDE;
97  virtual string16 GetTooltipText(const gfx::Font& font,
98                                  int max_width) const OVERRIDE;
99  virtual int PercentComplete() const OVERRIDE;
100  virtual string16 GetWarningText(const gfx::Font& font,
101                                  int base_width) const OVERRIDE;
102  virtual string16 GetWarningConfirmButtonText() const OVERRIDE;
103  virtual bool IsMalicious() const OVERRIDE;
104  virtual bool IsDangerous() const OVERRIDE;
105  virtual int64 GetTotalBytes() const OVERRIDE;
106  virtual int64 GetCompletedBytes() const OVERRIDE;
107
108 protected:
109  // Returns true if |download_| is a Drive dwonload. Protected virtual for
110  // testing.
111  virtual bool IsDriveDownload() const;
112
113 private:
114  // Returns a string representations of the current download progress sizes. If
115  // the total size of the download is known, this string looks like: "100/200
116  // MB" where the numerator is the transferred size and the denominator is the
117  // total size. If the total isn't known, returns the transferred size as a
118  // string (e.g.: "100 MB").
119  string16 GetProgressSizesString() const;
120
121  // Returns a string indicating the status of an in-progress download.
122  string16 GetInProgressStatusString() const;
123
124  DISALLOW_COPY_AND_ASSIGN(DownloadItemModel);
125};
126
127#endif  // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_MODEL_H_
128