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_FETCHER_H_ 6 #define EXTENSIONS_BROWSER_CONTENT_HASH_FETCHER_H_ 7 8 #include <set> 9 #include <string> 10 11 #include "base/callback.h" 12 #include "base/files/file_path.h" 13 #include "base/memory/weak_ptr.h" 14 #include "extensions/common/extension.h" 15 16 namespace content { 17 class BrowserContext; 18 } 19 20 namespace extensions { 21 22 class ExtensionRegistry; 23 class ContentHashFetcherJob; 24 class ContentVerifierDelegate; 25 26 // This class is responsible for getting signed expected hashes for use in 27 // extension content verification. As extensions are loaded it will fetch and 28 // parse/validate/cache this data as needed, including calculating expected 29 // hashes for each block of each file within an extension. (These unsigned leaf 30 // node block level hashes will always be checked at time of use use to make 31 // sure they match the signed treehash root hash). 32 class ContentHashFetcher { 33 public: 34 // A callback for when a fetch is complete. This reports back: 35 // -extension id 36 // -whether we were successful or not (have verified_contents.json and 37 // -computed_hashes.json files) 38 // -was it a forced check? 39 // -a set of paths whose contents didn't match expected values 40 typedef base::Callback< 41 void(const std::string&, bool, bool, const std::set<base::FilePath>&)> 42 FetchCallback; 43 44 // The consumer of this class needs to ensure that context and delegate 45 // outlive this object. 46 ContentHashFetcher(content::BrowserContext* context, 47 ContentVerifierDelegate* delegate, 48 const FetchCallback& callback); 49 virtual ~ContentHashFetcher(); 50 51 // Explicitly ask to fetch hashes for |extension|. If |force| is true, 52 // we will always check the validity of the verified_contents.json and 53 // re-check the contents of the files in the filesystem. 54 void DoFetch(const Extension* extension, bool force); 55 56 // These should be called when an extension is loaded or unloaded. 57 virtual void ExtensionLoaded(const Extension* extension); 58 virtual void ExtensionUnloaded(const Extension* extension); 59 60 private: 61 // Callback for when a job getting content hashes has completed. 62 void JobFinished(ContentHashFetcherJob* job); 63 64 content::BrowserContext* context_; 65 ContentVerifierDelegate* delegate_; 66 FetchCallback fetch_callback_; 67 68 // We keep around pointers to in-progress jobs, both so we can avoid 69 // scheduling duplicate work if fetching is already in progress, and so that 70 // we can cancel in-progress work at shutdown time. 71 typedef std::pair<ExtensionId, std::string> IdAndVersion; 72 typedef std::map<IdAndVersion, scoped_refptr<ContentHashFetcherJob> > JobMap; 73 JobMap jobs_; 74 75 // Used for binding callbacks passed to jobs. 76 base::WeakPtrFactory<ContentHashFetcher> weak_ptr_factory_; 77 78 DISALLOW_COPY_AND_ASSIGN(ContentHashFetcher); 79 }; 80 81 } // namespace extensions 82 83 #endif // EXTENSIONS_BROWSER_CONTENT_HASH_FETCHER_H_ 84