OLD | NEW |
---|---|
1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2010 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/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/singleton.h" | 9 #include "base/lazy_instance.h" |
rvargas (doing something else)
2010/12/10 20:55:54
nit: order of includes.
| |
10 #include "net/disk_cache/disk_cache.h" | 10 #include "net/disk_cache/disk_cache.h" |
11 | 11 |
12 namespace { | 12 namespace { |
13 | 13 |
14 // Structure used for asynchronous operations. | 14 // Structure used for asynchronous operations. |
15 struct MyOverlapped { | 15 struct MyOverlapped { |
16 MyOverlapped(disk_cache::File* file, size_t offset, | 16 MyOverlapped(disk_cache::File* file, size_t offset, |
17 disk_cache::FileIOCallback* callback); | 17 disk_cache::FileIOCallback* callback); |
18 ~MyOverlapped() {} | 18 ~MyOverlapped() {} |
19 OVERLAPPED* overlapped() { | 19 OVERLAPPED* overlapped() { |
20 return &context_.overlapped; | 20 return &context_.overlapped; |
21 } | 21 } |
22 | 22 |
23 MessageLoopForIO::IOContext context_; | 23 MessageLoopForIO::IOContext context_; |
24 scoped_refptr<disk_cache::File> file_; | 24 scoped_refptr<disk_cache::File> file_; |
25 disk_cache::FileIOCallback* callback_; | 25 disk_cache::FileIOCallback* callback_; |
26 }; | 26 }; |
27 | 27 |
28 COMPILE_ASSERT(!offsetof(MyOverlapped, context_), starts_with_overlapped); | 28 COMPILE_ASSERT(!offsetof(MyOverlapped, context_), starts_with_overlapped); |
29 | 29 |
30 // Helper class to handle the IO completion notifications from the message loop. | 30 // Helper class to handle the IO completion notifications from the message loop. |
31 class CompletionHandler : public MessageLoopForIO::IOHandler { | 31 class CompletionHandler : public MessageLoopForIO::IOHandler { |
32 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, | 32 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, |
33 DWORD actual_bytes, DWORD error); | 33 DWORD actual_bytes, DWORD error); |
34 }; | 34 }; |
35 | 35 |
36 static ::base::LazyInstance<CompletionHandler> g_completion_handler( | |
rvargas (doing something else)
2010/12/10 20:55:54
nit: remove the initial ::
| |
37 base::LINKER_INITIALIZED); | |
38 | |
36 void CompletionHandler::OnIOCompleted(MessageLoopForIO::IOContext* context, | 39 void CompletionHandler::OnIOCompleted(MessageLoopForIO::IOContext* context, |
37 DWORD actual_bytes, DWORD error) { | 40 DWORD actual_bytes, DWORD error) { |
38 MyOverlapped* data = reinterpret_cast<MyOverlapped*>(context); | 41 MyOverlapped* data = reinterpret_cast<MyOverlapped*>(context); |
39 | 42 |
40 if (error) { | 43 if (error) { |
41 DCHECK(!actual_bytes); | 44 DCHECK(!actual_bytes); |
42 actual_bytes = static_cast<DWORD>(-1); | 45 actual_bytes = static_cast<DWORD>(-1); |
43 NOTREACHED(); | 46 NOTREACHED(); |
44 } | 47 } |
45 | 48 |
46 if (data->callback_) | 49 if (data->callback_) |
47 data->callback_->OnFileIOComplete(static_cast<int>(actual_bytes)); | 50 data->callback_->OnFileIOComplete(static_cast<int>(actual_bytes)); |
48 | 51 |
49 delete data; | 52 delete data; |
50 } | 53 } |
51 | 54 |
52 MyOverlapped::MyOverlapped(disk_cache::File* file, size_t offset, | 55 MyOverlapped::MyOverlapped(disk_cache::File* file, size_t offset, |
53 disk_cache::FileIOCallback* callback) { | 56 disk_cache::FileIOCallback* callback) { |
54 memset(this, 0, sizeof(*this)); | 57 memset(this, 0, sizeof(*this)); |
55 context_.handler = Singleton<CompletionHandler>::get(); | 58 context_.handler = g_completion_handler.Pointer(); |
56 context_.overlapped.Offset = static_cast<DWORD>(offset); | 59 context_.overlapped.Offset = static_cast<DWORD>(offset); |
57 file_ = file; | 60 file_ = file; |
58 callback_ = callback; | 61 callback_ = callback; |
59 } | 62 } |
60 | 63 |
61 } // namespace | 64 } // namespace |
62 | 65 |
63 namespace disk_cache { | 66 namespace disk_cache { |
64 | 67 |
65 File::File(base::PlatformFile file) | 68 File::File(base::PlatformFile file) |
66 : init_(true), mixed_(true), platform_file_(INVALID_HANDLE_VALUE), | 69 : init_(true), mixed_(true), platform_file_(INVALID_HANDLE_VALUE), |
67 sync_platform_file_(file) { | 70 sync_platform_file_(file) { |
68 } | 71 } |
69 | 72 |
70 bool File::Init(const FilePath& name) { | 73 bool File::Init(const FilePath& name) { |
71 DCHECK(!init_); | 74 DCHECK(!init_); |
72 if (init_) | 75 if (init_) |
73 return false; | 76 return false; |
74 | 77 |
75 DWORD sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; | 78 DWORD sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; |
76 DWORD access = GENERIC_READ | GENERIC_WRITE | DELETE; | 79 DWORD access = GENERIC_READ | GENERIC_WRITE | DELETE; |
77 platform_file_ = CreateFile(name.value().c_str(), access, sharing, NULL, | 80 platform_file_ = CreateFile(name.value().c_str(), access, sharing, NULL, |
78 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); | 81 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); |
79 | 82 |
80 if (INVALID_HANDLE_VALUE == platform_file_) | 83 if (INVALID_HANDLE_VALUE == platform_file_) |
81 return false; | 84 return false; |
82 | 85 |
83 MessageLoopForIO::current()->RegisterIOHandler( | 86 MessageLoopForIO::current()->RegisterIOHandler( |
84 platform_file_, Singleton<CompletionHandler>::get()); | 87 platform_file_, g_completion_handler.Pointer()); |
85 | 88 |
86 init_ = true; | 89 init_ = true; |
87 sync_platform_file_ = CreateFile(name.value().c_str(), access, sharing, NULL, | 90 sync_platform_file_ = CreateFile(name.value().c_str(), access, sharing, NULL, |
88 OPEN_EXISTING, 0, NULL); | 91 OPEN_EXISTING, 0, NULL); |
89 | 92 |
90 if (INVALID_HANDLE_VALUE == sync_platform_file_) | 93 if (INVALID_HANDLE_VALUE == sync_platform_file_) |
91 return false; | 94 return false; |
92 | 95 |
93 return true; | 96 return true; |
94 } | 97 } |
(...skipping 153 matching lines...) Loading... | |
248 return ULONG_MAX; | 251 return ULONG_MAX; |
249 | 252 |
250 return static_cast<size_t>(size.LowPart); | 253 return static_cast<size_t>(size.LowPart); |
251 } | 254 } |
252 | 255 |
253 // Static. | 256 // Static. |
254 void File::WaitForPendingIO(int* num_pending_io) { | 257 void File::WaitForPendingIO(int* num_pending_io) { |
255 while (*num_pending_io) { | 258 while (*num_pending_io) { |
256 // Asynchronous IO operations may be in flight and the completion may end | 259 // Asynchronous IO operations may be in flight and the completion may end |
257 // up calling us back so let's wait for them. | 260 // up calling us back so let's wait for them. |
258 MessageLoopForIO::IOHandler* handler = Singleton<CompletionHandler>::get(); | 261 MessageLoopForIO::IOHandler* handler = g_completion_handler.Pointer(); |
259 MessageLoopForIO::current()->WaitForIOCompletion(100, handler); | 262 MessageLoopForIO::current()->WaitForIOCompletion(100, handler); |
260 } | 263 } |
261 } | 264 } |
262 | 265 |
263 } // namespace disk_cache | 266 } // namespace disk_cache |
OLD | NEW |