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 final { 38 public: 39 PnaclTranslationCache(); 40 41 PnaclTranslationCache(const PnaclTranslationCache&) = delete; 42 PnaclTranslationCache& operator=(const PnaclTranslationCache&) = delete; 43 44 virtual ~PnaclTranslationCache(); 45 46 // Initialize the translation cache in |cache_dir|. If the return value is 47 // net::ERR_IO_PENDING, |callback| will be called with a 0 argument on success 48 // and <0 otherwise. 49 int InitOnDisk(const base::FilePath& cache_dir, 50 CompletionOnceCallback callback); 51 52 // Initialize the translation cache in memory. If the return value is 53 // net::ERR_IO_PENDING, |callback| will be called with a 0 argument on success 54 // and <0 otherwise. 55 int InitInMemory(CompletionOnceCallback callback); 56 57 // Store the nexe in the translation cache, and call |callback| with 58 // the result. The result passed to the callback is 0 on success and 59 // <0 otherwise. A reference to |nexe_data| is held until completion 60 // or cancellation. 61 void StoreNexe(const std::string& key, 62 net::DrainableIOBuffer* nexe_data, 63 CompletionOnceCallback callback); 64 65 // Retrieve the nexe from the translation cache. Write the data into |nexe| 66 // and call |callback|, passing a result code (0 on success and <0 otherwise), 67 // and a DrainableIOBuffer with the data. 68 void GetNexe(const std::string& key, GetNexeCallback callback); 69 70 // Return the number of entries in the cache backend. 71 int Size(); 72 73 // Return the cache key for |info| 74 static std::string GetKey(const nacl::PnaclCacheInfo& info); 75 76 // Doom all entries between |initial| and |end|. If the return value is 77 // net::ERR_IO_PENDING, |callback| will be invoked when the operation 78 // completes. 79 int DoomEntriesBetween(base::Time initial, 80 base::Time end, 81 CompletionOnceCallback callback); 82 83 private: 84 friend class PnaclTranslationCacheEntry; 85 friend class PnaclTranslationCacheTest; 86 // PnaclTranslationCacheEntry should only use the 87 // OpComplete and backend methods on PnaclTranslationCache. 88 void OpComplete(PnaclTranslationCacheEntry* entry); backend()89 disk_cache::Backend* backend() { return disk_cache_.get(); } 90 91 int Init(net::CacheType, 92 const base::FilePath& directory, 93 int cache_size, 94 CompletionOnceCallback callback); 95 96 void OnCreateBackendComplete(disk_cache::BackendResult result); 97 98 std::unique_ptr<disk_cache::Backend> disk_cache_; 99 CompletionOnceCallback init_callback_; 100 bool in_memory_; 101 std::map<void*, scoped_refptr<PnaclTranslationCacheEntry> > open_entries_; 102 base::WeakPtrFactory<PnaclTranslationCache> weak_ptr_factory_{this}; 103 }; 104 105 } // namespace pnacl 106 107 #endif // COMPONENTS_NACL_BROWSER_PNACL_TRANSLATION_CACHE_H_ 108