summaryrefslogtreecommitdiffstats
path: root/extensions/browser/computed_hashes.h
blob: 071abf08767c62d2d69bc882d917d84f606a8da5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef EXTENSIONS_BROWSER_COMPUTED_HASHES_H_
#define EXTENSIONS_BROWSER_COMPUTED_HASHES_H_

#include <stddef.h>

#include <map>
#include <string>
#include <vector>

#include "base/memory/scoped_ptr.h"

namespace base {
class FilePath;
class ListValue;
}

namespace extensions {

// A pair of classes for serialization of a set of SHA256 block hashes computed
// over the files inside an extension.
class ComputedHashes {
 public:
  class Reader {
   public:
    Reader();
    ~Reader();
    bool InitFromFile(const base::FilePath& path);

    // The block size and hashes for |relative_path| will be copied into the
    // out parameters.
    bool GetHashes(const base::FilePath& relative_path,
                   int* block_size,
                   std::vector<std::string>* hashes);

   private:
    typedef std::pair<int, std::vector<std::string> > HashInfo;

    // This maps a relative path to a pair of (block size, hashes)
    std::map<base::FilePath, HashInfo> data_;
  };

  class Writer {
   public:
    Writer();
    ~Writer();

    // Adds hashes for |relative_path|. Should not be called more than once
    // for a given |relative_path|.
    void AddHashes(const base::FilePath& relative_path,
                   int block_size,
                   const std::vector<std::string>& hashes);

    bool WriteToFile(const base::FilePath& path);

   private:
    // Each element of this list contains the path and block hashes for one
    // file.
    scoped_ptr<base::ListValue> file_list_;
  };

  // Computes the SHA256 hash of each |block_size| chunk in |contents|, placing
  // the results into |hashes|.
  static void ComputeHashesForContent(const std::string& contents,
                                      size_t block_size,
                                      std::vector<std::string>* hashes);
};

}  // namespace extensions

#endif  // EXTENSIONS_BROWSER_COMPUTED_HASHES_H_