Home | History | Annotate | Download | only in download
      1 // Copyright 2013 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_TARGET_DETERMINER_DELEGATE_H_
      6 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TARGET_DETERMINER_DELEGATE_H_
      7 
      8 #include "base/callback_forward.h"
      9 
     10 #include "chrome/browser/download/download_path_reservation_tracker.h"
     11 #include "content/public/browser/download_danger_type.h"
     12 
     13 class ExtensionDownloadsEventRouter;
     14 
     15 namespace base {
     16 class FilePath;
     17 }
     18 
     19 namespace content {
     20 class DownloadItem;
     21 }
     22 
     23 // Delegate for DownloadTargetDeterminer. The delegate isn't owned by
     24 // DownloadTargetDeterminer and is expected to outlive it.
     25 class DownloadTargetDeterminerDelegate {
     26  public:
     27   // Callback to be invoked after NotifyExtensions() completes. The
     28   // |new_virtual_path| should be set to a new path if an extension wishes to
     29   // override the download path. |conflict_action| should be set to the action
     30   // to take if a file exists at |new_virtual_path|. If |new_virtual_path| is
     31   // empty, then the download target will be unchanged and |conflict_action| is
     32   // ignored.
     33   typedef base::Callback<void(
     34       const base::FilePath& new_virtual_path,
     35       DownloadPathReservationTracker::FilenameConflictAction conflict_action)>
     36   NotifyExtensionsCallback;
     37 
     38   // Callback to be invoked when ReserveVirtualPath() completes. If the path
     39   // reservation is successful, then |successful| should be true and
     40   // |reserved_path| should contain the reserved path. Otherwise, |successful|
     41   // should be false. In the failure case, |reserved_path| is ignored.
     42   typedef base::Callback<void(const base::FilePath& reserved_path,
     43                               bool successful)> ReservedPathCallback;
     44 
     45   // Callback to be invoked when PromptUserForDownloadPath() completes.
     46   // |virtual_path|: The path chosen by the user. If the user cancels the file
     47   //    selection, then this parameter will be the empty path. On Chrome OS,
     48   //    this path may contain virtual mount points if the user chose a virtual
     49   //    path (e.g. Google Drive).
     50   typedef base::Callback<void(const base::FilePath& virtual_path)>
     51   FileSelectedCallback;
     52 
     53   // Callback to be invoked when DetermineLocalPath() completes. The argument
     54   // should be the determined local path. It should be non-empty on success. If
     55   // |virtual_path| is already a local path, then |virtual_path| should be
     56   // returned as-is.
     57   typedef base::Callback<void(const base::FilePath&)> LocalPathCallback;
     58 
     59   // Callback to be invoked after CheckDownloadUrl() completes. The parameter to
     60   // the callback should indicate the danger type of the download based on the
     61   // results of the URL check.
     62   typedef base::Callback<void(content::DownloadDangerType danger_type)>
     63   CheckDownloadUrlCallback;
     64 
     65   // Notifies extensions of the impending filename determination. |virtual_path|
     66   // is the current suggested virtual path. The |callback| should be invoked to
     67   // indicate whether any extensions wish to override the path.
     68   virtual void NotifyExtensions(content::DownloadItem* download,
     69                                 const base::FilePath& virtual_path,
     70                                 const NotifyExtensionsCallback& callback) = 0;
     71 
     72   // Reserve |virtual_path|. This is expected to check the following:
     73   // - Whether |virtual_path| can be written to by the user. If not, the
     74   //   |virtual_path| can be changed to writeable path if necessary.
     75   // - If |conflict_action| is UNIQUIFY then |virtual_path| should be
     76   //   modified so that the new path is writeable and unique. If
     77   //   |conflict_action| is PROMPT, then in the event of a conflict,
     78   //   |callback| should be invoked with |success| set to |false| in
     79   //   order to force a prompt. |virtual_path| may or may not be
     80   //   modified in the latter case.
     81   // - If |create_directory| is true, then the parent directory of
     82   //   |virtual_path| should be created if it doesn't exist.
     83   //
     84   // |callback| should be invoked on completion with the results.
     85   virtual void ReserveVirtualPath(
     86       content::DownloadItem* download,
     87       const base::FilePath& virtual_path,
     88       bool create_directory,
     89       DownloadPathReservationTracker::FilenameConflictAction conflict_action,
     90       const ReservedPathCallback& callback) = 0;
     91 
     92   // Display a prompt to the user requesting that a download target be chosen.
     93   // Should invoke |callback| upon completion.
     94   virtual void PromptUserForDownloadPath(
     95       content::DownloadItem* download,
     96       const base::FilePath& virtual_path,
     97       const FileSelectedCallback& callback) = 0;
     98 
     99   // If |virtual_path| is not a local path, should return a possibly temporary
    100   // local path to use for storing the downloaded file. If |virtual_path| is
    101   // already local, then it should return the same path. |callback| should be
    102   // invoked to return the path.
    103   virtual void DetermineLocalPath(content::DownloadItem* download,
    104                                   const base::FilePath& virtual_path,
    105                                   const LocalPathCallback& callback) = 0;
    106 
    107   // Check whether the download URL is malicious and invoke |callback| with a
    108   // suggested danger type for the download.
    109   virtual void CheckDownloadUrl(content::DownloadItem* download,
    110                                 const base::FilePath& virtual_path,
    111                                 const CheckDownloadUrlCallback& callback) = 0;
    112 
    113  protected:
    114   virtual ~DownloadTargetDeterminerDelegate();
    115 };
    116 
    117 #endif  // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TARGET_DETERMINER_DELEGATE_H_
    118