Home | History | Annotate | Download | only in nacl_host
      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 
      5 #ifndef CHROME_BROWSER_NACL_HOST_PNACL_TRANSLATION_CACHE_H_
      6 #define CHROME_BROWSER_NACL_HOST_PNACL_TRANSLATION_CACHE_H_
      7 
      8 #include <map>
      9 
     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"
     15 
     16 namespace base {
     17 class MessageLoopProxy;
     18 }
     19 
     20 namespace disk_cache {
     21 class Backend;
     22 }
     23 
     24 namespace nacl {
     25 struct PnaclCacheInfo;
     26 }
     27 
     28 namespace net {
     29 class DrainableIOBuffer;
     30 }
     31 
     32 namespace pnacl {
     33 typedef base::Callback<void(int)> CompletionCallback;
     34 typedef base::Callback<void(int, scoped_refptr<net::DrainableIOBuffer>)>
     35     GetNexeCallback;
     36 class PnaclTranslationCacheEntry;
     37 extern const int kMaxMemCacheSize;
     38 
     39 class PnaclTranslationCache
     40     : public base::SupportsWeakPtr<PnaclTranslationCache> {
     41  public:
     42   PnaclTranslationCache();
     43   virtual ~PnaclTranslationCache();
     44 
     45   // Initialize the translation cache in |cache_dir| (or in memory if
     46   // |in_memory| is true). Call |callback| with a 0 argument on sucess and
     47   // <0 otherwise.
     48   int InitCache(const base::FilePath& cache_dir,
     49                 bool in_memory,
     50                 const CompletionCallback& callback);
     51 
     52   // Store the nexe in the translation cache. A reference to |nexe_data| is
     53   // held until completion or cancellation.
     54   void StoreNexe(const std::string& key, net::DrainableIOBuffer* nexe_data);
     55 
     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
     59   // or cancellation.
     60   void StoreNexe(const std::string& key,
     61                  net::DrainableIOBuffer* nexe_data,
     62                  const CompletionCallback& callback);
     63 
     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);
     68 
     69   // Return the number of entries in the cache backend.
     70   int Size();
     71 
     72   // Return the cache key for |info|
     73   static std::string GetKey(const nacl::PnaclCacheInfo& info);
     74 
     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
     77   // completes.
     78   int DoomEntriesBetween(base::Time initial, base::Time end,
     79                          const CompletionCallback& callback);
     80 
     81  private:
     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(); }
     88 
     89   int InitWithDiskBackend(const base::FilePath& disk_cache_dir,
     90                           int cache_size,
     91                           const CompletionCallback& callback);
     92 
     93   int InitWithMemBackend(int cache_size, const CompletionCallback& callback);
     94 
     95   int Init(net::CacheType,
     96            const base::FilePath& directory,
     97            int cache_size,
     98            const CompletionCallback& callback);
     99 
    100   void OnCreateBackendComplete(int rv);
    101 
    102   scoped_ptr<disk_cache::Backend> disk_cache_;
    103   CompletionCallback init_callback_;
    104   bool in_memory_;
    105   std::map<void*, scoped_refptr<PnaclTranslationCacheEntry> > open_entries_;
    106 
    107   DISALLOW_COPY_AND_ASSIGN(PnaclTranslationCache);
    108 };
    109 
    110 }  // namespace pnacl
    111 
    112 #endif  // CHROME_BROWSER_NACL_HOST_PNACL_TRANSLATION_CACHE_H_
    113