summaryrefslogtreecommitdiffstats
path: root/chrome/browser/nacl_host/nacl_validation_cache.cc
diff options
context:
space:
mode:
authorncbray@google.com <ncbray@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2012-05-09 18:37:47 +0000
committerncbray@google.com <ncbray@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2012-05-09 18:37:47 +0000
commit7cfaf98a8ca8d4a8e0b9bac445495ba87173d88b (patch)
treea8938778b620ee9a1049e612f4e0a66eff3d02eb /chrome/browser/nacl_host/nacl_validation_cache.cc
parentbb2c63a02afdc96c3a01702814a9e63d575a6a70 (diff)
downloadchromium_src-7cfaf98a8ca8d4a8e0b9bac445495ba87173d88b.zip
chromium_src-7cfaf98a8ca8d4a8e0b9bac445495ba87173d88b.tar.gz
chromium_src-7cfaf98a8ca8d4a8e0b9bac445495ba87173d88b.tar.bz2
NaCl Validation Cache: move browser implementation into its own file.
A few cleanups were also performed, such as encapsulating the UMA hooks. BUG= http://code.google.com/p/nativeclient/issues/detail?id=2515 TEST= none Review URL: https://chromiumcodereview.appspot.com/10377051 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@136085 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/nacl_host/nacl_validation_cache.cc')
-rw-r--r--chrome/browser/nacl_host/nacl_validation_cache.cc71
1 files changed, 71 insertions, 0 deletions
diff --git a/chrome/browser/nacl_host/nacl_validation_cache.cc b/chrome/browser/nacl_host/nacl_validation_cache.cc
new file mode 100644
index 0000000..5916b09
--- /dev/null
+++ b/chrome/browser/nacl_host/nacl_validation_cache.cc
@@ -0,0 +1,71 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/nacl_host/nacl_validation_cache.h"
+
+#include "base/metrics/histogram.h"
+#include "base/rand_util.h"
+
+namespace {
+
+// For the moment, choose an arbitrary cache size.
+const size_t kValidationCacheCacheSize = 200;
+// Key size is equal to the block size (not the digest size) of SHA256.
+const size_t kValidationCacheKeySize = 64;
+// Entry size is equal to the digest size of SHA256.
+const size_t kValidationCacheEntrySize = 32;
+
+enum ValidationCacheStatus {
+ CACHE_MISS = 0,
+ CACHE_HIT,
+ CACHE_ERROR,
+ CACHE_MAX
+};
+
+void LogCacheQuery(ValidationCacheStatus status) {
+ UMA_HISTOGRAM_ENUMERATION("NaCl.ValidationCache.Query", status, CACHE_MAX);
+}
+
+void LogCacheSet(ValidationCacheStatus status) {
+ // Bucket zero is reserved for future use.
+ UMA_HISTOGRAM_ENUMERATION("NaCl.ValidationCache.Set", status, CACHE_MAX);
+}
+
+} // namespace
+
+NaClValidationCache::NaClValidationCache()
+ : validation_cache_(kValidationCacheCacheSize),
+ validation_cache_key_(base::RandBytesAsString(kValidationCacheKeySize)){
+}
+
+NaClValidationCache::~NaClValidationCache() {
+ // Make clang's style checking happy by adding a destructor.
+}
+
+bool NaClValidationCache::QueryKnownToValidate(const std::string& signature) {
+ bool result = false;
+ if (signature.length() != kValidationCacheEntrySize) {
+ // Signature is the wrong size, something is wrong.
+ LogCacheQuery(CACHE_ERROR);
+ } else {
+ ValidationCacheType::iterator iter = validation_cache_.Get(signature);
+ if (iter != validation_cache_.end()) {
+ result = iter->second;
+ }
+ LogCacheQuery(result ? CACHE_HIT : CACHE_MISS);
+ }
+ return result;
+}
+
+void NaClValidationCache::SetKnownToValidate(const std::string& signature) {
+ if (signature.length() != kValidationCacheEntrySize) {
+ // Signature is the wrong size, something is wrong.
+ LogCacheSet(CACHE_ERROR);
+ } else {
+ validation_cache_.Put(signature, true);
+ // The number of sets should be equal to the number of cache misses, minus
+ // validation failures and successful validations where stubout occurs.
+ LogCacheSet(CACHE_HIT);
+ }
+}