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