download_item_model.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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 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 string16 GetInterruptReasonText() const; 41 42 // Returns a short one-line status string for the download. 43 string16 GetStatusText() const; 44 45 // Returns a string suitable for use as a tooltip. For a regular download, the 46 // tooltip is the filename. For an interrupted download, the string states the 47 // filename and a short description of the reason for interruption. For 48 // example: 49 // Report.pdf 50 // Network disconnected 51 // |font| and |max_width| are used to elide the filename and/or interrupt 52 // reason as necessary to keep the width of the tooltip text under 53 // |max_width|. The tooltip will be at most 2 lines. 54 string16 GetTooltipText(const gfx::Font& font, int max_width) const; 55 56 // Get the warning text to display for a dangerous download. The |base_width| 57 // is the maximum width of an embedded filename (if there is one). The metrics 58 // for the filename will be based on |font|. Should only be called if 59 // IsDangerous() is true. 60 string16 GetWarningText(const gfx::Font& font, int base_width) const; 61 62 // Get the caption text for a button for confirming a dangerous download 63 // warning. 64 string16 GetWarningConfirmButtonText() const; 65 66 // Get the number of bytes that has completed so far. Virtual for testing. 67 int64 GetCompletedBytes() const; 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. Virual for testing. 71 int64 GetTotalBytes() const; 72 73 // Rough percent complete. Returns -1 if the progress is unknown. 74 int PercentComplete() const; 75 76 // Is this considered a dangerous download? 77 bool IsDangerous() const; 78 79 // Is this considered a malicious download? Implies IsDangerous(). 80 bool IsMalicious() const; 81 82 // Returns |true| if this download is expected to complete successfully and 83 // thereafter be removed from the shelf. Downloads that are opened 84 // automatically or are temporary will be removed from the shelf on successful 85 // completion. 86 // 87 // Returns |false| if the download is not expected to complete (interrupted, 88 // cancelled, dangerous, malicious), or won't be removed on completion. 89 // 90 // Since the expectation of successful completion may change, the return value 91 // of this function will change over the course of a download. 92 bool ShouldRemoveFromShelfWhenComplete() const; 93 94 // Returns |true| if the download started animation (big download arrow 95 // animates down towards the shelf) should be displayed for this download. 96 // Downloads that were initiated via "Save As" or are extension installs don't 97 // show the animation. 98 bool ShouldShowDownloadStartedAnimation() const; 99 100 // Returns |true| if this download should be displayed in the downloads shelf. 101 bool ShouldShowInShelf() const; 102 103 // Change whether the download should be displayed on the downloads 104 // shelf. Setting this is only effective if the download hasn't already been 105 // displayed in the shelf. 106 void SetShouldShowInShelf(bool should_show); 107 108 // Returns |true| if the UI should be notified when the download is ready to 109 // be presented in the UI. By default, this value is |false| and should be 110 // changed explicitly using SetShouldNotifyUI(). Note that this is indpendent 111 // of ShouldShowInShelf() since there might be actions other than showing in 112 // the shelf that the UI must perform. 113 bool ShouldNotifyUI() const; 114 115 // Change what's returned by ShouldNotifyUI(). 116 void SetShouldNotifyUI(bool should_notify); 117 118 content::DownloadItem* download() { return download_; } 119 120 private: 121 // Returns a string representations of the current download progress sizes. If 122 // the total size of the download is known, this string looks like: "100/200 123 // MB" where the numerator is the transferred size and the denominator is the 124 // total size. If the total isn't known, returns the transferred size as a 125 // string (e.g.: "100 MB"). 126 string16 GetProgressSizesString() const; 127 128 // Returns a string indicating the status of an in-progress download. 129 string16 GetInProgressStatusString() const; 130 131 // The DownloadItem that this model represents. Note that DownloadItemModel 132 // itself shouldn't maintain any state since there can be more than one 133 // DownloadItemModel in use with the same DownloadItem. 134 content::DownloadItem* download_; 135 136 DISALLOW_COPY_AND_ASSIGN(DownloadItemModel); 137}; 138 139#endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_MODEL_H_ 140