• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SHELF_H_
6 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_SHELF_H_
7 
8 #include "base/memory/weak_ptr.h"
9 #include "base/time/time.h"
10 #include "build/build_config.h"
11 
12 class Browser;
13 
14 namespace gfx {
15 class Canvas;
16 class ImageSkia;
17 class Rect;
18 }
19 
20 namespace content {
21 class DownloadItem;
22 class DownloadManager;
23 }
24 
25 #if defined(TOOLKIT_VIEWS)
26 namespace views {
27 class View;
28 }
29 #endif
30 
31 // This is an abstract base class for platform specific download shelf
32 // implementations.
33 class DownloadShelf {
34  public:
35   // Reason for closing download shelf.
36   enum CloseReason {
37     // Closing the shelf automatically. E.g.: all remaining downloads in the
38     // shelf have been opened, last download in shelf was removed, or the
39     // browser is switching to full-screen mode.
40     AUTOMATIC,
41 
42     // Closing shelf due to a user selection. E.g.: the user clicked on the
43     // 'close' button on the download shelf, or the shelf is being closed as a
44     // side-effect of the user opening the downloads page.
45     USER_ACTION
46   };
47 
48   enum PaintDownloadProgressSize {
49     SMALL = 0,
50     BIG
51   };
52 
53   // Download progress animations ----------------------------------------------
54 
55   enum {
56     // Arc sweep angle for use with downloads of unknown size.
57     kUnknownAngleDegrees = 50,
58 
59     // Rate of progress for use with downloads of unknown size.
60     kUnknownIncrementDegrees = 12,
61 
62     // Start angle for downloads with known size (midnight position).
63     kStartAngleDegrees = -90,
64 
65     // A the maximum number of degrees of a circle.
66     kMaxDegrees = 360,
67 
68     // Progress animation timer period, in milliseconds.
69     kProgressRateMs = 150,
70 
71     // XP and Vista must support icons of this size.
72     kSmallIconSize = 16,
73     kBigIconSize = 32,
74 
75     kSmallProgressIconSize = 39,
76     kBigProgressIconSize = 52,
77 
78     kSmallProgressIconOffset = (kSmallProgressIconSize - kSmallIconSize) / 2
79   };
80 
81   DownloadShelf();
82   virtual ~DownloadShelf();
83 
84   // Our progress halo around the icon.
85   // Load a language dependent height so that the dangerous download
86   // confirmation message doesn't overlap with the download link label.
87   static int GetBigProgressIconSize();
88 
89   // The offset required to center the icon in the progress images.
90   static int GetBigProgressIconOffset();
91 
92   // Paint the common download animation progress foreground and background,
93   // clipping the foreground to 'percent' full. If percent is -1, then we don't
94   // know the total size, so we just draw a rotating segment until we're done.
95   //
96   // |containing_view| is the View subclass within which the progress animation
97   // is drawn (generally either DownloadItemTabView or DownloadItemView). We
98   // require the containing View in addition to the canvas because if we are
99   // drawing in a right-to-left locale, we need to mirror the position of the
100   // progress animation within the containing View.
101   static void PaintCustomDownloadProgress(
102       gfx::Canvas* canvas,
103       const gfx::ImageSkia& background_image,
104       const gfx::ImageSkia& foreground_image,
105       int image_size,
106       const gfx::Rect& bounds,
107       int start_angle,
108       int percent_done);
109 
110   static void PaintDownloadProgress(gfx::Canvas* canvas,
111 #if defined(TOOLKIT_VIEWS)
112                                     views::View* containing_view,
113 #endif
114                                     int origin_x,
115                                     int origin_y,
116                                     int start_angle,
117                                     int percent,
118                                     PaintDownloadProgressSize size);
119 
120   static void PaintDownloadComplete(gfx::Canvas* canvas,
121 #if defined(TOOLKIT_VIEWS)
122                                     views::View* containing_view,
123 #endif
124                                     int origin_x,
125                                     int origin_y,
126                                     double animation_progress,
127                                     PaintDownloadProgressSize size);
128 
129   static void PaintDownloadInterrupted(gfx::Canvas* canvas,
130 #if defined(TOOLKIT_VIEWS)
131                                        views::View* containing_view,
132 #endif
133                                        int origin_x,
134                                        int origin_y,
135                                        double animation_progress,
136                                        PaintDownloadProgressSize size);
137 
138   // A new download has started. Add it to our shelf and show the download
139   // started animation.
140   //
141   // Some downloads are removed from the shelf on completion (See
142   // DownloadItemModel::ShouldRemoveFromShelfWhenComplete()). These transient
143   // downloads are added to the shelf after a delay. If the download completes
144   // before the delay duration, it will not be added to the shelf at all.
145   void AddDownload(content::DownloadItem* download);
146 
147   // The browser view needs to know when we are going away to properly return
148   // the resize corner size to WebKit so that we don't draw on top of it.
149   // This returns the showing state of our animation which is set to true at
150   // the beginning Show and false at the beginning of a Hide.
151   virtual bool IsShowing() const = 0;
152 
153   // Returns whether the download shelf is showing the close animation.
154   virtual bool IsClosing() const = 0;
155 
156   // Opens the shelf.
157   void Show();
158 
159   // Closes the shelf.
160   void Close(CloseReason reason);
161 
162   // Hides the shelf. This closes the shelf if it is currently showing.
163   void Hide();
164 
165   // Unhides the shelf. This will cause the shelf to be opened if it was open
166   // when it was hidden, or was shown while it was hidden.
167   void Unhide();
168 
169   virtual Browser* browser() const = 0;
170 
171   // Returns whether the download shelf is hidden.
is_hidden()172   bool is_hidden() { return is_hidden_; }
173 
174  protected:
175   virtual void DoAddDownload(content::DownloadItem* download) = 0;
176   virtual void DoShow() = 0;
177   virtual void DoClose(CloseReason reason) = 0;
178 
179   // Time delay to wait before adding a transient download to the shelf.
180   // Protected virtual for testing.
181   virtual base::TimeDelta GetTransientDownloadShowDelay();
182 
183   // Returns the DownloadManager associated with this DownloadShelf. All
184   // downloads that are shown on this shelf is expected to belong to this
185   // DownloadManager. Protected virtual for testing.
186   virtual content::DownloadManager* GetDownloadManager();
187 
188  private:
189   // Show the download on the shelf immediately. Also displayes the download
190   // started animation if necessary.
191   void ShowDownload(content::DownloadItem* download);
192 
193   // Similar to ShowDownload() but refers to the download using an ID. This
194   // download should belong to the DownloadManager returned by
195   // GetDownloadManager().
196   void ShowDownloadById(int32 download_id);
197 
198   bool should_show_on_unhide_;
199   bool is_hidden_;
200   base::WeakPtrFactory<DownloadShelf> weak_ptr_factory_;
201 };
202 
203 #endif  // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_SHELF_H_
204