OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "content/browser/download/download_status_updater.h" | 5 #include "content/browser/download/download_status_updater.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "content/browser/download/download_status_updater_delegate.h" | 8 #include "content/browser/download/download_status_updater_delegate.h" |
9 | 9 |
10 DownloadStatusUpdater::DownloadStatusUpdater() { | 10 DownloadStatusUpdater::DownloadStatusUpdater() { |
11 } | 11 } |
12 | 12 |
13 DownloadStatusUpdater::~DownloadStatusUpdater() { | 13 DownloadStatusUpdater::~DownloadStatusUpdater() { |
14 } | 14 } |
15 | 15 |
16 void DownloadStatusUpdater::AddDelegate( | 16 void DownloadStatusUpdater::AddDelegate( |
17 DownloadStatusUpdaterDelegate* delegate) { | 17 DownloadStatusUpdaterDelegate* delegate) { |
18 delegates_.insert(delegate); | 18 delegates_.insert(delegate); |
19 } | 19 } |
20 | 20 |
21 void DownloadStatusUpdater::RemoveDelegate( | 21 void DownloadStatusUpdater::RemoveDelegate( |
22 DownloadStatusUpdaterDelegate* delegate) { | 22 DownloadStatusUpdaterDelegate* delegate) { |
23 delegates_.erase(delegate); | 23 delegates_.erase(delegate); |
24 } | 24 } |
25 | 25 |
26 bool DownloadStatusUpdater::GetProgress(float* progress, | 26 bool DownloadStatusUpdater::GetProgress(float* progress, |
27 int* download_count) { | 27 int* download_count) const { |
28 *progress = 0; | 28 *progress = 0; |
29 *download_count = GetInProgressDownloadCount(); | 29 *download_count = GetInProgressDownloadCount(); |
30 | 30 |
31 int64 received_bytes = 0; | 31 int64 received_bytes = 0; |
32 int64 total_bytes = 0; | 32 int64 total_bytes = 0; |
33 for (DelegateSet::iterator i = delegates_.begin(); | 33 for (DelegateSet::const_iterator i = delegates_.begin(); |
34 i != delegates_.end(); ++i) { | 34 i != delegates_.end(); ++i) { |
35 if (!(*i)->IsDownloadProgressKnown()) | 35 if (!(*i)->IsDownloadProgressKnown()) |
36 return false; | 36 return false; |
37 received_bytes += (*i)->GetReceivedDownloadBytes(); | 37 received_bytes += (*i)->GetReceivedDownloadBytes(); |
38 total_bytes += (*i)->GetTotalDownloadBytes(); | 38 total_bytes += (*i)->GetTotalDownloadBytes(); |
39 } | 39 } |
40 | 40 |
41 if (total_bytes > 0) | 41 if (total_bytes > 0) |
42 *progress = static_cast<float>(received_bytes) / total_bytes; | 42 *progress = static_cast<float>(received_bytes) / total_bytes; |
43 return true; | 43 return true; |
44 } | 44 } |
45 | 45 |
46 int64 DownloadStatusUpdater::GetInProgressDownloadCount() { | 46 int64 DownloadStatusUpdater::GetInProgressDownloadCount() const { |
47 int64 download_count = 0; | 47 int64 download_count = 0; |
48 for (DelegateSet::iterator i = delegates_.begin(); | 48 for (DelegateSet::const_iterator i = delegates_.begin(); |
49 i != delegates_.end(); ++i) { | 49 i != delegates_.end(); ++i) { |
50 download_count += (*i)->GetInProgressDownloadCount(); | 50 download_count += (*i)->GetInProgressDownloadCount(); |
51 } | 51 } |
52 | 52 |
53 return download_count; | 53 return download_count; |
54 } | 54 } |
OLD | NEW |