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