This source file includes following definitions.
- Get
- Clear
- GetProgress
- AddManager
- OnDownloadCreated
- OnDownloadUpdated
- UpdateAppIconDownloadProgress
#include "chrome/browser/download/download_status_updater.h"
#include <vector>
#include "base/logging.h"
#include "base/stl_util.h"
#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
#include "ui/views/linux_ui/linux_ui.h"
#endif
namespace {
class WasInProgressData : public base::SupportsUserData::Data {
public:
static bool Get(content::DownloadItem* item) {
return item->GetUserData(kKey) != NULL;
}
static void Clear(content::DownloadItem* item) {
item->RemoveUserData(kKey);
}
explicit WasInProgressData(content::DownloadItem* item) {
item->SetUserData(kKey, this);
}
private:
static const char kKey[];
DISALLOW_COPY_AND_ASSIGN(WasInProgressData);
};
const char WasInProgressData::kKey[] =
"DownloadItem DownloadStatusUpdater WasInProgressData";
}
DownloadStatusUpdater::DownloadStatusUpdater() {
}
DownloadStatusUpdater::~DownloadStatusUpdater() {
STLDeleteElements(¬ifiers_);
}
bool DownloadStatusUpdater::GetProgress(float* progress,
int* download_count) const {
*progress = 0;
*download_count = 0;
bool progress_certain = true;
int64 received_bytes = 0;
int64 total_bytes = 0;
for (std::vector<AllDownloadItemNotifier*>::const_iterator it =
notifiers_.begin(); it != notifiers_.end(); ++it) {
if ((*it)->GetManager()) {
content::DownloadManager::DownloadVector items;
(*it)->GetManager()->GetAllDownloads(&items);
for (content::DownloadManager::DownloadVector::const_iterator it =
items.begin(); it != items.end(); ++it) {
if ((*it)->GetState() == content::DownloadItem::IN_PROGRESS) {
++*download_count;
if ((*it)->GetTotalBytes() <= 0) {
progress_certain = false;
} else {
received_bytes += (*it)->GetReceivedBytes();
total_bytes += (*it)->GetTotalBytes();
}
}
}
}
}
if (total_bytes > 0)
*progress = static_cast<float>(received_bytes) / total_bytes;
return progress_certain;
}
void DownloadStatusUpdater::AddManager(content::DownloadManager* manager) {
notifiers_.push_back(new AllDownloadItemNotifier(manager, this));
content::DownloadManager::DownloadVector items;
manager->GetAllDownloads(&items);
for (content::DownloadManager::DownloadVector::const_iterator
it = items.begin(); it != items.end(); ++it) {
OnDownloadCreated(manager, *it);
}
}
void DownloadStatusUpdater::OnDownloadCreated(
content::DownloadManager* manager, content::DownloadItem* item) {
if (item->GetState() == content::DownloadItem::IN_PROGRESS) {
UpdateAppIconDownloadProgress(item);
new WasInProgressData(item);
}
}
void DownloadStatusUpdater::OnDownloadUpdated(
content::DownloadManager* manager, content::DownloadItem* item) {
if (item->GetState() == content::DownloadItem::IN_PROGRESS) {
if (!WasInProgressData::Get(item))
new WasInProgressData(item);
} else {
if (!WasInProgressData::Get(item))
return;
WasInProgressData::Clear(item);
}
UpdateAppIconDownloadProgress(item);
}
#if defined(OS_ANDROID) || (defined(USE_AURA) && !defined(OS_WIN))
void DownloadStatusUpdater::UpdateAppIconDownloadProgress(
content::DownloadItem* download) {
#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
const views::LinuxUI* linux_ui = views::LinuxUI::instance();
if (linux_ui) {
float progress = 0;
int download_count = 0;
GetProgress(&progress, &download_count);
linux_ui->SetDownloadCount(download_count);
linux_ui->SetProgressFraction(progress);
}
#endif
}
#endif