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.
5 #ifndef STORAGE_BROWSER_FILEAPI_FILE_STREAM_WRITER_H_
6 #define STORAGE_BROWSER_FILEAPI_FILE_STREAM_WRITER_H_
8 #include "base/basictypes.h"
9 #include "net/base/completion_callback.h"
10 #include "storage/browser/storage_browser_export.h"
23 // A generic interface for writing to a file-like object.
24 class FileStreamWriter
{
26 enum OpenOrCreate
{ OPEN_EXISTING_FILE
, CREATE_NEW_FILE
};
28 // Creates a writer for the existing file in the path |file_path| starting
29 // from |initial_offset|. Uses |task_runner| for async file operations.
30 STORAGE_EXPORT
static FileStreamWriter
* CreateForLocalFile(
31 base::TaskRunner
* task_runner
,
32 const base::FilePath
& file_path
,
34 OpenOrCreate open_or_create
);
36 // Closes the file. If there's an in-flight operation, it is canceled (i.e.,
37 // the callback function associated with the operation is not called).
38 virtual ~FileStreamWriter() {}
40 // Writes to the current cursor position asynchronously.
42 // Up to buf_len bytes will be written. (In other words, partial
43 // writes are allowed.) If the write completed synchronously, it returns
44 // the number of bytes written. If the operation could not be performed, it
45 // returns an error code. Otherwise, net::ERR_IO_PENDING is returned, and the
46 // callback will be run on the thread where Write() was called when the write
49 // After the last write, Flush() must be called if the file system written to
50 // was registered with the FlushPolicy::FLUSH_ON_COMPLETION policy in mount
53 // This errors out (either synchronously or via callback) with:
54 // net::ERR_FILE_NOT_FOUND: When the target file is not found.
55 // net::ERR_ACCESS_DENIED: When the target file is a directory or
56 // the writer has no permission on the file.
57 // net::ERR_FILE_NO_SPACE: When the write will result in out of quota
58 // or there is not enough room left on the disk.
60 // It is invalid to call Write while there is an in-flight async operation.
61 virtual int Write(net::IOBuffer
* buf
, int buf_len
,
62 const net::CompletionCallback
& callback
) = 0;
64 // Cancels an in-flight async operation.
66 // If the cancel is finished synchronously, it returns net::OK. If the
67 // cancel could not be performed, it returns an error code. Especially when
68 // there is no in-flight operation, net::ERR_UNEXPECTED is returned.
69 // Otherwise, net::ERR_IO_PENDING is returned, and the callback will be run on
70 // the thread where Cancel() was called when the cancel has completed. It is
71 // invalid to call Cancel() more than once on the same async operation.
73 // In either case, the callback function passed to the in-flight async
74 // operation is dismissed immediately when Cancel() is called, and thus
75 // will never be called.
76 virtual int Cancel(const net::CompletionCallback
& callback
) = 0;
78 // Flushes the data written so far.
80 // If the flush finished synchronously, it return net::OK. If the flush could
81 // not be performed, it returns an error code. Otherwise, net::ERR_IO_PENDING
82 // is returned, and the callback will be run on the thread where Flush() was
83 // called when the flush has completed.
85 // It is invalid to call Flush while there is an in-flight async operation.
86 virtual int Flush(const net::CompletionCallback
& callback
) = 0;
89 } // namespace storage
91 #endif // STORAGE_BROWSER_FILEAPI_FILE_STREAM_WRITER_H_