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/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/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 }; | 28 }; |
29 | 29 |
30 base::LazyInstance<FileWorkerPool>::Leaky s_worker_pool = | 30 base::LazyInstance<FileWorkerPool>::Leaky s_worker_pool = |
31 LAZY_INSTANCE_INITIALIZER; | 31 LAZY_INSTANCE_INITIALIZER; |
32 | 32 |
33 } // namespace | 33 } // namespace |
34 | 34 |
35 namespace disk_cache { | 35 namespace disk_cache { |
36 | 36 |
37 File::File(base::File file) | 37 File::File(base::File file) |
38 : init_(true), | 38 : init_(true), mixed_(true), base_file_(file.Pass()) { |
39 mixed_(true), | |
40 base_file_(file.Pass()) { | |
41 } | 39 } |
42 | 40 |
43 bool File::Init(const base::FilePath& name) { | 41 bool File::Init(const base::FilePath& name) { |
44 if (base_file_.IsValid()) | 42 if (base_file_.IsValid()) |
45 return false; | 43 return false; |
46 | 44 |
47 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | | 45 int flags = |
48 base::File::FLAG_WRITE; | 46 base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_WRITE; |
49 base_file_.Initialize(name, flags); | 47 base_file_.Initialize(name, flags); |
50 return base_file_.IsValid(); | 48 return base_file_.IsValid(); |
51 } | 49 } |
52 | 50 |
53 bool File::IsValid() const { | 51 bool File::IsValid() const { |
54 return base_file_.IsValid(); | 52 return base_file_.IsValid(); |
55 } | 53 } |
56 | 54 |
57 bool File::Read(void* buffer, size_t buffer_len, size_t offset) { | 55 bool File::Read(void* buffer, size_t buffer_len, size_t offset) { |
58 DCHECK(base_file_.IsValid()); | 56 DCHECK(base_file_.IsValid()); |
59 if (buffer_len > static_cast<size_t>(kint32max) || | 57 if (buffer_len > static_cast<size_t>(kint32max) || |
60 offset > static_cast<size_t>(kint32max)) { | 58 offset > static_cast<size_t>(kint32max)) { |
61 return false; | 59 return false; |
62 } | 60 } |
63 | 61 |
64 int ret = base_file_.Read(offset, static_cast<char*>(buffer), buffer_len); | 62 int ret = base_file_.Read(offset, static_cast<char*>(buffer), buffer_len); |
65 return (static_cast<size_t>(ret) == buffer_len); | 63 return (static_cast<size_t>(ret) == buffer_len); |
66 } | 64 } |
67 | 65 |
68 bool File::Write(const void* buffer, size_t buffer_len, size_t offset) { | 66 bool File::Write(const void* buffer, size_t buffer_len, size_t offset) { |
69 DCHECK(base_file_.IsValid()); | 67 DCHECK(base_file_.IsValid()); |
70 if (buffer_len > static_cast<size_t>(kint32max) || | 68 if (buffer_len > static_cast<size_t>(kint32max) || |
71 offset > static_cast<size_t>(kint32max)) { | 69 offset > static_cast<size_t>(kint32max)) { |
72 return false; | 70 return false; |
73 } | 71 } |
74 | 72 |
75 int ret = base_file_.Write(offset, static_cast<const char*>(buffer), | 73 int ret = |
76 buffer_len); | 74 base_file_.Write(offset, static_cast<const char*>(buffer), buffer_len); |
77 return (static_cast<size_t>(ret) == buffer_len); | 75 return (static_cast<size_t>(ret) == buffer_len); |
78 } | 76 } |
79 | 77 |
80 bool File::Read(void* buffer, size_t buffer_len, size_t offset, | 78 bool File::Read(void* buffer, |
81 FileIOCallback* callback, bool* completed) { | 79 size_t buffer_len, |
| 80 size_t offset, |
| 81 FileIOCallback* callback, |
| 82 bool* completed) { |
82 DCHECK(base_file_.IsValid()); | 83 DCHECK(base_file_.IsValid()); |
83 if (!callback) { | 84 if (!callback) { |
84 if (completed) | 85 if (completed) |
85 *completed = true; | 86 *completed = true; |
86 return Read(buffer, buffer_len, offset); | 87 return Read(buffer, buffer_len, offset); |
87 } | 88 } |
88 | 89 |
89 if (buffer_len > static_cast<size_t>(kint32max) || | 90 if (buffer_len > static_cast<size_t>(kint32max) || |
90 offset > static_cast<size_t>(kint32max)) { | 91 offset > static_cast<size_t>(kint32max)) { |
91 return false; | 92 return false; |
92 } | 93 } |
93 | 94 |
94 base::PostTaskAndReplyWithResult( | 95 base::PostTaskAndReplyWithResult( |
95 s_worker_pool.Pointer(), FROM_HERE, | 96 s_worker_pool.Pointer(), |
| 97 FROM_HERE, |
96 base::Bind(&File::DoRead, this, buffer, buffer_len, offset), | 98 base::Bind(&File::DoRead, this, buffer, buffer_len, offset), |
97 base::Bind(&File::OnOperationComplete, this, callback)); | 99 base::Bind(&File::OnOperationComplete, this, callback)); |
98 | 100 |
99 *completed = false; | 101 *completed = false; |
100 return true; | 102 return true; |
101 } | 103 } |
102 | 104 |
103 bool File::Write(const void* buffer, size_t buffer_len, size_t offset, | 105 bool File::Write(const void* buffer, |
104 FileIOCallback* callback, bool* completed) { | 106 size_t buffer_len, |
| 107 size_t offset, |
| 108 FileIOCallback* callback, |
| 109 bool* completed) { |
105 DCHECK(base_file_.IsValid()); | 110 DCHECK(base_file_.IsValid()); |
106 if (!callback) { | 111 if (!callback) { |
107 if (completed) | 112 if (completed) |
108 *completed = true; | 113 *completed = true; |
109 return Write(buffer, buffer_len, offset); | 114 return Write(buffer, buffer_len, offset); |
110 } | 115 } |
111 | 116 |
112 if (buffer_len > static_cast<size_t>(kint32max) || | 117 if (buffer_len > static_cast<size_t>(kint32max) || |
113 offset > static_cast<size_t>(kint32max)) { | 118 offset > static_cast<size_t>(kint32max)) { |
114 return false; | 119 return false; |
115 } | 120 } |
116 | 121 |
117 base::PostTaskAndReplyWithResult( | 122 base::PostTaskAndReplyWithResult( |
118 s_worker_pool.Pointer(), FROM_HERE, | 123 s_worker_pool.Pointer(), |
| 124 FROM_HERE, |
119 base::Bind(&File::DoWrite, this, buffer, buffer_len, offset), | 125 base::Bind(&File::DoWrite, this, buffer, buffer_len, offset), |
120 base::Bind(&File::OnOperationComplete, this, callback)); | 126 base::Bind(&File::OnOperationComplete, this, callback)); |
121 | 127 |
122 *completed = false; | 128 *completed = false; |
123 return true; | 129 return true; |
124 } | 130 } |
125 | 131 |
126 bool File::SetLength(size_t length) { | 132 bool File::SetLength(size_t length) { |
127 DCHECK(base_file_.IsValid()); | 133 DCHECK(base_file_.IsValid()); |
128 if (length > kuint32max) | 134 if (length > kuint32max) |
(...skipping 18 matching lines...) Expand all Loading... |
147 // worker pool only waits for tasks on the worker pool, not the "Reply" tasks | 153 // worker pool only waits for tasks on the worker pool, not the "Reply" tasks |
148 // so we have to let the current message loop to run. | 154 // so we have to let the current message loop to run. |
149 s_worker_pool.Get().FlushForTesting(); | 155 s_worker_pool.Get().FlushForTesting(); |
150 base::RunLoop().RunUntilIdle(); | 156 base::RunLoop().RunUntilIdle(); |
151 } | 157 } |
152 | 158 |
153 // Static. | 159 // Static. |
154 void File::DropPendingIO() { | 160 void File::DropPendingIO() { |
155 } | 161 } |
156 | 162 |
157 | |
158 File::~File() { | 163 File::~File() { |
159 } | 164 } |
160 | 165 |
161 base::PlatformFile File::platform_file() const { | 166 base::PlatformFile File::platform_file() const { |
162 return base_file_.GetPlatformFile(); | 167 return base_file_.GetPlatformFile(); |
163 } | 168 } |
164 | 169 |
165 // Runs on a worker thread. | 170 // Runs on a worker thread. |
166 int File::DoRead(void* buffer, size_t buffer_len, size_t offset) { | 171 int File::DoRead(void* buffer, size_t buffer_len, size_t offset) { |
167 if (Read(const_cast<void*>(buffer), buffer_len, offset)) | 172 if (Read(const_cast<void*>(buffer), buffer_len, offset)) |
(...skipping 10 matching lines...) Expand all Loading... |
178 return net::ERR_CACHE_WRITE_FAILURE; | 183 return net::ERR_CACHE_WRITE_FAILURE; |
179 } | 184 } |
180 | 185 |
181 // This method actually makes sure that the last reference to the file doesn't | 186 // This method actually makes sure that the last reference to the file doesn't |
182 // go away on the worker pool. | 187 // go away on the worker pool. |
183 void File::OnOperationComplete(FileIOCallback* callback, int result) { | 188 void File::OnOperationComplete(FileIOCallback* callback, int result) { |
184 callback->OnFileIOComplete(result); | 189 callback->OnFileIOComplete(result); |
185 } | 190 } |
186 | 191 |
187 } // namespace disk_cache | 192 } // namespace disk_cache |
OLD | NEW |