| 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 #include "net/disk_cache/file.h" | 5 #include "net/disk_cache/file.h" |
| 6 | 6 |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 base::MessageLoopForIO::IOContext context_; | 24 base::MessageLoopForIO::IOContext context_; |
| 25 scoped_refptr<disk_cache::File> file_; | 25 scoped_refptr<disk_cache::File> file_; |
| 26 disk_cache::FileIOCallback* callback_; | 26 disk_cache::FileIOCallback* callback_; |
| 27 }; | 27 }; |
| 28 | 28 |
| 29 COMPILE_ASSERT(!offsetof(MyOverlapped, context_), starts_with_overlapped); | 29 COMPILE_ASSERT(!offsetof(MyOverlapped, context_), starts_with_overlapped); |
| 30 | 30 |
| 31 // Helper class to handle the IO completion notifications from the message loop. | 31 // Helper class to handle the IO completion notifications from the message loop. |
| 32 class CompletionHandler : public base::MessageLoopForIO::IOHandler { | 32 class CompletionHandler : public base::MessageLoopForIO::IOHandler { |
| 33 virtual void OnIOCompleted(base::MessageLoopForIO::IOContext* context, | 33 virtual void OnIOCompleted(base::MessageLoopForIO::IOContext* context, |
| 34 DWORD actual_bytes, | 34 DWORD actual_bytes, DWORD error); |
| 35 DWORD error); | |
| 36 }; | 35 }; |
| 37 | 36 |
| 38 static base::LazyInstance<CompletionHandler> g_completion_handler = | 37 static base::LazyInstance<CompletionHandler> g_completion_handler = |
| 39 LAZY_INSTANCE_INITIALIZER; | 38 LAZY_INSTANCE_INITIALIZER; |
| 40 | 39 |
| 41 void CompletionHandler::OnIOCompleted( | 40 void CompletionHandler::OnIOCompleted(base::MessageLoopForIO::IOContext* context
, |
| 42 base::MessageLoopForIO::IOContext* context, | 41 DWORD actual_bytes, DWORD error) { |
| 43 DWORD actual_bytes, | |
| 44 DWORD error) { | |
| 45 MyOverlapped* data = reinterpret_cast<MyOverlapped*>(context); | 42 MyOverlapped* data = reinterpret_cast<MyOverlapped*>(context); |
| 46 | 43 |
| 47 if (error) { | 44 if (error) { |
| 48 DCHECK(!actual_bytes); | 45 DCHECK(!actual_bytes); |
| 49 actual_bytes = static_cast<DWORD>(net::ERR_CACHE_READ_FAILURE); | 46 actual_bytes = static_cast<DWORD>(net::ERR_CACHE_READ_FAILURE); |
| 50 NOTREACHED(); | |
| 51 } | 47 } |
| 52 | 48 |
| 53 if (data->callback_) | 49 if (data->callback_) |
| 54 data->callback_->OnFileIOComplete(static_cast<int>(actual_bytes)); | 50 data->callback_->OnFileIOComplete(static_cast<int>(actual_bytes)); |
| 55 | 51 |
| 56 delete data; | 52 delete data; |
| 57 } | 53 } |
| 58 | 54 |
| 59 MyOverlapped::MyOverlapped(disk_cache::File* file, size_t offset, | 55 MyOverlapped::MyOverlapped(disk_cache::File* file, size_t offset, |
| 60 disk_cache::FileIOCallback* callback) { | 56 disk_cache::FileIOCallback* callback) { |
| 61 memset(this, 0, sizeof(*this)); | 57 memset(this, 0, sizeof(*this)); |
| 62 context_.handler = g_completion_handler.Pointer(); | 58 context_.handler = g_completion_handler.Pointer(); |
| 63 context_.overlapped.Offset = static_cast<DWORD>(offset); | 59 context_.overlapped.Offset = static_cast<DWORD>(offset); |
| 64 file_ = file; | 60 file_ = file; |
| 65 callback_ = callback; | 61 callback_ = callback; |
| 66 } | 62 } |
| 67 | 63 |
| 68 } // namespace | 64 } // namespace |
| 69 | 65 |
| 70 namespace disk_cache { | 66 namespace disk_cache { |
| 71 | 67 |
| 72 File::File(base::PlatformFile file) | 68 File::File(base::PlatformFile file) |
| 73 : init_(true), mixed_(true), platform_file_(INVALID_HANDLE_VALUE), | 69 : init_(true), |
| 70 mixed_(true), |
| 71 force_creation_(false), |
| 72 platform_file_(INVALID_HANDLE_VALUE), |
| 74 sync_platform_file_(file) { | 73 sync_platform_file_(file) { |
| 75 } | 74 } |
| 76 | 75 |
| 77 bool File::Init(const base::FilePath& name) { | 76 bool File::Init(const base::FilePath& name) { |
| 78 DCHECK(!init_); | 77 DCHECK(!init_); |
| 79 if (init_) | 78 if (init_) |
| 80 return false; | 79 return false; |
| 81 | 80 |
| 82 DWORD sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; | 81 DWORD sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; |
| 83 DWORD access = GENERIC_READ | GENERIC_WRITE | DELETE; | 82 DWORD access = GENERIC_READ | GENERIC_WRITE | DELETE; |
| 83 DWORD mode = force_creation_ ? OPEN_ALWAYS : OPEN_EXISTING; |
| 84 platform_file_ = CreateFile(name.value().c_str(), access, sharing, NULL, | 84 platform_file_ = CreateFile(name.value().c_str(), access, sharing, NULL, |
| 85 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); | 85 mode, FILE_FLAG_OVERLAPPED, NULL); |
| 86 | 86 |
| 87 if (INVALID_HANDLE_VALUE == platform_file_) | 87 if (INVALID_HANDLE_VALUE == platform_file_) |
| 88 return false; | 88 return false; |
| 89 | 89 |
| 90 base::MessageLoopForIO::current()->RegisterIOHandler( | 90 base::MessageLoopForIO::current()->RegisterIOHandler( |
| 91 platform_file_, g_completion_handler.Pointer()); | 91 platform_file_, g_completion_handler.Pointer()); |
| 92 | 92 |
| 93 init_ = true; | 93 init_ = true; |
| 94 sync_platform_file_ = CreateFile(name.value().c_str(), access, sharing, NULL, | 94 sync_platform_file_ = CreateFile(name.value().c_str(), access, sharing, NULL, |
| 95 OPEN_EXISTING, 0, NULL); | 95 OPEN_EXISTING, 0, NULL); |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 void File::WaitForPendingIO(int* num_pending_io) { | 261 void File::WaitForPendingIO(int* num_pending_io) { |
| 262 while (*num_pending_io) { | 262 while (*num_pending_io) { |
| 263 // Asynchronous IO operations may be in flight and the completion may end | 263 // Asynchronous IO operations may be in flight and the completion may end |
| 264 // up calling us back so let's wait for them. | 264 // up calling us back so let's wait for them. |
| 265 base::MessageLoopForIO::IOHandler* handler = g_completion_handler.Pointer(); | 265 base::MessageLoopForIO::IOHandler* handler = g_completion_handler.Pointer(); |
| 266 base::MessageLoopForIO::current()->WaitForIOCompletion(100, handler); | 266 base::MessageLoopForIO::current()->WaitForIOCompletion(100, handler); |
| 267 } | 267 } |
| 268 } | 268 } |
| 269 | 269 |
| 270 } // namespace disk_cache | 270 } // namespace disk_cache |
| OLD | NEW |