blob: be24469942e6294f136bc59705bd6d44a758d5a4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/download/download_shelf.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/message_loop.h"
#include "chrome/browser/download/download_item_model.h"
#include "chrome/browser/download/download_started_animation.h"
#include "chrome/browser/platform_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/download_item.h"
#include "content/public/browser/download_manager.h"
#include "content/public/browser/web_contents.h"
#include "ui/base/animation/animation.h"
using content::DownloadItem;
namespace {
// Delay before we show a transient download.
const int64 kDownloadShowDelayInSeconds = 2;
} // namespace
DownloadShelf::DownloadShelf()
: should_show_on_unhide_(false),
is_hidden_(false),
ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
}
DownloadShelf::~DownloadShelf() {
}
void DownloadShelf::AddDownload(DownloadItem* download) {
DCHECK(download);
if (DownloadItemModel(download).ShouldRemoveFromShelfWhenComplete()) {
// If we are going to remove the download from the shelf upon completion,
// wait a few seconds to see if it completes quickly. If it's a small
// download, then the user won't have time to interact with it.
MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&DownloadShelf::ShowDownloadById,
weak_ptr_factory_.GetWeakPtr(),
download->GetId()),
GetTransientDownloadShowDelay());
} else {
ShowDownload(download);
}
}
void DownloadShelf::Show() {
if (is_hidden_) {
should_show_on_unhide_ = true;
return;
}
DoShow();
}
void DownloadShelf::Close() {
if (is_hidden_) {
should_show_on_unhide_ = false;
return;
}
DoClose();
}
void DownloadShelf::Hide() {
if (is_hidden_)
return;
is_hidden_ = true;
if (IsShowing()) {
should_show_on_unhide_ = true;
DoClose();
}
}
void DownloadShelf::Unhide() {
if (!is_hidden_)
return;
is_hidden_ = false;
if (should_show_on_unhide_) {
should_show_on_unhide_ = false;
DoShow();
}
}
base::TimeDelta DownloadShelf::GetTransientDownloadShowDelay() {
return base::TimeDelta::FromSeconds(kDownloadShowDelayInSeconds);
}
content::DownloadManager* DownloadShelf::GetDownloadManager() {
DCHECK(browser());
return content::BrowserContext::GetDownloadManager(browser()->profile());
}
void DownloadShelf::ShowDownload(DownloadItem* download) {
if (download->IsComplete() &&
DownloadItemModel(download).ShouldRemoveFromShelfWhenComplete()) {
return;
}
if (is_hidden_)
Unhide();
Show();
DoAddDownload(download);
// browser() can be NULL for tests.
if (!browser())
return;
// Show the download started animation if:
// - Download started animation is enabled for this download. It is disabled
// for "Save As" downloads and extension installs, for example.
// - The browser has an active visble WebContents. (browser isn't minimized,
// or running under a test etc.)
// - Rich animations are enabled.
content::WebContents* shelf_tab = chrome::GetActiveWebContents(browser());
if (DownloadItemModel(download).ShouldShowDownloadStartedAnimation() &&
shelf_tab &&
platform_util::IsVisible(shelf_tab->GetNativeView()) &&
ui::Animation::ShouldRenderRichAnimation()) {
DownloadStartedAnimation::Show(shelf_tab);
}
}
void DownloadShelf::ShowDownloadById(int32 download_id) {
content::DownloadManager* download_manager = GetDownloadManager();
if (!download_manager)
return;
DownloadItem* download = download_manager->GetDownload(download_id);
if (!download)
return;
ShowDownload(download);
}
|