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" 14c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/threading/thread_checker.h" 1558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "net/base/cache_type.h" 16d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "net/base/net_export.h" 177dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "net/base/net_log.h" 182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "net/disk_cache/disk_cache.h" 19c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "net/disk_cache/simple/simple_entry_format.h" 20bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch#include "net/disk_cache/simple/simple_entry_operation.h" 21c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 22c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace base { 237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class TaskRunner; 24c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace net { 27d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)class GrowableIOBuffer; 282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class IOBuffer; 292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace disk_cache { 322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 33c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class SimpleBackendImpl; 342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class SimpleSynchronousEntry; 35d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)class SimpleEntryStat; 362385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdochstruct SimpleEntryCreationResults; 372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// SimpleEntryImpl is the IO thread interface to an entry in the very simple 392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// disk cache. It proxies for the SimpleSynchronousEntry, which performs IO 402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// on the worker thread. 41d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)class NET_EXPORT_PRIVATE SimpleEntryImpl : public Entry, 421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci public base::RefCounted<SimpleEntryImpl> { 43c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) friend class base::RefCounted<SimpleEntryImpl>; 442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public: 45bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch enum OperationsMode { 46bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch NON_OPTIMISTIC_OPERATIONS, 47bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch OPTIMISTIC_OPERATIONS, 48bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch }; 49bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch 501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // The Backend provides an |ActiveEntryProxy| instance to this entry when it 511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // is active, meaning it's the canonical entry for this |entry_hash_|. The 521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // entry can make itself inactive by deleting its proxy. 531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci class ActiveEntryProxy { 541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci public: 551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci virtual ~ActiveEntryProxy() = 0; 561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci }; 571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 5858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) SimpleEntryImpl(net::CacheType cache_type, 5958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) const base::FilePath& path, 607dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch uint64 entry_hash, 61bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch OperationsMode operations_mode, 62bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch SimpleBackendImpl* backend, 637dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch net::NetLog* net_log); 64c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci void SetActiveEntryProxy( 661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci scoped_ptr<ActiveEntryProxy> active_entry_proxy); 671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 68c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Adds another reader/writer to this entry, if possible, returning |this| to 69c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // |entry|. 70c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) int OpenEntry(Entry** entry, const CompletionCallback& callback); 71c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 72c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Creates this entry, if possible. Returns |this| to |entry|. 73c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) int CreateEntry(Entry** entry, const CompletionCallback& callback); 742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 75c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Identical to Backend::Doom() except that it accepts a CompletionCallback. 76c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) int DoomEntry(const CompletionCallback& callback); 772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 78c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const std::string& key() const { return key_; } 79c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) uint64 entry_hash() const { return entry_hash_; } 802385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch void SetKey(const std::string& key); 812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // From Entry: 832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual void Doom() OVERRIDE; 842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual void Close() OVERRIDE; 852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual std::string GetKey() const OVERRIDE; 862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual base::Time GetLastUsed() const OVERRIDE; 872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual base::Time GetLastModified() const OVERRIDE; 882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual int32 GetDataSize(int index) const OVERRIDE; 89c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) virtual int ReadData(int stream_index, 902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int offset, 912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) net::IOBuffer* buf, 922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int buf_len, 932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const CompletionCallback& callback) OVERRIDE; 94c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) virtual int WriteData(int stream_index, 952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int offset, 962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) net::IOBuffer* buf, 972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int buf_len, 982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const CompletionCallback& callback, 992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bool truncate) OVERRIDE; 1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual int ReadSparseData(int64 offset, 1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) net::IOBuffer* buf, 1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int buf_len, 1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const CompletionCallback& callback) OVERRIDE; 1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual int WriteSparseData(int64 offset, 1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) net::IOBuffer* buf, 1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int buf_len, 1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const CompletionCallback& callback) OVERRIDE; 1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual int GetAvailableRange(int64 offset, 1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int len, 1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int64* start, 1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const CompletionCallback& callback) OVERRIDE; 1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual bool CouldBeSparse() const OVERRIDE; 1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual void CancelSparseIO() OVERRIDE; 1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual int ReadyForSparseIO(const CompletionCallback& callback) OVERRIDE; 1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private: 117c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) class ScopedOperationRunner; 118c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) friend class ScopedOperationRunner; 119c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 120c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) enum State { 121c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // The state immediately after construction, but before |synchronous_entry_| 122c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // has been assigned. This is the state at construction, and is the only 123c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // legal state to destruct an entry in. 124c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) STATE_UNINITIALIZED, 125c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 126c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // This entry is available for regular IO. 127c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) STATE_READY, 128c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 129c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // IO is currently in flight, operations must wait for completion before 130c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // launching. 131c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) STATE_IO_PENDING, 132c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 133c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // A failure occurred in the current or previous operation. All operations 134c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // after that must fail, until we receive a Close(). 135c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) STATE_FAILURE, 136c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) }; 1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 138868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Used in histograms, please only add entries at the end. 139868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) enum CheckCrcResult { 140868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) CRC_CHECK_NEVER_READ_TO_END = 0, 141868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) CRC_CHECK_NOT_DONE = 1, 142868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) CRC_CHECK_DONE = 2, 143a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) CRC_CHECK_NEVER_READ_AT_ALL = 3, 144a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) CRC_CHECK_MAX = 4, 145868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) }; 146868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual ~SimpleEntryImpl(); 1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 149d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // Must be used to invoke a client-provided completion callback for an 150d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // operation initiated through the backend (e.g. create, open) so that clients 151d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // don't get notified after they deleted the backend (which they would not 152d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // expect). 153d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) void PostClientCallback(const CompletionCallback& callback, int result); 154d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 155c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Sets entry to STATE_UNINITIALIZED. 156c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void MakeUninitialized(); 157c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 158c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Return this entry to a user of the API in |out_entry|. Increments the user 159c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // count. 160c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void ReturnEntryToCaller(Entry** out_entry); 161c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 162c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // An error occured, and the SimpleSynchronousEntry should have Doomed 163c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // us at this point. We need to remove |this| from the Backend and the 164c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // index. 165c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void MarkAsDoomed(); 166c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 167c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Runs the next operation in the queue, if any and if there is no other 168c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // operation running at the moment. 169c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // WARNING: May delete |this|, as an operation in the queue can contain 170c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // the last reference. 171c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void RunNextOperationIfNeeded(); 172c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 173a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) void OpenEntryInternal(bool have_index, 174a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) const CompletionCallback& callback, 175a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) Entry** out_entry); 176c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 177a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) void CreateEntryInternal(bool have_index, 178a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) const CompletionCallback& callback, 179c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) Entry** out_entry); 180c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 181c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void CloseInternal(); 182c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 183c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void ReadDataInternal(int index, 184c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) int offset, 185c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) net::IOBuffer* buf, 186c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) int buf_len, 187c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const CompletionCallback& callback); 188c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 189c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void WriteDataInternal(int index, 190c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) int offset, 191c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) net::IOBuffer* buf, 192c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) int buf_len, 193c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const CompletionCallback& callback, 194c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) bool truncate); 195c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 1960f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) void ReadSparseDataInternal(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 WriteSparseDataInternal(int64 sparse_offset, 2020f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) net::IOBuffer* buf, 2030f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) int buf_len, 2040f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) const CompletionCallback& callback); 2050f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2060f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) void GetAvailableRangeInternal(int64 sparse_offset, 2070f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) int len, 2080f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) int64* out_start, 2090f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) const CompletionCallback& callback); 2100f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 21158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) void DoomEntryInternal(const CompletionCallback& callback); 21258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 2132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Called after a SimpleSynchronousEntry has completed CreateEntry() or 214c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // OpenEntry(). If |in_sync_entry| is non-NULL, creation is successful and we 215c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // can return |this| SimpleEntryImpl to |*out_entry|. Runs 2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // |completion_callback|. 217c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void CreationOperationComplete( 2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const CompletionCallback& completion_callback, 219c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const base::TimeTicks& start_time, 2202385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch scoped_ptr<SimpleEntryCreationResults> in_results, 2212385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch Entry** out_entry, 2222385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch net::NetLog::EventType end_event_type); 223c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 224c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Called after we've closed and written the EOF record to our entry. Until 225c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // this point it hasn't been safe to OpenEntry() the same entry, but from this 226c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // point it is. 227c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void CloseOperationComplete(); 2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 229a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) // Internal utility method used by other completion methods. Calls 230a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) // |completion_callback| after updating state and dooming on errors. 2310f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) void EntryOperationComplete(const CompletionCallback& completion_callback, 232a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) const SimpleEntryStat& entry_stat, 233a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) scoped_ptr<int> result); 2342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 235c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Called after an asynchronous read. Updates |crc32s_| if possible. 236a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) void ReadOperationComplete(int stream_index, 237a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) int offset, 238a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) const CompletionCallback& completion_callback, 239a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) scoped_ptr<uint32> read_crc32, 240d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) scoped_ptr<SimpleEntryStat> entry_stat, 241a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) scoped_ptr<int> result); 242c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 243a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) // Called after an asynchronous write completes. 244a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) void WriteOperationComplete(int stream_index, 245a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) const CompletionCallback& completion_callback, 246a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) scoped_ptr<SimpleEntryStat> entry_stat, 247a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) scoped_ptr<int> result); 248a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 2490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) void ReadSparseOperationComplete( 2500f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) const CompletionCallback& completion_callback, 2510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) scoped_ptr<base::Time> last_used, 2520f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) scoped_ptr<int> result); 2530f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2540f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) void WriteSparseOperationComplete( 2550f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) const CompletionCallback& completion_callback, 2560f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) scoped_ptr<SimpleEntryStat> entry_stat, 2570f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) scoped_ptr<int> result); 2580f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2590f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) void GetAvailableRangeOperationComplete( 2600f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) const CompletionCallback& completion_callback, 2610f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) scoped_ptr<int> result); 2620f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 26358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // Called after an asynchronous doom completes. 26458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) void DoomOperationComplete(const CompletionCallback& callback, 26558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) State state_to_restore, 26658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) int result); 26758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 268c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Called after validating the checksums on an entry. Passes through the 269c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // original result if successful, propogates the error if the checksum does 270c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // not validate. 271c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void ChecksumOperationComplete( 272c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) int stream_index, 273c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) int orig_result, 274c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const CompletionCallback& completion_callback, 275c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) scoped_ptr<int> result); 276c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 277a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) // Called after completion of asynchronous IO and receiving file metadata for 278a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) // the entry in |entry_stat|. Updates the metadata in the entry and in the 279a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) // index to make them available on next IO operations. 280a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) void UpdateDataFromEntryStat(const SimpleEntryStat& entry_stat); 281a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) 282a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) int64 GetDiskUsage() const; 2832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 284bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch // Used to report histograms. 285bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch void RecordReadIsParallelizable(const SimpleEntryOperation& operation) const; 286bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch void RecordWriteDependencyType(const SimpleEntryOperation& operation) const; 287bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch 288d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // Reads from the stream 0 data kept in memory. 289d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) int ReadStream0Data(net::IOBuffer* buf, int offset, int buf_len); 290d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 291d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // Copies data from |buf| to the internal in-memory buffer for stream 0. If 292d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // |truncate| is set to true, the target buffer will be truncated at |offset| 293d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // + |buf_len| before being written. 294d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) int SetStream0Data(net::IOBuffer* buf, 295d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) int offset, int buf_len, 296d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) bool truncate); 297d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 298d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // Updates |crc32s_| and |crc32s_end_offset_| for a write of the data in 299d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // |buffer| on |stream_index|, starting at |offset| and of length |length|. 300d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) void AdvanceCrc(net::IOBuffer* buffer, 301d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) int offset, 302d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) int length, 303d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) int stream_index); 304d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 3051320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci scoped_ptr<ActiveEntryProxy> active_entry_proxy_; 3061320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 307c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // All nonstatic SimpleEntryImpl methods should always be called on the IO 308c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // thread, in all cases. |io_thread_checker_| documents and enforces this. 309c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::ThreadChecker io_thread_checker_; 3102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 31158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) const base::WeakPtr<SimpleBackendImpl> backend_; 31258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) const net::CacheType cache_type_; 3137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) const scoped_refptr<base::TaskRunner> worker_pool_; 3142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::FilePath path_; 315c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const uint64 entry_hash_; 316bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch const bool use_optimistic_operations_; 317eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch std::string key_; 3182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // |last_used_|, |last_modified_| and |data_size_| are copied from the 3202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // synchronous entry at the completion of each item of asynchronous IO. 321a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) // TODO(clamy): Unify last_used_ with data in the index. 3222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::Time last_used_; 3232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::Time last_modified_; 324d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) int32 data_size_[kSimpleEntryStreamCount]; 3250f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) int32 sparse_data_size_; 3262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 327c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Number of times this object has been returned from Backend::OpenEntry() and 328c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Backend::CreateEntry() without subsequent Entry::Close() calls. Used to 329c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // notify the backend when this entry not used by any callers. 330c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) int open_count_; 331c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 33258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) bool doomed_; 33358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 334c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) State state_; 335c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 336c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // When possible, we compute a crc32, for the data in each entry as we read or 337c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // write. For each stream, |crc32s_[index]| is the crc32 of that stream from 338c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // [0 .. |crc32s_end_offset_|). If |crc32s_end_offset_[index] == 0| then the 339c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // value of |crc32s_[index]| is undefined. 340d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) int32 crc32s_end_offset_[kSimpleEntryStreamCount]; 341d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) uint32 crc32s_[kSimpleEntryStreamCount]; 342c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 343d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // If |have_written_[index]| is true, we have written to the file that 344d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // contains stream |index|. 345d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) bool have_written_[kSimpleEntryStreamCount]; 346c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 347868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Reflects how much CRC checking has been done with the entry. This state is 348868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // reported on closing each entry stream. 349d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) CheckCrcResult crc_check_state_[kSimpleEntryStreamCount]; 350868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 3512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // The |synchronous_entry_| is the worker thread object that performs IO on 35258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // entries. It's owned by this SimpleEntryImpl whenever |executing_operation_| 35358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // is false (i.e. when an operation is not pending on the worker pool). When 35458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // an operation is being executed no one owns the synchronous entry. Therefore 35558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // SimpleEntryImpl should not be deleted while an operation is running as that 35658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // would leak the SimpleSynchronousEntry. 3572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) SimpleSynchronousEntry* synchronous_entry_; 3582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 359bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch std::queue<SimpleEntryOperation> pending_operations_; 3607dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 3617dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch net::BoundNetLog net_log_; 3629ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch 363bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch scoped_ptr<SimpleEntryOperation> executing_operation_; 364d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 365d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // Unlike other streams, stream 0 data is read from the disk when the entry is 366d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // opened, and then kept in memory. All read/write operations on stream 0 367d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // affect the |stream_0_data_| buffer. When the entry is closed, 368d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // |stream_0_data_| is written to the disk. 369d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // Stream 0 is kept in memory because it is stored in the same file as stream 370d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // 1 on disk, to reduce the number of file descriptors and save disk space. 371d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // This strategy allows stream 1 to change size easily. Since stream 0 is only 372d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // used to write HTTP headers, the memory consumption of keeping it in memory 373d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // is acceptable. 374d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) scoped_refptr<net::GrowableIOBuffer> stream_0_data_; 3752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}; 3762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} // namespace disk_cache 3782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif // NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_ 380