Override server-side simple-cache trial with commandline switches.
[chromium-blink-merge.git] / chrome / browser / download / download_shelf.cc
blobc69abed813a72d930feafc40be443f32620ccf14
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"
7 #include "base/bind.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;
25 namespace {
27 // Delay before we show a transient download.
28 const int64 kDownloadShowDelayInSeconds = 2;
30 } // namespace
32 DownloadShelf::DownloadShelf()
33 : should_show_on_unhide_(false),
34 is_hidden_(false),
35 weak_ptr_factory_(this) {
38 DownloadShelf::~DownloadShelf() {
41 void DownloadShelf::AddDownload(DownloadItem* download) {
42 DCHECK(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(
48 FROM_HERE,
49 base::Bind(&DownloadShelf::ShowDownloadById,
50 weak_ptr_factory_.GetWeakPtr(),
51 download->GetId()),
52 GetTransientDownloadShowDelay());
53 } else {
54 ShowDownload(download);
58 void DownloadShelf::Show() {
59 if (is_hidden_) {
60 should_show_on_unhide_ = true;
61 return;
63 DoShow();
66 void DownloadShelf::Close(CloseReason reason) {
67 if (is_hidden_) {
68 should_show_on_unhide_ = false;
69 return;
71 DoClose(reason);
74 void DownloadShelf::Hide() {
75 if (is_hidden_)
76 return;
77 is_hidden_ = true;
78 if (IsShowing()) {
79 should_show_on_unhide_ = true;
80 DoClose(AUTOMATIC);
84 void DownloadShelf::Unhide() {
85 if (!is_hidden_)
86 return;
87 is_hidden_ = false;
88 if (should_show_on_unhide_) {
89 should_show_on_unhide_ = false;
90 DoShow();
94 base::TimeDelta DownloadShelf::GetTransientDownloadShowDelay() {
95 return base::TimeDelta::FromSeconds(kDownloadShowDelayInSeconds);
98 content::DownloadManager* DownloadShelf::GetDownloadManager() {
99 DCHECK(browser());
100 return content::BrowserContext::GetDownloadManager(browser()->profile());
103 void DownloadShelf::ShowDownload(DownloadItem* download) {
104 if (download->IsComplete() &&
105 DownloadItemModel(download).ShouldRemoveFromShelfWhenComplete()) {
106 return;
109 if (is_hidden_)
110 Unhide();
111 Show();
112 DoAddDownload(download);
114 // browser() can be NULL for tests.
115 if (!browser())
116 return;
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() &&
127 shelf_tab &&
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)
137 return;
139 DownloadItem* download = download_manager->GetDownload(download_id);
140 if (!download)
141 return;
143 ShowDownload(download);