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