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/strings/string16.h"
13
14class SavePackage;
15
16namespace content {
17class DownloadItem;
18}
19
20namespace gfx {
21class FontList;
22}
23
24// This class is an abstraction for common UI tasks and properties associated
25// with a DownloadItem.
26//
27// It is intended to be used as a thin wrapper around a |DownloadItem*|. As
28// such, the caller is expected to ensure that the |download| passed into the
29// constructor outlives this |DownloadItemModel|. In addition, multiple
30// DownloadItemModel objects could be wrapping the same DownloadItem.
31class DownloadItemModel {
32 public:
33  // Constructs a DownloadItemModel. The caller must ensure that |download|
34  // outlives this object.
35  explicit DownloadItemModel(content::DownloadItem* download);
36  ~DownloadItemModel();
37
38  // Returns a long descriptive string for a download that's in the INTERRUPTED
39  // state. For other downloads, the returned string will be empty.
40  base::string16 GetInterruptReasonText() const;
41
42  // Returns a short one-line status string for the download.
43  base::string16 GetStatusText() const;
44
45  // Returns the localized status text for an in-progress download. This
46  // is the progress status used in the WebUI interface.
47  base::string16 GetTabProgressStatusText() const;
48
49  // Returns a string suitable for use as a tooltip. For a regular download, the
50  // tooltip is the filename. For an interrupted download, the string states the
51  // filename and a short description of the reason for interruption. For
52  // example:
53  //    Report.pdf
54  //    Network disconnected
55  // |font_list| and |max_width| are used to elide the filename and/or interrupt
56  // reason as necessary to keep the width of the tooltip text under
57  // |max_width|. The tooltip will be at most 2 lines.
58  base::string16 GetTooltipText(const gfx::FontList& font_list, int max_width) const;
59
60  // Get the warning text to display for a dangerous download. The |base_width|
61  // is the maximum width of an embedded filename (if there is one). The metrics
62  // for the filename will be based on |font_list|. Should only be called if
63  // IsDangerous() is true.
64  base::string16 GetWarningText(const gfx::FontList& font_list, int base_width) const;
65
66  // Get the caption text for a button for confirming a dangerous download
67  // warning.
68  base::string16 GetWarningConfirmButtonText() const;
69
70  // Get the number of bytes that has completed so far. Virtual for testing.
71  int64 GetCompletedBytes() const;
72
73  // Get the total number of bytes for this download. Should return 0 if the
74  // total size of the download is not known. Virual for testing.
75  int64 GetTotalBytes() const;
76
77  // Rough percent complete. Returns -1 if the progress is unknown.
78  int PercentComplete() const;
79
80  // Is this considered a dangerous download?
81  bool IsDangerous() const;
82
83  // Is this considered a malicious download? Implies IsDangerous().
84  bool MightBeMalicious() const;
85
86  // Is this considered a malicious download with very high confidence?
87  // Implies IsDangerous() and MightBeMalicious().
88  bool IsMalicious() const;
89
90  // Is safe browsing download feedback feature available for this download?
91  bool ShouldAllowDownloadFeedback() const;
92
93  // Returns |true| if this download is expected to complete successfully and
94  // thereafter be removed from the shelf.  Downloads that are opened
95  // automatically or are temporary will be removed from the shelf on successful
96  // completion.
97  //
98  // Returns |false| if the download is not expected to complete (interrupted,
99  // cancelled, dangerous, malicious), or won't be removed on completion.
100  //
101  // Since the expectation of successful completion may change, the return value
102  // of this function will change over the course of a download.
103  bool ShouldRemoveFromShelfWhenComplete() const;
104
105  // Returns |true| if the download started animation (big download arrow
106  // animates down towards the shelf) should be displayed for this download.
107  // Downloads that were initiated via "Save As" or are extension installs don't
108  // show the animation.
109  bool ShouldShowDownloadStartedAnimation() const;
110
111  // Returns |true| if this download should be displayed in the downloads shelf.
112  bool ShouldShowInShelf() const;
113
114  // Change whether the download should be displayed on the downloads
115  // shelf. Setting this is only effective if the download hasn't already been
116  // displayed in the shelf.
117  void SetShouldShowInShelf(bool should_show);
118
119  // Returns |true| if the UI should be notified when the download is ready to
120  // be presented in the UI. Note that this is indpendent of ShouldShowInShelf()
121  // since there might be actions other than showing in the shelf that the UI
122  // must perform.
123  bool ShouldNotifyUI() const;
124
125  // Returns |true| if the UI has been notified about this download. By default,
126  // this value is |false| and should be changed explicitly using
127  // SetWasUINotified().
128  bool WasUINotified() const;
129
130  // Change what's returned by WasUINotified().
131  void SetWasUINotified(bool should_notify);
132
133  // Returns |true| if opening in the browser is preferred for this download. If
134  // |false|, the download should be opened with the system default application.
135  bool ShouldPreferOpeningInBrowser() const;
136
137  // Change what's returned by ShouldPreferOpeningInBrowser to |preference|.
138  void SetShouldPreferOpeningInBrowser(bool preference);
139
140  // Mark that the download should be considered dangerous based on the file
141  // type. This value may differ from the download's danger type in cases where
142  // the SafeBrowsing service hasn't returned a verdict about the download. If
143  // SafeBrowsing fails to return a decision, then the download should be
144  // considered dangerous based on this flag. Defaults to false.
145  bool IsDangerousFileBasedOnType() const;
146
147  // Change what's returned by IsDangerousFileBasedOnType().
148  void SetIsDangerousFileBasedOnType(bool dangerous);
149
150  // Open the download using the platform handler for the download. The behavior
151  // of this method will be different from DownloadItem::OpenDownload() if
152  // ShouldPreferOpeningInBrowser().
153  void OpenUsingPlatformHandler();
154
155  content::DownloadItem* download() { return download_; }
156
157 private:
158  // Returns a string representations of the current download progress sizes. If
159  // the total size of the download is known, this string looks like: "100/200
160  // MB" where the numerator is the transferred size and the denominator is the
161  // total size. If the total isn't known, returns the transferred size as a
162  // string (e.g.: "100 MB").
163  base::string16 GetProgressSizesString() const;
164
165  // Returns a string indicating the status of an in-progress download.
166  base::string16 GetInProgressStatusString() const;
167
168  // The DownloadItem that this model represents. Note that DownloadItemModel
169  // itself shouldn't maintain any state since there can be more than one
170  // DownloadItemModel in use with the same DownloadItem.
171  content::DownloadItem* download_;
172
173  DISALLOW_COPY_AND_ASSIGN(DownloadItemModel);
174};
175
176#endif  // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_MODEL_H_
177