Home | History | Annotate | Download | only in download
      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
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/download/download_status_updater.h"
      6 
      7 #include "base/logging.h"
      8 #include "chrome/browser/download/download_status_updater_delegate.h"
      9 #include "chrome/browser/download/download_util.h"
     10 
     11 DownloadStatusUpdater::DownloadStatusUpdater() {
     12 }
     13 
     14 DownloadStatusUpdater::~DownloadStatusUpdater() {
     15 }
     16 
     17 void DownloadStatusUpdater::AddDelegate(
     18     DownloadStatusUpdaterDelegate* delegate) {
     19   delegates_.insert(delegate);
     20   Update();
     21 }
     22 
     23 void DownloadStatusUpdater::RemoveDelegate(
     24     DownloadStatusUpdaterDelegate* delegate) {
     25   delegates_.erase(delegate);
     26   Update();
     27 }
     28 
     29 void DownloadStatusUpdater::Update() {
     30   float progress = 0;
     31   bool progress_known = GetProgress(&progress);
     32   download_util::UpdateAppIconDownloadProgress(
     33       static_cast<int>(GetInProgressDownloadCount()),
     34       progress_known,
     35       progress);
     36 }
     37 
     38 bool DownloadStatusUpdater::GetProgress(float* progress) {
     39   *progress = 0;
     40 
     41   int64 received_bytes = 0;
     42   int64 total_bytes = 0;
     43   for (DelegateSet::iterator i = delegates_.begin();
     44        i != delegates_.end(); ++i) {
     45     if (!(*i)->IsDownloadProgressKnown())
     46       return false;
     47     received_bytes += (*i)->GetReceivedDownloadBytes();
     48     total_bytes += (*i)->GetTotalDownloadBytes();
     49   }
     50 
     51   if (total_bytes > 0)
     52     *progress = static_cast<float>(received_bytes) / total_bytes;
     53   return true;
     54 }
     55 
     56 int64 DownloadStatusUpdater::GetInProgressDownloadCount() {
     57   int64 download_count = 0;
     58   for (DelegateSet::iterator i = delegates_.begin();
     59        i != delegates_.end(); ++i) {
     60     download_count += (*i)->GetInProgressDownloadCount();
     61   }
     62 
     63   return download_count;
     64 }
     65