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.
5 #include "chrome/browser/download/download_shelf.h"
8 #include "base/callback.h"
9 #include "base/message_loop.h"
10 #include "chrome/browser/download/download_item_model.h"
11 #include "chrome/browser/download/download_started_animation.h"
12 #include "chrome/browser/platform_util.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "content/public/browser/browser_context.h"
17 #include "content/public/browser/download_item.h"
18 #include "content/public/browser/download_manager.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/browser/web_contents_view.h"
21 #include "ui/base/animation/animation.h"
23 using content::DownloadItem
;
27 // Delay before we show a transient download.
28 const int64 kDownloadShowDelayInSeconds
= 2;
32 DownloadShelf::DownloadShelf()
33 : should_show_on_unhide_(false),
35 weak_ptr_factory_(this) {
38 DownloadShelf::~DownloadShelf() {
41 void DownloadShelf::AddDownload(DownloadItem
* download
) {
43 if (DownloadItemModel(download
).ShouldRemoveFromShelfWhenComplete()) {
44 // If we are going to remove the download from the shelf upon completion,
45 // wait a few seconds to see if it completes quickly. If it's a small
46 // download, then the user won't have time to interact with it.
47 MessageLoop::current()->PostDelayedTask(
49 base::Bind(&DownloadShelf::ShowDownloadById
,
50 weak_ptr_factory_
.GetWeakPtr(),
52 GetTransientDownloadShowDelay());
54 ShowDownload(download
);
58 void DownloadShelf::Show() {
60 should_show_on_unhide_
= true;
66 void DownloadShelf::Close(CloseReason reason
) {
68 should_show_on_unhide_
= false;
74 void DownloadShelf::Hide() {
79 should_show_on_unhide_
= true;
84 void DownloadShelf::Unhide() {
88 if (should_show_on_unhide_
) {
89 should_show_on_unhide_
= false;
94 base::TimeDelta
DownloadShelf::GetTransientDownloadShowDelay() {
95 return base::TimeDelta::FromSeconds(kDownloadShowDelayInSeconds
);
98 content::DownloadManager
* DownloadShelf::GetDownloadManager() {
100 return content::BrowserContext::GetDownloadManager(browser()->profile());
103 void DownloadShelf::ShowDownload(DownloadItem
* download
) {
104 if (download
->IsComplete() &&
105 DownloadItemModel(download
).ShouldRemoveFromShelfWhenComplete()) {
112 DoAddDownload(download
);
114 // browser() can be NULL for tests.
118 // Show the download started animation if:
119 // - Download started animation is enabled for this download. It is disabled
120 // for "Save As" downloads and extension installs, for example.
121 // - The browser has an active visible WebContents. (browser isn't minimized,
122 // or running under a test etc.)
123 // - Rich animations are enabled.
124 content::WebContents
* shelf_tab
=
125 browser()->tab_strip_model()->GetActiveWebContents();
126 if (DownloadItemModel(download
).ShouldShowDownloadStartedAnimation() &&
128 platform_util::IsVisible(shelf_tab
->GetView()->GetNativeView()) &&
129 ui::Animation::ShouldRenderRichAnimation()) {
130 DownloadStartedAnimation::Show(shelf_tab
);
134 void DownloadShelf::ShowDownloadById(int32 download_id
) {
135 content::DownloadManager
* download_manager
= GetDownloadManager();
136 if (!download_manager
)
139 DownloadItem
* download
= download_manager
->GetDownload(download_id
);
143 ShowDownload(download
);