[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 1 | // Copyright 2014 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. |
| 4 | |
| 5 | #ifndef EXTENSIONS_BROWSER_COMPUTED_HASHES_H_ |
| 6 | #define EXTENSIONS_BROWSER_COMPUTED_HASHES_H_ |
| 7 | |
avi | c9cec10 | 2015-12-23 00:39:26 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 10 | #include <map> |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 11 | #include <memory> |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <vector> |
| 14 | |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 15 | |
| 16 | namespace base { |
| 17 | class FilePath; |
[email protected] | de00aeb | 2014-08-06 09:13:39 | [diff] [blame] | 18 | class ListValue; |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | namespace extensions { |
| 22 | |
| 23 | // A pair of classes for serialization of a set of SHA256 block hashes computed |
| 24 | // over the files inside an extension. |
| 25 | class ComputedHashes { |
| 26 | public: |
| 27 | class Reader { |
| 28 | public: |
| 29 | Reader(); |
| 30 | ~Reader(); |
| 31 | bool InitFromFile(const base::FilePath& path); |
| 32 | |
| 33 | // The block size and hashes for |relative_path| will be copied into the |
| 34 | // out parameters. |
| 35 | bool GetHashes(const base::FilePath& relative_path, |
| 36 | int* block_size, |
Istiaque Ahmed | 9bdd9d9 | 2017-12-16 04:53:27 | [diff] [blame] | 37 | std::vector<std::string>* hashes) const; |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 38 | |
| 39 | private: |
| 40 | typedef std::pair<int, std::vector<std::string> > HashInfo; |
| 41 | |
| 42 | // This maps a relative path to a pair of (block size, hashes) |
| 43 | std::map<base::FilePath, HashInfo> data_; |
| 44 | }; |
| 45 | |
| 46 | class Writer { |
| 47 | public: |
| 48 | Writer(); |
| 49 | ~Writer(); |
| 50 | |
| 51 | // Adds hashes for |relative_path|. Should not be called more than once |
| 52 | // for a given |relative_path|. |
| 53 | void AddHashes(const base::FilePath& relative_path, |
| 54 | int block_size, |
| 55 | const std::vector<std::string>& hashes); |
| 56 | |
| 57 | bool WriteToFile(const base::FilePath& path); |
| 58 | |
| 59 | private: |
[email protected] | de00aeb | 2014-08-06 09:13:39 | [diff] [blame] | 60 | // Each element of this list contains the path and block hashes for one |
| 61 | // file. |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 62 | std::unique_ptr<base::ListValue> file_list_; |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 63 | }; |
[email protected] | de00aeb | 2014-08-06 09:13:39 | [diff] [blame] | 64 | |
| 65 | // Computes the SHA256 hash of each |block_size| chunk in |contents|, placing |
| 66 | // the results into |hashes|. |
| 67 | static void ComputeHashesForContent(const std::string& contents, |
| 68 | size_t block_size, |
| 69 | std::vector<std::string>* hashes); |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | } // namespace extensions |
| 73 | |
| 74 | #endif // EXTENSIONS_BROWSER_COMPUTED_HASHES_H_ |