| 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: |
| 37 ContentVerifier(content::BrowserContext* context, | 32 ContentVerifier(content::BrowserContext* context, |
| 38 const ContentVerifierFilter& filter); | 33 ContentVerifierDelegate* delegate); |
| 39 void Start(); | 34 void Start(); |
| 40 void Shutdown(); | 35 void Shutdown(); |
| 41 | 36 |
| 42 // Call this before reading a file within an extension. The caller owns the | 37 // Call this before reading a file within an extension. The caller owns the |
| 43 // returned job. | 38 // returned job. |
| 44 ContentVerifyJob* CreateJobFor(const std::string& extension_id, | 39 ContentVerifyJob* CreateJobFor(const std::string& extension_id, |
| 45 const base::FilePath& extension_root, | 40 const base::FilePath& extension_root, |
| 46 const base::FilePath& relative_path); | 41 const base::FilePath& relative_path); |
| 47 | 42 |
| 48 // Called (typically by a verification job) to indicate that verification | 43 // Called (typically by a verification job) to indicate that verification |
| 49 // failed while reading some file in |extension_id|. | 44 // failed while reading some file in |extension_id|. |
| 50 void VerifyFailed(const std::string& extension_id, | 45 void VerifyFailed(const std::string& extension_id, |
| 51 ContentVerifyJob::FailureReason reason); | 46 ContentVerifyJob::FailureReason reason); |
| 52 | 47 |
| 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: | 48 private: |
| 59 DISALLOW_COPY_AND_ASSIGN(ContentVerifier); | 49 DISALLOW_COPY_AND_ASSIGN(ContentVerifier); |
| 60 | 50 |
| 61 friend class base::RefCountedThreadSafe<ContentVerifier>; | 51 friend class base::RefCountedThreadSafe<ContentVerifier>; |
| 62 virtual ~ContentVerifier(); | 52 virtual ~ContentVerifier(); |
| 63 | 53 |
| 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" | 54 // 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 | 55 // 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. | 56 // decrease, the mode you're running in compared to the experiment group. |
| 70 enum Mode { | 57 enum Mode { |
| 71 // Do not try to fetch content hashes if they are missing, and do not | 58 // Do not try to fetch content hashes if they are missing, and do not |
| 72 // enforce them if they are present. | 59 // enforce them if they are present. |
| 73 NONE = 0, | 60 NONE = 0, |
| 74 | 61 |
| 75 // If content hashes are missing, try to fetch them, but do not enforce. | 62 // If content hashes are missing, try to fetch them, but do not enforce. |
| 76 BOOTSTRAP, | 63 BOOTSTRAP, |
| 77 | 64 |
| 78 // If hashes are present, enforce them. If they are missing, try to fetch | 65 // If hashes are present, enforce them. If they are missing, try to fetch |
| 79 // them. | 66 // them. |
| 80 ENFORCE, | 67 ENFORCE, |
| 81 | 68 |
| 82 // Treat the absence of hashes the same as a verification failure. | 69 // Treat the absence of hashes the same as a verification failure. |
| 83 ENFORCE_STRICT | 70 ENFORCE_STRICT |
| 84 }; | 71 }; |
| 85 | 72 |
| 86 static Mode GetMode(); | 73 static Mode GetMode(); |
| 87 | 74 |
| 88 // The mode we're running in - set once at creation. | 75 // The mode we're running in - set once at creation. |
| 89 const Mode mode_; | 76 const Mode mode_; |
| 90 | 77 |
| 91 // The filter we use to decide whether to return a ContentVerifyJob. | |
| 92 ContentVerifierFilter filter_; | |
| 93 | |
| 94 // The associated BrowserContext. | 78 // The associated BrowserContext. |
| 95 content::BrowserContext* context_; | 79 content::BrowserContext* context_; |
| 96 | 80 |
| 97 // The set of objects interested in verification failures. | 81 // Unowned pointer to our delegate. |
| 98 scoped_refptr<ObserverListThreadSafe<ContentVerifierObserver> > observers_; | 82 ContentVerifierDelegate* delegate_; |
| 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 |