| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_NACL_HOST_PNACL_TRANSLATION_CACHE_H_ | 5 #ifndef CHROME_BROWSER_NACL_HOST_PNACL_TRANSLATION_CACHE_H_ |
| 6 #define CHROME_BROWSER_NACL_HOST_PNACL_TRANSLATION_CACHE_H_ | 6 #define CHROME_BROWSER_NACL_HOST_PNACL_TRANSLATION_CACHE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "net/base/cache_type.h" | 13 #include "net/base/cache_type.h" |
| 14 | 14 |
| 15 namespace base { | 15 namespace base { |
| 16 class MessageLoopProxy; | 16 class MessageLoopProxy; |
| 17 } | 17 } |
| 18 | 18 |
| 19 namespace disk_cache { | 19 namespace disk_cache { |
| 20 class Backend; | 20 class Backend; |
| 21 } | 21 } |
| 22 | 22 |
| 23 namespace nacl { | |
| 24 struct PnaclCacheInfo; | |
| 25 } | |
| 26 | |
| 27 namespace net { | |
| 28 class DrainableIOBuffer; | |
| 29 } | |
| 30 | |
| 31 namespace pnacl { | 23 namespace pnacl { |
| 32 typedef base::Callback<void(int)> CompletionCallback; | 24 typedef base::Callback<void(int)> CompletionCallback; |
| 33 typedef base::Callback<void(int, scoped_refptr<net::DrainableIOBuffer>)> | |
| 34 GetNexeCallback; | |
| 35 class PnaclTranslationCacheEntry; | 25 class PnaclTranslationCacheEntry; |
| 36 extern const int kMaxMemCacheSize; | 26 extern const int kMaxMemCacheSize; |
| 37 | 27 |
| 38 class PnaclTranslationCache | 28 class PnaclTranslationCache |
| 39 : public base::SupportsWeakPtr<PnaclTranslationCache> { | 29 : public base::SupportsWeakPtr<PnaclTranslationCache> { |
| 40 public: | 30 public: |
| 41 PnaclTranslationCache(); | 31 PnaclTranslationCache(); |
| 42 virtual ~PnaclTranslationCache(); | 32 virtual ~PnaclTranslationCache(); |
| 43 | 33 |
| 44 // Initialize the translation cache in |cache_dir| (or in memory if | 34 // Initialize the translation cache in |cache_dir| (or in memory if |
| 45 // |in_memory| is true). Call |callback| with a 0 argument on sucess and | 35 // |in_memory| is true). Call |callback| with a 0 argument on sucess and |
| 46 // <0 otherwise. | 36 // <0 otherwise. |
| 47 int InitCache(const base::FilePath& cache_dir, | 37 int InitCache(const base::FilePath& cache_dir, |
| 48 bool in_memory, | 38 bool in_memory, |
| 49 const CompletionCallback& callback); | 39 const CompletionCallback& callback); |
| 50 | 40 |
| 51 // Store the nexe in the translation cache. A reference to |nexe_data| is | 41 // Store the nexe in the translation cache. |
| 52 // held until completion or cancellation. | 42 void StoreNexe(const std::string& key, const std::string& nexe); |
| 53 void StoreNexe(const std::string& key, net::DrainableIOBuffer* nexe_data); | |
| 54 | 43 |
| 55 // Store the nexe in the translation cache, and call |callback| with | 44 // Store the nexe in the translation cache, and call |callback| with |
| 56 // the result. The result passed to the callback is 0 on success and | 45 // the result. The result passed to the callback is 0 on success and |
| 57 // <0 otherwise. A reference to |nexe_data| is held until completion | 46 // <0 otherwise. |
| 58 // or cancellation. | |
| 59 void StoreNexe(const std::string& key, | 47 void StoreNexe(const std::string& key, |
| 60 net::DrainableIOBuffer* nexe_data, | 48 const std::string& nexe, |
| 61 const CompletionCallback& callback); | 49 const CompletionCallback& callback); |
| 62 | 50 |
| 63 // Retrieve the nexe from the translation cache. Write the data into |nexe| | 51 // Retrieve the nexe from the translation cache. Write the data into |nexe| |
| 64 // and call |callback|, passing a result code (0 on success and <0 otherwise), | 52 // and call |callback| with the result (0 on success and <0 otherwise) |
| 65 // and a DrainableIOBuffer with the data. | 53 void GetNexe(const std::string& key, |
| 66 void GetNexe(const std::string& key, const GetNexeCallback& callback); | 54 std::string* nexe, |
| 55 const CompletionCallback& callback); |
| 67 | 56 |
| 68 // Return the number of entries in the cache backend. | 57 // Return the number of entries in the cache backend. |
| 69 int Size(); | 58 int Size(); |
| 70 | 59 |
| 71 // Return the cache key for |info| | |
| 72 static std::string GetKey(const nacl::PnaclCacheInfo& info); | |
| 73 | |
| 74 private: | 60 private: |
| 75 friend class PnaclTranslationCacheEntry; | 61 friend class PnaclTranslationCacheEntry; |
| 76 friend class PnaclTranslationCacheTest; | |
| 77 // PnaclTranslationCacheEntry should only use the | 62 // PnaclTranslationCacheEntry should only use the |
| 78 // OpComplete and backend methods on PnaclTranslationCache. | 63 // OpComplete and backend methods on PnaclTranslationCache. |
| 79 void OpComplete(PnaclTranslationCacheEntry* entry); | 64 void OpComplete(PnaclTranslationCacheEntry* entry); |
| 80 disk_cache::Backend* backend() { return disk_cache_; } | 65 disk_cache::Backend* backend() { return disk_cache_; } |
| 81 | 66 |
| 82 int InitWithDiskBackend(const base::FilePath& disk_cache_dir, | 67 int InitWithDiskBackend(const base::FilePath& disk_cache_dir, |
| 83 int cache_size, | 68 int cache_size, |
| 84 const CompletionCallback& callback); | 69 const CompletionCallback& callback); |
| 85 | 70 |
| 86 int InitWithMemBackend(int cache_size, const CompletionCallback& callback); | 71 int InitWithMemBackend(int cache_size, const CompletionCallback& callback); |
| 87 | 72 |
| 88 int Init(net::CacheType, | 73 int Init(net::CacheType, |
| 89 const base::FilePath& directory, | 74 const base::FilePath& directory, |
| 90 int cache_size, | 75 int cache_size, |
| 91 const CompletionCallback& callback); | 76 const CompletionCallback& callback); |
| 92 | 77 |
| 93 void OnCreateBackendComplete(int rv); | 78 void OnCreateBackendComplete(int rv); |
| 94 | 79 |
| 95 disk_cache::Backend* disk_cache_; | 80 disk_cache::Backend* disk_cache_; |
| 96 CompletionCallback init_callback_; | 81 CompletionCallback init_callback_; |
| 97 bool in_memory_; | 82 bool in_memory_; |
| 98 std::map<void*, scoped_refptr<PnaclTranslationCacheEntry> > open_entries_; | 83 std::map<void*, scoped_refptr<PnaclTranslationCacheEntry> > open_entries_; |
| 99 | 84 |
| 100 DISALLOW_COPY_AND_ASSIGN(PnaclTranslationCache); | 85 DISALLOW_COPY_AND_ASSIGN(PnaclTranslationCache); |
| 101 }; | 86 }; |
| 102 | 87 |
| 103 } // namespace pnacl | 88 } // namespace pnacl |
| 104 | 89 |
| 105 #endif // CHROME_BROWSER_NACL_HOST_PNACL_TRANSLATION_CACHE_H_ | 90 #endif // CHROME_BROWSER_NACL_HOST_PNACL_TRANSLATION_CACHE_H_ |
| OLD | NEW |