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.
5 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_H_
6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_H_
10 #include "base/basictypes.h"
11 #include "base/callback_forward.h"
12 #include "base/files/file_path.h"
13 #include "content/common/content_export.h"
14 #include "content/public/browser/download_interrupt_reasons.h"
18 class DownloadManager
;
20 // These objects live exclusively on the file thread and handle the writing
21 // operations for one download. These objects live only for the duration that
22 // the download is 'in progress': once the download has been completed or
23 // cancelled, the DownloadFile is destroyed.
24 class CONTENT_EXPORT DownloadFile
{
26 // Callback used with Initialize. On a successful initialize, |reason| will
27 // be DOWNLOAD_INTERRUPT_REASON_NONE; on a failed initialize, it will be
28 // set to the reason for the failure.
29 typedef base::Callback
<void(DownloadInterruptReason reason
)>
32 // Callback used with Rename*(). On a successful rename |reason| will be
33 // DOWNLOAD_INTERRUPT_REASON_NONE and |path| the path the rename
34 // was done to. On a failed rename, |reason| will contain the
36 typedef base::Callback
<void(DownloadInterruptReason reason
,
37 const base::FilePath
& path
)>
38 RenameCompletionCallback
;
40 virtual ~DownloadFile() {}
42 // Upon completion, |callback| will be called on the UI
43 // thread as per the comment above, passing DOWNLOAD_INTERRUPT_REASON_NONE
44 // on success, or a network download interrupt reason on failure.
45 virtual void Initialize(const InitializeCallback
& callback
) = 0;
47 // Rename the download file to |full_path|. If that file exists
48 // |full_path| will be uniquified by suffixing " (<number>)" to the
49 // file name before the extension.
50 virtual void RenameAndUniquify(const base::FilePath
& full_path
,
51 const RenameCompletionCallback
& callback
) = 0;
53 // Rename the download file to |full_path| and annotate it with
54 // "Mark of the Web" information about its source. No uniquification
56 virtual void RenameAndAnnotate(const base::FilePath
& full_path
,
57 const RenameCompletionCallback
& callback
) = 0;
59 // Detach the file so it is not deleted on destruction.
60 virtual void Detach() = 0;
62 // Abort the download and automatically close the file.
63 virtual void Cancel() = 0;
65 virtual base::FilePath
FullPath() const = 0;
66 virtual bool InProgress() const = 0;
67 virtual int64
CurrentSpeed() const = 0;
69 // Set |hash| with sha256 digest for the file.
70 // Returns true if digest is successfully calculated.
71 virtual bool GetHash(std::string
* hash
) = 0;
73 // Returns the current (intermediate) state of the hash as a byte string.
74 virtual std::string
GetHashState() = 0;
76 // For testing. Must be called on FILE thread.
77 // TODO(rdsmith): Replace use of EnsureNoPendingDownloads()
78 // on the DownloadManager with a test-specific DownloadFileFactory
79 // which keeps track of the number of DownloadFiles.
80 static int GetNumberOfDownloadFiles();
83 static int number_active_objects_
;
86 } // namespace content
88 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_H_