Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(451)

Side by Side Diff: chrome/browser/download/base_file.h

Issue 6023006: Add support to sha256 hash the downloaded file.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 CHROME_BROWSER_DOWNLOAD_BASE_FILE_H_ 5 #ifndef CHROME_BROWSER_DOWNLOAD_BASE_FILE_H_
6 #define CHROME_BROWSER_DOWNLOAD_BASE_FILE_H_ 6 #define CHROME_BROWSER_DOWNLOAD_BASE_FILE_H_
7 #pragma once 7 #pragma once
8 8
9 #include <string> 9 #include <string>
10 10
11 #include "base/file_path.h" 11 #include "base/file_path.h"
12 #include "base/linked_ptr.h" 12 #include "base/linked_ptr.h"
13 #include "base/scoped_ptr.h"
13 #include "chrome/browser/power_save_blocker.h" 14 #include "chrome/browser/power_save_blocker.h"
14 #include "googleurl/src/gurl.h" 15 #include "googleurl/src/gurl.h"
15 16
16 namespace net { 17 namespace net {
17 class FileStream; 18 class FileStream;
18 } 19 }
19 20
21 struct SHA256ContextStr;
22 typedef struct SHA256ContextStr SHA256Context;
Paweł Hajdan Jr. 2011/01/11 08:28:21 nit: Why do we need this typedef? It seems to add
lzheng 2011/01/12 02:11:54 I agree it looks confusing. Since the original SHA
23
20 // File being downloaded and saved to disk. This is a base class 24 // File being downloaded and saved to disk. This is a base class
21 // for DownloadFile and SaveFile, which keep more state information. 25 // for DownloadFile and SaveFile, which keep more state information.
22 class BaseFile { 26 class BaseFile {
23 public: 27 public:
24 BaseFile(const FilePath& full_path, 28 BaseFile(const FilePath& full_path,
25 const GURL& source_url, 29 const GURL& source_url,
26 const GURL& referrer_url, 30 const GURL& referrer_url,
27 int64 received_bytes, 31 int64 received_bytes,
28 const linked_ptr<net::FileStream>& file_stream); 32 const linked_ptr<net::FileStream>& file_stream,
33 bool calculate_hash);
Randy Smith (Not in Mondays) 2011/01/11 18:29:51 A tacky question, but I at least want to ask: What
lzheng 2011/01/12 02:11:54 Answered at the beginning of this email. On 2011/
29 ~BaseFile(); 34 ~BaseFile();
30 35
31 bool Initialize(); 36 bool Initialize();
32 37
33 // Write a new chunk of data to the file. Returns true on success (all bytes 38 // Write a new chunk of data to the file. Returns true on success (all bytes
34 // written to the file). 39 // written to the file).
35 bool AppendDataToFile(const char* data, size_t data_len); 40 bool AppendDataToFile(const char* data, size_t data_len);
36 41
37 // Rename the download file. Returns true on success. 42 // Rename the download file. Returns true on success.
38 // |path_renamed_| is set to true only if |is_final_rename| is true. 43 // |path_renamed_| is set to true only if |is_final_rename| is true.
39 // Marked virtual for testing. 44 // Marked virtual for testing.
40 virtual bool Rename(const FilePath& full_path, bool is_final_rename); 45 virtual bool Rename(const FilePath& full_path, bool is_final_rename);
41 46
42 // Abort the download and automatically close the file. 47 // Abort the download and automatically close the file.
43 void Cancel(); 48 void Cancel();
44 49
45 // Indicate that the download has finished. No new data will be received. 50 // Indicate that the download has finished. No new data will be received.
46 void Finish(); 51 void Finish();
47 52
48 // Informs the OS that this file came from the internet. 53 // Informs the OS that this file came from the internet.
49 void AnnotateWithSourceInformation(); 54 void AnnotateWithSourceInformation();
50 55
51 FilePath full_path() const { return full_path_; } 56 FilePath full_path() const { return full_path_; }
52 bool path_renamed() const { return path_renamed_; } 57 bool path_renamed() const { return path_renamed_; }
53 bool in_progress() const { return file_stream_ != NULL; } 58 bool in_progress() const { return file_stream_ != NULL; }
54 int64 bytes_so_far() const { return bytes_so_far_; } 59 int64 bytes_so_far() const { return bytes_so_far_; }
55 60
61 // Set |hash| with sha256 digest for the file. The |hash| would be a
62 // lower-case hex string. Returns false if the digest is not calcuated.
Paweł Hajdan Jr. 2011/01/11 08:28:21 nit: How about "Returns true on success" to avoid
Randy Smith (Not in Mondays) 2011/01/11 18:29:51 At what point will the digest be calculated? Is i
lzheng 2011/01/12 02:11:54 Done.
lzheng 2011/01/12 02:11:54 It should be. I added verification for the in_prog
63 virtual bool GetSha256Hash(std::string* hash);
64
56 virtual std::string DebugString() const; 65 virtual std::string DebugString() const;
57 66
58 protected: 67 protected:
59 bool Open(); 68 bool Open();
60 void Close(); 69 void Close();
61 70
62 // Full path to the file including the file name. 71 // Full path to the file including the file name.
63 FilePath full_path_; 72 FilePath full_path_;
64 73
65 // Whether the download is still using its initial temporary path. 74 // Whether the download is still using its initial temporary path.
66 bool path_renamed_; 75 bool path_renamed_;
67 76
68 private: 77 private:
78 static const size_t kSha256HashLen_ = 32;
Paweł Hajdan Jr. 2011/01/11 08:28:21 nit: The underscore at the end looks weird, I thin
lzheng 2011/01/12 02:11:54 Done.
79
69 // Source URL for the file being downloaded. 80 // Source URL for the file being downloaded.
70 GURL source_url_; 81 GURL source_url_;
71 82
72 // The URL where the download was initiated. 83 // The URL where the download was initiated.
73 GURL referrer_url_; 84 GURL referrer_url_;
74 85
75 // OS file stream for writing 86 // OS file stream for writing
76 linked_ptr<net::FileStream> file_stream_; 87 linked_ptr<net::FileStream> file_stream_;
77 88
78 // Amount of data received up so far, in bytes. 89 // Amount of data received up so far, in bytes.
79 int64 bytes_so_far_; 90 int64 bytes_so_far_;
80 91
81 // RAII handle to keep the system from sleeping while we're downloading. 92 // RAII handle to keep the system from sleeping while we're downloading.
82 PowerSaveBlocker power_save_blocker_; 93 PowerSaveBlocker power_save_blocker_;
83 94
95 // Indicates if sha256 hash should be calculated for the downloaded file.
96 bool calculate_hash_;
97
98 scoped_ptr<SHA256Context> sha_context_;
Paweł Hajdan Jr. 2011/01/11 08:28:21 nit: Could you add a short comment?
lzheng 2011/01/12 02:11:54 Done.
99
100 unsigned char sha256_hash_[kSha256HashLen_];
101
84 DISALLOW_COPY_AND_ASSIGN(BaseFile); 102 DISALLOW_COPY_AND_ASSIGN(BaseFile);
85 }; 103 };
86 104
87 #endif // CHROME_BROWSER_DOWNLOAD_BASE_FILE_H_ 105 #endif // CHROME_BROWSER_DOWNLOAD_BASE_FILE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698