OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/common/file_system/file_system_dispatcher.h" | |
6 | |
7 #include "base/file_util.h" | |
8 #include "base/process.h" | |
9 #include "content/common/child_thread.h" | |
10 #include "content/common/file_system_messages.h" | |
11 | |
12 FileSystemDispatcher::FileSystemDispatcher() { | |
13 } | |
14 | |
15 FileSystemDispatcher::~FileSystemDispatcher() { | |
16 // Make sure we fire all the remaining callbacks. | |
17 for (IDMap<fileapi::FileSystemCallbackDispatcher, IDMapOwnPointer>::iterator | |
18 iter(&dispatchers_); !iter.IsAtEnd(); iter.Advance()) { | |
19 int request_id = iter.GetCurrentKey(); | |
20 fileapi::FileSystemCallbackDispatcher* dispatcher = iter.GetCurrentValue(); | |
21 DCHECK(dispatcher); | |
22 dispatcher->DidFail(base::PLATFORM_FILE_ERROR_ABORT); | |
23 dispatchers_.Remove(request_id); | |
24 } | |
25 } | |
26 | |
27 bool FileSystemDispatcher::OnMessageReceived(const IPC::Message& msg) { | |
28 bool handled = true; | |
29 IPC_BEGIN_MESSAGE_MAP(FileSystemDispatcher, msg) | |
30 IPC_MESSAGE_HANDLER(FileSystemMsg_DidOpenFileSystem, OnDidOpenFileSystem) | |
31 IPC_MESSAGE_HANDLER(FileSystemMsg_DidSucceed, OnDidSucceed) | |
32 IPC_MESSAGE_HANDLER(FileSystemMsg_DidReadDirectory, OnDidReadDirectory) | |
33 IPC_MESSAGE_HANDLER(FileSystemMsg_DidReadMetadata, OnDidReadMetadata) | |
34 IPC_MESSAGE_HANDLER(FileSystemMsg_DidFail, OnDidFail) | |
35 IPC_MESSAGE_HANDLER(FileSystemMsg_DidWrite, OnDidWrite) | |
36 IPC_MESSAGE_HANDLER(FileSystemMsg_DidOpenFile, OnDidOpenFile) | |
37 IPC_MESSAGE_UNHANDLED(handled = false) | |
38 IPC_END_MESSAGE_MAP() | |
39 return handled; | |
40 } | |
41 | |
42 bool FileSystemDispatcher::OpenFileSystem( | |
43 const GURL& origin_url, fileapi::FileSystemType type, | |
44 long long size, bool create, | |
45 fileapi::FileSystemCallbackDispatcher* dispatcher) { | |
46 int request_id = dispatchers_.Add(dispatcher); | |
47 if (!ChildThread::current()->Send(new FileSystemHostMsg_Open( | |
48 request_id, origin_url, type, size, create))) { | |
49 dispatchers_.Remove(request_id); // destroys |dispatcher| | |
50 return false; | |
51 } | |
52 | |
53 return true; | |
54 } | |
55 | |
56 bool FileSystemDispatcher::Move( | |
57 const GURL& src_path, | |
58 const GURL& dest_path, | |
59 fileapi::FileSystemCallbackDispatcher* dispatcher) { | |
60 int request_id = dispatchers_.Add(dispatcher); | |
61 if (!ChildThread::current()->Send(new FileSystemHostMsg_Move( | |
62 request_id, src_path, dest_path))) { | |
63 dispatchers_.Remove(request_id); // destroys |dispatcher| | |
64 return false; | |
65 } | |
66 | |
67 return true; | |
68 } | |
69 | |
70 bool FileSystemDispatcher::Copy( | |
71 const GURL& src_path, | |
72 const GURL& dest_path, | |
73 fileapi::FileSystemCallbackDispatcher* dispatcher) { | |
74 int request_id = dispatchers_.Add(dispatcher); | |
75 if (!ChildThread::current()->Send(new FileSystemHostMsg_Copy( | |
76 request_id, src_path, dest_path))) { | |
77 dispatchers_.Remove(request_id); // destroys |dispatcher| | |
78 return false; | |
79 } | |
80 | |
81 return true; | |
82 } | |
83 | |
84 bool FileSystemDispatcher::Remove( | |
85 const GURL& path, | |
86 bool recursive, | |
87 fileapi::FileSystemCallbackDispatcher* dispatcher) { | |
88 int request_id = dispatchers_.Add(dispatcher); | |
89 if (!ChildThread::current()->Send( | |
90 new FileSystemMsg_Remove(request_id, path, recursive))) { | |
91 dispatchers_.Remove(request_id); // destroys |dispatcher| | |
92 return false; | |
93 } | |
94 | |
95 return true; | |
96 } | |
97 | |
98 bool FileSystemDispatcher::ReadMetadata( | |
99 const GURL& path, | |
100 fileapi::FileSystemCallbackDispatcher* dispatcher) { | |
101 int request_id = dispatchers_.Add(dispatcher); | |
102 if (!ChildThread::current()->Send( | |
103 new FileSystemHostMsg_ReadMetadata(request_id, path))) { | |
104 dispatchers_.Remove(request_id); // destroys |dispatcher| | |
105 return false; | |
106 } | |
107 | |
108 return true; | |
109 } | |
110 | |
111 bool FileSystemDispatcher::Create( | |
112 const GURL& path, | |
113 bool exclusive, | |
114 bool is_directory, | |
115 bool recursive, | |
116 fileapi::FileSystemCallbackDispatcher* dispatcher) { | |
117 int request_id = dispatchers_.Add(dispatcher); | |
118 if (!ChildThread::current()->Send(new FileSystemHostMsg_Create( | |
119 request_id, path, exclusive, is_directory, recursive))) { | |
120 dispatchers_.Remove(request_id); // destroys |dispatcher| | |
121 return false; | |
122 } | |
123 | |
124 return true; | |
125 } | |
126 | |
127 bool FileSystemDispatcher::Exists( | |
128 const GURL& path, | |
129 bool is_directory, | |
130 fileapi::FileSystemCallbackDispatcher* dispatcher) { | |
131 int request_id = dispatchers_.Add(dispatcher); | |
132 if (!ChildThread::current()->Send( | |
133 new FileSystemHostMsg_Exists(request_id, path, is_directory))) { | |
134 dispatchers_.Remove(request_id); // destroys |dispatcher| | |
135 return false; | |
136 } | |
137 | |
138 return true; | |
139 } | |
140 | |
141 bool FileSystemDispatcher::ReadDirectory( | |
142 const GURL& path, | |
143 fileapi::FileSystemCallbackDispatcher* dispatcher) { | |
144 int request_id = dispatchers_.Add(dispatcher); | |
145 if (!ChildThread::current()->Send( | |
146 new FileSystemHostMsg_ReadDirectory(request_id, path))) { | |
147 dispatchers_.Remove(request_id); // destroys |dispatcher| | |
148 return false; | |
149 } | |
150 | |
151 return true; | |
152 } | |
153 | |
154 bool FileSystemDispatcher::Truncate( | |
155 const GURL& path, | |
156 int64 offset, | |
157 int* request_id_out, | |
158 fileapi::FileSystemCallbackDispatcher* dispatcher) { | |
159 int request_id = dispatchers_.Add(dispatcher); | |
160 if (!ChildThread::current()->Send( | |
161 new FileSystemHostMsg_Truncate(request_id, path, offset))) { | |
162 dispatchers_.Remove(request_id); // destroys |dispatcher| | |
163 return false; | |
164 } | |
165 | |
166 if (request_id_out) | |
167 *request_id_out = request_id; | |
168 return true; | |
169 } | |
170 | |
171 bool FileSystemDispatcher::Write( | |
172 const GURL& path, | |
173 const GURL& blob_url, | |
174 int64 offset, | |
175 int* request_id_out, | |
176 fileapi::FileSystemCallbackDispatcher* dispatcher) { | |
177 int request_id = dispatchers_.Add(dispatcher); | |
178 if (!ChildThread::current()->Send( | |
179 new FileSystemHostMsg_Write(request_id, path, blob_url, offset))) { | |
180 dispatchers_.Remove(request_id); // destroys |dispatcher| | |
181 return false; | |
182 } | |
183 | |
184 if (request_id_out) | |
185 *request_id_out = request_id; | |
186 return true; | |
187 } | |
188 | |
189 bool FileSystemDispatcher::Cancel( | |
190 int request_id_to_cancel, | |
191 fileapi::FileSystemCallbackDispatcher* dispatcher) { | |
192 int request_id = dispatchers_.Add(dispatcher); | |
193 if (!ChildThread::current()->Send(new FileSystemHostMsg_CancelWrite( | |
194 request_id, request_id_to_cancel))) { | |
195 dispatchers_.Remove(request_id); // destroys |dispatcher| | |
196 return false; | |
197 } | |
198 | |
199 return true; | |
200 } | |
201 | |
202 bool FileSystemDispatcher::TouchFile( | |
203 const GURL& path, | |
204 const base::Time& last_access_time, | |
205 const base::Time& last_modified_time, | |
206 fileapi::FileSystemCallbackDispatcher* dispatcher) { | |
207 int request_id = dispatchers_.Add(dispatcher); | |
208 if (!ChildThread::current()->Send( | |
209 new FileSystemHostMsg_TouchFile( | |
210 request_id, path, last_access_time, last_modified_time))) { | |
211 dispatchers_.Remove(request_id); // destroys |dispatcher| | |
212 return false; | |
213 } | |
214 | |
215 return true; | |
216 } | |
217 | |
218 bool FileSystemDispatcher::OpenFile( | |
219 const GURL& file_path, | |
220 int file_flags, | |
221 fileapi::FileSystemCallbackDispatcher* dispatcher) { | |
222 int request_id = dispatchers_.Add(dispatcher); | |
223 if (!ChildThread::current()->Send( | |
224 new FileSystemHostMsg_OpenFile( | |
225 request_id, file_path, file_flags))) { | |
226 dispatchers_.Remove(request_id); // destroys |dispatcher| | |
227 return false; | |
228 } | |
229 | |
230 return true; | |
231 } | |
232 | |
233 bool FileSystemDispatcher::CreateSnapshotFile( | |
234 const GURL& blob_url, | |
235 const GURL& file_path, | |
236 fileapi::FileSystemCallbackDispatcher* dispatcher) { | |
237 int request_id = dispatchers_.Add(dispatcher); | |
238 if (!ChildThread::current()->Send( | |
239 new FileSystemHostMsg_CreateSnapshotFile( | |
240 request_id, blob_url, file_path))) { | |
241 dispatchers_.Remove(request_id); // destroys |dispatcher| | |
242 return false; | |
243 } | |
244 return true; | |
245 } | |
246 | |
247 void FileSystemDispatcher::OnDidOpenFileSystem(int request_id, | |
248 const std::string& name, | |
249 const GURL& root) { | |
250 DCHECK(root.is_valid()); | |
251 fileapi::FileSystemCallbackDispatcher* dispatcher = | |
252 dispatchers_.Lookup(request_id); | |
253 DCHECK(dispatcher); | |
254 dispatcher->DidOpenFileSystem(name, root); | |
255 dispatchers_.Remove(request_id); | |
256 } | |
257 | |
258 void FileSystemDispatcher::OnDidSucceed(int request_id) { | |
259 fileapi::FileSystemCallbackDispatcher* dispatcher = | |
260 dispatchers_.Lookup(request_id); | |
261 DCHECK(dispatcher); | |
262 dispatcher->DidSucceed(); | |
263 dispatchers_.Remove(request_id); | |
264 } | |
265 | |
266 void FileSystemDispatcher::OnDidReadMetadata( | |
267 int request_id, const base::PlatformFileInfo& file_info, | |
268 const FilePath& platform_path) { | |
269 fileapi::FileSystemCallbackDispatcher* dispatcher = | |
270 dispatchers_.Lookup(request_id); | |
271 DCHECK(dispatcher); | |
272 dispatcher->DidReadMetadata(file_info, platform_path); | |
273 dispatchers_.Remove(request_id); | |
274 } | |
275 | |
276 void FileSystemDispatcher::OnDidReadDirectory( | |
277 int request_id, | |
278 const std::vector<base::FileUtilProxy::Entry>& entries, | |
279 bool has_more) { | |
280 fileapi::FileSystemCallbackDispatcher* dispatcher = | |
281 dispatchers_.Lookup(request_id); | |
282 DCHECK(dispatcher); | |
283 dispatcher->DidReadDirectory(entries, has_more); | |
284 dispatchers_.Remove(request_id); | |
285 } | |
286 | |
287 void FileSystemDispatcher::OnDidFail( | |
288 int request_id, base::PlatformFileError error_code) { | |
289 fileapi::FileSystemCallbackDispatcher* dispatcher = | |
290 dispatchers_.Lookup(request_id); | |
291 DCHECK(dispatcher); | |
292 dispatcher->DidFail(error_code); | |
293 dispatchers_.Remove(request_id); | |
294 } | |
295 | |
296 void FileSystemDispatcher::OnDidWrite( | |
297 int request_id, int64 bytes, bool complete) { | |
298 fileapi::FileSystemCallbackDispatcher* dispatcher = | |
299 dispatchers_.Lookup(request_id); | |
300 DCHECK(dispatcher); | |
301 dispatcher->DidWrite(bytes, complete); | |
302 if (complete) | |
303 dispatchers_.Remove(request_id); | |
304 } | |
305 | |
306 void FileSystemDispatcher::OnDidOpenFile( | |
307 int request_id, IPC::PlatformFileForTransit file) { | |
308 fileapi::FileSystemCallbackDispatcher* dispatcher = | |
309 dispatchers_.Lookup(request_id); | |
310 DCHECK(dispatcher); | |
311 dispatcher->DidOpenFile(IPC::PlatformFileForTransitToPlatformFile(file), | |
312 base::kNullProcessHandle); | |
313 dispatchers_.Remove(request_id); | |
314 } | |
OLD | NEW |