OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_H_ | 5 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_H_ |
6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_H_ | 6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/memory/ref_counted.h" | |
13 #include "content/browser/download/base_file.h" | 12 #include "content/browser/download/base_file.h" |
14 #include "content/browser/download/download_id.h" | 13 #include "content/browser/download/download_id.h" |
15 #include "content/browser/download/download_request_handle.h" | |
16 #include "content/browser/download/download_types.h" | |
17 #include "content/common/content_export.h" | 14 #include "content/common/content_export.h" |
| 15 #include "net/base/net_errors.h" |
18 | 16 |
19 struct DownloadCreateInfo; | |
20 class DownloadManager; | 17 class DownloadManager; |
21 | 18 |
22 // These objects live exclusively on the download thread and handle the writing | 19 // These objects live exclusively on the file thread and handle the writing |
23 // operations for one download. These objects live only for the duration that | 20 // operations for one download. These objects live only for the duration that |
24 // the download is 'in progress': once the download has been completed or | 21 // the download is 'in progress': once the download has been completed or |
25 // cancelled, the DownloadFile is destroyed. | 22 // cancelled, the DownloadFile is destroyed. |
26 class CONTENT_EXPORT DownloadFile : public BaseFile { | 23 class CONTENT_EXPORT DownloadFile { |
27 public: | 24 public: |
28 // Takes ownership of the object pointed to by |request_handle|. | 25 virtual ~DownloadFile() {} |
29 DownloadFile(const DownloadCreateInfo* info, | 26 |
30 DownloadRequestHandleInterface* request_handle, | 27 // If calculate_hash is true, sha256 hash will be calculated. |
31 DownloadManager* download_manager); | 28 // Returns net::OK on success, or a network error code on failure. |
32 virtual ~DownloadFile(); | 29 virtual net::Error Initialize(bool calculate_hash) = 0; |
| 30 |
| 31 // Write a new chunk of data to the file. |
| 32 // Returns net::OK on success (all bytes written to the file), |
| 33 // or a network error code on failure. |
| 34 virtual net::Error AppendDataToFile(const char* data, size_t data_len) = 0; |
| 35 |
| 36 // Rename the download file. |
| 37 // Returns net::OK on success, or a network error code on failure. |
| 38 virtual net::Error Rename(const FilePath& full_path) = 0; |
| 39 |
| 40 // Detach the file so it is not deleted on destruction. |
| 41 virtual void Detach() = 0; |
| 42 |
| 43 // Abort the download and automatically close the file. |
| 44 virtual void Cancel() = 0; |
| 45 |
| 46 // Indicate that the download has finished. No new data will be received. |
| 47 virtual void Finish() = 0; |
| 48 |
| 49 // Informs the OS that this file came from the internet. |
| 50 virtual void AnnotateWithSourceInformation() = 0; |
| 51 |
| 52 virtual FilePath FullPath() const = 0; |
| 53 virtual bool InProgress() const = 0; |
| 54 virtual int64 BytesSoFar() const = 0; |
| 55 |
| 56 // Set |hash| with sha256 digest for the file. |
| 57 // Returns true if digest is successfully calculated. |
| 58 virtual bool GetSha256Hash(std::string* hash) = 0; |
33 | 59 |
34 // Cancels the download request associated with this file. | 60 // Cancels the download request associated with this file. |
35 void CancelDownloadRequest(); | 61 virtual void CancelDownloadRequest() = 0; |
36 | 62 |
37 int id() const { return id_.local(); } | 63 virtual int Id() const = 0; |
38 DownloadManager* GetDownloadManager(); | 64 virtual DownloadManager* GetDownloadManager() = 0; |
39 const DownloadId& global_id() const { return id_; } | 65 virtual const DownloadId& GlobalId() const = 0; |
40 | 66 |
41 virtual std::string DebugString() const; | 67 virtual std::string DebugString() const = 0; |
42 | 68 |
43 // Appends the passed the number between parenthesis the path before the | 69 // Appends the passed-in |number| between parenthesis to the |path| before |
44 // extension. | 70 // the file extension. |
45 static void AppendNumberToPath(FilePath* path, int number); | 71 static void AppendNumberToPath(FilePath* path, int number); |
46 | 72 |
47 // Attempts to find a number that can be appended to that path to make it | 73 // Appends the passed-in |suffix| to the |path|. |
| 74 static FilePath AppendSuffixToPath(const FilePath& path, |
| 75 const FilePath::StringType& suffix); |
| 76 |
| 77 // Attempts to find a number that can be appended to the |path| to make it |
48 // unique. If |path| does not exist, 0 is returned. If it fails to find such | 78 // unique. If |path| does not exist, 0 is returned. If it fails to find such |
49 // a number, -1 is returned. | 79 // a number, -1 is returned. |
50 static int GetUniquePathNumber(const FilePath& path); | 80 static int GetUniquePathNumber(const FilePath& path); |
51 | 81 |
52 static FilePath AppendSuffixToPath(const FilePath& path, | |
53 const FilePath::StringType& suffix); | |
54 | |
55 // Same as GetUniquePathNumber, except that it also checks the existence | 82 // Same as GetUniquePathNumber, except that it also checks the existence |
56 // of it with the given suffix. | 83 // of it with the given suffix. |
57 // If |path| does not exist, 0 is returned. If it fails to find such | 84 // If |path| does not exist, 0 is returned. If it fails to find such |
58 // a number, -1 is returned. | 85 // a number, -1 is returned. |
59 static int GetUniquePathNumberWithSuffix( | 86 static int GetUniquePathNumberWithSuffix( |
60 const FilePath& path, | 87 const FilePath& path, |
61 const FilePath::StringType& suffix); | 88 const FilePath::StringType& suffix); |
62 | |
63 private: | |
64 // The unique identifier for this download, assigned at creation by | |
65 // the DownloadFileManager for its internal record keeping. | |
66 DownloadId id_; | |
67 | |
68 // The handle to the request information. Used for operations outside the | |
69 // download system, specifically canceling a download. | |
70 scoped_ptr<DownloadRequestHandleInterface> request_handle_; | |
71 | |
72 // DownloadManager this download belongs to. | |
73 scoped_refptr<DownloadManager> download_manager_; | |
74 | |
75 DISALLOW_COPY_AND_ASSIGN(DownloadFile); | |
76 }; | 89 }; |
77 | 90 |
78 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_H_ | 91 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_H_ |
OLD | NEW |