OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_BASE_FILE_H_ | 5 #ifndef CONTENT_BROWSER_DOWNLOAD_BASE_FILE_H_ |
6 #define CONTENT_BROWSER_DOWNLOAD_BASE_FILE_H_ | 6 #define CONTENT_BROWSER_DOWNLOAD_BASE_FILE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 } | 27 } |
28 | 28 |
29 namespace content { | 29 namespace content { |
30 | 30 |
31 // File being downloaded and saved to disk. This is a base class | 31 // File being downloaded and saved to disk. This is a base class |
32 // for DownloadFile and SaveFile, which keep more state information. | 32 // for DownloadFile and SaveFile, which keep more state information. |
33 class CONTENT_EXPORT BaseFile { | 33 class CONTENT_EXPORT BaseFile { |
34 public: | 34 public: |
35 // May be constructed on any thread. All other routines (including | 35 // May be constructed on any thread. All other routines (including |
36 // destruction) must occur on the FILE thread. | 36 // destruction) must occur on the FILE thread. |
37 BaseFile(const FilePath& full_path, | 37 BaseFile(const base::FilePath& full_path, |
38 const GURL& source_url, | 38 const GURL& source_url, |
39 const GURL& referrer_url, | 39 const GURL& referrer_url, |
40 int64 received_bytes, | 40 int64 received_bytes, |
41 bool calculate_hash, | 41 bool calculate_hash, |
42 const std::string& hash_state, | 42 const std::string& hash_state, |
43 scoped_ptr<net::FileStream> file_stream, | 43 scoped_ptr<net::FileStream> file_stream, |
44 const net::BoundNetLog& bound_net_log); | 44 const net::BoundNetLog& bound_net_log); |
45 virtual ~BaseFile(); | 45 virtual ~BaseFile(); |
46 | 46 |
47 // Returns DOWNLOAD_INTERRUPT_REASON_NONE on success, or a | 47 // Returns DOWNLOAD_INTERRUPT_REASON_NONE on success, or a |
48 // DownloadInterruptReason on failure. |default_directory| specifies the | 48 // DownloadInterruptReason on failure. |default_directory| specifies the |
49 // directory to create the temporary file in if |full_path()| is empty. If | 49 // directory to create the temporary file in if |full_path()| is empty. If |
50 // |default_directory| and |full_path()| are empty, then a temporary file will | 50 // |default_directory| and |full_path()| are empty, then a temporary file will |
51 // be created in the default download location as determined by | 51 // be created in the default download location as determined by |
52 // ContentBrowserClient. | 52 // ContentBrowserClient. |
53 DownloadInterruptReason Initialize(const FilePath& default_directory); | 53 DownloadInterruptReason Initialize(const base::FilePath& default_directory); |
54 | 54 |
55 // Write a new chunk of data to the file. Returns a DownloadInterruptReason | 55 // Write a new chunk of data to the file. Returns a DownloadInterruptReason |
56 // indicating the result of the operation. | 56 // indicating the result of the operation. |
57 DownloadInterruptReason AppendDataToFile(const char* data, size_t data_len); | 57 DownloadInterruptReason AppendDataToFile(const char* data, size_t data_len); |
58 | 58 |
59 // Rename the download file. Returns a DownloadInterruptReason indicating the | 59 // Rename the download file. Returns a DownloadInterruptReason indicating the |
60 // result of the operation. | 60 // result of the operation. |
61 virtual DownloadInterruptReason Rename(const FilePath& full_path); | 61 virtual DownloadInterruptReason Rename(const base::FilePath& full_path); |
62 | 62 |
63 // Detach the file so it is not deleted on destruction. | 63 // Detach the file so it is not deleted on destruction. |
64 virtual void Detach(); | 64 virtual void Detach(); |
65 | 65 |
66 // Abort the download and automatically close the file. | 66 // Abort the download and automatically close the file. |
67 void Cancel(); | 67 void Cancel(); |
68 | 68 |
69 // Indicate that the download has finished. No new data will be received. | 69 // Indicate that the download has finished. No new data will be received. |
70 void Finish(); | 70 void Finish(); |
71 | 71 |
72 // Informs the OS that this file came from the internet. Returns a | 72 // Informs the OS that this file came from the internet. Returns a |
73 // DownloadInterruptReason indicating the result of the operation. | 73 // DownloadInterruptReason indicating the result of the operation. |
74 DownloadInterruptReason AnnotateWithSourceInformation(); | 74 DownloadInterruptReason AnnotateWithSourceInformation(); |
75 | 75 |
76 // Calculate and return the current speed in bytes per second. | 76 // Calculate and return the current speed in bytes per second. |
77 int64 CurrentSpeed() const; | 77 int64 CurrentSpeed() const; |
78 | 78 |
79 FilePath full_path() const { return full_path_; } | 79 base::FilePath full_path() const { return full_path_; } |
80 bool in_progress() const { return file_stream_.get() != NULL; } | 80 bool in_progress() const { return file_stream_.get() != NULL; } |
81 int64 bytes_so_far() const { return bytes_so_far_; } | 81 int64 bytes_so_far() const { return bytes_so_far_; } |
82 | 82 |
83 // Fills |hash| with the hash digest for the file. | 83 // Fills |hash| with the hash digest for the file. |
84 // Returns true if digest is successfully calculated. | 84 // Returns true if digest is successfully calculated. |
85 virtual bool GetHash(std::string* hash); | 85 virtual bool GetHash(std::string* hash); |
86 | 86 |
87 // Returns the current (intermediate) state of the hash as a byte string. | 87 // Returns the current (intermediate) state of the hash as a byte string. |
88 virtual std::string GetHashState(); | 88 virtual std::string GetHashState(); |
89 | 89 |
(...skipping 17 matching lines...) Expand all Loading... |
107 // Closes and resets file_stream_. | 107 // Closes and resets file_stream_. |
108 void Close(); | 108 void Close(); |
109 | 109 |
110 // Resets file_stream_. | 110 // Resets file_stream_. |
111 void ClearStream(); | 111 void ClearStream(); |
112 | 112 |
113 // Platform specific method that moves a file to a new path and adjusts the | 113 // Platform specific method that moves a file to a new path and adjusts the |
114 // security descriptor / permissions on the file to match the defaults for the | 114 // security descriptor / permissions on the file to match the defaults for the |
115 // new directory. | 115 // new directory. |
116 DownloadInterruptReason MoveFileAndAdjustPermissions( | 116 DownloadInterruptReason MoveFileAndAdjustPermissions( |
117 const FilePath& new_path); | 117 const base::FilePath& new_path); |
118 | 118 |
119 // Split out from CurrentSpeed to enable testing. | 119 // Split out from CurrentSpeed to enable testing. |
120 int64 CurrentSpeedAtTime(base::TimeTicks current_time) const; | 120 int64 CurrentSpeedAtTime(base::TimeTicks current_time) const; |
121 | 121 |
122 // Log a TYPE_DOWNLOAD_FILE_ERROR NetLog event with |error| and passes error | 122 // Log a TYPE_DOWNLOAD_FILE_ERROR NetLog event with |error| and passes error |
123 // on through, converting to a |DownloadInterruptReason|. | 123 // on through, converting to a |DownloadInterruptReason|. |
124 DownloadInterruptReason LogNetError(const char* operation, net::Error error); | 124 DownloadInterruptReason LogNetError(const char* operation, net::Error error); |
125 | 125 |
126 // Log the system error in |os_error| and converts it into a | 126 // Log the system error in |os_error| and converts it into a |
127 // |DownloadInterruptReason|. | 127 // |DownloadInterruptReason|. |
128 DownloadInterruptReason LogSystemError(const char* operation, int os_error); | 128 DownloadInterruptReason LogSystemError(const char* operation, int os_error); |
129 | 129 |
130 // Log a TYPE_DOWNLOAD_FILE_ERROR NetLog event with |os_error| and |reason|. | 130 // Log a TYPE_DOWNLOAD_FILE_ERROR NetLog event with |os_error| and |reason|. |
131 // Returns |reason|. | 131 // Returns |reason|. |
132 DownloadInterruptReason LogInterruptReason( | 132 DownloadInterruptReason LogInterruptReason( |
133 const char* operation, int os_error, | 133 const char* operation, int os_error, |
134 DownloadInterruptReason reason); | 134 DownloadInterruptReason reason); |
135 | 135 |
136 static const size_t kSha256HashLen = 32; | 136 static const size_t kSha256HashLen = 32; |
137 static const unsigned char kEmptySha256Hash[kSha256HashLen]; | 137 static const unsigned char kEmptySha256Hash[kSha256HashLen]; |
138 | 138 |
139 // Full path to the file including the file name. | 139 // Full path to the file including the file name. |
140 FilePath full_path_; | 140 base::FilePath full_path_; |
141 | 141 |
142 // Source URL for the file being downloaded. | 142 // Source URL for the file being downloaded. |
143 GURL source_url_; | 143 GURL source_url_; |
144 | 144 |
145 // The URL where the download was initiated. | 145 // The URL where the download was initiated. |
146 GURL referrer_url_; | 146 GURL referrer_url_; |
147 | 147 |
148 // OS file stream for writing | 148 // OS file stream for writing |
149 scoped_ptr<net::FileStream> file_stream_; | 149 scoped_ptr<net::FileStream> file_stream_; |
150 | 150 |
(...skipping 17 matching lines...) Expand all Loading... |
168 bool detached_; | 168 bool detached_; |
169 | 169 |
170 net::BoundNetLog bound_net_log_; | 170 net::BoundNetLog bound_net_log_; |
171 | 171 |
172 DISALLOW_COPY_AND_ASSIGN(BaseFile); | 172 DISALLOW_COPY_AND_ASSIGN(BaseFile); |
173 }; | 173 }; |
174 | 174 |
175 } // namespace content | 175 } // namespace content |
176 | 176 |
177 #endif // CONTENT_BROWSER_DOWNLOAD_BASE_FILE_H_ | 177 #endif // CONTENT_BROWSER_DOWNLOAD_BASE_FILE_H_ |
OLD | NEW |