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