Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(162)

Side by Side Diff: base/files/file_util_proxy.cc

Issue 17779003: Port base/files/file_util_proxy to Native Client. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Move NaCl workaround into platform_file_posix.cc. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/base.gypi ('k') | base/platform_file.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "base/files/file_util_proxy.h" 5 #include "base/files/file_util_proxy.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/location.h" 10 #include "base/location.h"
11 #include "base/message_loop/message_loop_proxy.h" 11 #include "base/message_loop/message_loop_proxy.h"
12 #include "base/task_runner.h" 12 #include "base/task_runner.h"
13 #include "base/task_runner_util.h" 13 #include "base/task_runner_util.h"
14 14
15 namespace base { 15 namespace base {
16 16
17 namespace { 17 namespace {
18 18
19 void CallWithTranslatedParameter(const FileUtilProxy::StatusCallback& callback, 19 void CallWithTranslatedParameter(const FileUtilProxy::StatusCallback& callback,
20 bool value) { 20 bool value) {
21 DCHECK(!callback.is_null()); 21 DCHECK(!callback.is_null());
22 callback.Run(value ? PLATFORM_FILE_OK : PLATFORM_FILE_ERROR_FAILED); 22 callback.Run(value ? PLATFORM_FILE_OK : PLATFORM_FILE_ERROR_FAILED);
23 } 23 }
24 24
25 #if !defined(OS_NACL)
25 // Helper classes or routines for individual methods. 26 // Helper classes or routines for individual methods.
26 class CreateOrOpenHelper { 27 class CreateOrOpenHelper {
27 public: 28 public:
28 CreateOrOpenHelper(TaskRunner* task_runner, 29 CreateOrOpenHelper(TaskRunner* task_runner,
29 const FileUtilProxy::CloseTask& close_task) 30 const FileUtilProxy::CloseTask& close_task)
30 : task_runner_(task_runner), 31 : task_runner_(task_runner),
31 close_task_(close_task), 32 close_task_(close_task),
32 file_handle_(kInvalidPlatformFileValue), 33 file_handle_(kInvalidPlatformFileValue),
33 created_(false), 34 created_(false),
34 error_(PLATFORM_FILE_OK) {} 35 error_(PLATFORM_FILE_OK) {}
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 callback.Run(error_, PassPlatformFile(&file_handle_), file_path_); 94 callback.Run(error_, PassPlatformFile(&file_handle_), file_path_);
94 } 95 }
95 96
96 private: 97 private:
97 scoped_refptr<TaskRunner> task_runner_; 98 scoped_refptr<TaskRunner> task_runner_;
98 PlatformFile file_handle_; 99 PlatformFile file_handle_;
99 FilePath file_path_; 100 FilePath file_path_;
100 PlatformFileError error_; 101 PlatformFileError error_;
101 DISALLOW_COPY_AND_ASSIGN(CreateTemporaryHelper); 102 DISALLOW_COPY_AND_ASSIGN(CreateTemporaryHelper);
102 }; 103 };
104 #endif // !defined(OS_NACL)
103 105
104 class GetFileInfoHelper { 106 class GetFileInfoHelper {
105 public: 107 public:
106 GetFileInfoHelper() 108 GetFileInfoHelper()
107 : error_(PLATFORM_FILE_OK) {} 109 : error_(PLATFORM_FILE_OK) {}
108 110
111 #if !defined(OS_NACL)
109 void RunWorkForFilePath(const FilePath& file_path) { 112 void RunWorkForFilePath(const FilePath& file_path) {
110 if (!file_util::PathExists(file_path)) { 113 if (!file_util::PathExists(file_path)) {
111 error_ = PLATFORM_FILE_ERROR_NOT_FOUND; 114 error_ = PLATFORM_FILE_ERROR_NOT_FOUND;
112 return; 115 return;
113 } 116 }
114 if (!file_util::GetFileInfo(file_path, &file_info_)) 117 if (!file_util::GetFileInfo(file_path, &file_info_))
115 error_ = PLATFORM_FILE_ERROR_FAILED; 118 error_ = PLATFORM_FILE_ERROR_FAILED;
116 } 119 }
120 #endif // !defined(OS_NACL)
117 121
118 void RunWorkForPlatformFile(PlatformFile file) { 122 void RunWorkForPlatformFile(PlatformFile file) {
119 if (!GetPlatformFileInfo(file, &file_info_)) 123 if (!GetPlatformFileInfo(file, &file_info_))
120 error_ = PLATFORM_FILE_ERROR_FAILED; 124 error_ = PLATFORM_FILE_ERROR_FAILED;
121 } 125 }
122 126
123 void Reply(const FileUtilProxy::GetFileInfoCallback& callback) { 127 void Reply(const FileUtilProxy::GetFileInfoCallback& callback) {
124 if (!callback.is_null()) { 128 if (!callback.is_null()) {
125 callback.Run(error_, file_info_); 129 callback.Run(error_, file_info_);
126 } 130 }
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 } 184 }
181 } 185 }
182 186
183 private: 187 private:
184 scoped_ptr<char[]> buffer_; 188 scoped_ptr<char[]> buffer_;
185 int bytes_to_write_; 189 int bytes_to_write_;
186 int bytes_written_; 190 int bytes_written_;
187 DISALLOW_COPY_AND_ASSIGN(WriteHelper); 191 DISALLOW_COPY_AND_ASSIGN(WriteHelper);
188 }; 192 };
189 193
190 194 #if !defined(OS_NACL)
191 PlatformFileError CreateOrOpenAdapter( 195 PlatformFileError CreateOrOpenAdapter(
192 const FilePath& file_path, int file_flags, 196 const FilePath& file_path, int file_flags,
193 PlatformFile* file_handle, bool* created) { 197 PlatformFile* file_handle, bool* created) {
194 DCHECK(file_handle); 198 DCHECK(file_handle);
195 DCHECK(created); 199 DCHECK(created);
196 if (!file_util::DirectoryExists(file_path.DirName())) { 200 if (!file_util::DirectoryExists(file_path.DirName())) {
197 // If its parent does not exist, should return NOT_FOUND error. 201 // If its parent does not exist, should return NOT_FOUND error.
198 return PLATFORM_FILE_ERROR_NOT_FOUND; 202 return PLATFORM_FILE_ERROR_NOT_FOUND;
199 } 203 }
200 PlatformFileError error = PLATFORM_FILE_OK; 204 PlatformFileError error = PLATFORM_FILE_OK;
201 *file_handle = CreatePlatformFile(file_path, file_flags, created, &error); 205 *file_handle = CreatePlatformFile(file_path, file_flags, created, &error);
202 return error; 206 return error;
203 } 207 }
204 208
209 #endif // !defined(OS_NACL)
brettw 2013/07/10 21:35:18 This should go one line higher
bbudge 2013/07/10 22:09:39 Done.
205 PlatformFileError CloseAdapter(PlatformFile file_handle) { 210 PlatformFileError CloseAdapter(PlatformFile file_handle) {
206 if (!ClosePlatformFile(file_handle)) { 211 if (!ClosePlatformFile(file_handle)) {
207 return PLATFORM_FILE_ERROR_FAILED; 212 return PLATFORM_FILE_ERROR_FAILED;
208 } 213 }
209 return PLATFORM_FILE_OK; 214 return PLATFORM_FILE_OK;
210 } 215 }
211 216
217 #if !defined(OS_NACL)
212 PlatformFileError DeleteAdapter(const FilePath& file_path, bool recursive) { 218 PlatformFileError DeleteAdapter(const FilePath& file_path, bool recursive) {
213 if (!file_util::PathExists(file_path)) { 219 if (!file_util::PathExists(file_path)) {
214 return PLATFORM_FILE_ERROR_NOT_FOUND; 220 return PLATFORM_FILE_ERROR_NOT_FOUND;
215 } 221 }
216 if (!base::Delete(file_path, recursive)) { 222 if (!base::Delete(file_path, recursive)) {
217 if (!recursive && !file_util::IsDirectoryEmpty(file_path)) { 223 if (!recursive && !file_util::IsDirectoryEmpty(file_path)) {
218 return PLATFORM_FILE_ERROR_NOT_EMPTY; 224 return PLATFORM_FILE_ERROR_NOT_EMPTY;
219 } 225 }
220 return PLATFORM_FILE_ERROR_FAILED; 226 return PLATFORM_FILE_ERROR_FAILED;
221 } 227 }
222 return PLATFORM_FILE_OK; 228 return PLATFORM_FILE_OK;
223 } 229 }
230 #endif // !defined(OS_NACL)
224 231
225 } // namespace 232 } // namespace
226 233
234 #if !defined(OS_NACL)
227 // static 235 // static
228 bool FileUtilProxy::CreateOrOpen( 236 bool FileUtilProxy::CreateOrOpen(
229 TaskRunner* task_runner, 237 TaskRunner* task_runner,
230 const FilePath& file_path, int file_flags, 238 const FilePath& file_path, int file_flags,
231 const CreateOrOpenCallback& callback) { 239 const CreateOrOpenCallback& callback) {
232 return RelayCreateOrOpen( 240 return RelayCreateOrOpen(
233 task_runner, 241 task_runner,
234 base::Bind(&CreateOrOpenAdapter, file_path, file_flags), 242 base::Bind(&CreateOrOpenAdapter, file_path, file_flags),
235 base::Bind(&CloseAdapter), 243 base::Bind(&CloseAdapter),
236 callback); 244 callback);
237 } 245 }
238 246
239 // static 247 // static
240 bool FileUtilProxy::CreateTemporary( 248 bool FileUtilProxy::CreateTemporary(
241 TaskRunner* task_runner, 249 TaskRunner* task_runner,
242 int additional_file_flags, 250 int additional_file_flags,
243 const CreateTemporaryCallback& callback) { 251 const CreateTemporaryCallback& callback) {
244 CreateTemporaryHelper* helper = new CreateTemporaryHelper(task_runner); 252 CreateTemporaryHelper* helper = new CreateTemporaryHelper(task_runner);
245 return task_runner->PostTaskAndReply( 253 return task_runner->PostTaskAndReply(
246 FROM_HERE, 254 FROM_HERE,
247 Bind(&CreateTemporaryHelper::RunWork, Unretained(helper), 255 Bind(&CreateTemporaryHelper::RunWork, Unretained(helper),
248 additional_file_flags), 256 additional_file_flags),
249 Bind(&CreateTemporaryHelper::Reply, Owned(helper), callback)); 257 Bind(&CreateTemporaryHelper::Reply, Owned(helper), callback));
250 } 258 }
259 #endif // !defined(OS_NACL)
251 260
252 // static 261 // static
253 bool FileUtilProxy::Close( 262 bool FileUtilProxy::Close(
254 TaskRunner* task_runner, 263 TaskRunner* task_runner,
255 base::PlatformFile file_handle, 264 base::PlatformFile file_handle,
256 const StatusCallback& callback) { 265 const StatusCallback& callback) {
257 return RelayClose( 266 return RelayClose(
258 task_runner, 267 task_runner,
259 base::Bind(&CloseAdapter), 268 base::Bind(&CloseAdapter),
260 file_handle, callback); 269 file_handle, callback);
261 } 270 }
262 271
272 #if !defined(OS_NACL)
263 // Retrieves the information about a file. It is invalid to pass NULL for the 273 // Retrieves the information about a file. It is invalid to pass NULL for the
264 // callback. 274 // callback.
265 bool FileUtilProxy::GetFileInfo( 275 bool FileUtilProxy::GetFileInfo(
266 TaskRunner* task_runner, 276 TaskRunner* task_runner,
267 const FilePath& file_path, 277 const FilePath& file_path,
268 const GetFileInfoCallback& callback) { 278 const GetFileInfoCallback& callback) {
269 GetFileInfoHelper* helper = new GetFileInfoHelper; 279 GetFileInfoHelper* helper = new GetFileInfoHelper;
270 return task_runner->PostTaskAndReply( 280 return task_runner->PostTaskAndReply(
271 FROM_HERE, 281 FROM_HERE,
272 Bind(&GetFileInfoHelper::RunWorkForFilePath, 282 Bind(&GetFileInfoHelper::RunWorkForFilePath,
273 Unretained(helper), file_path), 283 Unretained(helper), file_path),
274 Bind(&GetFileInfoHelper::Reply, Owned(helper), callback)); 284 Bind(&GetFileInfoHelper::Reply, Owned(helper), callback));
275 } 285 }
286 #endif // !defined(OS_NACL)
276 287
277 // static 288 // static
278 bool FileUtilProxy::GetFileInfoFromPlatformFile( 289 bool FileUtilProxy::GetFileInfoFromPlatformFile(
279 TaskRunner* task_runner, 290 TaskRunner* task_runner,
280 PlatformFile file, 291 PlatformFile file,
281 const GetFileInfoCallback& callback) { 292 const GetFileInfoCallback& callback) {
282 GetFileInfoHelper* helper = new GetFileInfoHelper; 293 GetFileInfoHelper* helper = new GetFileInfoHelper;
283 return task_runner->PostTaskAndReply( 294 return task_runner->PostTaskAndReply(
284 FROM_HERE, 295 FROM_HERE,
285 Bind(&GetFileInfoHelper::RunWorkForPlatformFile, 296 Bind(&GetFileInfoHelper::RunWorkForPlatformFile,
286 Unretained(helper), file), 297 Unretained(helper), file),
287 Bind(&GetFileInfoHelper::Reply, Owned(helper), callback)); 298 Bind(&GetFileInfoHelper::Reply, Owned(helper), callback));
288 } 299 }
289 300
301 #if !defined(OS_NACL)
290 // static 302 // static
291 bool FileUtilProxy::Delete(TaskRunner* task_runner, 303 bool FileUtilProxy::Delete(TaskRunner* task_runner,
292 const FilePath& file_path, 304 const FilePath& file_path,
293 bool recursive, 305 bool recursive,
294 const StatusCallback& callback) { 306 const StatusCallback& callback) {
295 return base::PostTaskAndReplyWithResult( 307 return base::PostTaskAndReplyWithResult(
296 task_runner, FROM_HERE, 308 task_runner, FROM_HERE,
297 Bind(&DeleteAdapter, file_path, recursive), 309 Bind(&DeleteAdapter, file_path, recursive),
298 callback); 310 callback);
299 } 311 }
300 312
301 // static 313 // static
302 bool FileUtilProxy::RecursiveDelete( 314 bool FileUtilProxy::RecursiveDelete(
303 TaskRunner* task_runner, 315 TaskRunner* task_runner,
304 const FilePath& file_path, 316 const FilePath& file_path,
305 const StatusCallback& callback) { 317 const StatusCallback& callback) {
306 return base::PostTaskAndReplyWithResult( 318 return base::PostTaskAndReplyWithResult(
307 task_runner, FROM_HERE, 319 task_runner, FROM_HERE,
308 Bind(&DeleteAdapter, file_path, true /* recursive */), 320 Bind(&DeleteAdapter, file_path, true /* recursive */),
309 callback); 321 callback);
310 } 322 }
323 #endif // !defined(OS_NACL)
311 324
312 // static 325 // static
313 bool FileUtilProxy::Read( 326 bool FileUtilProxy::Read(
314 TaskRunner* task_runner, 327 TaskRunner* task_runner,
315 PlatformFile file, 328 PlatformFile file,
316 int64 offset, 329 int64 offset,
317 int bytes_to_read, 330 int bytes_to_read,
318 const ReadCallback& callback) { 331 const ReadCallback& callback) {
319 if (bytes_to_read < 0) { 332 if (bytes_to_read < 0) {
320 return false; 333 return false;
(...skipping 16 matching lines...) Expand all
337 if (bytes_to_write <= 0 || buffer == NULL) { 350 if (bytes_to_write <= 0 || buffer == NULL) {
338 return false; 351 return false;
339 } 352 }
340 WriteHelper* helper = new WriteHelper(buffer, bytes_to_write); 353 WriteHelper* helper = new WriteHelper(buffer, bytes_to_write);
341 return task_runner->PostTaskAndReply( 354 return task_runner->PostTaskAndReply(
342 FROM_HERE, 355 FROM_HERE,
343 Bind(&WriteHelper::RunWork, Unretained(helper), file, offset), 356 Bind(&WriteHelper::RunWork, Unretained(helper), file, offset),
344 Bind(&WriteHelper::Reply, Owned(helper), callback)); 357 Bind(&WriteHelper::Reply, Owned(helper), callback));
345 } 358 }
346 359
360 #if !defined(OS_NACL)
347 // static 361 // static
348 bool FileUtilProxy::Touch( 362 bool FileUtilProxy::Touch(
349 TaskRunner* task_runner, 363 TaskRunner* task_runner,
350 PlatformFile file, 364 PlatformFile file,
351 const Time& last_access_time, 365 const Time& last_access_time,
352 const Time& last_modified_time, 366 const Time& last_modified_time,
353 const StatusCallback& callback) { 367 const StatusCallback& callback) {
354 return base::PostTaskAndReplyWithResult( 368 return base::PostTaskAndReplyWithResult(
355 task_runner, 369 task_runner,
356 FROM_HERE, 370 FROM_HERE,
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 const CreateOrOpenTask& open_task, 419 const CreateOrOpenTask& open_task,
406 const CloseTask& close_task, 420 const CloseTask& close_task,
407 const CreateOrOpenCallback& callback) { 421 const CreateOrOpenCallback& callback) {
408 CreateOrOpenHelper* helper = new CreateOrOpenHelper( 422 CreateOrOpenHelper* helper = new CreateOrOpenHelper(
409 task_runner, close_task); 423 task_runner, close_task);
410 return task_runner->PostTaskAndReply( 424 return task_runner->PostTaskAndReply(
411 FROM_HERE, 425 FROM_HERE,
412 Bind(&CreateOrOpenHelper::RunWork, Unretained(helper), open_task), 426 Bind(&CreateOrOpenHelper::RunWork, Unretained(helper), open_task),
413 Bind(&CreateOrOpenHelper::Reply, Owned(helper), callback)); 427 Bind(&CreateOrOpenHelper::Reply, Owned(helper), callback));
414 } 428 }
429 #endif // !defined(OS_NACL)
415 430
416 // static 431 // static
417 bool FileUtilProxy::RelayClose( 432 bool FileUtilProxy::RelayClose(
418 TaskRunner* task_runner, 433 TaskRunner* task_runner,
419 const CloseTask& close_task, 434 const CloseTask& close_task,
420 PlatformFile file_handle, 435 PlatformFile file_handle,
421 const StatusCallback& callback) { 436 const StatusCallback& callback) {
422 return base::PostTaskAndReplyWithResult( 437 return base::PostTaskAndReplyWithResult(
423 task_runner, FROM_HERE, Bind(close_task, file_handle), callback); 438 task_runner, FROM_HERE, Bind(close_task, file_handle), callback);
424 } 439 }
425 440
426 } // namespace base 441 } // namespace base
OLDNEW
« no previous file with comments | « base/base.gypi ('k') | base/platform_file.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698