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