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_VERIFY_JOB_H_ | 5 #ifndef EXTENSIONS_BROWSER_CONTENT_VERIFY_JOB_H_ |
6 #define EXTENSIONS_BROWSER_CONTENT_VERIFY_JOB_H_ | 6 #define EXTENSIONS_BROWSER_CONTENT_VERIFY_JOB_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/threading/thread_checker.h" | 13 #include "base/threading/thread_checker.h" |
14 | 14 |
15 namespace base { | 15 namespace base { |
16 class FilePath; | 16 class FilePath; |
17 } | 17 } |
18 | 18 |
| 19 namespace crypto { |
| 20 class SecureHash; |
| 21 } |
| 22 |
19 namespace extensions { | 23 namespace extensions { |
20 | 24 |
21 class ContentHashReader; | 25 class ContentHashReader; |
22 | 26 |
23 // Objects of this class are responsible for verifying that the actual content | 27 // Objects of this class are responsible for verifying that the actual content |
24 // read from an extension file matches an expected set of hashes. This class | 28 // read from an extension file matches an expected set of hashes. This class |
25 // can be created on any thread but the rest of the methods should be called | 29 // can be created on any thread but the rest of the methods should be called |
26 // from only one thread. | 30 // from only one thread. |
27 class ContentVerifyJob : public base::RefCountedThreadSafe<ContentVerifyJob> { | 31 class ContentVerifyJob : public base::RefCountedThreadSafe<ContentVerifyJob> { |
28 public: | 32 public: |
29 enum FailureReason { | 33 enum FailureReason { |
30 // No failure. | 34 // No failure. |
31 NONE, | 35 NONE, |
32 | 36 |
33 // Failed because there were no expected hashes. | 37 // Failed because there were no expected hashes. |
34 NO_HASHES, | 38 NO_HASHES, |
35 | 39 |
36 // Some of the content read did not match the expected hash. | 40 // Some of the content read did not match the expected hash. |
37 HASH_MISMATCH | 41 HASH_MISMATCH |
38 }; | 42 }; |
39 typedef base::Callback<void(FailureReason)> FailureCallback; | 43 typedef base::Callback<void(FailureReason)> FailureCallback; |
40 | 44 |
41 // The |failure_callback| will be called at most once if there was a failure. | 45 // The |failure_callback| will be called at most once if there was a failure. |
42 // | 46 ContentVerifyJob(ContentHashReader* hash_reader, |
43 // IMPORTANT NOTE: this class is still a stub right now - in the future this | |
44 // constructor will also be passed information to let it lookup expected | |
45 // block hashes for the file being read. | |
46 ContentVerifyJob(const std::string& extension_id, | |
47 const FailureCallback& failure_callback); | 47 const FailureCallback& failure_callback); |
48 | 48 |
49 // This begins the process of getting expected hashes, so it should be called | 49 // This begins the process of getting expected hashes, so it should be called |
50 // as early as possible. | 50 // as early as possible. |
51 void Start(); | 51 void Start(); |
52 | 52 |
53 // Call this to add more bytes to verify. If at any point the read bytes | 53 // Call this to add more bytes to verify. If at any point the read bytes |
54 // don't match the expected hashes, this will dispatch the failure | 54 // don't match the expected hashes, this will dispatch the failure |
55 // callback. The failure callback will only be run once even if more bytes | 55 // callback. The failure callback will only be run once even if more bytes |
56 // are read. Make sure to call DoneReading so that any final bytes that were | 56 // are read. Make sure to call DoneReading so that any final bytes that were |
(...skipping 16 matching lines...) Expand all Loading... |
73 }; | 73 }; |
74 | 74 |
75 static void SetDelegateForTests(TestDelegate* delegate); | 75 static void SetDelegateForTests(TestDelegate* delegate); |
76 | 76 |
77 private: | 77 private: |
78 DISALLOW_COPY_AND_ASSIGN(ContentVerifyJob); | 78 DISALLOW_COPY_AND_ASSIGN(ContentVerifyJob); |
79 | 79 |
80 virtual ~ContentVerifyJob(); | 80 virtual ~ContentVerifyJob(); |
81 friend class base::RefCountedThreadSafe<ContentVerifyJob>; | 81 friend class base::RefCountedThreadSafe<ContentVerifyJob>; |
82 | 82 |
| 83 // Called each time we're done adding bytes for the current block, and are |
| 84 // ready to finish the hash operation for those bytes and make sure it matches |
| 85 // what was expected for that block. |
| 86 void FinishBlock(); |
| 87 |
| 88 // Dispatches the failure callback with the given reason. |
83 void DispatchFailureCallback(FailureReason reason); | 89 void DispatchFailureCallback(FailureReason reason); |
84 | 90 |
85 // The id of the extension for the file being verified. | 91 // Called when our ContentHashReader has finished initializing. |
86 std::string extension_id_; | 92 void OnHashesReady(bool success); |
| 93 |
| 94 // Indicates whether the caller has told us they are done calling BytesRead. |
| 95 bool done_reading_; |
| 96 |
| 97 // Set to true once hash_reader_ has read its expected hashes. |
| 98 bool hashes_ready_; |
| 99 |
| 100 // While we're waiting for the callback from the ContentHashReader, we need |
| 101 // to queue up bytes any bytes that are read. |
| 102 std::string queue_; |
| 103 |
| 104 // The total bytes we've read. |
| 105 int64 total_bytes_read_; |
| 106 |
| 107 // The index of the block we're currently on. |
| 108 int current_block_; |
| 109 |
| 110 // The hash we're building up for the bytes of |current_block_|. |
| 111 scoped_ptr<crypto::SecureHash> current_hash_; |
| 112 |
| 113 // The number of bytes we've already input into |current_hash_|. |
| 114 int current_hash_byte_count_; |
| 115 |
| 116 scoped_refptr<ContentHashReader> hash_reader_; |
87 | 117 |
88 // Called once if verification fails. | 118 // Called once if verification fails. |
89 FailureCallback failure_callback_; | 119 FailureCallback failure_callback_; |
90 | 120 |
91 // For ensuring methods on called on the right thread. | 121 // For ensuring methods on called on the right thread. |
92 base::ThreadChecker thread_checker_; | 122 base::ThreadChecker thread_checker_; |
93 }; | 123 }; |
94 | 124 |
95 } // namespace extensions | 125 } // namespace extensions |
96 | 126 |
97 #endif // EXTENSIONS_BROWSER_CONTENT_VERIFY_JOB_H_ | 127 #endif // EXTENSIONS_BROWSER_CONTENT_VERIFY_JOB_H_ |
OLD | NEW |