| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 view that implements one download on the Download shelf. | |
| 6 // Each DownloadItemViewMd 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. | |
| 11 // | |
| 12 // The DownloadItemViewMd lives in the Browser, and has a corresponding | |
| 13 // DownloadController that receives / writes data which lives in the | |
| 14 // Renderer. | |
| 15 | |
| 16 #ifndef CHROME_BROWSER_UI_VIEWS_DOWNLOAD_DOWNLOAD_ITEM_VIEW_MD_H_ | |
| 17 #define CHROME_BROWSER_UI_VIEWS_DOWNLOAD_DOWNLOAD_ITEM_VIEW_MD_H_ | |
| 18 | |
| 19 #include <memory> | |
| 20 #include <string> | |
| 21 | |
| 22 #include "base/macros.h" | |
| 23 #include "base/memory/weak_ptr.h" | |
| 24 #include "base/strings/string_util.h" | |
| 25 #include "base/task/cancelable_task_tracker.h" | |
| 26 #include "base/time/time.h" | |
| 27 #include "base/timer/timer.h" | |
| 28 #include "chrome/browser/download/download_item_model.h" | |
| 29 #include "chrome/browser/icon_manager.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/animation/ink_drop_host_view.h" | |
| 35 #include "ui/views/context_menu_controller.h" | |
| 36 #include "ui/views/controls/button/vector_icon_button_delegate.h" | |
| 37 | |
| 38 class DownloadShelfView; | |
| 39 class DownloadShelfContextMenuView; | |
| 40 | |
| 41 namespace extensions { | |
| 42 class ExperienceSamplingEvent; | |
| 43 } | |
| 44 | |
| 45 namespace gfx { | |
| 46 class Image; | |
| 47 class ImageSkia; | |
| 48 class SlideAnimation; | |
| 49 } | |
| 50 | |
| 51 namespace ui { | |
| 52 class ThemeProvider; | |
| 53 } | |
| 54 | |
| 55 namespace views { | |
| 56 class ImageButton; | |
| 57 class Label; | |
| 58 class LabelButton; | |
| 59 } | |
| 60 | |
| 61 // The DownloadItemView in MD style. This is copied from DownloadItemView, | |
| 62 // which it should eventually replace. | |
| 63 class DownloadItemViewMd : public views::InkDropHostView, | |
| 64 public views::VectorIconButtonDelegate, | |
| 65 public views::ContextMenuController, | |
| 66 public content::DownloadItem::Observer, | |
| 67 public gfx::AnimationDelegate { | |
| 68 public: | |
| 69 DownloadItemViewMd(content::DownloadItem* download, | |
| 70 DownloadShelfView* parent); | |
| 71 ~DownloadItemViewMd() override; | |
| 72 | |
| 73 // Timer callback for handling animations | |
| 74 void UpdateDownloadProgress(); | |
| 75 void StartDownloadProgress(); | |
| 76 void StopDownloadProgress(); | |
| 77 | |
| 78 // Returns the base color for text on this download item, based on |theme|. | |
| 79 static SkColor GetTextColorForThemeProvider(const ui::ThemeProvider* theme); | |
| 80 | |
| 81 // IconManager::Client interface. | |
| 82 void OnExtractIconComplete(gfx::Image* icon); | |
| 83 | |
| 84 // Returns the DownloadItem model object belonging to this item. | |
| 85 content::DownloadItem* download() { return model_.download(); } | |
| 86 | |
| 87 // DownloadItem::Observer methods | |
| 88 void OnDownloadUpdated(content::DownloadItem* download) override; | |
| 89 void OnDownloadOpened(content::DownloadItem* download) override; | |
| 90 void OnDownloadDestroyed(content::DownloadItem* download) override; | |
| 91 | |
| 92 // Overridden from views::View: | |
| 93 void Layout() override; | |
| 94 gfx::Size GetPreferredSize() const override; | |
| 95 bool OnMousePressed(const ui::MouseEvent& event) override; | |
| 96 bool OnMouseDragged(const ui::MouseEvent& event) override; | |
| 97 void OnMouseReleased(const ui::MouseEvent& event) override; | |
| 98 void OnMouseCaptureLost() override; | |
| 99 bool OnKeyPressed(const ui::KeyEvent& event) override; | |
| 100 bool GetTooltipText(const gfx::Point& p, | |
| 101 base::string16* tooltip) const override; | |
| 102 void GetAccessibleState(ui::AXViewState* state) override; | |
| 103 void OnThemeChanged() override; | |
| 104 | |
| 105 // Overridden from view::InkDropHostView: | |
| 106 void AddInkDropLayer(ui::Layer* ink_drop_layer) override; | |
| 107 std::unique_ptr<views::InkDropRipple> CreateInkDropRipple() const override; | |
| 108 std::unique_ptr<views::InkDropHighlight> CreateInkDropHighlight() | |
| 109 const override; | |
| 110 | |
| 111 // Overridden from ui::EventHandler: | |
| 112 void OnGestureEvent(ui::GestureEvent* event) override; | |
| 113 | |
| 114 // Overridden from views::ContextMenuController. | |
| 115 void ShowContextMenuForView(View* source, | |
| 116 const gfx::Point& point, | |
| 117 ui::MenuSourceType source_type) override; | |
| 118 | |
| 119 // VectorIconButtonDelegate implementation. | |
| 120 void ButtonPressed(views::Button* sender, const ui::Event& event) override; | |
| 121 SkColor GetVectorIconBaseColor() const override; | |
| 122 | |
| 123 // gfx::AnimationDelegate implementation. | |
| 124 void AnimationProgressed(const gfx::Animation* animation) override; | |
| 125 | |
| 126 protected: | |
| 127 // Overridden from views::View: | |
| 128 void OnPaint(gfx::Canvas* canvas) override; | |
| 129 void OnFocus() override; | |
| 130 void OnBlur() override; | |
| 131 | |
| 132 private: | |
| 133 enum State { NORMAL = 0, HOT, PUSHED }; | |
| 134 class DropDownButton; | |
| 135 | |
| 136 enum Mode { | |
| 137 NORMAL_MODE = 0, // Showing download item. | |
| 138 DANGEROUS_MODE, // Displaying the dangerous download warning. | |
| 139 MALICIOUS_MODE // Displaying the malicious download warning. | |
| 140 }; | |
| 141 | |
| 142 void OpenDownload(); | |
| 143 | |
| 144 // Submits the downloaded file to the safebrowsing download feedback service. | |
| 145 // Returns whether submission was successful. On successful submission, | |
| 146 // |this| and the DownloadItem will have been deleted. | |
| 147 bool SubmitDownloadToFeedbackService(); | |
| 148 | |
| 149 // If the user has |enabled| uploading, calls SubmitDownloadToFeedbackService. | |
| 150 // Otherwise, it simply removes the DownloadItem without uploading. | |
| 151 void PossiblySubmitDownloadToFeedbackService(bool enabled); | |
| 152 | |
| 153 // This function calculates the vertical coordinate to draw the file name text | |
| 154 // relative to local bounds. | |
| 155 int GetYForFilenameText() const; | |
| 156 | |
| 157 // Painting of various download item bits. | |
| 158 void DrawStatusText(gfx::Canvas* canvas); | |
| 159 void DrawFilename(gfx::Canvas* canvas); | |
| 160 void DrawIcon(gfx::Canvas* canvas); | |
| 161 | |
| 162 void LoadIcon(); | |
| 163 void LoadIconIfItemPathChanged(); | |
| 164 | |
| 165 // Update the button colors based on the current theme. | |
| 166 void UpdateColorsFromTheme(); | |
| 167 | |
| 168 // Shows the context menu at the specified location. |point| is in the view's | |
| 169 // coordinate system. | |
| 170 void ShowContextMenuImpl(const gfx::Rect& rect, | |
| 171 ui::MenuSourceType source_type); | |
| 172 | |
| 173 // Common code for handling pointer events (i.e. mouse or gesture). | |
| 174 void HandlePressEvent(const ui::LocatedEvent& event, bool active_event); | |
| 175 void HandleClickEvent(const ui::LocatedEvent& event, bool active_event); | |
| 176 | |
| 177 // Sets the state and triggers a repaint. | |
| 178 void SetDropdownState(State new_state); | |
| 179 | |
| 180 // Whether we are in the dangerous mode. | |
| 181 bool IsShowingWarningDialog() const { | |
| 182 return mode_ == DANGEROUS_MODE || mode_ == MALICIOUS_MODE; | |
| 183 } | |
| 184 | |
| 185 // Clears or shows the warning dialog as per the state of |model_|. | |
| 186 void ToggleWarningDialog(); | |
| 187 | |
| 188 // Reverts from dangerous mode to normal download mode. | |
| 189 void ClearWarningDialog(); | |
| 190 | |
| 191 // Start displaying the dangerous download warning or the malicious download | |
| 192 // warning. | |
| 193 void ShowWarningDialog(); | |
| 194 | |
| 195 // Returns the current warning icon (should only be called when the view is | |
| 196 // actually showing a warning). | |
| 197 gfx::ImageSkia GetWarningIcon(); | |
| 198 | |
| 199 // Sets |size| with the size of the Save and Discard buttons (they have the | |
| 200 // same size). | |
| 201 gfx::Size GetButtonSize() const; | |
| 202 | |
| 203 // Sizes the dangerous download label to a minimum width available using 2 | |
| 204 // lines. The size is computed only the first time this method is invoked | |
| 205 // and simply returned on subsequent calls. | |
| 206 void SizeLabelToMinWidth(); | |
| 207 | |
| 208 // Reenables the item after it has been disabled when a user clicked it to | |
| 209 // open the downloaded file. | |
| 210 void Reenable(); | |
| 211 | |
| 212 // Releases drop down button after showing a context menu. | |
| 213 void ReleaseDropdown(); | |
| 214 | |
| 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(); | |
| 219 | |
| 220 // Show/Hide/Reset |animation| based on the state transition specified by | |
| 221 // |from| and |to|. | |
| 222 void AnimateStateTransition(State from, | |
| 223 State to, | |
| 224 gfx::SlideAnimation* animation); | |
| 225 | |
| 226 // Callback for |progress_timer_|. | |
| 227 void ProgressTimerFired(); | |
| 228 | |
| 229 // Returns the base text color. | |
| 230 SkColor GetTextColor() const; | |
| 231 | |
| 232 // Returns a slightly dimmed version of the base text color. | |
| 233 SkColor GetDimmedTextColor() const; | |
| 234 | |
| 235 // The download shelf that owns us. | |
| 236 DownloadShelfView* shelf_; | |
| 237 | |
| 238 // Elements of our particular download | |
| 239 base::string16 status_text_; | |
| 240 | |
| 241 // The font list used to print the file name and warning text. | |
| 242 gfx::FontList font_list_; | |
| 243 | |
| 244 // The font list used to print the status text below the file name. | |
| 245 gfx::FontList status_font_list_; | |
| 246 | |
| 247 // The tooltip. Only displayed when not showing a warning dialog. | |
| 248 base::string16 tooltip_text_; | |
| 249 | |
| 250 // The current state (normal, hot or pushed) of the body and drop-down. | |
| 251 State dropdown_state_; | |
| 252 | |
| 253 // Mode of the download item view. | |
| 254 Mode mode_; | |
| 255 | |
| 256 // When download progress last began animating (pausing and resuming will | |
| 257 // update this). Used for downloads of unknown size. | |
| 258 base::TimeTicks progress_start_time_; | |
| 259 | |
| 260 // Keeps the amount of time spent already animating. Used to keep track of | |
| 261 // total active time for downloads of unknown size. | |
| 262 base::TimeDelta previous_progress_elapsed_; | |
| 263 | |
| 264 // Whether we are dragging the download button. | |
| 265 bool dragging_; | |
| 266 | |
| 267 // Whether we are tracking a possible drag. | |
| 268 bool starting_drag_; | |
| 269 | |
| 270 // Position that a possible drag started at. | |
| 271 gfx::Point drag_start_point_; | |
| 272 | |
| 273 // For canceling an in progress icon request. | |
| 274 base::CancelableTaskTracker cancelable_task_tracker_; | |
| 275 | |
| 276 // A model class to control the status text we display. | |
| 277 DownloadItemModel model_; | |
| 278 | |
| 279 // Animation for download complete. | |
| 280 std::unique_ptr<gfx::SlideAnimation> complete_animation_; | |
| 281 | |
| 282 // Progress animation | |
| 283 base::RepeatingTimer progress_timer_; | |
| 284 | |
| 285 // Dangerous mode buttons. | |
| 286 views::LabelButton* save_button_; | |
| 287 views::LabelButton* discard_button_; | |
| 288 | |
| 289 // The drop down button. | |
| 290 DropDownButton* dropdown_button_; | |
| 291 | |
| 292 // Dangerous mode label. | |
| 293 views::Label* dangerous_download_label_; | |
| 294 | |
| 295 // Whether the dangerous mode label has been sized yet. | |
| 296 bool dangerous_download_label_sized_; | |
| 297 | |
| 298 // Whether we are currently disabled as part of opening the downloaded file. | |
| 299 bool disabled_while_opening_; | |
| 300 | |
| 301 // The time at which this view was created. | |
| 302 base::Time creation_time_; | |
| 303 | |
| 304 // The time at which a dangerous download warning was displayed. | |
| 305 base::Time time_download_warning_shown_; | |
| 306 | |
| 307 // The currently running download context menu. | |
| 308 std::unique_ptr<DownloadShelfContextMenuView> context_menu_; | |
| 309 | |
| 310 // The name of this view as reported to assistive technology. | |
| 311 base::string16 accessible_name_; | |
| 312 | |
| 313 // The icon loaded in the download shelf is based on the file path of the | |
| 314 // item. Store the path used, so that we can detect a change in the path | |
| 315 // and reload the icon. | |
| 316 base::FilePath last_download_item_path_; | |
| 317 | |
| 318 // ExperienceSampling: This tracks dangerous/malicious downloads warning UI | |
| 319 // and the user's decisions about it. | |
| 320 std::unique_ptr<extensions::ExperienceSamplingEvent> sampling_event_; | |
| 321 | |
| 322 // Method factory used to delay reenabling of the item when opening the | |
| 323 // downloaded file. | |
| 324 base::WeakPtrFactory<DownloadItemViewMd> weak_ptr_factory_; | |
| 325 | |
| 326 DISALLOW_COPY_AND_ASSIGN(DownloadItemViewMd); | |
| 327 }; | |
| 328 | |
| 329 #endif // CHROME_BROWSER_UI_VIEWS_DOWNLOAD_DOWNLOAD_ITEM_VIEW_MD_H_ | |
| OLD | NEW |