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