Home | History | Annotate | Download | only in disk_cache
      1 // Copyright (c) 2006-2009 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 // See net/disk_cache/disk_cache.h for the public interface of the cache.
      6 
      7 #ifndef NET_DISK_CACHE_MEM_BACKEND_IMPL_H__
      8 #define NET_DISK_CACHE_MEM_BACKEND_IMPL_H__
      9 
     10 #include "base/hash_tables.h"
     11 
     12 #include "net/disk_cache/disk_cache.h"
     13 #include "net/disk_cache/mem_rankings.h"
     14 
     15 namespace disk_cache {
     16 
     17 class MemEntryImpl;
     18 
     19 // This class implements the Backend interface. An object of this class handles
     20 // the operations of the cache without writing to disk.
     21 class MemBackendImpl : public Backend {
     22  public:
     23   MemBackendImpl() : max_size_(0), current_size_(0) {}
     24   ~MemBackendImpl();
     25 
     26   // Performs general initialization for this current instance of the cache.
     27   bool Init();
     28 
     29   // Backend interface.
     30   virtual int32 GetEntryCount() const;
     31   virtual bool OpenEntry(const std::string& key, Entry** entry);
     32   virtual int OpenEntry(const std::string& key, Entry** entry,
     33                         CompletionCallback* callback);
     34   virtual bool CreateEntry(const std::string& key, Entry** entry);
     35   virtual int CreateEntry(const std::string& key, Entry** entry,
     36                           CompletionCallback* callback);
     37   virtual bool DoomEntry(const std::string& key);
     38   virtual int DoomEntry(const std::string& key, CompletionCallback* callback);
     39   virtual bool DoomAllEntries();
     40   virtual int DoomAllEntries(CompletionCallback* callback);
     41   virtual bool DoomEntriesBetween(const base::Time initial_time,
     42                                   const base::Time end_time);
     43   virtual int DoomEntriesBetween(const base::Time initial_time,
     44                                  const base::Time end_time,
     45                                  CompletionCallback* callback);
     46   virtual bool DoomEntriesSince(const base::Time initial_time);
     47   virtual int DoomEntriesSince(const base::Time initial_time,
     48                                CompletionCallback* callback);
     49   virtual bool OpenNextEntry(void** iter, Entry** next_entry);
     50   virtual int OpenNextEntry(void** iter, Entry** next_entry,
     51                             CompletionCallback* callback);
     52   virtual void EndEnumeration(void** iter);
     53   virtual void GetStats(
     54       std::vector<std::pair<std::string, std::string> >* stats) {}
     55 
     56   // Sets the maximum size for the total amount of data stored by this instance.
     57   bool SetMaxSize(int max_bytes);
     58 
     59   // Permanently deletes an entry.
     60   void InternalDoomEntry(MemEntryImpl* entry);
     61 
     62   // Updates the ranking information for an entry.
     63   void UpdateRank(MemEntryImpl* node);
     64 
     65   // A user data block is being created, extended or truncated.
     66   void ModifyStorageSize(int32 old_size, int32 new_size);
     67 
     68   // Returns the maximum size for a file to reside on the cache.
     69   int MaxFileSize() const;
     70 
     71   // Insert an MemEntryImpl into the ranking list. This method is only called
     72   // from MemEntryImpl to insert child entries. The reference can be removed
     73   // by calling RemoveFromRankingList(|entry|).
     74   void InsertIntoRankingList(MemEntryImpl* entry);
     75 
     76   // Remove |entry| from ranking list. This method is only called from
     77   // MemEntryImpl to remove a child entry from the ranking list.
     78   void RemoveFromRankingList(MemEntryImpl* entry);
     79 
     80  private:
     81   // Deletes entries from the cache until the current size is below the limit.
     82   // If empty is true, the whole cache will be trimmed, regardless of being in
     83   // use.
     84   void TrimCache(bool empty);
     85 
     86   // Handles the used storage count.
     87   void AddStorageSize(int32 bytes);
     88   void SubstractStorageSize(int32 bytes);
     89 
     90   typedef base::hash_map<std::string, MemEntryImpl*> EntryMap;
     91 
     92   EntryMap entries_;
     93   MemRankings rankings_;  // Rankings to be able to trim the cache.
     94   int32 max_size_;        // Maximum data size for this instance.
     95   int32 current_size_;
     96 
     97   DISALLOW_EVIL_CONSTRUCTORS(MemBackendImpl);
     98 };
     99 
    100 }  // namespace disk_cache
    101 
    102 #endif  // NET_DISK_CACHE_MEM_BACKEND_IMPL_H__
    103