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_CONTENT_HASH_READER_H_ 6 #define EXTENSIONS_BROWSER_CONTENT_HASH_READER_H_ 7 8 #include <string> 9 #include <vector> 10 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" 16 17 namespace extensions { 18 19 class VerifiedContents; 20 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> { 25 public: 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); 33 34 const std::string& extension_id() const { return extension_id_; } 35 const base::FilePath& relative_path() const { return relative_path_; } 36 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. 41 bool Init(); 42 43 // These return whether we found valid verified_contents.json / 44 // computed_hashes.json files respectively. Note that both of these can be 45 // true but we still didn't find an entry for |relative_path_| in them. 46 bool have_verified_contents() { return have_verified_contents_; } 47 bool have_computed_hashes() { return have_computed_hashes_; } 48 49 // Return the number of blocks and block size, respectively. Only valid after 50 // calling Init(). 51 int block_count() const; 52 int block_size() const; 53 54 // Returns a pointer to the expected sha256 hash value for the block at the 55 // given index. Only valid after calling Init(). 56 bool GetHashForBlock(int block_index, const std::string** result) const; 57 58 private: 59 friend class base::RefCountedThreadSafe<ContentHashReader>; 60 virtual ~ContentHashReader(); 61 62 enum InitStatus { NOT_INITIALIZED, SUCCESS, FAILURE }; 63 64 std::string extension_id_; 65 base::Version extension_version_; 66 base::FilePath extension_root_; 67 base::FilePath relative_path_; 68 ContentVerifierKey key_; 69 70 InitStatus status_; 71 72 bool have_verified_contents_; 73 bool have_computed_hashes_; 74 75 // The blocksize used for generating the hashes. 76 int block_size_; 77 78 scoped_ptr<VerifiedContents> verified_contents_; 79 80 std::vector<std::string> hashes_; 81 82 DISALLOW_COPY_AND_ASSIGN(ContentHashReader); 83 }; 84 85 } // namespace extensions 86 87 #endif // EXTENSIONS_BROWSER_CONTENT_HASH_READER_H_ 88