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