1 // Copyright (c) 2006-2008 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 // See net/disk_cache/disk_cache.h for the public interface of the cache. 6 7 #ifndef NET_DISK_CACHE_FILE_H_ 8 #define NET_DISK_CACHE_FILE_H_ 9 10 #include <string> 11 12 #include "base/platform_file.h" 13 #include "base/ref_counted.h" 14 15 class FilePath; 16 17 namespace disk_cache { 18 19 // This interface is used to support asynchronous ReadData and WriteData calls. 20 class FileIOCallback { 21 public: 22 // Notified of the actual number of bytes read or written. This value is 23 // negative if an error occurred. 24 virtual void OnFileIOComplete(int bytes_copied) = 0; 25 ~FileIOCallback()26 virtual ~FileIOCallback() {} 27 }; 28 29 // Simple wrapper around a file that allows asynchronous operations. 30 class File : public base::RefCounted<File> { 31 friend class base::RefCounted<File>; 32 public: File()33 File() : init_(false), mixed_(false) {} 34 // mixed_mode set to true enables regular synchronous operations for the file. File(bool mixed_mode)35 explicit File(bool mixed_mode) : init_(false), mixed_(mixed_mode) {} 36 37 // Initializes the object to use the passed in file instead of opening it with 38 // the Init() call. No asynchronous operations can be performed with this 39 // object. 40 explicit File(base::PlatformFile file); 41 42 // Initializes the object to point to a given file. The file must aready exist 43 // on disk, and allow shared read and write. 44 bool Init(const FilePath& name); 45 46 // Returns the handle or file descriptor. 47 base::PlatformFile platform_file() const; 48 49 // Returns true if the file was opened properly. 50 bool IsValid() const; 51 52 // Performs synchronous IO. 53 bool Read(void* buffer, size_t buffer_len, size_t offset); 54 bool Write(const void* buffer, size_t buffer_len, size_t offset); 55 56 // Performs asynchronous IO. callback will be called when the IO completes, 57 // as an APC on the thread that queued the operation. 58 bool Read(void* buffer, size_t buffer_len, size_t offset, 59 FileIOCallback* callback, bool* completed); 60 bool Write(const void* buffer, size_t buffer_len, size_t offset, 61 FileIOCallback* callback, bool* completed); 62 63 // Performs asynchronous writes, but doesn't notify when done. Automatically 64 // deletes buffer when done. 65 bool PostWrite(const void* buffer, size_t buffer_len, size_t offset); 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 WaitForPendingIO(int* num_pending_io); 74 75 protected: 76 virtual ~File(); 77 78 // Performs the actual asynchronous write. If notify is set and there is no 79 // callback, the call will be re-synchronized. 80 bool AsyncWrite(const void* buffer, size_t buffer_len, size_t offset, 81 bool notify, FileIOCallback* callback, bool* completed); 82 83 private: 84 bool init_; 85 bool mixed_; 86 base::PlatformFile platform_file_; // Regular, asynchronous IO handle. 87 base::PlatformFile sync_platform_file_; // Synchronous IO handle. 88 89 DISALLOW_COPY_AND_ASSIGN(File); 90 }; 91 92 } // namespace disk_cache 93 94 #endif // NET_DISK_CACHE_FILE_H_ 95