simple_entry_impl.h revision 0f1bc08d4cfcc34181b0b5cbf065c40f687bf740
12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved. 22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file. 42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_ 62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_ 72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 8c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include <queue> 92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <string> 102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h" 12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/memory/ref_counted.h" 13c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/memory/scoped_ptr.h" 142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/memory/weak_ptr.h" 15c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/threading/thread_checker.h" 1658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "net/base/cache_type.h" 17d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "net/base/net_export.h" 187dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "net/base/net_log.h" 192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "net/disk_cache/disk_cache.h" 20c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "net/disk_cache/simple/simple_entry_format.h" 21bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch#include "net/disk_cache/simple/simple_entry_operation.h" 22c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 23c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace base { 247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class TaskRunner; 25c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace net { 28d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)class GrowableIOBuffer; 292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class IOBuffer; 302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace disk_cache { 332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 34c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class SimpleBackendImpl; 352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class SimpleSynchronousEntry; 36d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)class SimpleEntryStat; 372385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdochstruct SimpleEntryCreationResults; 382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// SimpleEntryImpl is the IO thread interface to an entry in the very simple 402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// disk cache. It proxies for the SimpleSynchronousEntry, which performs IO 412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// on the worker thread. 42d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)class NET_EXPORT_PRIVATE SimpleEntryImpl : public Entry, 43d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) public base::RefCounted<SimpleEntryImpl>, 44c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) public base::SupportsWeakPtr<SimpleEntryImpl> { 45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) friend class base::RefCounted<SimpleEntryImpl>; 462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public: 47bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch enum OperationsMode { 48bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch NON_OPTIMISTIC_OPERATIONS, 49bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch OPTIMISTIC_OPERATIONS, 50bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch }; 51bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch 5258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) SimpleEntryImpl(net::CacheType cache_type, 5358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) const base::FilePath& path, 547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch uint64 entry_hash, 55bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch OperationsMode operations_mode, 56bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch SimpleBackendImpl* backend, 577dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch net::NetLog* net_log); 58c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 59c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Adds another reader/writer to this entry, if possible, returning |this| to 60c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // |entry|. 61c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) int OpenEntry(Entry** entry, const CompletionCallback& callback); 62c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 63c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Creates this entry, if possible. Returns |this| to |entry|. 64c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) int CreateEntry(Entry** entry, const CompletionCallback& callback); 652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 66c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Identical to Backend::Doom() except that it accepts a CompletionCallback. 67c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) int DoomEntry(const CompletionCallback& callback); 682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 69c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const std::string& key() const { return key_; } 70c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) uint64 entry_hash() const { return entry_hash_; } 712385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch void SetKey(const std::string& key); 722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // From Entry: 742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual void Doom() OVERRIDE; 752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual void Close() OVERRIDE; 762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual std::string GetKey() const OVERRIDE; 772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual base::Time GetLastUsed() const OVERRIDE; 782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual base::Time GetLastModified() const OVERRIDE; 792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual int32 GetDataSize(int index) const OVERRIDE; 80c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) virtual int ReadData(int stream_index, 812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int offset, 822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) net::IOBuffer* buf, 832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int buf_len, 842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const CompletionCallback& callback) OVERRIDE; 85c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) virtual int WriteData(int stream_index, 862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int offset, 872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) net::IOBuffer* buf, 882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int buf_len, 892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const CompletionCallback& callback, 902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bool truncate) OVERRIDE; 912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual int ReadSparseData(int64 offset, 922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) net::IOBuffer* buf, 932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int buf_len, 942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const CompletionCallback& callback) OVERRIDE; 952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual int WriteSparseData(int64 offset, 962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) net::IOBuffer* buf, 972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int buf_len, 982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const CompletionCallback& callback) OVERRIDE; 992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual int GetAvailableRange(int64 offset, 1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int len, 1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int64* start, 1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const CompletionCallback& callback) OVERRIDE; 1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual bool CouldBeSparse() const OVERRIDE; 1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual void CancelSparseIO() OVERRIDE; 1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual int ReadyForSparseIO(const CompletionCallback& callback) OVERRIDE; 1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private: 108c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) class ScopedOperationRunner; 109c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) friend class ScopedOperationRunner; 110c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 111c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) enum State { 112c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // The state immediately after construction, but before |synchronous_entry_| 113c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // has been assigned. This is the state at construction, and is the only 114c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // legal state to destruct an entry in. 115c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) STATE_UNINITIALIZED, 116c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 117c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // This entry is available for regular IO. 118c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) STATE_READY, 119c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 120c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // IO is currently in flight, operations must wait for completion before 121c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // launching. 122c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) STATE_IO_PENDING, 123c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 124c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // A failure occurred in the current or previous operation. All operations 125c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // after that must fail, until we receive a Close(). 126c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) STATE_FAILURE, 127c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) }; 1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 129868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Used in histograms, please only add entries at the end. 130868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) enum CheckCrcResult { 131868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) CRC_CHECK_NEVER_READ_TO_END = 0, 132868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) CRC_CHECK_NOT_DONE = 1, 133868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) CRC_CHECK_DONE = 2, 134a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) CRC_CHECK_NEVER_READ_AT_ALL = 3, 135a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) CRC_CHECK_MAX = 4, 136868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) }; 137868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual ~SimpleEntryImpl(); 1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 140d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // Must be used to invoke a client-provided completion callback for an 141d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // operation initiated through the backend (e.g. create, open) so that clients 142d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // don't get notified after they deleted the backend (which they would not 143d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // expect). 144d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) void PostClientCallback(const CompletionCallback& callback, int result); 145d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 146c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Sets entry to STATE_UNINITIALIZED. 147c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void MakeUninitialized(); 148c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 149c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Return this entry to a user of the API in |out_entry|. Increments the user 150c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // count. 151c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void ReturnEntryToCaller(Entry** out_entry); 152c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 153c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Ensures that |this| is no longer referenced by our |backend_|, this 154c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // guarantees that this entry cannot have OpenEntry/CreateEntry called again. 155c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void RemoveSelfFromBackend(); 156c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 157c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // An error occured, and the SimpleSynchronousEntry should have Doomed 158c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // us at this point. We need to remove |this| from the Backend and the 159c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // index. 160c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void MarkAsDoomed(); 161c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 162c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Runs the next operation in the queue, if any and if there is no other 163c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // operation running at the moment. 164c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // WARNING: May delete |this|, as an operation in the queue can contain 165c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // the last reference. 166c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void RunNextOperationIfNeeded(); 167c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 168a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) void OpenEntryInternal(bool have_index, 169a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) const CompletionCallback& callback, 170a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) Entry** out_entry); 171c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 172a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) void CreateEntryInternal(bool have_index, 173a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) const CompletionCallback& callback, 174c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) Entry** out_entry); 175c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 176c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void CloseInternal(); 177c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 178c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void ReadDataInternal(int index, 179c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) int offset, 180c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) net::IOBuffer* buf, 181c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) int buf_len, 182c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const CompletionCallback& callback); 183c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 184c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void WriteDataInternal(int index, 185c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) int offset, 186c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) net::IOBuffer* buf, 187c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) int buf_len, 188c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const CompletionCallback& callback, 189c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) bool truncate); 190c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 1910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) void ReadSparseDataInternal(int64 sparse_offset, 1920f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) net::IOBuffer* buf, 1930f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) int buf_len, 1940f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) const CompletionCallback& callback); 1950f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1960f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) void WriteSparseDataInternal(int64 sparse_offset, 1970f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) net::IOBuffer* buf, 1980f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) int buf_len, 1990f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) const CompletionCallback& callback); 2000f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2010f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) void GetAvailableRangeInternal(int64 sparse_offset, 2020f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) int len, 2030f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) int64* out_start, 2040f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) const CompletionCallback& callback); 2050f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 20658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) void DoomEntryInternal(const CompletionCallback& callback); 20758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 2082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Called after a SimpleSynchronousEntry has completed CreateEntry() or 209c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // OpenEntry(). If |in_sync_entry| is non-NULL, creation is successful and we 210c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // can return |this| SimpleEntryImpl to |*out_entry|. Runs 2112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // |completion_callback|. 212c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void CreationOperationComplete( 2132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const CompletionCallback& completion_callback, 214c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const base::TimeTicks& start_time, 2152385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch scoped_ptr<SimpleEntryCreationResults> in_results, 2162385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch Entry** out_entry, 2172385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch net::NetLog::EventType end_event_type); 218c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 219c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Called after we've closed and written the EOF record to our entry. Until 220c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // this point it hasn't been safe to OpenEntry() the same entry, but from this 221c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // point it is. 222c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void CloseOperationComplete(); 2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 224a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) // Internal utility method used by other completion methods. Calls 225a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) // |completion_callback| after updating state and dooming on errors. 2260f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) void EntryOperationComplete(const CompletionCallback& completion_callback, 227a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) const SimpleEntryStat& entry_stat, 228a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) scoped_ptr<int> result); 2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 230c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Called after an asynchronous read. Updates |crc32s_| if possible. 231a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) void ReadOperationComplete(int stream_index, 232a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) int offset, 233a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) const CompletionCallback& completion_callback, 234a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) scoped_ptr<uint32> read_crc32, 235d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) scoped_ptr<SimpleEntryStat> entry_stat, 236a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) scoped_ptr<int> result); 237c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 238a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) // Called after an asynchronous write completes. 239a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) void WriteOperationComplete(int stream_index, 240a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) const CompletionCallback& completion_callback, 241a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) scoped_ptr<SimpleEntryStat> entry_stat, 242a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) scoped_ptr<int> result); 243a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 2440f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) void ReadSparseOperationComplete( 2450f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) const CompletionCallback& completion_callback, 2460f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) scoped_ptr<base::Time> last_used, 2470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) scoped_ptr<int> result); 2480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) void WriteSparseOperationComplete( 2500f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) const CompletionCallback& completion_callback, 2510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) scoped_ptr<SimpleEntryStat> entry_stat, 2520f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) scoped_ptr<int> result); 2530f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2540f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) void GetAvailableRangeOperationComplete( 2550f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) const CompletionCallback& completion_callback, 2560f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) scoped_ptr<int> result); 2570f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 25858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // Called after an asynchronous doom completes. 25958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) void DoomOperationComplete(const CompletionCallback& callback, 26058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) State state_to_restore, 26158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) int result); 26258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 263c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Called after validating the checksums on an entry. Passes through the 264c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // original result if successful, propogates the error if the checksum does 265c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // not validate. 266c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void ChecksumOperationComplete( 267c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) int stream_index, 268c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) int orig_result, 269c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const CompletionCallback& completion_callback, 270c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) scoped_ptr<int> result); 271c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 272a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) // Called after completion of asynchronous IO and receiving file metadata for 273a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) // the entry in |entry_stat|. Updates the metadata in the entry and in the 274a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) // index to make them available on next IO operations. 275a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) void UpdateDataFromEntryStat(const SimpleEntryStat& entry_stat); 276a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) 277a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) int64 GetDiskUsage() const; 2782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 279bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch // Used to report histograms. 280bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch void RecordReadIsParallelizable(const SimpleEntryOperation& operation) const; 281bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch void RecordWriteDependencyType(const SimpleEntryOperation& operation) const; 282bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch 283d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // Reads from the stream 0 data kept in memory. 284d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) int ReadStream0Data(net::IOBuffer* buf, int offset, int buf_len); 285d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 286d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // Copies data from |buf| to the internal in-memory buffer for stream 0. If 287d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // |truncate| is set to true, the target buffer will be truncated at |offset| 288d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // + |buf_len| before being written. 289d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) int SetStream0Data(net::IOBuffer* buf, 290d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) int offset, int buf_len, 291d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) bool truncate); 292d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 293d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // Updates |crc32s_| and |crc32s_end_offset_| for a write of the data in 294d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // |buffer| on |stream_index|, starting at |offset| and of length |length|. 295d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) void AdvanceCrc(net::IOBuffer* buffer, 296d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) int offset, 297d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) int length, 298d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) int stream_index); 299d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 300c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // All nonstatic SimpleEntryImpl methods should always be called on the IO 301c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // thread, in all cases. |io_thread_checker_| documents and enforces this. 302c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::ThreadChecker io_thread_checker_; 3032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 30458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) const base::WeakPtr<SimpleBackendImpl> backend_; 30558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) const net::CacheType cache_type_; 3067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) const scoped_refptr<base::TaskRunner> worker_pool_; 3072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::FilePath path_; 308c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const uint64 entry_hash_; 309bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch const bool use_optimistic_operations_; 310eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch std::string key_; 3112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // |last_used_|, |last_modified_| and |data_size_| are copied from the 3132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // synchronous entry at the completion of each item of asynchronous IO. 314a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) // TODO(clamy): Unify last_used_ with data in the index. 3152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::Time last_used_; 3162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::Time last_modified_; 317d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) int32 data_size_[kSimpleEntryStreamCount]; 3180f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) int32 sparse_data_size_; 3192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 320c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Number of times this object has been returned from Backend::OpenEntry() and 321c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Backend::CreateEntry() without subsequent Entry::Close() calls. Used to 322c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // notify the backend when this entry not used by any callers. 323c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) int open_count_; 324c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 32558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) bool doomed_; 32658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 327c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) State state_; 328c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 329c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // When possible, we compute a crc32, for the data in each entry as we read or 330c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // write. For each stream, |crc32s_[index]| is the crc32 of that stream from 331c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // [0 .. |crc32s_end_offset_|). If |crc32s_end_offset_[index] == 0| then the 332c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // value of |crc32s_[index]| is undefined. 333d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) int32 crc32s_end_offset_[kSimpleEntryStreamCount]; 334d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) uint32 crc32s_[kSimpleEntryStreamCount]; 335c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 336d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // If |have_written_[index]| is true, we have written to the file that 337d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // contains stream |index|. 338d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) bool have_written_[kSimpleEntryStreamCount]; 339c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 340868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Reflects how much CRC checking has been done with the entry. This state is 341868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // reported on closing each entry stream. 342d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) CheckCrcResult crc_check_state_[kSimpleEntryStreamCount]; 343868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 3442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // The |synchronous_entry_| is the worker thread object that performs IO on 34558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // entries. It's owned by this SimpleEntryImpl whenever |executing_operation_| 34658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // is false (i.e. when an operation is not pending on the worker pool). When 34758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // an operation is being executed no one owns the synchronous entry. Therefore 34858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // SimpleEntryImpl should not be deleted while an operation is running as that 34958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // would leak the SimpleSynchronousEntry. 3502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) SimpleSynchronousEntry* synchronous_entry_; 3512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 352bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch std::queue<SimpleEntryOperation> pending_operations_; 3537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 3547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch net::BoundNetLog net_log_; 3559ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch 356bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch scoped_ptr<SimpleEntryOperation> executing_operation_; 357d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 358d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // Unlike other streams, stream 0 data is read from the disk when the entry is 359d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // opened, and then kept in memory. All read/write operations on stream 0 360d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // affect the |stream_0_data_| buffer. When the entry is closed, 361d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // |stream_0_data_| is written to the disk. 362d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // Stream 0 is kept in memory because it is stored in the same file as stream 363d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // 1 on disk, to reduce the number of file descriptors and save disk space. 364d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // This strategy allows stream 1 to change size easily. Since stream 0 is only 365d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // used to write HTTP headers, the memory consumption of keeping it in memory 366d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // is acceptable. 367d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) scoped_refptr<net::GrowableIOBuffer> stream_0_data_; 3682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}; 3692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} // namespace disk_cache 3712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif // NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_ 373