download_manager_delegate.h revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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 CONTENT_PUBLIC_BROWSER_DOWNLOAD_MANAGER_DELEGATE_H_
6#define CONTENT_PUBLIC_BROWSER_DOWNLOAD_MANAGER_DELEGATE_H_
7
8#include "base/basictypes.h"
9#include "base/callback.h"
10#include "base/files/file_path.h"
11#include "base/logging.h"
12#include "base/time/time.h"
13#include "content/common/content_export.h"
14#include "content/public/browser/download_danger_type.h"
15#include "content/public/browser/download_item.h"
16#include "content/public/browser/save_page_type.h"
17
18namespace content {
19
20class BrowserContext;
21class DownloadId;
22class WebContents;
23
24// Called by SavePackage when it creates a DownloadItem.
25typedef base::Callback<void(DownloadItem*)>
26    SavePackageDownloadCreatedCallback;
27
28// Will be called asynchronously with the results of the ChooseSavePath
29// operation.  If the delegate wants notification of the download item created
30// in response to this operation, the SavePackageDownloadCreatedCallback will be
31// non-null.
32typedef base::Callback<void(const base::FilePath&,
33                            SavePageType,
34                            const SavePackageDownloadCreatedCallback&)>
35    SavePackagePathPickedCallback;
36
37// Called with the results of DetermineDownloadTarget(). If the delegate decides
38// to cancel the download, then |target_path| should be set to an empty path. If
39// |target_path| is non-empty, then |intermediate_path| is required to be
40// non-empty and specify the path to the intermediate file (which could be the
41// same as |target_path|). Both |target_path| and |intermediate_path| are
42// expected to in the same directory.
43typedef base::Callback<void(
44    const base::FilePath& target_path,
45    DownloadItem::TargetDisposition disposition,
46    DownloadDangerType danger_type,
47    const base::FilePath& intermediate_path)> DownloadTargetCallback;
48
49// Called when a download delayed by the delegate has completed.
50typedef base::Callback<void(bool)> DownloadOpenDelayedCallback;
51
52// Called with the reuslt of CheckForFileExistence().
53typedef base::Callback<void(bool result)> CheckForFileExistenceCallback;
54
55// Browser's download manager: manages all downloads and destination view.
56class CONTENT_EXPORT DownloadManagerDelegate {
57 public:
58  // Lets the delegate know that the download manager is shutting down.
59  virtual void Shutdown() {}
60
61  // Returns a new DownloadId.
62  virtual DownloadId GetNextId();
63
64  // Called to notify the delegate that a new download |item| requires a
65  // download target to be determined. The delegate should return |true| if it
66  // will determine the target information and will invoke |callback|. The
67  // callback may be invoked directly (synchronously). If this function returns
68  // |false|, the download manager will continue the download using a default
69  // target path.
70  //
71  // The state of the |item| shouldn't be modified during the process of
72  // filename determination save for external data (GetExternalData() /
73  // SetExternalData()).
74  //
75  // If the download should be canceled, |callback| should be invoked with an
76  // empty |target_path| argument.
77  virtual bool DetermineDownloadTarget(DownloadItem* item,
78                                       const DownloadTargetCallback& callback);
79
80  // Tests if a file type should be opened automatically.
81  virtual bool ShouldOpenFileBasedOnExtension(const base::FilePath& path);
82
83  // Allows the delegate to delay completion of the download.  This function
84  // will either return true (in which case the download may complete)
85  // or will call the callback passed when the download is ready for
86  // completion.  This routine may be called multiple times; once the callback
87  // has been called or the function has returned true for a particular
88  // download it should continue to return true for that download.
89  virtual bool ShouldCompleteDownload(
90      DownloadItem* item,
91      const base::Closure& complete_callback);
92
93  // Allows the delegate to override opening the download. If this function
94  // returns false, the delegate needs to call callback when it's done
95  // with the item, and is responsible for opening it.  This function is called
96  // after the final rename, but before the download state is set to COMPLETED.
97  virtual bool ShouldOpenDownload(DownloadItem* item,
98                                  const DownloadOpenDelayedCallback& callback);
99
100  // Returns true if we need to generate a binary hash for downloads.
101  virtual bool GenerateFileHash();
102
103  // Retrieve the directories to save html pages and downloads to.
104  virtual void GetSaveDir(BrowserContext* browser_context,
105                          base::FilePath* website_save_dir,
106                          base::FilePath* download_save_dir,
107                          bool* skip_dir_check) {}
108
109  // Asks the user for the path to save a page. The delegate calls the callback
110  // to give the answer.
111  virtual void ChooseSavePath(
112      WebContents* web_contents,
113      const base::FilePath& suggested_path,
114      const base::FilePath::StringType& default_extension,
115      bool can_save_as_complete,
116      const SavePackagePathPickedCallback& callback) {
117  }
118
119  // Opens the file associated with this download.
120  virtual void OpenDownload(DownloadItem* download) {}
121
122  // Shows the download via the OS shell.
123  virtual void ShowDownloadInShell(DownloadItem* download) {}
124
125  // Checks whether a downloaded file still exists.
126  virtual void CheckForFileExistence(
127      DownloadItem* download,
128      const CheckForFileExistenceCallback& callback) {}
129
130 protected:
131  virtual ~DownloadManagerDelegate();
132};
133
134}  // namespace content
135
136#endif  // CONTENT_PUBLIC_BROWSER_DOWNLOAD_MANAGER_DELEGATE_H_
137