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 #ifndef WEBKIT_BROWSER_FILEAPI_FILE_STREAM_WRITER_H_ 6 #define WEBKIT_BROWSER_FILEAPI_FILE_STREAM_WRITER_H_ 7 8 #include "base/basictypes.h" 9 #include "net/base/completion_callback.h" 10 #include "webkit/browser/webkit_storage_browser_export.h" 11 12 namespace base { 13 class FilePath; 14 class TaskRunner; 15 } 16 17 namespace net { 18 class IOBuffer; 19 } 20 21 namespace fileapi { 22 23 // A generic interface for writing to a file-like object. 24 class FileStreamWriter { 25 public: 26 // Creates a writer for the existing file in the path |file_path| starting 27 // from |initial_offset|. Uses |task_runner| for async file operations. 28 WEBKIT_STORAGE_BROWSER_EXPORT static FileStreamWriter* CreateForLocalFile( 29 base::TaskRunner* task_runner, 30 const base::FilePath& file_path, 31 int64 initial_offset); 32 33 // Closes the file. If there's an in-flight operation, it is canceled (i.e., 34 // the callback function associated with the operation is not called). 35 virtual ~FileStreamWriter() {} 36 37 // Writes to the current cursor position asynchronously. 38 // 39 // Up to buf_len bytes will be written. (In other words, partial 40 // writes are allowed.) If the write completed synchronously, it returns 41 // the number of bytes written. If the operation could not be performed, it 42 // returns an error code. Otherwise, net::ERR_IO_PENDING is returned, and the 43 // callback will be run on the thread where Write() was called when the write 44 // has completed. 45 // 46 // This errors out (either synchronously or via callback) with: 47 // net::ERR_FILE_NOT_FOUND: When the target file is not found. 48 // net::ERR_ACCESS_DENIED: When the target file is a directory or 49 // the writer has no permission on the file. 50 // net::ERR_FILE_NO_SPACE: When the write will result in out of quota 51 // or there is not enough room left on the disk. 52 // 53 // It is invalid to call Write while there is an in-flight async operation. 54 virtual int Write(net::IOBuffer* buf, int buf_len, 55 const net::CompletionCallback& callback) = 0; 56 57 // Cancels an in-flight async operation. 58 // 59 // If the cancel is finished synchronously, it returns net::OK. If the 60 // cancel could not be performed, it returns an error code. Especially when 61 // there is no in-flight operation, net::ERR_UNEXPECTED is returned. 62 // Otherwise, net::ERR_IO_PENDING is returned, and the callback will be run on 63 // the thread where Cancel() was called when the cancel has completed. It is 64 // invalid to call Cancel() more than once on the same async operation. 65 // 66 // In either case, the callback function passed to the in-flight async 67 // operation is dismissed immediately when Cancel() is called, and thus 68 // will never be called. 69 virtual int Cancel(const net::CompletionCallback& callback) = 0; 70 71 // Flushes the data written so far. 72 // 73 // If the flush finished synchronously, it return net::OK. If the flush could 74 // not be performed, it returns an error code. Otherwise, net::ERR_IO_PENDING 75 // is returned, and the callback will be run on the thread where Flush() was 76 // called when the flush has completed. 77 // 78 // It is invalid to call Flush while there is an in-flight async operation. 79 virtual int Flush(const net::CompletionCallback& callback) = 0; 80 }; 81 82 } // namespace fileapi 83 84 #endif // WEBKIT_BROWSER_FILEAPI_FILE_STREAM_WRITER_H_ 85