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.
5 #ifndef COMPONENTS_NACL_BROWSER_PNACL_TRANSLATION_CACHE_H_
6 #define COMPONENTS_NACL_BROWSER_PNACL_TRANSLATION_CACHE_H_
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"
17 class MessageLoopProxy
;
20 namespace disk_cache
{
25 struct PnaclCacheInfo
;
29 class DrainableIOBuffer
;
33 typedef base::Callback
<void(int)> CompletionCallback
;
34 typedef base::Callback
<void(int, scoped_refptr
<net::DrainableIOBuffer
>)>
36 class PnaclTranslationCacheEntry
;
37 extern const int kMaxMemCacheSize
;
39 class PnaclTranslationCache
40 : public base::SupportsWeakPtr
<PnaclTranslationCache
> {
42 PnaclTranslationCache();
43 virtual ~PnaclTranslationCache();
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
48 int InitOnDisk(const base::FilePath
& cache_dir
,
49 const CompletionCallback
& callback
);
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
54 int InitInMemory(const CompletionCallback
& callback
);
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
60 void StoreNexe(const std::string
& key
,
61 net::DrainableIOBuffer
* nexe_data
,
62 const CompletionCallback
& callback
);
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
);
69 // Return the number of entries in the cache backend.
72 // Return the cache key for |info|
73 static std::string
GetKey(const nacl::PnaclCacheInfo
& info
);
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
78 int DoomEntriesBetween(base::Time initial
, base::Time end
,
79 const CompletionCallback
& callback
);
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
);
87 disk_cache::Backend
* backend() { return disk_cache_
.get(); }
89 int Init(net::CacheType
,
90 const base::FilePath
& directory
,
92 const CompletionCallback
& callback
);
94 void OnCreateBackendComplete(int rv
);
96 scoped_ptr
<disk_cache::Backend
> disk_cache_
;
97 CompletionCallback init_callback_
;
99 std::map
<void*, scoped_refptr
<PnaclTranslationCacheEntry
> > open_entries_
;
101 DISALLOW_COPY_AND_ASSIGN(PnaclTranslationCache
);
106 #endif // COMPONENTS_NACL_BROWSER_PNACL_TRANSLATION_CACHE_H_