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"
16 namespace disk_cache
{
21 struct PnaclCacheInfo
;
25 class DrainableIOBuffer
;
29 typedef base::Callback
<void(int)> CompletionCallback
;
30 typedef base::Callback
<void(int, scoped_refptr
<net::DrainableIOBuffer
>)>
32 class PnaclTranslationCacheEntry
;
33 extern const int kMaxMemCacheSize
;
35 class PnaclTranslationCache
36 : public base::SupportsWeakPtr
<PnaclTranslationCache
> {
38 PnaclTranslationCache();
39 virtual ~PnaclTranslationCache();
41 // Initialize the translation cache in |cache_dir|. If the return value is
42 // net::ERR_IO_PENDING, |callback| will be called with a 0 argument on sucess
44 int InitOnDisk(const base::FilePath
& cache_dir
,
45 const CompletionCallback
& callback
);
47 // Initialize the translation cache in memory. If the return value is
48 // net::ERR_IO_PENDING, |callback| will be called with a 0 argument on sucess
50 int InitInMemory(const CompletionCallback
& callback
);
52 // Store the nexe in the translation cache, and call |callback| with
53 // the result. The result passed to the callback is 0 on success and
54 // <0 otherwise. A reference to |nexe_data| is held until completion
56 void StoreNexe(const std::string
& key
,
57 net::DrainableIOBuffer
* nexe_data
,
58 const CompletionCallback
& callback
);
60 // Retrieve the nexe from the translation cache. Write the data into |nexe|
61 // and call |callback|, passing a result code (0 on success and <0 otherwise),
62 // and a DrainableIOBuffer with the data.
63 void GetNexe(const std::string
& key
, const GetNexeCallback
& callback
);
65 // Return the number of entries in the cache backend.
68 // Return the cache key for |info|
69 static std::string
GetKey(const nacl::PnaclCacheInfo
& info
);
71 // Doom all entries between |initial| and |end|. If the return value is
72 // net::ERR_IO_PENDING, |callback| will be invoked when the operation
74 int DoomEntriesBetween(base::Time initial
, base::Time end
,
75 const CompletionCallback
& callback
);
78 friend class PnaclTranslationCacheEntry
;
79 friend class PnaclTranslationCacheTest
;
80 // PnaclTranslationCacheEntry should only use the
81 // OpComplete and backend methods on PnaclTranslationCache.
82 void OpComplete(PnaclTranslationCacheEntry
* entry
);
83 disk_cache::Backend
* backend() { return disk_cache_
.get(); }
85 int Init(net::CacheType
,
86 const base::FilePath
& directory
,
88 const CompletionCallback
& callback
);
90 void OnCreateBackendComplete(int rv
);
92 scoped_ptr
<disk_cache::Backend
> disk_cache_
;
93 CompletionCallback init_callback_
;
95 std::map
<void*, scoped_refptr
<PnaclTranslationCacheEntry
> > open_entries_
;
97 DISALLOW_COPY_AND_ASSIGN(PnaclTranslationCache
);
102 #endif // COMPONENTS_NACL_BROWSER_PNACL_TRANSLATION_CACHE_H_