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 |
69 // Informs the OS that this file came from the internet. | 75 // Informs the OS that this file came from the internet. Returns a |
70 void AnnotateWithSourceInformation(); | 76 // DownloadInterruptReason indicating the result of the operation. |
77 content::DownloadInterruptReason AnnotateWithSourceInformation(); | |
Randy Smith (Not in Mondays)
2012/10/23 20:18:13
I don't object, but why? Nothing appears to retur
asanka
2012/10/23 21:53:34
Done.
| |
71 | 78 |
72 // Calculate and return the current speed in bytes per second. | 79 // Calculate and return the current speed in bytes per second. |
73 int64 CurrentSpeed() const; | 80 int64 CurrentSpeed() const; |
74 | 81 |
75 FilePath full_path() const { return full_path_; } | 82 FilePath full_path() const { return full_path_; } |
76 bool in_progress() const { return file_stream_.get() != NULL; } | 83 bool in_progress() const { return file_stream_.get() != NULL; } |
77 int64 bytes_so_far() const { return bytes_so_far_; } | 84 int64 bytes_so_far() const { return bytes_so_far_; } |
78 | 85 |
79 // Fills |hash| with the hash digest for the file. | 86 // Fills |hash| with the hash digest for the file. |
80 // Returns true if digest is successfully calculated. | 87 // Returns true if digest is successfully calculated. |
81 virtual bool GetHash(std::string* hash); | 88 virtual bool GetHash(std::string* hash); |
82 | 89 |
83 // Returns the current (intermediate) state of the hash as a byte string. | 90 // Returns the current (intermediate) state of the hash as a byte string. |
84 virtual std::string GetHashState(); | 91 virtual std::string GetHashState(); |
85 | 92 |
86 // Returns true if the given hash is considered empty. An empty hash is | 93 // 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 | 94 // a string of size kSha256HashLen that contains only zeros (initial value |
88 // for the hash). | 95 // for the hash). |
89 static bool IsEmptyHash(const std::string& hash); | 96 static bool IsEmptyHash(const std::string& hash); |
90 | 97 |
91 virtual std::string DebugString() const; | 98 virtual std::string DebugString() const; |
92 | 99 |
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: | 100 private: |
103 friend class BaseFileTest; | 101 friend class BaseFileTest; |
104 FRIEND_TEST_ALL_PREFIXES(BaseFileTest, IsEmptyHash); | 102 FRIEND_TEST_ALL_PREFIXES(BaseFileTest, IsEmptyHash); |
105 | 103 |
104 // Re-initializes file_stream_ with a newly allocated net::FileStream(). | |
105 void CreateFileStream(); | |
106 | |
107 // Creates and opens the file_stream_ if it is NULL. | |
108 content::DownloadInterruptReason Open(); | |
109 | |
110 // Closes and resets file_stream_. | |
111 void Close(); | |
112 | |
113 // Resets file_stream_. | |
114 void ClearStream(); | |
115 | |
116 // Platform specific method that moves a file to a new path and adjusts the | |
117 // security descriptor / permissions on the file to match the defaults for the | |
118 // new directory. | |
119 content::DownloadInterruptReason MoveFileAndAdjustPermissions( | |
120 const FilePath& new_path); | |
121 | |
106 // Split out from CurrentSpeed to enable testing. | 122 // Split out from CurrentSpeed to enable testing. |
107 int64 CurrentSpeedAtTime(base::TimeTicks current_time) const; | 123 int64 CurrentSpeedAtTime(base::TimeTicks current_time) const; |
108 | 124 |
109 // Resets the current state of the hash to the contents of |hash_state_bytes|. | 125 // Resets the current state of the hash to the contents of |hash_state_bytes|. |
110 virtual bool SetHashState(const std::string& hash_state_bytes); | 126 virtual bool SetHashState(const std::string& hash_state_bytes); |
111 | 127 |
112 net::Error ClearStream(net::Error error); | 128 // Log a TYPE_DOWNLOAD_FILE_ERROR NetLog event with |error| and passes error |
129 // on through, converting to a |content::DownloadInterruptReason|. | |
130 content::DownloadInterruptReason LogNetError( | |
131 const char* operation, net::Error error); | |
132 | |
133 // Log a TYPE_DOWNLOAD_FILE_ERROR NetLog event with |os_error| and |reason|. | |
134 // Returns |reason|. | |
135 content::DownloadInterruptReason LogInterruptReason( | |
136 const char* operation, int os_error, | |
Randy Smith (Not in Mondays)
2012/10/23 20:18:13
Why associate the os_error logging with the Downlo
asanka
2012/10/23 21:53:34
It's based on usage. When we have a net::Error we
| |
137 content::DownloadInterruptReason reason); | |
113 | 138 |
114 static const size_t kSha256HashLen = 32; | 139 static const size_t kSha256HashLen = 32; |
115 static const unsigned char kEmptySha256Hash[kSha256HashLen]; | 140 static const unsigned char kEmptySha256Hash[kSha256HashLen]; |
116 | 141 |
142 // Full path to the file including the file name. | |
143 FilePath full_path_; | |
144 | |
117 // Source URL for the file being downloaded. | 145 // Source URL for the file being downloaded. |
118 GURL source_url_; | 146 GURL source_url_; |
119 | 147 |
120 // The URL where the download was initiated. | 148 // The URL where the download was initiated. |
121 GURL referrer_url_; | 149 GURL referrer_url_; |
122 | 150 |
123 // OS file stream for writing | 151 // OS file stream for writing |
124 scoped_ptr<net::FileStream> file_stream_; | 152 scoped_ptr<net::FileStream> file_stream_; |
125 | 153 |
126 // Amount of data received up so far, in bytes. | 154 // 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 | 169 // Indicates that this class no longer owns the associated file, and so |
142 // won't delete it on destruction. | 170 // won't delete it on destruction. |
143 bool detached_; | 171 bool detached_; |
144 | 172 |
145 net::BoundNetLog bound_net_log_; | 173 net::BoundNetLog bound_net_log_; |
146 | 174 |
147 DISALLOW_COPY_AND_ASSIGN(BaseFile); | 175 DISALLOW_COPY_AND_ASSIGN(BaseFile); |
148 }; | 176 }; |
149 | 177 |
150 #endif // CONTENT_BROWSER_DOWNLOAD_BASE_FILE_H_ | 178 #endif // CONTENT_BROWSER_DOWNLOAD_BASE_FILE_H_ |
OLD | NEW |