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