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 #include "extensions/browser/content_verify_job.h" | 5 #include "extensions/browser/content_verify_job.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
11 #include "base/task_runner_util.h" | 11 #include "base/task_runner_util.h" |
12 #include "base/timer/elapsed_timer.h" | 12 #include "base/timer/elapsed_timer.h" |
13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
14 #include "crypto/secure_hash.h" | 14 #include "crypto/secure_hash.h" |
15 #include "crypto/sha2.h" | 15 #include "crypto/sha2.h" |
16 #include "extensions/browser/content_hash_reader.h" | 16 #include "extensions/browser/content_hash_reader.h" |
17 | 17 |
18 namespace extensions { | 18 namespace extensions { |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 ContentVerifyJob::TestDelegate* g_test_delegate = NULL; | 22 ContentVerifyJob::TestDelegate* g_test_delegate = NULL; |
| 23 ContentVerifyJob::TestObserver* g_test_observer = NULL; |
23 | 24 |
24 class ScopedElapsedTimer { | 25 class ScopedElapsedTimer { |
25 public: | 26 public: |
26 ScopedElapsedTimer(base::TimeDelta* total) : total_(total) { DCHECK(total_); } | 27 ScopedElapsedTimer(base::TimeDelta* total) : total_(total) { DCHECK(total_); } |
27 | 28 |
28 ~ScopedElapsedTimer() { *total_ += timer.Elapsed(); } | 29 ~ScopedElapsedTimer() { *total_ += timer.Elapsed(); } |
29 | 30 |
30 private: | 31 private: |
31 // Some total amount of time we should add our elapsed time to at | 32 // Some total amount of time we should add our elapsed time to at |
32 // destruction. | 33 // destruction. |
(...skipping 20 matching lines...) Expand all Loading... |
53 thread_checker_.DetachFromThread(); | 54 thread_checker_.DetachFromThread(); |
54 } | 55 } |
55 | 56 |
56 ContentVerifyJob::~ContentVerifyJob() { | 57 ContentVerifyJob::~ContentVerifyJob() { |
57 UMA_HISTOGRAM_COUNTS("ExtensionContentVerifyJob.TimeSpentUS", | 58 UMA_HISTOGRAM_COUNTS("ExtensionContentVerifyJob.TimeSpentUS", |
58 time_spent_.InMicroseconds()); | 59 time_spent_.InMicroseconds()); |
59 } | 60 } |
60 | 61 |
61 void ContentVerifyJob::Start() { | 62 void ContentVerifyJob::Start() { |
62 DCHECK(thread_checker_.CalledOnValidThread()); | 63 DCHECK(thread_checker_.CalledOnValidThread()); |
| 64 if (g_test_observer) |
| 65 g_test_observer->JobStarted(hash_reader_->extension_id(), |
| 66 hash_reader_->relative_path()); |
63 base::PostTaskAndReplyWithResult( | 67 base::PostTaskAndReplyWithResult( |
64 content::BrowserThread::GetBlockingPool(), | 68 content::BrowserThread::GetBlockingPool(), |
65 FROM_HERE, | 69 FROM_HERE, |
66 base::Bind(&ContentHashReader::Init, hash_reader_), | 70 base::Bind(&ContentHashReader::Init, hash_reader_), |
67 base::Bind(&ContentVerifyJob::OnHashesReady, this)); | 71 base::Bind(&ContentVerifyJob::OnHashesReady, this)); |
68 } | 72 } |
69 | 73 |
70 void ContentVerifyJob::BytesRead(int count, const char* data) { | 74 void ContentVerifyJob::BytesRead(int count, const char* data) { |
71 ScopedElapsedTimer timer(&time_spent_); | 75 ScopedElapsedTimer timer(&time_spent_); |
72 DCHECK(thread_checker_.CalledOnValidThread()); | 76 DCHECK(thread_checker_.CalledOnValidThread()); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 FailureReason reason = | 127 FailureReason reason = |
124 g_test_delegate->DoneReading(hash_reader_->extension_id()); | 128 g_test_delegate->DoneReading(hash_reader_->extension_id()); |
125 if (reason != NONE) { | 129 if (reason != NONE) { |
126 DispatchFailureCallback(reason); | 130 DispatchFailureCallback(reason); |
127 return; | 131 return; |
128 } | 132 } |
129 } | 133 } |
130 done_reading_ = true; | 134 done_reading_ = true; |
131 if (hashes_ready_ && !FinishBlock()) | 135 if (hashes_ready_ && !FinishBlock()) |
132 DispatchFailureCallback(HASH_MISMATCH); | 136 DispatchFailureCallback(HASH_MISMATCH); |
| 137 |
| 138 if (!failed_ && g_test_observer) |
| 139 g_test_observer->JobFinished( |
| 140 hash_reader_->extension_id(), hash_reader_->relative_path(), failed_); |
133 } | 141 } |
134 | 142 |
135 bool ContentVerifyJob::FinishBlock() { | 143 bool ContentVerifyJob::FinishBlock() { |
136 if (current_hash_byte_count_ <= 0) | 144 if (current_hash_byte_count_ <= 0) |
137 return true; | 145 return true; |
138 std::string final(crypto::kSHA256Length, 0); | 146 std::string final(crypto::kSHA256Length, 0); |
139 current_hash_->Finish(string_as_array(&final), final.size()); | 147 current_hash_->Finish(string_as_array(&final), final.size()); |
140 current_hash_.reset(); | 148 current_hash_.reset(); |
141 current_hash_byte_count_ = 0; | 149 current_hash_byte_count_ = 0; |
142 | 150 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 if (!FinishBlock()) | 183 if (!FinishBlock()) |
176 DispatchFailureCallback(HASH_MISMATCH); | 184 DispatchFailureCallback(HASH_MISMATCH); |
177 } | 185 } |
178 } | 186 } |
179 | 187 |
180 // static | 188 // static |
181 void ContentVerifyJob::SetDelegateForTests(TestDelegate* delegate) { | 189 void ContentVerifyJob::SetDelegateForTests(TestDelegate* delegate) { |
182 g_test_delegate = delegate; | 190 g_test_delegate = delegate; |
183 } | 191 } |
184 | 192 |
| 193 // static |
| 194 void ContentVerifyJob::SetObserverForTests(TestObserver* observer) { |
| 195 g_test_observer = observer; |
| 196 } |
| 197 |
185 void ContentVerifyJob::DispatchFailureCallback(FailureReason reason) { | 198 void ContentVerifyJob::DispatchFailureCallback(FailureReason reason) { |
186 DCHECK(!failed_); | 199 DCHECK(!failed_); |
187 failed_ = true; | 200 failed_ = true; |
188 if (!failure_callback_.is_null()) { | 201 if (!failure_callback_.is_null()) { |
189 VLOG(1) << "job failed for " << hash_reader_->extension_id() << " " | 202 VLOG(1) << "job failed for " << hash_reader_->extension_id() << " " |
190 << hash_reader_->relative_path().MaybeAsASCII() | 203 << hash_reader_->relative_path().MaybeAsASCII() |
191 << " reason:" << reason; | 204 << " reason:" << reason; |
192 failure_callback_.Run(reason); | 205 failure_callback_.Run(reason); |
193 failure_callback_.Reset(); | 206 failure_callback_.Reset(); |
194 } | 207 } |
| 208 if (g_test_observer) |
| 209 g_test_observer->JobFinished( |
| 210 hash_reader_->extension_id(), hash_reader_->relative_path(), failed_); |
195 } | 211 } |
196 | 212 |
197 } // namespace extensions | 213 } // namespace extensions |
OLD | NEW |