OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "webkit/browser/fileapi/file_system_operation_runner.h" | 5 #include "webkit/browser/fileapi/file_system_operation_runner.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "net/url_request/url_request_context.h" | 8 #include "net/url_request/url_request_context.h" |
9 #include "webkit/browser/fileapi/file_observers.h" | 9 #include "webkit/browser/fileapi/file_observers.h" |
10 #include "webkit/browser/fileapi/file_stream_writer.h" | 10 #include "webkit/browser/fileapi/file_stream_writer.h" |
11 #include "webkit/browser/fileapi/file_system_context.h" | 11 #include "webkit/browser/fileapi/file_system_context.h" |
12 #include "webkit/browser/fileapi/file_system_operation_impl.h" | 12 #include "webkit/browser/fileapi/file_system_operation_impl.h" |
13 #include "webkit/browser/fileapi/file_writer_delegate.h" | 13 #include "webkit/browser/fileapi/file_writer_delegate.h" |
14 #include "webkit/common/blob/shareable_file_reference.h" | 14 #include "webkit/common/blob/shareable_file_reference.h" |
15 | 15 |
16 namespace fileapi { | 16 namespace fileapi { |
17 | 17 |
18 typedef FileSystemOperationRunner::OperationID OperationID; | 18 typedef FileSystemOperationRunner::OperationID OperationID; |
19 | 19 |
20 const OperationID FileSystemOperationRunner::kErrorOperationID = -1; | |
21 | |
22 FileSystemOperationRunner::~FileSystemOperationRunner() { | 20 FileSystemOperationRunner::~FileSystemOperationRunner() { |
23 } | 21 } |
24 | 22 |
25 void FileSystemOperationRunner::Shutdown() { | 23 void FileSystemOperationRunner::Shutdown() { |
26 operations_.Clear(); | 24 operations_.Clear(); |
27 } | 25 } |
28 | 26 |
29 OperationID FileSystemOperationRunner::CreateFile( | 27 OperationID FileSystemOperationRunner::CreateFile( |
30 const FileSystemURL& url, | 28 const FileSystemURL& url, |
31 bool exclusive, | 29 bool exclusive, |
32 const StatusCallback& callback) { | 30 const StatusCallback& callback) { |
33 base::PlatformFileError error = base::PLATFORM_FILE_OK; | 31 base::PlatformFileError error = base::PLATFORM_FILE_OK; |
34 FileSystemOperation* operation = | 32 FileSystemOperation* operation = |
35 file_system_context_->CreateFileSystemOperation(url, &error); | 33 file_system_context_->CreateFileSystemOperation(url, &error); |
| 34 OperationID id = operations_.Add(operation); |
36 if (!operation) { | 35 if (!operation) { |
37 callback.Run(error); | 36 DidFinish(id, callback, error); |
38 return kErrorOperationID; | 37 } else { |
| 38 PrepareForWrite(id, url); |
| 39 operation->CreateFile( |
| 40 url, exclusive, |
| 41 base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), |
| 42 id, callback)); |
39 } | 43 } |
40 OperationID id = operations_.Add(operation); | |
41 PrepareForWrite(id, url); | |
42 operation->CreateFile( | |
43 url, exclusive, | |
44 base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), | |
45 id, callback)); | |
46 return id; | 44 return id; |
47 } | 45 } |
48 | 46 |
49 OperationID FileSystemOperationRunner::CreateDirectory( | 47 OperationID FileSystemOperationRunner::CreateDirectory( |
50 const FileSystemURL& url, | 48 const FileSystemURL& url, |
51 bool exclusive, | 49 bool exclusive, |
52 bool recursive, | 50 bool recursive, |
53 const StatusCallback& callback) { | 51 const StatusCallback& callback) { |
54 base::PlatformFileError error = base::PLATFORM_FILE_OK; | 52 base::PlatformFileError error = base::PLATFORM_FILE_OK; |
55 FileSystemOperation* operation = | 53 FileSystemOperation* operation = |
56 file_system_context_->CreateFileSystemOperation(url, &error); | 54 file_system_context_->CreateFileSystemOperation(url, &error); |
| 55 OperationID id = operations_.Add(operation); |
57 if (!operation) { | 56 if (!operation) { |
58 callback.Run(error); | 57 DidFinish(id, callback, error); |
59 return kErrorOperationID; | 58 } else { |
| 59 PrepareForWrite(id, url); |
| 60 operation->CreateDirectory( |
| 61 url, exclusive, recursive, |
| 62 base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), |
| 63 id, callback)); |
60 } | 64 } |
61 OperationID id = operations_.Add(operation); | |
62 PrepareForWrite(id, url); | |
63 operation->CreateDirectory( | |
64 url, exclusive, recursive, | |
65 base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), | |
66 id, callback)); | |
67 return id; | 65 return id; |
68 } | 66 } |
69 | 67 |
70 OperationID FileSystemOperationRunner::Copy( | 68 OperationID FileSystemOperationRunner::Copy( |
71 const FileSystemURL& src_url, | 69 const FileSystemURL& src_url, |
72 const FileSystemURL& dest_url, | 70 const FileSystemURL& dest_url, |
73 const StatusCallback& callback) { | 71 const StatusCallback& callback) { |
74 base::PlatformFileError error = base::PLATFORM_FILE_OK; | 72 base::PlatformFileError error = base::PLATFORM_FILE_OK; |
75 FileSystemOperation* operation = | 73 FileSystemOperation* operation = |
76 file_system_context_->CreateFileSystemOperation(dest_url, &error); | 74 file_system_context_->CreateFileSystemOperation(dest_url, &error); |
| 75 OperationID id = operations_.Add(operation); |
77 if (!operation) { | 76 if (!operation) { |
78 callback.Run(error); | 77 DidFinish(id, callback, error); |
79 return kErrorOperationID; | 78 } else { |
| 79 PrepareForWrite(id, dest_url); |
| 80 PrepareForRead(id, src_url); |
| 81 operation->Copy( |
| 82 src_url, dest_url, |
| 83 base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), |
| 84 id, callback)); |
80 } | 85 } |
81 OperationID id = operations_.Add(operation); | |
82 PrepareForWrite(id, dest_url); | |
83 PrepareForRead(id, src_url); | |
84 operation->Copy( | |
85 src_url, dest_url, | |
86 base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), | |
87 id, callback)); | |
88 return id; | 86 return id; |
89 } | 87 } |
90 | 88 |
91 OperationID FileSystemOperationRunner::Move( | 89 OperationID FileSystemOperationRunner::Move( |
92 const FileSystemURL& src_url, | 90 const FileSystemURL& src_url, |
93 const FileSystemURL& dest_url, | 91 const FileSystemURL& dest_url, |
94 const StatusCallback& callback) { | 92 const StatusCallback& callback) { |
95 base::PlatformFileError error = base::PLATFORM_FILE_OK; | 93 base::PlatformFileError error = base::PLATFORM_FILE_OK; |
96 FileSystemOperation* operation = | 94 FileSystemOperation* operation = |
97 file_system_context_->CreateFileSystemOperation(dest_url, &error); | 95 file_system_context_->CreateFileSystemOperation(dest_url, &error); |
| 96 OperationID id = operations_.Add(operation); |
98 if (!operation) { | 97 if (!operation) { |
99 callback.Run(error); | 98 DidFinish(id, callback, error); |
100 return kErrorOperationID; | 99 } else { |
| 100 PrepareForWrite(id, dest_url); |
| 101 PrepareForWrite(id, src_url); |
| 102 operation->Move( |
| 103 src_url, dest_url, |
| 104 base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), |
| 105 id, callback)); |
101 } | 106 } |
102 OperationID id = operations_.Add(operation); | |
103 PrepareForWrite(id, dest_url); | |
104 PrepareForWrite(id, src_url); | |
105 operation->Move( | |
106 src_url, dest_url, | |
107 base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), | |
108 id, callback)); | |
109 return id; | 107 return id; |
110 } | 108 } |
111 | 109 |
112 OperationID FileSystemOperationRunner::DirectoryExists( | 110 OperationID FileSystemOperationRunner::DirectoryExists( |
113 const FileSystemURL& url, | 111 const FileSystemURL& url, |
114 const StatusCallback& callback) { | 112 const StatusCallback& callback) { |
115 base::PlatformFileError error = base::PLATFORM_FILE_OK; | 113 base::PlatformFileError error = base::PLATFORM_FILE_OK; |
116 FileSystemOperation* operation = | 114 FileSystemOperation* operation = |
117 file_system_context_->CreateFileSystemOperation(url, &error); | 115 file_system_context_->CreateFileSystemOperation(url, &error); |
| 116 OperationID id = operations_.Add(operation); |
118 if (!operation) { | 117 if (!operation) { |
119 callback.Run(error); | 118 DidFinish(id, callback, error); |
120 return kErrorOperationID; | 119 } else { |
| 120 PrepareForRead(id, url); |
| 121 operation->DirectoryExists( |
| 122 url, |
| 123 base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), |
| 124 id, callback)); |
121 } | 125 } |
122 OperationID id = operations_.Add(operation); | |
123 PrepareForRead(id, url); | |
124 operation->DirectoryExists( | |
125 url, | |
126 base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), | |
127 id, callback)); | |
128 return id; | 126 return id; |
129 } | 127 } |
130 | 128 |
131 OperationID FileSystemOperationRunner::FileExists( | 129 OperationID FileSystemOperationRunner::FileExists( |
132 const FileSystemURL& url, | 130 const FileSystemURL& url, |
133 const StatusCallback& callback) { | 131 const StatusCallback& callback) { |
134 base::PlatformFileError error = base::PLATFORM_FILE_OK; | 132 base::PlatformFileError error = base::PLATFORM_FILE_OK; |
135 FileSystemOperation* operation = | 133 FileSystemOperation* operation = |
136 file_system_context_->CreateFileSystemOperation(url, &error); | 134 file_system_context_->CreateFileSystemOperation(url, &error); |
| 135 OperationID id = operations_.Add(operation); |
137 if (!operation) { | 136 if (!operation) { |
138 callback.Run(error); | 137 DidFinish(id, callback, error); |
139 return kErrorOperationID; | 138 } else { |
| 139 PrepareForRead(id, url); |
| 140 operation->FileExists( |
| 141 url, |
| 142 base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), |
| 143 id, callback)); |
140 } | 144 } |
141 OperationID id = operations_.Add(operation); | |
142 PrepareForRead(id, url); | |
143 operation->FileExists( | |
144 url, | |
145 base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), | |
146 id, callback)); | |
147 return id; | 145 return id; |
148 } | 146 } |
149 | 147 |
150 OperationID FileSystemOperationRunner::GetMetadata( | 148 OperationID FileSystemOperationRunner::GetMetadata( |
151 const FileSystemURL& url, | 149 const FileSystemURL& url, |
152 const GetMetadataCallback& callback) { | 150 const GetMetadataCallback& callback) { |
153 base::PlatformFileError error = base::PLATFORM_FILE_OK; | 151 base::PlatformFileError error = base::PLATFORM_FILE_OK; |
154 FileSystemOperation* operation = | 152 FileSystemOperation* operation = |
155 file_system_context_->CreateFileSystemOperation(url, &error); | 153 file_system_context_->CreateFileSystemOperation(url, &error); |
| 154 OperationID id = operations_.Add(operation); |
156 if (!operation) { | 155 if (!operation) { |
157 callback.Run(error, base::PlatformFileInfo()); | 156 DidGetMetadata(id, callback, error, base::PlatformFileInfo()); |
158 return kErrorOperationID; | 157 } else { |
| 158 PrepareForRead(id, url); |
| 159 operation->GetMetadata( |
| 160 url, |
| 161 base::Bind(&FileSystemOperationRunner::DidGetMetadata, AsWeakPtr(), |
| 162 id, callback)); |
159 } | 163 } |
160 OperationID id = operations_.Add(operation); | |
161 PrepareForRead(id, url); | |
162 operation->GetMetadata( | |
163 url, | |
164 base::Bind(&FileSystemOperationRunner::DidGetMetadata, AsWeakPtr(), | |
165 id, callback)); | |
166 return id; | 164 return id; |
167 } | 165 } |
168 | 166 |
169 OperationID FileSystemOperationRunner::ReadDirectory( | 167 OperationID FileSystemOperationRunner::ReadDirectory( |
170 const FileSystemURL& url, | 168 const FileSystemURL& url, |
171 const ReadDirectoryCallback& callback) { | 169 const ReadDirectoryCallback& callback) { |
172 base::PlatformFileError error = base::PLATFORM_FILE_OK; | 170 base::PlatformFileError error = base::PLATFORM_FILE_OK; |
173 FileSystemOperation* operation = | 171 FileSystemOperation* operation = |
174 file_system_context_->CreateFileSystemOperation(url, &error); | 172 file_system_context_->CreateFileSystemOperation(url, &error); |
| 173 OperationID id = operations_.Add(operation); |
175 if (!operation) { | 174 if (!operation) { |
176 callback.Run(error, std::vector<DirectoryEntry>(), false); | 175 DidReadDirectory( |
177 return kErrorOperationID; | 176 id, callback, error, std::vector<DirectoryEntry>(), false); |
| 177 } else { |
| 178 PrepareForRead(id, url); |
| 179 operation->ReadDirectory( |
| 180 url, |
| 181 base::Bind(&FileSystemOperationRunner::DidReadDirectory, AsWeakPtr(), |
| 182 id, callback)); |
178 } | 183 } |
179 OperationID id = operations_.Add(operation); | |
180 PrepareForRead(id, url); | |
181 operation->ReadDirectory( | |
182 url, | |
183 base::Bind(&FileSystemOperationRunner::DidReadDirectory, AsWeakPtr(), | |
184 id, callback)); | |
185 return id; | 184 return id; |
186 } | 185 } |
187 | 186 |
188 OperationID FileSystemOperationRunner::Remove( | 187 OperationID FileSystemOperationRunner::Remove( |
189 const FileSystemURL& url, bool recursive, | 188 const FileSystemURL& url, bool recursive, |
190 const StatusCallback& callback) { | 189 const StatusCallback& callback) { |
191 base::PlatformFileError error = base::PLATFORM_FILE_OK; | 190 base::PlatformFileError error = base::PLATFORM_FILE_OK; |
192 FileSystemOperation* operation = | 191 FileSystemOperation* operation = |
193 file_system_context_->CreateFileSystemOperation(url, &error); | 192 file_system_context_->CreateFileSystemOperation(url, &error); |
| 193 OperationID id = operations_.Add(operation); |
194 if (!operation) { | 194 if (!operation) { |
195 callback.Run(error); | 195 DidFinish(id, callback, error); |
196 return kErrorOperationID; | 196 } else { |
| 197 PrepareForWrite(id, url); |
| 198 operation->Remove( |
| 199 url, recursive, |
| 200 base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), |
| 201 id, callback)); |
197 } | 202 } |
198 OperationID id = operations_.Add(operation); | |
199 PrepareForWrite(id, url); | |
200 operation->Remove( | |
201 url, recursive, | |
202 base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), | |
203 id, callback)); | |
204 return id; | 203 return id; |
205 } | 204 } |
206 | 205 |
207 OperationID FileSystemOperationRunner::Write( | 206 OperationID FileSystemOperationRunner::Write( |
208 const net::URLRequestContext* url_request_context, | 207 const net::URLRequestContext* url_request_context, |
209 const FileSystemURL& url, | 208 const FileSystemURL& url, |
210 const GURL& blob_url, | 209 const GURL& blob_url, |
211 int64 offset, | 210 int64 offset, |
212 const WriteCallback& callback) { | 211 const WriteCallback& callback) { |
213 base::PlatformFileError error = base::PLATFORM_FILE_OK; | 212 base::PlatformFileError error = base::PLATFORM_FILE_OK; |
214 FileSystemOperation* operation = | 213 FileSystemOperation* operation = |
215 file_system_context_->CreateFileSystemOperation(url, &error); | 214 file_system_context_->CreateFileSystemOperation(url, &error); |
| 215 OperationID id = operations_.Add(operation); |
| 216 |
216 if (!operation) { | 217 if (!operation) { |
217 callback.Run(error, 0, true); | 218 DidWrite(id, callback, error, 0, true); |
218 return kErrorOperationID; | 219 return id; |
219 } | 220 } |
220 | 221 |
221 scoped_ptr<FileStreamWriter> writer( | 222 scoped_ptr<FileStreamWriter> writer( |
222 file_system_context_->CreateFileStreamWriter(url, offset)); | 223 file_system_context_->CreateFileStreamWriter(url, offset)); |
223 if (!writer) { | 224 if (!writer) { |
224 // Write is not supported. | 225 // Write is not supported. |
225 callback.Run(base::PLATFORM_FILE_ERROR_SECURITY, 0, true); | 226 DidWrite(id, callback, base::PLATFORM_FILE_ERROR_SECURITY, 0, true); |
226 return kErrorOperationID; | 227 return id; |
227 } | 228 } |
228 | 229 |
229 DCHECK(blob_url.is_valid()); | 230 DCHECK(blob_url.is_valid()); |
230 scoped_ptr<FileWriterDelegate> writer_delegate( | 231 scoped_ptr<FileWriterDelegate> writer_delegate( |
231 new FileWriterDelegate(writer.Pass())); | 232 new FileWriterDelegate(writer.Pass())); |
232 scoped_ptr<net::URLRequest> blob_request(url_request_context->CreateRequest( | 233 scoped_ptr<net::URLRequest> blob_request(url_request_context->CreateRequest( |
233 blob_url, writer_delegate.get())); | 234 blob_url, writer_delegate.get())); |
234 | 235 |
235 OperationID id = operations_.Add(operation); | |
236 PrepareForWrite(id, url); | 236 PrepareForWrite(id, url); |
237 operation->Write( | 237 operation->Write( |
238 url, writer_delegate.Pass(), blob_request.Pass(), | 238 url, writer_delegate.Pass(), blob_request.Pass(), |
239 base::Bind(&FileSystemOperationRunner::DidWrite, AsWeakPtr(), | 239 base::Bind(&FileSystemOperationRunner::DidWrite, AsWeakPtr(), |
240 id, callback)); | 240 id, callback)); |
241 return id; | 241 return id; |
242 } | 242 } |
243 | 243 |
244 OperationID FileSystemOperationRunner::Truncate( | 244 OperationID FileSystemOperationRunner::Truncate( |
245 const FileSystemURL& url, int64 length, | 245 const FileSystemURL& url, int64 length, |
246 const StatusCallback& callback) { | 246 const StatusCallback& callback) { |
247 base::PlatformFileError error = base::PLATFORM_FILE_OK; | 247 base::PlatformFileError error = base::PLATFORM_FILE_OK; |
248 FileSystemOperation* operation = | 248 FileSystemOperation* operation = |
249 file_system_context_->CreateFileSystemOperation(url, &error); | 249 file_system_context_->CreateFileSystemOperation(url, &error); |
| 250 OperationID id = operations_.Add(operation); |
250 if (!operation) { | 251 if (!operation) { |
251 callback.Run(error); | 252 DidFinish(id, callback, error); |
252 return kErrorOperationID; | 253 } else { |
| 254 PrepareForWrite(id, url); |
| 255 operation->Truncate( |
| 256 url, length, |
| 257 base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), |
| 258 id, callback)); |
253 } | 259 } |
254 OperationID id = operations_.Add(operation); | |
255 PrepareForWrite(id, url); | |
256 operation->Truncate( | |
257 url, length, | |
258 base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), | |
259 id, callback)); | |
260 return id; | 260 return id; |
261 } | 261 } |
262 | 262 |
263 void FileSystemOperationRunner::Cancel( | 263 void FileSystemOperationRunner::Cancel( |
264 OperationID id, | 264 OperationID id, |
265 const StatusCallback& callback) { | 265 const StatusCallback& callback) { |
266 FileSystemOperation* operation = operations_.Lookup(id); | 266 FileSystemOperation* operation = operations_.Lookup(id); |
267 if (!operation) { | 267 if (!operation) { |
268 // The operation is already finished; report that we failed to stop it. | 268 // The operation is already finished; report that we failed to stop it. |
269 callback.Run(base::PLATFORM_FILE_ERROR_INVALID_OPERATION); | 269 callback.Run(base::PLATFORM_FILE_ERROR_INVALID_OPERATION); |
270 return; | 270 return; |
271 } | 271 } |
272 operation->Cancel(callback); | 272 operation->Cancel(callback); |
273 } | 273 } |
274 | 274 |
275 OperationID FileSystemOperationRunner::TouchFile( | 275 OperationID FileSystemOperationRunner::TouchFile( |
276 const FileSystemURL& url, | 276 const FileSystemURL& url, |
277 const base::Time& last_access_time, | 277 const base::Time& last_access_time, |
278 const base::Time& last_modified_time, | 278 const base::Time& last_modified_time, |
279 const StatusCallback& callback) { | 279 const StatusCallback& callback) { |
280 base::PlatformFileError error = base::PLATFORM_FILE_OK; | 280 base::PlatformFileError error = base::PLATFORM_FILE_OK; |
281 FileSystemOperation* operation = | 281 FileSystemOperation* operation = |
282 file_system_context_->CreateFileSystemOperation(url, &error); | 282 file_system_context_->CreateFileSystemOperation(url, &error); |
| 283 OperationID id = operations_.Add(operation); |
283 if (!operation) { | 284 if (!operation) { |
284 callback.Run(error); | 285 DidFinish(id, callback, error); |
285 return kErrorOperationID; | 286 } else { |
| 287 PrepareForWrite(id, url); |
| 288 operation->TouchFile( |
| 289 url, last_access_time, last_modified_time, |
| 290 base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), |
| 291 id, callback)); |
286 } | 292 } |
287 OperationID id = operations_.Add(operation); | |
288 PrepareForWrite(id, url); | |
289 operation->TouchFile( | |
290 url, last_access_time, last_modified_time, | |
291 base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), | |
292 id, callback)); | |
293 return id; | 293 return id; |
294 } | 294 } |
295 | 295 |
296 OperationID FileSystemOperationRunner::OpenFile( | 296 OperationID FileSystemOperationRunner::OpenFile( |
297 const FileSystemURL& url, | 297 const FileSystemURL& url, |
298 int file_flags, | 298 int file_flags, |
299 base::ProcessHandle peer_handle, | 299 base::ProcessHandle peer_handle, |
300 const OpenFileCallback& callback) { | 300 const OpenFileCallback& callback) { |
301 base::PlatformFileError error = base::PLATFORM_FILE_OK; | 301 base::PlatformFileError error = base::PLATFORM_FILE_OK; |
302 FileSystemOperation* operation = | 302 FileSystemOperation* operation = |
303 file_system_context_->CreateFileSystemOperation(url, &error); | 303 file_system_context_->CreateFileSystemOperation(url, &error); |
| 304 OperationID id = operations_.Add(operation); |
304 if (!operation) { | 305 if (!operation) { |
305 callback.Run(error, base::kInvalidPlatformFileValue, | 306 DidOpenFile(id, callback, error, base::kInvalidPlatformFileValue, |
306 base::Closure(), base::ProcessHandle()); | 307 base::Closure(), base::ProcessHandle()); |
307 return kErrorOperationID; | 308 return id; |
308 } | 309 } |
309 OperationID id = operations_.Add(operation); | |
310 if (file_flags & | 310 if (file_flags & |
311 (base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_OPEN_ALWAYS | | 311 (base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_OPEN_ALWAYS | |
312 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_OPEN_TRUNCATED | | 312 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_OPEN_TRUNCATED | |
313 base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_EXCLUSIVE_WRITE | | 313 base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_EXCLUSIVE_WRITE | |
314 base::PLATFORM_FILE_DELETE_ON_CLOSE | | 314 base::PLATFORM_FILE_DELETE_ON_CLOSE | |
315 base::PLATFORM_FILE_WRITE_ATTRIBUTES)) { | 315 base::PLATFORM_FILE_WRITE_ATTRIBUTES)) { |
316 PrepareForWrite(id, url); | 316 PrepareForWrite(id, url); |
317 } else { | 317 } else { |
318 PrepareForRead(id, url); | 318 PrepareForRead(id, url); |
319 } | 319 } |
320 operation->OpenFile( | 320 operation->OpenFile( |
321 url, file_flags, peer_handle, | 321 url, file_flags, peer_handle, |
322 base::Bind(&FileSystemOperationRunner::DidOpenFile, AsWeakPtr(), | 322 base::Bind(&FileSystemOperationRunner::DidOpenFile, AsWeakPtr(), |
323 id, callback)); | 323 id, callback)); |
324 return id; | 324 return id; |
325 } | 325 } |
326 | 326 |
327 OperationID FileSystemOperationRunner::CreateSnapshotFile( | 327 OperationID FileSystemOperationRunner::CreateSnapshotFile( |
328 const FileSystemURL& url, | 328 const FileSystemURL& url, |
329 const SnapshotFileCallback& callback) { | 329 const SnapshotFileCallback& callback) { |
330 base::PlatformFileError error = base::PLATFORM_FILE_OK; | 330 base::PlatformFileError error = base::PLATFORM_FILE_OK; |
331 FileSystemOperation* operation = | 331 FileSystemOperation* operation = |
332 file_system_context_->CreateFileSystemOperation(url, &error); | 332 file_system_context_->CreateFileSystemOperation(url, &error); |
| 333 OperationID id = operations_.Add(operation); |
333 if (!operation) { | 334 if (!operation) { |
334 callback.Run(error, base::PlatformFileInfo(), base::FilePath(), NULL); | 335 DidCreateSnapshot( |
335 return kErrorOperationID; | 336 id, callback, error, base::PlatformFileInfo(), base::FilePath(), NULL); |
| 337 } else { |
| 338 PrepareForRead(id, url); |
| 339 operation->CreateSnapshotFile( |
| 340 url, |
| 341 base::Bind(&FileSystemOperationRunner::DidCreateSnapshot, AsWeakPtr(), |
| 342 id, callback)); |
336 } | 343 } |
337 OperationID id = operations_.Add(operation); | |
338 PrepareForRead(id, url); | |
339 operation->CreateSnapshotFile( | |
340 url, | |
341 base::Bind(&FileSystemOperationRunner::DidCreateSnapshot, AsWeakPtr(), | |
342 id, callback)); | |
343 return id; | 344 return id; |
344 } | 345 } |
345 | 346 |
346 OperationID FileSystemOperationRunner::CopyInForeignFile( | 347 OperationID FileSystemOperationRunner::CopyInForeignFile( |
347 const base::FilePath& src_local_disk_path, | 348 const base::FilePath& src_local_disk_path, |
348 const FileSystemURL& dest_url, | 349 const FileSystemURL& dest_url, |
349 const StatusCallback& callback) { | 350 const StatusCallback& callback) { |
350 base::PlatformFileError error = base::PLATFORM_FILE_OK; | 351 base::PlatformFileError error = base::PLATFORM_FILE_OK; |
351 FileSystemOperation* operation = | 352 FileSystemOperation* operation = |
352 file_system_context_->CreateFileSystemOperation(dest_url, &error); | 353 file_system_context_->CreateFileSystemOperation(dest_url, &error); |
| 354 OperationID id = operations_.Add(operation); |
353 if (!operation) { | 355 if (!operation) { |
354 callback.Run(error); | 356 DidFinish(id, callback, error); |
355 return kErrorOperationID; | 357 } else { |
| 358 operation->CopyInForeignFile( |
| 359 src_local_disk_path, dest_url, |
| 360 base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), |
| 361 id, callback)); |
356 } | 362 } |
357 OperationID id = operations_.Add(operation); | |
358 operation->CopyInForeignFile( | |
359 src_local_disk_path, dest_url, | |
360 base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), | |
361 id, callback)); | |
362 return id; | 363 return id; |
363 } | 364 } |
364 | 365 |
365 OperationID FileSystemOperationRunner::RemoveFile( | 366 OperationID FileSystemOperationRunner::RemoveFile( |
366 const FileSystemURL& url, | 367 const FileSystemURL& url, |
367 const StatusCallback& callback) { | 368 const StatusCallback& callback) { |
368 base::PlatformFileError error = base::PLATFORM_FILE_OK; | 369 base::PlatformFileError error = base::PLATFORM_FILE_OK; |
369 FileSystemOperation* operation = | 370 FileSystemOperation* operation = |
370 file_system_context_->CreateFileSystemOperation(url, &error); | 371 file_system_context_->CreateFileSystemOperation(url, &error); |
| 372 OperationID id = operations_.Add(operation); |
371 if (!operation) { | 373 if (!operation) { |
372 callback.Run(error); | 374 DidFinish(id, callback, error); |
373 return kErrorOperationID; | 375 } else { |
| 376 operation->RemoveFile( |
| 377 url, |
| 378 base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), |
| 379 id, callback)); |
374 } | 380 } |
375 OperationID id = operations_.Add(operation); | |
376 operation->RemoveFile( | |
377 url, | |
378 base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), | |
379 id, callback)); | |
380 return id; | 381 return id; |
381 } | 382 } |
382 | 383 |
383 OperationID FileSystemOperationRunner::RemoveDirectory( | 384 OperationID FileSystemOperationRunner::RemoveDirectory( |
384 const FileSystemURL& url, | 385 const FileSystemURL& url, |
385 const StatusCallback& callback) { | 386 const StatusCallback& callback) { |
386 base::PlatformFileError error = base::PLATFORM_FILE_OK; | 387 base::PlatformFileError error = base::PLATFORM_FILE_OK; |
387 FileSystemOperation* operation = | 388 FileSystemOperation* operation = |
388 file_system_context_->CreateFileSystemOperation(url, &error); | 389 file_system_context_->CreateFileSystemOperation(url, &error); |
| 390 OperationID id = operations_.Add(operation); |
389 if (!operation) { | 391 if (!operation) { |
390 callback.Run(error); | 392 DidFinish(id, callback, error); |
391 return kErrorOperationID; | 393 } else { |
| 394 operation->RemoveDirectory( |
| 395 url, |
| 396 base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), |
| 397 id, callback)); |
392 } | 398 } |
393 OperationID id = operations_.Add(operation); | |
394 operation->RemoveDirectory( | |
395 url, | |
396 base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), | |
397 id, callback)); | |
398 return id; | 399 return id; |
399 } | 400 } |
400 | 401 |
401 OperationID FileSystemOperationRunner::CopyFileLocal( | 402 OperationID FileSystemOperationRunner::CopyFileLocal( |
402 const FileSystemURL& src_url, | 403 const FileSystemURL& src_url, |
403 const FileSystemURL& dest_url, | 404 const FileSystemURL& dest_url, |
404 const StatusCallback& callback) { | 405 const StatusCallback& callback) { |
405 base::PlatformFileError error = base::PLATFORM_FILE_OK; | 406 base::PlatformFileError error = base::PLATFORM_FILE_OK; |
406 FileSystemOperation* operation = | 407 FileSystemOperation* operation = |
407 file_system_context_->CreateFileSystemOperation(src_url, &error); | 408 file_system_context_->CreateFileSystemOperation(src_url, &error); |
| 409 OperationID id = operations_.Add(operation); |
408 if (!operation) { | 410 if (!operation) { |
409 callback.Run(error); | 411 DidFinish(id, callback, error); |
410 return kErrorOperationID; | 412 } else { |
| 413 operation->CopyFileLocal( |
| 414 src_url, dest_url, |
| 415 base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), |
| 416 id, callback)); |
411 } | 417 } |
412 OperationID id = operations_.Add(operation); | |
413 operation->CopyFileLocal( | |
414 src_url, dest_url, | |
415 base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), | |
416 id, callback)); | |
417 return id; | 418 return id; |
418 } | 419 } |
419 | 420 |
420 OperationID FileSystemOperationRunner::MoveFileLocal( | 421 OperationID FileSystemOperationRunner::MoveFileLocal( |
421 const FileSystemURL& src_url, | 422 const FileSystemURL& src_url, |
422 const FileSystemURL& dest_url, | 423 const FileSystemURL& dest_url, |
423 const StatusCallback& callback) { | 424 const StatusCallback& callback) { |
424 base::PlatformFileError error = base::PLATFORM_FILE_OK; | 425 base::PlatformFileError error = base::PLATFORM_FILE_OK; |
425 FileSystemOperation* operation = | 426 FileSystemOperation* operation = |
426 file_system_context_->CreateFileSystemOperation(src_url, &error); | 427 file_system_context_->CreateFileSystemOperation(src_url, &error); |
| 428 OperationID id = operations_.Add(operation); |
427 if (!operation) { | 429 if (!operation) { |
428 callback.Run(error); | 430 DidFinish(id, callback, error); |
429 return kErrorOperationID; | 431 } else { |
| 432 operation->MoveFileLocal( |
| 433 src_url, dest_url, |
| 434 base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), |
| 435 id, callback)); |
430 } | 436 } |
431 OperationID id = operations_.Add(operation); | |
432 operation->MoveFileLocal( | |
433 src_url, dest_url, | |
434 base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), | |
435 id, callback)); | |
436 return id; | 437 return id; |
437 } | 438 } |
438 | 439 |
439 base::PlatformFileError FileSystemOperationRunner::SyncGetPlatformPath( | 440 base::PlatformFileError FileSystemOperationRunner::SyncGetPlatformPath( |
440 const FileSystemURL& url, | 441 const FileSystemURL& url, |
441 base::FilePath* platform_path) { | 442 base::FilePath* platform_path) { |
442 base::PlatformFileError error = base::PLATFORM_FILE_OK; | 443 base::PlatformFileError error = base::PLATFORM_FILE_OK; |
443 FileSystemOperation* operation = | 444 FileSystemOperation* operation = |
444 file_system_context_->CreateFileSystemOperation(url, &error); | 445 file_system_context_->CreateFileSystemOperation(url, &error); |
445 if (!operation) | 446 if (!operation) |
446 return error; | 447 return error; |
447 return operation->SyncGetPlatformPath(url, platform_path); | 448 return operation->SyncGetPlatformPath(url, platform_path); |
448 } | 449 } |
449 | 450 |
450 FileSystemOperationRunner::FileSystemOperationRunner( | 451 FileSystemOperationRunner::FileSystemOperationRunner( |
451 FileSystemContext* file_system_context) | 452 FileSystemContext* file_system_context) |
452 : file_system_context_(file_system_context) {} | 453 : file_system_context_(file_system_context) {} |
453 | 454 |
454 void FileSystemOperationRunner::DidFinish( | 455 void FileSystemOperationRunner::DidFinish( |
455 OperationID id, | 456 OperationID id, |
456 const StatusCallback& callback, | 457 const StatusCallback& callback, |
457 base::PlatformFileError rv) { | 458 base::PlatformFileError rv) { |
458 callback.Run(rv); | 459 base::MessageLoopProxy::current()->PostTask( |
| 460 FROM_HERE, base::Bind(callback, rv)); |
459 FinishOperation(id); | 461 FinishOperation(id); |
460 } | 462 } |
461 | 463 |
462 void FileSystemOperationRunner::DidGetMetadata( | 464 void FileSystemOperationRunner::DidGetMetadata( |
463 OperationID id, | 465 OperationID id, |
464 const GetMetadataCallback& callback, | 466 const GetMetadataCallback& callback, |
465 base::PlatformFileError rv, | 467 base::PlatformFileError rv, |
466 const base::PlatformFileInfo& file_info) { | 468 const base::PlatformFileInfo& file_info) { |
467 callback.Run(rv, file_info); | 469 base::MessageLoopProxy::current()->PostTask( |
| 470 FROM_HERE, base::Bind(callback, rv, file_info)); |
468 FinishOperation(id); | 471 FinishOperation(id); |
469 } | 472 } |
470 | 473 |
471 void FileSystemOperationRunner::DidReadDirectory( | 474 void FileSystemOperationRunner::DidReadDirectory( |
472 OperationID id, | 475 OperationID id, |
473 const ReadDirectoryCallback& callback, | 476 const ReadDirectoryCallback& callback, |
474 base::PlatformFileError rv, | 477 base::PlatformFileError rv, |
475 const std::vector<DirectoryEntry>& entries, | 478 const std::vector<DirectoryEntry>& entries, |
476 bool has_more) { | 479 bool has_more) { |
477 callback.Run(rv, entries, has_more); | 480 base::MessageLoopProxy::current()->PostTask( |
| 481 FROM_HERE, base::Bind(callback, rv, entries, has_more)); |
478 if (rv != base::PLATFORM_FILE_OK || !has_more) | 482 if (rv != base::PLATFORM_FILE_OK || !has_more) |
479 FinishOperation(id); | 483 FinishOperation(id); |
480 } | 484 } |
481 | 485 |
482 void FileSystemOperationRunner::DidWrite( | 486 void FileSystemOperationRunner::DidWrite( |
483 OperationID id, | 487 OperationID id, |
484 const WriteCallback& callback, | 488 const WriteCallback& callback, |
485 base::PlatformFileError rv, | 489 base::PlatformFileError rv, |
486 int64 bytes, | 490 int64 bytes, |
487 bool complete) { | 491 bool complete) { |
488 callback.Run(rv, bytes, complete); | 492 base::MessageLoopProxy::current()->PostTask( |
| 493 FROM_HERE, base::Bind(callback, rv, bytes, complete)); |
489 if (rv != base::PLATFORM_FILE_OK || complete) | 494 if (rv != base::PLATFORM_FILE_OK || complete) |
490 FinishOperation(id); | 495 FinishOperation(id); |
491 } | 496 } |
492 | 497 |
493 void FileSystemOperationRunner::DidOpenFile( | 498 void FileSystemOperationRunner::DidOpenFile( |
494 OperationID id, | 499 OperationID id, |
495 const OpenFileCallback& callback, | 500 const OpenFileCallback& callback, |
496 base::PlatformFileError rv, | 501 base::PlatformFileError rv, |
497 base::PlatformFile file, | 502 base::PlatformFile file, |
498 const base::Closure& on_close_callback, | 503 const base::Closure& on_close_callback, |
499 base::ProcessHandle peer_handle) { | 504 base::ProcessHandle peer_handle) { |
500 callback.Run(rv, file, on_close_callback, peer_handle); | 505 base::MessageLoopProxy::current()->PostTask( |
| 506 FROM_HERE, |
| 507 base::Bind(callback, rv, file, on_close_callback, peer_handle)); |
501 FinishOperation(id); | 508 FinishOperation(id); |
502 } | 509 } |
503 | 510 |
504 void FileSystemOperationRunner::DidCreateSnapshot( | 511 void FileSystemOperationRunner::DidCreateSnapshot( |
505 OperationID id, | 512 OperationID id, |
506 const SnapshotFileCallback& callback, | 513 const SnapshotFileCallback& callback, |
507 base::PlatformFileError rv, | 514 base::PlatformFileError rv, |
508 const base::PlatformFileInfo& file_info, | 515 const base::PlatformFileInfo& file_info, |
509 const base::FilePath& platform_path, | 516 const base::FilePath& platform_path, |
510 const scoped_refptr<webkit_blob::ShareableFileReference>& file_ref) { | 517 const scoped_refptr<webkit_blob::ShareableFileReference>& file_ref) { |
511 callback.Run(rv, file_info, platform_path, file_ref); | 518 base::MessageLoopProxy::current()->PostTask( |
| 519 FROM_HERE, base::Bind(callback, rv, file_info, platform_path, file_ref)); |
512 FinishOperation(id); | 520 FinishOperation(id); |
513 } | 521 } |
514 | 522 |
515 void FileSystemOperationRunner::PrepareForWrite(OperationID id, | 523 void FileSystemOperationRunner::PrepareForWrite(OperationID id, |
516 const FileSystemURL& url) { | 524 const FileSystemURL& url) { |
517 if (file_system_context_->GetUpdateObservers(url.type())) { | 525 if (file_system_context_->GetUpdateObservers(url.type())) { |
518 file_system_context_->GetUpdateObservers(url.type())->Notify( | 526 file_system_context_->GetUpdateObservers(url.type())->Notify( |
519 &FileUpdateObserver::OnStartUpdate, MakeTuple(url)); | 527 &FileUpdateObserver::OnStartUpdate, MakeTuple(url)); |
520 } | 528 } |
521 write_target_urls_[id].insert(url); | 529 write_target_urls_[id].insert(url); |
(...skipping 18 matching lines...) Expand all Loading... |
540 &FileUpdateObserver::OnEndUpdate, MakeTuple(*iter)); | 548 &FileUpdateObserver::OnEndUpdate, MakeTuple(*iter)); |
541 } | 549 } |
542 } | 550 } |
543 write_target_urls_.erase(found); | 551 write_target_urls_.erase(found); |
544 } | 552 } |
545 DCHECK(operations_.Lookup(id)); | 553 DCHECK(operations_.Lookup(id)); |
546 operations_.Remove(id); | 554 operations_.Remove(id); |
547 } | 555 } |
548 | 556 |
549 } // namespace fileapi | 557 } // namespace fileapi |
OLD | NEW |