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
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_SERVICE_H_
6#define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_SERVICE_H_
7
8#include "base/basictypes.h"
9#include "base/callback_forward.h"
10#include "base/memory/scoped_ptr.h"
11#include "components/keyed_service/core/keyed_service.h"
12
13class ChromeDownloadManagerDelegate;
14class DownloadHistory;
15class DownloadUIController;
16class ExtensionDownloadsEventRouter;
17class Profile;
18
19namespace content {
20class DownloadManager;
21}
22
23namespace extensions {
24class ExtensionDownloadsEventRouter;
25}
26
27// Owning class for ChromeDownloadManagerDelegate.
28class DownloadService : public KeyedService {
29 public:
30  explicit DownloadService(Profile* profile);
31  virtual ~DownloadService();
32
33  // Get the download manager delegate, creating it if it doesn't already exist.
34  ChromeDownloadManagerDelegate* GetDownloadManagerDelegate();
35
36  // Get the interface to the history system. Returns NULL if profile is
37  // incognito or if the DownloadManager hasn't been created yet or if there is
38  // no HistoryService for profile. Virtual for testing.
39  virtual DownloadHistory* GetDownloadHistory();
40
41#if defined(ENABLE_EXTENSIONS)
42  extensions::ExtensionDownloadsEventRouter* GetExtensionEventRouter() {
43    return extension_event_router_.get();
44  }
45#endif
46
47  // Has a download manager been created?
48  bool HasCreatedDownloadManager();
49
50  // Number of non-malicious downloads associated with this instance of the
51  // service.
52  int NonMaliciousDownloadCount() const;
53
54  // Cancels all in-progress downloads for this profile.
55  void CancelDownloads();
56
57  // Number of non-malicious downloads associated with all profiles.
58  static int NonMaliciousDownloadCountAllProfiles();
59
60  // Cancels all in-progress downloads for all profiles.
61  static void CancelAllDownloads();
62
63  // Sets the DownloadManagerDelegate associated with this object and
64  // its DownloadManager.  Takes ownership of |delegate|, and destroys
65  // the previous delegate.  For testing.
66  void SetDownloadManagerDelegateForTesting(
67      scoped_ptr<ChromeDownloadManagerDelegate> delegate);
68
69  // Will be called to release references on other services as part
70  // of Profile shutdown.
71  virtual void Shutdown() OVERRIDE;
72
73  // Returns false if at least one extension has disabled the shelf, true
74  // otherwise.
75  bool IsShelfEnabled();
76
77 private:
78  bool download_manager_created_;
79  Profile* profile_;
80
81  // ChromeDownloadManagerDelegate may be the target of callbacks from
82  // the history service/DB thread and must be kept alive for those
83  // callbacks.
84  scoped_ptr<ChromeDownloadManagerDelegate> manager_delegate_;
85
86  scoped_ptr<DownloadHistory> download_history_;
87
88  // The UI controller is responsible for observing the download manager and
89  // notifying the UI of any new downloads. Its lifetime matches that of the
90  // associated download manager.
91  // Note on destruction order: download_ui_ depends on download_history_ and
92  // should be destroyed before the latter.
93  scoped_ptr<DownloadUIController> download_ui_;
94
95  // On Android, GET downloads are not handled by the DownloadManager.
96  // Once we have extensions on android, we probably need the EventRouter
97  // in ContentViewDownloadDelegate which knows about both GET and POST
98  // downloads.
99#if defined(ENABLE_EXTENSIONS)
100  // The ExtensionDownloadsEventRouter dispatches download creation, change, and
101  // erase events to extensions. Like ChromeDownloadManagerDelegate, it's a
102  // chrome-level concept and its lifetime should match DownloadManager. There
103  // should be a separate EDER for on-record and off-record managers.
104  // There does not appear to be a separate ExtensionSystem for on-record and
105  // off-record profiles, so ExtensionSystem cannot own the EDER.
106  scoped_ptr<extensions::ExtensionDownloadsEventRouter> extension_event_router_;
107#endif
108
109  DISALLOW_COPY_AND_ASSIGN(DownloadService);
110};
111
112#endif  // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_SERVICE_H_
113