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.
5 #ifndef EXTENSIONS_BROWSER_CONTENT_HASH_READER_H_
6 #define EXTENSIONS_BROWSER_CONTENT_HASH_READER_H_
11 #include "base/files/file_path.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/version.h"
15 #include "extensions/browser/content_verifier_delegate.h"
17 namespace extensions
{
19 class VerifiedContents
;
21 // This class creates an object that will read expected hashes that may have
22 // been fetched/calculated by the ContentHashFetcher, and vends them out for
23 // use in ContentVerifyJob's.
24 class ContentHashReader
: public base::RefCountedThreadSafe
<ContentHashReader
> {
26 // Create one of these to get expected hashes for the file at |relative_path|
27 // within an extension.
28 ContentHashReader(const std::string
& extension_id
,
29 const base::Version
& extension_version
,
30 const base::FilePath
& extension_root
,
31 const base::FilePath
& relative_path
,
32 const ContentVerifierKey
& key
);
34 const std::string
& extension_id() const { return extension_id_
; }
35 const base::FilePath
& relative_path() const { return relative_path_
; }
37 // This should be called to initialize this object (reads the expected hashes
38 // from storage, etc.). Must be called on a thread that is allowed to do file
39 // I/O. Returns a boolean indicating success/failure. On failure, this object
40 // should likely be discarded.
43 // Return the number of blocks and block size, respectively. Only valid after
45 int block_count() const;
46 int block_size() const;
48 // Returns a pointer to the expected sha256 hash value for the block at the
49 // given index. Only valid after calling Init().
50 bool GetHashForBlock(int block_index
, const std::string
** result
) const;
53 friend class base::RefCountedThreadSafe
<ContentHashReader
>;
54 virtual ~ContentHashReader();
56 enum InitStatus
{ NOT_INITIALIZED
, SUCCESS
, FAILURE
};
58 std::string extension_id_
;
59 base::Version extension_version_
;
60 base::FilePath extension_root_
;
61 base::FilePath relative_path_
;
62 ContentVerifierKey key_
;
66 // The blocksize used for generating the hashes.
69 scoped_ptr
<VerifiedContents
> verified_contents_
;
71 std::vector
<std::string
> hashes_
;
73 DISALLOW_COPY_AND_ASSIGN(ContentHashReader
);
76 } // namespace extensions
78 #endif // EXTENSIONS_BROWSER_CONTENT_HASH_READER_H_