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 #ifndef COMPONENTS_LEVELDB_ENV_MOJO_H_ | 5 #ifndef COMPONENTS_LEVELDB_ENV_MOJO_H_ |
6 #define COMPONENTS_LEVELDB_ENV_MOJO_H_ | 6 #define COMPONENTS_LEVELDB_ENV_MOJO_H_ |
7 | 7 |
| 8 #include "base/id_map.h" |
8 #include "components/filesystem/public/interfaces/directory.mojom.h" | 9 #include "components/filesystem/public/interfaces/directory.mojom.h" |
9 #include "components/leveldb/leveldb_mojo_proxy.h" | 10 #include "components/leveldb/leveldb_mojo_proxy.h" |
10 #include "third_party/leveldatabase/env_chromium.h" | 11 #include "third_party/leveldatabase/env_chromium.h" |
11 #include "third_party/leveldatabase/src/include/leveldb/env.h" | 12 #include "third_party/leveldatabase/src/include/leveldb/env.h" |
12 | 13 |
13 namespace leveldb { | 14 namespace leveldb { |
14 | 15 |
15 // An implementation of the leveldb operating system interaction code which | 16 // An implementation of the leveldb operating system interaction code which |
16 // proxies to a specified mojo:filesystem directory. Most of these methods are | 17 // proxies to a specified mojo:filesystem directory. Most of these methods are |
17 // synchronous and block on responses from the filesystem service. That's fine | 18 // synchronous and block on responses from the filesystem service. That's fine |
18 // since, for the most part, they merely open files or check for a file's | 19 // since, for the most part, they merely open files or check for a file's |
19 // existence. | 20 // existence. |
20 class MojoEnv : public leveldb_env::ChromiumEnv { | 21 class MojoEnv : public leveldb_env::ChromiumEnv { |
21 public: | 22 public: |
22 MojoEnv(const std::string& name, | 23 static MojoEnv* Get( |
23 scoped_refptr<LevelDBMojoProxy> file_thread, | 24 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner); |
24 LevelDBMojoProxy::OpaqueDir* dir); | 25 |
25 ~MojoEnv() override; | 26 // Registers a |directory| with the leveldb environment, returning a string |
| 27 // that should be used as prefix to indicate files in that directory. |
| 28 std::string RegisterDirectory(filesystem::mojom::DirectoryPtr directory); |
| 29 void UnregisterDirectory(base::StringPiece prefix); |
26 | 30 |
27 // Overridden from leveldb_env::EnvChromium: | 31 // Overridden from leveldb_env::EnvChromium: |
28 Status NewSequentialFile(const std::string& fname, | 32 Status NewSequentialFile(const std::string& fname, |
29 SequentialFile** result) override; | 33 SequentialFile** result) override; |
30 Status NewRandomAccessFile(const std::string& fname, | 34 Status NewRandomAccessFile(const std::string& fname, |
31 RandomAccessFile** result) override; | 35 RandomAccessFile** result) override; |
32 Status NewWritableFile(const std::string& fname, | 36 Status NewWritableFile(const std::string& fname, |
33 WritableFile** result) override; | 37 WritableFile** result) override; |
34 Status NewAppendableFile(const std::string& fname, | 38 Status NewAppendableFile(const std::string& fname, |
35 WritableFile** result) override; | 39 WritableFile** result) override; |
36 bool FileExists(const std::string& fname) override; | 40 bool FileExists(const std::string& fname) override; |
37 Status GetChildren(const std::string& dir, | 41 Status GetChildren(const std::string& dir, |
38 std::vector<std::string>* result) override; | 42 std::vector<std::string>* result) override; |
39 Status DeleteFile(const std::string& fname) override; | 43 Status DeleteFile(const std::string& fname) override; |
40 Status CreateDir(const std::string& dirname) override; | 44 Status CreateDir(const std::string& dirname) override; |
41 Status DeleteDir(const std::string& dirname) override; | 45 Status DeleteDir(const std::string& dirname) override; |
42 Status GetFileSize(const std::string& fname, uint64_t* file_size) override; | 46 Status GetFileSize(const std::string& fname, uint64_t* file_size) override; |
43 Status RenameFile(const std::string& src, const std::string& target) override; | 47 Status RenameFile(const std::string& src, const std::string& target) override; |
44 Status LockFile(const std::string& fname, FileLock** lock) override; | 48 Status LockFile(const std::string& fname, FileLock** lock) override; |
45 Status UnlockFile(FileLock* lock) override; | 49 Status UnlockFile(FileLock* lock) override; |
46 Status GetTestDirectory(std::string* path) override; | 50 Status GetTestDirectory(std::string* path) override; |
47 Status NewLogger(const std::string& fname, Logger** result) override; | 51 Status NewLogger(const std::string& fname, Logger** result) override; |
48 | 52 |
49 // For reference, we specifically don't override Schedule(), StartThread(), | 53 // For reference, we specifically don't override Schedule(), StartThread(), |
50 // NowMicros() or SleepForMicroseconds() and use the EnvChromium versions. | 54 // NowMicros() or SleepForMicroseconds() and use the EnvChromium versions. |
51 | 55 |
52 private: | 56 private: |
| 57 MojoEnv(scoped_refptr<base::SingleThreadTaskRunner> file_task_runner); |
| 58 ~MojoEnv() override; |
| 59 |
| 60 void ParsePath(const std::string& path, |
| 61 LevelDBMojoProxy::OpaqueDir** out_dir, |
| 62 std::string* out_path); |
| 63 |
53 scoped_refptr<LevelDBMojoProxy> thread_; | 64 scoped_refptr<LevelDBMojoProxy> thread_; |
54 LevelDBMojoProxy::OpaqueDir* dir_; | 65 IDMap<LevelDBMojoProxy::OpaqueDir*> dirs_; |
55 | 66 |
56 DISALLOW_COPY_AND_ASSIGN(MojoEnv); | 67 DISALLOW_COPY_AND_ASSIGN(MojoEnv); |
57 }; | 68 }; |
58 | 69 |
59 } // namespace leveldb | 70 } // namespace leveldb |
60 | 71 |
61 #endif // COMPONENTS_LEVELDB_ENV_MOJO_H_ | 72 #endif // COMPONENTS_LEVELDB_ENV_MOJO_H_ |
OLD | NEW |