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_CHROME_DOWNLOAD_MANAGER_DELEGATE_H_
      6 #define CHROME_BROWSER_DOWNLOAD_CHROME_DOWNLOAD_MANAGER_DELEGATE_H_
      7 
      8 #include "base/compiler_specific.h"
      9 #include "base/containers/hash_tables.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/memory/weak_ptr.h"
     12 #include "chrome/browser/download/download_path_reservation_tracker.h"
     13 #include "chrome/browser/download/download_target_determiner_delegate.h"
     14 #include "chrome/browser/download/download_target_info.h"
     15 #include "chrome/browser/safe_browsing/download_protection_service.h"
     16 #include "content/public/browser/download_danger_type.h"
     17 #include "content/public/browser/download_item.h"
     18 #include "content/public/browser/download_manager_delegate.h"
     19 #include "content/public/browser/notification_observer.h"
     20 #include "content/public/browser/notification_registrar.h"
     21 
     22 class DownloadPrefs;
     23 class Profile;
     24 
     25 namespace content {
     26 class DownloadManager;
     27 }
     28 
     29 namespace extensions {
     30 class CrxInstaller;
     31 }
     32 
     33 namespace user_prefs {
     34 class PrefRegistrySyncable;
     35 }
     36 
     37 #if defined(COMPILER_GCC) && defined(ENABLE_EXTENSIONS)
     38 namespace BASE_HASH_NAMESPACE {
     39 template<>
     40 struct hash<extensions::CrxInstaller*> {
     41   std::size_t operator()(extensions::CrxInstaller* const& p) const {
     42     return reinterpret_cast<std::size_t>(p);
     43   }
     44 };
     45 }  // namespace BASE_HASH_NAMESPACE
     46 #endif
     47 
     48 // This is the Chrome side helper for the download system.
     49 class ChromeDownloadManagerDelegate
     50     : public content::DownloadManagerDelegate,
     51       public content::NotificationObserver,
     52       public DownloadTargetDeterminerDelegate {
     53  public:
     54   explicit ChromeDownloadManagerDelegate(Profile* profile);
     55   virtual ~ChromeDownloadManagerDelegate();
     56 
     57   // Should be called before the first call to ShouldCompleteDownload() to
     58   // disable SafeBrowsing checks for |item|.
     59   static void DisableSafeBrowsing(content::DownloadItem* item);
     60 
     61   void SetDownloadManager(content::DownloadManager* dm);
     62 
     63   // Callbacks passed to GetNextId() will not be called until the returned
     64   // callback is called.
     65   content::DownloadIdCallback GetDownloadIdReceiverCallback();
     66 
     67   // content::DownloadManagerDelegate
     68   virtual void Shutdown() OVERRIDE;
     69   virtual void GetNextId(const content::DownloadIdCallback& callback) OVERRIDE;
     70   virtual bool DetermineDownloadTarget(
     71       content::DownloadItem* item,
     72       const content::DownloadTargetCallback& callback) OVERRIDE;
     73   virtual bool ShouldOpenFileBasedOnExtension(
     74       const base::FilePath& path) OVERRIDE;
     75   virtual bool ShouldCompleteDownload(
     76       content::DownloadItem* item,
     77       const base::Closure& complete_callback) OVERRIDE;
     78   virtual bool ShouldOpenDownload(
     79       content::DownloadItem* item,
     80       const content::DownloadOpenDelayedCallback& callback) OVERRIDE;
     81   virtual bool GenerateFileHash() OVERRIDE;
     82   virtual void GetSaveDir(content::BrowserContext* browser_context,
     83                           base::FilePath* website_save_dir,
     84                           base::FilePath* download_save_dir,
     85                           bool* skip_dir_check) OVERRIDE;
     86   virtual void ChooseSavePath(
     87       content::WebContents* web_contents,
     88       const base::FilePath& suggested_path,
     89       const base::FilePath::StringType& default_extension,
     90       bool can_save_as_complete,
     91       const content::SavePackagePathPickedCallback& callback) OVERRIDE;
     92   virtual void OpenDownload(content::DownloadItem* download) OVERRIDE;
     93   virtual void ShowDownloadInShell(content::DownloadItem* download) OVERRIDE;
     94   virtual void CheckForFileExistence(
     95       content::DownloadItem* download,
     96       const content::CheckForFileExistenceCallback& callback) OVERRIDE;
     97   virtual std::string ApplicationClientIdForFileScanning() const OVERRIDE;
     98 
     99   // Opens a download using the platform handler. DownloadItem::OpenDownload,
    100   // which ends up being handled by OpenDownload(), will open a download in the
    101   // browser if doing so is preferred.
    102   void OpenDownloadUsingPlatformHandler(content::DownloadItem* download);
    103 
    104   DownloadPrefs* download_prefs() { return download_prefs_.get(); }
    105 
    106  protected:
    107   // So that test classes that inherit from this for override purposes
    108   // can call back into the DownloadManager.
    109   content::DownloadManager* download_manager_;
    110 
    111   virtual safe_browsing::DownloadProtectionService*
    112       GetDownloadProtectionService();
    113 
    114   // DownloadTargetDeterminerDelegate. Protected for testing.
    115   virtual void NotifyExtensions(
    116       content::DownloadItem* download,
    117       const base::FilePath& suggested_virtual_path,
    118       const NotifyExtensionsCallback& callback) OVERRIDE;
    119   virtual void ReserveVirtualPath(
    120       content::DownloadItem* download,
    121       const base::FilePath& virtual_path,
    122       bool create_directory,
    123       DownloadPathReservationTracker::FilenameConflictAction conflict_action,
    124       const ReservedPathCallback& callback) OVERRIDE;
    125   virtual void PromptUserForDownloadPath(
    126       content::DownloadItem* download,
    127       const base::FilePath& suggested_virtual_path,
    128       const FileSelectedCallback& callback) OVERRIDE;
    129   virtual void DetermineLocalPath(
    130       content::DownloadItem* download,
    131       const base::FilePath& virtual_path,
    132       const LocalPathCallback& callback) OVERRIDE;
    133   virtual void CheckDownloadUrl(
    134       content::DownloadItem* download,
    135       const base::FilePath& suggested_virtual_path,
    136       const CheckDownloadUrlCallback& callback) OVERRIDE;
    137   virtual void GetFileMimeType(
    138       const base::FilePath& path,
    139       const GetFileMimeTypeCallback& callback) OVERRIDE;
    140 
    141  private:
    142   friend class base::RefCountedThreadSafe<ChromeDownloadManagerDelegate>;
    143 
    144   typedef std::vector<content::DownloadIdCallback> IdCallbackVector;
    145 
    146   // content::NotificationObserver implementation.
    147   virtual void Observe(int type,
    148                        const content::NotificationSource& source,
    149                        const content::NotificationDetails& details) OVERRIDE;
    150 
    151   // Callback function after the DownloadProtectionService completes.
    152   void CheckClientDownloadDone(
    153       uint32 download_id,
    154       safe_browsing::DownloadProtectionService::DownloadCheckResult result);
    155 
    156   // Internal gateways for ShouldCompleteDownload().
    157   bool IsDownloadReadyForCompletion(
    158       content::DownloadItem* item,
    159       const base::Closure& internal_complete_callback);
    160   void ShouldCompleteDownloadInternal(
    161     uint32 download_id,
    162     const base::Closure& user_complete_callback);
    163 
    164   void SetNextId(uint32 id);
    165 
    166   void ReturnNextId(const content::DownloadIdCallback& callback);
    167 
    168   void OnDownloadTargetDetermined(
    169       int32 download_id,
    170       const content::DownloadTargetCallback& callback,
    171       scoped_ptr<DownloadTargetInfo> target_info);
    172 
    173   // Returns true if |path| should open in the browser.
    174   bool IsOpenInBrowserPreferreredForFile(const base::FilePath& path);
    175 
    176   Profile* profile_;
    177   uint32 next_download_id_;
    178   IdCallbackVector id_callbacks_;
    179   scoped_ptr<DownloadPrefs> download_prefs_;
    180 
    181 #if defined(ENABLE_EXTENSIONS)
    182   // Maps from pending extension installations to DownloadItem IDs.
    183   typedef base::hash_map<extensions::CrxInstaller*,
    184       content::DownloadOpenDelayedCallback> CrxInstallerMap;
    185   CrxInstallerMap crx_installers_;
    186 #endif
    187 
    188   content::NotificationRegistrar registrar_;
    189 
    190   base::WeakPtrFactory<ChromeDownloadManagerDelegate> weak_ptr_factory_;
    191 
    192   DISALLOW_COPY_AND_ASSIGN(ChromeDownloadManagerDelegate);
    193 };
    194 
    195 #endif  // CHROME_BROWSER_DOWNLOAD_CHROME_DOWNLOAD_MANAGER_DELEGATE_H_
    196