#include <algorithm>
+#include "base/metrics/histogram.h"
#include "base/stl_util.h"
#include "base/task_runner_util.h"
+#include "base/timer/elapsed_timer.h"
#include "content/public/browser/browser_thread.h"
#include "crypto/secure_hash.h"
#include "crypto/sha2.h"
ContentVerifyJob::TestDelegate* g_test_delegate = NULL;
+class ScopedElapsedTimer {
+ public:
+ ScopedElapsedTimer(base::TimeDelta* total) : total_(total) { DCHECK(total_); }
+
+ ~ScopedElapsedTimer() { *total_ += timer.Elapsed(); }
+
+ private:
+ // Some total amount of time we should add our elapsed time to at
+ // destruction.
+ base::TimeDelta* total_;
+
+ // A timer for how long this object has been alive.
+ base::ElapsedTimer timer;
+};
+
} // namespace
ContentVerifyJob::ContentVerifyJob(ContentHashReader* hash_reader,
}
ContentVerifyJob::~ContentVerifyJob() {
+ UMA_HISTOGRAM_COUNTS("ExtensionContentVerifyJob.TimeSpentUS",
+ time_spent_.InMicroseconds());
}
void ContentVerifyJob::Start() {
}
void ContentVerifyJob::BytesRead(int count, const char* data) {
+ ScopedElapsedTimer timer(&time_spent_);
DCHECK(thread_checker_.CalledOnValidThread());
if (failed_)
return;
}
void ContentVerifyJob::DoneReading() {
+ ScopedElapsedTimer timer(&time_spent_);
DCHECK(thread_checker_.CalledOnValidThread());
if (failed_)
return;
return true;
std::string final(crypto::kSHA256Length, 0);
current_hash_->Finish(string_as_array(&final), final.size());
+ current_hash_.reset();
+ current_hash_byte_count_ = 0;
+
+ int block = current_block_++;
const std::string* expected_hash = NULL;
- if (!hash_reader_->GetHashForBlock(current_block_, &expected_hash) ||
+ if (!hash_reader_->GetHashForBlock(block, &expected_hash) ||
*expected_hash != final)
return false;
- current_hash_.reset();
- current_hash_byte_count_ = 0;
- current_block_++;
return true;
}
void ContentVerifyJob::OnHashesReady(bool success) {
if (!success && !g_test_delegate) {
- if (hash_reader_->have_verified_contents() &&
- hash_reader_->have_computed_hashes())
+ if (!hash_reader_->content_exists()) {
+ // Ignore verification of non-existent resources.
+ return;
+ } else if (hash_reader_->have_verified_contents() &&
+ hash_reader_->have_computed_hashes()) {
DispatchFailureCallback(NO_HASHES_FOR_FILE);
- else
+ } else {
DispatchFailureCallback(MISSING_ALL_HASHES);
+ }
return;
}
queue_.swap(tmp);
BytesRead(tmp.size(), string_as_array(&tmp));
}
- if (done_reading_ && !FinishBlock())
- DispatchFailureCallback(HASH_MISMATCH);
+ if (done_reading_) {
+ ScopedElapsedTimer timer(&time_spent_);
+ if (!FinishBlock())
+ DispatchFailureCallback(HASH_MISMATCH);
+ }
}
// static