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_VERIFIER_H_ | 5 #ifndef EXTENSIONS_BROWSER_CONTENT_VERIFIER_H_ |
6 #define EXTENSIONS_BROWSER_CONTENT_VERIFIER_H_ | 6 #define EXTENSIONS_BROWSER_CONTENT_VERIFIER_H_ |
7 | 7 |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/observer_list_threadsafe.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "extensions/browser/content_verifier_filter.h" | |
12 #include "extensions/browser/content_verify_job.h" | 11 #include "extensions/browser/content_verify_job.h" |
13 | 12 |
14 namespace base { | 13 namespace base { |
15 class FilePath; | 14 class FilePath; |
16 } | 15 } |
17 | 16 |
18 namespace content { | 17 namespace content { |
19 class BrowserContext; | 18 class BrowserContext; |
20 } | 19 } |
21 | 20 |
22 namespace extensions { | 21 namespace extensions { |
23 | 22 |
24 // Interface for clients of ContentVerifier. | 23 class Extension; |
25 class ContentVerifierObserver { | 24 class ContentHashFetcher; |
26 public: | 25 class ContentVerifierDelegate; |
27 // Called when the content verifier detects that a read of a file inside | |
28 // an extension did not match its expected hash. | |
29 virtual void ContentVerifyFailed(const std::string& extension_id) = 0; | |
30 }; | |
31 | 26 |
32 // Used for managing overall content verification - both fetching content | 27 // Used for managing overall content verification - both fetching content |
33 // hashes as needed, and supplying job objects to verify file contents as they | 28 // hashes as needed, and supplying job objects to verify file contents as they |
34 // are read. | 29 // are read. |
35 class ContentVerifier : public base::RefCountedThreadSafe<ContentVerifier> { | 30 class ContentVerifier : public base::RefCountedThreadSafe<ContentVerifier> { |
36 public: | 31 public: |
| 32 // Takes ownership of |delegate|. |
37 ContentVerifier(content::BrowserContext* context, | 33 ContentVerifier(content::BrowserContext* context, |
38 const ContentVerifierFilter& filter); | 34 ContentVerifierDelegate* delegate); |
39 void Start(); | 35 void Start(); |
40 void Shutdown(); | 36 void Shutdown(); |
41 | 37 |
42 // Call this before reading a file within an extension. The caller owns the | 38 // Call this before reading a file within an extension. The caller owns the |
43 // returned job. | 39 // returned job. |
44 ContentVerifyJob* CreateJobFor(const std::string& extension_id, | 40 ContentVerifyJob* CreateJobFor(const std::string& extension_id, |
45 const base::FilePath& extension_root, | 41 const base::FilePath& extension_root, |
46 const base::FilePath& relative_path); | 42 const base::FilePath& relative_path); |
47 | 43 |
48 // Called (typically by a verification job) to indicate that verification | 44 // Called (typically by a verification job) to indicate that verification |
49 // failed while reading some file in |extension_id|. | 45 // failed while reading some file in |extension_id|. |
50 void VerifyFailed(const std::string& extension_id, | 46 void VerifyFailed(const std::string& extension_id, |
51 ContentVerifyJob::FailureReason reason); | 47 ContentVerifyJob::FailureReason reason); |
52 | 48 |
53 // Observers will be called back on the same thread that they call | |
54 // AddObserver on. | |
55 void AddObserver(ContentVerifierObserver* observer); | |
56 void RemoveObserver(ContentVerifierObserver* observer); | |
57 | |
58 private: | 49 private: |
59 DISALLOW_COPY_AND_ASSIGN(ContentVerifier); | 50 DISALLOW_COPY_AND_ASSIGN(ContentVerifier); |
60 | 51 |
61 friend class base::RefCountedThreadSafe<ContentVerifier>; | 52 friend class base::RefCountedThreadSafe<ContentVerifier>; |
62 virtual ~ContentVerifier(); | 53 virtual ~ContentVerifier(); |
63 | 54 |
64 // Attempts to fetch content hashes for |extension_id|. | |
65 void RequestFetch(const std::string& extension_id); | |
66 | |
67 // Note that it is important for these to appear in increasing "severity" | 55 // Note that it is important for these to appear in increasing "severity" |
68 // order, because we use this to let command line flags increase, but not | 56 // order, because we use this to let command line flags increase, but not |
69 // decrease, the mode you're running in compared to the experiment group. | 57 // decrease, the mode you're running in compared to the experiment group. |
70 enum Mode { | 58 enum Mode { |
71 // Do not try to fetch content hashes if they are missing, and do not | 59 // Do not try to fetch content hashes if they are missing, and do not |
72 // enforce them if they are present. | 60 // enforce them if they are present. |
73 NONE = 0, | 61 NONE = 0, |
74 | 62 |
75 // If content hashes are missing, try to fetch them, but do not enforce. | 63 // If content hashes are missing, try to fetch them, but do not enforce. |
76 BOOTSTRAP, | 64 BOOTSTRAP, |
77 | 65 |
78 // If hashes are present, enforce them. If they are missing, try to fetch | 66 // If hashes are present, enforce them. If they are missing, try to fetch |
79 // them. | 67 // them. |
80 ENFORCE, | 68 ENFORCE, |
81 | 69 |
82 // Treat the absence of hashes the same as a verification failure. | 70 // Treat the absence of hashes the same as a verification failure. |
83 ENFORCE_STRICT | 71 ENFORCE_STRICT |
84 }; | 72 }; |
85 | 73 |
86 static Mode GetMode(); | 74 static Mode GetMode(); |
87 | 75 |
88 // The mode we're running in - set once at creation. | 76 // The mode we're running in - set once at creation. |
89 const Mode mode_; | 77 const Mode mode_; |
90 | 78 |
91 // The filter we use to decide whether to return a ContentVerifyJob. | |
92 ContentVerifierFilter filter_; | |
93 | |
94 // The associated BrowserContext. | 79 // The associated BrowserContext. |
95 content::BrowserContext* context_; | 80 content::BrowserContext* context_; |
96 | 81 |
97 // The set of objects interested in verification failures. | 82 scoped_ptr<ContentVerifierDelegate> delegate_; |
98 scoped_refptr<ObserverListThreadSafe<ContentVerifierObserver> > observers_; | 83 |
| 84 // For fetching content hash signatures. |
| 85 scoped_ptr<ContentHashFetcher> fetcher_; |
99 }; | 86 }; |
100 | 87 |
101 } // namespace extensions | 88 } // namespace extensions |
102 | 89 |
103 #endif // EXTENSIONS_BROWSER_CONTENT_VERIFIER_H_ | 90 #endif // EXTENSIONS_BROWSER_CONTENT_VERIFIER_H_ |
OLD | NEW |