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