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.
49 typedef base::Callback
<void(FailureReason
)> FailureCallback
;
51 // The |failure_callback| will be called at most once if there was a failure.
52 ContentVerifyJob(ContentHashReader
* hash_reader
,
53 const FailureCallback
& failure_callback
);
55 // This begins the process of getting expected hashes, so it should be called
56 // as early as possible.
59 // Call this to add more bytes to verify. If at any point the read bytes
60 // don't match the expected hashes, this will dispatch the failure
61 // callback. The failure callback will only be run once even if more bytes
62 // are read. Make sure to call DoneReading so that any final bytes that were
63 // read that didn't align exactly on a block size boundary get their hash
65 void BytesRead(int count
, const char* data
);
67 // Call once when finished adding bytes via BytesRead.
72 // These methods will be called inside BytesRead/DoneReading respectively.
73 // If either return something other than NONE, then the failure callback
74 // will be dispatched with that reason.
75 virtual FailureReason
BytesRead(const std::string
& extension_id
,
77 const char* data
) = 0;
78 virtual FailureReason
DoneReading(const std::string
& extension_id
) = 0;
83 virtual void JobStarted(const std::string
& extension_id
,
84 const base::FilePath
& relative_path
) = 0;
86 virtual void JobFinished(const std::string
& extension_id
,
87 const base::FilePath
& relative_path
,
91 static void SetDelegateForTests(TestDelegate
* delegate
);
92 static void SetObserverForTests(TestObserver
* observer
);
95 DISALLOW_COPY_AND_ASSIGN(ContentVerifyJob
);
97 virtual ~ContentVerifyJob();
98 friend class base::RefCountedThreadSafe
<ContentVerifyJob
>;
100 // Called each time we're done adding bytes for the current block, and are
101 // ready to finish the hash operation for those bytes and make sure it
102 // matches what was expected for that block. Returns true if everything is
103 // still ok so far, or false if a mismatch was detected.
106 // Dispatches the failure callback with the given reason.
107 void DispatchFailureCallback(FailureReason reason
);
109 // Called when our ContentHashReader has finished initializing.
110 void OnHashesReady(bool success
);
112 // Indicates whether the caller has told us they are done calling BytesRead.
115 // Set to true once hash_reader_ has read its expected hashes.
118 // While we're waiting for the callback from the ContentHashReader, we need
119 // to queue up bytes any bytes that are read.
122 // The total bytes we've read.
123 int64 total_bytes_read_
;
125 // The index of the block we're currently on.
128 // The hash we're building up for the bytes of |current_block_|.
129 scoped_ptr
<crypto::SecureHash
> current_hash_
;
131 // The number of bytes we've already input into |current_hash_|.
132 int current_hash_byte_count_
;
134 scoped_refptr
<ContentHashReader
> hash_reader_
;
136 base::TimeDelta time_spent_
;
138 // Called once if verification fails.
139 FailureCallback failure_callback_
;
141 // Set to true if we detected a mismatch and called the failure callback.
144 // For ensuring methods on called on the right thread.
145 base::ThreadChecker thread_checker_
;
148 } // namespace extensions
150 #endif // EXTENSIONS_BROWSER_CONTENT_VERIFY_JOB_H_