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