Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / views / download / download_item_view.h
bloba1c34b18c59882fa9c0858890a865112c773c97f
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 // A ChromeView that implements one download on the Download shelf.
6 // Each DownloadItemView contains an application icon, a text label
7 // indicating the download's file name, a text label indicating the
8 // download's status (such as the number of bytes downloaded so far)
9 // and a button for canceling an in progress download, or opening
10 // the completed download.
12 // The DownloadItemView lives in the Browser, and has a corresponding
13 // DownloadController that receives / writes data which lives in the
14 // Renderer.
16 #ifndef CHROME_BROWSER_UI_VIEWS_DOWNLOAD_DOWNLOAD_ITEM_VIEW_H__
17 #define CHROME_BROWSER_UI_VIEWS_DOWNLOAD_DOWNLOAD_ITEM_VIEW_H__
19 #include <string>
21 #include "base/basictypes.h"
22 #include "base/memory/scoped_ptr.h"
23 #include "base/memory/weak_ptr.h"
24 #include "base/strings/string_util.h"
25 #include "base/time/time.h"
26 #include "base/timer/timer.h"
27 #include "chrome/browser/download/download_item_model.h"
28 #include "chrome/browser/icon_manager.h"
29 #include "chrome/common/cancelable_task_tracker.h"
30 #include "content/public/browser/download_item.h"
31 #include "content/public/browser/download_manager.h"
32 #include "ui/gfx/animation/animation_delegate.h"
33 #include "ui/gfx/font_list.h"
34 #include "ui/views/context_menu_controller.h"
35 #include "ui/views/controls/button/button.h"
36 #include "ui/views/view.h"
38 class DownloadShelfView;
39 class DownloadShelfContextMenuView;
41 namespace gfx {
42 class Image;
43 class ImageSkia;
44 class SlideAnimation;
47 namespace views {
48 class Label;
49 class LabelButton;
52 class DownloadItemView : public views::ButtonListener,
53 public views::View,
54 public views::ContextMenuController,
55 public content::DownloadItem::Observer,
56 public gfx::AnimationDelegate {
57 public:
58 DownloadItemView(content::DownloadItem* download, DownloadShelfView* parent);
59 virtual ~DownloadItemView();
61 // Timer callback for handling animations
62 void UpdateDownloadProgress();
63 void StartDownloadProgress();
64 void StopDownloadProgress();
66 // IconManager::Client interface.
67 void OnExtractIconComplete(gfx::Image* icon);
69 // Returns the DownloadItem model object belonging to this item.
70 content::DownloadItem* download() { return model_.download(); }
72 // DownloadItem::Observer methods
73 virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE;
74 virtual void OnDownloadOpened(content::DownloadItem* download) OVERRIDE;
75 virtual void OnDownloadDestroyed(content::DownloadItem* download) OVERRIDE;
77 // Overridden from views::View:
78 virtual void Layout() OVERRIDE;
79 virtual gfx::Size GetPreferredSize() OVERRIDE;
80 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE;
81 virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE;
82 virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE;
83 virtual void OnMouseCaptureLost() OVERRIDE;
84 virtual void OnMouseMoved(const ui::MouseEvent& event) OVERRIDE;
85 virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE;
86 virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE;
87 virtual bool GetTooltipText(const gfx::Point& p,
88 base::string16* tooltip) const OVERRIDE;
89 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
90 virtual void OnThemeChanged() OVERRIDE;
92 // Overridden from ui::EventHandler:
93 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
95 // Overridden from views::ContextMenuController.
96 virtual void ShowContextMenuForView(View* source,
97 const gfx::Point& point,
98 ui::MenuSourceType source_type) OVERRIDE;
100 // ButtonListener implementation.
101 virtual void ButtonPressed(views::Button* sender,
102 const ui::Event& event) OVERRIDE;
104 // gfx::AnimationDelegate implementation.
105 virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE;
107 protected:
108 // Overridden from views::View:
109 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
110 virtual void OnPaintBackground(gfx::Canvas* canvas) OVERRIDE;
111 virtual void OnFocus() OVERRIDE;
112 virtual void OnBlur() OVERRIDE;
114 private:
115 enum State {
116 NORMAL = 0,
117 HOT,
118 PUSHED
121 enum Mode {
122 NORMAL_MODE = 0, // Showing download item.
123 DANGEROUS_MODE, // Displaying the dangerous download warning.
124 MALICIOUS_MODE // Displaying the malicious download warning.
127 // The image set associated with the part containing the icon and text.
128 struct BodyImageSet {
129 gfx::ImageSkia* top_left;
130 gfx::ImageSkia* left;
131 gfx::ImageSkia* bottom_left;
132 gfx::ImageSkia* top;
133 gfx::ImageSkia* center;
134 gfx::ImageSkia* bottom;
135 gfx::ImageSkia* top_right;
136 gfx::ImageSkia* right;
137 gfx::ImageSkia* bottom_right;
140 // The image set associated with the drop-down button on the right.
141 struct DropDownImageSet {
142 gfx::ImageSkia* top;
143 gfx::ImageSkia* center;
144 gfx::ImageSkia* bottom;
147 void OpenDownload();
149 // Submit the downloaded file to the safebrowsing download feedback service.
150 // If true is returned, the DownloadItem and |this| have been deleted. If
151 // false is returned, nothing has changed.
152 bool BeginDownloadFeedback();
154 void LoadIcon();
155 void LoadIconIfItemPathChanged();
157 // Update the button colors based on the current theme.
158 void UpdateColorsFromTheme();
160 // Shows the context menu at the specified location. |point| is in the view's
161 // coordinate system.
162 void ShowContextMenuImpl(const gfx::Point& point,
163 ui::MenuSourceType source_type);
165 // Common code for handling pointer events (i.e. mouse or gesture).
166 void HandlePressEvent(const ui::LocatedEvent& event, bool active_event);
167 void HandleClickEvent(const ui::LocatedEvent& event, bool active_event);
169 // Convenience method to paint the 3 vertical images (bottom, middle, top)
170 // that form the background.
171 void PaintImages(gfx::Canvas* canvas,
172 const gfx::ImageSkia* top_image,
173 const gfx::ImageSkia* center_image,
174 const gfx::ImageSkia* bottom_image,
175 int x,
176 int y,
177 int height,
178 int width);
180 // Sets the state and triggers a repaint.
181 void SetState(State body_state, State drop_down_state);
183 // Whether we are in the dangerous mode.
184 bool IsShowingWarningDialog() const {
185 return mode_ == DANGEROUS_MODE || mode_ == MALICIOUS_MODE;
188 // Reverts from dangerous mode to normal download mode.
189 void ClearWarningDialog();
191 // Start displaying the dangerous download warning or the malicious download
192 // warning.
193 void ShowWarningDialog();
195 // Sets |size| with the size of the Save and Discard buttons (they have the
196 // same size).
197 gfx::Size GetButtonSize();
199 // Sizes the dangerous download label to a minimum width available using 2
200 // lines. The size is computed only the first time this method is invoked
201 // and simply returned on subsequent calls.
202 void SizeLabelToMinWidth();
204 // Reenables the item after it has been disabled when a user clicked it to
205 // open the downloaded file.
206 void Reenable();
208 // Releases drop down button after showing a context menu.
209 void ReleaseDropDown();
211 // Given |x|, returns whether |x| is within the x coordinate range of
212 // the drop-down button or not.
213 bool InDropDownButtonXCoordinateRange(int x);
215 // Update the accessible name to reflect the current state of the control,
216 // so that screenreaders can access the filename, status text, and
217 // dangerous download warning message (if any).
218 void UpdateAccessibleName();
220 // Update the location of the drop down button.
221 void UpdateDropDownButtonPosition();
223 // Show/Hide/Reset |animation| based on the state transition specified by
224 // |from| and |to|.
225 void AnimateStateTransition(State from, State to,
226 gfx::SlideAnimation* animation);
228 // The different images used for the background.
229 BodyImageSet normal_body_image_set_;
230 BodyImageSet hot_body_image_set_;
231 BodyImageSet pushed_body_image_set_;
232 BodyImageSet dangerous_mode_body_image_set_;
233 BodyImageSet malicious_mode_body_image_set_;
234 DropDownImageSet normal_drop_down_image_set_;
235 DropDownImageSet hot_drop_down_image_set_;
236 DropDownImageSet pushed_drop_down_image_set_;
238 // The warning icon showns for dangerous downloads.
239 const gfx::ImageSkia* warning_icon_;
241 // The download shelf that owns us.
242 DownloadShelfView* shelf_;
244 // Elements of our particular download
245 base::string16 status_text_;
247 // The font list used to print the file name and status.
248 gfx::FontList font_list_;
250 // The tooltip. Only displayed when not showing a warning dialog.
251 base::string16 tooltip_text_;
253 // The current state (normal, hot or pushed) of the body and drop-down.
254 State body_state_;
255 State drop_down_state_;
257 // Mode of the download item view.
258 Mode mode_;
260 // In degrees, for downloads with no known total size.
261 int progress_angle_;
263 // The left and right x coordinates of the drop-down button.
264 int drop_down_x_left_;
265 int drop_down_x_right_;
267 // Used when we are showing the menu to show the drop-down as pressed.
268 bool drop_down_pressed_;
270 // The height of the box formed by the background images and its labels.
271 int box_height_;
273 // The y coordinate of the box formed by the background images and its labels.
274 int box_y_;
276 // Whether we are dragging the download button.
277 bool dragging_;
279 // Whether we are tracking a possible drag.
280 bool starting_drag_;
282 // Position that a possible drag started at.
283 gfx::Point drag_start_point_;
285 // For canceling an in progress icon request.
286 CancelableTaskTracker cancelable_task_tracker_;
288 // A model class to control the status text we display.
289 DownloadItemModel model_;
291 // Hover animations for our body and drop buttons.
292 scoped_ptr<gfx::SlideAnimation> body_hover_animation_;
293 scoped_ptr<gfx::SlideAnimation> drop_hover_animation_;
295 // Animation for download complete.
296 scoped_ptr<gfx::SlideAnimation> complete_animation_;
298 // Progress animation
299 base::RepeatingTimer<DownloadItemView> progress_timer_;
301 // Dangerous mode buttons.
302 views::LabelButton* save_button_;
303 views::LabelButton* discard_button_;
305 // Dangerous mode label.
306 views::Label* dangerous_download_label_;
308 // Whether the dangerous mode label has been sized yet.
309 bool dangerous_download_label_sized_;
311 // The size of the buttons. Cached so animation works when hidden.
312 gfx::Size cached_button_size_;
314 // Whether we are currently disabled as part of opening the downloaded file.
315 bool disabled_while_opening_;
317 // The time at which this view was created.
318 base::Time creation_time_;
320 // The time at which a dangerous download warning was displayed.
321 base::Time time_download_warning_shown_;
323 // Method factory used to delay reenabling of the item when opening the
324 // downloaded file.
325 base::WeakPtrFactory<DownloadItemView> weak_ptr_factory_;
327 // The currently running download context menu.
328 scoped_ptr<DownloadShelfContextMenuView> context_menu_;
330 // The name of this view as reported to assistive technology.
331 base::string16 accessible_name_;
333 // The icon loaded in the download shelf is based on the file path of the
334 // item. Store the path used, so that we can detect a change in the path
335 // and reload the icon.
336 base::FilePath last_download_item_path_;
338 DISALLOW_COPY_AND_ASSIGN(DownloadItemView);
341 #endif // CHROME_BROWSER_UI_VIEWS_DOWNLOAD_DOWNLOAD_ITEM_VIEW_H__