OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/leveldb/env_mojo.h" | 5 #include "components/leveldb/env_mojo.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "base/task_scheduler/post_task.h" |
12 #include "base/trace_event/trace_event.h" | 13 #include "base/trace_event/trace_event.h" |
13 #include "third_party/leveldatabase/chromium_logger.h" | 14 #include "third_party/leveldatabase/chromium_logger.h" |
| 15 #include "third_party/leveldatabase/env_chromium.h" |
14 #include "third_party/leveldatabase/src/include/leveldb/status.h" | 16 #include "third_party/leveldatabase/src/include/leveldb/status.h" |
15 | 17 |
16 namespace leveldb { | 18 namespace leveldb { |
17 | 19 |
18 namespace { | 20 namespace { |
19 | 21 |
20 const base::FilePath::CharType table_extension[] = FILE_PATH_LITERAL(".ldb"); | 22 const base::FilePath::CharType table_extension[] = FILE_PATH_LITERAL(".ldb"); |
21 | 23 |
22 base::File::Error LastFileError() { | 24 base::File::Error LastFileError() { |
23 #if defined(OS_WIN) | 25 #if defined(OS_WIN) |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 std::string filename_; | 210 std::string filename_; |
209 base::File file_; | 211 base::File file_; |
210 Type file_type_; | 212 Type file_type_; |
211 LevelDBMojoProxy::OpaqueDir* dir_; | 213 LevelDBMojoProxy::OpaqueDir* dir_; |
212 std::string parent_dir_; | 214 std::string parent_dir_; |
213 scoped_refptr<LevelDBMojoProxy> thread_; | 215 scoped_refptr<LevelDBMojoProxy> thread_; |
214 | 216 |
215 DISALLOW_COPY_AND_ASSIGN(MojoWritableFile); | 217 DISALLOW_COPY_AND_ASSIGN(MojoWritableFile); |
216 }; | 218 }; |
217 | 219 |
| 220 class Thread : public base::PlatformThread::Delegate { |
| 221 public: |
| 222 Thread(void (*function)(void* arg), void* arg) |
| 223 : function_(function), arg_(arg) { |
| 224 base::PlatformThreadHandle handle; |
| 225 bool success = base::PlatformThread::Create(0, this, &handle); |
| 226 DCHECK(success); |
| 227 } |
| 228 ~Thread() override {} |
| 229 void ThreadMain() override { |
| 230 (*function_)(arg_); |
| 231 delete this; |
| 232 } |
| 233 |
| 234 private: |
| 235 void (*function_)(void* arg); |
| 236 void* arg_; |
| 237 |
| 238 DISALLOW_COPY_AND_ASSIGN(Thread); |
| 239 }; |
| 240 |
218 } // namespace | 241 } // namespace |
219 | 242 |
220 MojoEnv::MojoEnv(const std::string& name, | 243 MojoEnv::MojoEnv(scoped_refptr<LevelDBMojoProxy> file_thread, |
221 scoped_refptr<LevelDBMojoProxy> file_thread, | |
222 LevelDBMojoProxy::OpaqueDir* dir) | 244 LevelDBMojoProxy::OpaqueDir* dir) |
223 : ChromiumEnv(name), thread_(file_thread), dir_(dir) {} | 245 : thread_(file_thread), dir_(dir) {} |
224 | 246 |
225 MojoEnv::~MojoEnv() { | 247 MojoEnv::~MojoEnv() { |
226 thread_->UnregisterDirectory(dir_); | 248 thread_->UnregisterDirectory(dir_); |
227 } | 249 } |
228 | 250 |
229 Status MojoEnv::NewSequentialFile(const std::string& fname, | 251 Status MojoEnv::NewSequentialFile(const std::string& fname, |
230 SequentialFile** result) { | 252 SequentialFile** result) { |
231 TRACE_EVENT1("leveldb", "MojoEnv::NewSequentialFile", "fname", fname); | 253 TRACE_EVENT1("leveldb", "MojoEnv::NewSequentialFile", "fname", fname); |
232 base::File f = thread_->OpenFileHandle( | 254 base::File f = thread_->OpenFileHandle( |
233 dir_, fname, filesystem::mojom::kFlagOpen | filesystem::mojom::kFlagRead); | 255 dir_, fname, filesystem::mojom::kFlagOpen | filesystem::mojom::kFlagRead); |
234 if (!f.IsValid()) { | 256 if (!f.IsValid()) { |
235 *result = nullptr; | 257 *result = nullptr; |
236 return MakeIOError(fname, "Unable to create sequential file", | 258 return MakeIOError(fname, "Unable to create sequential file", |
237 leveldb_env::kNewSequentialFile, f.error_details()); | 259 leveldb_env::kNewSequentialFile, f.error_details()); |
238 } | 260 } |
239 | 261 |
240 *result = new MojoSequentialFile(fname, std::move(f)); | 262 *result = new MojoSequentialFile(fname, std::move(f)); |
241 return Status::OK(); | 263 return Status::OK(); |
242 } | 264 } |
243 | 265 |
244 Status MojoEnv::NewRandomAccessFile(const std::string& fname, | 266 Status MojoEnv::NewRandomAccessFile(const std::string& fname, |
245 RandomAccessFile** result) { | 267 RandomAccessFile** result) { |
246 TRACE_EVENT1("leveldb", "MojoEnv::NewRandomAccessFile", "fname", fname); | 268 TRACE_EVENT1("leveldb", "MojoEnv::NewRandomAccessFile", "fname", fname); |
247 base::File f = thread_->OpenFileHandle( | 269 base::File f = thread_->OpenFileHandle( |
248 dir_, fname, filesystem::mojom::kFlagRead | filesystem::mojom::kFlagOpen); | 270 dir_, fname, filesystem::mojom::kFlagRead | filesystem::mojom::kFlagOpen); |
249 if (!f.IsValid()) { | 271 if (!f.IsValid()) { |
250 *result = nullptr; | 272 *result = nullptr; |
251 base::File::Error error_code = f.error_details(); | 273 base::File::Error error_code = f.error_details(); |
252 return MakeIOError(fname, FileErrorString(error_code), | 274 return MakeIOError(fname, base::File::ErrorToString(error_code), |
253 leveldb_env::kNewRandomAccessFile, error_code); | 275 leveldb_env::kNewRandomAccessFile, error_code); |
254 } | 276 } |
255 | 277 |
256 *result = new MojoRandomAccessFile(fname, std::move(f)); | 278 *result = new MojoRandomAccessFile(fname, std::move(f)); |
257 return Status::OK(); | 279 return Status::OK(); |
258 } | 280 } |
259 | 281 |
260 Status MojoEnv::NewWritableFile(const std::string& fname, | 282 Status MojoEnv::NewWritableFile(const std::string& fname, |
261 WritableFile** result) { | 283 WritableFile** result) { |
262 TRACE_EVENT1("leveldb", "MojoEnv::NewWritableFile", "fname", fname); | 284 TRACE_EVENT1("leveldb", "MojoEnv::NewWritableFile", "fname", fname); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 if (!f.IsValid()) { | 396 if (!f.IsValid()) { |
375 *result = NULL; | 397 *result = NULL; |
376 return MakeIOError(fname, "Unable to create log file", | 398 return MakeIOError(fname, "Unable to create log file", |
377 leveldb_env::kNewLogger, f.error_details()); | 399 leveldb_env::kNewLogger, f.error_details()); |
378 } else { | 400 } else { |
379 *result = new leveldb::ChromiumLogger(std::move(f)); | 401 *result = new leveldb::ChromiumLogger(std::move(f)); |
380 return Status::OK(); | 402 return Status::OK(); |
381 } | 403 } |
382 } | 404 } |
383 | 405 |
| 406 uint64_t MojoEnv::NowMicros() { |
| 407 return base::TimeTicks::Now().ToInternalValue(); |
| 408 } |
| 409 |
| 410 void MojoEnv::SleepForMicroseconds(int micros) { |
| 411 // Round up to the next millisecond. |
| 412 base::PlatformThread::Sleep(base::TimeDelta::FromMicroseconds(micros)); |
| 413 } |
| 414 |
| 415 void MojoEnv::Schedule(void (*function)(void* arg), void* arg) { |
| 416 base::PostTask(FROM_HERE, base::Bind(function, arg)); |
| 417 } |
| 418 |
| 419 void MojoEnv::StartThread(void (*function)(void* arg), void* arg) { |
| 420 new Thread(function, arg); // Will self-delete. |
| 421 } |
| 422 |
384 } // namespace leveldb | 423 } // namespace leveldb |
OLD | NEW |