OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef COMPONENTS_LEVELDB_LEVELDB_MOJO_PROXY_H_ | |
6 #define COMPONENTS_LEVELDB_LEVELDB_MOJO_PROXY_H_ | |
7 | |
8 #include <map> | |
9 #include <string> | |
10 #include <utility> | |
11 #include <vector> | |
12 | |
13 #include "base/files/file.h" | |
14 #include "base/memory/ref_counted.h" | |
15 #include "base/synchronization/waitable_event.h" | |
16 #include "base/threading/thread.h" | |
17 #include "components/filesystem/public/interfaces/directory.mojom.h" | |
18 | |
19 namespace leveldb { | |
20 | |
21 // A proxy for thread safe access to mojo objects from multiple threads. | |
22 // | |
23 // MojoEnv is an object passed to the leveldb implementation which can be | |
jam
2016/03/29 05:58:00
nit: just to be consistent with how these projects
| |
24 // called from multiple threads. mojo pipes are bound to a single | |
25 // thread. Because of this mismatch, we create a proxy object which will | |
26 // redirect calls to the thread which owns the mojo pipe, sends and receives | |
27 // messages. | |
28 // | |
29 // All public methods can be accessed from any thread. | |
30 class LevelDBMojoProxy : public base::RefCountedThreadSafe<LevelDBMojoProxy> { | |
31 public: | |
32 explicit LevelDBMojoProxy( | |
33 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | |
34 | |
35 // A private struct to hide the underlying file that holds the lock from our | |
36 // callers, forcing them to go through our LockFile()/UnlockFile() interface | |
37 // so that they don't try to use the underlying pointer from an unsafe thread. | |
38 struct OpaqueLock; | |
39 | |
40 // A private struct to hide the underlying root directory that we're | |
41 // operating in. LevelDBMojoProxy will want to own all the directory | |
42 // pointers, so while opening a database, we pass the directory to the thread | |
43 // it will be operated on. | |
44 struct OpaqueDir; | |
45 | |
46 // Passes ownership of a |directory| to the other thread, giving a reference | |
47 // handle back to the caller. | |
48 OpaqueDir* RegisterDirectory(filesystem::DirectoryPtr directory); | |
49 void UnregisterDirectory(OpaqueDir* dir); | |
50 | |
51 // Synchronously calls Directory.OpenFileHandle(). | |
52 base::File OpenFileHandle(OpaqueDir* dir, | |
53 const std::string& name, | |
54 uint32_t open_flags); | |
55 | |
56 // Synchronously flushes |directory_|. | |
jam
2016/03/29 05:58:00
nit: s/flushes/sync
| |
57 filesystem::FileError SyncDirectory(OpaqueDir* dir, const std::string& name); | |
58 | |
59 // Synchronously checks whether |name| exists. | |
60 bool FileExists(OpaqueDir* dir, const std::string& name); | |
61 | |
62 // Synchronously returns the filenames of all files in |path|. | |
63 filesystem::FileError GetChildren(OpaqueDir* dir, | |
64 const std::string& path, | |
65 std::vector<std::string>* result); | |
66 | |
67 // Synchronously deletes |path|. | |
68 filesystem::FileError Delete(OpaqueDir* dir, | |
69 const std::string& path, | |
70 uint32_t delete_flags); | |
71 | |
72 // Synchronously creates |path|. | |
73 filesystem::FileError CreateDir(OpaqueDir* dir, const std::string& path); | |
74 | |
75 // Synchronously gets the size of a file. | |
76 filesystem::FileError GetFileSize(OpaqueDir* dir, | |
77 const std::string& path, | |
78 uint64_t* file_size); | |
79 | |
80 // Synchronously renames a file. | |
81 filesystem::FileError RenameFile(OpaqueDir* dir, | |
82 const std::string& old_path, | |
83 const std::string& new_path); | |
84 | |
85 // Synchronously locks a file. Returns both the file return code, and if OK, | |
86 // an opaque object to the lock to enforce going through this interface to | |
87 // unlock the file so that unlocking happens on the correct thread. | |
88 std::pair<filesystem::FileError, OpaqueLock*> LockFile( | |
89 OpaqueDir* dir, | |
90 const std::string& path); | |
91 | |
92 // Unlocks a file. LevelDBMojoProxy takes ownership of lock. (We don't make | |
93 // this a scoped_ptr because exporting the ctor/dtor for this struct publicly | |
94 // defeats the purpose of the struct.) | |
95 filesystem::FileError UnlockFile(OpaqueLock* lock); | |
96 | |
97 private: | |
98 friend class base::RefCountedThreadSafe<LevelDBMojoProxy>; | |
99 ~LevelDBMojoProxy(); | |
100 | |
101 void SignalIfNeeded(base::WaitableEvent* done_event); | |
102 | |
103 // Implementation methods of the public interface. Depending on whether they | |
104 // were called from the thread that |task_runner_| is, these might be called | |
105 // directly or through PostTask(). | |
106 void RegisterDirectoryImpl( | |
107 mojo::InterfacePtrInfo<filesystem::Directory> directory_info, | |
108 base::WaitableEvent* done_event, | |
109 OpaqueDir** out_dir); | |
110 void UnregisterDirectoryImpl(OpaqueDir* dir, base::WaitableEvent* done_event); | |
111 void OpenFileHandleImpl(OpaqueDir* dir, | |
112 std::string name, | |
113 uint32_t open_flags, | |
114 base::WaitableEvent* done_event, | |
115 base::File* out_file); | |
116 void SyncDirectoryImpl(OpaqueDir* dir, | |
117 std::string name, | |
118 base::WaitableEvent* done_event, | |
119 filesystem::FileError* out_error); | |
120 void FileExistsImpl(OpaqueDir* dir, | |
121 std::string name, | |
122 base::WaitableEvent* done_event, | |
123 bool* exists); | |
124 void GetChildrenImpl(OpaqueDir* dir, | |
125 std::string name, | |
126 std::vector<std::string>* contents, | |
127 base::WaitableEvent* done_event, | |
128 filesystem::FileError* out_error); | |
129 void DeleteImpl(OpaqueDir* dir, | |
130 std::string name, | |
131 uint32_t delete_flags, | |
132 base::WaitableEvent* done_event, | |
133 filesystem::FileError* out_error); | |
134 void CreateDirImpl(OpaqueDir* dir, | |
135 std::string name, | |
136 base::WaitableEvent* done_event, | |
137 filesystem::FileError* out_error); | |
138 void GetFileSizeImpl(OpaqueDir* dir, | |
139 const std::string& path, | |
140 uint64_t* file_size, | |
141 base::WaitableEvent* done_event, | |
142 filesystem::FileError* out_error); | |
143 void RenameFileImpl(OpaqueDir* dir, | |
144 const std::string& old_path, | |
145 const std::string& new_path, | |
146 base::WaitableEvent* done_event, | |
147 filesystem::FileError* out_error); | |
148 void LockFileImpl(OpaqueDir* dir, | |
149 const std::string& path, | |
150 base::WaitableEvent* done_event, | |
151 filesystem::FileError* out_error, | |
152 OpaqueLock** out_lock); | |
153 void UnlockFileImpl(scoped_ptr<OpaqueLock> lock, | |
154 base::WaitableEvent* done_event, | |
155 filesystem::FileError* out_error); | |
156 | |
157 // The task runner which represents the thread that all mojo objects are | |
158 // bound to. | |
159 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
160 | |
161 int outstanding_opaque_dirs_; | |
162 | |
163 DISALLOW_COPY_AND_ASSIGN(LevelDBMojoProxy); | |
164 }; | |
165 | |
166 } // namespace leveldb | |
167 | |
168 #endif // COMPONENTS_LEVELDB_LEVELDB_MOJO_PROXY_H_ | |
OLD | NEW |