Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / content / browser / download / base_file.h
blobd1242938b82c5ace41ad7352217a87b3f2509ce4
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_BASE_FILE_H_
6 #define CONTENT_BROWSER_DOWNLOAD_BASE_FILE_H_
8 #include <string>
10 #include "base/files/file_path.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/linked_ptr.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/time.h"
15 #include "content/common/content_export.h"
16 #include "content/public/browser/download_interrupt_reasons.h"
17 #include "googleurl/src/gurl.h"
18 #include "net/base/file_stream.h"
19 #include "net/base/net_errors.h"
20 #include "net/base/net_log.h"
22 namespace crypto {
23 class SecureHash;
25 namespace net {
26 class FileStream;
29 namespace content {
31 // File being downloaded and saved to disk. This is a base class
32 // for DownloadFile and SaveFile, which keep more state information.
33 class CONTENT_EXPORT BaseFile {
34 public:
35 // May be constructed on any thread. All other routines (including
36 // destruction) must occur on the FILE thread.
37 BaseFile(const base::FilePath& full_path,
38 const GURL& source_url,
39 const GURL& referrer_url,
40 int64 received_bytes,
41 bool calculate_hash,
42 const std::string& hash_state,
43 scoped_ptr<net::FileStream> file_stream,
44 const net::BoundNetLog& bound_net_log);
45 virtual ~BaseFile();
47 // Returns DOWNLOAD_INTERRUPT_REASON_NONE on success, or a
48 // DownloadInterruptReason on failure. |default_directory| specifies the
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
51 // be created in the default download location as determined by
52 // ContentBrowserClient.
53 DownloadInterruptReason Initialize(const base::FilePath& default_directory);
55 // Write a new chunk of data to the file. Returns a DownloadInterruptReason
56 // indicating the result of the operation.
57 DownloadInterruptReason AppendDataToFile(const char* data, size_t data_len);
59 // Rename the download file. Returns a DownloadInterruptReason indicating the
60 // result of the operation.
61 virtual DownloadInterruptReason Rename(const base::FilePath& full_path);
63 // Detach the file so it is not deleted on destruction.
64 virtual void Detach();
66 // Abort the download and automatically close the file.
67 void Cancel();
69 // Indicate that the download has finished. No new data will be received.
70 void Finish();
72 // Informs the OS that this file came from the internet. Returns a
73 // DownloadInterruptReason indicating the result of the operation.
74 DownloadInterruptReason AnnotateWithSourceInformation();
76 base::FilePath full_path() const { return full_path_; }
77 bool in_progress() const { return file_stream_.get() != NULL; }
78 int64 bytes_so_far() const { return bytes_so_far_; }
80 // Fills |hash| with the hash digest for the file.
81 // Returns true if digest is successfully calculated.
82 virtual bool GetHash(std::string* hash);
84 // Returns the current (intermediate) state of the hash as a byte string.
85 virtual std::string GetHashState();
87 // Returns true if the given hash is considered empty. An empty hash is
88 // a string of size kSha256HashLen that contains only zeros (initial value
89 // for the hash).
90 static bool IsEmptyHash(const std::string& hash);
92 virtual std::string DebugString() const;
94 private:
95 friend class BaseFileTest;
96 FRIEND_TEST_ALL_PREFIXES(BaseFileTest, IsEmptyHash);
98 // Re-initializes file_stream_ with a newly allocated net::FileStream().
99 void CreateFileStream();
101 // Creates and opens the file_stream_ if it is NULL.
102 DownloadInterruptReason Open();
104 // Closes and resets file_stream_.
105 void Close();
107 // Resets file_stream_.
108 void ClearStream();
110 // Platform specific method that moves a file to a new path and adjusts the
111 // security descriptor / permissions on the file to match the defaults for the
112 // new directory.
113 DownloadInterruptReason MoveFileAndAdjustPermissions(
114 const base::FilePath& new_path);
116 // Split out from CurrentSpeed to enable testing.
117 int64 CurrentSpeedAtTime(base::TimeTicks current_time) const;
119 // Log a TYPE_DOWNLOAD_FILE_ERROR NetLog event with |error| and passes error
120 // on through, converting to a |DownloadInterruptReason|.
121 DownloadInterruptReason LogNetError(const char* operation, net::Error error);
123 // Log the system error in |os_error| and converts it into a
124 // |DownloadInterruptReason|.
125 DownloadInterruptReason LogSystemError(const char* operation, int os_error);
127 // Log a TYPE_DOWNLOAD_FILE_ERROR NetLog event with |os_error| and |reason|.
128 // Returns |reason|.
129 DownloadInterruptReason LogInterruptReason(
130 const char* operation, int os_error,
131 DownloadInterruptReason reason);
133 static const size_t kSha256HashLen = 32;
134 static const unsigned char kEmptySha256Hash[kSha256HashLen];
136 // Full path to the file including the file name.
137 base::FilePath full_path_;
139 // Source URL for the file being downloaded.
140 GURL source_url_;
142 // The URL where the download was initiated.
143 GURL referrer_url_;
145 // OS file stream for writing
146 scoped_ptr<net::FileStream> file_stream_;
148 // Amount of data received up so far, in bytes.
149 int64 bytes_so_far_;
151 // Start time for calculating speed.
152 base::TimeTicks start_tick_;
154 // Indicates if hash should be calculated for the file.
155 bool calculate_hash_;
157 // Used to calculate hash for the file when calculate_hash_
158 // is set.
159 scoped_ptr<crypto::SecureHash> secure_hash_;
161 unsigned char sha256_hash_[kSha256HashLen];
163 // Indicates that this class no longer owns the associated file, and so
164 // won't delete it on destruction.
165 bool detached_;
167 net::BoundNetLog bound_net_log_;
169 DISALLOW_COPY_AND_ASSIGN(BaseFile);
172 } // namespace content
174 #endif // CONTENT_BROWSER_DOWNLOAD_BASE_FILE_H_