Home | History | Annotate | Download | only in browser
      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   // Indicates whether the content in question exists in the local extension
     44   // installation. This may be |false| if Init fails.
     45   bool content_exists() const { return content_exists_; }
     46 
     47   // These return whether we found valid verified_contents.json /
     48   // computed_hashes.json files respectively. Note that both of these can be
     49   // true but we still didn't find an entry for |relative_path_| in them.
     50   bool have_verified_contents() const { return have_verified_contents_; }
     51   bool have_computed_hashes() const { return have_computed_hashes_; }
     52 
     53   // Return the number of blocks and block size, respectively. Only valid after
     54   // calling Init().
     55   int block_count() const;
     56   int block_size() const;
     57 
     58   // Returns a pointer to the expected sha256 hash value for the block at the
     59   // given index. Only valid after calling Init().
     60   bool GetHashForBlock(int block_index, const std::string** result) const;
     61 
     62  private:
     63   friend class base::RefCountedThreadSafe<ContentHashReader>;
     64   virtual ~ContentHashReader();
     65 
     66   enum InitStatus { NOT_INITIALIZED, SUCCESS, FAILURE };
     67 
     68   std::string extension_id_;
     69   base::Version extension_version_;
     70   base::FilePath extension_root_;
     71   base::FilePath relative_path_;
     72   ContentVerifierKey key_;
     73 
     74   InitStatus status_;
     75 
     76   bool content_exists_;
     77 
     78   bool have_verified_contents_;
     79   bool have_computed_hashes_;
     80 
     81   // The blocksize used for generating the hashes.
     82   int block_size_;
     83 
     84   scoped_ptr<VerifiedContents> verified_contents_;
     85 
     86   std::vector<std::string> hashes_;
     87 
     88   DISALLOW_COPY_AND_ASSIGN(ContentHashReader);
     89 };
     90 
     91 }  // namespace extensions
     92 
     93 #endif  // EXTENSIONS_BROWSER_CONTENT_HASH_READER_H_
     94