| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/download/download_status_updater.h" | 5 #include "chrome/browser/download/download_status_updater.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/stl_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 15 | 15 |
| 16 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) | 16 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 17 #include "ui/views/linux_ui/linux_ui.h" | 17 #include "ui/views/linux_ui/linux_ui.h" |
| 18 #endif | 18 #endif |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 // DownloadStatusUpdater::UpdateAppIconDownloadProgress() expects to only be | 22 // DownloadStatusUpdater::UpdateAppIconDownloadProgress() expects to only be |
| 23 // called once when a DownloadItem completes, then not again (except perhaps | 23 // called once when a DownloadItem completes, then not again (except perhaps |
| (...skipping 25 matching lines...) Expand all Loading... |
| 49 | 49 |
| 50 const char WasInProgressData::kKey[] = | 50 const char WasInProgressData::kKey[] = |
| 51 "DownloadItem DownloadStatusUpdater WasInProgressData"; | 51 "DownloadItem DownloadStatusUpdater WasInProgressData"; |
| 52 | 52 |
| 53 } // anonymous namespace | 53 } // anonymous namespace |
| 54 | 54 |
| 55 DownloadStatusUpdater::DownloadStatusUpdater() { | 55 DownloadStatusUpdater::DownloadStatusUpdater() { |
| 56 } | 56 } |
| 57 | 57 |
| 58 DownloadStatusUpdater::~DownloadStatusUpdater() { | 58 DownloadStatusUpdater::~DownloadStatusUpdater() { |
| 59 base::STLDeleteElements(¬ifiers_); | |
| 60 } | 59 } |
| 61 | 60 |
| 62 bool DownloadStatusUpdater::GetProgress(float* progress, | 61 bool DownloadStatusUpdater::GetProgress(float* progress, |
| 63 int* download_count) const { | 62 int* download_count) const { |
| 64 *progress = 0; | 63 *progress = 0; |
| 65 *download_count = 0; | 64 *download_count = 0; |
| 66 bool progress_certain = true; | 65 bool progress_certain = true; |
| 67 int64_t received_bytes = 0; | 66 int64_t received_bytes = 0; |
| 68 int64_t total_bytes = 0; | 67 int64_t total_bytes = 0; |
| 69 | 68 |
| 70 for (std::vector<AllDownloadItemNotifier*>::const_iterator it = | 69 for (const auto& notifier : notifiers_) { |
| 71 notifiers_.begin(); it != notifiers_.end(); ++it) { | 70 if (notifier->GetManager()) { |
| 72 if ((*it)->GetManager()) { | |
| 73 content::DownloadManager::DownloadVector items; | 71 content::DownloadManager::DownloadVector items; |
| 74 (*it)->GetManager()->GetAllDownloads(&items); | 72 notifier->GetManager()->GetAllDownloads(&items); |
| 75 for (content::DownloadManager::DownloadVector::const_iterator it = | 73 for (auto* item : items) { |
| 76 items.begin(); it != items.end(); ++it) { | 74 if (item->GetState() == content::DownloadItem::IN_PROGRESS) { |
| 77 if ((*it)->GetState() == content::DownloadItem::IN_PROGRESS) { | |
| 78 ++*download_count; | 75 ++*download_count; |
| 79 if ((*it)->GetTotalBytes() <= 0) { | 76 if (item->GetTotalBytes() <= 0) { |
| 80 // There may or may not be more data coming down this pipe. | 77 // There may or may not be more data coming down this pipe. |
| 81 progress_certain = false; | 78 progress_certain = false; |
| 82 } else { | 79 } else { |
| 83 received_bytes += (*it)->GetReceivedBytes(); | 80 received_bytes += item->GetReceivedBytes(); |
| 84 total_bytes += (*it)->GetTotalBytes(); | 81 total_bytes += item->GetTotalBytes(); |
| 85 } | 82 } |
| 86 } | 83 } |
| 87 } | 84 } |
| 88 } | 85 } |
| 89 } | 86 } |
| 90 | 87 |
| 91 if (total_bytes > 0) | 88 if (total_bytes > 0) |
| 92 *progress = static_cast<float>(received_bytes) / total_bytes; | 89 *progress = static_cast<float>(received_bytes) / total_bytes; |
| 93 return progress_certain; | 90 return progress_certain; |
| 94 } | 91 } |
| 95 | 92 |
| 96 void DownloadStatusUpdater::AddManager(content::DownloadManager* manager) { | 93 void DownloadStatusUpdater::AddManager(content::DownloadManager* manager) { |
| 97 notifiers_.push_back(new AllDownloadItemNotifier(manager, this)); | 94 notifiers_.push_back( |
| 95 base::MakeUnique<AllDownloadItemNotifier>(manager, this)); |
| 98 content::DownloadManager::DownloadVector items; | 96 content::DownloadManager::DownloadVector items; |
| 99 manager->GetAllDownloads(&items); | 97 manager->GetAllDownloads(&items); |
| 100 for (content::DownloadManager::DownloadVector::const_iterator | 98 for (auto* item : items) |
| 101 it = items.begin(); it != items.end(); ++it) { | 99 OnDownloadCreated(manager, item); |
| 102 OnDownloadCreated(manager, *it); | |
| 103 } | |
| 104 } | 100 } |
| 105 | 101 |
| 106 void DownloadStatusUpdater::OnDownloadCreated( | 102 void DownloadStatusUpdater::OnDownloadCreated( |
| 107 content::DownloadManager* manager, content::DownloadItem* item) { | 103 content::DownloadManager* manager, content::DownloadItem* item) { |
| 108 // Ignore downloads loaded from history, which are in a terminal state. | 104 // Ignore downloads loaded from history, which are in a terminal state. |
| 109 // TODO(benjhayden): Use the Observer interface to distinguish between | 105 // TODO(benjhayden): Use the Observer interface to distinguish between |
| 110 // historical and started downloads. | 106 // historical and started downloads. |
| 111 if (item->GetState() == content::DownloadItem::IN_PROGRESS) { | 107 if (item->GetState() == content::DownloadItem::IN_PROGRESS) { |
| 112 UpdateAppIconDownloadProgress(item); | 108 UpdateAppIconDownloadProgress(item); |
| 113 new WasInProgressData(item); | 109 new WasInProgressData(item); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 float progress = 0; | 142 float progress = 0; |
| 147 int download_count = 0; | 143 int download_count = 0; |
| 148 GetProgress(&progress, &download_count); | 144 GetProgress(&progress, &download_count); |
| 149 linux_ui->SetDownloadCount(download_count); | 145 linux_ui->SetDownloadCount(download_count); |
| 150 linux_ui->SetProgressFraction(progress); | 146 linux_ui->SetProgressFraction(progress); |
| 151 } | 147 } |
| 152 #endif | 148 #endif |
| 153 // TODO(avi): Implement for Android? | 149 // TODO(avi): Implement for Android? |
| 154 } | 150 } |
| 155 #endif | 151 #endif |
| OLD | NEW |