Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / extensions / browser / computed_hashes.h
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
8 #include <map>
9 #include <string>
10 #include <vector>
11
12 #include "base/memory/scoped_ptr.h"
13
14 namespace base {
15 class FilePath;
16 class ListValue;
17 }
18
19 namespace extensions {
20
21 // A pair of classes for serialization of a set of SHA256 block hashes computed
22 // over the files inside an extension.
23 class ComputedHashes {
24  public:
25   class Reader {
26    public:
27     Reader();
28     ~Reader();
29     bool InitFromFile(const base::FilePath& path);
30
31     // The block size and hashes for |relative_path| will be copied into the
32     // out parameters.
33     bool GetHashes(const base::FilePath& relative_path,
34                    int* block_size,
35                    std::vector<std::string>* hashes);
36
37    private:
38     typedef std::pair<int, std::vector<std::string> > HashInfo;
39
40     // This maps a relative path to a pair of (block size, hashes)
41     std::map<base::FilePath, HashInfo> data_;
42   };
43
44   class Writer {
45    public:
46     Writer();
47     ~Writer();
48
49     // Adds hashes for |relative_path|. Should not be called more than once
50     // for a given |relative_path|.
51     void AddHashes(const base::FilePath& relative_path,
52                    int block_size,
53                    const std::vector<std::string>& hashes);
54
55     bool WriteToFile(const base::FilePath& path);
56
57    private:
58     // Each element of this list contains the path and block hashes for one
59     // file.
60     scoped_ptr<base::ListValue> file_list_;
61   };
62
63   // Computes the SHA256 hash of each |block_size| chunk in |contents|, placing
64   // the results into |hashes|.
65   static void ComputeHashesForContent(const std::string& contents,
66                                       size_t block_size,
67                                       std::vector<std::string>* hashes);
68 };
69
70 }  // namespace extensions
71
72 #endif  // EXTENSIONS_BROWSER_COMPUTED_HASHES_H_