pnacl_translation_cache.h 3.6 KB

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