Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / download / download_file.h
blob568e7ee091c27c1a610ce8041e85d4641b65bf0d
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_
8 #include <string>
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"
16 namespace content {
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 {
25 public:
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)>
30 InitializeCallback;
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
35 // error.
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
55 // will be performed.
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 // Set the application GUID to be used to identify the app to the
77 // system AV function when scanning downloaded files. Should be called
78 // before RenameAndAnnotate() to take effect.
79 virtual void SetClientGuid(const std::string& guid) = 0;
81 // For testing. Must be called on FILE thread.
82 // TODO(rdsmith): Replace use of EnsureNoPendingDownloads()
83 // on the DownloadManager with a test-specific DownloadFileFactory
84 // which keeps track of the number of DownloadFiles.
85 static int GetNumberOfDownloadFiles();
87 protected:
88 static int number_active_objects_;
91 } // namespace content
93 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_H_