• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PNACL_TRANSLATION_CACHE_H_
6 #define COMPONENTS_NACL_BROWSER_PNACL_TRANSLATION_CACHE_H_
7 
8 #include <map>
9 #include <memory>
10 
11 #include "base/files/file_path.h"
12 #include "base/functional/callback.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/time/time.h"
15 #include "net/base/cache_type.h"
16 
17 namespace disk_cache {
18 class Backend;
19 struct BackendResult;
20 }
21 
22 namespace nacl {
23 struct PnaclCacheInfo;
24 }
25 
26 namespace net {
27 class DrainableIOBuffer;
28 }
29 
30 namespace pnacl {
31 typedef base::OnceCallback<void(int)> CompletionOnceCallback;
32 typedef base::OnceCallback<void(int, scoped_refptr<net::DrainableIOBuffer>)>
33     GetNexeCallback;
34 class PnaclTranslationCacheEntry;
35 extern const int kMaxMemCacheSize;
36 
37 class PnaclTranslationCache
38     : public base::SupportsWeakPtr<PnaclTranslationCache> {
39  public:
40   PnaclTranslationCache();
41 
42   PnaclTranslationCache(const PnaclTranslationCache&) = delete;
43   PnaclTranslationCache& operator=(const PnaclTranslationCache&) = delete;
44 
45   virtual ~PnaclTranslationCache();
46 
47   // Initialize the translation cache in |cache_dir|.  If the return value is
48   // net::ERR_IO_PENDING, |callback| will be called with a 0 argument on success
49   // and <0 otherwise.
50   int InitOnDisk(const base::FilePath& cache_dir,
51                  CompletionOnceCallback callback);
52 
53   // Initialize the translation cache in memory.  If the return value is
54   // net::ERR_IO_PENDING, |callback| will be called with a 0 argument on success
55   // and <0 otherwise.
56   int InitInMemory(CompletionOnceCallback callback);
57 
58   // Store the nexe in the translation cache, and call |callback| with
59   // the result. The result passed to the callback is 0 on success and
60   // <0 otherwise. A reference to |nexe_data| is held until completion
61   // or cancellation.
62   void StoreNexe(const std::string& key,
63                  net::DrainableIOBuffer* nexe_data,
64                  CompletionOnceCallback callback);
65 
66   // Retrieve the nexe from the translation cache. Write the data into |nexe|
67   // and call |callback|, passing a result code (0 on success and <0 otherwise),
68   // and a DrainableIOBuffer with the data.
69   void GetNexe(const std::string& key, GetNexeCallback callback);
70 
71   // Return the number of entries in the cache backend.
72   int Size();
73 
74   // Return the cache key for |info|
75   static std::string GetKey(const nacl::PnaclCacheInfo& info);
76 
77   // Doom all entries between |initial| and |end|. If the return value is
78   // net::ERR_IO_PENDING, |callback| will be invoked when the operation
79   // completes.
80   int DoomEntriesBetween(base::Time initial,
81                          base::Time end,
82                          CompletionOnceCallback callback);
83 
84  private:
85   friend class PnaclTranslationCacheEntry;
86   friend class PnaclTranslationCacheTest;
87   // PnaclTranslationCacheEntry should only use the
88   // OpComplete and backend methods on PnaclTranslationCache.
89   void OpComplete(PnaclTranslationCacheEntry* entry);
backend()90   disk_cache::Backend* backend() { return disk_cache_.get(); }
91 
92   int Init(net::CacheType,
93            const base::FilePath& directory,
94            int cache_size,
95            CompletionOnceCallback callback);
96 
97   void OnCreateBackendComplete(disk_cache::BackendResult result);
98 
99   std::unique_ptr<disk_cache::Backend> disk_cache_;
100   CompletionOnceCallback init_callback_;
101   bool in_memory_;
102   std::map<void*, scoped_refptr<PnaclTranslationCacheEntry> > open_entries_;
103 };
104 
105 }  // namespace pnacl
106 
107 #endif  // COMPONENTS_NACL_BROWSER_PNACL_TRANSLATION_CACHE_H_
108