summaryrefslogtreecommitdiffstats
path: root/chrome/browser/download/download_service.cc
blob: 98031ec7e2d771e224c2a0eb6f67bdaf2559bced (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/download/download_service.h"

#include "base/callback.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/download/chrome_download_manager_delegate.h"
#include "chrome/browser/download/download_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "content/browser/download/download_id_factory.h"
#include "content/public/browser/download_manager.h"

using content::DownloadManager;

DownloadService::DownloadService(Profile* profile)
    : download_manager_created_(false),
      profile_(profile) {
  if (profile_->IsOffTheRecord()) {
    id_factory_ = DownloadServiceFactory::GetForProfile(
        profile_->GetOriginalProfile())->GetDownloadIdFactory();
  } else {
    id_factory_ = new DownloadIdFactory(this);
  }
}

DownloadService::~DownloadService() {}

DownloadIdFactory* DownloadService::GetDownloadIdFactory() const {
  return id_factory_.get();
}

void DownloadService::OnManagerCreated(
    const DownloadService::OnManagerCreatedCallback& cb) {
  if (download_manager_created_) {
    cb.Run(manager_.get());
  } else {
    on_manager_created_callbacks_.push_back(cb);
  }
}

DownloadManager* DownloadService::GetDownloadManager() {
  if (!download_manager_created_) {
    // In case the delegate has already been set by
    // SetDownloadManagerDelegateForTesting.
    if (!manager_delegate_.get())
      manager_delegate_ = new ChromeDownloadManagerDelegate(profile_);
    manager_ = DownloadManager::Create(
        manager_delegate_.get(),
        id_factory_.get(),
        g_browser_process->download_status_updater());
    manager_->Init(profile_);
    manager_delegate_->SetDownloadManager(manager_);
    download_manager_created_ = true;
    for (std::vector<OnManagerCreatedCallback>::iterator cb
         = on_manager_created_callbacks_.begin();
         cb != on_manager_created_callbacks_.end(); ++cb) {
      cb->Run(manager_.get());
    }
    on_manager_created_callbacks_.clear();
  }
  return manager_.get();
}

bool DownloadService::HasCreatedDownloadManager() {
  return download_manager_created_;
}

int DownloadService::DownloadCount() const {
  return download_manager_created_ ? manager_->InProgressCount() : 0;
}

// static
int DownloadService::DownloadCountAllProfiles() {
  std::vector<Profile*> profiles(
      g_browser_process->profile_manager()->GetLoadedProfiles());

  int count = 0;
  for (std::vector<Profile*>::iterator it = profiles.begin();
       it < profiles.end(); ++it) {
    count += DownloadServiceFactory::GetForProfile(*it)->DownloadCount();
    if ((*it)->HasOffTheRecordProfile())
      count += DownloadServiceFactory::GetForProfile(
          (*it)->GetOffTheRecordProfile())->DownloadCount();
  }

  return count;
}

void DownloadService::SetDownloadManagerDelegateForTesting(
    ChromeDownloadManagerDelegate* new_delegate) {
  // Guarantee everything is properly initialized.
  GetDownloadManager();

  manager_->SetDownloadManagerDelegate(new_delegate);
  new_delegate->SetDownloadManager(manager_);
  manager_delegate_ = new_delegate;
}

void DownloadService::Shutdown() {
  if (manager_.get()) {
    manager_->Shutdown();

    // The manager reference can be released any time after shutdown;
    // it will be destroyed when the last reference is released on the
    // FILE thread.
    // Resetting here will guarantee that any attempts to get the
    // DownloadManager after shutdown will return null.
    //
    // TODO(rdsmith): Figure out how to guarantee when the last reference
    // will be released and make DownloadManager not RefCountedThreadSafe<>.
    manager_.release();
  }
  if (manager_delegate_.get())
    manager_delegate_.release();
}