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