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