[email protected] | 1db76f2 | 2012-01-16 04:00:49 | [diff] [blame] | 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 | |
[email protected] | 3a8fb45 | 2013-08-06 22:23:59 | [diff] [blame] | 5 | #define _USE_MATH_DEFINES // For VC++ to get M_PI. This has to be first. |
| 6 | |
[email protected] | 1db76f2 | 2012-01-16 04:00:49 | [diff] [blame] | 7 | #include "chrome/browser/download/download_shelf.h" |
| 8 | |
[email protected] | 3a8fb45 | 2013-08-06 22:23:59 | [diff] [blame] | 9 | #include <cmath> |
| 10 | |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 11 | #include "base/bind.h" |
| 12 | #include "base/callback.h" |
skyostil | 0259835 | 2015-06-12 12:37:25 | [diff] [blame] | 13 | #include "base/location.h" |
| 14 | #include "base/single_thread_task_runner.h" |
[email protected] | 3a8fb45 | 2013-08-06 22:23:59 | [diff] [blame] | 15 | #include "base/strings/string_number_conversions.h" |
gab | b15e1907 | 2016-05-11 20:45:41 | [diff] [blame] | 16 | #include "base/threading/thread_task_runner_handle.h" |
estade | 6ef02ec8 | 2015-07-24 20:52:08 | [diff] [blame] | 17 | #include "base/time/time.h" |
enne | 34f6084c | 2017-02-02 22:39:08 | [diff] [blame] | 18 | #include "cc/paint/paint_flags.h" |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 19 | #include "chrome/browser/download/download_item_model.h" |
[email protected] | 423939d | 2013-07-31 20:00:08 | [diff] [blame] | 20 | #include "chrome/browser/download/download_service.h" |
| 21 | #include "chrome/browser/download/download_service_factory.h" |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 22 | #include "chrome/browser/download/download_started_animation.h" |
| 23 | #include "chrome/browser/platform_util.h" |
| 24 | #include "chrome/browser/profiles/profile.h" |
estade | 6ef02ec8 | 2015-07-24 20:52:08 | [diff] [blame] | 25 | #include "chrome/browser/themes/theme_properties.h" |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 26 | #include "chrome/browser/ui/browser.h" |
[email protected] | 47ae2337 | 2013-01-29 01:50:48 | [diff] [blame] | 27 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 28 | #include "content/public/browser/browser_context.h" |
| 29 | #include "content/public/browser/download_item.h" |
| 30 | #include "content/public/browser/download_manager.h" |
| 31 | #include "content/public/browser/web_contents.h" |
estade | 6ef02ec8 | 2015-07-24 20:52:08 | [diff] [blame] | 32 | #include "third_party/skia/include/core/SkPath.h" |
[email protected] | 3a8fb45 | 2013-08-06 22:23:59 | [diff] [blame] | 33 | #include "ui/base/resource/resource_bundle.h" |
estade | 6ef02ec8 | 2015-07-24 20:52:08 | [diff] [blame] | 34 | #include "ui/base/theme_provider.h" |
[email protected] | ffb15d1 | 2013-09-15 17:29:30 | [diff] [blame] | 35 | #include "ui/gfx/animation/animation.h" |
[email protected] | 3a8fb45 | 2013-08-06 22:23:59 | [diff] [blame] | 36 | #include "ui/gfx/canvas.h" |
[email protected] | 3a8fb45 | 2013-08-06 22:23:59 | [diff] [blame] | 37 | |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 38 | using content::DownloadItem; |
| 39 | |
| 40 | namespace { |
| 41 | |
| 42 | // Delay before we show a transient download. |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 43 | const int64_t kDownloadShowDelayInSeconds = 2; |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 44 | |
[email protected] | 3a8fb45 | 2013-08-06 22:23:59 | [diff] [blame] | 45 | // Get the opacity based on |animation_progress|, with values in [0.0, 1.0]. |
| 46 | // Range of return value is [0, 255]. |
| 47 | int GetOpacity(double animation_progress) { |
| 48 | DCHECK(animation_progress >= 0 && animation_progress <= 1); |
| 49 | |
| 50 | // How many times to cycle the complete animation. This should be an odd |
| 51 | // number so that the animation ends faded out. |
| 52 | static const int kCompleteAnimationCycles = 5; |
| 53 | double temp = animation_progress * kCompleteAnimationCycles * M_PI + M_PI_2; |
| 54 | temp = sin(temp) / 2 + 0.5; |
| 55 | return static_cast<int>(255.0 * temp); |
| 56 | } |
| 57 | |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 58 | } // namespace |
| 59 | |
[email protected] | 1db76f2 | 2012-01-16 04:00:49 | [diff] [blame] | 60 | DownloadShelf::DownloadShelf() |
| 61 | : should_show_on_unhide_(false), |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 62 | is_hidden_(false), |
[email protected] | 9c00909 | 2013-05-01 03:14:09 | [diff] [blame] | 63 | weak_ptr_factory_(this) { |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 64 | } |
[email protected] | 1db76f2 | 2012-01-16 04:00:49 | [diff] [blame] | 65 | |
[email protected] | 3a8fb45 | 2013-08-06 22:23:59 | [diff] [blame] | 66 | DownloadShelf::~DownloadShelf() {} |
| 67 | |
[email protected] | 3a8fb45 | 2013-08-06 22:23:59 | [diff] [blame] | 68 | // Download progress painting -------------------------------------------------- |
| 69 | |
[email protected] | 3a8fb45 | 2013-08-06 22:23:59 | [diff] [blame] | 70 | // static |
estade | 2d381b0 | 2015-05-22 16:26:21 | [diff] [blame] | 71 | void DownloadShelf::PaintDownloadProgress( |
[email protected] | 3a8fb45 | 2013-08-06 22:23:59 | [diff] [blame] | 72 | gfx::Canvas* canvas, |
estade | 6ef02ec8 | 2015-07-24 20:52:08 | [diff] [blame] | 73 | const ui::ThemeProvider& theme_provider, |
estade | de2162f | 2015-08-26 19:57:58 | [diff] [blame] | 74 | const base::TimeDelta& progress_time, |
[email protected] | 3a8fb45 | 2013-08-06 22:23:59 | [diff] [blame] | 75 | int percent_done) { |
estade | 6ef02ec8 | 2015-07-24 20:52:08 | [diff] [blame] | 76 | // Draw background (light blue circle). |
enne | 3283082 | 2017-02-14 21:12:18 | [diff] [blame] | 77 | cc::PaintFlags bg_flags; |
| 78 | bg_flags.setStyle(cc::PaintFlags::kFill_Style); |
estade | 6ef02ec8 | 2015-07-24 20:52:08 | [diff] [blame] | 79 | SkColor indicator_color = |
estade | ff6ab32 | 2015-12-01 03:07:51 | [diff] [blame] | 80 | theme_provider.GetColor(ThemeProperties::COLOR_TAB_THROBBER_SPINNING); |
enne | 3283082 | 2017-02-14 21:12:18 | [diff] [blame] | 81 | bg_flags.setColor(SkColorSetA(indicator_color, 0x33)); |
| 82 | bg_flags.setAntiAlias(true); |
estade | 6ef02ec8 | 2015-07-24 20:52:08 | [diff] [blame] | 83 | const SkScalar kCenterPoint = kProgressIndicatorSize / 2.f; |
estade | 6ef02ec8 | 2015-07-24 20:52:08 | [diff] [blame] | 84 | SkPath bg; |
estade | 4eb03e5 | 2015-09-28 21:16:18 | [diff] [blame] | 85 | bg.addCircle(kCenterPoint, kCenterPoint, kCenterPoint); |
enne | 3283082 | 2017-02-14 21:12:18 | [diff] [blame] | 86 | canvas->DrawPath(bg, bg_flags); |
estade | 2d381b0 | 2015-05-22 16:26:21 | [diff] [blame] | 87 | |
estade | 6ef02ec8 | 2015-07-24 20:52:08 | [diff] [blame] | 88 | // Calculate progress. |
| 89 | SkScalar sweep_angle = 0.f; |
| 90 | // Start at 12 o'clock. |
| 91 | SkScalar start_pos = SkIntToScalar(270); |
[email protected] | 3a8fb45 | 2013-08-06 22:23:59 | [diff] [blame] | 92 | if (percent_done < 0) { |
estade | 6ef02ec8 | 2015-07-24 20:52:08 | [diff] [blame] | 93 | // For unknown size downloads, draw a 50 degree sweep that moves at |
| 94 | // 0.08 degrees per millisecond. |
| 95 | sweep_angle = 50.f; |
estade | de2162f | 2015-08-26 19:57:58 | [diff] [blame] | 96 | start_pos += static_cast<SkScalar>(progress_time.InMilliseconds() * 0.08); |
[email protected] | 3a8fb45 | 2013-08-06 22:23:59 | [diff] [blame] | 97 | } else if (percent_done > 0) { |
estade | 6ef02ec8 | 2015-07-24 20:52:08 | [diff] [blame] | 98 | sweep_angle = static_cast<SkScalar>(360 * percent_done / 100.0); |
[email protected] | 3a8fb45 | 2013-08-06 22:23:59 | [diff] [blame] | 99 | } |
| 100 | |
estade | 6ef02ec8 | 2015-07-24 20:52:08 | [diff] [blame] | 101 | // Draw progress. |
| 102 | SkPath progress; |
estade | 4eb03e5 | 2015-09-28 21:16:18 | [diff] [blame] | 103 | progress.addArc( |
| 104 | SkRect::MakeLTRB(0, 0, kProgressIndicatorSize, kProgressIndicatorSize), |
| 105 | start_pos, sweep_angle); |
enne | 3283082 | 2017-02-14 21:12:18 | [diff] [blame] | 106 | cc::PaintFlags progress_flags; |
| 107 | progress_flags.setColor(indicator_color); |
| 108 | progress_flags.setStyle(cc::PaintFlags::kStroke_Style); |
| 109 | progress_flags.setStrokeWidth(1.7f); |
| 110 | progress_flags.setAntiAlias(true); |
| 111 | canvas->DrawPath(progress, progress_flags); |
[email protected] | 3a8fb45 | 2013-08-06 22:23:59 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // static |
[email protected] | 080572c | 2014-07-16 04:21:21 | [diff] [blame] | 115 | void DownloadShelf::PaintDownloadComplete( |
| 116 | gfx::Canvas* canvas, |
estade | 6ef02ec8 | 2015-07-24 20:52:08 | [diff] [blame] | 117 | const ui::ThemeProvider& theme_provider, |
estade | 2d381b0 | 2015-05-22 16:26:21 | [diff] [blame] | 118 | double animation_progress) { |
[email protected] | 3a8fb45 | 2013-08-06 22:23:59 | [diff] [blame] | 119 | // Start at full opacity, then loop back and forth five times before ending |
| 120 | // at zero opacity. |
pkasting | b822f55 | 2015-11-04 01:51:42 | [diff] [blame] | 121 | canvas->SaveLayerAlpha(GetOpacity(animation_progress)); |
estade | de2162f | 2015-08-26 19:57:58 | [diff] [blame] | 122 | PaintDownloadProgress(canvas, theme_provider, base::TimeDelta(), 100); |
pkasting | b822f55 | 2015-11-04 01:51:42 | [diff] [blame] | 123 | canvas->Restore(); |
[email protected] | 3a8fb45 | 2013-08-06 22:23:59 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // static |
[email protected] | 080572c | 2014-07-16 04:21:21 | [diff] [blame] | 127 | void DownloadShelf::PaintDownloadInterrupted( |
| 128 | gfx::Canvas* canvas, |
estade | 6ef02ec8 | 2015-07-24 20:52:08 | [diff] [blame] | 129 | const ui::ThemeProvider& theme_provider, |
estade | 2d381b0 | 2015-05-22 16:26:21 | [diff] [blame] | 130 | double animation_progress) { |
[email protected] | 3a8fb45 | 2013-08-06 22:23:59 | [diff] [blame] | 131 | // Start at zero opacity, then loop back and forth five times before ending |
| 132 | // at full opacity. |
estade | 6ef02ec8 | 2015-07-24 20:52:08 | [diff] [blame] | 133 | PaintDownloadComplete(canvas, theme_provider, 1.0 - animation_progress); |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void DownloadShelf::AddDownload(DownloadItem* download) { |
| 137 | DCHECK(download); |
| 138 | if (DownloadItemModel(download).ShouldRemoveFromShelfWhenComplete()) { |
| 139 | // If we are going to remove the download from the shelf upon completion, |
| 140 | // wait a few seconds to see if it completes quickly. If it's a small |
| 141 | // download, then the user won't have time to interact with it. |
skyostil | 0259835 | 2015-06-12 12:37:25 | [diff] [blame] | 142 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 143 | FROM_HERE, |
| 144 | base::Bind(&DownloadShelf::ShowDownloadById, |
skyostil | 0259835 | 2015-06-12 12:37:25 | [diff] [blame] | 145 | weak_ptr_factory_.GetWeakPtr(), download->GetId()), |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 146 | GetTransientDownloadShowDelay()); |
| 147 | } else { |
| 148 | ShowDownload(download); |
| 149 | } |
[email protected] | 1db76f2 | 2012-01-16 04:00:49 | [diff] [blame] | 150 | } |
| 151 | |
sdy | 7578925 | 2017-02-17 17:25:57 | [diff] [blame] | 152 | void DownloadShelf::Open() { |
[email protected] | 1db76f2 | 2012-01-16 04:00:49 | [diff] [blame] | 153 | if (is_hidden_) { |
| 154 | should_show_on_unhide_ = true; |
| 155 | return; |
| 156 | } |
sdy | 7578925 | 2017-02-17 17:25:57 | [diff] [blame] | 157 | DoOpen(); |
[email protected] | 1db76f2 | 2012-01-16 04:00:49 | [diff] [blame] | 158 | } |
| 159 | |
[email protected] | 6fab103 | 2013-04-01 17:29:17 | [diff] [blame] | 160 | void DownloadShelf::Close(CloseReason reason) { |
[email protected] | 1db76f2 | 2012-01-16 04:00:49 | [diff] [blame] | 161 | if (is_hidden_) { |
| 162 | should_show_on_unhide_ = false; |
| 163 | return; |
| 164 | } |
[email protected] | 6fab103 | 2013-04-01 17:29:17 | [diff] [blame] | 165 | DoClose(reason); |
[email protected] | 1db76f2 | 2012-01-16 04:00:49 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void DownloadShelf::Hide() { |
| 169 | if (is_hidden_) |
| 170 | return; |
| 171 | is_hidden_ = true; |
| 172 | if (IsShowing()) { |
| 173 | should_show_on_unhide_ = true; |
sdy | 7578925 | 2017-02-17 17:25:57 | [diff] [blame] | 174 | DoHide(); |
[email protected] | 1db76f2 | 2012-01-16 04:00:49 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
| 178 | void DownloadShelf::Unhide() { |
| 179 | if (!is_hidden_) |
| 180 | return; |
| 181 | is_hidden_ = false; |
| 182 | if (should_show_on_unhide_) { |
| 183 | should_show_on_unhide_ = false; |
sdy | 7578925 | 2017-02-17 17:25:57 | [diff] [blame] | 184 | DoUnhide(); |
[email protected] | 1db76f2 | 2012-01-16 04:00:49 | [diff] [blame] | 185 | } |
| 186 | } |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 187 | |
| 188 | base::TimeDelta DownloadShelf::GetTransientDownloadShowDelay() { |
| 189 | return base::TimeDelta::FromSeconds(kDownloadShowDelayInSeconds); |
| 190 | } |
| 191 | |
| 192 | content::DownloadManager* DownloadShelf::GetDownloadManager() { |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 193 | return content::BrowserContext::GetDownloadManager(browser()->profile()); |
| 194 | } |
| 195 | |
| 196 | void DownloadShelf::ShowDownload(DownloadItem* download) { |
[email protected] | 9f259e1 | 2013-05-24 23:41:11 | [diff] [blame] | 197 | if (download->GetState() == DownloadItem::COMPLETE && |
[email protected] | 423939d | 2013-07-31 20:00:08 | [diff] [blame] | 198 | DownloadItemModel(download).ShouldRemoveFromShelfWhenComplete()) |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 199 | return; |
[email protected] | 423939d | 2013-07-31 20:00:08 | [diff] [blame] | 200 | if (!DownloadServiceFactory::GetForBrowserContext( |
| 201 | download->GetBrowserContext())->IsShelfEnabled()) |
| 202 | return; |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 203 | |
| 204 | if (is_hidden_) |
| 205 | Unhide(); |
sdy | 7578925 | 2017-02-17 17:25:57 | [diff] [blame] | 206 | Open(); |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 207 | DoAddDownload(download); |
| 208 | |
| 209 | // browser() can be NULL for tests. |
| 210 | if (!browser()) |
| 211 | return; |
| 212 | |
| 213 | // Show the download started animation if: |
| 214 | // - Download started animation is enabled for this download. It is disabled |
| 215 | // for "Save As" downloads and extension installs, for example. |
[email protected] | 47ae2337 | 2013-01-29 01:50:48 | [diff] [blame] | 216 | // - The browser has an active visible WebContents. (browser isn't minimized, |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 217 | // or running under a test etc.) |
| 218 | // - Rich animations are enabled. |
[email protected] | 47ae2337 | 2013-01-29 01:50:48 | [diff] [blame] | 219 | content::WebContents* shelf_tab = |
| 220 | browser()->tab_strip_model()->GetActiveWebContents(); |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 221 | if (DownloadItemModel(download).ShouldShowDownloadStartedAnimation() && |
| 222 | shelf_tab && |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 223 | platform_util::IsVisible(shelf_tab->GetNativeView()) && |
[email protected] | ffb15d1 | 2013-09-15 17:29:30 | [diff] [blame] | 224 | gfx::Animation::ShouldRenderRichAnimation()) { |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 225 | DownloadStartedAnimation::Show(shelf_tab); |
| 226 | } |
| 227 | } |
| 228 | |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 229 | void DownloadShelf::ShowDownloadById(int32_t download_id) { |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 230 | content::DownloadManager* download_manager = GetDownloadManager(); |
| 231 | if (!download_manager) |
| 232 | return; |
| 233 | |
| 234 | DownloadItem* download = download_manager->GetDownload(download_id); |
| 235 | if (!download) |
| 236 | return; |
| 237 | |
| 238 | ShowDownload(download); |
| 239 | } |