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
3 // found in the LICENSE file.
5 #ifndef EXTENSIONS_BROWSER_CONTENT_VERIFY_JOB_H_
6 #define EXTENSIONS_BROWSER_CONTENT_VERIFY_JOB_H_
10 #include "base/callback.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/threading/thread_checker.h"
23 namespace extensions
{
25 class ContentHashReader
;
27 // Objects of this class are responsible for verifying that the actual content
28 // read from an extension file matches an expected set of hashes. This class
29 // can be created on any thread but the rest of the methods should be called
30 // from only one thread.
31 class ContentVerifyJob
: public base::RefCountedThreadSafe
<ContentVerifyJob
> {
37 // Failed because there were no expected hashes at all (eg they haven't
41 // Failed because this file wasn't found in the list of expected hashes.
44 // Some of the content read did not match the expected hash.
47 typedef base::Callback
<void(FailureReason
)> FailureCallback
;
49 // The |failure_callback| will be called at most once if there was a failure.
50 ContentVerifyJob(ContentHashReader
* hash_reader
,
51 const FailureCallback
& failure_callback
);
53 // This begins the process of getting expected hashes, so it should be called
54 // as early as possible.
57 // Call this to add more bytes to verify. If at any point the read bytes
58 // don't match the expected hashes, this will dispatch the failure
59 // callback. The failure callback will only be run once even if more bytes
60 // are read. Make sure to call DoneReading so that any final bytes that were
61 // read that didn't align exactly on a block size boundary get their hash
63 void BytesRead(int count
, const char* data
);
65 // Call once when finished adding bytes via BytesRead.
70 // These methods will be called inside BytesRead/DoneReading respectively.
71 // If either return something other than NONE, then the failure callback
72 // will be dispatched with that reason.
73 virtual FailureReason
BytesRead(const std::string
& extension_id
,
75 const char* data
) = 0;
76 virtual FailureReason
DoneReading(const std::string
& extension_id
) = 0;
79 static void SetDelegateForTests(TestDelegate
* delegate
);
82 DISALLOW_COPY_AND_ASSIGN(ContentVerifyJob
);
84 virtual ~ContentVerifyJob();
85 friend class base::RefCountedThreadSafe
<ContentVerifyJob
>;
87 // Called each time we're done adding bytes for the current block, and are
88 // ready to finish the hash operation for those bytes and make sure it
89 // matches what was expected for that block. Returns true if everything is
90 // still ok so far, or false if a mismatch was detected.
93 // Dispatches the failure callback with the given reason.
94 void DispatchFailureCallback(FailureReason reason
);
96 // Called when our ContentHashReader has finished initializing.
97 void OnHashesReady(bool success
);
99 // Indicates whether the caller has told us they are done calling BytesRead.
102 // Set to true once hash_reader_ has read its expected hashes.
105 // While we're waiting for the callback from the ContentHashReader, we need
106 // to queue up bytes any bytes that are read.
109 // The total bytes we've read.
110 int64 total_bytes_read_
;
112 // The index of the block we're currently on.
115 // The hash we're building up for the bytes of |current_block_|.
116 scoped_ptr
<crypto::SecureHash
> current_hash_
;
118 // The number of bytes we've already input into |current_hash_|.
119 int current_hash_byte_count_
;
121 scoped_refptr
<ContentHashReader
> hash_reader_
;
123 base::TimeDelta time_spent_
;
125 // Called once if verification fails.
126 FailureCallback failure_callback_
;
128 // Set to true if we detected a mismatch and called the failure callback.
131 // For ensuring methods on called on the right thread.
132 base::ThreadChecker thread_checker_
;
135 } // namespace extensions
137 #endif // EXTENSIONS_BROWSER_CONTENT_VERIFY_JOB_H_