1*6777b538SAndroid Build Coastguard Worker // Copyright 2012 The Chromium Authors 2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file. 4*6777b538SAndroid Build Coastguard Worker 5*6777b538SAndroid Build Coastguard Worker // See net/disk_cache/disk_cache.h for the public interface of the cache. 6*6777b538SAndroid Build Coastguard Worker 7*6777b538SAndroid Build Coastguard Worker #ifndef NET_DISK_CACHE_BLOCKFILE_FILE_H_ 8*6777b538SAndroid Build Coastguard Worker #define NET_DISK_CACHE_BLOCKFILE_FILE_H_ 9*6777b538SAndroid Build Coastguard Worker 10*6777b538SAndroid Build Coastguard Worker #include <stddef.h> 11*6777b538SAndroid Build Coastguard Worker 12*6777b538SAndroid Build Coastguard Worker #include "base/files/file.h" 13*6777b538SAndroid Build Coastguard Worker #include "base/memory/ref_counted.h" 14*6777b538SAndroid Build Coastguard Worker #include "net/base/net_export.h" 15*6777b538SAndroid Build Coastguard Worker 16*6777b538SAndroid Build Coastguard Worker namespace base { 17*6777b538SAndroid Build Coastguard Worker class FilePath; 18*6777b538SAndroid Build Coastguard Worker } 19*6777b538SAndroid Build Coastguard Worker 20*6777b538SAndroid Build Coastguard Worker namespace disk_cache { 21*6777b538SAndroid Build Coastguard Worker 22*6777b538SAndroid Build Coastguard Worker // This interface is used to support asynchronous ReadData and WriteData calls. 23*6777b538SAndroid Build Coastguard Worker class FileIOCallback { 24*6777b538SAndroid Build Coastguard Worker public: 25*6777b538SAndroid Build Coastguard Worker // Notified of the actual number of bytes read or written. This value is 26*6777b538SAndroid Build Coastguard Worker // negative if an error occurred. 27*6777b538SAndroid Build Coastguard Worker virtual void OnFileIOComplete(int bytes_copied) = 0; 28*6777b538SAndroid Build Coastguard Worker 29*6777b538SAndroid Build Coastguard Worker protected: 30*6777b538SAndroid Build Coastguard Worker virtual ~FileIOCallback() = default; 31*6777b538SAndroid Build Coastguard Worker }; 32*6777b538SAndroid Build Coastguard Worker 33*6777b538SAndroid Build Coastguard Worker // Simple wrapper around a file that allows asynchronous operations. 34*6777b538SAndroid Build Coastguard Worker class NET_EXPORT_PRIVATE File : public base::RefCounted<File> { 35*6777b538SAndroid Build Coastguard Worker friend class base::RefCounted<File>; 36*6777b538SAndroid Build Coastguard Worker public: 37*6777b538SAndroid Build Coastguard Worker File(); 38*6777b538SAndroid Build Coastguard Worker // mixed_mode set to true enables regular synchronous operations for the file. 39*6777b538SAndroid Build Coastguard Worker explicit File(bool mixed_mode); 40*6777b538SAndroid Build Coastguard Worker 41*6777b538SAndroid Build Coastguard Worker // Initializes the object to use the passed in file instead of opening it with 42*6777b538SAndroid Build Coastguard Worker // the Init() call. No asynchronous operations can be performed with this 43*6777b538SAndroid Build Coastguard Worker // object. 44*6777b538SAndroid Build Coastguard Worker explicit File(base::File file); 45*6777b538SAndroid Build Coastguard Worker 46*6777b538SAndroid Build Coastguard Worker File(const File&) = delete; 47*6777b538SAndroid Build Coastguard Worker File& operator=(const File&) = delete; 48*6777b538SAndroid Build Coastguard Worker 49*6777b538SAndroid Build Coastguard Worker // Initializes the object to point to a given file. The file must aready exist 50*6777b538SAndroid Build Coastguard Worker // on disk, and allow shared read and write. 51*6777b538SAndroid Build Coastguard Worker bool Init(const base::FilePath& name); 52*6777b538SAndroid Build Coastguard Worker 53*6777b538SAndroid Build Coastguard Worker // Returns true if the file was opened properly. 54*6777b538SAndroid Build Coastguard Worker bool IsValid() const; 55*6777b538SAndroid Build Coastguard Worker 56*6777b538SAndroid Build Coastguard Worker // Performs synchronous IO. 57*6777b538SAndroid Build Coastguard Worker bool Read(void* buffer, size_t buffer_len, size_t offset); 58*6777b538SAndroid Build Coastguard Worker bool Write(const void* buffer, size_t buffer_len, size_t offset); 59*6777b538SAndroid Build Coastguard Worker 60*6777b538SAndroid Build Coastguard Worker // Performs asynchronous IO. callback will be called when the IO completes, 61*6777b538SAndroid Build Coastguard Worker // as an APC on the thread that queued the operation. 62*6777b538SAndroid Build Coastguard Worker bool Read(void* buffer, size_t buffer_len, size_t offset, 63*6777b538SAndroid Build Coastguard Worker FileIOCallback* callback, bool* completed); 64*6777b538SAndroid Build Coastguard Worker bool Write(const void* buffer, size_t buffer_len, size_t offset, 65*6777b538SAndroid Build Coastguard Worker FileIOCallback* callback, bool* completed); 66*6777b538SAndroid Build Coastguard Worker 67*6777b538SAndroid Build Coastguard Worker // Sets the file's length. The file is truncated or extended with zeros to 68*6777b538SAndroid Build Coastguard Worker // the new length. 69*6777b538SAndroid Build Coastguard Worker bool SetLength(size_t length); 70*6777b538SAndroid Build Coastguard Worker size_t GetLength(); 71*6777b538SAndroid Build Coastguard Worker 72*6777b538SAndroid Build Coastguard Worker // Blocks until |num_pending_io| IO operations complete. 73*6777b538SAndroid Build Coastguard Worker static void WaitForPendingIOForTesting(int* num_pending_io); 74*6777b538SAndroid Build Coastguard Worker 75*6777b538SAndroid Build Coastguard Worker // Drops current pending operations without waiting for them to complete. 76*6777b538SAndroid Build Coastguard Worker static void DropPendingIO(); 77*6777b538SAndroid Build Coastguard Worker 78*6777b538SAndroid Build Coastguard Worker protected: 79*6777b538SAndroid Build Coastguard Worker virtual ~File(); 80*6777b538SAndroid Build Coastguard Worker 81*6777b538SAndroid Build Coastguard Worker // Returns the handle or file descriptor. 82*6777b538SAndroid Build Coastguard Worker base::PlatformFile platform_file() const; 83*6777b538SAndroid Build Coastguard Worker 84*6777b538SAndroid Build Coastguard Worker private: 85*6777b538SAndroid Build Coastguard Worker // Performs the actual asynchronous write. If notify is set and there is no 86*6777b538SAndroid Build Coastguard Worker // callback, the call will be re-synchronized. 87*6777b538SAndroid Build Coastguard Worker bool AsyncWrite(const void* buffer, size_t buffer_len, size_t offset, 88*6777b538SAndroid Build Coastguard Worker FileIOCallback* callback, bool* completed); 89*6777b538SAndroid Build Coastguard Worker 90*6777b538SAndroid Build Coastguard Worker // Infrastructure for async IO. 91*6777b538SAndroid Build Coastguard Worker int DoRead(void* buffer, size_t buffer_len, size_t offset); 92*6777b538SAndroid Build Coastguard Worker int DoWrite(const void* buffer, size_t buffer_len, size_t offset); 93*6777b538SAndroid Build Coastguard Worker void OnOperationComplete(FileIOCallback* callback, int result); 94*6777b538SAndroid Build Coastguard Worker 95*6777b538SAndroid Build Coastguard Worker bool init_; 96*6777b538SAndroid Build Coastguard Worker bool mixed_; 97*6777b538SAndroid Build Coastguard Worker base::File base_file_; // Regular, asynchronous IO handle. 98*6777b538SAndroid Build Coastguard Worker base::File sync_base_file_; // Synchronous IO handle. 99*6777b538SAndroid Build Coastguard Worker }; 100*6777b538SAndroid Build Coastguard Worker 101*6777b538SAndroid Build Coastguard Worker } // namespace disk_cache 102*6777b538SAndroid Build Coastguard Worker 103*6777b538SAndroid Build Coastguard Worker #endif // NET_DISK_CACHE_BLOCKFILE_FILE_H_ 104