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" |
11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
12 #include "base/memory/linked_ptr.h" | 12 #include "base/memory/linked_ptr.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/time.h" | 14 #include "base/time.h" |
15 #include "content/common/content_export.h" | 15 #include "content/common/content_export.h" |
| 16 #include "content/public/browser/download_interrupt_reasons.h" |
16 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
17 #include "net/base/file_stream.h" | 18 #include "net/base/file_stream.h" |
18 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
19 #include "net/base/net_log.h" | 20 #include "net/base/net_log.h" |
20 | 21 |
| 22 namespace content { |
| 23 enum DownloadInterruptReason; |
| 24 } |
21 namespace crypto { | 25 namespace crypto { |
22 class SecureHash; | 26 class SecureHash; |
23 } | 27 } |
24 namespace net { | 28 namespace net { |
25 class FileStream; | 29 class FileStream; |
26 } | 30 } |
27 | 31 |
28 // File being downloaded and saved to disk. This is a base class | 32 // File being downloaded and saved to disk. This is a base class |
29 // for DownloadFile and SaveFile, which keep more state information. | 33 // for DownloadFile and SaveFile, which keep more state information. |
30 class CONTENT_EXPORT BaseFile { | 34 class CONTENT_EXPORT BaseFile { |
31 public: | 35 public: |
32 // May be constructed on any thread. All other routines (including | 36 // May be constructed on any thread. All other routines (including |
33 // destruction) must occur on the FILE thread. | 37 // destruction) must occur on the FILE thread. |
34 BaseFile(const FilePath& full_path, | 38 BaseFile(const FilePath& full_path, |
35 const GURL& source_url, | 39 const GURL& source_url, |
36 const GURL& referrer_url, | 40 const GURL& referrer_url, |
37 int64 received_bytes, | 41 int64 received_bytes, |
38 bool calculate_hash, | 42 bool calculate_hash, |
39 const std::string& hash_state, | 43 const std::string& hash_state, |
40 scoped_ptr<net::FileStream> file_stream, | 44 scoped_ptr<net::FileStream> file_stream, |
41 const net::BoundNetLog& bound_net_log); | 45 const net::BoundNetLog& bound_net_log); |
42 virtual ~BaseFile(); | 46 virtual ~BaseFile(); |
43 | 47 |
44 // Returns net::OK on success, or a network error code on failure. | 48 // Returns DOWNLOAD_INTERRUPT_REASON_NONE on success, or a |
45 // |default_directory| specifies the directory to create the temporary file in | 49 // DownloadInterruptReason on failure. |default_directory| specifies the |
46 // if |full_path()| is empty. If |default_directory| and |full_path()| are | 50 // directory to create the temporary file in if |full_path()| is empty. If |
47 // empty, then a temporary file will be created in the default download | 51 // |default_directory| and |full_path()| are empty, then a temporary file will |
48 // location as determined by ContentBrowserClient. | 52 // be created in the default download location as determined by |
49 net::Error Initialize(const FilePath& default_directory); | 53 // ContentBrowserClient. |
| 54 content::DownloadInterruptReason Initialize( |
| 55 const FilePath& default_directory); |
50 | 56 |
51 // Write a new chunk of data to the file. | 57 // Write a new chunk of data to the file. Returns a DownloadInterruptReason |
52 // Returns net::OK on success (all bytes written to the file), | 58 // indicating the result of the operation. |
53 // or a network error code on failure. | 59 content::DownloadInterruptReason AppendDataToFile(const char* data, |
54 net::Error AppendDataToFile(const char* data, size_t data_len); | 60 size_t data_len); |
55 | 61 |
56 // Rename the download file. | 62 // Rename the download file. Returns a DownloadInterruptReason indicating the |
57 // Returns net::OK on success, or a network error code on failure. | 63 // result of the operation. |
58 virtual net::Error Rename(const FilePath& full_path); | 64 virtual content::DownloadInterruptReason Rename(const FilePath& full_path); |
59 | 65 |
60 // Detach the file so it is not deleted on destruction. | 66 // Detach the file so it is not deleted on destruction. |
61 virtual void Detach(); | 67 virtual void Detach(); |
62 | 68 |
63 // Abort the download and automatically close the file. | 69 // Abort the download and automatically close the file. |
64 void Cancel(); | 70 void Cancel(); |
65 | 71 |
66 // Indicate that the download has finished. No new data will be received. | 72 // Indicate that the download has finished. No new data will be received. |
67 void Finish(); | 73 void Finish(); |
68 | 74 |
(...skipping 14 matching lines...) Expand all Loading... |
83 // Returns the current (intermediate) state of the hash as a byte string. | 89 // Returns the current (intermediate) state of the hash as a byte string. |
84 virtual std::string GetHashState(); | 90 virtual std::string GetHashState(); |
85 | 91 |
86 // Returns true if the given hash is considered empty. An empty hash is | 92 // Returns true if the given hash is considered empty. An empty hash is |
87 // a string of size kSha256HashLen that contains only zeros (initial value | 93 // a string of size kSha256HashLen that contains only zeros (initial value |
88 // for the hash). | 94 // for the hash). |
89 static bool IsEmptyHash(const std::string& hash); | 95 static bool IsEmptyHash(const std::string& hash); |
90 | 96 |
91 virtual std::string DebugString() const; | 97 virtual std::string DebugString() const; |
92 | 98 |
93 protected: | |
94 virtual void CreateFileStream(); // For testing. | |
95 // Returns net::OK on success, or a network error code on failure. | |
96 net::Error Open(); | |
97 void Close(); | |
98 | |
99 // Full path to the file including the file name. | |
100 FilePath full_path_; | |
101 | |
102 private: | 99 private: |
103 friend class BaseFileTest; | 100 friend class BaseFileTest; |
104 FRIEND_TEST_ALL_PREFIXES(BaseFileTest, IsEmptyHash); | 101 FRIEND_TEST_ALL_PREFIXES(BaseFileTest, IsEmptyHash); |
105 | 102 |
| 103 // Re-initializes file_stream_ with a newly allocated net::FileStream(). |
| 104 void CreateFileStream(); |
| 105 |
| 106 // Creates and opens the file_stream_ if it is NULL. |
| 107 content::DownloadInterruptReason Open(); |
| 108 |
| 109 // Closes and resets file_stream_. |
| 110 void Close(); |
| 111 |
| 112 // Resets file_stream_. |
| 113 void ClearStream(); |
| 114 |
| 115 // Platform specific method that moves a file to a new path and adjusts the |
| 116 // security descriptor / permissions on the file to match the defaults for the |
| 117 // new directory. |
| 118 content::DownloadInterruptReason MoveFileAndAdjustPermissions( |
| 119 const FilePath& new_path); |
| 120 |
106 // Split out from CurrentSpeed to enable testing. | 121 // Split out from CurrentSpeed to enable testing. |
107 int64 CurrentSpeedAtTime(base::TimeTicks current_time) const; | 122 int64 CurrentSpeedAtTime(base::TimeTicks current_time) const; |
108 | 123 |
109 // Resets the current state of the hash to the contents of |hash_state_bytes|. | 124 // Log a TYPE_DOWNLOAD_FILE_ERROR NetLog event with |error| and passes error |
110 virtual bool SetHashState(const std::string& hash_state_bytes); | 125 // on through, converting to a |content::DownloadInterruptReason|. |
| 126 content::DownloadInterruptReason LogNetError( |
| 127 const char* operation, net::Error error); |
111 | 128 |
112 net::Error ClearStream(net::Error error); | 129 // Log the system error in |os_error| and converts it into a |
| 130 // |content::DownloadInterruptReason|. |
| 131 content::DownloadInterruptReason LogSystemError( |
| 132 const char* operation, int os_error); |
| 133 |
| 134 // Log a TYPE_DOWNLOAD_FILE_ERROR NetLog event with |os_error| and |reason|. |
| 135 // Returns |reason|. |
| 136 content::DownloadInterruptReason LogInterruptReason( |
| 137 const char* operation, int os_error, |
| 138 content::DownloadInterruptReason reason); |
113 | 139 |
114 static const size_t kSha256HashLen = 32; | 140 static const size_t kSha256HashLen = 32; |
115 static const unsigned char kEmptySha256Hash[kSha256HashLen]; | 141 static const unsigned char kEmptySha256Hash[kSha256HashLen]; |
116 | 142 |
| 143 // Full path to the file including the file name. |
| 144 FilePath full_path_; |
| 145 |
117 // Source URL for the file being downloaded. | 146 // Source URL for the file being downloaded. |
118 GURL source_url_; | 147 GURL source_url_; |
119 | 148 |
120 // The URL where the download was initiated. | 149 // The URL where the download was initiated. |
121 GURL referrer_url_; | 150 GURL referrer_url_; |
122 | 151 |
123 // OS file stream for writing | 152 // OS file stream for writing |
124 scoped_ptr<net::FileStream> file_stream_; | 153 scoped_ptr<net::FileStream> file_stream_; |
125 | 154 |
126 // Amount of data received up so far, in bytes. | 155 // Amount of data received up so far, in bytes. |
(...skipping 14 matching lines...) Expand all Loading... |
141 // Indicates that this class no longer owns the associated file, and so | 170 // Indicates that this class no longer owns the associated file, and so |
142 // won't delete it on destruction. | 171 // won't delete it on destruction. |
143 bool detached_; | 172 bool detached_; |
144 | 173 |
145 net::BoundNetLog bound_net_log_; | 174 net::BoundNetLog bound_net_log_; |
146 | 175 |
147 DISALLOW_COPY_AND_ASSIGN(BaseFile); | 176 DISALLOW_COPY_AND_ASSIGN(BaseFile); |
148 }; | 177 }; |
149 | 178 |
150 #endif // CONTENT_BROWSER_DOWNLOAD_BASE_FILE_H_ | 179 #endif // CONTENT_BROWSER_DOWNLOAD_BASE_FILE_H_ |
OLD | NEW |