1 // Copyright 2013 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.
4 
5 #ifndef COMPONENTS_NACL_BROWSER_NACL_VALIDATION_CACHE_H_
6 #define COMPONENTS_NACL_BROWSER_NACL_VALIDATION_CACHE_H_
7 
8 #include <stddef.h>
9 #include <string>
10 #include <vector>
11 
12 #include "base/containers/mru_cache.h"
13 #include "base/macros.h"
14 
15 namespace base {
16 class Pickle;
17 }
18 
19 namespace nacl {
20 
21 class NaClValidationCache {
22  public:
23   NaClValidationCache();
24   ~NaClValidationCache();
25 
26   // Get the key used for HMACing validation signatures.  This should be a
27   // string of cryptographically secure random bytes.
GetValidationCacheKey()28   const std::string& GetValidationCacheKey() const {
29     return validation_cache_key_;
30   }
31 
32   // Is the validation signature in the database?
33   bool QueryKnownToValidate(const std::string& signature, bool reorder);
34 
35   // Put the validation signature in the database.
36   void SetKnownToValidate(const std::string& signature);
37 
38   void Reset();
39   void Serialize(base::Pickle* pickle) const;
40   bool Deserialize(const base::Pickle* pickle);
41 
42   // Testing functions
size()43   size_t size() const {
44     return validation_cache_.size();
45   }
SetValidationCacheKey(std::string & key)46   void SetValidationCacheKey(std::string& key) {
47     validation_cache_key_ = key;
48   }
GetContents()49   std::vector<std::string> GetContents() const {
50     std::vector<std::string> contents;
51     ValidationCacheType::const_iterator iter = validation_cache_.begin();
52     for (iter = validation_cache_.begin();
53          iter != validation_cache_.end();
54          iter++) {
55       contents.push_back(iter->first);
56     }
57     return contents;
58   }
59 
60  private:
61   bool DeserializeImpl(const base::Pickle* pickle);
62 
63   typedef base::HashingMRUCache<std::string, bool> ValidationCacheType;
64   ValidationCacheType validation_cache_;
65 
66   std::string validation_cache_key_;
67 
68   DISALLOW_COPY_AND_ASSIGN(NaClValidationCache);
69 };
70 
71 } // namespace nacl
72 
73 #endif  // COMPONENTS_NACL_BROWSER_NACL_VALIDATION_CACHE_H_
74