simple_backend_impl.h revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
1// Copyright (c) 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 NET_DISK_CACHE_SIMPLE_SIMPLE_BACKEND_IMPL_H_ 6#define NET_DISK_CACHE_SIMPLE_SIMPLE_BACKEND_IMPL_H_ 7 8#include <map> 9#include <string> 10#include <utility> 11#include <vector> 12 13#include "base/compiler_specific.h" 14#include "base/files/file_path.h" 15#include "base/memory/ref_counted.h" 16#include "base/memory/scoped_ptr.h" 17#include "base/memory/weak_ptr.h" 18#include "base/task_runner.h" 19#include "net/base/cache_type.h" 20#include "net/disk_cache/disk_cache.h" 21 22namespace base { 23class SingleThreadTaskRunner; 24} 25 26namespace disk_cache { 27 28// SimpleBackendImpl is a new cache backend that stores entries in individual 29// files. 30 31// It is currently a work in progress, missing many features of a real cache, 32// such as eviction. 33 34// See http://www.chromium.org/developers/design-documents/network-stack/disk-cache/very-simple-backend 35 36class SimpleEntryImpl; 37class SimpleIndex; 38 39class NET_EXPORT_PRIVATE SimpleBackendImpl : public Backend, 40 public base::SupportsWeakPtr<SimpleBackendImpl> { 41 public: 42 SimpleBackendImpl(const base::FilePath& path, int max_bytes, 43 net::CacheType type, 44 base::SingleThreadTaskRunner* cache_thread, 45 net::NetLog* net_log); 46 47 virtual ~SimpleBackendImpl(); 48 49 SimpleIndex* index() { return index_.get(); } 50 51 // Must run on IO Thread. 52 int Init(const CompletionCallback& completion_callback); 53 54 // Sets the maximum size for the total amount of data stored by this instance. 55 bool SetMaxSize(int max_bytes); 56 57 // Removes |entry| from the |active_entries_| set, forcing future Open/Create 58 // operations to construct a new object. 59 void OnDeactivated(const SimpleEntryImpl* entry); 60 61 // From Backend: 62 virtual net::CacheType GetCacheType() const OVERRIDE; 63 virtual int32 GetEntryCount() const OVERRIDE; 64 virtual int OpenEntry(const std::string& key, Entry** entry, 65 const CompletionCallback& callback) OVERRIDE; 66 virtual int CreateEntry(const std::string& key, Entry** entry, 67 const CompletionCallback& callback) OVERRIDE; 68 virtual int DoomEntry(const std::string& key, 69 const CompletionCallback& callback) OVERRIDE; 70 virtual int DoomAllEntries(const CompletionCallback& callback) OVERRIDE; 71 virtual int DoomEntriesBetween(base::Time initial_time, 72 base::Time end_time, 73 const CompletionCallback& callback) OVERRIDE; 74 virtual int DoomEntriesSince(base::Time initial_time, 75 const CompletionCallback& callback) OVERRIDE; 76 virtual int OpenNextEntry(void** iter, Entry** next_entry, 77 const CompletionCallback& callback) OVERRIDE; 78 virtual void EndEnumeration(void** iter) OVERRIDE; 79 virtual void GetStats( 80 std::vector<std::pair<std::string, std::string> >* stats) OVERRIDE; 81 virtual void OnExternalCacheHit(const std::string& key) OVERRIDE; 82 83 private: 84 typedef std::map<uint64, base::WeakPtr<SimpleEntryImpl> > EntryMap; 85 86 typedef base::Callback<void(uint64 max_size, int result)> 87 InitializeIndexCallback; 88 89 // Must run on IO Thread. 90 void InitializeIndex(const CompletionCallback& callback, 91 uint64 suggested_max_size, 92 int result); 93 94 // Dooms all entries previously accessed between |initial_time| and 95 // |end_time|. Invoked when the index is ready. 96 void IndexReadyForDoom(base::Time initial_time, 97 base::Time end_time, 98 const CompletionCallback& callback, 99 int result); 100 101 // Try to create the directory if it doesn't exist. Replies with maximum cache 102 // size adjustment. Must run on Cache Thread. 103 static void ProvideDirectorySuggestBetterCacheSize( 104 base::SingleThreadTaskRunner* io_thread, 105 const base::FilePath& path, 106 const InitializeIndexCallback& initialize_index_callback, 107 uint64 suggested_max_size); 108 109 // Searches |active_entries_| for the entry corresponding to |key|. If found, 110 // returns the found entry. Otherwise, creates a new entry and returns that. 111 scoped_refptr<SimpleEntryImpl> CreateOrFindActiveEntry( 112 const std::string& key); 113 114 const base::FilePath path_; 115 scoped_ptr<SimpleIndex> index_; 116 const scoped_refptr<base::SingleThreadTaskRunner> cache_thread_; 117 118 int orig_max_size_; 119 120 // TODO(gavinp): Store the entry_hash in SimpleEntryImpl, and index this map 121 // by hash. This will save memory, and make IndexReadyForDoom easier. 122 EntryMap active_entries_; 123}; 124 125} // namespace disk_cache 126 127#endif // NET_DISK_CACHE_SIMPLE_SIMPLE_BACKEND_IMPL_H_ 128