| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "net/disk_cache/blockfile/file.h" | 5 #include "net/disk_cache/blockfile/file.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/threading/worker_pool.h" | 10 #include "base/threading/worker_pool.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 | 40 |
| 41 // Read and Write are the operations that can be performed asynchronously. | 41 // Read and Write are the operations that can be performed asynchronously. |
| 42 // The actual parameters for the operation are setup in the constructor of | 42 // The actual parameters for the operation are setup in the constructor of |
| 43 // the object. Both methods should be called from a worker thread, by posting | 43 // the object. Both methods should be called from a worker thread, by posting |
| 44 // a task to the WorkerPool (they are RunnableMethods). When finished, | 44 // a task to the WorkerPool (they are RunnableMethods). When finished, |
| 45 // controller->OnIOComplete() is called. | 45 // controller->OnIOComplete() is called. |
| 46 void Read(); | 46 void Read(); |
| 47 void Write(); | 47 void Write(); |
| 48 | 48 |
| 49 private: | 49 private: |
| 50 virtual ~FileBackgroundIO() {} | 50 ~FileBackgroundIO() override {} |
| 51 | 51 |
| 52 disk_cache::FileIOCallback* callback_; | 52 disk_cache::FileIOCallback* callback_; |
| 53 | 53 |
| 54 disk_cache::File* file_; | 54 disk_cache::File* file_; |
| 55 const void* buf_; | 55 const void* buf_; |
| 56 size_t buf_len_; | 56 size_t buf_len_; |
| 57 size_t offset_; | 57 size_t offset_; |
| 58 | 58 |
| 59 DISALLOW_COPY_AND_ASSIGN(FileBackgroundIO); | 59 DISALLOW_COPY_AND_ASSIGN(FileBackgroundIO); |
| 60 }; | 60 }; |
| 61 | 61 |
| 62 | 62 |
| 63 // The specialized controller that keeps track of current operations. | 63 // The specialized controller that keeps track of current operations. |
| 64 class FileInFlightIO : public disk_cache::InFlightIO { | 64 class FileInFlightIO : public disk_cache::InFlightIO { |
| 65 public: | 65 public: |
| 66 FileInFlightIO() {} | 66 FileInFlightIO() {} |
| 67 virtual ~FileInFlightIO() {} | 67 ~FileInFlightIO() override {} |
| 68 | 68 |
| 69 // These methods start an asynchronous operation. The arguments have the same | 69 // These methods start an asynchronous operation. The arguments have the same |
| 70 // semantics of the File asynchronous operations, with the exception that the | 70 // semantics of the File asynchronous operations, with the exception that the |
| 71 // operation never finishes synchronously. | 71 // operation never finishes synchronously. |
| 72 void PostRead(disk_cache::File* file, void* buf, size_t buf_len, | 72 void PostRead(disk_cache::File* file, void* buf, size_t buf_len, |
| 73 size_t offset, disk_cache::FileIOCallback* callback); | 73 size_t offset, disk_cache::FileIOCallback* callback); |
| 74 void PostWrite(disk_cache::File* file, const void* buf, size_t buf_len, | 74 void PostWrite(disk_cache::File* file, const void* buf, size_t buf_len, |
| 75 size_t offset, disk_cache::FileIOCallback* callback); | 75 size_t offset, disk_cache::FileIOCallback* callback); |
| 76 | 76 |
| 77 protected: | 77 protected: |
| 78 // Invokes the users' completion callback at the end of the IO operation. | 78 // Invokes the users' completion callback at the end of the IO operation. |
| 79 // |cancel| is true if the actual task posted to the thread is still | 79 // |cancel| is true if the actual task posted to the thread is still |
| 80 // queued (because we are inside WaitForPendingIO), and false if said task is | 80 // queued (because we are inside WaitForPendingIO), and false if said task is |
| 81 // the one performing the call. | 81 // the one performing the call. |
| 82 virtual void OnOperationComplete(disk_cache::BackgroundIO* operation, | 82 void OnOperationComplete(disk_cache::BackgroundIO* operation, |
| 83 bool cancel) override; | 83 bool cancel) override; |
| 84 | 84 |
| 85 private: | 85 private: |
| 86 DISALLOW_COPY_AND_ASSIGN(FileInFlightIO); | 86 DISALLOW_COPY_AND_ASSIGN(FileInFlightIO); |
| 87 }; | 87 }; |
| 88 | 88 |
| 89 // --------------------------------------------------------------------------- | 89 // --------------------------------------------------------------------------- |
| 90 | 90 |
| 91 // Runs on a worker thread. | 91 // Runs on a worker thread. |
| 92 void FileBackgroundIO::Read() { | 92 void FileBackgroundIO::Read() { |
| 93 if (file_->Read(const_cast<void*>(buf_), buf_len_, offset_)) { | 93 if (file_->Read(const_cast<void*>(buf_), buf_len_, offset_)) { |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 return false; | 288 return false; |
| 289 | 289 |
| 290 GetFileInFlightIO()->PostWrite(this, buffer, buffer_len, offset, callback); | 290 GetFileInFlightIO()->PostWrite(this, buffer, buffer_len, offset, callback); |
| 291 | 291 |
| 292 if (completed) | 292 if (completed) |
| 293 *completed = false; | 293 *completed = false; |
| 294 return true; | 294 return true; |
| 295 } | 295 } |
| 296 | 296 |
| 297 } // namespace disk_cache | 297 } // namespace disk_cache |
| OLD | NEW |